Add support for $pid_nice and $pid_priority
[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 #define UID_ENTRY "Uid:\t"
471 void print_pid_uid(struct text_object *obj, char *p, int p_max_size) {
472 #define UIDNOTFOUND     "Can't find the process real uid in '%s'"
473         char *begin, *end, *buf = NULL;
474         int bytes_read;
475
476         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
477         strcpy(obj->data.s, buf);
478         free(buf);
479         buf = readfile(obj->data.s, &bytes_read, 1);
480         if(buf != NULL) {
481                 begin = strstr(buf, UID_ENTRY);
482                 if(begin != NULL) {
483                         begin += strlen(UID_ENTRY);
484                         end = strchr(begin, '\t');
485                         if(end != NULL) {
486                                 *(end) = 0;
487                         }
488                         snprintf(p, p_max_size, "%s", begin);
489                 } else {
490                         NORM_ERR(UIDNOTFOUND, obj->data.s);
491                 }
492                 free(buf);
493         }
494 }
495
496 void print_pid_euid(struct text_object *obj, char *p, int p_max_size) {
497 #define EUIDNOTFOUND    "Can't find the process effective uid in '%s'"
498         char *begin, *end, *buf = NULL;
499         int bytes_read;
500
501         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
502         strcpy(obj->data.s, buf);
503         free(buf);
504         buf = readfile(obj->data.s, &bytes_read, 1);
505         if(buf != NULL) {
506                 begin = strstr(buf, UID_ENTRY);
507                 if(begin != NULL) {
508                         begin = strchr(begin, '\t'); begin++;
509                         begin = strchr(begin, '\t'); begin++;
510                         end = strchr(begin, '\t');
511                         if(end != NULL) {
512                                 *(end) = 0;
513                         }
514                         snprintf(p, p_max_size, "%s", begin);
515                 } else {
516                         NORM_ERR(EUIDNOTFOUND, obj->data.s);
517                 }
518                 free(buf);
519         }
520 }
521
522 void print_pid_suid(struct text_object *obj, char *p, int p_max_size) {
523 #define SUIDNOTFOUND    "Can't find the process saved set uid in '%s'"
524         char *begin, *end, *buf = NULL;
525         int bytes_read;
526
527         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
528         strcpy(obj->data.s, buf);
529         free(buf);
530         buf = readfile(obj->data.s, &bytes_read, 1);
531         if(buf != NULL) {
532                 begin = strstr(buf, UID_ENTRY);
533                 if(begin != NULL) {
534                         begin = strchr(begin, '\t'); begin++;
535                         begin = strchr(begin, '\t'); begin++;
536                         begin = strchr(begin, '\t'); begin++;
537                         end = strchr(begin, '\t');
538                         if(end != NULL) {
539                                 *(end) = 0;
540                         }
541                         snprintf(p, p_max_size, "%s", begin);
542                 } else {
543                         NORM_ERR(SUIDNOTFOUND, obj->data.s);
544                 }
545                 free(buf);
546         }
547 }
548
549 void print_pid_fsuid(struct text_object *obj, char *p, int p_max_size) {
550 #define FSUIDNOTFOUND   "Can't find the process file system uid in '%s'"
551         char *begin, *end, *buf = NULL;
552         int bytes_read;
553
554         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
555         strcpy(obj->data.s, buf);
556         free(buf);
557         buf = readfile(obj->data.s, &bytes_read, 1);
558         if(buf != NULL) {
559                 begin = strstr(buf, UID_ENTRY);
560                 if(begin != NULL) {
561                         begin = strchr(begin, '\t'); begin++;
562                         begin = strchr(begin, '\t'); begin++;
563                         begin = strchr(begin, '\t'); begin++;
564                         begin = strchr(begin, '\t'); begin++;
565                         end = strchr(begin, '\n');
566                         if(end != NULL) {
567                                 *(end) = 0;
568                         }
569                         snprintf(p, p_max_size, "%s", begin);
570                 } else {
571                         NORM_ERR(FSUIDNOTFOUND, obj->data.s);
572                 }
573                 free(buf);
574         }
575 }
576
577 #define GID_ENTRY "Gid:\t"
578 void print_pid_gid(struct text_object *obj, char *p, int p_max_size) {
579 #define GIDNOTFOUND     "Can't find the process real gid in '%s'"
580         char *begin, *end, *buf = NULL;
581         int bytes_read;
582
583         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
584         strcpy(obj->data.s, buf);
585         free(buf);
586         buf = readfile(obj->data.s, &bytes_read, 1);
587         if(buf != NULL) {
588                 begin = strstr(buf, GID_ENTRY);
589                 if(begin != NULL) {
590                         begin += strlen(GID_ENTRY);
591                         end = strchr(begin, '\t');
592                         if(end != NULL) {
593                                 *(end) = 0;
594                         }
595                         snprintf(p, p_max_size, "%s", begin);
596                 } else {
597                         NORM_ERR(GIDNOTFOUND, obj->data.s);
598                 }
599                 free(buf);
600         }
601 }
602
603 void print_pid_egid(struct text_object *obj, char *p, int p_max_size) {
604 #define EGIDNOTFOUND    "Can't find the process effective gid in '%s'"
605         char *begin, *end, *buf = NULL;
606         int bytes_read;
607
608         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
609         strcpy(obj->data.s, buf);
610         free(buf);
611         buf = readfile(obj->data.s, &bytes_read, 1);
612         if(buf != NULL) {
613                 begin = strstr(buf, GID_ENTRY);
614                 if(begin != NULL) {
615                         begin = strchr(begin, '\t'); begin++;
616                         begin = strchr(begin, '\t'); begin++;
617                         end = strchr(begin, '\t');
618                         if(end != NULL) {
619                                 *(end) = 0;
620                         }
621                         snprintf(p, p_max_size, "%s", begin);
622                 } else {
623                         NORM_ERR(EGIDNOTFOUND, obj->data.s);
624                 }
625                 free(buf);
626         }
627 }
628
629 void print_pid_sgid(struct text_object *obj, char *p, int p_max_size) {
630 #define SGIDNOTFOUND    "Can't find the process saved set gid in '%s'"
631         char *begin, *end, *buf = NULL;
632         int bytes_read;
633
634         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
635         strcpy(obj->data.s, buf);
636         free(buf);
637         buf = readfile(obj->data.s, &bytes_read, 1);
638         if(buf != NULL) {
639                 begin = strstr(buf, GID_ENTRY);
640                 if(begin != NULL) {
641                         begin = strchr(begin, '\t'); begin++;
642                         begin = strchr(begin, '\t'); begin++;
643                         begin = strchr(begin, '\t'); begin++;
644                         end = strchr(begin, '\t');
645                         if(end != NULL) {
646                                 *(end) = 0;
647                         }
648                         snprintf(p, p_max_size, "%s", begin);
649                 } else {
650                         NORM_ERR(SGIDNOTFOUND, obj->data.s);
651                 }
652                 free(buf);
653         }
654 }
655
656 void print_pid_fsgid(struct text_object *obj, char *p, int p_max_size) {
657 #define FSGIDNOTFOUND   "Can't find the process file system gid in '%s'"
658         char *begin, *end, *buf = NULL;
659         int bytes_read;
660
661         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
662         strcpy(obj->data.s, buf);
663         free(buf);
664         buf = readfile(obj->data.s, &bytes_read, 1);
665         if(buf != NULL) {
666                 begin = strstr(buf, GID_ENTRY);
667                 if(begin != NULL) {
668                         begin = strchr(begin, '\t'); begin++;
669                         begin = strchr(begin, '\t'); begin++;
670                         begin = strchr(begin, '\t'); begin++;
671                         begin = strchr(begin, '\t'); begin++;
672                         end = strchr(begin, '\n');
673                         if(end != NULL) {
674                                 *(end) = 0;
675                         }
676                         snprintf(p, p_max_size, "%s", begin);
677                 } else {
678                         NORM_ERR(FSGIDNOTFOUND, obj->data.s);
679                 }
680                 free(buf);
681         }
682 }
683
684 void internal_print_pid_vm(char* pid, char *p, int p_max_size, const char* entry, const char* errorstring) {
685         char *begin, *end, *buf = NULL;
686         int bytes_read;
687
688         asprintf(&buf, PROCDIR "/%s/status", pid);
689         strcpy(pid, buf);
690         free(buf);
691         buf = readfile(pid, &bytes_read, 1);
692         if(buf != NULL) {
693                 begin = strstr(buf, entry);
694                 if(begin != NULL) {
695                         begin += strlen(entry);
696                         while(*begin == '\t' || *begin == ' ') {
697                                 begin++;
698                         }
699                         end = strchr(begin, '\n');
700                         if(end != NULL) {
701                                 *(end) = 0;
702                         }
703                         snprintf(p, p_max_size, "%s", begin);
704                 } else {
705                         NORM_ERR(errorstring, pid);
706                 }
707                 free(buf);
708         }
709 }
710
711 void print_pid_vmpeak(struct text_object *obj, char *p, int p_max_size) {
712         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPeak:\t", "Can't find the process peak virtual memory size in '%s'");
713 }
714
715 void print_pid_vmsize(struct text_object *obj, char *p, int p_max_size) {
716         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmSize:\t", "Can't find the process virtual memory size in '%s'");
717 }
718
719 void print_pid_vmlck(struct text_object *obj, char *p, int p_max_size) {
720         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLck:\t", "Can't find the process locked memory size in '%s'");
721 }
722
723 void print_pid_vmhwm(struct text_object *obj, char *p, int p_max_size) {
724         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process peak resident set size in '%s'");
725 }
726
727 void print_pid_vmrss(struct text_object *obj, char *p, int p_max_size) {
728         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process resident set size in '%s'");
729 }
730
731 void print_pid_vmdata(struct text_object *obj, char *p, int p_max_size) {
732         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process data segment size in '%s'");
733 }
734
735 void print_pid_vmstk(struct text_object *obj, char *p, int p_max_size) {
736         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process stack segment size in '%s'");
737 }
738
739 void print_pid_vmexe(struct text_object *obj, char *p, int p_max_size) {
740         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process text segment size in '%s'");
741 }
742
743 void print_pid_vmlib(struct text_object *obj, char *p, int p_max_size) {
744         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLib:\t", "Can't find the process shared library code size in '%s'");
745 }
746
747 void print_pid_vmpte(struct text_object *obj, char *p, int p_max_size) {
748         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPTE:\t", "Can't find the process page table entries size in '%s'");
749 }