summaryrefslogtreecommitdiffstats
path: root/src/Daemon/CrashWatcher.cpp
blob: 89e9fe9a1ef6b0a7e6903f8a1c385c63453ddfea (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
    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 "abrtlib.h"
#include "CrashWatcher.h"
#include <iostream>
#include <sstream>
#include "ABRTException.h"

void CCrashWatcher::Status(const std::string& pMessage, const std::string& pDest)
{
    std::cout << "Update: " + pMessage << std::endl;
    //FIXME: send updates only to job owner
    if (g_pCommLayer != NULL)
        g_pCommLayer->Update(pDest, pMessage);
}

void CCrashWatcher::Warning(const std::string& pMessage, const std::string& pDest)
{
    std::cerr << "Warning: " + pMessage << std::endl;
    if (g_pCommLayer != NULL)
        g_pCommLayer->Warning(pDest, pMessage);
}

void CCrashWatcher::Debug(const std::string& pMessage, const std::string& pDest)
{
    //some logic to add logging levels?
    std::cout << "Debug: " + pMessage << std::endl;
}

CCrashWatcher::CCrashWatcher()
{
    g_cw = this;
}

CCrashWatcher::~CCrashWatcher()
{
}

vector_crash_infos_t CCrashWatcher::GetCrashInfos(const std::string &pUID)
{
    vector_crash_infos_t retval;
    Debug("Getting crash infos...");
    try
    {
        vector_pair_string_string_t UUIDsUIDs;
        UUIDsUIDs = GetUUIDsOfCrash(pUID);

        unsigned int ii;
        for (ii = 0; ii < UUIDsUIDs.size(); ii++)
        {
            mw_result_t res;
            map_crash_info_t info;

            res = GetCrashInfo(UUIDsUIDs[ii].first, UUIDsUIDs[ii].second, info);
            switch (res)
            {
                case MW_OK:
                    retval.push_back(info);
                    break;
                case MW_ERROR:
                    Warning("Can not find debug dump directory for UUID: " + UUIDsUIDs[ii].first + ", deleting from database");
                    Status("Can not find debug dump directory for UUID: " + UUIDsUIDs[ii].first + ", deleting from database");
                    DeleteCrashInfo(UUIDsUIDs[ii].first, UUIDsUIDs[ii].second);
                    break;
                case MW_FILE_ERROR:
                    {
                        std::string debugDumpDir;
                        Warning("Can not open file in debug dump directory for UUID: " + UUIDsUIDs[ii].first + ", deleting ");
                        Status("Can not open file in debug dump directory for UUID: " + UUIDsUIDs[ii].first + ", deleting ");
                        debugDumpDir = DeleteCrashInfo(UUIDsUIDs[ii].first, UUIDsUIDs[ii].second);
                        DeleteDebugDumpDir(debugDumpDir);
                    }
                    break;
                default:
                    break;
            }
        }
    }
    catch (CABRTException& e)
    {
        if (e.type() == EXCEP_FATAL)
        {
            throw e;
        }
        Warning(e.what());
        Status(e.what());
    }

    //retval = GetCrashInfos(pUID);
    //Notify("Sent crash info");
    return retval;
}

typedef struct thread_data_t {
    pthread_t thread_id;
    char* UUID;
    char* UID;
    char *dest;
} thread_data_t;
static void *create_report(void *arg)
{
    thread_data_t *thread_data = (thread_data_t *) arg;
    map_crash_info_t crashReport;
    g_cw->Debug("Creating report...");
    try
    {
        mw_result_t res;
        res = CreateCrashReport(thread_data->UUID, thread_data->UID, crashReport);
        switch (res)
        {
            case MW_OK:
                break;
            case MW_IN_DB_ERROR:
                g_cw->Warning(std::string("Did not find crash with UUID ")+thread_data->UUID+ " in database.");
                break;
            case MW_PLUGIN_ERROR:
                g_cw->Warning(std::string("Particular analyzer plugin isn't loaded or there is an error within plugin(s)."));
                break;
            case MW_CORRUPTED:
            case MW_FILE_ERROR:
            default:
                {
                    std::string debugDumpDir;
                    g_cw->Warning(std::string("Corrupted crash with UUID ")+thread_data->UUID+", deleting.");
                    debugDumpDir = DeleteCrashInfo(thread_data->UUID, thread_data->UID);
                    DeleteDebugDumpDir(debugDumpDir);
                }
                break;
        }
        /* only one thread can write */
        pthread_mutex_lock(&g_pJobsMutex);
        g_pending_jobs[std::string(thread_data->UID)][thread_data->thread_id] = crashReport;
        pthread_mutex_unlock(&g_pJobsMutex);
        g_pCommLayer->JobDone(thread_data->dest, thread_data->thread_id);
    }
    catch (CABRTException& e)
    {
        if (e.type() == EXCEP_FATAL)
        {
            /* free strduped strings */
            free(thread_data->UUID);
            free(thread_data->UID);
            free(thread_data->dest);
            free(thread_data);
            throw e;
        }
        g_cw->Warning(e.what());
    }
    /* free strduped strings */
    free(thread_data->UUID);
    free(thread_data->UID);
    free(thread_data->dest);
    free(thread_data);

    /* Bogus value. pthreads require us to return void* */
    return NULL;
}
uint64_t CCrashWatcher::CreateReport_t(const std::string &pUUID,const std::string &pUID, const std::string &pSender)
{
    thread_data_t *thread_data = (thread_data_t *)xzalloc(sizeof(thread_data_t));
    if (thread_data != NULL)
    {
        thread_data->UUID = xstrdup(pUUID.c_str());
        thread_data->UID = xstrdup(pUID.c_str());
        thread_data->dest = xstrdup(pSender.c_str());
        if (pthread_create(&(thread_data->thread_id), NULL, create_report, (void *)thread_data) != 0)
        {
            throw CABRTException(EXCEP_FATAL, "CCrashWatcher::CreateReport_t(): Cannot create thread!");
        }
    }
    else
    {
        throw CABRTException(EXCEP_FATAL, "CCrashWatcher::CreateReport_t(): Cannot allocate memory!");
    }
    //FIXME: we don't use this value anymore, so fix the API
    return 0;
}

report_status_t CCrashWatcher::Report(map_crash_report_t pReport, const std::string& pUID)
{
    //#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;
    //}
    report_status_t rs;
    try
    {
        rs = ::Report(pReport, pUID);
    }
    catch (CABRTException& e)
    {
        if (e.type() == EXCEP_FATAL)
        {
            throw e;
        }
        Warning(e.what());
        Status(e.what());
        return rs;
    }
    return rs;
}

bool CCrashWatcher::DeleteDebugDump(const std::string& pUUID, const std::string& pUID)
{
    try
    {
        std::string debugDumpDir;
        debugDumpDir = DeleteCrashInfo(pUUID, pUID);
        DeleteDebugDumpDir(debugDumpDir);
    }
    catch (CABRTException& e)
    {
        if (e.type() == EXCEP_FATAL)
        {
            throw e;
        }
        Warning(e.what());
        Status(e.what());
        return false;
    }
    return true;
}

map_crash_report_t CCrashWatcher::GetJobResult(uint64_t pJobID, const std::string& pSender)
{
    /* FIXME: once we return the result, we should remove it from map to free memory
       - use some TTL to clean the memory even if client won't get it
       - if we don't find it in the cache we should try to ask MW to get it again??
    */
    return g_pending_jobs[pSender][pJobID];
}

vector_map_string_string_t CCrashWatcher::GetPluginsInfo()
{
    try
    {
        return g_pPluginManager->GetPluginsInfo();
    }
    catch (CABRTException &e)
    {
        if (e.type() == EXCEP_FATAL)
        {
            throw e;
        }
        Warning(e.what());
    }
    // TODO: is it right? I added it just to disable a warning...
    // but maybe returning empty map is wrong here?
    return vector_map_string_string_t();
}

map_plugin_settings_t CCrashWatcher::GetPluginSettings(const std::string& pName, const std::string& pUID)
{
    try
    {
        return g_pPluginManager->GetPluginSettings(pName, pUID);
    }
    catch(CABRTException &e)
    {
        if (e.type() == EXCEP_FATAL)
        {
            throw e;
        }
        Warning(e.what());
    }
    // TODO: is it right? I added it just to disable a warning...
    // but maybe returning empty map is wrong here?
    return map_plugin_settings_t();
}

void CCrashWatcher::RegisterPlugin(const std::string& pName)
{
    try
    {
        g_pPluginManager->RegisterPlugin(pName);
    }
    catch(CABRTException &e)
    {
        if (e.type() == EXCEP_FATAL)
        {
            throw e;
        }
        Warning(e.what());
    }
}

void CCrashWatcher::UnRegisterPlugin(const std::string& pName)
{
    try
    {
        g_pPluginManager->UnRegisterPlugin(pName);
    }
    catch(CABRTException &e)
    {
        if (e.type() == EXCEP_FATAL)
        {
            throw e;
        }
        Warning(e.what());
    }
}

void CCrashWatcher::SetPluginSettings(const std::string& pName, const std::string& pUID, const map_plugin_settings_t& pSettings)
{
    try
    {
        g_pPluginManager->SetPluginSettings(pName, pUID, pSettings);
    }
    catch(CABRTException &e)
    {
        if (e.type() == EXCEP_FATAL)
        {
            throw e;
        }
        Warning(e.what());
    }
}