Add support for $pid_parent
[monky] / src / proc.c
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
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *   (see AUTHORS)
15  * All rights reserved.
16  *
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.
21  *
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/>.
28  *
29  */
30
31 #include <logging.h>
32 #include "conky.h"
33 #include "proc.h"
34 #include <unistd.h>
35 #include <ctype.h>
36 #include <dirent.h>
37
38 char* readfile(char* filename, int* total_read, char showerror) {
39         FILE* file;
40         char* buf = NULL;
41         int bytes_read;
42
43         *total_read = 0;
44         file = fopen(filename, "r");
45         if(file) {
46                 do {
47                         buf = realloc(buf, *total_read + READSIZE + 1);
48                         bytes_read = fread(buf + *total_read, 1, READSIZE, file);
49                         *total_read += bytes_read;
50                         buf[*total_read] = 0;
51                 }while(bytes_read != 0);
52                 fclose(file);
53         } else if(showerror != 0) {
54                 NORM_ERR(READERR, filename);
55         }
56         return buf;
57 }
58
59 void pid_readlink(char *file, char *p, int p_max_size)
60 {
61         char buf[p_max_size];
62
63         memset(buf, 0, p_max_size);
64         if(readlink(file, buf, p_max_size) >= 0) {
65                 snprintf(p, p_max_size, "%s", buf);
66         } else {
67                 NORM_ERR(READERR, file);
68         }
69 }
70
71 struct ll_string {
72         char *string;
73         struct ll_string* next;
74 };
75
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);
79         current->next = NULL;
80         if(end != NULL) end->next = current;
81         return current;
82 }
83
84 void freelist(struct ll_string* front) {
85         if(front != NULL) {
86                 free(front->string);
87                 if(front->next != NULL) {
88                         freelist(front->next);
89                 }
90                 free(front);
91         }
92 }
93
94 int inlist(struct ll_string* front, char* string) {
95         struct ll_string* current;
96
97         for(current = front; current != NULL; current = current->next) {
98                 if(strcmp(current->string, string) == 0) {
99                         return 1;
100                 }
101         }
102         return 0;
103 }
104
105 void print_pid_chroot(struct text_object *obj, char *p, int p_max_size) {
106         char *buffer;
107
108         asprintf(&buffer, PROCDIR "/%s/root", obj->data.s);
109         pid_readlink(buffer, p, p_max_size);
110         free(buffer);
111 }
112
113 void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
114 {
115         char* buf;
116         int i, bytes_read;
117
118         asprintf(&buf, PROCDIR "/%s/cmdline", obj->data.s);
119         strcpy(obj->data.s, buf);
120         free(buf);
121         buf = readfile(obj->data.s, &bytes_read, 1);
122         if(buf != NULL) {
123                 for(i = 0; i < bytes_read-1; i++) {
124                         if(buf[i] == 0) {
125                                 buf[i] = ' ';
126                         }
127                 }
128                 snprintf(p, p_max_size, "%s", buf);
129                 free(buf);
130         }
131 }
132
133 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
134 {
135         char buf[p_max_size];
136         int bytes_read;
137
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) {
142                 buf[bytes_read] = 0;
143                 snprintf(p, p_max_size, "%s", buf);
144         } else {
145                 NORM_ERR(READERR, obj->data.s);
146         }
147 }
148
149 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
150 {
151         int i, total_read;
152         pid_t pid;
153         char *buf, *file, *var=strdup(obj->data.s);;
154
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]);
158                 }
159                 asprintf(&file, PROCDIR "/%d/environ", pid);
160                 buf = readfile(file, &total_read, 1);
161                 free(file);
162                 if(buf != NULL) {
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);
166                                         free(buf);
167                                         free(var);
168                                         return;
169                                 }
170                         }
171                         free(buf);
172                 }
173                 free(var);
174                 *p = 0;
175         }
176 }
177
178 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
179 {
180         char *buf = NULL;
181         char *buf2;
182         int bytes_read, total_read;
183         int i = 0;
184
185         asprintf(&buf, PROCDIR "/%s/environ", obj->data.s);
186         strcpy(obj->data.s, buf);
187         free(buf);
188         buf = readfile(obj->data.s, &total_read, 1);
189         if(buf != NULL) {
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);
194                         free(buf2);
195                         i = strlen(buf) + 1;
196                 }
197                 buf[i-1] = 0;
198                 snprintf(p, p_max_size, "%s", buf);
199                 free(buf);
200         }
201 }
202
203 void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
204         char *buffer;
205
206         asprintf(&buffer, PROCDIR "/%s/exe", obj->data.s);
207         pid_readlink(buffer, p, p_max_size);
208         free(buffer);
209 }
210
211 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
212         DIR* dir;
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;
218
219         dir = opendir(obj->data.s);
220         if(dir != NULL) {
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);
225                                 buf[length] = 0;
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("; ");
230                                 }
231                                 if(files_front == NULL) {
232                                         files_front = files_back;
233                                 }
234                         }
235                 }
236                 closedir(dir);
237                 freelist(files_front);
238                 p[totallength - strlen("; ")] = 0;
239         } else {
240                 p[0] = 0;
241         }
242 }
243
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;
248         int bytes_read;
249
250         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
251         strcpy(obj->data.s, buf);
252         free(buf);
253         buf = readfile(obj->data.s, &bytes_read, 1);
254         if(buf != NULL) {
255                 begin = strstr(buf, PARENT_ENTRY);
256                 if(begin != NULL) {
257                         begin += strlen(PARENT_ENTRY);
258                         end = strchr(begin, '\n');
259                         if(end != NULL) {
260                                 *(end) = 0;
261                         }
262                         snprintf(p, p_max_size, "%s", begin);
263                 } else {
264                         NORM_ERR(PARENTNOTFOUND, obj->data.s);
265                 }
266                 free(buf);
267         }
268 }
269
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;
274         int bytes_read;
275
276         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
277         strcpy(obj->data.s, buf);
278         free(buf);
279         buf = readfile(obj->data.s, &bytes_read, 1);
280         if(buf != NULL) {
281                 begin = strstr(buf, STATE_ENTRY);
282                 if(begin != NULL) {
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');
285                         if(end != NULL) {
286                                 *(end-1) = 0;   // -1 strips the ')'
287                         }
288                         snprintf(p, p_max_size, "%s", begin);
289                 } else {
290                         NORM_ERR(STATENOTFOUND, obj->data.s);
291                 }
292                 free(buf);
293         }
294 }
295
296 void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
297         char *begin, *buf = NULL;
298         int bytes_read;
299
300         asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
301         strcpy(obj->data.s, buf);
302         free(buf);
303         buf = readfile(obj->data.s, &bytes_read, 1);
304         if(buf != NULL) {
305                 begin = strstr(buf, STATE_ENTRY);
306                 if(begin != NULL) {
307                         snprintf(p, p_max_size, "%c", *begin);
308                 } else {
309                         NORM_ERR(STATENOTFOUND, obj->data.s);
310                 }
311                 free(buf);
312         }
313 }
314
315 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
316         char *buffer;
317
318         asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
319         pid_readlink(buffer, p, p_max_size);
320         free(buffer);
321 }
322
323 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
324         char *buffer;
325
326         asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
327         pid_readlink(buffer, p, p_max_size);
328         free(buffer);
329 }
330
331 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
332         char *buffer;
333
334         asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
335         pid_readlink(buffer, p, p_max_size);
336         free(buffer);
337 }
338
339 void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
340         unsigned int i;
341
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);
347                         }
348                 }
349                 if(obj->data.s[i - 1] == ' ') {
350                         obj->data.s[i - 1] = 0;
351                 }
352         } else {
353                 CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
354         }
355 }
356
357 void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
358         DIR* dir;
359         struct dirent *entry;
360         char *filename, *buf;
361         int bytes_read, i;
362
363         dir = opendir(PROCDIR);
364         if(dir != NULL) {
365                 while ((entry = readdir(dir))) {
366                         asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
367                         buf = readfile(filename, &bytes_read, 0);
368                         free(filename);
369                         if(buf != NULL) {
370                                 for(i = 0; i < bytes_read - 1; i++) {
371                                         if(buf[i] == 0) buf[i] = ' ';
372                                 }
373                                 if(strstr(buf, obj->data.s) != NULL) {
374                                         snprintf(p, p_max_size, "%s", entry->d_name);
375                                         free(buf);
376                                         closedir(dir);
377                                         return;
378                                 }
379                                 free(buf);
380                         }
381                 }
382                 closedir(dir);
383         } else {
384                 NORM_ERR(READERR, PROCDIR);
385         }
386 }