summaryrefslogtreecommitdiff
path: root/src/corewatcher.c
blob: d7b9fa85041c3b0711df40d3c17f1c31f36af0ab (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
#define _GNU_SOURCE
/*
 * Copyright 2007, Intel Corporation
 *
 * This file is part of corewatcher.org
 *
 * This program file 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; version 2 of the License.
 *
 * 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 in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authors:
 *	Arjan van de Ven <arjan@linux.intel.com>
 *	William Douglas <william.douglas@intel.com>
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sched.h>
#include <syslog.h>
#include <getopt.h>
#include <sys/prctl.h>
#include <asm/unistd.h>
#include <curl/curl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>

#include <glib.h>


/* see linux kernel doc Documentation/block/ioprio.txt */
#define IOPRIO_WHO_PROCESS 1
#define IOPRIO_CLASS_IDLE 3
#define IOPRIO_CLASS_SHIFT 13
#define IOPRIO_CLASS_DATA 7
#define IOPRIO_IDLE_LOWEST (IOPRIO_CLASS_DATA |  (IOPRIO_CLASS_IDLE << IOPRIO_CLASS_SHIFT))

#include "corewatcher.h"

/*
 * rather than malloc() on each inotify event, preallocate a decent chunk
 * of memory so multiple events can be read in one go, trading a little
 * extra memory for less runtime overhead if/when multiple crashes happen
 * in short order.
 */
#include <sys/inotify.h>
#define BUF_LEN 2048

static struct option opts[] = {
	{ "nodaemon", 0, NULL, 'n' },
	{ "debug",    0, NULL, 'd' },
	{ "always",   0, NULL, 'a' },
	{ "test",     0, NULL, 't' },
	{ "help",     0, NULL, 'h' },
	{ 0, 0, NULL, 0 }
};

struct core_status core_status;

int testmode = 0;

static void usage(const char *name)
{
	fprintf(stderr, "Usage: %s [OPTIONS...]\n", name);
	fprintf(stderr, "  -n, --nodaemon  Do not daemonize, run in foreground\n");
	fprintf(stderr, "  -d, --debug     Enable debug mode\n");
	fprintf(stderr, "  -t, --test      Do not send anything\n");
	fprintf(stderr, "  -h, --help      Display this help message\n");
}

gboolean inotify_source_prepare(__unused GSource *source, gint *timeout_)
{
	*timeout_ = -1;
	fprintf(stderr, "+ inotification prepare\n");
	return FALSE;
}

gboolean inotify_source_check(__unused GSource *source)
{
	int fd, wd;
	char buffer[BUF_LEN];
	size_t len;

	fprintf(stderr, "+ inotification check\n");
	/* inotification of crashes */
	fd = inotify_init();
	if (fd < 0) {
		fprintf(stderr, "corewatcher inotify init failed.. exiting\n");
		return EXIT_FAILURE;
	}
	wd = inotify_add_watch(fd, core_folder, IN_CLOSE_WRITE);
	if (wd < 0) {
		fprintf(stderr, "corewatcher inotify add failed.. exiting\n");
		return EXIT_FAILURE;
	}
	fprintf(stderr, "+ awaiting inotification...\n");
	len = read(fd, buffer, BUF_LEN);
	if (len <=0 ) {
		fprintf(stderr, "corewatcher inotify read failed.. exiting\n");
		return FALSE;
	}
	fprintf(stderr, "+ inotification received!\n");
	/* for now simply ignore the actual crash files we've been notified of
	 * and let our callback be dispatched to go look for any/all crash
	 * files */

	/* slight delay to minimize storms of notifications (the inotify
	 * read() can return a batch of notifications*/
	sleep(5);
	return TRUE;
}

gboolean inotify_source_dispatch(__unused GSource *source,
                                 GSourceFunc callback, gpointer user_data)
{
	fprintf(stderr, "+ inotify dispatch\n");
	if(callback(user_data)) {
		fprintf(stderr, "+ inotify dispatch success\n");
		return TRUE;
	} else {
		//should not happen as our callback always returns 1
		fprintf(stderr, "+ inotify dispatch failed.\n");
		return FALSE;
	}
}

void *inotify_loop(void __unused *unused)
{
	/* inotification of crashes */
	GMainLoop *loop;
	GMainContext *context;
	GSource *source;
	GSourceFuncs InotifySourceFuncs = {
		inotify_source_prepare,
		inotify_source_check,
		inotify_source_dispatch,
		NULL,
		NULL,
		NULL,
	};

	context = g_main_context_new();
	loop = g_main_loop_new(context, FALSE);
	loop = g_main_loop_ref(loop);
	source = g_source_new(&InotifySourceFuncs, sizeof(GSource));
	g_source_attach(source, context);
	g_source_set_callback(source, scan_corefolders, NULL, NULL);
	g_main_loop_run(loop);
	g_main_loop_unref(loop);

	return NULL;
}

int main(int argc, char**argv)
{
	GMainLoop *loop;
	int godaemon = 1;
	int debug = 0;
	int j = 0;
	DIR *dir = NULL;
	GThread *inotify_thread = NULL;

	g_thread_init (NULL);

	core_status.processing_oops = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
	core_status.queued_oops = g_hash_table_new(g_str_hash, g_str_equal);

/*
 * Signal the kernel that we're not timing critical
 */
#ifdef PR_SET_TIMERSLACK
	prctl(PR_SET_TIMERSLACK,1000*1000*1000, 0, 0, 0);
#endif
	/* Be easier on the rest of the system */
	if (syscall(__NR_ioprio_set, IOPRIO_WHO_PROCESS, getpid(),
		    IOPRIO_IDLE_LOWEST) == -1)
		perror("Can not set IO priority to lowest IDLE class");
	if (nice(15) < 0)
		perror("Can not set schedule priority");

	read_config_file("/etc/corewatcher/corewatcher.conf");

	/* insure our directories exist */
	dir = opendir(core_folder);
	if (!dir) {
		mkdir(core_folder, S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX);
		dir = opendir(core_folder);
		if (!dir)
			return 1;
	}
	chmod(core_folder, S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX);
	closedir(dir);
	dir = opendir(processed_folder);
	if (!dir) {
		mkdir(processed_folder, S_IRWXU);
		chmod(processed_folder, S_IRWXU);
		dir = opendir(processed_folder);
		if (!dir)
			return 1;
	}
	chmod(processed_folder, S_IRWXU);
	closedir(dir);

	while (1) {
		int c;
		int i;

		c = getopt_long(argc, argv, "adnth", opts, &i);
		if (c == -1)
			break;

		switch(c) {
		case 'n':
			fprintf(stderr, "+ Not running as daemon\n");
			godaemon = 0;
			break;
		case 'd':
			fprintf(stderr, "+ Starting corewatcher in debug mode\n");
			debug = 1;
			break;
		case 't':
			testmode = 1;
			fprintf(stderr, "+ Test mode enabled: not sending anything\n");
			break;
		case 'h':
			usage(argv[0]);
			return EXIT_SUCCESS;
		default:
			break;
		}
	}

	/*
	 * the curl docs say that we "should" call curl_global_init early,
	 * even though it'll be called later on via curl_easy_init().
	 * We ignore this advice, since 99.99% of the time this program
	 * will not use http at all, but the curl code does consume
	 * memory.
	 */

/*
	curl_global_init(CURL_GLOBAL_ALL);
*/

	if (godaemon && daemon(0, 0)) {
		fprintf(stderr, "corewatcher failed to daemonize.. exiting \n");
		return EXIT_FAILURE;
	}
	sched_yield();

	loop = g_main_loop_new(NULL, FALSE);
	loop = g_main_loop_ref(loop);

	if (!debug)
		sleep(20);

	scan_corefolders(NULL);

	if (testmode) {
		fprintf(stderr, "+ Exiting from testmode\n");
		goto out;
	}

	inotify_thread = g_thread_new("corewatcher inotify", inotify_loop, NULL);
	if (inotify_thread == NULL)
		fprintf(stderr, "+ Unable to start inotify thread\n");

	/*
	 * TODO: add a thread / event source tied to a connmand plugin
	 *  o  network up: trigger scan_corefolders(), enables event sources
	 *  o  network down: disable sources (or allow them to run and create
	 *     a low quality crash reports?)
	 *  o  low bandwidth net up: allow transmitting of .txt crash
	 *     summaries (ie: no running gdb)
	 *  o  high bandwidth net up: look at existing cores vs .txt's for
	 *     quality and if debuginfo is retrievable, try to improve
	 *     the report quality and submit again
	 */

	/*
	 * long poll for crashes: at inotify time we might not have been
	 * able to fully process things, here we'd push those reports out
	 */
	g_timeout_add_seconds(900, scan_corefolders, NULL);

	g_main_loop_run(loop);
out:
	g_main_loop_unref(loop);

	for (j = 0; j < url_count; j++)
		free(submit_url[j]);

	g_hash_table_destroy(core_status.processing_oops);
	g_hash_table_destroy(core_status.queued_oops);

	return EXIT_SUCCESS;
}