aboutsummaryrefslogtreecommitdiff
blob: 3e54485364306bcffc8ffe823138fef598fdf1a2 (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
/******************************************************************************/
/* THE BEER-WARE LICENSE   (Revision 42):                                     */
/*  As long as you retain this notice you can do whatever you want with this  */
/*   stuff. If we meet some day, and you think this stuff is worth it,        */
/*   you can buy me a beer in return.    Ned Ludd. --solarx                   */
/******************************************************************************/

/*
 * normal compile.
 *  cc -o pspax pspax.c
 * or with libcap. 
 *  cc -o pspax pspax.c -DWANT_SYSCAP -lcap
 *
 */

#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <libgen.h>
#include <unistd.h>
#include <dirent.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <pwd.h>

#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <getopt.h>

#include "paxelf.h"

#ifdef WANT_SYSCAP
#undef _POSIX_SOURCE
#include <sys/capability.h>
#endif

#define PROC_DIR "/proc"

static const char *rcsid = "$Id: pspax.c,v 1.12 2005/05/06 02:22:12 solar Exp $";
#define argv0 "pspax"



/* variables to control behavior */
static char show_all = 0;
static char show_banner = 1;



static char *get_proc_name(pid_t pid)
{
	FILE *fp;
	static char buf[PATH_MAX];
	memset(&buf, 0, sizeof(buf));

	snprintf(buf, sizeof(buf), PROC_DIR "/%d/stat", (int) pid);
	if ((fp = fopen(buf, "r")) == NULL)
		return NULL;

	memset(&buf, 0, sizeof(buf));

	fscanf(fp, "%*d %s.16", buf);
	if (*buf) {
		buf[strlen(buf) - 1] = '\0';
		buf[16] = 0;
	}
	fclose(fp);
	return (buf+1);
}

static struct passwd *get_proc_uid(pid_t pid)
{
	struct passwd *pwd;
	struct stat st;
	static char s[PATH_MAX];

	snprintf(s, sizeof(s), PROC_DIR "/%d/stat", (int) pid);

	/* this is bullshit but getpwuid() is leaking memory
	 * and I've wasted a few hrs 1 day tracking it down.
	 * I forgot I tracked it down before and saw pspax leaking
	 * memory so I tracked it down again (silly me)
	 * anyway.. please leave this comment here so I don't waste my
	 * time again the next time I forget.
	 * and till such time as getpwuid()/nis/nss/pam or whatever does not suck.
	 */
	if ((stat(s, &st)) != (-1))
		if ((pwd = getpwuid(st.st_uid)) != NULL)
			return pwd;
	return NULL;
}

static char *get_proc_status(pid_t pid, char *name)
{
	FILE *fp;
	size_t len;
	static char s[PATH_MAX];

	snprintf(s, sizeof(s), PROC_DIR "/%d/status", (int) pid);
	if ((fp = fopen(s, "r")) == NULL)
		return NULL;

	len = strlen(name);
	while (fgets(s, sizeof(s), fp)) {
		if (strncasecmp(s, name, len) == 0) {
			if (s[len] == ':') {
				fclose(fp);
				s[strlen(s) - 1] = 0;
				return (s + len + 2);
			}
		}
	}
	fclose(fp);
	return NULL;
}

static char *get_pid_attr(int pid)
{
	FILE *fp;
	char *p;
	char s[32];
	static char buf[BUFSIZ];

	memset(buf, 0, sizeof(buf));

	snprintf(s, sizeof(s), PROC_DIR "/%d/attr/current", pid);
	if ((fp = fopen(s, "r")) == NULL)
		return NULL;
	if (fgets(buf, sizeof(buf), fp) != NULL)
		if ((p = strchr(buf, '\n')) != NULL)
			*p = 0;
	fclose(fp);
	return buf;
}

static const char *get_pid_type(int pid)
{
	char fname[32];
	elfobj *elf = NULL;
	char *ret = NULL;

	snprintf(fname, sizeof(fname), PROC_DIR "/%d/exe", pid);
	if ((elf = readelf(fname)) == NULL)
		return ret;
	ret = (char *)get_elfetype(elf);
	unreadelf(elf);
	return ret;
}

static void pspax()
{
	register DIR *dir;
	register struct dirent *de;
	pid_t pid;
	int have_attr;
	struct passwd *uid;
	struct stat st;
	const char *pax, *type, *name, *caps, *attr;
#ifdef WANT_SYSCAP
	ssize_t length;
	cap_t cap_d;
	cap_d = cap_init();
#endif

	caps = NULL;

	chdir(PROC_DIR);
	if (!(dir = opendir(PROC_DIR))) {
		perror(PROC_DIR);
		exit(EXIT_FAILURE);
	}

	if (access("/proc/self/attr/current", R_OK) != (-1))
		have_attr = 1;
	else
		have_attr = 0;

	if (show_banner)
		printf("%-8s %-6s %-6s %-10s %-16s %-4s %-4s\n",
		       "USER", "PID", "PAX", "ELF_TYPE", "NAME", "CAPS", "ATTR");

	while ((de = readdir(dir))) {
		errno = 0;
		stat(de->d_name, &st);
		if ((errno != ENOENT) && (errno != EACCES)) {
			pid = (pid_t) atoi((char *) basename((char *) de->d_name));
			if (!pid)
				continue;

#ifdef WANT_SYSCAP
			/* this is a non-POSIX function */
			capgetp(pid, cap_d);
			caps = cap_to_text(cap_d, &length);
#endif

			uid = get_proc_uid(pid);
			pax = get_proc_status(pid, "PAX");
			type = get_pid_type(pid);
			name = get_proc_name(pid);
			attr = (have_attr ? get_pid_attr(pid) : NULL);

			if (show_all || type)
				printf("%-8s %-6d %-6s %-10s %-16s %-4s %s\n",
				       uid  ? uid->pw_name : "--------",
				       pid,
				       pax  ? pax  : "---",
				       type ? type : "-------",
				       name ? name : "-----",
				       caps ? caps : " = ",
				       attr ? attr : "-");
#ifdef WANT_SYSCAP
			if (caps)
				cap_free(caps);
#endif
		}
	}
	closedir(dir);
}



/* usage / invocation handling functions */
#define PARSE_FLAGS "aBhV"
static struct option const long_opts[] = {
	{"all",       no_argument, NULL, 'a'},
	{"nobanner",  no_argument, NULL, 'B'},
	{"help",      no_argument, NULL, 'h'},
	{"version",   no_argument, NULL, 'V'},
	{NULL,        no_argument, NULL, 0x0}
};
static char *opts_help[] = {
	"Show all processes\n",
	"Don't display the header",
	"Print this help and exit",
	"Print version and exit",
	NULL
};

/* display usage and exit */
static void usage(int status)
{
	int i;
	printf("� List ELF/PaX information about running processes\n\n"
	       "Usage: %s [options]\n\n", argv0);
	fputs("Options:\n", stdout);
	for (i = 0; long_opts[i].name; ++i)
		printf("  -%c, --%-12s� %s\n", long_opts[i].val, 
		       long_opts[i].name, opts_help[i]);
#ifdef MANLYPAGE
	for (i = 0; long_opts[i].name; ++i)
		printf(".TP\n\\fB\\-%c, \\-\\-%s\\fR\n%s\n", long_opts[i].val, 
		       long_opts[i].name, opts_help[i]);
#endif
	exit(status);
}

/* parse command line arguments and preform needed actions */
static void parseargs(int argc, char *argv[])
{
	int flag;

	opterr = 0;
	while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
		switch (flag) {

		case 'V':                        /* version info */
			printf("%s compiled %s\n%s\n"
			       "%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n",
			       __FILE__, __DATE__, rcsid, argv0);
			exit(EXIT_SUCCESS);
			break;
		case 'h': usage(EXIT_SUCCESS); break;

		case 'B': show_banner = 0; break;
		case 'a': show_all = 1; break;

		case ':':
			warn("Option missing parameter");
			usage(EXIT_FAILURE);
			break;
		case '?':
			warn("Unknown option");
			usage(EXIT_FAILURE);
			break;
		default:
			err("Unhandled option '%c'", flag);
			break;
		}
	}
}



int main(int argc, char *argv[])
{
	parseargs(argc, argv);
	pspax();
	return EXIT_SUCCESS;
}