summaryrefslogtreecommitdiff
path: root/src/coredump.c
blob: 2990cc7b50bff6c96ade7ab25be6a996d7dbc091 (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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
#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>
 *	Tim Pepper <timothy.c.pepper@linux.intel.com>
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <asm/unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dirent.h>
#include <signal.h>
#include <glib.h>
#include <errno.h>

#include "corewatcher.h"

int uid = 0;
int sig = 0;

const char *core_folder = "/var/lib/corewatcher/";
const char *processed_folder = "/var/lib/corewatcher/processed/";

/* Always pick up the processing_mtx and then the
   processing_queue_mtx, reverse for setting down */
/* Always pick up the gdb_mtx and then the
   processing_queue_mtx, reverse for setting down */
/* Always pick up the processing_mtx and then the
   gdb_mtx, reverse for setting down */
/* so order for pick up should be:
   processing_mtx -> gdb_mtx -> processing_queue_mtx
   and the reverse for setting down */
GMutex processing_queue_mtx;
static char *processing_queue[MAX_PROCESSING_OOPS];
static int pq_tail = 0;
static int pq_head = 0;
GMutex gdb_mtx;

static char *get_release(void)
{
	FILE *file = NULL;
	char *line = NULL;
	size_t dummy = 0;

	file = fopen("/etc/os-release", "r");
	if (!file) {
		line = strdup("Unknown");
		return line;
	}

	while (!feof(file)) {
		if (getline(&line, &dummy, file) == -1)
			break;
		if (strstr(line, "VERSION_ID=")) {
			char *c = NULL;

			c = strchr(line, '\n');
			if (c) {
				*c = 0;
				c = strdup(&line[11]);
				fclose(file);
				free(line);
				return c;
			}
		}
	}

	fclose(file);
	free(line);

	line = strdup("Unknown");

	return line;
}

static char *set_wrapped_app(char *line)
{
	char *dline = NULL, *app = NULL, *appfile = NULL, *abs_path = NULL;
	char delim[] = " '";
	char app_folder[] = "/usr/share/";
	int r = 0;

	if (!line)
		return NULL;

	dline = strdup(line);

	app = strtok(dline, delim);
	while(app) {
		if (strcmp(app, "--app") == 0) {
			app = strtok(NULL, delim);
			break;
		}
		app = strtok(NULL, delim);
	}
	if (!app)
		goto cleanup_set_wrapped_app;
	r = asprintf(&abs_path, "%s%s", app_folder, app);
	if (r == -1) {
		abs_path = NULL;
		goto cleanup_set_wrapped_app;
	} else if (((unsigned int)r) != strlen(app_folder) + strlen(app)) {
		goto cleanup_set_wrapped_app;
	}

	appfile = find_executable(abs_path);

cleanup_set_wrapped_app:
	free(abs_path);
	free(dline);
	return appfile;
}

/*
 * Scan core dump in case a wrapper was used
 * to run the process and get the actual binary name
 */
static char *wrapper_scan(char *command)
{
	char *line = NULL, *appfile = NULL;
	FILE *file = NULL;

	file = popen(command, "r");
	if (!file)
		return NULL;

	while (!feof(file)) {
		size_t size = 0;
		int ret = 0;
		free(line);
		ret = getline(&line, &size, file);
		if (!size)
			break;
		if (ret < 0)
			break;

		if (strstr(line, "Core was generated by") &&
		    strstr(line, "--app")) {
			/* attempt to update appfile */
			appfile = set_wrapped_app(line);
			break;
		}
	}
	if (line)
		free(line);
	pclose(file);

	return appfile;
}

/*
 * Strip the directories from the path
 * given by fullname
 */
char *strip_directories(char *fullpath)
{
	char *dfile = NULL, *c1 = NULL, *c2 = NULL, *r = NULL;
	char delim[] = "/";

	if (!fullpath)
		return NULL;

	dfile = strdup(fullpath);
	if (!dfile)
		return NULL;

	c1 = strtok(dfile, delim);
	while(c1) {
		c2 = c1;
		c1 = strtok(NULL, delim);
	}

	if (c2)
		r = strdup(c2);
	free(dfile);

	return r;
}

/*
 * Move corefile from core_folder to processed_folder subdir.
 * If this type of core has recently been seen, unlink this more recent
 * example in order to rate limit submissions of extremely crashy
 * applications.
 * Add extension if given and attempt to create directories if needed.
 */
int move_core(char *fullpath, char *extension)
{
	char *corefilename = NULL, newpath[8192], *coreprefix = NULL;
	char *s = NULL;
	size_t prefix_len;
	DIR *dir = NULL;
	struct dirent *entry = NULL;

	if (!fullpath)
		return -1;

	if (!(corefilename = strip_directories(fullpath)))
		return -ENOMEM;

	/* if the corefile's name minus any suffixes (such as .$PID) and
	 * minus two additional characters (ie: last two digits of
	 * timestamp assuming core_%e_%t) matches another core file in the
	 * processed_folder, simply unlink it instead of processing it for
	 * submission.  TODO: consider a (configurable) time delta greater
	 * than which the cores must be separated, stat'ing the files, etc.
	 */
	if (!(coreprefix = strdup(corefilename)))
		return -ENOMEM;
	if (!(s = strstr(coreprefix, ".")))
		return -1;
	*s = '\0';
	prefix_len = strlen(coreprefix);
	if (prefix_len > 2) {
		s = strndup(coreprefix, prefix_len - 2);
		free(coreprefix);
		coreprefix = s;
	} else {
		goto error;
	}
	dir = opendir(processed_folder);
	if (!dir)
		goto error;
	while(1) {
		entry = readdir(dir);
		if (!entry || !entry->d_name)
			break;
		if (entry->d_name[0] == '.')
			continue;
		if (!strstr(entry->d_name, coreprefix))
			continue;
		fprintf(stderr, "+ ...ignoring/unlinking %s\n", fullpath);
		unlink(fullpath);
		goto error;
	}

	if (extension)
		snprintf(newpath, 8192, "%s%s.%s", processed_folder, corefilename, extension);
	else
		snprintf(newpath, 8192, "%s%s", processed_folder, corefilename);

	free(coreprefix);
	free(corefilename);
	rename(fullpath, newpath);

	return 0;
error:
	free(coreprefix);
	free(corefilename);
	return -1;
}

/*
 * Finds the full path for the application that crashed,
 * and moves file to processed_folder for processing
 */
static char *get_appfile(char *fullpath)
{
	char *appname = NULL, *appfile = NULL;

	if (!fullpath)
		return NULL;

	appname = find_coredump(fullpath);
	if (!appname)
		return NULL;

	/* don't try and do anything for rpm, gdb or corewatcher crashes */
	if (!(strcmp(appname, "rpm") && strcmp(appname, "gdb") && strcmp(appname, "corewatcher")))
		return NULL;

	appfile = find_executable(appname);
	/* appname no longer used, so free it as it was strdup'd */
	free(appname);
	if (!appfile)
		return NULL;

	move_core(fullpath, "to-process");

	return appfile;
}

/*
 * Use GDB to extract backtrace information from corefile
 */
static struct oops *extract_core(char *fullpath, char *appfile)
{
	struct oops *oops = NULL;
	int ret = 0;
	char *command = NULL, *h1 = NULL, *c1 = NULL, *c2 = NULL, *line = NULL;
	char *text = NULL, *at = NULL, *coretime = NULL;
	char *m1 = NULL, *m2 = NULL;
	FILE *file = NULL;
	char *badchar = NULL;
	char *release = get_release();
	int parsing_maps = 0;
	struct stat stat_buf;

	fprintf(stderr, "+ extract_core() called for %s\n", fullpath);

	if (asprintf(&command, "LANG=C gdb --batch -f %s %s -x /etc/corewatcher/gdb.command 2> /dev/null", appfile, fullpath) == -1)
		return NULL;

	if (stat(fullpath, &stat_buf) != -1) {
		coretime = ctime(&stat_buf.st_mtime);
	}
	if (coretime == NULL) {
		if (asprintf(&coretime, "Unknown\n") == -1)
			return NULL;
	}

	if ((at = wrapper_scan(command))) {
		free(appfile);
		appfile = at;
	}

	ret = asprintf(&h1,
		       "cmdline: %s\n"
		       "release: %s\n"
		       "time: %s",
		       appfile,
		       release,
		       coretime);
	free(release);
	if (ret == -1)
		h1 = strdup("Unknown");

	file = popen(command, "r");
	if (!file)
		fprintf(stderr, "+ gdb failed for %s\n", fullpath);

	while (file && !feof(file)) {
		size_t size = 0;

		free(line);
		line = NULL;
		ret = getline(&line, &size, file);
		if (!size)
			break;
		if (ret == -1)
			break;

		if (strncmp(line, "From", 4) == 0) {
			parsing_maps = 1;
			/*continue;*/
		}
		if (strncmp(line, "No shared libraries loaded at this time.", 40) == 0) {
			break;
		}

		if (!parsing_maps) { /* parsing backtrace */
			c2 = c1;
			if (line[0] != '#')
				continue;
fixup:			/* gdb outputs some 0x1a's which break XML */
			badchar = memchr(line, 0x1a, size);
			if (badchar) {
				*badchar = ' ';
				goto fixup;
			}

			if (c1) {
				c1 = NULL;
				if (asprintf(&c1, "%s        %s", c2, line) == -1)
					continue;
				free(c2);
			} else {
				/* keep going even if asprintf has errors */
				ret = asprintf(&c1, "        %s", line);
			}
		} else { /* parsing maps */
			m2 = m1;
			if (m1) {
				m1 = NULL;
				if (asprintf(&m1, "%s        %s", m2, line) == -1)
					continue;
				free(m2);
			} else {
				/* keep going even if asprintf has errors */
				ret = asprintf(&m1, "        %s", line);
			}
		}
	}
	if (line)
		free(line);
	if (file)
		pclose(file);
	free(command);

	if (!h1)
		h1 = strdup("Unknown");

	if (!m1)
		m1 = strdup("        Unknown");

	ret = asprintf(&text,
		       "%s"
		       "backtrace: |\n"
		       "%s"
		       "maps: |\n"
		       "%s",
		       h1, c1, m1);
	if (ret == -1)
		text = NULL;

	free(h1);
	free(c1);

	oops = malloc(sizeof(struct oops));
	if (!oops) {
		free(text);
		return NULL;
	}
	memset(oops, 0, sizeof(struct oops));
	oops->application = strdup(appfile);
	oops->text = text;
	oops->filename = strdup(fullpath);
	return oops;
}

/*
 * input filename has the form: core_$APP_$TIMESTAMP[.$PID]
 * output filename has form of: $APP_$TIMESTAMP[.ext]
 */
char *get_core_filename(char *filename, char *ext)
{
	char *name = NULL, *dotpid = NULL, *stamp = NULL, *detail_filename = NULL;

	if (!filename)
		return NULL;

	if (!(stamp = strstr(filename, "_")))
		return NULL;

	if (!(++stamp))
		return NULL;

	if (!(name = strdup(stamp)))
		return NULL;

	/* strip trailing .PID if present */
	dotpid = strstr(name, ".");
	if (dotpid)
		*dotpid = '\0';

	if (ext) {
		if ((asprintf(&detail_filename, "%s%s.%s", processed_folder, name, ext)) == -1) {
			free(name);
			return NULL;
		}
	} else {
		if ((asprintf(&detail_filename, "%s%s", processed_folder, name)) == -1) {
			free(name);
			return NULL;
		}
	}
	free(name);

	return detail_filename;
}

/*
 * Write the backtrace from the core file into a text
 * file named as $APP_$TIMESTAMP.txt
 */
static void write_core_detail_file(char *filename, char *text)
{
	int fd = 0;
	char *detail_filename = NULL;

	if (!filename || !text)
		return;

	if (!(detail_filename = get_core_filename(filename, "txt")))
		return;

	if ((fd = open(detail_filename, O_WRONLY | O_CREAT | O_TRUNC, 0)) != -1) {
		if(write(fd, text, strlen(text)) >= 0) {
			fchmod(fd, 0644);
		} else {
			fprintf(stderr, "+ ...ignoring/unlinking %s\n", detail_filename);
			unlink(detail_filename);
		}
		close(fd);
	}
	free(detail_filename);
}

/*
 * Removes corefile (core.XXXX) from the processing_queue.
 *
 * Expects the processing_queue_mtx to be held.
 */
static void remove_from_processing_queue(void)
{
	fprintf(stderr, "+ removing processing_queue head\n");
	free(processing_queue[pq_head]);
	processing_queue[pq_head++] = NULL;

	if (pq_head == MAX_PROCESSING_OOPS) {
		fprintf(stderr, "+ wrapping processing_queue head to 0 (bugs here?)\n");
		pq_head = 0;
	}
}

/*
 * Removes file from processing_oops hash based on core name,
 * extracting that core name from a fullpath such as
 * "/${processed_folder}/core_$APP_$TIMESTAMP.$PID"
 * in order to get just "$APP_$TIMESTAMP"
 *
 * Expects the lock on the given hash to be held.
 */
void remove_name_from_hash(char *fullpath, GHashTable *ht)
{
	char *name = NULL, *corename = NULL, *shortname = NULL;

	if (!(name = strip_directories(fullpath)))
		return;

	if (!(corename = get_core_filename(name, NULL))) {
		free(name);
		return;
	}
	free(name);

	if (!(shortname = strip_directories(corename))) {
		free(corename);
		return;
	}
	free(corename);

	if (g_hash_table_remove(ht, shortname))
		fprintf(stderr, "+ core %s removed from processing_oops hash table\n", shortname);
	else
		fprintf(stderr, "+ core %s not in processing_oops hash table\n", shortname);

	free(shortname);
}

/*
 * Common function for processing core
 * files to generate oops structures
 */
static struct oops *process_common(char *fullpath)
{
	struct oops *oops = NULL;
	char *appname = NULL, *appfile = NULL;

	if(!(appname = find_coredump(fullpath))) {
		return NULL;
	}

	if (!(appfile = find_executable(appname))) {
		free(appname);
		return NULL;
	}
	free(appname);

	if (!(oops = extract_core(fullpath, appfile))) {
		free(appfile);
		return NULL;
	}
	free(appfile);

	return oops;
}


/*
 * Processes .to-process core files.
 * Also creates the $APP_$TIMESTAMP.txt file and adds
 * the oops struct to the submit queue
 *
 * Picks up and sets down the gdb_mtx and
 * picks up and sets down the processing_queue_mtx.
 * (held at the same time in that order)
 */
static void *process_new(void __unused *vp)
{
	struct oops *oops = NULL;
	char *procfn = NULL, *corefn = NULL, *fullpath = NULL;

	g_mutex_lock(&core_status.processing_mtx);
	g_mutex_lock(&gdb_mtx);
	g_mutex_lock(&processing_queue_mtx);

	fprintf(stderr, "+ Entered process_new()\n");

	if (!(fullpath = processing_queue[pq_head])) {
		fprintf(stderr, "+ processing_queue corruption?\n");
		g_mutex_unlock(&processing_queue_mtx);
		g_mutex_unlock(&gdb_mtx);
		g_mutex_unlock(&core_status.processing_mtx);
		return NULL;
	}

	if (!(corefn = strip_directories(fullpath))) {
		fprintf(stderr, "+ No corefile? (%s)\n", fullpath);
		goto clean_process_new;
	}

	if (!(procfn = replace_name(fullpath, ".to-process", ".processed"))) {
		fprintf(stderr, "+ Problems with filename manipulation for %s\n", corefn);
		goto clean_process_new;
	}

	if (!(oops = process_common(fullpath))) {
		fprintf(stderr, "+ Problems processing %s\n", procfn);
		goto clean_process_new;
	}

	if (!(oops->detail_filename = get_core_filename(corefn, "txt"))) {
		fprintf(stderr, "+ Problems with filename manipulation for %s\n", procfn);
		goto clean_process_new;
	}

	if (rename(fullpath, procfn)) {
		fprintf(stderr, "+ Unable to move %s to %s\n", fullpath, procfn);
		goto clean_process_new;
	}

	free(oops->filename);
	oops->filename = procfn;

	remove_from_processing_queue();

	g_mutex_unlock(&processing_queue_mtx);
	g_mutex_unlock(&gdb_mtx);
	g_mutex_unlock(&core_status.processing_mtx);

	write_core_detail_file(corefn, oops->text);

	queue_backtrace(oops);

	fprintf(stderr, "+ Leaving process_new() with %s queued\n", oops->detail_filename);

	/* mustn't free procfn because it was hung on oops->filename */
	free(corefn);
	return NULL;

clean_process_new:
	remove_name_from_hash(fullpath, core_status.processing_oops);
	remove_from_processing_queue();
	free(procfn);
	free(corefn);
	FREE_OOPS(oops);
	g_mutex_unlock(&processing_queue_mtx);
	g_mutex_unlock(&gdb_mtx);
	g_mutex_unlock(&core_status.processing_mtx);
	return NULL;
}

/*
 * Reprocesses .processed core files.
 *
 * Picks up and sets down the gdb_mtx.
 * Picks up and sets down the processing_queue_mtx.
 * (held at the same time in that order)
 */
static void *process_old(void __unused *vp)
{
	struct oops *oops = NULL;
	char *corefn = NULL, *fullpath = NULL;

	g_mutex_lock(&gdb_mtx);
	g_mutex_lock(&processing_queue_mtx);

	fprintf(stderr, "+ Entered process_old()\n");

	if (!(fullpath = processing_queue[pq_head])) {
		fprintf(stderr, "+ processing_queue corruption?\n");
		g_mutex_unlock(&processing_queue_mtx);
		g_mutex_unlock(&gdb_mtx);
		return NULL;
	}
	fprintf(stderr, "+ Reprocessing %s\n", fullpath);

	if (!(corefn = strip_directories(fullpath))) {
		fprintf(stderr, "+ No corefile? (%s)\n", fullpath);
		goto clean_process_old;
	}

	if (!(oops = process_common(fullpath))) {
		fprintf(stderr, "+ Problems processing %s\n", corefn);
		goto clean_process_old;
	}

	if (!(oops->detail_filename = get_core_filename(corefn, "txt"))) {
		fprintf(stderr, "+ Problems with filename manipulation for %s\n", corefn);
		goto clean_process_old;
	}

	remove_from_processing_queue();

	g_mutex_unlock(&processing_queue_mtx);
	g_mutex_unlock(&gdb_mtx);

	queue_backtrace(oops);

	fprintf(stderr, "+ Leaving process_old() with %s queued\n", oops->detail_filename);

	free(corefn);
	return NULL;

clean_process_old:
	remove_name_from_hash(fullpath, core_status.processing_oops);
	remove_from_processing_queue();
	free(corefn);
	FREE_OOPS(oops);
	g_mutex_unlock(&processing_queue_mtx);
	g_mutex_unlock(&gdb_mtx);
	return NULL;
}

/*
 * Adds corefile (based on name) to the processing_oops
 * hash table if it is not already there, then
 * tries to add to the processing_queue.
 *
 * Picks up and sets down the processing_mtx.
 * Picks up and sets down the processing_queue_mtx.
 */
static int add_to_processing(char *fullpath)
{
	char *c1 = NULL, *c2 = NULL, *fp = NULL;

	if (!fullpath)
		return -1;

	if (!(fp = strdup(fullpath)))
		goto clean_add_to_processing;

	if (!(c1 = get_core_filename(fp, NULL)))
		goto clean_add_to_processing;

	if (!(c2 = strip_directories(c1)))
		goto clean_add_to_processing;

	free(c1);
	c1 = NULL;

	g_mutex_lock(&core_status.processing_mtx);
	if (g_hash_table_lookup(core_status.processing_oops, c2)) {
		g_mutex_unlock(&core_status.processing_mtx);
		fprintf(stderr, "+ ...name %s already in processing_oops hash table\n", c2);
		goto clean_add_to_processing;
	}

	g_mutex_lock(&processing_queue_mtx);
	if (processing_queue[pq_tail]) {
		g_mutex_unlock(&processing_queue_mtx);
		g_mutex_unlock(&core_status.processing_mtx);
		fprintf(stderr, "+ ...processing_queue full\n");
		goto clean_add_to_processing;
	}

	g_hash_table_insert(core_status.processing_oops, c2, c2);
	processing_queue[pq_tail++] = fp;
	if (pq_tail == MAX_PROCESSING_OOPS)
		pq_tail = 0;

	g_mutex_unlock(&processing_queue_mtx);
	g_mutex_unlock(&core_status.processing_mtx);
	return 0;
clean_add_to_processing:
	free(fp);
	free(c1);
	free(c2);
	return -1;
}

/*
 * Entry for processing new core files.
 */
static void process_corefile(char *fullpath)
{
	GThread *thrd = NULL;
	int r = 1;

	r = add_to_processing(fullpath);

	if (r)
		return;

	thrd = g_thread_new("process_new", process_new, NULL);
	if (thrd == NULL)
		fprintf(stderr, "Couldn't start thread for process_new()\n");
}

/*
 * Entry for processing already seen core files.
 */
static void reprocess_corefile(char *fullpath)
{
	GThread *thrd = NULL;
	int r = 0;

	r = add_to_processing(fullpath);

	if (r)
		return;

	thrd = g_thread_new("process_old", process_old, NULL);
	if (thrd == NULL)
		fprintf(stderr, "Couldn't start thread for process_old()\n");
}

static void scan_core_folder(void __unused *unused)
{
	/* scan for new crash data */
	DIR *dir = NULL;
	struct dirent *entry = NULL;
	char *fullpath = NULL, *appfile = NULL;
	int r = 0;

	dir = opendir(core_folder);
	if (!dir)
		return;
	fprintf(stderr, "+ Begin scanning %s...\n", core_folder);
	while(1) {
		free(fullpath);
		fullpath = NULL;

		entry = readdir(dir);
		if (!entry || !entry->d_name)
			break;
		if (entry->d_name[0] == '.')
			continue;
		if (strncmp(entry->d_name, "core_", 5))
			continue;

		/* matched core_#### */
		r = asprintf(&fullpath, "%s%s", core_folder, entry->d_name);
		if (r == -1) {
			fullpath = NULL;
			continue;
		} else if (((unsigned int)r) != strlen(core_folder) + strlen(entry->d_name)) {
			continue;
		}

		/* If one were to prompt the user before submitting, that
		 * might happen here.  */

		fprintf(stderr, "+ Looking at %s\n", fullpath);
		appfile = get_appfile(fullpath);

		if (!appfile) {
			fprintf(stderr, "+ ...ignoring/unlinking %s\n", fullpath);
			unlink(fullpath);
		} else {
			free(appfile);
			appfile = NULL;
		}
	}
	closedir(dir);
	fprintf(stderr, "+ End scanning %s...\n", core_folder);
}

static void scan_processed_folder(void __unused *unused)
{
	/* scan for partially processed crash data */
	DIR *dir = NULL;
	struct dirent *entry = NULL;
	char *fullpath = NULL;
	int r = 0;

	dir = opendir(processed_folder);
	if (!dir)
		return;
	fprintf(stderr, "+ Begin scanning %s...\n", processed_folder);
	while(1) {
		free(fullpath);
		fullpath = NULL;

		entry = readdir(dir);
		if (!entry || !entry->d_name)
			break;
		if (entry->d_name[0] == '.')
			continue;
		if (!strstr(entry->d_name, "process"))
			continue;

		r = asprintf(&fullpath, "%s%s", processed_folder, entry->d_name);
		if (r == -1) {
			fullpath = NULL;
			continue;
		} else if (((unsigned int)r) != strlen(processed_folder) + strlen(entry->d_name)) {
			continue;
		}

		fprintf(stderr, "+ Looking at %s\n", fullpath);
		if (strstr(fullpath, "to-process"))
			process_corefile(fullpath);
		else
			reprocess_corefile(fullpath);
	}
	closedir(dir);
	fprintf(stderr, "+ End scanning %s...\n", processed_folder);
}

int scan_corefolders(void __unused *unused)
{
	scan_core_folder(NULL);
	scan_processed_folder(NULL);

	return 1;
}