summaryrefslogtreecommitdiff
path: root/src/find_file.c
blob: 510eeb31aaa8bfe09cef2547fc51c793b1a52073 (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
#define _GNU_SOURCE
/*
 * Core dump watcher & collector
 *
 * (C) 2009 Intel Corporation
 *
 * 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; version 3 of the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <glib.h>

#include "corewatcher.h"

/* Attempt to find a file which would match the path/file fragment and
 * possibly represent a system binary.  We'll get more checking later when
 * we call gdb.  This is just a best effort candidate executable file to
 * hand to gdb along with a core. */
char *find_apppath(char *fragment)
{
	char *path = "/usr/bin";         /* ':' sep'd system path */
	char *c1, *c2;
	char *filename = NULL;
	char *candidate = NULL;
	char *apppath = NULL;
	int stripped = 0;

	fprintf(stderr, "+ Looking for %s\n", fragment);

	/* explicit absolute path in the standard directory */
	if (!strncmp(fragment, "/usr/bin", 8)) {
		if (!access(fragment, X_OK)) {
			fprintf(stderr, "+  found system executable %s\n", fragment);
			return strdup(fragment);
		}
		fprintf(stderr, "+  can't access system executable %s\n", fragment);
		return NULL;
	}

	/* explicit absolute path not in the standard directory */
	if (!strncmp(fragment, "/", 1)) {
		fprintf(stderr, "+  bad absolute path %s\n", fragment);
		return NULL;
	}

        /* relative path: problematic as we cannot at this point know the
	 * $PATH from the environment when the crash'd program started */
	if (strchr(fragment, '/')) {
	        candidate = strip_directories(fragment);
		fprintf(stderr, "+  stripped %s to %s\n", fragment, candidate);
		if (candidate == NULL)
			return NULL;
		stripped = 1;
	} else {
		/* fragment is just an executable's name */
		candidate = fragment;
	}

	/* search the path */
	c1 = path;
	while (c1 && strlen(c1)>0) {
		free(filename);
		filename = NULL;
		c2 = strchr(c1, ':');
		if (c2) *c2=0;
		fprintf(stderr, "+  looking in %s\n", c1);
		if(asprintf(&filename, "%s/%s", c1, candidate) == -1) {
			apppath = NULL;
			free(filename);
			goto out;
		}
		if (!access(filename, X_OK)) {
			printf("+  found %s\n", filename);
			apppath = filename;
			goto out;
		}
		c1 = c2;
		if (c2) c1++;
	}
out:
	if (stripped)
		free(candidate);
	return apppath;
}

char *find_causingapp(char *fullpath)
{
	char *line = NULL, *line_len = NULL, *c = NULL, *c2 = NULL;
	size_t size = 0;
	FILE *file = NULL;
	char *app = NULL, *command = NULL;

	if (asprintf(&command, "eu-readelf -n '%s'", fullpath) == -1)
		return NULL;

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

	while (!feof(file)) {
		if (getline(&line, &size, file) == -1)
			break;

		/* lines 4 chars and under won't have information we need */
		if (size < 5)
			continue;

		line_len = line + size;
		c = strstr(line,"psargs: ");
		if (c) {
			c += 8;
			if (c < line_len) {
				c2 = strchr(c, ' ');
				if (c2)
					*c2 = 0;
				c2 = strchr(c, '\n');
				if (c2)
					*c2 = 0;
				app = strdup(c);

				fprintf(stderr,"+ causing app: %s\n", app);
			}
		}

		c = strstr(line, "EUID: ");
		if (c) {
			c += 6;
			if (c < line_len) {
				int uid;
				sscanf(c, "%i", &uid);
				fprintf(stderr, "+ uid: %d\n", uid);
			}
		}

		c = strstr(line, "cursig: ");
		if (c) {
			c += 8;
			if (c < line_len) {
				int sig;
				sscanf(c, "%i", &sig);
				fprintf(stderr, "+ sig: %d\n", sig);
			}
		}
	}

	pclose(file);
	free(line);

	return app;
}