1
/* $Id: plugin_exec.c 840 2007-09-09 12:17:42Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_exec.c $
4
* plugin for external processes
6
* Copyright (C) 2004 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* based on the old 'exec' client which is
10
* Copyright (C) 2001 Leopold T�tsch <lt@toetsch.at>
13
* This file is part of LCD4Linux.
15
* LCD4Linux is free software; you can redistribute it and/or modify
16
* it under the terms of the GNU General Public License as published by
17
* the Free Software Foundation; either version 2, or (at your option)
20
* LCD4Linux is distributed in the hope that it will be useful,
21
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
* GNU General Public License for more details.
25
* You should have received a copy of the GNU General Public License
26
* along with this program; if not, write to the Free Software
27
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34
* int plugin_init_exec (void)
35
* adds functions to start external pocesses
56
#define NUM_THREADS 16
69
static EXEC_THREAD Thread[NUM_THREADS];
70
static int max_thread = -1;
75
/* x^0 + x^5 + x^12 */
76
#define CRCPOLY 0x8408
78
static unsigned short CRC(const char *s)
88
for (i = 0; i < 8; i++)
89
crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY : 0);
95
static void exec_thread(void *data)
97
EXEC_THREAD *Thread = (EXEC_THREAD *) data;
99
char buffer[SHM_SIZE];
102
/* use a safe path */
103
putenv("PATH=/usr/local/bin:/usr/bin:/bin");
107
pipe = popen(Thread->cmd, "r");
109
error("exec error: could not run pipe '%s': %s", Thread->cmd, strerror(errno));
112
len = fread(buffer, 1, SHM_SIZE - 1, pipe);
114
error("exec error: could not read from pipe '%s': %s", Thread->cmd, strerror(errno));
120
/* force trailing zero */
123
/* remove trailing CR/LF */
124
while (len > 0 && (buffer[len - 1] == '\n' || buffer[len - 1] == '\r')) {
125
buffer[--len] = '\0';
128
/* lock shared memory */
129
mutex_lock(Thread->mutex);
131
strncpy(Thread->ret, buffer, SHM_SIZE);
132
/* unlock shared memory */
133
mutex_unlock(Thread->mutex);
134
usleep(Thread->delay);
139
static void destroy_exec_thread(const int n)
141
thread_destroy(Thread[n].pid);
143
if (Thread[n].mutex != 0)
144
mutex_destroy(Thread[n].mutex);
150
shm_destroy(Thread[n].shmid, Thread[n].ret);
156
Thread[n].cmd = NULL;
157
Thread[n].key = NULL;
158
Thread[n].ret = NULL;
162
static int create_exec_thread(const char *cmd, const char *key, const int delay)
166
if (max_thread >= NUM_THREADS) {
167
error("cannot create exec thread <%s>: thread buffer full!", cmd);
172
Thread[max_thread].delay = delay;
173
Thread[max_thread].mutex = mutex_create();
174
Thread[max_thread].pid = -1;
175
Thread[max_thread].cmd = strdup(cmd);
176
Thread[max_thread].key = strdup(key);
177
Thread[max_thread].ret = NULL;
179
/* create communication buffer */
180
Thread[max_thread].shmid = shm_create((void **) &Thread[max_thread].ret, SHM_SIZE);
183
if (Thread[max_thread].shmid < 0) {
184
error("cannot create exec thread <%s>: shared memory allocation failed!", cmd);
185
destroy_exec_thread(max_thread--);
190
qprintf(name, sizeof(name), "exec-%s", key);
191
Thread[max_thread].pid = thread_create(name, exec_thread, &Thread[max_thread]);
194
if (Thread[max_thread].pid < 0) {
195
error("cannot create exec thread <%s>: fork failed?!", cmd);
196
destroy_exec_thread(max_thread--);
204
static int do_exec(const char *cmd, const char *key, int delay)
208
age = hash_age(&EXEC, key);
211
hash_put(&EXEC, key, "");
212
/* first-time call: create thread */
214
error("exec(%s): delay %d is too short! using 10 msec", cmd, delay);
217
if (create_exec_thread(cmd, key, 1000 * delay)) {
223
/* reread every 10 msec only */
224
if (age > 0 && age <= 10)
228
for (i = 0; i <= max_thread; i++) {
229
if (strcmp(key, Thread[i].key) == 0) {
230
/* lock shared memory */
231
mutex_lock(Thread[i].mutex);
233
hash_put(&EXEC, key, Thread[i].ret);
234
/* unlock shared memory */
235
mutex_unlock(Thread[i].mutex);
240
error("internal error: could not find thread exec-%s", key);
244
static void my_exec(RESULT * result, RESULT * arg1, RESULT * arg2)
246
char *cmd, key[5], *val;
250
delay = (int) R2N(arg2);
252
qprintf(key, sizeof(key), "%x", CRC(cmd));
254
if (do_exec(cmd, key, delay) < 0) {
255
SetResult(&result, R_STRING, "");
259
val = hash_get(&EXEC, key, NULL);
263
SetResult(&result, R_STRING, val);
267
int plugin_init_exec(void)
270
AddFunction("exec", 2, my_exec);
275
void plugin_exit_exec(void)
279
for (i = 0; i <= max_thread; i++) {
280
destroy_exec_thread(i);