summaryrefslogtreecommitdiffstats
path: root/src/lib/report.c
blob: 08be48a8a2472c91fd28427a2962773d3a97e143 (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
/*
    Copyright (C) 2011  ABRT Team
    Copyright (C) 2011  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 "report.h"

static void create_hash(char hash_str[SHA1_RESULT_LEN*2 + 1], const char *pInput)
{
    unsigned char hash_bytes[SHA1_RESULT_LEN];
    sha1_ctx_t sha1ctx;
    sha1_begin(&sha1ctx);
    sha1_hash(&sha1ctx, pInput, strlen(pInput));
    sha1_end(&sha1ctx, hash_bytes);

    unsigned len = SHA1_RESULT_LEN;
    unsigned char *s = hash_bytes;
    char *d = hash_str;
    while (len)
    {
        *d++ = "0123456789abcdef"[*s >> 4];
        *d++ = "0123456789abcdef"[*s & 0xf];
        s++;
        len--;
    }
    *d = '\0';
    //log("hash:%s str:'%s'", hash_str, pInput);
}

static void generate_hash_for_all(gpointer key, gpointer value, gpointer user_data)
{
    problem_item *pi = (problem_item *)value;
    char *hash_str = (char *)user_data;
    create_hash(hash_str, pi->content);
}

static int run_reporter_ui(char **args, int flags)
{
    char path[PATH_MAX+1];
    /*
    if is isatty
      -> run cli reporter
      path = "cli"
    */

    pid_t pid = vfork();
    if (pid == 0)
    {
        /* Some callers set SIGCHLD to SIG_IGN.
         * However, reporting spawns chils processes.
         * Suppressing chil death notification terribly confuses some of them.
         * Just in case, undo it.
         * Note that we do it in the child, so the parent is never affected.
         */
        signal(SIGCHLD, SIG_DFL); // applet still set it to SIG_IGN
        strncpy(path, BIN_DIR"/bug-reporting-wizard", PATH_MAX);
        path[PATH_MAX] = 0;
        VERB1 log("Executing: %s", path);
        execv(path, args);
        /* Did not find abrt-gui in installation directory. Oh well */
        /* Trying to find it in PATH */
        strncpy(path, "bug-reporting-wizard", PATH_MAX);
        execvp(path, args);
        perror_msg_and_die("Can't execute %s", "bug-reporting-wizard");
    }
    else if(pid > 0)
    {
        if (flags & WAIT)
        {
            int status = 0;
            pid_t p = waitpid(pid, &status, WUNTRACED);
            if(p == -1)
            {
                error_msg("can't waitpid");
                return EXIT_FAILURE;
            }
            if (WIFEXITED(status))
            {
                VERB2 log("reporting finished with exitcode: status=%d\n", WEXITSTATUS(status));
                return WEXITSTATUS(status);
            }
            else if (WIFSIGNALED(status))
            {
                VERB2 log("reporting killed by signal %d\n", WTERMSIG(status));
            }
            else if (WIFSTOPPED(status))
            {
                /* should parent continue when the reporting is stopped??*/
                VERB2 log("reporting stopped by signal %d\n", WSTOPSIG(status));
            }
            else if (WIFCONTINUED(status))
            {
                VERB2 log("continued\n");
            }
        }
    }
    return 0;
}

int analyze_and_report_dir(const char* dirname, int flags)
{
    char *args[4];

    args[0] = (char *)"bug-reporting-wizard";
    args[1] = (char *)"--";
    args[2] = (char *)dirname;
    args[3] = NULL;

    run_reporter_ui(args, flags);
    return 0;
}

/* analyzes AND reports a problem saved on disk
 * - takes user through all the steps in reporting wizard
 */
int analyze_and_report(problem_data_t *pd, int flags)
{
    int result = 0;
    struct dump_dir *dd = create_dump_dir_from_problem_data(pd, "/tmp"/* /var/tmp ?? */);
    if (!dd)
        return -1;
    char *dir_name = strdup(dd->dd_dirname);
    dd_close(dd);
    VERB2 log("Temp problem dir: '%s'\n", dir_name);
    result = analyze_and_report_dir(dir_name, flags);

    /* if we wait for reporter to finish, we can try to clean the tmp dir */
    if (flags & WAIT)
    {
        dd = dd_opendir(dir_name, 0);
        if (dd)
        {
            if (dd_delete(dd) != 0)
            {
                error_msg("Can't remove tmp dir: %s", dd->dd_dirname);
            }
        }
    }
    free(dir_name);
    return result;
}

/* report() and report_dir() don't take flags, because in all known use-cases
 * it doesn't make sense to not wait for the result
 *
*/

/* reports a problem saved on disk
 * - shows only reporter selector and progress
*/
int report_dir(const char* dirname)
{
    char *args[5];

    args[0] = (char *)"bug-reporting-wizard";
    args[1] = (char *)"--report-only";
    args[2] = (char *)"--";
    args[3] = (char *)dirname;
    args[4] = NULL;

    int flags = WAIT;
    run_reporter_ui(args, flags);
    return 0;
}

int report(problem_data_t *pd)
{
    /* create hash from all components, so we at least eliminate the exact same
     * reports
    */
    char hash_str[SHA1_RESULT_LEN*2 + 1];
    g_hash_table_foreach(pd, &generate_hash_for_all, hash_str);
    add_to_problem_data(pd, FILENAME_DUPHASH, hash_str);

    /* adds:
     *  analyzer:libreport
     *  executable:readlink(/proc/<pid>/exe)
     * tries to guess component
    */
    add_basics_to_problem_data(pd);
    struct dump_dir *dd = create_dump_dir_from_problem_data(pd, "/tmp"/* /var/tmp ?? */);
    if (!dd)
        return -1;
    dd_create_basic_files(dd, getuid());
    char *dir_name = xstrdup(dd->dd_dirname);
    dd_close(dd);
    VERB2 log("Temp problem dir: '%s'\n", dir_name);
    report_dir(dir_name);
    free(dir_name);

    return 0;
}