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/>.
35 #include "text_object.h"
36 #include "timed_thread.h"
38 #include <sys/types.h>
47 timed_thread *p_timed_thread;
51 /* FIXME: this will probably not work, since the variable is being reused
52 * between different text objects. So when a process really hangs, it's PID
53 * will be overwritten at the next iteration. */
56 //our own implementation of popen, the difference : the value of 'childpid' will be filled with
57 //the pid of the running 'command'. This is useful if want to kill it when it hangs while reading
58 //or writing to it. We have to kill it because pclose will wait until the process dies by itself
59 static FILE* pid_popen(const char *command, const char *mode, pid_t *child) {
61 int parentend, childend;
63 //by running pipe after the strcmp's we make sure that we don't have to create a pipe
64 //and close the ends if mode is something illegal
65 if(strcmp(mode, "r") == 0) {
71 } else if(strcmp(mode, "w") == 0) {
85 } else if(*child > 0) {
87 waitpid(*child, NULL, 0);
89 //don't read from both stdin and pipe or write to both stdout and pipe
90 if(childend == ends[0]) {
95 dup(childend); //by dupping childend, the returned fd will have close-on-exec turned off
96 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
97 _exit(EXIT_FAILURE); //child should die here, (normally execl will take care of this but it can fail)
99 return fdopen(parentend, mode);
102 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
103 //string has to end with \0 and it's length should fit in a int
105 static void remove_deleted_chars(char *string){
107 while(string[i] != 0){
108 if(string[i] == BACKSPACE){
110 strcpy( &(string[i-1]), &(string[i+1]) );
112 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
117 static inline double get_barnum(char *buf)
129 if (sscanf(buf, "%lf", &barnum) == 0) {
130 NORM_ERR("reading exec value failed (perhaps it's not the "
134 if (barnum > 100.0 || barnum < 0.0) {
135 NORM_ERR("your exec value is not between 0 and 100, "
136 "therefore it will be ignored");
142 static inline void read_exec(const char *data, char *buf, const int size)
146 memset(buf, 0, size);
151 alarm(update_interval);
152 fp = pid_popen(data, "r", &childpid);
156 length = fread(buf, 1, size, fp);
159 if (length > 0 && buf[length - 1] == '\n') {
160 buf[length - 1] = '\0';
168 static void *threaded_exec(void *) __attribute__((noreturn));
170 static void *threaded_exec(void *arg)
173 struct text_object *obj = arg;
174 struct execi_data *ed = obj->data.opaque;
177 buff = malloc(text_buffer_size);
178 read_exec(ed->cmd, buff, text_buffer_size);
186 timed_thread_lock(ed->p_timed_thread);
190 timed_thread_unlock(ed->p_timed_thread);
191 if (timed_thread_test(ed->p_timed_thread, 0)) {
192 timed_thread_exit(ed->p_timed_thread);
198 /* check the execi fields and return true if the given interval has passed */
199 static int time_to_update(struct execi_data *ed)
203 if (current_update_time - ed->last_update >= ed->interval)
208 void scan_exec_arg(struct text_object *obj, const char *arg)
210 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
213 void scan_pre_exec_arg(struct text_object *obj, const char *arg)
217 obj->type = OBJ_text;
218 read_exec(arg, buf, sizeof(buf));
219 obj->data.s = strndup(buf, text_buffer_size);
222 void scan_execi_arg(struct text_object *obj, const char *arg)
224 struct execi_data *ed;
227 ed = malloc(sizeof(struct execi_data));
228 memset(ed, 0, sizeof(struct execi_data));
230 if (sscanf(arg, "%f %n", &ed->interval, &n) <= 0) {
231 NORM_ERR("${execi* <interval> command}");
235 ed->cmd = strndup(arg + n, text_buffer_size);
236 obj->data.opaque = ed;
239 void scan_execgraph_arg(struct text_object *obj, const char *arg)
241 struct execi_data *ed;
244 ed = malloc(sizeof(struct execi_data));
245 memset(ed, 0, sizeof(struct execi_data));
247 buf = scan_graph(obj, arg);
249 NORM_ERR("missing command argument to execgraph object");
253 obj->data.opaque = ed;
256 void print_exec(struct text_object *obj, char *p, int p_max_size)
258 read_exec(obj->data.s, p, p_max_size);
259 remove_deleted_chars(p);
262 void print_execp(struct text_object *obj, char *p, int p_max_size)
264 struct information *tmp_info;
265 struct text_object subroot;
267 read_exec(obj->data.s, p, p_max_size);
269 tmp_info = malloc(sizeof(struct information));
270 memcpy(tmp_info, &info, sizeof(struct information));
271 parse_conky_vars(&subroot, p, p, tmp_info);
273 free_text_objects(&subroot, 1);
277 void print_execi(struct text_object *obj, char *p, int p_max_size)
279 struct execi_data *ed = obj->data.opaque;
284 if (time_to_update(ed)) {
286 ed->buffer = malloc(text_buffer_size);
287 read_exec(ed->cmd, ed->buffer, text_buffer_size);
288 ed->last_update = current_update_time;
290 snprintf(p, p_max_size, "%s", ed->buffer);
293 void print_execpi(struct text_object *obj, char *p)
295 struct execi_data *ed = obj->data.opaque;
296 struct text_object subroot;
297 struct information *tmp_info;
302 tmp_info = malloc(sizeof(struct information));
303 memcpy(tmp_info, &info, sizeof(struct information));
305 if (!time_to_update(ed)) {
306 parse_conky_vars(&subroot, ed->buffer, p, tmp_info);
310 FILE *fp = pid_popen(ed->cmd, "r", &childpid);
313 ed->buffer = malloc(text_buffer_size);
315 length = fread(ed->buffer, 1, text_buffer_size, fp);
319 output[length] = '\0';
320 if (length > 0 && output[length - 1] == '\n') {
321 output[length - 1] = '\0';
324 parse_conky_vars(&subroot, ed->buffer, p, tmp_info);
325 ed->last_update = current_update_time;
327 free_text_objects(&subroot, 1);
331 void print_texeci(struct text_object *obj, char *p, int p_max_size)
333 struct execi_data *ed = obj->data.opaque;
338 if (!ed->p_timed_thread) {
339 ed->p_timed_thread = timed_thread_create(&threaded_exec,
340 (void *) obj, ed->interval * 1000000);
341 if (!ed->p_timed_thread) {
342 NORM_ERR("Error creating texeci timed thread");
345 * note that we don't register this thread with the
346 * timed_thread list, because we destroy it manually
348 if (timed_thread_run(ed->p_timed_thread)) {
349 NORM_ERR("Error running texeci timed thread");
352 timed_thread_lock(ed->p_timed_thread);
353 snprintf(p, p_max_size, "%s", ed->buffer);
354 timed_thread_unlock(ed->p_timed_thread);
359 void print_execgauge(struct text_object *obj, char *p, int p_max_size)
363 read_exec(obj->data.s, p, p_max_size);
364 barnum = get_barnum(p); /*using the same function*/
368 new_gauge(obj, p, round_to_int(barnum * 255.0));
372 void print_execgraph(struct text_object *obj, char *p, int p_max_size)
375 struct execi_data *ed = obj->data.opaque;
380 read_exec(ed->cmd, p, p_max_size);
381 barnum = get_barnum(p);
384 new_graph(obj, p, round_to_int(barnum));
388 void print_execigraph(struct text_object *obj, char *p, int p_max_size)
390 struct execi_data *ed = obj->data.opaque;
395 if (time_to_update(ed)) {
398 read_exec(ed->cmd, p, p_max_size);
399 barnum = get_barnum(p);
404 ed->last_update = current_update_time;
406 new_graph(obj, p, (int) (ed->barnum));
409 void print_execigauge(struct text_object *obj, char *p, int p_max_size)
411 struct execi_data *ed = obj->data.opaque;
416 if (time_to_update(ed)) {
419 read_exec(ed->cmd, p, p_max_size);
420 barnum = get_barnum(p);
423 ed->barnum = 255 * barnum / 100.0;
425 ed->last_update = current_update_time;
427 new_gauge(obj, p, round_to_int(ed->barnum));
431 void print_execbar(struct text_object *obj, char *p, int p_max_size)
434 read_exec(obj->data.s, p, p_max_size);
435 barnum = get_barnum(p);
439 if(output_methods & TO_X) {
441 new_bar(obj, p, round_to_int(barnum * 255.0));
444 new_bar_in_shell(obj, p, p_max_size, barnum);
448 void print_execibar(struct text_object *obj, char *p, int p_max_size)
450 struct execi_data *ed = obj->data.opaque;
456 if (time_to_update(ed)) {
457 read_exec(ed->cmd, p, p_max_size);
458 barnum = get_barnum(p);
463 ed->last_update = current_update_time;
466 if(output_methods & TO_X) {
467 new_bar(obj, p, round_to_int(ed->barnum * 2.55));
470 new_bar_in_shell(obj, p, p_max_size, round_to_int(ed->barnum));
473 void free_exec(struct text_object *obj)
481 void free_execi(struct text_object *obj)
483 struct execi_data *ed = obj->data.opaque;
488 if (ed->p_timed_thread)
489 timed_thread_destroy(ed->p_timed_thread, &ed->p_timed_thread);
494 free(obj->data.opaque);
495 obj->data.opaque = NULL;