summaryrefslogtreecommitdiffstats
path: root/src/lib/create_dump_dir.c
blob: 2caf9af9daaaabc70527acafc0d08c2d8f9fbe5b (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
/*
    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"

static struct dump_dir *try_dd_create(const char *base_dir_name, const char *dir_name)
{
    char *path = concat_path_file(base_dir_name, dir_name);
    struct dump_dir *dd = dd_create(path, (uid_t)-1L);
    if (dd)
        dd_create_basic_files(dd, (uid_t)-1L);
    free(path);
    return dd;
}

struct dump_dir *create_dump_dir_from_crash_data(crash_data_t *crash_data, const char *base_dir_name)
{
    char dir_name[sizeof("abrt-tmp-%lu-%lu") + sizeof(long)*3 * 2];
    sprintf(dir_name, "abrt-tmp-%lu-%lu", (long)getpid(), (long)time(NULL));

    struct dump_dir *dd;
    if (base_dir_name)
        dd = try_dd_create(base_dir_name, dir_name);
    else
    {
        /* Try /var/run/abrt */
        dd = try_dd_create(LOCALSTATEDIR"/run/abrt", dir_name);
        /* Try $HOME/tmp */
        if (!dd)
        {
            char *home = getenv("HOME");
            if (home && home[0])
            {
                home = concat_path_file(home, "tmp");
                /*mkdir(home, 0777); - do we want this? */
                dd = try_dd_create(home, dir_name);
                free(home);
            }
        }
        /* Try /tmp */
        if (!dd)
            dd = try_dd_create("/tmp", dir_name);
    }
    if (!dd)
        return NULL;

    GHashTableIter iter;
    char *name;
    struct crash_item *value;
    g_hash_table_iter_init(&iter, crash_data);
    while (g_hash_table_iter_next(&iter, (void**)&name, (void**)&value))
    {
        if (name[0] == '.' || strchr(name, '/'))
        {
            error_msg("Crash data field name contains disallowed chars: '%s'", name);
            goto next;
        }

//FIXME: what to do with CD_FLAG_BINs??
        if (value->flags & CD_FLAG_BIN)
            goto next;

        dd_save_text(dd, name, value->content);
 next: ;
    }

    return dd;
}