Added support for $pid_chroot
[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 void scan_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash, const char *file)
39 {
40         pid_t pid;
41
42         if(sscanf(arg, "%d", &pid) == 1) {
43                 asprintf(&obj->data.s, PROCDIR "/%d/%s", pid, file);
44         } else {
45                 CRIT_ERR(obj, free_at_crash, "syntax error: ${pid_%s pid}", file);
46         }
47 }
48
49 void scan_pid_cmdline_arg(struct text_object *obj, const char *arg, void* free_at_crash)
50 {
51         scan_pid_arg(obj, arg, free_at_crash, "cmdline");
52 }
53
54 void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
55 {
56         char buf[p_max_size];
57         FILE* infofile;
58         int i, bytes_read;
59
60         infofile = fopen(obj->data.s, "r");
61         if(infofile) {
62                 bytes_read = fread(buf, 1, p_max_size, infofile);
63                 for(i = 0; i < bytes_read-1; i++) {
64                         if(buf[i] == 0) {
65                                 buf[i] = ' ';
66                         }
67                 }
68                 snprintf(p, p_max_size, "%s", buf);
69                 fclose(infofile);
70         } else {
71                 NORM_ERR(READERR, obj->data.s);
72         }
73 }
74
75 void scan_pid_cwd_arg(struct text_object *obj, const char *arg, void* free_at_crash)
76 {
77         scan_pid_arg(obj, arg, free_at_crash, "cwd");
78 }
79
80 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
81 {
82         char buf[p_max_size];
83         int bytes_read;
84
85         memset(buf, 0, p_max_size);
86         bytes_read = readlink(obj->data.s, buf, p_max_size);
87         if(bytes_read != -1) {
88                 snprintf(p, p_max_size, "%s", buf);
89         } else {
90                 NORM_ERR(READERR, obj->data.s);
91         }
92 }
93
94 void scan_pid_environ_arg(struct text_object *obj, const char *arg, void* free_at_crash)
95 {
96         pid_t pid;
97         int i;
98         struct environ_data* ed = malloc(sizeof(struct environ_data));
99
100         ed->var = malloc(strlen(arg));
101         if(sscanf(arg, "%d %s", &pid, ed->var) == 2) {
102                 asprintf(&ed->file, PROCDIR "/%d/environ", pid);
103                 for(i = 0; ed->var[i] != 0; i++) {
104                         ed->var[i] = toupper(ed->var[i]);
105                 }
106                 obj->data.opaque = ed;
107         } else {
108                 free(ed->var);
109                 free(ed);
110                 CRIT_ERR(obj, free_at_crash, "${pid_environ pid varname}");
111         }
112 }
113
114 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
115 {
116         char *buf = NULL;
117         char *searchstring;
118         FILE* infofile;
119         int bytes_read, total_read = 0;
120
121         searchstring = malloc(strlen(((struct environ_data*) obj->data.opaque)->var) + strlen("=%[\1-\255]") + 1);
122         strcpy(searchstring, ((struct environ_data*) obj->data.opaque)->var);
123         strcat(searchstring, "=%[\1-\255]");
124         infofile = fopen(((struct environ_data*) obj->data.opaque)->file, "r");
125         if(infofile) {
126                 do {
127                         buf = realloc(buf, total_read + p_max_size + 1);
128                         bytes_read = fread(buf + total_read, 1, p_max_size, infofile);
129                         total_read += bytes_read;
130                         buf[total_read] = 0;
131                 }while(bytes_read != 0);
132                 for(bytes_read = 0; bytes_read < total_read; bytes_read += strlen(buf + bytes_read) + 1) {
133                         if(sscanf(buf + bytes_read, searchstring, p) == 1) {
134                                 free(buf);
135                                 free(searchstring);
136                                 fclose(infofile);
137                                 return;
138                         }
139                 }
140                 p[0] = 0;
141                 free(buf);
142                 free(searchstring);
143                 fclose(infofile);
144         } else {
145                 NORM_ERR(READERR, ((struct environ_data*) obj->data.opaque)->file);
146         }
147 }
148
149 void free_pid_environ(struct text_object *obj) {
150         free(((struct environ_data*) obj->data.opaque)->file);
151         free(((struct environ_data*) obj->data.opaque)->var);
152         free(obj->data.opaque);
153 }
154
155 void scan_pid_environ_list_arg(struct text_object *obj, const char *arg, void* free_at_crash)
156 {
157         scan_pid_arg(obj, arg, free_at_crash, "environ");
158 }
159
160 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
161 {
162         char *buf = NULL;
163         char *buf2;
164         FILE* infofile;
165         int bytes_read, total_read = 0;
166         int i = 0;
167
168         infofile = fopen(obj->data.s, "r");
169         if(infofile) {
170                 do {
171                         buf = realloc(buf, total_read + p_max_size + 1);
172                         bytes_read = fread(buf + total_read, 1, p_max_size, infofile);
173                         total_read += bytes_read;
174                         buf[total_read] = 0;
175                 }while(bytes_read != 0);
176                 while(bytes_read < total_read) {
177                         buf2 = strdup(buf+bytes_read);
178                         bytes_read += strlen(buf2)+1;
179                         sscanf(buf2, "%[^=]", buf+i);
180                         free(buf2);
181                         i = strlen(buf) + 1;
182                         buf[i-1] = ';';
183                 }
184                 buf[i-1] = 0;
185                 snprintf(p, p_max_size, "%s", buf);
186                 free(buf);
187                 fclose(infofile);
188         } else {
189                 NORM_ERR(READERR, obj->data.s);
190         }
191 }
192
193 void scan_pid_exe_arg(struct text_object *obj, const char *arg, void* free_at_crash)
194 {
195         scan_pid_arg(obj, arg, free_at_crash, "exe");
196 }
197
198 void print_pid_readlink(struct text_object *obj, char *p, int p_max_size)
199 {
200         char buf[p_max_size];
201
202         memset(buf, 0, p_max_size);
203         if(readlink(obj->data.s, buf, p_max_size) >= 0) {
204                 snprintf(p, p_max_size, "%s", buf);
205         } else {
206                 NORM_ERR(READERR, obj->data.s);
207         }
208 }
209
210 void scan_pid_chroot_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
211         scan_pid_arg(obj, arg, free_at_crash, "root");
212 }
213
214 void print_pid_chroot(struct text_object *obj, char *p, int p_max_size) {
215         print_pid_readlink(obj, p, p_max_size);
216 }
217
218 void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
219         print_pid_readlink(obj, p, p_max_size);
220 }
221
222 void scan_pid_stderr_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
223         scan_pid_arg(obj, arg, free_at_crash, "fd/2");
224 }
225
226 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
227         print_pid_readlink(obj, p, p_max_size);
228 }
229
230 void scan_pid_stdin_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
231         scan_pid_arg(obj, arg, free_at_crash, "fd/0");
232 }
233
234 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
235         print_pid_readlink(obj, p, p_max_size);
236 }
237
238 void scan_pid_stdout_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
239         scan_pid_arg(obj, arg, free_at_crash, "fd/1");
240 }
241
242 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
243         print_pid_readlink(obj, p, p_max_size);
244 }
245
246 void scan_pid_openfiles_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
247         scan_pid_arg(obj, arg, free_at_crash, "fd");
248 }
249
250 struct ll_string {
251         char *string;
252         struct ll_string* next;
253 };
254
255 struct ll_string* addnode(struct ll_string* end, char* string) {
256         struct ll_string* current = malloc(sizeof(struct ll_string));
257         current->string = strdup(string);
258         current->next = NULL;
259         if(end != NULL) end->next = current;
260         return current;
261 }
262
263 void freelist(struct ll_string* front) {
264         if(front != NULL) {
265                 free(front->string);
266                 if(front->next != NULL) {
267                         freelist(front->next);
268                 }
269                 free(front);
270         }
271 }
272
273 int inlist(struct ll_string* front, char* string) {
274         struct ll_string* current;
275
276         for(current = front; current != NULL; current = current->next) {
277                 if(strcmp(current->string, string) == 0) {
278                         return 1;
279                 }
280         }
281         return 0;
282 }
283
284 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
285         DIR* dir;
286         struct dirent *entry;
287         char buf[p_max_size];
288         int length, totallength = 0;
289         struct ll_string* files_front = NULL;
290         struct ll_string* files_back = NULL;
291
292         dir = opendir(obj->data.s);
293         if(dir != NULL) {
294                 while ((entry = readdir(dir))) {
295                         if(entry->d_name[0] != '.') {
296                                 snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
297                                 length = readlink(buf, buf, p_max_size);
298                                 buf[length] = 0;
299                                 if(inlist(files_front, buf) == 0) {
300                                         files_back = addnode(files_back, buf);
301                                         snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
302                                         totallength += length + strlen("; ");
303                                 }
304                                 if(files_front == NULL) {
305                                         files_front = files_back;
306                                 }
307                         }
308                 }
309                 closedir(dir);
310                 freelist(files_front);
311                 p[totallength - strlen("; ")] = 0;
312         } else {
313                 p[0] = 0;
314         }
315 }