summaryrefslogtreecommitdiffstats
path: root/src/daemon/MiddleWare.cpp
blob: 8d62c6978cf270ae6879b1d8a065c255a492c8f3 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
    MiddleWare.cpp

    Copyright (C) 2009  Zdenek Prikryl (zprikryl@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 "Settings.h"
#include "comm_layer_inner.h"
#include "CommLayerServerDBus.h"
#include "MiddleWare.h"

using namespace std;

/**
 * Get one crash info. If getting is successful,
 * then crash info is filled.
 * @param dump_dir_name A dump dir containing all necessary data.
 * @param pCrashData A crash info.
 * @return It return results of operation. See mw_result_t.
 */
static crash_data_t *FillCrashInfo(const char *dump_dir_name);

/**
 * Transforms a debugdump directory to inner crash
 * report form. This form is used for later reporting.
 * @param dump_dir_name A debugdump dir containing all necessary data.
 * @param pCrashData A created crash report.
 */
static crash_data_t *DebugDumpToCrashReport(const char *dump_dir_name)
{
    VERB3 log(" DebugDumpToCrashReport('%s')", dump_dir_name);

    struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
    if (!dd)
        return NULL;

    static const char *const must_have_files[] = {
	FILENAME_ARCHITECTURE,
	FILENAME_KERNEL      ,
	FILENAME_PACKAGE     ,
	FILENAME_COMPONENT   ,
	FILENAME_OS_RELEASE  ,
	FILENAME_EXECUTABLE  ,
	NULL
    };
    const char *const *v = must_have_files;
    while (*v)
    {
        if (!dd_exist(dd, *v))
        {
            /* Old dump dir format compat. Remove in abrt-2.1 */
            if (strcmp(*v, FILENAME_OS_RELEASE) == 0)
                if (dd_exist(dd, "release"))
                    goto ok;

            dd_close(dd);
            log("Important file '%s/%s' is missing", dump_dir_name, *v);
            return NULL;
        }
 ok:
        v++;
    }

    crash_data_t *crash_data = create_crash_data_from_dump_dir(dd);
    char *events = list_possible_events(dd, NULL, "");
    dd_close(dd);

    add_to_crash_data_ext(crash_data, CD_EVENTS, events,
                          CD_FLAG_TXT + CD_FLAG_ISNOTEDITABLE);
    free(events);

    add_to_crash_data_ext(crash_data, CD_DUMPDIR, dump_dir_name,
                          CD_FLAG_TXT + CD_FLAG_ISNOTEDITABLE);

    return crash_data;
}

static char *do_log_and_update_client(char *log_line, void *param)
{
    VERB1 log("%s", log_line);
    update_client("%s", log_line);
    return log_line;
}

/**
 * Takes care of getting all additional data needed
 * for computing UUIDs and creating a report for particular analyzer
 * plugin. This report could be send somewhere afterwards. If a creation
 * is successful, then  a crash report is filled.
 * @param pAnalyzer A name of an analyzer plugin.
 * @param pDebugDumpPath A debugdump dir containing all necessary data.
 * @param pCrashData A filled crash report.
 * @return It return results of operation. See mw_result_t.
 */
/*
 * Called in two cases:
 * (1) by StartJob dbus call -> CreateReportThread(), in the thread
 * (2) by CreateReport dbus call
 */
static mw_result_t CreateCrashReport(const char *dump_dir_name,
                long caller_uid,
                int force,
                crash_data_t **crash_data)
{
    VERB2 log("CreateCrashReport('%s',%ld)", dump_dir_name, caller_uid);

    *crash_data = NULL;

    struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
    if (!dd)
        return MW_NOENT_ERROR;

    struct run_event_state *run_state;
    int res;
    mw_result_t r = MW_OK;

    if (caller_uid != 0) /* not called by root */
    {
        char caller_uid_str[sizeof(long) * 3 + 2];
        sprintf(caller_uid_str, "%ld", caller_uid);

        char *uid = dd_load_text(dd, FILENAME_UID);
        if (strcmp(uid, caller_uid_str) != 0)
        {
            char *inform_all = dd_load_text_ext(dd, FILENAME_INFORMALL, DD_FAIL_QUIETLY_ENOENT);
            bool for_all = string_to_bool(inform_all);
            free(inform_all);
            if (!for_all)
            {
                dd_close(dd);
                error_msg("crash '%s' can't be accessed by user with uid %ld", dump_dir_name, caller_uid);
                r = MW_PERM_ERROR;
                goto ret;
            }
        }
    }
    dd_close(dd);

    run_state = new_run_event_state();
    run_state->logging_callback = do_log_and_update_client;
    res = run_event_on_dir_name(run_state, dump_dir_name, force ? "reanalyze" : "analyze");
    free_run_event_state(run_state);
    if (res != 0)
    {
        r = MW_PLUGIN_ERROR;
        goto ret;
    }

    /* Do a create_crash_data_from_dump_dir from (possibly updated)
     * crash dump dir
     */
    *crash_data = DebugDumpToCrashReport(dump_dir_name);
    if (!*crash_data)
    {
        error_msg("Error loading crash data");
        r = MW_ERROR;
    }

 ret:
    if (*crash_data == NULL)
        *crash_data = new_crash_data();
    VERB3 log("CreateCrashReport() returns %d", r);
    return r;
}

struct logging_state {
    char *last_line;
};

static char *do_log_and_save_line(char *log_line, void *param)
{
    struct logging_state *l_state = (struct logging_state *)param;

    VERB1 log("%s", log_line);
    update_client("%s", log_line);
    free(l_state->last_line);
    l_state->last_line = log_line;
    return NULL;
}

// Do not trust client_report here!
// dbus handler passes it from user without checking
report_status_t Report(crash_data_t *client_report,
                       const vector_string_t& events,
                       const map_map_string_t& settings,
                       long caller_uid)
{
    report_status_t ret;
    const char *dump_dir_name = get_crash_item_content_or_NULL(client_report, CD_DUMPDIR);
    if (!dump_dir_name)
    {
        update_client("Reporting error: %s", "DUMPDIR is missing in client's report data");
        ret[""].push_back("0");      // REPORT_STATUS_IDX_FLAG
        ret[""].push_back("DUMPDIR is missing in client's report data"); // REPORT_STATUS_IDX_MSG
        return ret;
    }

    // Retrieve corresponding stored record
    crash_data_t *stored_report = FillCrashInfo(dump_dir_name);
    if (!stored_report)
    {
        return report_status_t();
    }

    // Is it allowed for this user to report?
    if (caller_uid != 0)   // not called by root
    {
        char caller_uid_str[sizeof(long)*3 + 2];
        sprintf(caller_uid_str, "%ld", caller_uid);
        if (strcmp(caller_uid_str, get_crash_item_content_or_die(stored_report, FILENAME_UID)) != 0)
        {
            const char *inform_all = get_crash_item_content_or_NULL(stored_report, FILENAME_INFORMALL);
            if (!inform_all || !string_to_bool(inform_all))
            {
                free_crash_data(stored_report);
                char *errmsg = xasprintf("user with uid %ld can't report crash %s", caller_uid, dump_dir_name);
                update_client("Reporting error: %s", errmsg);
                ret[""].push_back("0");      // REPORT_STATUS_IDX_FLAG
                ret[""].push_back(errmsg); // REPORT_STATUS_IDX_MSG
                free(errmsg);
                return ret;
            }
        }
    }

    // Save comment, backtrace
//TODO: we should iterate through stored_report and modify all
//modifiable fields which have new data in client_report
    const char *comment = get_crash_item_content_or_NULL(client_report, FILENAME_COMMENT);
    const char *backtrace = get_crash_item_content_or_NULL(client_report, FILENAME_BACKTRACE);
    if (comment || backtrace)
    {
        struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
        if (dd)
        {
            if (comment)
            {
                dd_save_text(dd, FILENAME_COMMENT, comment);
                add_to_crash_data_ext(stored_report, FILENAME_COMMENT, comment, CD_FLAG_TXT + CD_FLAG_ISEDITABLE);
            }
            if (backtrace)
            {
                dd_save_text(dd, FILENAME_BACKTRACE, backtrace);
                add_to_crash_data_ext(stored_report, FILENAME_BACKTRACE, backtrace, CD_FLAG_TXT + CD_FLAG_ISEDITABLE);
            }
            dd_close(dd);
        }
    }

    /* Remove BIN filenames from stored_report if they are not present in client's data */
    GHashTableIter iter;
    char *name;
    struct crash_item *value;
    g_hash_table_iter_init(&iter, stored_report);
    while (g_hash_table_iter_next(&iter, (void**)&name, (void**)&value))
    {
        if (value->flags & CD_FLAG_BIN)
        {
            if (get_crash_item_content_or_NULL(client_report, name) == NULL)
            {
                /* client does not have it -> does not want it passed to events */
                VERB3 log("Won't report BIN file %s:'%s'", name, value->content);
                g_hash_table_iter_remove(&iter);
                continue;
            }
        }
    }

    VERB3 {
        log_crash_data(client_report, " client_report");
        log_crash_data(stored_report, " stored_report");
    }
    free_crash_data(stored_report);
#define stored_report stored_report_must_not_be_used_below
#define client_report client_report_must_not_be_used_below

    // Export overridden settings as environment variables
    GList *env_list = NULL;
    map_map_string_t::const_iterator reporter_settings = settings.begin();
    while (reporter_settings != settings.end())
    {
        map_string_t::const_iterator var = reporter_settings->second.begin();
        while (var != reporter_settings->second.end())
        {
            char *s = xasprintf("%s_%s=%s", reporter_settings->first.c_str(), var->first.c_str(), var->second.c_str());
            VERB3 log("Exporting '%s'", s);
            putenv(s);
            env_list = g_list_append(env_list, s);
            var++;
        }
        reporter_settings++;
    }

    // Run events
    bool at_least_one_reporter_succeeded = false;
    std::string message;
    struct logging_state l_state;
    struct run_event_state *run_state = new_run_event_state();
    run_state->logging_callback = do_log_and_save_line;
    run_state->logging_param = &l_state;
    for (unsigned i = 0; i < events.size(); i++)
    {
        std::string event = events[i];

        l_state.last_line = NULL;
        int r = run_event_on_dir_name(run_state, dump_dir_name, event.c_str());
        if (r == 0 && run_state->children_count == 0)
        {
            l_state.last_line = xasprintf("Error: no processing is specified for event '%s'", event.c_str());
            r = -1;
        }
        if (r == 0)
        {
            at_least_one_reporter_succeeded = true;
            ret[event].push_back("1"); // REPORT_STATUS_IDX_FLAG
            ret[event].push_back(l_state.last_line ? : "Reporting succeeded"); // REPORT_STATUS_IDX_MSG
            if (message != "")
                message += ";";
            message += (l_state.last_line ? : "Reporting succeeded");
        }
        else
        {
            ret[event].push_back("0");      // REPORT_STATUS_IDX_FLAG
            ret[event].push_back(l_state.last_line ? : "Error in reporting"); // REPORT_STATUS_IDX_MSG
            update_client("Reporting via '%s' was not successful%s%s",
                    event.c_str(),
                    l_state.last_line ? ": " : "",
                    l_state.last_line ? l_state.last_line : ""
            );
        }
        free(l_state.last_line);
    }
    free_run_event_state(run_state);

    // Unexport overridden settings
    for (GList *li = env_list; li; li = g_list_next(li))
    {
        char *s = (char*)li->data;
        VERB3 log("Unexporting '%s'", s);
        safe_unsetenv(s);
        free(s);
    }
    g_list_free(env_list);

    // Save reporting results
    if (at_least_one_reporter_succeeded)
    {
        report_status_t::iterator ret_it = ret.begin();
        while (ret_it != ret.end())
        {
//            const string &event = ret_it->first;
//            const vector_string_t &v = ret_it->second;
//            if (v[REPORT_STATUS_IDX_FLAG] == "1")
//            {
// TODO: append to a log of reports done
//                database->SetReportedPerReporter(dump_dir_name, event.c_str(), v[REPORT_STATUS_IDX_MSG].c_str());
//            }
            ret_it++;
        }
        /* Was: database->SetReported(dump_dir_name, message.c_str()); */
        struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
        if (dd)
        {
            dd_save_text(dd, FILENAME_MESSAGE, message.c_str());
            dd_close(dd);
        }
    }

    return ret;
#undef stored_report
#undef client_report
}

/* We need to share some data between LoadDebugDump and is_crash_a_dup: */
struct cdump_state {
    char *uid;                   /* filled by LoadDebugDump */
    char *uuid;                  /* filled by is_crash_a_dup */
    char *crash_dump_dup_name;   /* filled by is_crash_a_dup */
};

static int is_crash_a_dup(const char *dump_dir_name, void *param)
{
    struct cdump_state *state = (struct cdump_state *)param;

    if (state->uuid)
        return 0; /* we already checked it, don't do it again */

    struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
    if (!dd)
        return 0; /* wtf? (error, but will be handled elsewhere later) */
    state->uuid = dd_load_text_ext(dd, FILENAME_UUID,
                DD_FAIL_QUIETLY_ENOENT + DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE
    );
    dd_close(dd);
    if (!state->uuid)
    {
        return 0; /* no uuid (yet), "run_event, please continue iterating" */
    }

    /* Scan crash dumps looking for a dup */
//TODO: explain why this is safe wrt concurrent runs
    DIR *dir = opendir(DEBUG_DUMPS_DIR);
    if (dir != NULL)
    {
        struct dirent *dent;
        while ((dent = readdir(dir)) != NULL)
        {
            if (dot_or_dotdot(dent->d_name))
                continue; /* skip "." and ".." */

            int different;
            char *uid, *uuid;
            char *dump_dir_name2 = concat_path_file(DEBUG_DUMPS_DIR, dent->d_name);

            if (strcmp(dump_dir_name, dump_dir_name2) == 0)
                goto next; /* we are never a dup of ourself */

            dd = dd_opendir(dump_dir_name2, /*flags:*/ 0);
            if (!dd)
                goto next;
            uid = dd_load_text(dd, FILENAME_UID);
            uuid = dd_load_text(dd, FILENAME_UUID);
            dd_close(dd);
            different = strcmp(state->uid, uid) || strcmp(state->uuid, uuid);
            free(uid);
            free(uuid);
            if (different)
                goto next;

            state->crash_dump_dup_name = dump_dir_name2;
            /* "run_event, please stop iterating": */
            return 1;

 next:
            free(dump_dir_name2);
        }
        closedir(dir);
    }

    /* No dup found */
    return 0; /* "run_event, please continue iterating" */
}

static char *do_log(char *log_line, void *param)
{
    VERB1 log("%s", log_line);
    //update_client("%s", log_line);
    return log_line;
}

mw_result_t LoadDebugDump(const char *dump_dir_name, crash_data_t **crash_data)
{
    mw_result_t res;

    struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
    if (!dd)
        return MW_ERROR;
    struct cdump_state state;
    state.uid = dd_load_text(dd, FILENAME_UID);
    state.uuid = NULL;
    state.crash_dump_dup_name = NULL;
    char *analyzer = dd_load_text(dd, FILENAME_ANALYZER);
    dd_close(dd);

    res = MW_ERROR;

    /* Run post-create event handler(s) */
    struct run_event_state *run_state = new_run_event_state();
    run_state->post_run_callback = is_crash_a_dup;
    run_state->post_run_param = &state;
    run_state->logging_callback = do_log;
    int r = run_event_on_dir_name(run_state, dump_dir_name, "post-create");
    free_run_event_state(run_state);

//TODO: consider this case:
// new dump is created, post-create detects that it is a dup,
// but then FillCrashInfo(dup_name) *FAILS*.
// In this case, we later delete damaged dup_name (right?)
// but new dump never gets its FILENAME_COUNT set!

    /* Is crash a dup? (In this case, is_crash_a_dup() should have
     * aborted "post-create" event processing as soon as it saw uuid
     * and determined that there is another crash with same uuid.
     * In this case it sets state.crash_dump_dup_name)
     */
    if (!state.crash_dump_dup_name)
    {
        /* No. Was there error on one of processing steps in run_event? */
        if (r != 0)
            goto ret; /* yes */

        /* Was uuid created after all? (In this case, is_crash_a_dup()
         * should have fetched it and created state.uuid)
         */
        if (!state.uuid)
        {
            /* no */
            log("Dump directory '%s' has no UUID element", dump_dir_name);
            goto ret;
        }
    }
    else
    {
        dump_dir_name = state.crash_dump_dup_name;
    }

    /* Loads crash_data (from the *first debugdump dir* if this one is a dup)
     * Returns:
     * MW_OCCURRED: "crash count is != 1" (iow: it is > 1 - dup)
     * MW_OK: "crash count is 1" (iow: this is a new crash, not a dup)
     * else: an error code
     */
    {
        dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
        if (!dd)
        {
            res = MW_ERROR;
            goto ret;
        }
        char *count_str = dd_load_text_ext(dd, FILENAME_COUNT, DD_FAIL_QUIETLY_ENOENT);
        unsigned long count = strtoul(count_str, NULL, 10);
        count++;
        char new_count_str[sizeof(long)*3 + 2];
        sprintf(new_count_str, "%lu", count);
        dd_save_text(dd, FILENAME_COUNT, new_count_str);
        dd_close(dd);

        *crash_data = FillCrashInfo(dump_dir_name);
        if (*crash_data != NULL)
        {
            res = MW_OK;
            if (count > 1)
            {
                log("Crash dump is a duplicate of %s", dump_dir_name);
                res = MW_OCCURRED;
            }
        }
    }

 ret:
    free(state.uuid);
    free(state.uid);
    free(state.crash_dump_dup_name);
    free(analyzer);

    return res;
}

static crash_data_t *FillCrashInfo(const char *dump_dir_name)
{
    struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
    if (!dd)
        return NULL;

    crash_data_t *crash_data = create_crash_data_from_dump_dir(dd);
    char *events = list_possible_events(dd, NULL, "");
    dd_close(dd);

    add_to_crash_data_ext(crash_data, CD_EVENTS, events,
                          CD_FLAG_TXT + CD_FLAG_ISNOTEDITABLE);
    free(events);

    add_to_crash_data_ext(crash_data, CD_DUMPDIR, dump_dir_name,
                          CD_FLAG_TXT + CD_FLAG_ISNOTEDITABLE);

    return crash_data;
}

vector_of_crash_data_t *GetCrashInfos(long caller_uid)
{
    vector_of_crash_data_t *retval = new_vector_of_crash_data();
    log("Getting crash infos...");

    DIR *dir = opendir(DEBUG_DUMPS_DIR);
    if (dir != NULL)
    {
        struct dirent *dent;
        while ((dent = readdir(dir)) != NULL)
        {
            if (dot_or_dotdot(dent->d_name))
                continue; /* skip "." and ".." */

            char *dump_dir_name = concat_path_file(DEBUG_DUMPS_DIR, dent->d_name);

            struct stat statbuf;
            if (stat(dump_dir_name, &statbuf) != 0
             || !S_ISDIR(statbuf.st_mode)
            ) {
                goto next; /* not a dir, skip */
            }

            /* Skip directories which are not for this uid */
            if (caller_uid != 0) /* not called by root? */
            {
                char *uid;
                char caller_uid_str[sizeof(long) * 3 + 2];

                struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
                if (!dd)
                    goto next;

                sprintf(caller_uid_str, "%ld", caller_uid);
                uid = dd_load_text(dd, FILENAME_UID);
                if (strcmp(uid, caller_uid_str) != 0)
                {
                    char *inform_all = dd_load_text_ext(dd, FILENAME_INFORMALL, DD_FAIL_QUIETLY_ENOENT);
                    bool for_all = string_to_bool(inform_all);
                    free(inform_all);
                    if (!for_all)
                    {
                        dd_close(dd);
                        goto next;
                    }
                }
                dd_close(dd);
            }

            {
                crash_data_t *crash_data = FillCrashInfo(dump_dir_name);
                if (!crash_data)
                {
                    error_msg("Dump directory %s doesn't exist or misses crucial files, deleting", dump_dir_name);
                    delete_dump_dir(dump_dir_name);
                }
                else
                {
                    g_ptr_array_add(retval, crash_data);
                }
            }
 next:
            free(dump_dir_name);
        }
        closedir(dir);
    }

    return retval;
}

/*
 * Called in two cases:
 * (1) by StartJob dbus call -> CreateReportThread(), in the thread
 * (2) by CreateReport dbus call
 * In the second case, it finishes quickly, because previous
 * StartJob dbus call already did all the processing, and we just retrieve
 * the result from dump directory, which is fast.
 */
void CreateReport(const char* crash_id, long caller_uid, int force, crash_data_t **crash_data)
{
    /* FIXME: starting from here, any shared data must be protected with a mutex. */
    mw_result_t res = CreateCrashReport(crash_id, caller_uid, force, crash_data);
    switch (res)
    {
        case MW_OK:
            VERB2 log_crash_data(*crash_data, "crashReport");
            break;
        case MW_NOENT_ERROR:
            error_msg("Can't find crash with id '%s'", crash_id);
            break;
        case MW_PERM_ERROR:
            error_msg("Can't find crash with id '%s'", crash_id);
            break;
        case MW_PLUGIN_ERROR:
            error_msg("Particular analyzer plugin isn't loaded or there is an error within plugin(s)");
            break;
        default:
            error_msg("Corrupted crash with id %s, deleting", crash_id);
            DeleteDebugDump(crash_id, /*caller_uid:*/ 0);
            break;
    }
}

typedef struct thread_data_t {
    pthread_t thread_id;
    long caller_uid;
    int force;
    char* crash_id;
    char* peer;
} thread_data_t;
static void* create_report(void* arg)
{
    thread_data_t *thread_data = (thread_data_t *) arg;

    /* Client name is per-thread, need to set it */
    set_client_name(thread_data->peer);

    log("Creating report...");
    crash_data_t *crash_data = NULL;
    CreateReport(thread_data->crash_id, thread_data->caller_uid, thread_data->force, &crash_data);
    send_dbus_sig_JobDone(thread_data->peer);
    set_client_name(NULL);

    /* free strduped strings */
    free(thread_data->crash_id);
    free(thread_data->peer);
    free(thread_data);

    /* Bogus value. pthreads require us to return void* */
    return NULL;
}
int CreateReportThread(const char* crash_id, long caller_uid, int force, const char* pSender)
{
    thread_data_t *thread_data = (thread_data_t *)xzalloc(sizeof(thread_data_t));
    thread_data->crash_id = xstrdup(crash_id);
    thread_data->caller_uid = caller_uid;
    thread_data->force = force;
    thread_data->peer = xstrdup(pSender);

    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    int r = pthread_create(&thread_data->thread_id, &attr, create_report, thread_data);
    pthread_attr_destroy(&attr);
    if (r != 0)
    {
        free(thread_data->crash_id);
        free(thread_data->peer);
        free(thread_data);
        /* The only reason this may happen is system-wide resource starvation,
         * or ulimit is exceeded (someone floods us with CreateReport() dbus calls?)
         */
        error_msg("Can't create thread");
        return r;
    }
    VERB3 log("Thread %llx created", (unsigned long long)thread_data->thread_id);
    return r;
}


/* Remove dump dir */
int DeleteDebugDump(const char *dump_dir_name, long caller_uid)
{
    struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
    if (!dd)
        return MW_NOENT_ERROR;

    if (caller_uid != 0) /* not called by root */
    {
        char caller_uid_str[sizeof(long) * 3 + 2];
        sprintf(caller_uid_str, "%ld", caller_uid);

        char *uid = dd_load_text(dd, FILENAME_UID);
        if (strcmp(uid, caller_uid_str) != 0)
        {
            char *inform_all = dd_load_text_ext(dd, FILENAME_INFORMALL, DD_FAIL_QUIETLY_ENOENT);
            if (!string_to_bool(inform_all))
            {
                dd_close(dd);
                error_msg("crash '%s' can't be accessed by user with uid %ld", dump_dir_name, caller_uid);
                return 1;
            }
        }
    }

    dd_delete(dd);

    return 0; /* success */
}

void GetPluginsInfo(map_map_string_t &map_of_plugin_info)
{
    DIR *dir = opendir(PLUGINS_CONF_DIR);
    if (!dir)
        return;

    struct dirent *dent;
    while ((dent = readdir(dir)) != NULL)
    {
        char *ext = strrchr(dent->d_name, '.');
        if (!ext || strcmp(ext + 1, "conf") != 0)
            continue;
        if (!is_regular_file(dent, PLUGINS_CONF_DIR))
            continue;
        VERB3 log("Found %s", dent->d_name);
        *ext = '\0';

        char *glade_file = xasprintf(PLUGINS_LIB_DIR"/%s.glade", dent->d_name);
        if (access(glade_file, F_OK) == 0)
        {
            *ext = '.';
            char *conf_file = concat_path_file(PLUGINS_CONF_DIR, dent->d_name);
            *ext = '\0';
            FILE *fp = fopen(conf_file, "r");
            free(conf_file);

            char *descr = NULL;
            if (fp)
            {
                descr = xmalloc_fgetline(fp);
                fclose(fp);
                if (descr && strncmp("# Description:", descr, strlen("# Description:")) == 0)
                    overlapping_strcpy(descr, skip_whitespace(descr + strlen("# Description:")));
                else
                {
                    free(descr);
                    descr = NULL;
                }
            }
            map_string_t plugin_info;
            plugin_info["Name"] = dent->d_name;
            plugin_info["Enabled"] = "yes";
            plugin_info["Type"] = "Reporter";       //was: plugin_type_str[module->GetType()]; field to be removed
            plugin_info["Version"] = VERSION;       //was: module->GetVersion(); field to be removed?
            plugin_info["Description"] = descr ? descr : ""; //was: module->GetDescription();
            plugin_info["Email"] = "";              //was: module->GetEmail(); field to be removed
            plugin_info["WWW"] = "";                //was: module->GetWWW(); field to be removed
            plugin_info["GTKBuilder"] = glade_file; //was: module->GetGTKBuilder();
            free(descr);
            map_of_plugin_info[dent->d_name] = plugin_info;
        }
        free(glade_file);

    }
    closedir(dir);
}

map_string_h *GetPluginSettings(const char *plugin_name)
{
    char *conf_file = xasprintf(PLUGINS_CONF_DIR"/%s.conf", plugin_name);
    map_string_h *settings = new_map_string();
    if (load_conf_file(conf_file, settings, /*skip w/o value:*/ false))
        VERB3 log("Loaded %s.conf", plugin_name);
    free(conf_file);
    return settings;
}