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 if(*(obj->data.s) != 0) {
217 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
218 strcpy(obj->data.s, buf);
220 buf = readfile(obj->data.s, &bytes_read, 1);
222 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %ld", &nice_value);
223 snprintf(p, p_max_size, "%ld", nice_value);
227 NORM_ERR("$pid_nice didn't receive a argument");
231 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
233 struct dirent *entry;
234 char buf[p_max_size];
235 int length, totallength = 0;
236 struct ll_string* files_front = NULL;
237 struct ll_string* files_back = NULL;
239 dir = opendir(obj->data.s);
241 while ((entry = readdir(dir))) {
242 if(entry->d_name[0] != '.') {
243 snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
244 length = readlink(buf, buf, p_max_size);
246 if(inlist(files_front, buf) == 0) {
247 files_back = addnode(files_back, buf);
248 snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
249 totallength += length + strlen("; ");
251 if(files_front == NULL) {
252 files_front = files_back;
257 freelist(files_front);
258 p[totallength - strlen("; ")] = 0;
264 void print_pid_parent(struct text_object *obj, char *p, int p_max_size) {
265 #define PARENT_ENTRY "PPid:\t"
266 #define PARENTNOTFOUND "Can't find the process parent in '%s'"
267 char *begin, *end, *buf = NULL;
270 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
271 strcpy(obj->data.s, buf);
273 buf = readfile(obj->data.s, &bytes_read, 1);
275 begin = strstr(buf, PARENT_ENTRY);
277 begin += strlen(PARENT_ENTRY);
278 end = strchr(begin, '\n');
282 snprintf(p, p_max_size, "%s", begin);
284 NORM_ERR(PARENTNOTFOUND, obj->data.s);
290 void print_pid_priority(struct text_object *obj, char *p, int p_max_size) {
295 if(*(obj->data.s) != 0) {
296 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
297 strcpy(obj->data.s, buf);
299 buf = readfile(obj->data.s, &bytes_read, 1);
301 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %ld", &priority);
302 snprintf(p, p_max_size, "%ld", priority);
306 NORM_ERR("$pid_priority didn't receive a argument");
310 void print_pid_state(struct text_object *obj, char *p, int p_max_size) {
311 #define STATE_ENTRY "State:\t"
312 #define STATENOTFOUND "Can't find the process state in '%s'"
313 char *begin, *end, *buf = NULL;
316 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
317 strcpy(obj->data.s, buf);
319 buf = readfile(obj->data.s, &bytes_read, 1);
321 begin = strstr(buf, STATE_ENTRY);
323 begin += strlen(STATE_ENTRY) + 3; // +3 will strip the char representing the short state and the space and '(' that follow
324 end = strchr(begin, '\n');
326 *(end-1) = 0; // -1 strips the ')'
328 snprintf(p, p_max_size, "%s", begin);
330 NORM_ERR(STATENOTFOUND, obj->data.s);
336 void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
337 char *begin, *buf = NULL;
340 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
341 strcpy(obj->data.s, buf);
343 buf = readfile(obj->data.s, &bytes_read, 1);
345 begin = strstr(buf, STATE_ENTRY);
347 snprintf(p, p_max_size, "%c", *begin);
349 NORM_ERR(STATENOTFOUND, obj->data.s);
355 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
358 asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
359 pid_readlink(buffer, p, p_max_size);
363 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
366 asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
367 pid_readlink(buffer, p, p_max_size);
371 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
374 asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
375 pid_readlink(buffer, p, p_max_size);
379 void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
382 if(strlen(arg) > 0) {
383 obj->data.s = strdup(arg);
384 for(i = 0; obj->data.s[i] != 0; i++) {
385 while(obj->data.s[i] == ' ' && obj->data.s[i + 1] == ' ') {
386 memmove(obj->data.s + i, obj->data.s + i + 1, strlen(obj->data.s + i + 1) + 1);
389 if(obj->data.s[i - 1] == ' ') {
390 obj->data.s[i - 1] = 0;
393 CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
397 void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
399 struct dirent *entry;
400 char *filename, *buf;
403 dir = opendir(PROCDIR);
405 while ((entry = readdir(dir))) {
406 asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
407 buf = readfile(filename, &bytes_read, 0);
410 for(i = 0; i < bytes_read - 1; i++) {
411 if(buf[i] == 0) buf[i] = ' ';
413 if(strstr(buf, obj->data.s) != NULL) {
414 snprintf(p, p_max_size, "%s", entry->d_name);
424 NORM_ERR(READERR, PROCDIR);
428 void print_pid_threads(struct text_object *obj, char *p, int p_max_size) {
429 #define THREADS_ENTRY "Threads:\t"
430 #define THREADSNOTFOUND "Can't find the number of the threads of the process in '%s'"
431 char *begin, *end, *buf = NULL;
434 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
435 strcpy(obj->data.s, buf);
437 buf = readfile(obj->data.s, &bytes_read, 1);
439 begin = strstr(buf, THREADS_ENTRY);
441 begin += strlen(THREADS_ENTRY);
442 end = strchr(begin, '\n');
446 snprintf(p, p_max_size, "%s", begin);
448 NORM_ERR(THREADSNOTFOUND, obj->data.s);
454 void print_pid_thread_list(struct text_object *obj, char *p, int p_max_size) {
457 struct dirent *entry;
460 asprintf(&buf, PROCDIR "/%s/task", obj->data.s);
461 strcpy(obj->data.s, buf);
463 dir = opendir(obj->data.s);
465 while ((entry = readdir(dir))) {
466 if(entry->d_name[0] != '.') {
467 snprintf(p + totallength, p_max_size - totallength, "%s," , entry->d_name);
468 totallength += strlen(entry->d_name)+1;
472 if(p[totallength - 1] == ',') p[totallength - 1] = 0;
478 void print_pid_time_kernelmode(struct text_object *obj, char *p, int p_max_size) {
481 unsigned long int umtime;
483 if(*(obj->data.s) != 0) {
484 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
485 strcpy(obj->data.s, buf);
487 buf = readfile(obj->data.s, &bytes_read, 1);
489 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &umtime);
490 snprintf(p, p_max_size, "%.2f", (float) umtime / 100);
494 NORM_ERR("$pid_time_kernelmode didn't receive a argument");
498 void print_pid_time_usermode(struct text_object *obj, char *p, int p_max_size) {
501 unsigned long int kmtime;
503 if(*(obj->data.s) != 0) {
504 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
505 strcpy(obj->data.s, buf);
507 buf = readfile(obj->data.s, &bytes_read, 1);
509 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %lu", &kmtime);
510 snprintf(p, p_max_size, "%.2f", (float) kmtime / 100);
514 NORM_ERR("$pid_time_usermode didn't receive a argument");
518 void print_pid_time(struct text_object *obj, char *p, int p_max_size) {
521 unsigned long int umtime, kmtime;
523 if(*(obj->data.s) != 0) {
524 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
525 strcpy(obj->data.s, buf);
527 buf = readfile(obj->data.s, &bytes_read, 1);
529 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &umtime, &kmtime);
530 snprintf(p, p_max_size, "%.2f", (float) (umtime + kmtime) / 100);
534 NORM_ERR("$pid_time didn't receive a argument");
538 #define UID_ENTRY "Uid:\t"
539 void print_pid_uid(struct text_object *obj, char *p, int p_max_size) {
540 #define UIDNOTFOUND "Can't find the process real uid in '%s'"
541 char *begin, *end, *buf = NULL;
544 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
545 strcpy(obj->data.s, buf);
547 buf = readfile(obj->data.s, &bytes_read, 1);
549 begin = strstr(buf, UID_ENTRY);
551 begin += strlen(UID_ENTRY);
552 end = strchr(begin, '\t');
556 snprintf(p, p_max_size, "%s", begin);
558 NORM_ERR(UIDNOTFOUND, obj->data.s);
564 void print_pid_euid(struct text_object *obj, char *p, int p_max_size) {
565 #define EUIDNOTFOUND "Can't find the process effective uid in '%s'"
566 char *begin, *end, *buf = NULL;
569 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
570 strcpy(obj->data.s, buf);
572 buf = readfile(obj->data.s, &bytes_read, 1);
574 begin = strstr(buf, UID_ENTRY);
576 begin = strchr(begin, '\t'); begin++;
577 begin = strchr(begin, '\t'); begin++;
578 end = strchr(begin, '\t');
582 snprintf(p, p_max_size, "%s", begin);
584 NORM_ERR(EUIDNOTFOUND, obj->data.s);
590 void print_pid_suid(struct text_object *obj, char *p, int p_max_size) {
591 #define SUIDNOTFOUND "Can't find the process saved set uid in '%s'"
592 char *begin, *end, *buf = NULL;
595 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
596 strcpy(obj->data.s, buf);
598 buf = readfile(obj->data.s, &bytes_read, 1);
600 begin = strstr(buf, UID_ENTRY);
602 begin = strchr(begin, '\t'); begin++;
603 begin = strchr(begin, '\t'); begin++;
604 begin = strchr(begin, '\t'); begin++;
605 end = strchr(begin, '\t');
609 snprintf(p, p_max_size, "%s", begin);
611 NORM_ERR(SUIDNOTFOUND, obj->data.s);
617 void print_pid_fsuid(struct text_object *obj, char *p, int p_max_size) {
618 #define FSUIDNOTFOUND "Can't find the process file system uid in '%s'"
619 char *begin, *end, *buf = NULL;
622 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
623 strcpy(obj->data.s, buf);
625 buf = readfile(obj->data.s, &bytes_read, 1);
627 begin = strstr(buf, UID_ENTRY);
629 begin = strchr(begin, '\t'); begin++;
630 begin = strchr(begin, '\t'); begin++;
631 begin = strchr(begin, '\t'); begin++;
632 begin = strchr(begin, '\t'); begin++;
633 end = strchr(begin, '\n');
637 snprintf(p, p_max_size, "%s", begin);
639 NORM_ERR(FSUIDNOTFOUND, obj->data.s);
645 #define GID_ENTRY "Gid:\t"
646 void print_pid_gid(struct text_object *obj, char *p, int p_max_size) {
647 #define GIDNOTFOUND "Can't find the process real gid in '%s'"
648 char *begin, *end, *buf = NULL;
651 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
652 strcpy(obj->data.s, buf);
654 buf = readfile(obj->data.s, &bytes_read, 1);
656 begin = strstr(buf, GID_ENTRY);
658 begin += strlen(GID_ENTRY);
659 end = strchr(begin, '\t');
663 snprintf(p, p_max_size, "%s", begin);
665 NORM_ERR(GIDNOTFOUND, obj->data.s);
671 void print_pid_egid(struct text_object *obj, char *p, int p_max_size) {
672 #define EGIDNOTFOUND "Can't find the process effective gid in '%s'"
673 char *begin, *end, *buf = NULL;
676 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
677 strcpy(obj->data.s, buf);
679 buf = readfile(obj->data.s, &bytes_read, 1);
681 begin = strstr(buf, GID_ENTRY);
683 begin = strchr(begin, '\t'); begin++;
684 begin = strchr(begin, '\t'); begin++;
685 end = strchr(begin, '\t');
689 snprintf(p, p_max_size, "%s", begin);
691 NORM_ERR(EGIDNOTFOUND, obj->data.s);
697 void print_pid_sgid(struct text_object *obj, char *p, int p_max_size) {
698 #define SGIDNOTFOUND "Can't find the process saved set gid in '%s'"
699 char *begin, *end, *buf = NULL;
702 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
703 strcpy(obj->data.s, buf);
705 buf = readfile(obj->data.s, &bytes_read, 1);
707 begin = strstr(buf, GID_ENTRY);
709 begin = strchr(begin, '\t'); begin++;
710 begin = strchr(begin, '\t'); begin++;
711 begin = strchr(begin, '\t'); begin++;
712 end = strchr(begin, '\t');
716 snprintf(p, p_max_size, "%s", begin);
718 NORM_ERR(SGIDNOTFOUND, obj->data.s);
724 void print_pid_fsgid(struct text_object *obj, char *p, int p_max_size) {
725 #define FSGIDNOTFOUND "Can't find the process file system gid in '%s'"
726 char *begin, *end, *buf = NULL;
729 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
730 strcpy(obj->data.s, buf);
732 buf = readfile(obj->data.s, &bytes_read, 1);
734 begin = strstr(buf, GID_ENTRY);
736 begin = strchr(begin, '\t'); begin++;
737 begin = strchr(begin, '\t'); begin++;
738 begin = strchr(begin, '\t'); begin++;
739 begin = strchr(begin, '\t'); begin++;
740 end = strchr(begin, '\n');
744 snprintf(p, p_max_size, "%s", begin);
746 NORM_ERR(FSGIDNOTFOUND, obj->data.s);
752 void internal_print_pid_vm(char* pid, char *p, int p_max_size, const char* entry, const char* errorstring) {
753 char *begin, *end, *buf = NULL;
756 asprintf(&buf, PROCDIR "/%s/status", pid);
759 buf = readfile(pid, &bytes_read, 1);
761 begin = strstr(buf, entry);
763 begin += strlen(entry);
764 while(*begin == '\t' || *begin == ' ') {
767 end = strchr(begin, '\n');
771 snprintf(p, p_max_size, "%s", begin);
773 NORM_ERR(errorstring, pid);
779 void print_pid_vmpeak(struct text_object *obj, char *p, int p_max_size) {
780 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPeak:\t", "Can't find the process peak virtual memory size in '%s'");
783 void print_pid_vmsize(struct text_object *obj, char *p, int p_max_size) {
784 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmSize:\t", "Can't find the process virtual memory size in '%s'");
787 void print_pid_vmlck(struct text_object *obj, char *p, int p_max_size) {
788 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLck:\t", "Can't find the process locked memory size in '%s'");
791 void print_pid_vmhwm(struct text_object *obj, char *p, int p_max_size) {
792 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmHWM:\t", "Can't find the process peak resident set size in '%s'");
795 void print_pid_vmrss(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 resident set size in '%s'");
799 void print_pid_vmdata(struct text_object *obj, char *p, int p_max_size) {
800 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmData:\t", "Can't find the process data segment size in '%s'");
803 void print_pid_vmstk(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 stack segment size in '%s'");
807 void print_pid_vmexe(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 text segment size in '%s'");
811 void print_pid_vmlib(struct text_object *obj, char *p, int p_max_size) {
812 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmLib:\t", "Can't find the process shared library code size in '%s'");
815 void print_pid_vmpte(struct text_object *obj, char *p, int p_max_size) {
816 internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPTE:\t", "Can't find the process page table entries size in '%s'");