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);