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_nice(struct text_object *obj, char *p, int p_max_size) {
216 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
217 strcpy(obj->data.s, buf);
219 buf = readfile(obj->data.s, &bytes_read, 1);
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);
227 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
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;
235 dir = opendir(obj->data.s);
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);
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("; ");
247 if(files_front == NULL) {
248 files_front = files_back;
253 freelist(files_front);
254 p[totallength - strlen("; ")] = 0;
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;
266 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
267 strcpy(obj->data.s, buf);
269 buf = readfile(obj->data.s, &bytes_read, 1);
271 begin = strstr(buf, PARENT_ENTRY);
273 begin += strlen(PARENT_ENTRY);
274 end = strchr(begin, '\n');
278 snprintf(p, p_max_size, "%s", begin);
280 NORM_ERR(PARENTNOTFOUND, obj->data.s);
286 void print_pid_priority(struct text_object *obj, char *p, int p_max_size) {
291 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
292 strcpy(obj->data.s, buf);
294 buf = readfile(obj->data.s, &bytes_read, 1);
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);
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;
308 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
309 strcpy(obj->data.s, buf);
311 buf = readfile(obj->data.s, &bytes_read, 1);
313 begin = strstr(buf, STATE_ENTRY);
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');
318 *(end-1) = 0; // -1 strips the ')'
320 snprintf(p, p_max_size, "%s", begin);
322 NORM_ERR(STATENOTFOUND, obj->data.s);
328 void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
329 char *begin, *buf = NULL;
332 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
333 strcpy(obj->data.s, buf);
335 buf = readfile(obj->data.s, &bytes_read, 1);
337 begin = strstr(buf, STATE_ENTRY);
339 snprintf(p, p_max_size, "%c", *begin);
341 NORM_ERR(STATENOTFOUND, obj->data.s);
347 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
350 asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
351 pid_readlink(buffer, p, p_max_size);
355 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
358 asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
359 pid_readlink(buffer, p, p_max_size);
363 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
366 asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
367 pid_readlink(buffer, p, p_max_size);
371 void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
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);
381 if(obj->data.s[i - 1] == ' ') {
382 obj->data.s[i - 1] = 0;
385 CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
389 void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
391 struct dirent *entry;
392 char *filename, *buf;
395 dir = opendir(PROCDIR);
397 while ((entry = readdir(dir))) {
398 asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
399 buf = readfile(filename, &bytes_read, 0);
402 for(i = 0; i < bytes_read - 1; i++) {
403 if(buf[i] == 0) buf[i] = ' ';
405 if(strstr(buf, obj->data.s) != NULL) {
406 snprintf(p, p_max_size, "%s", entry->d_name);
416 NORM_ERR(READERR, PROCDIR);
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;
426 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
427 strcpy(obj->data.s, buf);
429 buf = readfile(obj->data.s, &bytes_read, 1);
431 begin = strstr(buf, THREADS_ENTRY);
433 begin += strlen(THREADS_ENTRY);
434 end = strchr(begin, '\n');
438 snprintf(p, p_max_size, "%s", begin);
440 NORM_ERR(THREADSNOTFOUND, obj->data.s);
446 void print_pid_thread_list(struct text_object *obj, char *p, int p_max_size) {
449 struct dirent *entry;
452 asprintf(&buf, PROCDIR "/%s/task", obj->data.s);
453 strcpy(obj->data.s, buf);
455 dir = opendir(obj->data.s);
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;
464 if(p[totallength - 1] == ',') p[totallength - 1] = 0;
470 void print_pid_time_kernelmode(struct text_object *obj, char *p, int p_max_size) {
473 unsigned long int umtime;
475 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
476 strcpy(obj->data.s, buf);
478 buf = readfile(obj->data.s, &bytes_read, 1);
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);
486 void print_pid_time_usermode(struct text_object *obj, char *p, int p_max_size) {
489 unsigned long int kmtime;
491 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
492 strcpy(obj->data.s, buf);
494 buf = readfile(obj->data.s, &bytes_read, 1);
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);
502 void print_pid_time(struct text_object *obj, char *p, int p_max_size) {
505 unsigned long int umtime, kmtime;
507 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
508 strcpy(obj->data.s, buf);
510 buf = readfile(obj->data.s, &bytes_read, 1);
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);
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;
524 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
525 strcpy(obj->data.s, buf);
527 buf = readfile(obj->data.s, &bytes_read, 1);
529 begin = strstr(buf, UID_ENTRY);
531 begin += strlen(UID_ENTRY);
532 end = strchr(begin, '\t');
536 snprintf(p, p_max_size, "%s", begin);
538 NORM_ERR(UIDNOTFOUND, obj->data.s);
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;
549 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
550 strcpy(obj->data.s, buf);
552 buf = readfile(obj->data.s, &bytes_read, 1);
554 begin = strstr(buf, UID_ENTRY);
556 begin = strchr(begin, '\t'); begin++;
557 begin = strchr(begin, '\t'); begin++;
558 end = strchr(begin, '\t');
562 snprintf(p, p_max_size, "%s", begin);
564 NORM_ERR(EUIDNOTFOUND, obj->data.s);
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;
575 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
576 strcpy(obj->data.s, buf);
578 buf = readfile(obj->data.s, &bytes_read, 1);
580 begin = strstr(buf, UID_ENTRY);
582 begin = strchr(begin, '\t'); begin++;
583 begin = strchr(begin, '\t'); begin++;
584 begin = strchr(begin, '\t'); begin++;
585 end = strchr(begin, '\t');
589 snprintf(p, p_max_size, "%s", begin);
591 NORM_ERR(SUIDNOTFOUND, obj->data.s);
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;
602 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
603 strcpy(obj->data.s, buf);
605 buf = readfile(obj->data.s, &bytes_read, 1);
607 begin = strstr(buf, UID_ENTRY);
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');
617 snprintf(p, p_max_size, "%s", begin);
619 NORM_ERR(FSUIDNOTFOUND, obj->data.s);
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;
631 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
632 strcpy(obj->data.s, buf);
634 buf = readfile(obj->data.s, &bytes_read, 1);
636 begin = strstr(buf, GID_ENTRY);
638 begin += strlen(GID_ENTRY);
639 end = strchr(begin, '\t');
643 snprintf(p, p_max_size, "%s", begin);
645 NORM_ERR(GIDNOTFOUND, obj->data.s);
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;
656 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
657 strcpy(obj->data.s, buf);
659 buf = readfile(obj->data.s, &bytes_read, 1);
661 begin = strstr(buf, GID_ENTRY);
663 begin = strchr(begin, '\t'); begin++;
664 begin = strchr(begin, '\t'); begin++;
665 end = strchr(begin, '\t');
669 snprintf(p, p_max_size, "%s", begin);
671 NORM_ERR(EGIDNOTFOUND, obj->data.s);
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;
682 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
683 strcpy(obj->data.s, buf);
685 buf = readfile(obj->data.s, &bytes_read, 1);
687 begin = strstr(buf, GID_ENTRY);
689 begin = strchr(begin, '\t'); begin++;
690 begin = strchr(begin, '\t'); begin++;
691 begin = strchr(begin, '\t'); begin++;
692 end = strchr(begin, '\t');
696 snprintf(p, p_max_size, "%s", begin);
698 NORM_ERR(SGIDNOTFOUND, obj->data.s);
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;
709 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
710 strcpy(obj->data.s, buf);
712 buf = readfile(obj->data.s, &bytes_read, 1);
714 begin = strstr(buf, GID_ENTRY);
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');
724 snprintf(p, p_max_size, "%s", begin);
726 NORM_ERR(FSGIDNOTFOUND, obj->data.s);
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;
736 asprintf(&buf, PROCDIR "/%s/status", pid);
739 buf = readfile(pid, &bytes_read, 1);
741 begin = strstr(buf, entry);
743 begin += strlen(entry);
744 while(*begin == '\t' || *begin == ' ') {
747 end = strchr(begin, '\n');
751 snprintf(p, p_max_size, "%s", begin);
753 NORM_ERR(errorstring, pid);
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");