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 print_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 print_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_state(struct text_object *obj, char *p, int p_max_size) {
245 char *begin, *end, *buf = NULL;
248 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
249 strcpy(obj->data.s, buf);
251 buf = readfile(obj->data.s, &bytes_read, 1);
253 begin = strstr(buf, STATE_ENTRY);
255 begin += strlen(STATE_ENTRY) + 3; // +3 will strip the char representing the short state and the space and '(' that follow
256 end = strchr(begin, '\n');
260 snprintf(p, p_max_size, "%s", begin);
262 NORM_ERR(STATENOTFOUND, obj->data.s);
268 void print_pid_state_short(struct text_object *obj, char *p, int p_max_size) {
269 char *begin, *buf = NULL;
272 asprintf(&buf, PROCDIR "/%s/status", obj->data.s);
273 strcpy(obj->data.s, buf);
275 buf = readfile(obj->data.s, &bytes_read, 1);
277 begin = strstr(buf, STATE_ENTRY);
279 snprintf(p, p_max_size, "%c", *begin);
281 NORM_ERR(STATENOTFOUND, obj->data.s);
287 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
290 asprintf(&buffer, PROCDIR "/%s/fd/2", obj->data.s);
291 print_pid_readlink(buffer, p, p_max_size);
295 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
298 asprintf(&buffer, PROCDIR "/%s/fd/0", obj->data.s);
299 print_pid_readlink(buffer, p, p_max_size);
303 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
306 asprintf(&buffer, PROCDIR "/%s/fd/1", obj->data.s);
307 print_pid_readlink(buffer, p, p_max_size);
311 void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
314 if(strlen(arg) > 0) {
315 obj->data.s = strdup(arg);
316 for(i = 0; obj->data.s[i] != 0; i++) {
317 while(obj->data.s[i] == ' ' && obj->data.s[i + 1] == ' ') {
318 memmove(obj->data.s + i, obj->data.s + i + 1, strlen(obj->data.s + i + 1) + 1);
321 if(obj->data.s[i - 1] == ' ') {
322 obj->data.s[i - 1] = 0;
325 CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}");
329 void print_cmdline_to_pid(struct text_object *obj, char *p, int p_max_size) {
331 struct dirent *entry;
332 char *filename, *buf;
335 dir = opendir(PROCDIR);
337 while ((entry = readdir(dir))) {
338 asprintf(&filename, PROCDIR "/%s/cmdline", entry->d_name);
339 buf = readfile(filename, &bytes_read, 0);
342 for(i = 0; i < bytes_read - 1; i++) {
343 if(buf[i] == 0) buf[i] = ' ';
345 if(strcmp(buf, obj->data.s) == 0) {
346 snprintf(p, p_max_size, "%s", entry->d_name);
356 NORM_ERR(READERR, PROCDIR);