12d215a318aa73c296f8240b178412206cc3eaac
[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         struct environ_data* ed = malloc(sizeof(struct environ_data));
97         ed->var = malloc(strlen(arg));
98
99         if(sscanf(arg, "%d %s", &pid, ed->var) == 2) {
100                 asprintf(&ed->file, PROCDIR "/%d/environ", pid);
101                 for(int i = 0; ed->var[i] != 0; i++) {
102                         ed->var[i] = toupper(ed->var[i]);
103                 }
104                 obj->data.opaque = ed;
105         } else {
106                 free(ed->var);
107                 free(ed);
108                 CRIT_ERR(obj, free_at_crash, "${pid_environ pid varname}");
109         }
110 }
111
112 void print_pid_environ(struct text_object *obj, char *p, int p_max_size)
113 {
114         char *buf = NULL;
115         char *searchstring;
116         FILE* infofile;
117         int bytes_read, total_read = 0;
118
119         searchstring = malloc(strlen(((struct environ_data*) obj->data.opaque)->var) + strlen("=%s") + 1);
120         strcpy(searchstring, ((struct environ_data*) obj->data.opaque)->var);
121         strcat(searchstring, "=%s");
122         infofile = fopen(((struct environ_data*) obj->data.opaque)->file, "r");
123         if(infofile) {
124                 do {
125                         buf = realloc(buf, total_read + p_max_size + 1);
126                         bytes_read = fread(buf + total_read, 1, p_max_size, infofile);
127                         total_read += bytes_read;
128                         buf[total_read] = 0;
129                 }while(bytes_read != 0);
130                 for(bytes_read = 0; bytes_read < total_read; bytes_read += strlen(buf + bytes_read) + 1) {
131                         if(sscanf(buf + bytes_read, searchstring, p) == 1) {
132                                 free(buf);
133                                 free(searchstring);
134                                 fclose(infofile);
135                                 return;
136                         }
137                 }
138                 snprintf(p, p_max_size, VARNOTFOUND);
139                 free(buf);
140                 free(searchstring);
141                 fclose(infofile);
142         } else {
143                 NORM_ERR(READERR, ((struct environ_data*) obj->data.opaque)->file);
144         }
145 }
146
147 void free_pid_environ(struct text_object *obj) {
148         free(((struct environ_data*) obj->data.opaque)->file);
149         free(((struct environ_data*) obj->data.opaque)->var);
150         free(obj->data.opaque);
151 }