-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistproc.c
More file actions
executable file
·145 lines (130 loc) · 3.15 KB
/
listproc.c
File metadata and controls
executable file
·145 lines (130 loc) · 3.15 KB
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
/**
* This program lists the PID and command name of all programs being run by
* the speicified user. It makes use of the /proc directory to do this.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <ctype.h>
#define PID_PATH_MAX 100
#define SYM_MAX_SZ 100
void error(char *reason) {
puts(reason);
exit(EXIT_FAILURE);
}
int skipline(int fd) {
int nread;
char c;
while ((nread = read(fd, &c, 1)) > 0) {
if (c == '\n') {
return 1;
}
}
if (nread == -1)
error("read\n");
return 0;
}
int seeksym(int fd, char *sym) {
int len, cseek, nread;
char buf[SYM_MAX_SZ];
len = strlen(sym);
if (len > SYM_MAX_SZ)
error("seeksym sym too long\n");
if (lseek(fd, 0, SEEK_SET) == -1)
error("lseek\n");
cseek = 0;
while ((nread = read(fd, buf, len)) == len) {
if (strncmp(sym, buf, len) == 0) {
if (lseek(fd, cseek, SEEK_SET) == -1)
error("lseek\n");
return 1;
}
if (!skipline(fd))
break;
if ((cseek = (int)lseek(fd, 0, SEEK_CUR)) == -1)
error("lseek\n");
}
if (nread == -1)
error("read\n");
return 0;
}
int main(int argc, char **argv) {
struct dirent *dirent = NULL;
DIR *dirp = NULL;
int pid, fd, data_s, data_e;
char *endptr, fp[PID_PATH_MAX], buf[100];
struct passwd *pwd;
uid_t uid;
if (argc < 2)
error("Missing argument. listproc username.\n");
if ((dirp = opendir("/proc")) == NULL)
error("opendir\n");
errno = 0;
if ((pwd = getpwnam(argv[1])) == NULL) {
if (errno != 0)
error("getpwnam\n");
else
error("can't find user\n");
}
uid = pwd->pw_uid;
errno = 0;
printf("PID\tUser\tProcess\n");
while ((dirent = readdir(dirp)) != NULL) {
pid = (int)strtol(dirent->d_name, &endptr, 10);
if (*endptr != '\0')
continue;
if (sprintf(fp, "/proc/%d/status", pid) < 0)
error("sprintf\n");
if ((fd = open(fp, O_RDONLY)) == -1)
error("open\n");
if (seeksym(fd, "Uid:") != 1)
error("can't find Uid: entry in /status\n");
data_e = -1;
if ((data_s = lseek(fd, 5, SEEK_CUR)) == -1)
error("lseek\n");
while ((read(fd, buf, 1)) == 1) {
if (!isalnum(buf[0])) {
if((data_e = lseek(fd, 0, SEEK_CUR)) == -1)
error("lseek\n");
break;
}
}
if (data_e == -1)
error("error finding delimiter for data\n");
if ((lseek(fd, data_s, SEEK_SET)) == -1)
error("lseek\n");
if (read(fd, buf, data_e - data_s) == -1)
error("read\n");
buf[data_e - data_s - 1] = '\0';
if (uid != (uid_t)strtol(buf, NULL, 10))
continue;
if (seeksym(fd, "Name:") != 1)
error("Couldn't find Name in /status\n");
if ((data_s = lseek(fd, 6, SEEK_CUR)) == -1)
error("lseek\n");
while ((read(fd, buf, 1)) == 1) {
if (!isalnum(buf[0])) {
if((data_e = lseek(fd, 0, SEEK_CUR)) == -1)
error("lseek\n");
break;
}
}
if (data_e == -1)
error("error finding delimiter for data\n");
if ((lseek(fd, data_s, SEEK_SET)) == -1)
error("lseek\n");
if (read(fd, buf, data_e - data_s) == -1)
error("read\n");
buf[data_e - data_s - 1] = '\0';
printf("%d\t%s\t%s\n", pid, argv[1], buf);
errno = 0;
}
if (errno > 0)
error("readdir\n");
exit(EXIT_SUCCESS);
}