50ee571b43711db257952c5de1b8efd58f688fc0
[monky] / src / proc.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *   (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include <logging.h>
32 #include "conky.h"
33 #include "proc.h"
34 #include <unistd.h>
35 #include <ctype.h>
36 #include <dirent.h>
37
38 char* readfile(char* filename, int* total_read, char showerror) {
39         FILE* file;
40         char* buf = NULL;
41         int bytes_read;
42
43         *total_read = 0;
44         file = fopen(filename, "r");
45         if(file) {
46                 do {
47                         buf = realloc(buf, *total_read + READSIZE + 1);
48                         bytes_read = fread(buf + *total_read, 1, READSIZE, file);
49                         *total_read += bytes_read;
50                         buf[*total_read] = 0;
51                 }while(bytes_read != 0);
52                 fclose(file);
53         } else if(showerror != 0) {
54                 NORM_ERR(READERR, filename);
55         }
56         return buf;
57 }
58
59 void pid_readlink(char *file, char *p, int p_max_size)
60 {
61         char buf[p_max_size];
62
63         memset(buf, 0, p_max_size);
64         if(readlink(file, buf, p_max_size) >= 0) {
65                 snprintf(p, p_max_size, "%s", buf);
66         } else {
67                 NORM_ERR(READERR, file);
68         }
69 }
70
71 struct ll_string {
72         char *string;
73         struct ll_string* next;
74 };
75
76 struct ll_string* addnode(struct ll_string* end, char* string) {
77         struct ll_string* current = malloc(sizeof(struct ll_string));
78         current->string = strdup(string);
79         current->next = NULL;
80         if(end != NULL) end->next = current;
81         return current;
82 }
83
84 void freelist(struct ll_string* front) {
85         if(front != NULL) {
86                 free(front->string);
87                 if(front->next != NULL) {
88                         freelist(front->next);
89                 }
90                 free(front);
91         }
92 }
93
94 int inlist(struct ll_string* front, char* string) {
95         struct ll_string* current;
96
97         for(current = front; current != NULL; current = current->next) {
98                 if(strcmp(current->string, string) == 0) {
99                         return 1;
100                 }
101         }
102         return 0;
103 }
104
105 void print_pid_chroot(struct text_object *obj, char *p, int p_max_size) {
106         char *buffer;
107
108         asprintf(&buffer, PROCDIR "/%s/root", obj->data.s);
109         pid_readlink(buffer, p, p_max_size);
110         free(buffer);
111 }
112
113 void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
114 {
115         char* buf;
116         int i, bytes_read;
117
118         asprintf(&buf, PROCDIR "/%s/cmdline", obj->data.s);
119         strcpy(obj->data.s, buf);
120         free(buf);
121         buf = readfile(obj->data.s, &bytes_read, 1);
122         if(buf != NULL) {
123                 for(i = 0; i < bytes_read-1; i++) {
124                         if(buf[i] == 0) {
125                                 buf[i] = ' ';
126                         }
127                 }
128                 snprintf(p, p_max_size, "%s", buf);
129                 free(buf);
130         }
131 }
132
133 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
134 {
135         char buf[p_max_size];
136         int bytes_read;
137
138         sprintf(buf, PROCDIR "/%s/cwd", obj->data.s);
139         strcpy(obj->data.s, buf);
140         bytes_read = readlink(obj->data.s, buf, p_max_size);
141         if(bytes_read != -1) {
142                 buf[bytes_read] = 0;
143                 snprintf(p, p_max_size, "%s", buf);
144         } else {
145                 NORM_ERR(READERR, obj->data.s);
146         }
147 }
148
149 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
150 {
151         int i, total_read;
152         pid_t pid;
153         char *buf, *file, *var=strdup(obj->data.s);;
154
155         if(sscanf(obj->data.s, "%d %s", &pid, var) == 2) {
156                 for(i = 0; var[i] != 0; i++) {
157                         var[i] = toupper(var[i]);
158                 }
159                 asprintf(&file, PROCDIR "/%d/environ", pid);
160                 buf = readfile(file, &total_read, 1);
161                 free(file);
162                 if(buf != NULL) {
163                         for(i = 0; i < total_read; i += strlen(buf + i) + 1) {
164                                 if(strncmp(buf + i, var, strlen(var)) == 0 && *(buf + i + strlen(var)) == '=') {
165                                         snprintf(p, p_max_size, "%s", buf + i + strlen(var) + 1);
166                                         free(buf);
167                                         free(var);
168                                         return;
169                                 }
170                         }
171                         free(buf);
172                 }
173                 free(var);
174                 *p = 0;
175         }
176 }
177
178 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
179 {
180         char *buf = NULL;
181         char *buf2;
182         int bytes_read, total_read;
183         int i = 0;
184
185         asprintf(&buf, PROCDIR "/%s/environ", obj->data.s);
186         strcpy(obj->data.s, buf);
187         free(buf);
188         buf = readfile(obj->data.s, &total_read, 1);
189         if(buf != NULL) {
190                 for(bytes_read = 0; bytes_read < total_read; buf[i-1] = ';') {
191                         buf2 = strdup(buf+bytes_read);
192                         bytes_read += strlen(buf2)+1;
193                         sscanf(buf2, "%[^=]", buf+i);
194                         free(buf2);
195                         i = strlen(buf) + 1;
196                 }
197                 buf[i-1] = 0;
198                 snprintf(p, p_max_size, "%s", buf);
199                 free(buf);
200         }
201 }
202
203 void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
204         char *buffer;
205
206         asprintf(&buffer, PROCDIR "/%s/exe", obj->data.s);
207         pid_readlink(buffer, p, p_max_size);
208         free(buffer);
209 }
210
211 void print_pid_nice(struct text_object *obj, char *p, int p_max_size) {
212         char *buf = NULL;
213         int bytes_read;
214         long int nice_value;
215
216         if(*(obj->data.s) != 0) {
217                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
218                 strcpy(obj->data.s, buf);
219                 free(buf);
220                 buf = readfile(obj->data.s, &bytes_read, 1);
221                 if(buf != NULL) {
222                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %ld", &nice_value);
223                         snprintf(p, p_max_size, "%ld", nice_value);
224                         free(buf);
225                 }
226         } else {
227                 NORM_ERR("$pid_nice didn't receive a argument");
228         }
229 }
230
231 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
232         DIR* dir;
233         struct dirent *entry;
234         char buf[p_max_size];
235         int length, totallength = 0;
236         struct ll_string* files_front = NULL;
237         struct ll_string* files_back = NULL;
238
239         dir = opendir(obj->data.s);
240         if(dir != NULL) {
241                 while ((entry = readdir(dir))) {
242                         if(entry->d_name[0] != '.') {
243                                 snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
244                                 length = readlink(buf, buf, p_max_size);
245                                 buf[length] = 0;
246                                 if(inlist(files_front, buf) == 0) {
247                                         files_back = addnode(files_back, buf);
248                                         snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
249                                         totallength += length + strlen("; ");
250                                 }
251                                 if(files_front == NULL) {
252                                         files_front = files_back;
253                                 }
254                         }
255                 }
256                 closedir(dir);
257                 freelist(files_front);
258                 p[totallength - strlen("; ")] = 0;
259         } else {
260                 p[0] = 0;
261         }
262 }
263
264 void print_pid_parent(struct text_object *obj, char *p, int p_max_size) {
265 #define PARENT_ENTRY "PPid:\t"
266 #define PARENTNOTFOUND  "Can't find the process parent in '%s'"
267         char *begin, *end, *buf = NULL;
268         int bytes_read;
269
270         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
271         strcpy(obj->data.s, buf);
272         free(buf);
273         buf = readfile(obj->data.s, &bytes_read, 1);
274         if(buf != NULL) {
275                 begin = strstr(buf, PARENT_ENTRY);
276                 if(begin != NULL) {
277                         begin += strlen(PARENT_ENTRY);
278                         end = strchr(begin, '\n');
279                         if(end != NULL) {
280                                 *(end) = 0;
281                         }
282                         snprintf(p, p_max_size, "%s", begin);
283                 } else {
284                         NORM_ERR(PARENTNOTFOUND, obj->data.s);
285                 }
286                 free(buf);
287         }
288 }
289
290 void print_pid_priority(struct text_object *obj, char *p, int p_max_size) {
291         char *buf = NULL;
292         int bytes_read;
293         long int priority;
294
295         if(*(obj->data.s) != 0) {
296                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
297                 strcpy(obj->data.s, buf);
298                 free(buf);
299                 buf = readfile(obj->data.s, &bytes_read, 1);
300                 if(buf != NULL) {
301                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %ld", &priority);
302                         snprintf(p, p_max_size, "%ld", priority);
303                         free(buf);
304                 }
305         } else {
306                 NORM_ERR("$pid_priority didn't receive a argument");
307         }
308 }
309
310 void print_pid_state(struct text_object *obj, char *p, int p_max_size) {
311 #define STATE_ENTRY "State:\t"
312 #define STATENOTFOUND   "Can't find the process state in '%s'"
313         char *begin, *end, *buf = NULL;
314         int bytes_read;
315
316         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
317         strcpy(obj->data.s, buf);
318         free(buf);
319         buf = readfile(obj->data.s, &bytes_read, 1);
320         if(buf != NULL) {
321                 begin = strstr(buf, STATE_ENTRY);
322                 if(begin != NULL) {
323                         begin += strlen(STATE_ENTRY) + 3;       // +3 will strip the char representing the short state and the space and '(' that follow
324                         end = strchr(begin, '\n');
325                         if(end != NULL) {
326                                 *(end-1) = 0;   // -1 strips the ')'
327                         }
328                         snprintf(p, p_max_size, "%s", begin);
329                 } else {
330                         NORM_ERR(STATENOTFOUND, obj->data.s);
331                 }
332                 free(buf);
333         }
334 }
335
336 void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
337         char *begin, *buf = NULL;
338         int bytes_read;
339
340         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
341         strcpy(obj->data.s, buf);
342         free(buf);
343         buf = readfile(obj->data.s, &bytes_read, 1);
344         if(buf != NULL) {
345                 begin = strstr(buf, STATE_ENTRY);
346                 if(begin != NULL) {
347                         snprintf(p, p_max_size, "%c", *begin);
348                 } else {
349                         NORM_ERR(STATENOTFOUND, obj->data.s);
350                 }
351                 free(buf);
352         }
353 }
354
355 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
356         char *buffer;
357
358         asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
359         pid_readlink(buffer, p, p_max_size);
360         free(buffer);
361 }
362
363 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
364         char *buffer;
365
366         asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
367         pid_readlink(buffer, p, p_max_size);
368         free(buffer);
369 }
370
371 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
372         char *buffer;
373
374         asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
375         pid_readlink(buffer, p, p_max_size);
376         free(buffer);
377 }
378
379 void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
380         unsigned int i;
381
382         if(strlen(arg) > 0) {
383                 obj->data.s = strdup(arg);
384                 for(i = 0; obj->data.s[i] != 0; i++) {
385                         while(obj->data.s[i] == ' ' && obj->data.s[i + 1] == ' ') {
386                                 memmove(obj->data.s + i, obj->data.s + i + 1, strlen(obj->data.s + i + 1) + 1);
387                         }
388                 }
389                 if(obj->data.s[i - 1] == ' ') {
390                         obj->data.s[i - 1] = 0;
391                 }
392         } else {
393                 CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
394         }
395 }
396
397 void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
398         DIR* dir;
399         struct dirent *entry;
400         char *filename, *buf;
401         int bytes_read, i;
402
403         dir = opendir(PROCDIR);
404         if(dir != NULL) {
405                 while ((entry = readdir(dir))) {
406                         asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
407                         buf = readfile(filename, &bytes_read, 0);
408                         free(filename);
409                         if(buf != NULL) {
410                                 for(i = 0; i < bytes_read - 1; i++) {
411                                         if(buf[i] == 0) buf[i] = ' ';
412                                 }
413                                 if(strstr(buf, obj->data.s) != NULL) {
414                                         snprintf(p, p_max_size, "%s", entry->d_name);
415                                         free(buf);
416                                         closedir(dir);
417                                         return;
418                                 }
419                                 free(buf);
420                         }
421                 }
422                 closedir(dir);
423         } else {
424                 NORM_ERR(READERR, PROCDIR);
425         }
426 }
427
428 void print_pid_threads(struct text_object *obj, char *p, int p_max_size) {
429 #define THREADS_ENTRY "Threads:\t"
430 #define THREADSNOTFOUND "Can't find the number of the threads of the process in '%s'"
431         char *begin, *end, *buf = NULL;
432         int bytes_read;
433
434         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
435         strcpy(obj->data.s, buf);
436         free(buf);
437         buf = readfile(obj->data.s, &bytes_read, 1);
438         if(buf != NULL) {
439                 begin = strstr(buf, THREADS_ENTRY);
440                 if(begin != NULL) {
441                         begin += strlen(THREADS_ENTRY);
442                         end = strchr(begin, '\n');
443                         if(end != NULL) {
444                                 *(end) = 0;
445                         }
446                         snprintf(p, p_max_size, "%s", begin);
447                 } else {
448                         NORM_ERR(THREADSNOTFOUND, obj->data.s);
449                 }
450                 free(buf);
451         }
452 }
453
454 void print_pid_thread_list(struct text_object *obj, char *p, int p_max_size) {
455         char *buf = NULL;
456         DIR* dir;
457         struct dirent *entry;
458         int totallength = 0;
459
460         asprintf(&buf, PROCDIR "/%s/task", obj->data.s);
461         strcpy(obj->data.s, buf);
462         free(buf);
463         dir = opendir(obj->data.s);
464         if(dir != NULL) {
465                 while ((entry = readdir(dir))) {
466                         if(entry->d_name[0] != '.') {
467                                 snprintf(p + totallength, p_max_size - totallength, "%s," , entry->d_name);
468                                 totallength += strlen(entry->d_name)+1;
469                         }
470                 }
471                 closedir(dir);
472                 if(p[totallength - 1] == ',') p[totallength - 1] = 0;
473         } else {
474                 p[0] = 0;
475         }
476 }
477
478 void print_pid_time_kernelmode(struct text_object *obj, char *p, int p_max_size) {
479         char *buf = NULL;
480         int bytes_read;
481         unsigned long int umtime;
482
483         if(*(obj->data.s) != 0) {
484                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
485                 strcpy(obj->data.s, buf);
486                 free(buf);
487                 buf = readfile(obj->data.s, &bytes_read, 1);
488                 if(buf != NULL) {
489                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &umtime);
490                         snprintf(p, p_max_size, "%.2f", (float) umtime / 100);
491                         free(buf);
492                 }
493         } else {
494                 NORM_ERR("$pid_time_kernelmode didn't receive a argument");
495         }
496 }
497
498 void print_pid_time_usermode(struct text_object *obj, char *p, int p_max_size) {
499         char *buf = NULL;
500         int bytes_read;
501         unsigned long int kmtime;
502
503         if(*(obj->data.s) != 0) {
504                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
505                 strcpy(obj->data.s, buf);
506                 free(buf);
507                 buf = readfile(obj->data.s, &bytes_read, 1);
508                 if(buf != NULL) {
509                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %lu", &kmtime);
510                         snprintf(p, p_max_size, "%.2f", (float) kmtime / 100);
511                         free(buf);
512                 }
513         } else {
514                 NORM_ERR("$pid_time_usermode didn't receive a argument");
515         }
516 }
517
518 void print_pid_time(struct text_object *obj, char *p, int p_max_size) {
519         char *buf = NULL;
520         int bytes_read;
521         unsigned long int umtime, kmtime;
522
523         if(*(obj->data.s) != 0) {
524                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
525                 strcpy(obj->data.s, buf);
526                 free(buf);
527                 buf = readfile(obj->data.s, &bytes_read, 1);
528                 if(buf != NULL) {
529                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &umtime, &kmtime);
530                         snprintf(p, p_max_size, "%.2f", (float) (umtime + kmtime) / 100);
531                         free(buf);
532                 }
533         } else {
534                 NORM_ERR("$pid_time didn't receive a argument");
535         }
536 }
537
538 #define UID_ENTRY "Uid:\t"
539 void print_pid_uid(struct text_object *obj, char *p, int p_max_size) {
540 #define UIDNOTFOUND     "Can't find the process real uid in '%s'"
541         char *begin, *end, *buf = NULL;
542         int bytes_read;
543
544         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
545         strcpy(obj->data.s, buf);
546         free(buf);
547         buf = readfile(obj->data.s, &bytes_read, 1);
548         if(buf != NULL) {
549                 begin = strstr(buf, UID_ENTRY);
550                 if(begin != NULL) {
551                         begin += strlen(UID_ENTRY);
552                         end = strchr(begin, '\t');
553                         if(end != NULL) {
554                                 *(end) = 0;
555                         }
556                         snprintf(p, p_max_size, "%s", begin);
557                 } else {
558                         NORM_ERR(UIDNOTFOUND, obj->data.s);
559                 }
560                 free(buf);
561         }
562 }
563
564 void print_pid_euid(struct text_object *obj, char *p, int p_max_size) {
565 #define EUIDNOTFOUND    "Can't find the process effective uid in '%s'"
566         char *begin, *end, *buf = NULL;
567         int bytes_read;
568
569         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
570         strcpy(obj->data.s, buf);
571         free(buf);
572         buf = readfile(obj->data.s, &bytes_read, 1);
573         if(buf != NULL) {
574                 begin = strstr(buf, UID_ENTRY);
575                 if(begin != NULL) {
576                         begin = strchr(begin, '\t'); begin++;
577                         begin = strchr(begin, '\t'); begin++;
578                         end = strchr(begin, '\t');
579                         if(end != NULL) {
580                                 *(end) = 0;
581                         }
582                         snprintf(p, p_max_size, "%s", begin);
583                 } else {
584                         NORM_ERR(EUIDNOTFOUND, obj->data.s);
585                 }
586                 free(buf);
587         }
588 }
589
590 void print_pid_suid(struct text_object *obj, char *p, int p_max_size) {
591 #define SUIDNOTFOUND    "Can't find the process saved set uid in '%s'"
592         char *begin, *end, *buf = NULL;
593         int bytes_read;
594
595         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
596         strcpy(obj->data.s, buf);
597         free(buf);
598         buf = readfile(obj->data.s, &bytes_read, 1);
599         if(buf != NULL) {
600                 begin = strstr(buf, UID_ENTRY);
601                 if(begin != NULL) {
602                         begin = strchr(begin, '\t'); begin++;
603                         begin = strchr(begin, '\t'); begin++;
604                         begin = strchr(begin, '\t'); begin++;
605                         end = strchr(begin, '\t');
606                         if(end != NULL) {
607                                 *(end) = 0;
608                         }
609                         snprintf(p, p_max_size, "%s", begin);
610                 } else {
611                         NORM_ERR(SUIDNOTFOUND, obj->data.s);
612                 }
613                 free(buf);
614         }
615 }
616
617 void print_pid_fsuid(struct text_object *obj, char *p, int p_max_size) {
618 #define FSUIDNOTFOUND   "Can't find the process file system uid in '%s'"
619         char *begin, *end, *buf = NULL;
620         int bytes_read;
621
622         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
623         strcpy(obj->data.s, buf);
624         free(buf);
625         buf = readfile(obj->data.s, &bytes_read, 1);
626         if(buf != NULL) {
627                 begin = strstr(buf, UID_ENTRY);
628                 if(begin != NULL) {
629                         begin = strchr(begin, '\t'); begin++;
630                         begin = strchr(begin, '\t'); begin++;
631                         begin = strchr(begin, '\t'); begin++;
632                         begin = strchr(begin, '\t'); begin++;
633                         end = strchr(begin, '\n');
634                         if(end != NULL) {
635                                 *(end) = 0;
636                         }
637                         snprintf(p, p_max_size, "%s", begin);
638                 } else {
639                         NORM_ERR(FSUIDNOTFOUND, obj->data.s);
640                 }
641                 free(buf);
642         }
643 }
644
645 #define GID_ENTRY "Gid:\t"
646 void print_pid_gid(struct text_object *obj, char *p, int p_max_size) {
647 #define GIDNOTFOUND     "Can't find the process real gid in '%s'"
648         char *begin, *end, *buf = NULL;
649         int bytes_read;
650
651         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
652         strcpy(obj->data.s, buf);
653         free(buf);
654         buf = readfile(obj->data.s, &bytes_read, 1);
655         if(buf != NULL) {
656                 begin = strstr(buf, GID_ENTRY);
657                 if(begin != NULL) {
658                         begin += strlen(GID_ENTRY);
659                         end = strchr(begin, '\t');
660                         if(end != NULL) {
661                                 *(end) = 0;
662                         }
663                         snprintf(p, p_max_size, "%s", begin);
664                 } else {
665                         NORM_ERR(GIDNOTFOUND, obj->data.s);
666                 }
667                 free(buf);
668         }
669 }
670
671 void print_pid_egid(struct text_object *obj, char *p, int p_max_size) {
672 #define EGIDNOTFOUND    "Can't find the process effective gid in '%s'"
673         char *begin, *end, *buf = NULL;
674         int bytes_read;
675
676         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
677         strcpy(obj->data.s, buf);
678         free(buf);
679         buf = readfile(obj->data.s, &bytes_read, 1);
680         if(buf != NULL) {
681                 begin = strstr(buf, GID_ENTRY);
682                 if(begin != NULL) {
683                         begin = strchr(begin, '\t'); begin++;
684                         begin = strchr(begin, '\t'); begin++;
685                         end = strchr(begin, '\t');
686                         if(end != NULL) {
687                                 *(end) = 0;
688                         }
689                         snprintf(p, p_max_size, "%s", begin);
690                 } else {
691                         NORM_ERR(EGIDNOTFOUND, obj->data.s);
692                 }
693                 free(buf);
694         }
695 }
696
697 void print_pid_sgid(struct text_object *obj, char *p, int p_max_size) {
698 #define SGIDNOTFOUND    "Can't find the process saved set gid in '%s'"
699         char *begin, *end, *buf = NULL;
700         int bytes_read;
701
702         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
703         strcpy(obj->data.s, buf);
704         free(buf);
705         buf = readfile(obj->data.s, &bytes_read, 1);
706         if(buf != NULL) {
707                 begin = strstr(buf, GID_ENTRY);
708                 if(begin != NULL) {
709                         begin = strchr(begin, '\t'); begin++;
710                         begin = strchr(begin, '\t'); begin++;
711                         begin = strchr(begin, '\t'); begin++;
712                         end = strchr(begin, '\t');
713                         if(end != NULL) {
714                                 *(end) = 0;
715                         }
716                         snprintf(p, p_max_size, "%s", begin);
717                 } else {
718                         NORM_ERR(SGIDNOTFOUND, obj->data.s);
719                 }
720                 free(buf);
721         }
722 }
723
724 void print_pid_fsgid(struct text_object *obj, char *p, int p_max_size) {
725 #define FSGIDNOTFOUND   "Can't find the process file system gid in '%s'"
726         char *begin, *end, *buf = NULL;
727         int bytes_read;
728
729         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
730         strcpy(obj->data.s, buf);
731         free(buf);
732         buf = readfile(obj->data.s, &bytes_read, 1);
733         if(buf != NULL) {
734                 begin = strstr(buf, GID_ENTRY);
735                 if(begin != NULL) {
736                         begin = strchr(begin, '\t'); begin++;
737                         begin = strchr(begin, '\t'); begin++;
738                         begin = strchr(begin, '\t'); begin++;
739                         begin = strchr(begin, '\t'); begin++;
740                         end = strchr(begin, '\n');
741                         if(end != NULL) {
742                                 *(end) = 0;
743                         }
744                         snprintf(p, p_max_size, "%s", begin);
745                 } else {
746                         NORM_ERR(FSGIDNOTFOUND, obj->data.s);
747                 }
748                 free(buf);
749         }
750 }
751
752 void internal_print_pid_vm(char* pid, char *p, int p_max_size, const char* entry, const char* errorstring) {
753         char *begin, *end, *buf = NULL;
754         int bytes_read;
755
756         asprintf(&buf, PROCDIR "/%s/status", pid);
757         strcpy(pid, buf);
758         free(buf);
759         buf = readfile(pid, &bytes_read, 1);
760         if(buf != NULL) {
761                 begin = strstr(buf, entry);
762                 if(begin != NULL) {
763                         begin += strlen(entry);
764                         while(*begin == '\t' || *begin == ' ') {
765                                 begin++;
766                         }
767                         end = strchr(begin, '\n');
768                         if(end != NULL) {
769                                 *(end) = 0;
770                         }
771                         snprintf(p, p_max_size, "%s", begin);
772                 } else {
773                         NORM_ERR(errorstring, pid);
774                 }
775                 free(buf);
776         }
777 }
778
779 void print_pid_vmpeak(struct text_object *obj, char *p, int p_max_size) {
780         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPeak:\t", "Can't find the process peak virtual memory size in '%s'");
781 }
782
783 void print_pid_vmsize(struct text_object *obj, char *p, int p_max_size) {
784         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmSize:\t", "Can't find the process virtual memory size in '%s'");
785 }
786
787 void print_pid_vmlck(struct text_object *obj, char *p, int p_max_size) {
788         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLck:\t", "Can't find the process locked memory size in '%s'");
789 }
790
791 void print_pid_vmhwm(struct text_object *obj, char *p, int p_max_size) {
792         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process peak resident set size in '%s'");
793 }
794
795 void print_pid_vmrss(struct text_object *obj, char *p, int p_max_size) {
796         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process resident set size in '%s'");
797 }
798
799 void print_pid_vmdata(struct text_object *obj, char *p, int p_max_size) {
800         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process data segment size in '%s'");
801 }
802
803 void print_pid_vmstk(struct text_object *obj, char *p, int p_max_size) {
804         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process stack segment size in '%s'");
805 }
806
807 void print_pid_vmexe(struct text_object *obj, char *p, int p_max_size) {
808         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process text segment size in '%s'");
809 }
810
811 void print_pid_vmlib(struct text_object *obj, char *p, int p_max_size) {
812         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLib:\t", "Can't find the process shared library code size in '%s'");
813 }
814
815 void print_pid_vmpte(struct text_object *obj, char *p, int p_max_size) {
816         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPTE:\t", "Can't find the process page table entries size in '%s'");
817 }