-
Notifications
You must be signed in to change notification settings - Fork 3
/
proc.c
301 lines (268 loc) · 8.17 KB
/
proc.c
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Watch Resources
* ------------------
* Copyright (C) 2012, Eduardo Silva P. <[email protected]>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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 Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "wr.h"
#include "proc.h"
char *human_readable_size(long size)
{
long u = 1024, i, len = 128;
char *buf = malloc(len);
static const char *__units[] = { "b", "K", "M", "G",
"T", "P", "E", "Z", "Y", NULL
};
for (i = 0; __units[i] != NULL; i++) {
if ((size / u) == 0) {
break;
}
u *= 1024;
}
if (!i) {
snprintf(buf, len, "%ld %s", size, __units[0]);
}
else {
float fsize = (float) ((double) size / (u / 1024));
snprintf(buf, len, "%.2f%s", fsize, __units[i]);
}
return buf;
}
/* Read file content to a memory buffer,
* Use this function just for really SMALL files
*/
static char *file_to_buffer(const char *path)
{
FILE *fp;
char *buffer;
long bytes;
if (access(path, R_OK) != 0) {
printf("Error: could not open '%s'\n", path);
exit(EXIT_FAILURE);
}
if (!(fp = fopen(path, "r"))) {
return NULL;
}
buffer = malloc(PROC_STAT_BUF_SIZE);
if (!buffer) {
fclose(fp);
printf("Error: could not allocate memory to open '%s'\n", path);
exit(EXIT_FAILURE);
}
bytes = fread(buffer, PROC_STAT_BUF_SIZE, 1, fp);
if (bytes < 0) {
free(buffer);
fclose(fp);
printf("Error: could not read '%s'\n", path);
exit(EXIT_FAILURE);
}
fclose(fp);
return (char *) buffer;
}
/*
* Given a Parent process ID, it store the child process ID on the childs
* array and set the number of PIDs stored on count. Initially count also
* serve to give the childs size in terms of slots. Under any error the
* function returns -1.
*/
int wr_proc_get_childs(pid_t ppid, struct wr_proc_task **childs, int *count)
{
pid_t p;
DIR *dir;
int childs_count = 0;
int childs_size = *count;
struct dirent *ent;
struct wr_proc_task *s;
/*
* first step is to scan the whole content of /proc/ to find out the
* processes available
*/
dir = opendir("/proc/");
if (!dir) {
return -1;
}
while ((ent = readdir(dir)) != NULL) {
if ((ent->d_name[0] == '.') || (strncmp(ent->d_name, "..", 2) == 0)) {
continue;
}
/* just directories */
if (ent->d_type != DT_DIR) {
continue;
}
/* only numeric values */
p = atol(ent->d_name);
if (p <= 0) {
continue;
}
s = wr_proc_stat(p);
if (s->ppid == ppid) {
if (childs_count >= childs_size) {
wr_proc_free(s);
*count = childs_count;
return 0;
}
childs[childs_count] = s;
childs_count++;
}
else {
wr_proc_free(s);
}
}
*count = childs_count;
closedir(dir);
return 0;
}
char *wr_proc_cmdline(pid_t pid)
{
char target[PROC_PID_SIZE];
snprintf(target, sizeof(PROC_PID_SIZE) -1, "/proc/%i/cmdline", pid);
return file_to_buffer(target);
}
struct wr_proc_task *wr_proc_stat(pid_t pid)
{
int ret;
char *p, *q;
char *buf;
char pid_path[PROC_PID_SIZE];
struct wr_proc_task *t = malloc(sizeof(struct wr_proc_task));
memset(t, '\0', sizeof(struct wr_proc_task));
/* Compose path for /proc/PID/stat */
ret = snprintf(pid_path, PROC_PID_SIZE, "/proc/%i/stat", pid);
if (ret < 0) {
printf("Error: could not compose PID path\n");
exit(EXIT_FAILURE);
}
buf = file_to_buffer(pid_path);
sscanf(buf, "%d", &t->pid);
/*
* workaround for process with spaces in the name, so we dont screw up
* sscanf(3).
*/
p = buf;
while (*p != '(') p++; p++;
q = p;
while (*q != ')') q++;
strncpy(t->comm, p, q - p);
q += 2;
/* Read pending values */
sscanf(q, PROC_STAT_FORMAT,
&t->state,
&t->ppid,
&t->pgrp,
&t->session,
&t->tty_nr,
&t->tpgid,
&t->flags,
&t->minflt,
&t->cminflt,
&t->majflt,
&t->cmajflt,
&t->utime,
&t->stime,
&t->cutime,
&t->cstime,
&t->priority,
&t->nice,
&t->num_threads,
&t->itrealvalue,
&t->starttime,
&t->vsize,
&t->rss,
&t->rlim,
&t->startcode,
&t->endcode,
&t->startstack,
&t->kstkesp,
&t->kstkeip,
&t->signal,
&t->blocked,
&t->sigignore,
&t->sigcatch,
&t->wchan,
&t->nswap,
&t->cnswap,
&t->exit_signal,
&t->processor,
&t->rt_priority,
&t->policy,
&t->delayacct_blkio_ticks);
/* Internal conversion */
t->wr_rss = (t->rss * wr_pagesize);
t->wr_rss_hr = human_readable_size(t->wr_rss);
t->wr_utime_s = (t->utime / wr_cpu_hz);
t->wr_utime_ms = ((t->utime * 1000) / wr_cpu_hz);
t->wr_stime_s = (t->stime / wr_cpu_hz);
t->wr_stime_ms = ((t->stime * 1000) / wr_cpu_hz);
return t;
}
void wr_proc_free(struct wr_proc_task *t)
{
free(t->wr_rss_hr);
free(t);
t = NULL;
}
void wr_proc_print(struct wr_proc_task *t)
{
printf("pid = %d\n", t->pid);
printf("comm = %s\n", t->comm);
printf("state = %c\n", t->state);
printf("ppid = %d\n", t->ppid);
printf("pgrp = %d\n", t->pgrp);
printf("session = %d\n", t->session);
printf("tty_nr = %d\n", t->tty_nr);
printf("tpgid = %d\n", t->tpgid);
printf("flags = %lu\n", t->flags);
printf("minflt = %lu\n", t->minflt);
printf("cminflt = %lu\n", t->cminflt);
printf("majflt = %lu\n", t->majflt);
printf("cmajflt = %lu\n", t->cmajflt);
printf("utime = %lu\n", t->utime);
printf("stime = %lu\n", t->stime);
printf("cutime = %ld\n", t->cutime);
printf("cstime = %ld\n", t->cstime);
printf("priority = %ld\n", t->priority);
printf("nice = %ld\n", t->nice);
printf("num_threads = %ld\n", t->num_threads);
printf("itrealvalue = %ld\n", t->itrealvalue);
printf("starttime = %lu\n", t->starttime);
printf("vsize = %lu\n", t->vsize);
printf("rss = %ld\n", t->rss);
printf("rlim = %lu\n", t->rlim);
printf("startcode = %lu\n", t->startcode);
printf("endcode = %lu\n", t->endcode);
printf("startstack = %lu\n", t->startstack);
printf("kstkesp = %lu\n", t->kstkesp);
printf("kstkeip = %lu\n", t->kstkeip);
printf("signal = %lu\n", t->signal);
printf("blocked = %lu\n", t->blocked);
printf("sigignore = %lu\n", t->sigignore);
printf("sigcatch = %lu\n", t->sigcatch);
printf("wchan = %lu\n", t->wchan);
printf("nswap = %lu\n", t->nswap);
printf("cnswap = %lu\n", t->cnswap);
printf("exit_signal = %d\n", t->exit_signal);
printf("processor = %d\n", t->processor);
printf("rt_priority = %lu\n", t->rt_priority);
printf("policy = %lu\n", t->policy);
printf("delayacct_blkio_ticks = %llu\n", t->delayacct_blkio_ticks);
fflush(stdout);
}