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-2010 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 if(*(obj->data.s) != 0) {
119 asprintf(&buf, PROCDIR "/%s/cmdline", obj->data.s);
120 strcpy(obj->data.s, buf);
122 buf = readfile(obj->data.s, &bytes_read, 1);
124 for(i = 0; i < bytes_read-1; i++) {
129 snprintf(p, p_max_size, "%s", buf);
133 NORM_ERR("$pid_cmdline didn't receive a argument");
137 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
139 char buf[p_max_size];
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) {
147 snprintf(p, p_max_size, "%s", buf);
149 NORM_ERR(READERR, obj->data.s);
153 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
157 char *buf, *file, *var=strdup(obj->data.s);;
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]);
163 asprintf(&file, PROCDIR "/%d/environ", pid);
164 buf = readfile(file, &total_read, 1);
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);
182 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
186 int bytes_read, total_read;
189 asprintf(&buf, PROCDIR "/%s/environ", obj->data.s);
190 strcpy(obj->data.s, buf);
192 buf = readfile(obj->data.s, &total_read, 1);
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);
202 snprintf(p, p_max_size, "%s", buf);
207 void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
210 asprintf(&buffer, PROCDIR "/%s/exe", obj->data.s);
211 pid_readlink(buffer, p, p_max_size);
215 void print_pid_nice(struct text_object *obj, char *p, int p_max_size) {
220 if(*(obj->data.s) != 0) {
221 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
222 strcpy(obj->data.s, buf);
224 buf = readfile(obj->data.s, &bytes_read, 1);
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);
231 NORM_ERR("$pid_nice didn't receive a argument");
235 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
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;
243 dir = opendir(obj->data.s);
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);
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("; ");
255 if(files_front == NULL) {
256 files_front = files_back;
261 freelist(files_front);
262 p[totallength - strlen("; ")] = 0;
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;
274 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
275 strcpy(obj->data.s, buf);
277 buf = readfile(obj->data.s, &bytes_read, 1);
279 begin = strstr(buf, PARENT_ENTRY);
281 begin += strlen(PARENT_ENTRY);
282 end = strchr(begin, '\n');
286 snprintf(p, p_max_size, "%s", begin);
288 NORM_ERR(PARENTNOTFOUND, obj->data.s);
294 void print_pid_priority(struct text_object *obj, char *p, int p_max_size) {
299 if(*(obj->data.s) != 0) {
300 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
301 strcpy(obj->data.s, buf);
303 buf = readfile(obj->data.s, &bytes_read, 1);
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);
310 NORM_ERR("$pid_priority didn't receive a argument");
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;
320 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
321 strcpy(obj->data.s, buf);
323 buf = readfile(obj->data.s, &bytes_read, 1);
325 begin = strstr(buf, STATE_ENTRY);
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');
330 *(end-1) = 0; // -1 strips the ')'
332 snprintf(p, p_max_size, "%s", begin);
334 NORM_ERR(STATENOTFOUND, obj->data.s);
340 void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
341 char *begin, *buf = NULL;
344 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
345 strcpy(obj->data.s, buf);
347 buf = readfile(obj->data.s, &bytes_read, 1);
349 begin = strstr(buf, STATE_ENTRY);
351 snprintf(p, p_max_size, "%c", *begin);
353 NORM_ERR(STATENOTFOUND, obj->data.s);
359 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
362 asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
363 pid_readlink(buffer, p, p_max_size);
367 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
370 asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
371 pid_readlink(buffer, p, p_max_size);
375 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
378 asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
379 pid_readlink(buffer, p, p_max_size);
383 void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
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);
393 if(obj->data.s[i - 1] == ' ') {
394 obj->data.s[i - 1] = 0;
397 CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
401 void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
403 struct dirent *entry;
404 char *filename, *buf;
407 dir = opendir(PROCDIR);
409 while ((entry = readdir(dir))) {
410 asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
411 buf = readfile(filename, &bytes_read, 0);
414 for(i = 0; i < bytes_read - 1; i++) {
415 if(buf[i] == 0) buf[i] = ' ';
417 if(strstr(buf, obj->data.s) != NULL) {
418 snprintf(p, p_max_size, "%s", entry->d_name);
428 NORM_ERR(READERR, PROCDIR);
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;
438 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
439 strcpy(obj->data.s, buf);
441 buf = readfile(obj->data.s, &bytes_read, 1);
443 begin = strstr(buf, THREADS_ENTRY);
445 begin += strlen(THREADS_ENTRY);
446 end = strchr(begin, '\n');
450 snprintf(p, p_max_size, "%s", begin);
452 NORM_ERR(THREADSNOTFOUND, obj->data.s);
458 void print_pid_thread_list(struct text_object *obj, char *p, int p_max_size) {
461 struct dirent *entry;
464 asprintf(&buf, PROCDIR "/%s/task", obj->data.s);
465 strcpy(obj->data.s, buf);
467 dir = opendir(obj->data.s);
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;
476 if(p[totallength - 1] == ',') p[totallength - 1] = 0;
482 void print_pid_time_kernelmode(struct text_object *obj, char *p, int p_max_size) {
485 unsigned long int umtime;
487 if(*(obj->data.s) != 0) {
488 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
489 strcpy(obj->data.s, buf);
491 buf = readfile(obj->data.s, &bytes_read, 1);
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);
498 NORM_ERR("$pid_time_kernelmode didn't receive a argument");
502 void print_pid_time_usermode(struct text_object *obj, char *p, int p_max_size) {
505 unsigned long int kmtime;
507 if(*(obj->data.s) != 0) {
508 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
509 strcpy(obj->data.s, buf);
511 buf = readfile(obj->data.s, &bytes_read, 1);
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);
518 NORM_ERR("$pid_time_usermode didn't receive a argument");
522 void print_pid_time(struct text_object *obj, char *p, int p_max_size) {
525 unsigned long int umtime, kmtime;
527 if(*(obj->data.s) != 0) {
528 asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
529 strcpy(obj->data.s, buf);
531 buf = readfile(obj->data.s, &bytes_read, 1);
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);
538 NORM_ERR("$pid_time didn't receive a argument");
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;
548 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
549 strcpy(obj->data.s, buf);
551 buf = readfile(obj->data.s, &bytes_read, 1);
553 begin = strstr(buf, UID_ENTRY);
555 begin += strlen(UID_ENTRY);
556 end = strchr(begin, '\t');
560 snprintf(p, p_max_size, "%s", begin);
562 NORM_ERR(UIDNOTFOUND, obj->data.s);
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;
573 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
574 strcpy(obj->data.s, buf);
576 buf = readfile(obj->data.s, &bytes_read, 1);
578 begin = strstr(buf, UID_ENTRY);
580 begin = strchr(begin, '\t'); begin++;
581 begin = strchr(begin, '\t'); begin++;
582 end = strchr(begin, '\t');
586 snprintf(p, p_max_size, "%s", begin);
588 NORM_ERR(EUIDNOTFOUND, obj->data.s);
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;
599 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
600 strcpy(obj->data.s, buf);
602 buf = readfile(obj->data.s, &bytes_read, 1);
604 begin = strstr(buf, UID_ENTRY);
606 begin = strchr(begin, '\t'); begin++;
607 begin = strchr(begin, '\t'); begin++;
608 begin = strchr(begin, '\t'); begin++;
609 end = strchr(begin, '\t');
613 snprintf(p, p_max_size, "%s", begin);
615 NORM_ERR(SUIDNOTFOUND, obj->data.s);
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;
626 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
627 strcpy(obj->data.s, buf);
629 buf = readfile(obj->data.s, &bytes_read, 1);
631 begin = strstr(buf, UID_ENTRY);
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');
641 snprintf(p, p_max_size, "%s", begin);
643 NORM_ERR(FSUIDNOTFOUND, obj->data.s);
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;
655 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
656 strcpy(obj->data.s, buf);
658 buf = readfile(obj->data.s, &bytes_read, 1);
660 begin = strstr(buf, GID_ENTRY);
662 begin += strlen(GID_ENTRY);
663 end = strchr(begin, '\t');
667 snprintf(p, p_max_size, "%s", begin);
669 NORM_ERR(GIDNOTFOUND, obj->data.s);
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;
680 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
681 strcpy(obj->data.s, buf);
683 buf = readfile(obj->data.s, &bytes_read, 1);
685 begin = strstr(buf, GID_ENTRY);
687 begin = strchr(begin, '\t'); begin++;
688 begin = strchr(begin, '\t'); begin++;
689 end = strchr(begin, '\t');
693 snprintf(p, p_max_size, "%s", begin);
695 NORM_ERR(EGIDNOTFOUND, obj->data.s);
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;
706 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
707 strcpy(obj->data.s, buf);
709 buf = readfile(obj->data.s, &bytes_read, 1);
711 begin = strstr(buf, GID_ENTRY);
713 begin = strchr(begin, '\t'); begin++;
714 begin = strchr(begin, '\t'); begin++;
715 begin = strchr(begin, '\t'); begin++;
716 end = strchr(begin, '\t');
720 snprintf(p, p_max_size, "%s", begin);
722 NORM_ERR(SGIDNOTFOUND, obj->data.s);
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;
733 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
734 strcpy(obj->data.s, buf);
736 buf = readfile(obj->data.s, &bytes_read, 1);
738 begin = strstr(buf, GID_ENTRY);
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');
748 snprintf(p, p_max_size, "%s", begin);
750 NORM_ERR(FSGIDNOTFOUND, obj->data.s);
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;
760 asprintf(&buf, PROCDIR "/%s/status", pid);
763 buf = readfile(pid, &bytes_read, 1);
765 begin = strstr(buf, entry);
767 begin += strlen(entry);
768 while(*begin == '\t' || *begin == ' ') {
771 end = strchr(begin, '\n');
775 snprintf(p, p_max_size, "%s", begin);
777 NORM_ERR(errorstring, pid);
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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;
829 asprintf(&buf, PROCDIR "/%s/io", obj->data.s);
830 strcpy(obj->data.s, buf);
832 buf = readfile(obj->data.s, &bytes_read, 1);
834 begin = strstr(buf, READ_ENTRY);
836 end = strchr(begin, '\n');
840 snprintf(p, p_max_size, "%s", begin);
842 NORM_ERR(READNOTFOUND, obj->data.s);
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;
854 asprintf(&buf, PROCDIR "/%s/io", obj->data.s);
855 strcpy(obj->data.s, buf);
857 buf = readfile(obj->data.s, &bytes_read, 1);
859 begin = strstr(buf, WRITE_ENTRY);
861 end = strchr(begin, '\n');
865 snprintf(p, p_max_size, "%s", begin);
867 NORM_ERR(WRITENOTFOUND, obj->data.s);