aboutsummaryrefslogtreecommitdiff
path: root/src/recoverable-problem.c
blob: 7aff163b971ae47fcfe09cc01f2eee0b2c89201f (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
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Ted Gould <ted.gould@canonical.com>
 */

#include "recoverable-problem.h"
#include <glib/gstdio.h>
#include <string.h>
#include <errno.h>

/* Helpers to ensure we write nicely */
static void 
write_string (int          fd,
              const gchar *string)
{
	int res; 
	do
		res = write (fd, string, strlen (string));
	while (G_UNLIKELY (res == -1 && errno == EINTR));
}

/* Make NULLs fast and fun! */
static void 
write_null (int fd)
{
	int res; 
	do
		res = write (fd, "", 1);
	while (G_UNLIKELY (res == -1 && errno == EINTR));
}

/* Child watcher */
static gboolean
apport_child_watch (GPid pid G_GNUC_UNUSED, gint status G_GNUC_UNUSED, gpointer user_data)
{
	g_main_loop_quit((GMainLoop *)user_data);
	return FALSE;
}

static gboolean
apport_child_timeout (gpointer user_data)
{
	g_warning("Recoverable Error Reporter Timeout");
	g_main_loop_quit((GMainLoop *)user_data);
	return FALSE;
}

/* Code to report an error */
void
report_recoverable_problem (const gchar * signature, GPid report_pid, gboolean wait, gchar * additional_properties[])
{
	GSpawnFlags flags;
	gboolean first;
	GError * error = NULL;
	gint error_stdin = 0;
	GPid pid = 0;
	gchar * pid_str = NULL;
	gchar ** argv = NULL;
	gchar * argv_nopid[2] = {
		"/usr/share/apport/recoverable_problem",
		NULL
	};
	gchar * argv_pid[4] = {
		"/usr/share/apport/recoverable_problem",
		"-p",
		NULL, /* put pid_str when allocated here */
		NULL
	};


	argv = (gchar **)argv_nopid;

	if (report_pid != 0) {
		pid_str = g_strdup_printf("%d", report_pid);
		argv_pid[2] = pid_str;
		argv = (gchar**)argv_pid;
	}

	flags = G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL;
	if (wait) {
		flags |= G_SPAWN_DO_NOT_REAP_CHILD;
	}

	g_spawn_async_with_pipes(NULL, /* cwd */
		argv,
		NULL, /* envp */
		flags,
		NULL, NULL, /* child setup func */
		&pid,
		&error_stdin,
		NULL, /* stdout */
		NULL, /* stderr */
		&error);

	if (error != NULL) {
		g_warning("Unable to report a recoverable error: %s", error->message);
		g_error_free(error);
	}

	first = TRUE;

	if (error_stdin != 0 && signature != NULL) {
		write_string(error_stdin, "DuplicateSignature");
		write_null(error_stdin);
		write_string(error_stdin, signature);

		first = FALSE;
	}

	if (error_stdin != 0 && additional_properties != NULL) {
		gint i;
		for (i = 0; additional_properties[i] != NULL; i++) {
			if (!first) {
				write_null(error_stdin);
			} else {
				first = FALSE;
			}

			write_string(error_stdin, additional_properties[i]);
		}
	}

	if (error_stdin != 0) {
		close(error_stdin);
	}

	if (wait && pid != 0) {
		GSource * child_source, * timeout_source;
		GMainContext * context = g_main_context_new();
		GMainLoop * loop = g_main_loop_new(context, FALSE);

		child_source = g_child_watch_source_new(pid);
		g_source_attach(child_source, context);
		g_source_set_callback(child_source, (GSourceFunc)apport_child_watch, loop, NULL);

		timeout_source = g_timeout_source_new_seconds(5);
		g_source_attach(timeout_source, context);
		g_source_set_callback(timeout_source, apport_child_timeout, loop, NULL);

		g_main_loop_run(loop);

		g_source_destroy(timeout_source);
		g_source_destroy(child_source);
		g_main_loop_unref(loop);
		g_main_context_unref(context);

		g_spawn_close_pid(pid);
	}

	g_free(pid_str);

	return;
}