summaryrefslogtreecommitdiffstats
path: root/src/lib/run_event.c
blob: e577c3836fc78258ff16b6e35688c3ee3a1a8132 (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
/*
    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 <glob.h>
#include "abrtlib.h"

struct run_event_state *new_run_event_state()
{
    return xzalloc(sizeof(struct run_event_state));
}

void free_run_event_state(struct run_event_state *state)
{
    if (state)
    {
        free_commands(state);
        free(state);
    }
}


/* Asyncronous command execution */

/* It is not yet clear whether we need to re-parse event config file
 * and re-check the elements in dump dir after each comamnd.
 *
 * Consider this config file:
 *
 * EVENT=e         cmd1
 * EVENT=e foo=bar cmd2
 * EVENT=e foo=baz cmd3
 *
 * Imagine that element foo existed and was equal to bar at the beginning.
 * After cmd1, should we execute cmd2 if element foo disappeared?
 * After cmd1/2, should we execute cmd3 if element foo changed value to baz?
 *
 * So far, we read entire config file and select a list of commands to execute,
 * checking all conditions in the beginning. It is a bit more simple to code up.
 * But we may want to change it later. Therefore list of commands machinery
 * is encapsulated in struct run_event_state and public async API:
 *      prepare_commands(state, dir, event);
 *      spawn_next_command(state, dir, event);
 *      free_commands(state);
 * does not expose it.
 */

/* Stop-gap measure against infinite recursion */
#define MAX_recursion_depth 32

static GList *load_event_config(GList *list,
                const char *dump_dir_name,
                const char *event,
                const char *conf_file_name,
                unsigned recursion_depth
) {
    FILE *conffile = fopen(conf_file_name, "r");
    if (!conffile)
    {
        error_msg("Can't open '%s'", conf_file_name);
        return list;
    }

    /* Read, match, and remember commands to execute */
    struct dump_dir *dd = NULL;
    char *next_line = xmalloc_fgetline(conffile);
    while (next_line)
    {
        char *line = next_line;
        while (1)
        {
            next_line = xmalloc_fgetline(conffile);
            if (!next_line || !isblank(next_line[0]))
                break;
            char *old_line = line;
            line = xasprintf("%s\n%s", line, next_line);
            free(old_line);
            free(next_line);
        }

        /* Line has form: [VAR=VAL]... PROG [ARGS] */
        char *p = skip_whitespace(line);
        if (*p == '\0' || *p == '#')
            goto next_line; /* empty or comment line, skip */

        //VERB3 log("%s: line '%s'", __func__, p);

        if (recursion_depth < MAX_recursion_depth
         && strncmp(p, "include", strlen("include")) == 0
         && isblank(p[strlen("include")])
        ) {
            /* include GLOB_PATTERN */
            p = skip_whitespace(p + strlen("include"));

            const char *last_slash;
            char *name_to_glob;
            if (*p != '/'
             && (last_slash = strrchr(conf_file_name, '/')) != NULL
            )
                /* GLOB_PATTERN is relative, and this include is in path/to/file.conf
                 * Construct path/to/GLOB_PATTERN:
                 */
                name_to_glob = xasprintf("%.*s%s", (int)(last_slash - conf_file_name + 1), conf_file_name, p);
            else
                /* Either GLOB_PATTERN is absolute, or this include is in file.conf
                 * (no slashes in its name). Use unchanged GLOB_PATTERN:
                 */
                name_to_glob = xstrdup(p);

            glob_t globbuf;
            memset(&globbuf, 0, sizeof(globbuf));
            //VERB3 log("%s: globbing '%s'", __func__, name_to_glob);
            glob(name_to_glob, 0, NULL, &globbuf);
            free(name_to_glob);
            char **name = globbuf.gl_pathv;
            if (name) while (*name)
            {
                //VERB3 log("%s: recursing into '%s'", __func__, *name);
                list = load_event_config(list, dump_dir_name, event, *name, recursion_depth + 1);
                //VERB3 log("%s: returned from '%s'", __func__, *name);
                name++;
            }
            globfree(&globbuf);
            goto next_line;
        }

        while (1) /* word loop */
        {
            char *end_word = skip_non_whitespace(p);
            char *next_word = skip_whitespace(end_word);

            /* *end_word = '\0'; - BUG, truncates command */

            /* If there is no '=' in this word... */
            char *line_val = strchr(p, '=');
            if (!line_val || line_val >= end_word)
                break; /* ...we found the start of a command */

            *end_word = '\0';

            /* Current word has VAR=VAL form. line_val => VAL */
            *line_val++ = '\0';

            const char *real_val;
            char *malloced_val = NULL;

            /* Is it EVENT? */
            if (strcmp(p, "EVENT") == 0)
                real_val = event;
            else
            {
                /* Get this name from dump dir */
                if (!dd)
                {
                    dd = dd_opendir(dump_dir_name, DD_OPEN_READONLY);
                    if (!dd)
                    {
                        free(line);
                        free(next_line);
                        goto stop; /* error (note: dd_opendir logged error msg) */
                    }
                }
                real_val = malloced_val = dd_load_text_ext(dd, p, DD_FAIL_QUIETLY_ENOENT);
            }

            /* Does VAL match? */
            if (strcmp(real_val, line_val) != 0)
            {
                //VERB3 log("var '%s': '%.*s'!='%s', skipping line",
                //        p,
                //        (int)(strchrnul(real_val, '\n') - real_val), real_val,
                //        line_val);
                free(malloced_val);
                goto next_line; /* no */
            }
            free(malloced_val);

            /* Go to next word */
            p = next_word;
        } /* end of word loop */

        /* We found matching line, remember its command */
        VERB1 log("Adding '%s'", p);
        overlapping_strcpy(line, p);
        list = g_list_append(list, line);
        continue;

 next_line:
        free(line);
    } /* end of line loop */

 stop:
    dd_close(dd);
    fclose(conffile);

    return list;
}

int prepare_commands(struct run_event_state *state,
                const char *dump_dir_name,
                const char *event
) {
    free_commands(state);

    state->children_count = 0;

    GList *commands = load_event_config(NULL, dump_dir_name, event, CONF_DIR"/abrt_event.conf", /*recursion_depth:*/ 0);
    state->commands = commands;
    return commands != NULL;
}

void free_commands(struct run_event_state *state)
{
    list_free_with_free(state->commands);
    state->commands = NULL;
    state->command_out_fd = -1;
    state->command_pid = 0;
}

/* event parameter is unused for now,
 * but may be needed if we change implementation later
 */
int spawn_next_command(struct run_event_state *state,
                const char *dump_dir_name,
                const char *event
) {
    if (!state->commands)
        return -1;

    /* We count it even if fork fails. The counter isn't meant
     * to count *successful* forks, it is meant to let caller know
     * whether the event we run has *any* handlers configured, or not.
     */
    state->children_count++;

    char *cmd = state->commands->data;
    VERB1 log("Executing '%s'", cmd);

    /* Export some useful environment variables for children */
    char *env_vec[3];
    /* Just exporting dump_dir_name isn't always ok: it can be "."
     * and some children want to cd to other directory but still
     * be able to find dump directory by using $DUMP_DIR...
     */
    char *full_name = realpath(dump_dir_name, NULL);
    env_vec[0] = xasprintf("DUMP_DIR=%s", (full_name ? full_name : dump_dir_name));
    free(full_name);
    env_vec[1] = xasprintf("EVENT=%s", event);
    env_vec[2] = NULL;

    char *argv[4];
    argv[0] = (char*)"/bin/sh"; // TODO: honor $SHELL?
    argv[1] = (char*)"-c";
    argv[2] = cmd;
    argv[3] = NULL;

    int pipefds[2];
    state->command_pid = fork_execv_on_steroids(
                EXECFLG_INPUT_NUL + EXECFLG_OUTPUT + EXECFLG_ERR2OUT,
                argv,
                pipefds,
                /* env_vec: */ env_vec,
                /* dir: */ dump_dir_name,
                /* uid(unused): */ 0
    );
    state->command_out_fd = pipefds[0];

    free(env_vec[0]);
    free(env_vec[1]);

    state->commands = g_list_remove(state->commands, cmd);

    return 0;
}


/* Syncronous command execution:
 */
int run_event_on_dir_name(struct run_event_state *state,
                const char *dump_dir_name,
                const char *event
) {
    prepare_commands(state, dump_dir_name, event);

    /* Execute every command in shell */

    int retval = 0;
    while (spawn_next_command(state, dump_dir_name, event) >= 0)
    {
        /* Consume log from stdout */
        FILE *fp = fdopen(state->command_out_fd, "r");
        if (!fp)
            die_out_of_memory();
        char *buf;
        while ((buf = xmalloc_fgetline(fp)) != NULL)
        {
            if (state->logging_callback)
                buf = state->logging_callback(buf, state->logging_param);
            free(buf);
        }
        fclose(fp); /* Got EOF, close. This also closes state->command_out_fd */

        /* Wait for child to actually exit, collect status */
        int status;
        waitpid(state->command_pid, &status, 0);

        retval = WEXITSTATUS(status);
        if (WIFSIGNALED(status))
            retval = WTERMSIG(status) + 128;
        if (retval != 0)
        {
            break;
        }

        if (state->post_run_callback)
        {
            retval = state->post_run_callback(dump_dir_name, state->post_run_param);
            if (retval != 0)
                break;
        }
    }

    free_commands(state);

    return retval;
}

int run_event_on_problem_data(struct run_event_state *state, problem_data_t *data, const char *event)
{
    state->children_count = 0;

    struct dump_dir *dd = create_dump_dir_from_problem_data(data, NULL);
    if (!dd)
        return -1;
    char *dir_name = xstrdup(dd->dd_dirname);
    dd_close(dd);

    int r = run_event_on_dir_name(state, dir_name, event);

    g_hash_table_remove_all(data);
    dd = dd_opendir(dir_name, /*flags:*/ 0);
    free(dir_name);
    if (dd)
    {
        load_problem_data_from_dump_dir(data, dd);
        dd_delete(dd);
    }
    return r;
}


/* TODO: very similar to run_event_helper, try to combine into one fn? */
static int list_possible_events_helper(struct strbuf *result,
                struct dump_dir *dd,
                const char *dump_dir_name,
                const char *pfx,
                const char *conf_file_name
) {
    FILE *conffile = fopen(conf_file_name, "r");
    if (!conffile)
    {
        error_msg("Can't open '%s'", conf_file_name);
        return 0;
    }

    /* We check "dump_dir_name == NULL" later.
     * Prevent the possibility that both dump_dir_name
     * and dd are non-NULL (which does not make sense)
     */
    if (dd)
        dump_dir_name = NULL;

    int error = 0;
    unsigned pfx_len = strlen(pfx);
    char *next_line = xmalloc_fgetline(conffile);
    while (next_line)
    {
        char *line = next_line;
        while (1)
        {
            next_line = xmalloc_fgetline(conffile);
            if (!next_line || !isblank(next_line[0]))
                break;
            char *old_line = line;
            line = xasprintf("%s\n%s", line, next_line);
            free(old_line);
            free(next_line);
        }

        /* Line has form: [VAR=VAL]... PROG [ARGS] */
        char *p = skip_whitespace(line);
        if (*p == '\0' || *p == '#')
            goto next_line; /* empty or comment line, skip */

        //VERB3 log("%s: line '%s'", __func__, p);

        if (strncmp(p, "include", strlen("include")) == 0 && isblank(p[strlen("include")]))
        {
            /* include GLOB_PATTERN */
            p = skip_whitespace(p + strlen("include"));

            const char *last_slash;
            char *name_to_glob;
            if (*p != '/'
             && (last_slash = strrchr(conf_file_name, '/')) != NULL
            )
                /* GLOB_PATTERN is relative, and this include is in path/to/file.conf
                 * Construct path/to/GLOB_PATTERN:
                 */
                name_to_glob = xasprintf("%.*s%s", (int)(last_slash - conf_file_name + 1), conf_file_name, p);
            else
                /* Either GLOB_PATTERN is absolute, or this include is in file.conf
                 * (no slashes in its name). Use unchanged GLOB_PATTERN:
                 */
                name_to_glob = xstrdup(p);

            glob_t globbuf;
            memset(&globbuf, 0, sizeof(globbuf));
            //VERB3 log("%s: globbing '%s'", __func__, name_to_glob);
            glob(name_to_glob, 0, NULL, &globbuf);
            free(name_to_glob);
            char **name = globbuf.gl_pathv;
            if (name) while (*name)
            {
                //VERB3 log("%s: recursing into '%s'", __func__, *name);
                error = list_possible_events_helper(result, dd, dump_dir_name, pfx, *name);
                //VERB3 log("%s: returned from '%s'", __func__, *name);
                if (error)
                    break;
                name++;
            }
            globfree(&globbuf);
            goto next_line;
        }

        while (1) /* word loop */
        {
            char *end_word = skip_non_whitespace(p);
            char *next_word = skip_whitespace(end_word);
            *end_word = '\0';

            /* If there is no '=' in this word... */
            char *line_val = strchr(p, '=');
            if (!line_val)
                break; /* ...we found the start of a command */

            /* Current word has VAR=VAL form. line_val => VAL */
            *line_val++ = '\0';

            /* Is it EVENT? */
            if (strcmp(p, "EVENT") == 0)
            {
                if (strncmp(line_val, pfx, pfx_len) != 0)
                    goto next_line; /* prefix doesn't match */
                /* (Ab)use line to save matching "\nEVENT_VAL\n" */
                sprintf(line, "\n%s\n", line_val);
            }
            else
            {
                /* Get this name from dump dir */
                if (!dd)
                {
                    /* Without dir name to match, we assume match for this expr */
                    if (!dump_dir_name)
                        goto next_word;
                    dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
                    if (!dd)
                    {
                        error = -1;
                        free(line);
                        free(next_line);
                        goto stop; /* error (note: dd_opendir logged error msg) */
                    }
                }
                char *real_val = dd_load_text_ext(dd, p, DD_FAIL_QUIETLY_ENOENT);
                /* Does VAL match? */
                if (strcmp(real_val, line_val) != 0)
                {
                    //VERB3 log("var '%s': '%.*s'!='%s', skipping line",
                    //        p,
                    //        (int)(strchrnul(real_val, '\n') - real_val), real_val,
                    //        line_val);
                    free(real_val);
                    goto next_line; /* no */
                }
                free(real_val);
            }

 next_word:
            /* Go to next word */
            p = next_word;
        } /* end of word loop */

        if (line[0] == '\n' /* do we *have* saved matched "\nEVENT_VAL\n"? */
         /* and does result->buf NOT yet have VAL? */
         && strncmp(result->buf, line + 1, strlen(line + 1)) != 0
         && !strstr(result->buf, line)
        ) {
            /* Add "EVENT_VAL\n" */
            strbuf_append_str(result, line + 1);
        }

 next_line:
        free(line);
    } /* end of line loop */

 stop:
    if (dump_dir_name != NULL)
        dd_close(dd);
    fclose(conffile);

    return error;
}

char *list_possible_events(struct dump_dir *dd, const char *dump_dir_name, const char *pfx)
{
    struct strbuf *result = strbuf_new();
    int error = list_possible_events_helper(result, dd, dump_dir_name, pfx, CONF_DIR"/abrt_event.conf");
    if (error)
        strbuf_clear(result);
    return strbuf_free_nobuf(result);
}