Fix buffer overflows in eve.c (sf.net #3034056)
[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-2010 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 "conky.h"
32 #include "logging.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         if(*(obj->data.s) != 0) {
119                 asprintf(&buf, PROCDIR "/%s/cmdline", obj->data.s);
120                 strcpy(obj->data.s, buf);
121                 free(buf);
122                 buf = readfile(obj->data.s, &bytes_read, 1);
123                 if(buf != NULL) {
124                         for(i = 0; i < bytes_read-1; i++) {
125                                 if(buf[i] == 0) {
126                                         buf[i] = ' ';
127                                 }
128                         }
129                         snprintf(p, p_max_size, "%s", buf);
130                         free(buf);
131                 }
132         } else {
133                 NORM_ERR("$pid_cmdline didn't receive a argument");
134         }
135 }
136
137 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
138 {
139         char buf[p_max_size];
140         int bytes_read;
141
142         sprintf(buf, PROCDIR "/%s/cwd", obj->data.s);
143         strcpy(obj->data.s, buf);
144         bytes_read = readlink(obj->data.s, buf, p_max_size);
145         if(bytes_read != -1) {
146                 buf[bytes_read] = 0;
147                 snprintf(p, p_max_size, "%s", buf);
148         } else {
149                 NORM_ERR(READERR, obj->data.s);
150         }
151 }
152
153 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
154 {
155         int i, total_read;
156         pid_t pid;
157         char *buf, *file, *var=strdup(obj->data.s);;
158
159         if(sscanf(obj->data.s, "%d %s", &pid, var) == 2) {
160                 for(i = 0; var[i] != 0; i++) {
161                         var[i] = toupper(var[i]);
162                 }
163                 asprintf(&file, PROCDIR "/%d/environ", pid);
164                 buf = readfile(file, &total_read, 1);
165                 free(file);
166                 if(buf != NULL) {
167                         for(i = 0; i < total_read; i += strlen(buf + i) + 1) {
168                                 if(strncmp(buf + i, var, strlen(var)) == 0 && *(buf + i + strlen(var)) == '=') {
169                                         snprintf(p, p_max_size, "%s", buf + i + strlen(var) + 1);
170                                         free(buf);
171                                         free(var);
172                                         return;
173                                 }
174                         }
175                         free(buf);
176                 }
177                 free(var);
178                 *p = 0;
179         }
180 }
181
182 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
183 {
184         char *buf = NULL;
185         char *buf2;
186         int bytes_read, total_read;
187         int i = 0;
188
189         asprintf(&buf, PROCDIR "/%s/environ", obj->data.s);
190         strcpy(obj->data.s, buf);
191         free(buf);
192         buf = readfile(obj->data.s, &total_read, 1);
193         if(buf != NULL) {
194                 for(bytes_read = 0; bytes_read < total_read; buf[i-1] = ';') {
195                         buf2 = strdup(buf+bytes_read);
196                         bytes_read += strlen(buf2)+1;
197                         sscanf(buf2, "%[^=]", buf+i);
198                         free(buf2);
199                         i = strlen(buf) + 1;
200                 }
201                 buf[i-1] = 0;
202                 snprintf(p, p_max_size, "%s", buf);
203                 free(buf);
204         }
205 }
206
207 void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
208         char *buffer;
209
210         asprintf(&buffer, PROCDIR "/%s/exe", obj->data.s);
211         pid_readlink(buffer, p, p_max_size);
212         free(buffer);
213 }
214
215 void print_pid_nice(struct text_object *obj, char *p, int p_max_size) {
216         char *buf = NULL;
217         int bytes_read;
218         long int nice_value;
219
220         if(*(obj->data.s) != 0) {
221                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
222                 strcpy(obj->data.s, buf);
223                 free(buf);
224                 buf = readfile(obj->data.s, &bytes_read, 1);
225                 if(buf != NULL) {
226                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %ld", &nice_value);
227                         snprintf(p, p_max_size, "%ld", nice_value);
228                         free(buf);
229                 }
230         } else {
231                 NORM_ERR("$pid_nice didn't receive a argument");
232         }
233 }
234
235 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
236         DIR* dir;
237         struct dirent *entry;
238         char buf[p_max_size];
239         int length, totallength = 0;
240         struct ll_string* files_front = NULL;
241         struct ll_string* files_back = NULL;
242
243         dir = opendir(obj->data.s);
244         if(dir != NULL) {
245                 while ((entry = readdir(dir))) {
246                         if(entry->d_name[0] != '.') {
247                                 snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
248                                 length = readlink(buf, buf, p_max_size);
249                                 buf[length] = 0;
250                                 if(inlist(files_front, buf) == 0) {
251                                         files_back = addnode(files_back, buf);
252                                         snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
253                                         totallength += length + strlen("; ");
254                                 }
255                                 if(files_front == NULL) {
256                                         files_front = files_back;
257                                 }
258                         }
259                 }
260                 closedir(dir);
261                 freelist(files_front);
262                 p[totallength - strlen("; ")] = 0;
263         } else {
264                 p[0] = 0;
265         }
266 }
267
268 void print_pid_parent(struct text_object *obj, char *p, int p_max_size) {
269 #define PARENT_ENTRY "PPid:\t"
270 #define PARENTNOTFOUND  "Can't find the process parent in '%s'"
271         char *begin, *end, *buf = NULL;
272         int bytes_read;
273
274         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
275         strcpy(obj->data.s, buf);
276         free(buf);
277         buf = readfile(obj->data.s, &bytes_read, 1);
278         if(buf != NULL) {
279                 begin = strstr(buf, PARENT_ENTRY);
280                 if(begin != NULL) {
281                         begin += strlen(PARENT_ENTRY);
282                         end = strchr(begin, '\n');
283                         if(end != NULL) {
284                                 *(end) = 0;
285                         }
286                         snprintf(p, p_max_size, "%s", begin);
287                 } else {
288                         NORM_ERR(PARENTNOTFOUND, obj->data.s);
289                 }
290                 free(buf);
291         }
292 }
293
294 void print_pid_priority(struct text_object *obj, char *p, int p_max_size) {
295         char *buf = NULL;
296         int bytes_read;
297         long int priority;
298
299         if(*(obj->data.s) != 0) {
300                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
301                 strcpy(obj->data.s, buf);
302                 free(buf);
303                 buf = readfile(obj->data.s, &bytes_read, 1);
304                 if(buf != NULL) {
305                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %ld", &priority);
306                         snprintf(p, p_max_size, "%ld", priority);
307                         free(buf);
308                 }
309         } else {
310                 NORM_ERR("$pid_priority didn't receive a argument");
311         }
312 }
313
314 void print_pid_state(struct text_object *obj, char *p, int p_max_size) {
315 #define STATE_ENTRY "State:\t"
316 #define STATENOTFOUND   "Can't find the process state in '%s'"
317         char *begin, *end, *buf = NULL;
318         int bytes_read;
319
320         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
321         strcpy(obj->data.s, buf);
322         free(buf);
323         buf = readfile(obj->data.s, &bytes_read, 1);
324         if(buf != NULL) {
325                 begin = strstr(buf, STATE_ENTRY);
326                 if(begin != NULL) {
327                         begin += strlen(STATE_ENTRY) + 3;       // +3 will strip the char representing the short state and the space and '(' that follow
328                         end = strchr(begin, '\n');
329                         if(end != NULL) {
330                                 *(end-1) = 0;   // -1 strips the ')'
331                         }
332                         snprintf(p, p_max_size, "%s", begin);
333                 } else {
334                         NORM_ERR(STATENOTFOUND, obj->data.s);
335                 }
336                 free(buf);
337         }
338 }
339
340 void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
341         char *begin, *buf = NULL;
342         int bytes_read;
343
344         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
345         strcpy(obj->data.s, buf);
346         free(buf);
347         buf = readfile(obj->data.s, &bytes_read, 1);
348         if(buf != NULL) {
349                 begin = strstr(buf, STATE_ENTRY);
350                 if(begin != NULL) {
351                         snprintf(p, p_max_size, "%c", *begin);
352                 } else {
353                         NORM_ERR(STATENOTFOUND, obj->data.s);
354                 }
355                 free(buf);
356         }
357 }
358
359 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
360         char *buffer;
361
362         asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
363         pid_readlink(buffer, p, p_max_size);
364         free(buffer);
365 }
366
367 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
368         char *buffer;
369
370         asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
371         pid_readlink(buffer, p, p_max_size);
372         free(buffer);
373 }
374
375 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
376         char *buffer;
377
378         asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
379         pid_readlink(buffer, p, p_max_size);
380         free(buffer);
381 }
382
383 void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
384         unsigned int i;
385
386         if(strlen(arg) > 0) {
387                 obj->data.s = strdup(arg);
388                 for(i = 0; obj->data.s[i] != 0; i++) {
389                         while(obj->data.s[i] == ' ' && obj->data.s[i + 1] == ' ') {
390                                 memmove(obj->data.s + i, obj->data.s + i + 1, strlen(obj->data.s + i + 1) + 1);
391                         }
392                 }
393                 if(obj->data.s[i - 1] == ' ') {
394                         obj->data.s[i - 1] = 0;
395                 }
396         } else {
397                 CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
398         }
399 }
400
401 void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
402         DIR* dir;
403         struct dirent *entry;
404         char *filename, *buf;
405         int bytes_read, i;
406
407         dir = opendir(PROCDIR);
408         if(dir != NULL) {
409                 while ((entry = readdir(dir))) {
410                         asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
411                         buf = readfile(filename, &bytes_read, 0);
412                         free(filename);
413                         if(buf != NULL) {
414                                 for(i = 0; i < bytes_read - 1; i++) {
415                                         if(buf[i] == 0) buf[i] = ' ';
416                                 }
417                                 if(strstr(buf, obj->data.s) != NULL) {
418                                         snprintf(p, p_max_size, "%s", entry->d_name);
419                                         free(buf);
420                                         closedir(dir);
421                                         return;
422                                 }
423                                 free(buf);
424                         }
425                 }
426                 closedir(dir);
427         } else {
428                 NORM_ERR(READERR, PROCDIR);
429         }
430 }
431
432 void print_pid_threads(struct text_object *obj, char *p, int p_max_size) {
433 #define THREADS_ENTRY "Threads:\t"
434 #define THREADSNOTFOUND "Can't find the number of the threads of the process in '%s'"
435         char *begin, *end, *buf = NULL;
436         int bytes_read;
437
438         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
439         strcpy(obj->data.s, buf);
440         free(buf);
441         buf = readfile(obj->data.s, &bytes_read, 1);
442         if(buf != NULL) {
443                 begin = strstr(buf, THREADS_ENTRY);
444                 if(begin != NULL) {
445                         begin += strlen(THREADS_ENTRY);
446                         end = strchr(begin, '\n');
447                         if(end != NULL) {
448                                 *(end) = 0;
449                         }
450                         snprintf(p, p_max_size, "%s", begin);
451                 } else {
452                         NORM_ERR(THREADSNOTFOUND, obj->data.s);
453                 }
454                 free(buf);
455         }
456 }
457
458 void print_pid_thread_list(struct text_object *obj, char *p, int p_max_size) {
459         char *buf = NULL;
460         DIR* dir;
461         struct dirent *entry;
462         int totallength = 0;
463
464         asprintf(&buf, PROCDIR "/%s/task", obj->data.s);
465         strcpy(obj->data.s, buf);
466         free(buf);
467         dir = opendir(obj->data.s);
468         if(dir != NULL) {
469                 while ((entry = readdir(dir))) {
470                         if(entry->d_name[0] != '.') {
471                                 snprintf(p + totallength, p_max_size - totallength, "%s," , entry->d_name);
472                                 totallength += strlen(entry->d_name)+1;
473                         }
474                 }
475                 closedir(dir);
476                 if(p[totallength - 1] == ',') p[totallength - 1] = 0;
477         } else {
478                 p[0] = 0;
479         }
480 }
481
482 void print_pid_time_kernelmode(struct text_object *obj, char *p, int p_max_size) {
483         char *buf = NULL;
484         int bytes_read;
485         unsigned long int umtime;
486
487         if(*(obj->data.s) != 0) {
488                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
489                 strcpy(obj->data.s, buf);
490                 free(buf);
491                 buf = readfile(obj->data.s, &bytes_read, 1);
492                 if(buf != NULL) {
493                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &umtime);
494                         snprintf(p, p_max_size, "%.2f", (float) umtime / 100);
495                         free(buf);
496                 }
497         } else {
498                 NORM_ERR("$pid_time_kernelmode didn't receive a argument");
499         }
500 }
501
502 void print_pid_time_usermode(struct text_object *obj, char *p, int p_max_size) {
503         char *buf = NULL;
504         int bytes_read;
505         unsigned long int kmtime;
506
507         if(*(obj->data.s) != 0) {
508                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
509                 strcpy(obj->data.s, buf);
510                 free(buf);
511                 buf = readfile(obj->data.s, &bytes_read, 1);
512                 if(buf != NULL) {
513                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %lu", &kmtime);
514                         snprintf(p, p_max_size, "%.2f", (float) kmtime / 100);
515                         free(buf);
516                 }
517         } else {
518                 NORM_ERR("$pid_time_usermode didn't receive a argument");
519         }
520 }
521
522 void print_pid_time(struct text_object *obj, char *p, int p_max_size) {
523         char *buf = NULL;
524         int bytes_read;
525         unsigned long int umtime, kmtime;
526
527         if(*(obj->data.s) != 0) {
528                 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
529                 strcpy(obj->data.s, buf);
530                 free(buf);
531                 buf = readfile(obj->data.s, &bytes_read, 1);
532                 if(buf != NULL) {
533                         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &umtime, &kmtime);
534                         snprintf(p, p_max_size, "%.2f", (float) (umtime + kmtime) / 100);
535                         free(buf);
536                 }
537         } else {
538                 NORM_ERR("$pid_time didn't receive a argument");
539         }
540 }
541
542 #define UID_ENTRY "Uid:\t"
543 void print_pid_uid(struct text_object *obj, char *p, int p_max_size) {
544 #define UIDNOTFOUND     "Can't find the process real uid in '%s'"
545         char *begin, *end, *buf = NULL;
546         int bytes_read;
547
548         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
549         strcpy(obj->data.s, buf);
550         free(buf);
551         buf = readfile(obj->data.s, &bytes_read, 1);
552         if(buf != NULL) {
553                 begin = strstr(buf, UID_ENTRY);
554                 if(begin != NULL) {
555                         begin += strlen(UID_ENTRY);
556                         end = strchr(begin, '\t');
557                         if(end != NULL) {
558                                 *(end) = 0;
559                         }
560                         snprintf(p, p_max_size, "%s", begin);
561                 } else {
562                         NORM_ERR(UIDNOTFOUND, obj->data.s);
563                 }
564                 free(buf);
565         }
566 }
567
568 void print_pid_euid(struct text_object *obj, char *p, int p_max_size) {
569 #define EUIDNOTFOUND    "Can't find the process effective uid in '%s'"
570         char *begin, *end, *buf = NULL;
571         int bytes_read;
572
573         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
574         strcpy(obj->data.s, buf);
575         free(buf);
576         buf = readfile(obj->data.s, &bytes_read, 1);
577         if(buf != NULL) {
578                 begin = strstr(buf, UID_ENTRY);
579                 if(begin != NULL) {
580                         begin = strchr(begin, '\t'); begin++;
581                         begin = strchr(begin, '\t'); begin++;
582                         end = strchr(begin, '\t');
583                         if(end != NULL) {
584                                 *(end) = 0;
585                         }
586                         snprintf(p, p_max_size, "%s", begin);
587                 } else {
588                         NORM_ERR(EUIDNOTFOUND, obj->data.s);
589                 }
590                 free(buf);
591         }
592 }
593
594 void print_pid_suid(struct text_object *obj, char *p, int p_max_size) {
595 #define SUIDNOTFOUND    "Can't find the process saved set uid in '%s'"
596         char *begin, *end, *buf = NULL;
597         int bytes_read;
598
599         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
600         strcpy(obj->data.s, buf);
601         free(buf);
602         buf = readfile(obj->data.s, &bytes_read, 1);
603         if(buf != NULL) {
604                 begin = strstr(buf, UID_ENTRY);
605                 if(begin != NULL) {
606                         begin = strchr(begin, '\t'); begin++;
607                         begin = strchr(begin, '\t'); begin++;
608                         begin = strchr(begin, '\t'); begin++;
609                         end = strchr(begin, '\t');
610                         if(end != NULL) {
611                                 *(end) = 0;
612                         }
613                         snprintf(p, p_max_size, "%s", begin);
614                 } else {
615                         NORM_ERR(SUIDNOTFOUND, obj->data.s);
616                 }
617                 free(buf);
618         }
619 }
620
621 void print_pid_fsuid(struct text_object *obj, char *p, int p_max_size) {
622 #define FSUIDNOTFOUND   "Can't find the process file system uid in '%s'"
623         char *begin, *end, *buf = NULL;
624         int bytes_read;
625
626         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
627         strcpy(obj->data.s, buf);
628         free(buf);
629         buf = readfile(obj->data.s, &bytes_read, 1);
630         if(buf != NULL) {
631                 begin = strstr(buf, UID_ENTRY);
632                 if(begin != NULL) {
633                         begin = strchr(begin, '\t'); begin++;
634                         begin = strchr(begin, '\t'); begin++;
635                         begin = strchr(begin, '\t'); begin++;
636                         begin = strchr(begin, '\t'); begin++;
637                         end = strchr(begin, '\n');
638                         if(end != NULL) {
639                                 *(end) = 0;
640                         }
641                         snprintf(p, p_max_size, "%s", begin);
642                 } else {
643                         NORM_ERR(FSUIDNOTFOUND, obj->data.s);
644                 }
645                 free(buf);
646         }
647 }
648
649 #define GID_ENTRY "Gid:\t"
650 void print_pid_gid(struct text_object *obj, char *p, int p_max_size) {
651 #define GIDNOTFOUND     "Can't find the process real gid in '%s'"
652         char *begin, *end, *buf = NULL;
653         int bytes_read;
654
655         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
656         strcpy(obj->data.s, buf);
657         free(buf);
658         buf = readfile(obj->data.s, &bytes_read, 1);
659         if(buf != NULL) {
660                 begin = strstr(buf, GID_ENTRY);
661                 if(begin != NULL) {
662                         begin += strlen(GID_ENTRY);
663                         end = strchr(begin, '\t');
664                         if(end != NULL) {
665                                 *(end) = 0;
666                         }
667                         snprintf(p, p_max_size, "%s", begin);
668                 } else {
669                         NORM_ERR(GIDNOTFOUND, obj->data.s);
670                 }
671                 free(buf);
672         }
673 }
674
675 void print_pid_egid(struct text_object *obj, char *p, int p_max_size) {
676 #define EGIDNOTFOUND    "Can't find the process effective gid in '%s'"
677         char *begin, *end, *buf = NULL;
678         int bytes_read;
679
680         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
681         strcpy(obj->data.s, buf);
682         free(buf);
683         buf = readfile(obj->data.s, &bytes_read, 1);
684         if(buf != NULL) {
685                 begin = strstr(buf, GID_ENTRY);
686                 if(begin != NULL) {
687                         begin = strchr(begin, '\t'); begin++;
688                         begin = strchr(begin, '\t'); begin++;
689                         end = strchr(begin, '\t');
690                         if(end != NULL) {
691                                 *(end) = 0;
692                         }
693                         snprintf(p, p_max_size, "%s", begin);
694                 } else {
695                         NORM_ERR(EGIDNOTFOUND, obj->data.s);
696                 }
697                 free(buf);
698         }
699 }
700
701 void print_pid_sgid(struct text_object *obj, char *p, int p_max_size) {
702 #define SGIDNOTFOUND    "Can't find the process saved set gid in '%s'"
703         char *begin, *end, *buf = NULL;
704         int bytes_read;
705
706         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
707         strcpy(obj->data.s, buf);
708         free(buf);
709         buf = readfile(obj->data.s, &bytes_read, 1);
710         if(buf != NULL) {
711                 begin = strstr(buf, GID_ENTRY);
712                 if(begin != NULL) {
713                         begin = strchr(begin, '\t'); begin++;
714                         begin = strchr(begin, '\t'); begin++;
715                         begin = strchr(begin, '\t'); begin++;
716                         end = strchr(begin, '\t');
717                         if(end != NULL) {
718                                 *(end) = 0;
719                         }
720                         snprintf(p, p_max_size, "%s", begin);
721                 } else {
722                         NORM_ERR(SGIDNOTFOUND, obj->data.s);
723                 }
724                 free(buf);
725         }
726 }
727
728 void print_pid_fsgid(struct text_object *obj, char *p, int p_max_size) {
729 #define FSGIDNOTFOUND   "Can't find the process file system gid in '%s'"
730         char *begin, *end, *buf = NULL;
731         int bytes_read;
732
733         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
734         strcpy(obj->data.s, buf);
735         free(buf);
736         buf = readfile(obj->data.s, &bytes_read, 1);
737         if(buf != NULL) {
738                 begin = strstr(buf, GID_ENTRY);
739                 if(begin != NULL) {
740                         begin = strchr(begin, '\t'); begin++;
741                         begin = strchr(begin, '\t'); begin++;
742                         begin = strchr(begin, '\t'); begin++;
743                         begin = strchr(begin, '\t'); begin++;
744                         end = strchr(begin, '\n');
745                         if(end != NULL) {
746                                 *(end) = 0;
747                         }
748                         snprintf(p, p_max_size, "%s", begin);
749                 } else {
750                         NORM_ERR(FSGIDNOTFOUND, obj->data.s);
751                 }
752                 free(buf);
753         }
754 }
755
756 void internal_print_pid_vm(char* pid, char *p, int p_max_size, const char* entry, const char* errorstring) {
757         char *begin, *end, *buf = NULL;
758         int bytes_read;
759
760         asprintf(&buf, PROCDIR "/%s/status", pid);
761         strcpy(pid, buf);
762         free(buf);
763         buf = readfile(pid, &bytes_read, 1);
764         if(buf != NULL) {
765                 begin = strstr(buf, entry);
766                 if(begin != NULL) {
767                         begin += strlen(entry);
768                         while(*begin == '\t' || *begin == ' ') {
769                                 begin++;
770                         }
771                         end = strchr(begin, '\n');
772                         if(end != NULL) {
773                                 *(end) = 0;
774                         }
775                         snprintf(p, p_max_size, "%s", begin);
776                 } else {
777                         NORM_ERR(errorstring, pid);
778                 }
779                 free(buf);
780         }
781 }
782
783 void print_pid_vmpeak(struct text_object *obj, char *p, int p_max_size) {
784         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPeak:\t", "Can't find the process peak virtual memory size in '%s'");
785 }
786
787 void print_pid_vmsize(struct text_object *obj, char *p, int p_max_size) {
788         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmSize:\t", "Can't find the process virtual memory size in '%s'");
789 }
790
791 void print_pid_vmlck(struct text_object *obj, char *p, int p_max_size) {
792         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLck:\t", "Can't find the process locked memory size in '%s'");
793 }
794
795 void print_pid_vmhwm(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 peak resident set size in '%s'");
797 }
798
799 void print_pid_vmrss(struct text_object *obj, char *p, int p_max_size) {
800         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process resident set size in '%s'");
801 }
802
803 void print_pid_vmdata(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 data segment size in '%s'");
805 }
806
807 void print_pid_vmstk(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 stack segment size in '%s'");
809 }
810
811 void print_pid_vmexe(struct text_object *obj, char *p, int p_max_size) {
812         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process text segment size in '%s'");
813 }
814
815 void print_pid_vmlib(struct text_object *obj, char *p, int p_max_size) {
816         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLib:\t", "Can't find the process shared library code size in '%s'");
817 }
818
819 void print_pid_vmpte(struct text_object *obj, char *p, int p_max_size) {
820         internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPTE:\t", "Can't find the process page table entries size in '%s'");
821 }
822
823 #define READ_ENTRY "read_bytes: "
824 #define READNOTFOUND    "Can't find the amount of bytes read in '%s'"
825 void print_pid_read(struct text_object *obj, char *p, int p_max_size) {
826         char *begin, *end, *buf = NULL;
827         int bytes_read;
828
829         asprintf(&buf, PROCDIR "/%s/io", obj->data.s);
830         strcpy(obj->data.s, buf);
831         free(buf);
832         buf = readfile(obj->data.s, &bytes_read, 1);
833         if(buf != NULL) {
834                 begin = strstr(buf, READ_ENTRY);
835                 if(begin != NULL) {
836                         end = strchr(begin, '\n');
837                         if(end != NULL) {
838                                 *(end) = 0;
839                         }
840                         snprintf(p, p_max_size, "%s", begin);
841                 } else {
842                         NORM_ERR(READNOTFOUND, obj->data.s);
843                 }
844                 free(buf);
845         }
846 }
847
848 #define WRITE_ENTRY "write_bytes: "
849 #define WRITENOTFOUND   "Can't find the amount of bytes written in '%s'"
850 void print_pid_write(struct text_object *obj, char *p, int p_max_size) {
851         char *begin, *end, *buf = NULL;
852         int bytes_read;
853
854         asprintf(&buf, PROCDIR "/%s/io", obj->data.s);
855         strcpy(obj->data.s, buf);
856         free(buf);
857         buf = readfile(obj->data.s, &bytes_read, 1);
858         if(buf != NULL) {
859                 begin = strstr(buf, WRITE_ENTRY);
860                 if(begin != NULL) {
861                         end = strchr(begin, '\n');
862                         if(end != NULL) {
863                                 *(end) = 0;
864                         }
865                         snprintf(p, p_max_size, "%s", begin);
866                 } else {
867                         NORM_ERR(WRITENOTFOUND, obj->data.s);
868                 }
869                 free(buf);
870         }
871 }