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 void scan_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash, const char *file)
42 if(sscanf(arg, "%d", &pid) == 1) {
43 asprintf(&obj->data.s, PROCDIR "/%d/%s", pid, file);
45 CRIT_ERR(obj, free_at_crash, "syntax error: ${pid_%s pid}", file);
49 void scan_pid_cmdline_arg(struct text_object *obj, const char *arg, void* free_at_crash)
51 scan_pid_arg(obj, arg, free_at_crash, "cmdline");
54 void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
60 infofile = fopen(obj->data.s, "r");
62 bytes_read = fread(buf, 1, p_max_size, infofile);
63 for(i = 0; i < bytes_read-1; i++) {
68 snprintf(p, p_max_size, "%s", buf);
71 NORM_ERR(READERR, obj->data.s);
75 void scan_pid_cwd_arg(struct text_object *obj, const char *arg, void* free_at_crash)
77 scan_pid_arg(obj, arg, free_at_crash, "cwd");
80 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
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);
90 NORM_ERR(READERR, obj->data.s);
94 void scan_pid_environ_arg(struct text_object *obj, const char *arg, void* free_at_crash)
98 struct environ_data* ed = malloc(sizeof(struct environ_data));
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]);
106 obj->data.opaque = ed;
110 CRIT_ERR(obj, free_at_crash, "${pid_environ pid varname}");
114 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
119 int bytes_read, total_read = 0;
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");
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;
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) {
145 NORM_ERR(READERR, ((struct environ_data*) obj->data.opaque)->file);
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);
155 void scan_pid_environ_list_arg(struct text_object *obj, const char *arg, void* free_at_crash)
157 scan_pid_arg(obj, arg, free_at_crash, "environ");
160 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
165 int bytes_read, total_read = 0;
168 infofile = fopen(obj->data.s, "r");
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;
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);
185 snprintf(p, p_max_size, "%s", buf);
189 NORM_ERR(READERR, obj->data.s);
193 void scan_pid_exe_arg(struct text_object *obj, const char *arg, void* free_at_crash)
195 scan_pid_arg(obj, arg, free_at_crash, "exe");
198 void print_pid_readlink(struct text_object *obj, char *p, int p_max_size)
200 char buf[p_max_size];
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);
206 NORM_ERR(READERR, obj->data.s);
210 void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
211 print_pid_readlink(obj, p, p_max_size);
214 void scan_pid_stderr_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
215 scan_pid_arg(obj, arg, free_at_crash, "fd/2");
218 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
219 print_pid_readlink(obj, p, p_max_size);
222 void scan_pid_stdin_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
223 scan_pid_arg(obj, arg, free_at_crash, "fd/0");
226 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
227 print_pid_readlink(obj, p, p_max_size);
230 void scan_pid_stdout_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
231 scan_pid_arg(obj, arg, free_at_crash, "fd/1");
234 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
235 print_pid_readlink(obj, p, p_max_size);
238 void scan_pid_openfiles_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
239 scan_pid_arg(obj, arg, free_at_crash, "fd");
244 struct ll_string* next;
247 struct ll_string* addnode(struct ll_string* end, char* string) {
248 struct ll_string* current = malloc(sizeof(struct ll_string));
249 current->string = strdup(string);
250 current->next = NULL;
251 if(end != NULL) end->next = current;
255 void freelist(struct ll_string* front) {
258 if(front->next != NULL) {
259 freelist(front->next);
265 int inlist(struct ll_string* front, char* string) {
266 struct ll_string* current;
268 for(current = front; current != NULL; current = current->next) {
269 if(strcmp(current->string, string) == 0) {
276 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
278 struct dirent *entry;
279 char buf[p_max_size];
280 int length, totallength = 0;
281 struct ll_string* files_front = NULL;
282 struct ll_string* files_back = NULL;
284 dir = opendir(obj->data.s);
285 while ((entry = readdir(dir))) {
286 if(entry->d_name[0] != '.') {
287 snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
288 length = readlink(buf, buf, p_max_size);
290 if(inlist(files_front, buf) == 0) {
291 files_back = addnode(files_back, buf);
292 snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
293 totallength += length + strlen("; ");
295 if(files_front == NULL) {
296 files_front = files_back;
301 freelist(files_front);
302 p[totallength - strlen("; ")] = 0;