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 char* readfile(char* filename, int* total_read) {
60 file = fopen(filename, "r");
63 buf = realloc(buf, *total_read + READSIZE + 1);
64 bytes_read = fread(buf + *total_read, 1, READSIZE, file);
65 *total_read += bytes_read;
67 }while(bytes_read != 0);
70 NORM_ERR(READERR, filename);
75 void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
80 buf = readfile(obj->data.s, &bytes_read);
82 for(i = 0; i < bytes_read-1; i++) {
87 snprintf(p, p_max_size, "%s", buf);
92 void scan_pid_cwd_arg(struct text_object *obj, const char *arg, void* free_at_crash)
94 scan_pid_arg(obj, arg, free_at_crash, "cwd");
97 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
102 memset(buf, 0, p_max_size);
103 bytes_read = readlink(obj->data.s, buf, p_max_size);
104 if(bytes_read != -1) {
105 snprintf(p, p_max_size, "%s", buf);
107 NORM_ERR(READERR, obj->data.s);
111 void scan_pid_environ_arg(struct text_object *obj, const char *arg, void* free_at_crash)
115 struct environ_data* ed = malloc(sizeof(struct environ_data));
117 ed->var = malloc(strlen(arg));
118 if(sscanf(arg, "%d %s", &pid, ed->var) == 2) {
119 asprintf(&ed->file, PROCDIR "/%d/environ", pid);
120 for(i = 0; ed->var[i] != 0; i++) {
121 ed->var[i] = toupper(ed->var[i]);
123 obj->data.opaque = ed;
127 CRIT_ERR(obj, free_at_crash, "${pid_environ pid varname}");
131 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
134 char *searchstring = ((struct environ_data*) obj->data.opaque)->var;
135 int bytes_read, total_read = 0;
137 buf = readfile(((struct environ_data*) obj->data.opaque)->file, &total_read);
139 for(bytes_read = 0; bytes_read < total_read; bytes_read += strlen(buf + bytes_read) + 1) {
140 if(strncmp(buf + bytes_read, searchstring, strlen(searchstring)) == 0 && *(buf + bytes_read + strlen(searchstring)) == '=') {
141 snprintf(p, p_max_size, "%s", buf + bytes_read + strlen(searchstring) + 1);
151 void free_pid_environ(struct text_object *obj) {
152 free(((struct environ_data*) obj->data.opaque)->file);
153 free(((struct environ_data*) obj->data.opaque)->var);
154 free(obj->data.opaque);
157 void scan_pid_environ_list_arg(struct text_object *obj, const char *arg, void* free_at_crash)
159 scan_pid_arg(obj, arg, free_at_crash, "environ");
162 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
166 int bytes_read, total_read;
169 buf = readfile(obj->data.s, &total_read);
171 for(bytes_read = 0; bytes_read < total_read; buf[i-1] = ';') {
172 buf2 = strdup(buf+bytes_read);
173 bytes_read += strlen(buf2)+1;
174 sscanf(buf2, "%[^=]", buf+i);
179 snprintf(p, p_max_size, "%s", buf);
184 void scan_pid_exe_arg(struct text_object *obj, const char *arg, void* free_at_crash)
186 scan_pid_arg(obj, arg, free_at_crash, "exe");
189 void print_pid_readlink(struct text_object *obj, char *p, int p_max_size)
191 char buf[p_max_size];
193 memset(buf, 0, p_max_size);
194 if(readlink(obj->data.s, buf, p_max_size) >= 0) {
195 snprintf(p, p_max_size, "%s", buf);
197 NORM_ERR(READERR, obj->data.s);
201 void scan_pid_chroot_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
202 scan_pid_arg(obj, arg, free_at_crash, "root");
205 void print_pid_chroot(struct text_object *obj, char *p, int p_max_size) {
206 print_pid_readlink(obj, p, p_max_size);
209 void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
210 print_pid_readlink(obj, p, p_max_size);
213 void scan_pid_state_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
214 scan_pid_arg(obj, arg, free_at_crash, "status");
217 void print_pid_state(struct text_object *obj, char *p, int p_max_size) {
218 char *begin, *end, *buf = NULL;
221 buf = readfile(obj->data.s, &bytes_read);
223 begin = strstr(buf, STATE_ENTRY);
225 begin += strlen(STATE_ENTRY) + 3; // +3 will strip the char representing the short state and the space and '(' that follow
226 end = strchr(begin, '\n');
230 snprintf(p, p_max_size, "%s", begin);
232 NORM_ERR(STATENOTFOUND, obj->data.s);
238 void scan_pid_stderr_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
239 scan_pid_arg(obj, arg, free_at_crash, "fd/2");
242 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
243 print_pid_readlink(obj, p, p_max_size);
246 void scan_pid_stdin_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
247 scan_pid_arg(obj, arg, free_at_crash, "fd/0");
250 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
251 print_pid_readlink(obj, p, p_max_size);
254 void scan_pid_stdout_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
255 scan_pid_arg(obj, arg, free_at_crash, "fd/1");
258 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
259 print_pid_readlink(obj, p, p_max_size);
262 void scan_pid_openfiles_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
263 scan_pid_arg(obj, arg, free_at_crash, "fd");
268 struct ll_string* next;
271 struct ll_string* addnode(struct ll_string* end, char* string) {
272 struct ll_string* current = malloc(sizeof(struct ll_string));
273 current->string = strdup(string);
274 current->next = NULL;
275 if(end != NULL) end->next = current;
279 void freelist(struct ll_string* front) {
282 if(front->next != NULL) {
283 freelist(front->next);
289 int inlist(struct ll_string* front, char* string) {
290 struct ll_string* current;
292 for(current = front; current != NULL; current = current->next) {
293 if(strcmp(current->string, string) == 0) {
300 void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
302 struct dirent *entry;
303 char buf[p_max_size];
304 int length, totallength = 0;
305 struct ll_string* files_front = NULL;
306 struct ll_string* files_back = NULL;
308 dir = opendir(obj->data.s);
310 while ((entry = readdir(dir))) {
311 if(entry->d_name[0] != '.') {
312 snprintf(buf, p_max_size, "%s/%s", obj->data.s, entry->d_name);
313 length = readlink(buf, buf, p_max_size);
315 if(inlist(files_front, buf) == 0) {
316 files_back = addnode(files_back, buf);
317 snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
318 totallength += length + strlen("; ");
320 if(files_front == NULL) {
321 files_front = files_back;
326 freelist(files_front);
327 p[totallength - strlen("; ")] = 0;