Use seconds instead of centiseconds as unit for $pid_time_kernelmode, $pid_time_userm...
[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         asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
217         strcpy(obj->data.s, buf);
218         free(buf);
219         buf = readfile(obj->data.s, &bytes_read, 1);
220         if(buf != NULL) {
221                 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %ld", &nice_value);
222                 snprintf(p, p_max_size, "%ld", nice_value);
223                 free(buf);
224         }
225 }
226
227 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
228         DIR* dir;
229         struct dirent *entry;
230         char buf[p_max_size];
231         int length, totallength = 0;
232         struct ll_string* files_front = NULL;
233         struct ll_string* files_back = NULL;
234
235         dir = opendir(obj->data.s);
236         if(dir != NULL) {
237                 while ((entry = readdir(dir))) {
238                         if(entry->d_name[0] != '.') {
239                                 snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
240                                 length = readlink(buf, buf, p_max_size);
241                                 buf[length] = 0;
242                                 if(inlist(files_front, buf) == 0) {
243                                         files_back = addnode(files_back, buf);
244                                         snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
245                                         totallength += length + strlen("; ");
246                                 }
247                                 if(files_front == NULL) {
248                                         files_front = files_back;
249                                 }
250                         }
251                 }
252                 closedir(dir);
253                 freelist(files_front);
254                 p[totallength - strlen("; ")] = 0;
255         } else {
256                 p[0] = 0;
257         }
258 }
259
260 void print_pid_parent(struct text_object *obj, char *p, int p_max_size) {
261 #define PARENT_ENTRY "PPid:\t"
262 #define PARENTNOTFOUND  "Can't find the process parent in '%s'"
263         char *begin, *end, *buf = NULL;
264         int bytes_read;
265
266         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
267         strcpy(obj->data.s, buf);
268         free(buf);
269         buf = readfile(obj->data.s, &bytes_read, 1);
270         if(buf != NULL) {
271                 begin = strstr(buf, PARENT_ENTRY);
272                 if(begin != NULL) {
273                         begin += strlen(PARENT_ENTRY);
274                         end = strchr(begin, '\n');
275                         if(end != NULL) {
276                                 *(end) = 0;
277                         }
278                         snprintf(p, p_max_size, "%s", begin);
279                 } else {
280                         NORM_ERR(PARENTNOTFOUND, obj->data.s);
281                 }
282                 free(buf);
283         }
284 }
285
286 void print_pid_priority(struct text_object *obj, char *p, int p_max_size) {
287         char *buf = NULL;
288         int bytes_read;
289         long int priority;
290
291         asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
292         strcpy(obj->data.s, buf);
293         free(buf);
294         buf = readfile(obj->data.s, &bytes_read, 1);
295         if(buf != NULL) {
296                 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %ld", &priority);
297                 snprintf(p, p_max_size, "%ld", priority);
298                 free(buf);
299         }
300 }
301
302 void print_pid_state(struct text_object *obj, char *p, int p_max_size) {
303 #define STATE_ENTRY "State:\t"
304 #define STATENOTFOUND   "Can't find the process state in '%s'"
305         char *begin, *end, *buf = NULL;
306         int bytes_read;
307
308         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
309         strcpy(obj->data.s, buf);
310         free(buf);
311         buf = readfile(obj->data.s, &bytes_read, 1);
312         if(buf != NULL) {
313                 begin = strstr(buf, STATE_ENTRY);
314                 if(begin != NULL) {
315                         begin += strlen(STATE_ENTRY) + 3;       // +3 will strip the char representing the short state and the space and '(' that follow
316                         end = strchr(begin, '\n');
317                         if(end != NULL) {
318                                 *(end-1) = 0;   // -1 strips the ')'
319                         }
320                         snprintf(p, p_max_size, "%s", begin);
321                 } else {
322                         NORM_ERR(STATENOTFOUND, obj->data.s);
323                 }
324                 free(buf);
325         }
326 }
327
328 void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
329         char *begin, *buf = NULL;
330         int bytes_read;
331
332         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
333         strcpy(obj->data.s, buf);
334         free(buf);
335         buf = readfile(obj->data.s, &bytes_read, 1);
336         if(buf != NULL) {
337                 begin = strstr(buf, STATE_ENTRY);
338                 if(begin != NULL) {
339                         snprintf(p, p_max_size, "%c", *begin);
340                 } else {
341                         NORM_ERR(STATENOTFOUND, obj->data.s);
342                 }
343                 free(buf);
344         }
345 }
346
347 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
348         char *buffer;
349
350         asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
351         pid_readlink(buffer, p, p_max_size);
352         free(buffer);
353 }
354
355 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
356         char *buffer;
357
358         asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
359         pid_readlink(buffer, p, p_max_size);
360         free(buffer);
361 }
362
363 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
364         char *buffer;
365
366         asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
367         pid_readlink(buffer, p, p_max_size);
368         free(buffer);
369 }
370
371 void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
372         unsigned int i;
373
374         if(strlen(arg) > 0) {
375                 obj->data.s = strdup(arg);
376                 for(i = 0; obj->data.s[i] != 0; i++) {
377                         while(obj->data.s[i] == ' ' && obj->data.s[i + 1] == ' ') {
378                                 memmove(obj->data.s + i, obj->data.s + i + 1, strlen(obj->data.s + i + 1) + 1);
379                         }
380                 }
381                 if(obj->data.s[i - 1] == ' ') {
382                         obj->data.s[i - 1] = 0;
383                 }
384         } else {
385                 CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
386         }
387 }
388
389 void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
390         DIR* dir;
391         struct dirent *entry;
392         char *filename, *buf;
393         int bytes_read, i;
394
395         dir = opendir(PROCDIR);
396         if(dir != NULL) {
397                 while ((entry = readdir(dir))) {
398                         asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
399                         buf = readfile(filename, &bytes_read, 0);
400                         free(filename);
401                         if(buf != NULL) {
402                                 for(i = 0; i < bytes_read - 1; i++) {
403                                         if(buf[i] == 0) buf[i] = ' ';
404                                 }
405                                 if(strstr(buf, obj->data.s) != NULL) {
406                                         snprintf(p, p_max_size, "%s", entry->d_name);
407                                         free(buf);
408                                         closedir(dir);
409                                         return;
410                                 }
411                                 free(buf);
412                         }
413                 }
414                 closedir(dir);
415         } else {
416                 NORM_ERR(READERR, PROCDIR);
417         }
418 }
419
420 void print_pid_threads(struct text_object *obj, char *p, int p_max_size) {
421 #define THREADS_ENTRY "Threads:\t"
422 #define THREADSNOTFOUND "Can't find the number of the threads of the process in '%s'"
423         char *begin, *end, *buf = NULL;
424         int bytes_read;
425
426         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
427         strcpy(obj->data.s, buf);
428         free(buf);
429         buf = readfile(obj->data.s, &bytes_read, 1);
430         if(buf != NULL) {
431                 begin = strstr(buf, THREADS_ENTRY);
432                 if(begin != NULL) {
433                         begin += strlen(THREADS_ENTRY);
434                         end = strchr(begin, '\n');
435                         if(end != NULL) {
436                                 *(end) = 0;
437                         }
438                         snprintf(p, p_max_size, "%s", begin);
439                 } else {
440                         NORM_ERR(THREADSNOTFOUND, obj->data.s);
441                 }
442                 free(buf);
443         }
444 }
445
446 void print_pid_thread_list(struct text_object *obj, char *p, int p_max_size) {
447         char *buf = NULL;
448         DIR* dir;
449         struct dirent *entry;
450         int totallength = 0;
451
452         asprintf(&buf, PROCDIR "/%s/task", obj->data.s);
453         strcpy(obj->data.s, buf);
454         free(buf);
455         dir = opendir(obj->data.s);
456         if(dir != NULL) {
457                 while ((entry = readdir(dir))) {
458                         if(entry->d_name[0] != '.') {
459                                 snprintf(p + totallength, p_max_size - totallength, "%s," , entry->d_name);
460                                 totallength += strlen(entry->d_name)+1;
461                         }
462                 }
463                 closedir(dir);
464                 if(p[totallength - 1] == ',') p[totallength - 1] = 0;
465         } else {
466                 p[0] = 0;
467         }
468 }
469
470 void print_pid_time_kernelmode(struct text_object *obj, char *p, int p_max_size) {
471         char *buf = NULL;
472         int bytes_read;
473         unsigned long int umtime;
474
475         asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
476         strcpy(obj->data.s, buf);
477         free(buf);
478         buf = readfile(obj->data.s, &bytes_read, 1);
479         if(buf != NULL) {
480                 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &umtime);
481                 snprintf(p, p_max_size, "%.2f", (float) umtime / 100);
482                 free(buf);
483         }
484 }
485
486 void print_pid_time_usermode(struct text_object *obj, char *p, int p_max_size) {
487         char *buf = NULL;
488         int bytes_read;
489         unsigned long int kmtime;
490
491         asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
492         strcpy(obj->data.s, buf);
493         free(buf);
494         buf = readfile(obj->data.s, &bytes_read, 1);
495         if(buf != NULL) {
496                 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %lu", &kmtime);
497                 snprintf(p, p_max_size, "%.2f", (float) kmtime / 100);
498                 free(buf);
499         }
500 }
501
502 void print_pid_time(struct text_object *obj, char *p, int p_max_size) {
503         char *buf = NULL;
504         int bytes_read;
505         unsigned long int umtime, kmtime;
506
507         asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
508         strcpy(obj->data.s, buf);
509         free(buf);
510         buf = readfile(obj->data.s, &bytes_read, 1);
511         if(buf != NULL) {
512                 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &umtime, &kmtime);
513                 snprintf(p, p_max_size, "%.2f", (float) (umtime + kmtime) / 100);
514                 free(buf);
515         }
516 }
517
518 #define UID_ENTRY "Uid:\t"
519 void print_pid_uid(struct text_object *obj, char *p, int p_max_size) {
520 #define UIDNOTFOUND     "Can't find the process real uid in '%s'"
521         char *begin, *end, *buf = NULL;
522         int bytes_read;
523
524         asprintf(&buf, PROCDIR "/%s/status", 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                 begin = strstr(buf, UID_ENTRY);
530                 if(begin != NULL) {
531                         begin += strlen(UID_ENTRY);
532                         end = strchr(begin, '\t');
533                         if(end != NULL) {
534                                 *(end) = 0;
535                         }
536                         snprintf(p, p_max_size, "%s", begin);
537                 } else {
538                         NORM_ERR(UIDNOTFOUND, obj->data.s);
539                 }
540                 free(buf);
541         }
542 }
543
544 void print_pid_euid(struct text_object *obj, char *p, int p_max_size) {
545 #define EUIDNOTFOUND    "Can't find the process effective uid in '%s'"
546         char *begin, *end, *buf = NULL;
547         int bytes_read;
548
549         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
550         strcpy(obj->data.s, buf);
551         free(buf);
552         buf = readfile(obj->data.s, &bytes_read, 1);
553         if(buf != NULL) {
554                 begin = strstr(buf, UID_ENTRY);
555                 if(begin != NULL) {
556                         begin = strchr(begin, '\t'); begin++;
557                         begin = strchr(begin, '\t'); begin++;
558                         end = strchr(begin, '\t');
559                         if(end != NULL) {
560                                 *(end) = 0;
561                         }
562                         snprintf(p, p_max_size, "%s", begin);
563                 } else {
564                         NORM_ERR(EUIDNOTFOUND, obj->data.s);
565                 }
566                 free(buf);
567         }
568 }
569
570 void print_pid_suid(struct text_object *obj, char *p, int p_max_size) {
571 #define SUIDNOTFOUND    "Can't find the process saved set uid in '%s'"
572         char *begin, *end, *buf = NULL;
573         int bytes_read;
574
575         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
576         strcpy(obj->data.s, buf);
577         free(buf);
578         buf = readfile(obj->data.s, &bytes_read, 1);
579         if(buf != NULL) {
580                 begin = strstr(buf, UID_ENTRY);
581                 if(begin != NULL) {
582                         begin = strchr(begin, '\t'); begin++;
583                         begin = strchr(begin, '\t'); begin++;
584                         begin = strchr(begin, '\t'); begin++;
585                         end = strchr(begin, '\t');
586                         if(end != NULL) {
587                                 *(end) = 0;
588                         }
589                         snprintf(p, p_max_size, "%s", begin);
590                 } else {
591                         NORM_ERR(SUIDNOTFOUND, obj->data.s);
592                 }
593                 free(buf);
594         }
595 }
596
597 void print_pid_fsuid(struct text_object *obj, char *p, int p_max_size) {
598 #define FSUIDNOTFOUND   "Can't find the process file system uid in '%s'"
599         char *begin, *end, *buf = NULL;
600         int bytes_read;
601
602         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
603         strcpy(obj->data.s, buf);
604         free(buf);
605         buf = readfile(obj->data.s, &bytes_read, 1);
606         if(buf != NULL) {
607                 begin = strstr(buf, UID_ENTRY);
608                 if(begin != NULL) {
609                         begin = strchr(begin, '\t'); begin++;
610                         begin = strchr(begin, '\t'); begin++;
611                         begin = strchr(begin, '\t'); begin++;
612                         begin = strchr(begin, '\t'); begin++;
613                         end = strchr(begin, '\n');
614                         if(end != NULL) {
615                                 *(end) = 0;
616                         }
617                         snprintf(p, p_max_size, "%s", begin);
618                 } else {
619                         NORM_ERR(FSUIDNOTFOUND, obj->data.s);
620                 }
621                 free(buf);
622         }
623 }
624
625 #define GID_ENTRY "Gid:\t"
626 void print_pid_gid(struct text_object *obj, char *p, int p_max_size) {
627 #define GIDNOTFOUND     "Can't find the process real gid in '%s'"
628         char *begin, *end, *buf = NULL;
629         int bytes_read;
630
631         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
632         strcpy(obj->data.s, buf);
633         free(buf);
634         buf = readfile(obj->data.s, &bytes_read, 1);
635         if(buf != NULL) {
636                 begin = strstr(buf, GID_ENTRY);
637                 if(begin != NULL) {
638                         begin += strlen(GID_ENTRY);
639                         end = strchr(begin, '\t');
640                         if(end != NULL) {
641                                 *(end) = 0;
642                         }
643                         snprintf(p, p_max_size, "%s", begin);
644                 } else {
645                         NORM_ERR(GIDNOTFOUND, obj->data.s);
646                 }
647                 free(buf);
648         }
649 }
650
651 void print_pid_egid(struct text_object *obj, char *p, int p_max_size) {
652 #define EGIDNOTFOUND    "Can't find the process effective gid in '%s'"
653         char *begin, *end, *buf = NULL;
654         int bytes_read;
655
656         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
657         strcpy(obj->data.s, buf);
658         free(buf);
659         buf = readfile(obj->data.s, &bytes_read, 1);
660         if(buf != NULL) {
661                 begin = strstr(buf, GID_ENTRY);
662                 if(begin != NULL) {
663                         begin = strchr(begin, '\t'); begin++;
664                         begin = strchr(begin, '\t'); begin++;
665                         end = strchr(begin, '\t');
666                         if(end != NULL) {
667                                 *(end) = 0;
668                         }
669                         snprintf(p, p_max_size, "%s", begin);
670                 } else {
671                         NORM_ERR(EGIDNOTFOUND, obj->data.s);
672                 }
673                 free(buf);
674         }
675 }
676
677 void print_pid_sgid(struct text_object *obj, char *p, int p_max_size) {
678 #define SGIDNOTFOUND    "Can't find the process saved set gid in '%s'"
679         char *begin, *end, *buf = NULL;
680         int bytes_read;
681
682         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
683         strcpy(obj->data.s, buf);
684         free(buf);
685         buf = readfile(obj->data.s, &bytes_read, 1);
686         if(buf != NULL) {
687                 begin = strstr(buf, GID_ENTRY);
688                 if(begin != NULL) {
689                         begin = strchr(begin, '\t'); begin++;
690                         begin = strchr(begin, '\t'); begin++;
691                         begin = strchr(begin, '\t'); begin++;
692                         end = strchr(begin, '\t');
693                         if(end != NULL) {
694                                 *(end) = 0;
695                         }
696                         snprintf(p, p_max_size, "%s", begin);
697                 } else {
698                         NORM_ERR(SGIDNOTFOUND, obj->data.s);
699                 }
700                 free(buf);
701         }
702 }
703
704 void print_pid_fsgid(struct text_object *obj, char *p, int p_max_size) {
705 #define FSGIDNOTFOUND   "Can't find the process file system gid in '%s'"
706         char *begin, *end, *buf = NULL;
707         int bytes_read;
708
709         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
710         strcpy(obj->data.s, buf);
711         free(buf);
712         buf = readfile(obj->data.s, &bytes_read, 1);
713         if(buf != NULL) {
714                 begin = strstr(buf, GID_ENTRY);
715                 if(begin != NULL) {
716                         begin = strchr(begin, '\t'); begin++;
717                         begin = strchr(begin, '\t'); begin++;
718                         begin = strchr(begin, '\t'); begin++;
719                         begin = strchr(begin, '\t'); begin++;
720                         end = strchr(begin, '\n');
721                         if(end != NULL) {
722                                 *(end) = 0;
723                         }
724                         snprintf(p, p_max_size, "%s", begin);
725                 } else {
726                         NORM_ERR(FSGIDNOTFOUND, obj->data.s);
727                 }
728                 free(buf);
729         }
730 }
731
732 void internal_print_pid_vm(char* pid, char *p, int p_max_size, const char* entry, const char* errorstring) {
733         char *begin, *end, *buf = NULL;
734         int bytes_read;
735
736         asprintf(&buf, PROCDIR "/%s/status", pid);
737         strcpy(pid, buf);
738         free(buf);
739         buf = readfile(pid, &bytes_read, 1);
740         if(buf != NULL) {
741                 begin = strstr(buf, entry);
742                 if(begin != NULL) {
743                         begin += strlen(entry);
744                         while(*begin == '\t' || *begin == ' ') {
745                                 begin++;
746                         }
747                         end = strchr(begin, '\n');
748                         if(end != NULL) {
749                                 *(end) = 0;
750                         }
751                         snprintf(p, p_max_size, "%s", begin);
752                 } else {
753                         NORM_ERR(errorstring, pid);
754                 }
755                 free(buf);
756         }
757 }
758
759 void print_pid_vmpeak(struct text_object *obj, char *p, int p_max_size) {
760         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPeak:\t", "Can't find the process peak virtual memory size in '%s'");
761 }
762
763 void print_pid_vmsize(struct text_object *obj, char *p, int p_max_size) {
764         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmSize:\t", "Can't find the process virtual memory size in '%s'");
765 }
766
767 void print_pid_vmlck(struct text_object *obj, char *p, int p_max_size) {
768         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLck:\t", "Can't find the process locked memory size in '%s'");
769 }
770
771 void print_pid_vmhwm(struct text_object *obj, char *p, int p_max_size) {
772         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process peak resident set size in '%s'");
773 }
774
775 void print_pid_vmrss(struct text_object *obj, char *p, int p_max_size) {
776         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process resident set size in '%s'");
777 }
778
779 void print_pid_vmdata(struct text_object *obj, char *p, int p_max_size) {
780         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process data segment size in '%s'");
781 }
782
783 void print_pid_vmstk(struct text_object *obj, char *p, int p_max_size) {
784         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process stack segment size in '%s'");
785 }
786
787 void print_pid_vmexe(struct text_object *obj, char *p, int p_max_size) {
788         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process text segment size in '%s'");
789 }
790
791 void print_pid_vmlib(struct text_object *obj, char *p, int p_max_size) {
792         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLib:\t", "Can't find the process shared library code size in '%s'");
793 }
794
795 void print_pid_vmpte(struct text_object *obj, char *p, int p_max_size) {
796         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPTE:\t", "Can't find the process page table entries size in '%s'");
797 }