summaryrefslogtreecommitdiffstats
path: root/src/daemon/abrt-handle-crashdump.c
blob: e7847c5fee704567e7ccfb3ea4fe373382c0eb2b (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
/*
    Copyright (C) 2010  ABRT team
    Copyright (C) 2010  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 "parse_options.h"

#define PROGNAME "abrt-handle-crashdump"

static const char *dump_dir_name = NULL;
//static const char *conf_filename = CONF_DIR"/abrt_event.conf";
static const char *event;
static const char *pfx = "";

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

int main(int argc, char **argv)
{
    char *env_verbose = getenv("ABRT_VERBOSE");
    if (env_verbose)
        g_verbose = atoi(env_verbose);

    /* Can't keep these strings/structs static: _() doesn't support that */
    const char *program_usage_string = _(
        PROGNAME" [-vs]" /*" [-c CONFFILE]"*/ " -d DIR -e EVENT\n"
        "   or: "PROGNAME" [-vs]" /*" [-c CONFFILE]"*/ " [-d DIR] -l[PFX]\n"
        "\n"
        "Handle crash dump according to rules in abrt_event.conf"
    );
    enum {
        OPT_v = 1 << 0,
        OPT_s = 1 << 1,
        OPT_d = 1 << 2,
        OPT_e = 1 << 3,
        OPT_l = 1 << 4,
//      OPT_c = 1 << ?,
    };
    /* Keep enum above and order of options below in sync! */
    struct options program_options[] = {
        OPT__VERBOSE(&g_verbose),
        OPT_BOOL(     's', NULL, NULL          ,             _("Log to syslog"       )),
        OPT_STRING(   'd', NULL, &dump_dir_name, "DIR"     , _("Crash dump directory")),
        OPT_STRING(   'e', NULL, &event        , "EVENT"   , _("Handle EVENT"        )),
        OPT_OPTSTRING('l', NULL, &pfx          , "PFX"     , _("List possible events [which start with PFX]")),
//      OPT_STRING(   'c', NULL, &conf_filename, "CONFFILE", _("Configuration file"  )),
        OPT_END()
    };
    unsigned opts = parse_opts(argc, argv, program_options, program_usage_string);
    if (!(opts & (OPT_e|OPT_l)))
        show_usage_and_die(program_usage_string, program_options);

    putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose));
    msg_prefix = PROGNAME;
    if (opts & OPT_s)
    {
        openlog(msg_prefix, 0, LOG_DAEMON);
        logmode = LOGMODE_SYSLOG;
    }

    if (opts & OPT_l)
    {
        /* Note that dump_dir_name may be NULL here, it means "show all
         * possible events regardless of dir"
         */
        char *events = list_possible_events(NULL, dump_dir_name, pfx);
        if (!events)
            return 1; /* error msg is already logged */
        fputs(events, stdout);
        free(events);
        return 0;
    }

    /* -e EVENT: run event */

    struct run_event_state *run_state = new_run_event_state();
    run_state->logging_callback = do_log;
    int r = run_event_on_dir_name(run_state, dump_dir_name ? dump_dir_name : ".", event);
    if (r == 0 && run_state->children_count == 0)
        error_msg_and_die("No actions are found for event '%s'", event);
    free_run_event_state(run_state);

    return r;
}