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
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
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.
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/>.
38 char* readfile(char* filename, int* total_read, char showerror) {
44 file = fopen(filename, "r");
47 buf = realloc(buf, *total_read + READSIZE + 1);
48 bytes_read = fread(buf + *total_read, 1, READSIZE, file);
49 *total_read += bytes_read;
51 }while(bytes_read != 0);
53 } else if(showerror != 0) {
54 NORM_ERR(READERR, filename);
59 void pid_readlink(char *file, char *p, int p_max_size)
63 memset(buf, 0, p_max_size);
64 if(readlink(file, buf, p_max_size) >= 0) {
65 snprintf(p, p_max_size, "%s", buf);
67 NORM_ERR(READERR, file);
73 struct ll_string* next;
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);
80 if(end != NULL) end->next = current;
84 void freelist(struct ll_string* front) {
87 if(front->next != NULL) {
88 freelist(front->next);
94 int inlist(struct ll_string* front, char* string) {
95 struct ll_string* current;
97 for(current = front; current != NULL; current = current->next) {
98 if(strcmp(current->string, string) == 0) {
105 void print_pid_chroot(struct text_object *obj, char *p, int p_max_size) {
108 asprintf(&buffer, PROCDIR "/%s/root", obj->data.s);
109 pid_readlink(buffer, p, p_max_size);
113 void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
118 asprintf(&buf, PROCDIR "/%s/cmdline", obj->data.s);
119 strcpy(obj->data.s, buf);
121 buf = readfile(obj->data.s, &bytes_read, 1);
123 for(i = 0; i < bytes_read-1; i++) {
128 snprintf(p, p_max_size, "%s", buf);
133 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
135 char buf[p_max_size];
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) {
143 snprintf(p, p_max_size, "%s", buf);
145 NORM_ERR(READERR, obj->data.s);
149 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
153 char *buf, *file, *var=strdup(obj->data.s);;
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]);
159 asprintf(&file, PROCDIR "/%d/environ", pid);
160 buf = readfile(file, &total_read, 1);
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);
178 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
182 int bytes_read, total_read;
185 asprintf(&buf, PROCDIR "/%s/environ", obj->data.s);
186 strcpy(obj->data.s, buf);
188 buf = readfile(obj->data.s, &total_read, 1);
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);
198 snprintf(p, p_max_size, "%s", buf);
203 void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
206 asprintf(&buffer, PROCDIR "/%s/exe", obj->data.s);
207 pid_readlink(buffer, p, p_max_size);
211 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
213 struct dirent *entry;
214 char buf[p_max_size];
215 int length, totallength = 0;
216 struct ll_string* files_front = NULL;
217 struct ll_string* files_back = NULL;
219 dir = opendir(obj->data.s);
221 while ((entry = readdir(dir))) {
222 if(entry->d_name[0] != '.') {
223 snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
224 length = readlink(buf, buf, p_max_size);
226 if(inlist(files_front, buf) == 0) {
227 files_back = addnode(files_back, buf);
228 snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
229 totallength += length + strlen("; ");
231 if(files_front == NULL) {
232 files_front = files_back;
237 freelist(files_front);
238 p[totallength - strlen("; ")] = 0;
244 void print_pid_parent(struct text_object *obj, char *p, int p_max_size) {
245 #define PARENT_ENTRY "PPid:\t"
246 #define PARENTNOTFOUND "Can't find the process parent in '%s'"
247 char *begin, *end, *buf = NULL;
250 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
251 strcpy(obj->data.s, buf);
253 buf = readfile(obj->data.s, &bytes_read, 1);
255 begin = strstr(buf, PARENT_ENTRY);
257 begin += strlen(PARENT_ENTRY);
258 end = strchr(begin, '\n');
262 snprintf(p, p_max_size, "%s", begin);
264 NORM_ERR(PARENTNOTFOUND, obj->data.s);
270 void print_pid_state(struct text_object *obj, char *p, int p_max_size) {
271 #define STATE_ENTRY "State:\t"
272 #define STATENOTFOUND "Can't find the process state in '%s'"
273 char *begin, *end, *buf = NULL;
276 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
277 strcpy(obj->data.s, buf);
279 buf = readfile(obj->data.s, &bytes_read, 1);
281 begin = strstr(buf, STATE_ENTRY);
283 begin += strlen(STATE_ENTRY) + 3; // +3 will strip the char representing the short state and the space and '(' that follow
284 end = strchr(begin, '\n');
286 *(end-1) = 0; // -1 strips the ')'
288 snprintf(p, p_max_size, "%s", begin);
290 NORM_ERR(STATENOTFOUND, obj->data.s);
296 void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
297 char *begin, *buf = NULL;
300 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
301 strcpy(obj->data.s, buf);
303 buf = readfile(obj->data.s, &bytes_read, 1);
305 begin = strstr(buf, STATE_ENTRY);
307 snprintf(p, p_max_size, "%c", *begin);
309 NORM_ERR(STATENOTFOUND, obj->data.s);
315 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
318 asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
319 pid_readlink(buffer, p, p_max_size);
323 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
326 asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
327 pid_readlink(buffer, p, p_max_size);
331 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
334 asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
335 pid_readlink(buffer, p, p_max_size);
339 void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
342 if(strlen(arg) > 0) {
343 obj->data.s = strdup(arg);
344 for(i = 0; obj->data.s[i] != 0; i++) {
345 while(obj->data.s[i] == ' ' && obj->data.s[i + 1] == ' ') {
346 memmove(obj->data.s + i, obj->data.s + i + 1, strlen(obj->data.s + i + 1) + 1);
349 if(obj->data.s[i - 1] == ' ') {
350 obj->data.s[i - 1] = 0;
353 CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
357 void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
359 struct dirent *entry;
360 char *filename, *buf;
363 dir = opendir(PROCDIR);
365 while ((entry = readdir(dir))) {
366 asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
367 buf = readfile(filename, &bytes_read, 0);
370 for(i = 0; i < bytes_read - 1; i++) {
371 if(buf[i] == 0) buf[i] = ' ';
373 if(strstr(buf, obj->data.s) != NULL) {
374 snprintf(p, p_max_size, "%s", entry->d_name);
384 NORM_ERR(READERR, PROCDIR);
388 #define UID_ENTRY "Uid:\t"
389 void print_pid_uid(struct text_object *obj, char *p, int p_max_size) {
390 #define UIDNOTFOUND "Can't find the process real uid in '%s'"
391 char *begin, *end, *buf = NULL;
394 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
395 strcpy(obj->data.s, buf);
397 buf = readfile(obj->data.s, &bytes_read, 1);
399 begin = strstr(buf, UID_ENTRY);
401 begin += strlen(UID_ENTRY);
402 end = strchr(begin, '\t');
406 snprintf(p, p_max_size, "%s", begin);
408 NORM_ERR(UIDNOTFOUND, obj->data.s);
414 void print_pid_euid(struct text_object *obj, char *p, int p_max_size) {
415 #define EUIDNOTFOUND "Can't find the process effective uid in '%s'"
416 char *begin, *end, *buf = NULL;
419 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
420 strcpy(obj->data.s, buf);
422 buf = readfile(obj->data.s, &bytes_read, 1);
424 begin = strstr(buf, UID_ENTRY);
426 begin = strchr(begin, '\t'); begin++;
427 begin = strchr(begin, '\t'); begin++;
428 end = strchr(begin, '\t');
432 snprintf(p, p_max_size, "%s", begin);
434 NORM_ERR(EUIDNOTFOUND, obj->data.s);
440 void print_pid_suid(struct text_object *obj, char *p, int p_max_size) {
441 #define SUIDNOTFOUND "Can't find the process saved set uid in '%s'"
442 char *begin, *end, *buf = NULL;
445 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
446 strcpy(obj->data.s, buf);
448 buf = readfile(obj->data.s, &bytes_read, 1);
450 begin = strstr(buf, UID_ENTRY);
452 begin = strchr(begin, '\t'); begin++;
453 begin = strchr(begin, '\t'); begin++;
454 begin = strchr(begin, '\t'); begin++;
455 end = strchr(begin, '\t');
459 snprintf(p, p_max_size, "%s", begin);
461 NORM_ERR(SUIDNOTFOUND, obj->data.s);
467 void print_pid_fsuid(struct text_object *obj, char *p, int p_max_size) {
468 #define FSUIDNOTFOUND "Can't find the process file system uid in '%s'"
469 char *begin, *end, *buf = NULL;
472 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
473 strcpy(obj->data.s, buf);
475 buf = readfile(obj->data.s, &bytes_read, 1);
477 begin = strstr(buf, UID_ENTRY);
479 begin = strchr(begin, '\t'); begin++;
480 begin = strchr(begin, '\t'); begin++;
481 begin = strchr(begin, '\t'); begin++;
482 begin = strchr(begin, '\t'); begin++;
483 end = strchr(begin, '\n');
487 snprintf(p, p_max_size, "%s", begin);
489 NORM_ERR(FSUIDNOTFOUND, obj->data.s);
495 #define GID_ENTRY "Gid:\t"
496 void print_pid_gid(struct text_object *obj, char *p, int p_max_size) {
497 #define GIDNOTFOUND "Can't find the process real gid in '%s'"
498 char *begin, *end, *buf = NULL;
501 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
502 strcpy(obj->data.s, buf);
504 buf = readfile(obj->data.s, &bytes_read, 1);
506 begin = strstr(buf, GID_ENTRY);
508 begin += strlen(GID_ENTRY);
509 end = strchr(begin, '\t');
513 snprintf(p, p_max_size, "%s", begin);
515 NORM_ERR(GIDNOTFOUND, obj->data.s);
521 void print_pid_egid(struct text_object *obj, char *p, int p_max_size) {
522 #define EGIDNOTFOUND "Can't find the process effective gid in '%s'"
523 char *begin, *end, *buf = NULL;
526 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
527 strcpy(obj->data.s, buf);
529 buf = readfile(obj->data.s, &bytes_read, 1);
531 begin = strstr(buf, GID_ENTRY);
533 begin = strchr(begin, '\t'); begin++;
534 begin = strchr(begin, '\t'); begin++;
535 end = strchr(begin, '\t');
539 snprintf(p, p_max_size, "%s", begin);
541 NORM_ERR(EGIDNOTFOUND, obj->data.s);
547 void print_pid_sgid(struct text_object *obj, char *p, int p_max_size) {
548 #define SGIDNOTFOUND "Can't find the process saved set gid in '%s'"
549 char *begin, *end, *buf = NULL;
552 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
553 strcpy(obj->data.s, buf);
555 buf = readfile(obj->data.s, &bytes_read, 1);
557 begin = strstr(buf, GID_ENTRY);
559 begin = strchr(begin, '\t'); begin++;
560 begin = strchr(begin, '\t'); begin++;
561 begin = strchr(begin, '\t'); begin++;
562 end = strchr(begin, '\t');
566 snprintf(p, p_max_size, "%s", begin);
568 NORM_ERR(SGIDNOTFOUND, obj->data.s);
574 void print_pid_fsgid(struct text_object *obj, char *p, int p_max_size) {
575 #define FSGIDNOTFOUND "Can't find the process file system gid in '%s'"
576 char *begin, *end, *buf = NULL;
579 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
580 strcpy(obj->data.s, buf);
582 buf = readfile(obj->data.s, &bytes_read, 1);
584 begin = strstr(buf, GID_ENTRY);
586 begin = strchr(begin, '\t'); begin++;
587 begin = strchr(begin, '\t'); begin++;
588 begin = strchr(begin, '\t'); begin++;
589 begin = strchr(begin, '\t'); begin++;
590 end = strchr(begin, '\n');
594 snprintf(p, p_max_size, "%s", begin);
596 NORM_ERR(FSGIDNOTFOUND, obj->data.s);
602 void internal_print_pid_vm(char* pid, char *p, int p_max_size, const char* entry, const char* errorstring) {
603 char *begin, *end, *buf = NULL;
606 asprintf(&buf, PROCDIR "/%s/status", pid);
609 buf = readfile(pid, &bytes_read, 1);
611 begin = strstr(buf, entry);
613 begin += strlen(entry);
614 while(*begin == '\t' || *begin == ' ') {
617 end = strchr(begin, '\n');
621 snprintf(p, p_max_size, "%s", begin);
623 NORM_ERR(errorstring, pid);
629 void print_pid_vmpeak(struct text_object *obj, char *p, int p_max_size) {
630 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPeak:\t", "Can't find the process peak virtual memory size in '%s'");
633 void print_pid_vmsize(struct text_object *obj, char *p, int p_max_size) {
634 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmSize:\t", "Can't find the process virtual memory size in '%s'");
637 void print_pid_vmlck(struct text_object *obj, char *p, int p_max_size) {
638 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLck:\t", "Can't find the process locked memory size in '%s'");
641 void print_pid_vmhwm(struct text_object *obj, char *p, int p_max_size) {
642 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process peak resident set size in '%s'");
645 void print_pid_vmrss(struct text_object *obj, char *p, int p_max_size) {
646 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process resident set size in '%s'");
649 void print_pid_vmdata(struct text_object *obj, char *p, int p_max_size) {
650 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process data segment size in '%s'");
653 void print_pid_vmstk(struct text_object *obj, char *p, int p_max_size) {
654 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process stack segment size in '%s'");
657 void print_pid_vmexe(struct text_object *obj, char *p, int p_max_size) {
658 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process text segment size in '%s'");
661 void print_pid_vmlib(struct text_object *obj, char *p, int p_max_size) {
662 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLib:\t", "Can't find the process shared library code size in '%s'");
665 void print_pid_vmpte(struct text_object *obj, char *p, int p_max_size) {
666 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPTE:\t", "Can't find the process page table entries size in '%s'");