1873ffd4c8abb18e06bdff768158a18f25dde926
[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
37 void scan_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash, const char *file)
38 {
39         pid_t pid;
40
41         if(sscanf(arg, "%d", &pid) == 1) {
42                 asprintf(&obj->data.s, PROCDIR "/%d/%s", pid, file);
43         } else {
44                 CRIT_ERR(obj, free_at_crash, "syntax error: ${pid_%s pid}", file);
45         }
46 }
47
48 void scan_pid_cmdline_arg(struct text_object *obj, const char *arg, void* free_at_crash)
49 {
50         scan_pid_arg(obj, arg, free_at_crash, "cmdline");
51 }
52
53 void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
54 {
55         char buf[p_max_size];
56         FILE* infofile;
57         int i, bytes_read;
58
59         infofile = fopen(obj->data.s, "r");
60         if(infofile) {
61                 bytes_read = fread(buf, 1, p_max_size, infofile);
62                 for(i = 0; i < bytes_read-1; i++) {
63                         if(buf[i] == 0) {
64                                 buf[i] = ' ';
65                         }
66                 }
67                 snprintf(p, p_max_size, "%s", buf);
68                 fclose(infofile);
69         } else {
70                 NORM_ERR(READERR, obj->data.s);
71         }
72 }
73
74 void scan_pid_cwd_arg(struct text_object *obj, const char *arg, void* free_at_crash)
75 {
76         scan_pid_arg(obj, arg, free_at_crash, "cwd");
77 }
78
79 void print_pid_cwd(struct text_object *obj, char *p, int p_max_size)
80 {
81         char buf[p_max_size];
82         int bytes_read;
83
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);
88         } else {
89                 NORM_ERR(READERR, obj->data.s);
90         }
91 }
92
93 void scan_pid_environ_arg(struct text_object *obj, const char *arg, void* free_at_crash)
94 {
95         pid_t pid;
96         int i;
97         struct environ_data* ed = malloc(sizeof(struct environ_data));
98
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]);
104                 }
105                 obj->data.opaque = ed;
106         } else {
107                 free(ed->var);
108                 free(ed);
109                 CRIT_ERR(obj, free_at_crash, "${pid_environ pid varname}");
110         }
111 }
112
113 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
114 {
115         char *buf = NULL;
116         char *searchstring;
117         FILE* infofile;
118         int bytes_read, total_read = 0;
119
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");
124         if(infofile) {
125                 do {
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;
129                         buf[total_read] = 0;
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) {
133                                 free(buf);
134                                 free(searchstring);
135                                 fclose(infofile);
136                                 return;
137                         }
138                 }
139                 p[0] = 0;
140                 free(buf);
141                 free(searchstring);
142                 fclose(infofile);
143         } else {
144                 NORM_ERR(READERR, ((struct environ_data*) obj->data.opaque)->file);
145         }
146 }
147
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);
152 }
153
154 void scan_pid_environ_list_arg(struct text_object *obj, const char *arg, void* free_at_crash)
155 {
156         scan_pid_arg(obj, arg, free_at_crash, "environ");
157 }
158
159 void print_pid_environ_list(struct text_object *obj, char *p, int p_max_size)
160 {
161         char *buf = NULL;
162         char *buf2;
163         FILE* infofile;
164         int bytes_read, total_read = 0;
165         int i = 0;
166
167         infofile = fopen(obj->data.s, "r");
168         if(infofile) {
169                 do {
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;
173                         buf[total_read] = 0;
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);
179                         free(buf2);
180                         i = strlen(buf) + 1;
181                         buf[i-1] = ';';
182                 }
183                 buf[i-1] = 0;
184                 snprintf(p, p_max_size, "%s", buf);
185                 free(buf);
186                 fclose(infofile);
187         } else {
188                 NORM_ERR(READERR, obj->data.s);
189         }
190 }
191
192 void scan_pid_exe_arg(struct text_object *obj, const char *arg, void* free_at_crash)
193 {
194         scan_pid_arg(obj, arg, free_at_crash, "exe");
195 }
196
197 void print_pid_readlink(struct text_object *obj, char *p, int p_max_size)
198 {
199         char buf[p_max_size];
200
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);
204         } else {
205                 NORM_ERR(READERR, obj->data.s);
206         }
207 }
208
209 void print_pid_exe(struct text_object *obj, char *p, int p_max_size) {
210         print_pid_readlink(obj, p, p_max_size);
211 }
212
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");
215 }
216
217 void print_pid_stderr(struct text_object *obj, char *p, int p_max_size) {
218         print_pid_readlink(obj, p, p_max_size);
219 }
220
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");
223 }
224
225 void print_pid_stdin(struct text_object *obj, char *p, int p_max_size) {
226         print_pid_readlink(obj, p, p_max_size);
227 }
228
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");
231 }
232
233 void print_pid_stdout(struct text_object *obj, char *p, int p_max_size) {
234         print_pid_readlink(obj, p, p_max_size);
235 }