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/>.
37 void scan_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash, const char *file)
41 if(sscanf(arg, "%d", &pid) == 1) {
42 asprintf(&obj->data.s, PROCDIR "/%d/%s", pid, file);
44 CRIT_ERR(obj, free_at_crash, "syntax error: ${pid_%s pid}", file);
48 void scan_pid_cmdline_arg(struct text_object *obj, const char *arg, void* free_at_crash)
50 scan_pid_arg(obj, arg, free_at_crash, "cmdline");
53 void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
59 infofile = fopen(obj->data.s, "r");
61 bytes_read = fread(buf, 1, p_max_size, infofile);
62 for(i = 0; i < bytes_read-1; i++) {
67 snprintf(p, p_max_size, "%s", buf);
70 NORM_ERR(READERR, obj->data.s);
74 void scan_pid_cwd_arg(struct text_object *obj, const char *arg, void* free_at_crash)
76 scan_pid_arg(obj, arg, free_at_crash, "cwd");
79 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
84 memset(buf, 0, p_max_size);
85 bytes_read = readlink(obj->data.s, buf, p_max_size);
86 if(bytes_read != -1) {
87 snprintf(p, p_max_size, "%s", buf);
89 NORM_ERR(READERR, obj->data.s);
93 void scan_pid_environ_arg(struct text_object *obj, const char *arg, void* free_at_crash)
97 struct environ_data* ed = malloc(sizeof(struct environ_data));
99 ed->var = malloc(strlen(arg));
100 if(sscanf(arg, "%d %s", &pid, ed->var) == 2) {
101 asprintf(&ed->file, PROCDIR "/%d/environ", pid);
102 for(i = 0; ed->var[i] != 0; i++) {
103 ed->var[i] = toupper(ed->var[i]);
105 obj->data.opaque = ed;
109 CRIT_ERR(obj, free_at_crash, "${pid_environ pid varname}");
113 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
118 int bytes_read, total_read = 0;
120 searchstring = malloc(strlen(((struct environ_data*) obj->data.opaque)->var) + strlen("=%[\1-\255]") + 1);
121 strcpy(searchstring, ((struct environ_data*) obj->data.opaque)->var);
122 strcat(searchstring, "=%[\1-\255]");
123 infofile = fopen(((struct environ_data*) obj->data.opaque)->file, "r");
126 buf = realloc(buf, total_read + p_max_size + 1);
127 bytes_read = fread(buf + total_read, 1, p_max_size, infofile);
128 total_read += bytes_read;
130 }while(bytes_read != 0);
131 for(bytes_read = 0; bytes_read < total_read; bytes_read += strlen(buf + bytes_read) + 1) {
132 if(sscanf(buf + bytes_read, searchstring, p) == 1) {
144 NORM_ERR(READERR, ((struct environ_data*) obj->data.opaque)->file);
148 void free_pid_environ(struct text_object *obj) {
149 free(((struct environ_data*) obj->data.opaque)->file);
150 free(((struct environ_data*) obj->data.opaque)->var);
151 free(obj->data.opaque);
154 void scan_pid_environ_list_arg(struct text_object *obj, const char *arg, void* free_at_crash)
156 scan_pid_arg(obj, arg, free_at_crash, "environ");
159 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
164 int bytes_read, total_read = 0;
167 infofile = fopen(obj->data.s, "r");
170 buf = realloc(buf, total_read + p_max_size + 1);
171 bytes_read = fread(buf + total_read, 1, p_max_size, infofile);
172 total_read += bytes_read;
174 }while(bytes_read != 0);
175 while(bytes_read < total_read) {
176 buf2 = strdup(buf+bytes_read);
177 bytes_read += strlen(buf2)+1;
178 sscanf(buf2, "%[^=]", buf+i);
184 snprintf(p, p_max_size, "%s", buf);
188 NORM_ERR(READERR, obj->data.s);
192 void scan_pid_exe_arg(struct text_object *obj, const char *arg, void* free_at_crash)
194 scan_pid_arg(obj, arg, free_at_crash, "exe");
197 void print_pid_readlink(struct text_object *obj, char *p, int p_max_size)
199 char buf[p_max_size];
201 memset(buf, 0, p_max_size);
202 if(readlink(obj->data.s, buf, p_max_size) >= 0) {
203 snprintf(p, p_max_size, "%s", buf);
205 NORM_ERR(READERR, obj->data.s);
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_stderr_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
214 scan_pid_arg(obj, arg, free_at_crash, "fd/2");
217 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
218 print_pid_readlink(obj, p, p_max_size);
221 void scan_pid_stdin_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
222 scan_pid_arg(obj, arg, free_at_crash, "fd/0");
225 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
226 print_pid_readlink(obj, p, p_max_size);
229 void scan_pid_stdout_arg(struct text_object *obj, const char *arg, void* free_at_crash) {
230 scan_pid_arg(obj, arg, free_at_crash, "fd/1");
233 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
234 print_pid_readlink(obj, p, p_max_size);