summaryrefslogtreecommitdiffstats
path: root/src/Daemon/CrashWatcher.cpp
blob: 1cf6e1f03694e5e19b71a6398d96719352fe18a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* 
    Copyright (C) 2009  Jiri Moskovcak (jmoskovc@redhat.com) 
    Copyright (C) 2009  RedHat inc. 
 
    This program is free software; you can redistribute it and/or modify 
    it under the terms of the GNU General Public License as published by 
    the Free Software Foundation; either version 2 of the License, or 
    (at your option) any later version. 
 
    This program is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    GNU General Public License for more details. 
 
    You should have received a copy of the GNU General Public License along 
    with this program; if not, write to the Free Software Foundation, Inc., 
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
    */
    
#include "CrashWatcher.h"
#include <unistd.h>
#include <iostream>
#include <climits>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>
#include <csignal>
#include <sstream>

/* just a helper function */
template< class T >
std::string
to_string( T x )
{
    std::ostringstream o;
    o << x;
    return o.str();
}

gboolean CCrashWatcher::handle_event_cb(GIOChannel *gio, GIOCondition condition, gpointer daemon){
    GIOError err;
    char buf[INOTIFY_BUFF_SIZE];
    gsize len;
    int i = 0;
    err = g_io_channel_read (gio, buf, INOTIFY_BUFF_SIZE, &len);
    if (err != G_IO_ERROR_NONE) {
            g_warning ("Error reading inotify fd: %d\n", err);
            return FALSE;
    }
    /* reconstruct each event and send message to the dbus */
    while (i < len) {
        const char *name;
        struct inotify_event *event;

        event = (struct inotify_event *) &buf[i];
        if (event->len)
                name = &buf[i] + sizeof (struct inotify_event);
        i += sizeof (struct inotify_event) + event->len;
#ifdef DEBUG
        std::cout << "Created file: " << name << std::endl;
#endif /*DEBUG*/
     
        /* we want to ignore the lock files */
        if(event->mask & IN_ISDIR)
        {
            std::string sName = name;
            CCrashWatcher *cc = (CCrashWatcher*)daemon;
            crash_info_t crashinfo;
            if(cc->m_pMW->SaveDebugDump(std::string(DEBUG_DUMPS_DIR) + "/" + name, crashinfo))
            {
                /* send message to dbus */
                cc->Crash(crashinfo.m_sPackage);
            }
        }
#ifdef DEBUG
        else
        {
            std::cerr << "Some file created, ignoring.." << std::endl;
        }
#endif /*DEBUG*/
    }
    return TRUE;
}

CCrashWatcher::CCrashWatcher(const std::string& pPath,DBus::Connection &connection)
: DBus::ObjectAdaptor(connection, CC_DBUS_PATH)
{
    m_pConn = &connection;
    int watch = 0;
    m_sTarget = pPath;
    // middleware object
    m_pMW = new CMiddleWare(PLUGINS_CONF_DIR,PLUGINS_LIB_DIR, std::string(CONF_DIR) + "/crash-catcher.conf");
    m_nMainloop = g_main_loop_new(NULL,FALSE);
    connection.request_name(CC_DBUS_NAME);
    if((m_nFd = inotify_init()) == -1){
        throw std::string("Init Failed");
        //std::cerr << "Init Failed" << std::endl;
        exit(-1);
    }
    if((watch = inotify_add_watch(m_nFd, pPath.c_str(), IN_CREATE)) == -1){
        
        throw std::string("Add watch failed:") + pPath.c_str();
    }
    m_nGio = g_io_channel_unix_new(m_nFd);
}

CCrashWatcher::~CCrashWatcher()
{
     //delete dispatcher, connection, etc..
     delete m_pMW;
}

dbus_vector_crash_infos_t CCrashWatcher::GetCrashInfos(const std::string &pUID)
{
    dbus_vector_crash_infos_t retval;
    vector_crash_infos_t crash_info; 
    m_pMW->GetCrashInfos("501");
    for (vector_crash_infos_t::iterator it = crash_info.begin(); it!=crash_info.end(); ++it) {
        std::cerr << it->m_sExecutable << std::endl;
    }
	return retval;
}

dbus_vector_map_crash_infos_t CCrashWatcher::GetCrashInfosMap(const std::string &pDBusSender)
{
    dbus_vector_map_crash_infos_t retval;
    vector_crash_infos_t crash_info;
    unsigned long unix_uid = m_pConn->sender_unix_uid(pDBusSender.c_str());
    crash_info = m_pMW->GetCrashInfos(to_string(unix_uid));
    for (vector_crash_infos_t::iterator it = crash_info.begin(); it!=crash_info.end(); ++it) {
        retval.push_back(it->GetMap());
    }
	return retval;
}

dbus_map_report_info_t CCrashWatcher::CreateReport(const std::string &pUUID,const std::string &pDBusSender)
{
    dbus_map_report_info_t retval;
    unsigned long unix_uid = m_pConn->sender_unix_uid(pDBusSender.c_str());
    std::cerr << pUUID << ":" << unix_uid << std::endl;
    crash_report_t crashReport;
    std::cerr << "Creating report" << std::endl;
    m_pMW->CreateReport(pUUID,to_string(unix_uid), crashReport);
    retval = crashReport.GetMap();
    return retval;
}

bool CCrashWatcher::Report(dbus_map_report_info_t pReport)
{
    crash_report_t crashReport;
    //#define FIELD(X) crashReport.m_s##X = pReport[#X];
    //crashReport.m_sUUID = pReport["UUID"];
    //ALL_CRASH_REPORT_FIELDS;
    //#undef FIELD
    //for (dbus_map_report_info_t::iterator it = pReport.begin(); it!=pReport.end(); ++it) {
    //     std::cerr << it->second << std::endl;
    //}
    crashReport.SetFromMap(pReport);
    std::cerr << crashReport.m_sPackage << std::endl;
    m_pMW->Report(crashReport);
    return true;
}

void CCrashWatcher::Lock()
{
    int lfp = open("crashcatcher.lock",O_RDWR|O_CREAT,0640);
	if (lfp < 0) 
        throw std::string("CCrashWatcher.cpp:can not open lock file");
	if (lockf(lfp,F_TLOCK,0) < 0)
        throw std::string("CCrashWatcher.cpp:Lock:cannot create lock on lockfile");
	/* only first instance continues */
	//sprintf(str,"%d\n",getpid());
	//write(lfp,str,strlen(str)); /* record pid to lockfile */
}

void CCrashWatcher::StartWatch()
{
    char *buff = new char[INOTIFY_BUFF_SIZE];
    int len = 0;
    int i = 0;
    char action[FILENAME_MAX];
    struct inotify_event *pevent;
    //run forever
    while(1){
        i = 0;
        len = read(m_nFd,buff,INOTIFY_BUFF_SIZE);
        while(i < len){
            pevent = (struct inotify_event *)&buff[i];
            if (pevent->len) 
                std::strcpy(action, pevent->name);
            else
                std::strcpy(action, m_sTarget.c_str());
            i += sizeof(struct inotify_event) + pevent->len;
#ifdef DEBUG
            std::cout << "Created file: " << action << std::endl;
#endif /*DEBUG*/
        }
    }
    delete[] buff;
}

/* daemon loop with glib */
void CCrashWatcher::GStartWatch()
{
    char action[FILENAME_MAX];
    struct inotify_event *pevent;
    g_io_add_watch (m_nGio, G_IO_IN, handle_event_cb, this);
    //enter the event loop
    g_main_run (m_nMainloop);
}


void CCrashWatcher::Daemonize()
{
#ifdef DEBUG
    std::cout << "Daemonize" << std::endl;
#endif
    // forking to background
    pid_t pid = fork();
	if (pid < 0)
    {
        throw "CCrashWatcher.cpp:Daemonize:Fork error";
    }
    /* parent exits */
	if (pid > 0) _exit(0);
	/* child (daemon) continues */
    pid_t sid = setsid();
    if(sid == -1){
        throw "CCrashWatcher.cpp:Daemonize:setsid failed";
    }
    //Lock();
    GStartWatch();
}

void CCrashWatcher::Run()
{
#ifdef DEBUG
    std::cout << "Run" << std::endl;
#endif
    Lock();
    GStartWatch();
}