summaryrefslogtreecommitdiff
path: root/src/find_file.c
blob: f520cac381f8539fce3a10e456c7b7d33ef8602e (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
#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>

#ifndef PATH_MAX
#define PATH_MAX 4096
#endif

#include "corewatcher.h"

char *find_apppath(char *fragment)
{
	char *path, *c1, *c2;
	char *filename = NULL;

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

	if (fragment == NULL || strlen(fragment) < 3)
		return NULL;

	/* Deal with absolute paths first */
	if (!access(fragment, X_OK)) {
		if (!(filename = strdup(fragment)))
			return NULL;
		return filename;
	}

	path = strdup(getenv("PATH"));

	c1 = path;
	while (c1 && strlen(c1)>0) {
		free(filename);
		filename = NULL;
		c2 = strchr(c1, ':');
		if (c2) *c2=0;
		if(asprintf(&filename, "%s/%s", c1, fragment) == -1)
			return NULL;
		if (!access(filename, X_OK)) {
			printf("+ Found %s\n", filename);
			free(path);
			return filename;
		}
		c1 = c2;
		if (c2) c1++;
	}
	free(path);
	free(filename);
	return NULL;
}

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;
}