aboutsummaryrefslogtreecommitdiffstats
path: root/plugin_exec.c
diff options
context:
space:
mode:
authorreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2005-05-08 04:32:45 +0000
committerreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2005-05-08 04:32:45 +0000
commitdbf5d92605a9db10e3e3cb154d03a515c5d5c5af (patch)
tree501d5dda3ce90fe924b893e487d9ef9f0683382b /plugin_exec.c
parenta1c471e9fb7cadb68bb182ab2e9715a0aa092386 (diff)
downloadlcd4linux-dbf5d92605a9db10e3e3cb154d03a515c5d5c5af.tar.gz
[lcd4linux @ 2005-05-08 04:32:43 by reinelt]
CodingStyle added and applied git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@547 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
Diffstat (limited to 'plugin_exec.c')
-rw-r--r--plugin_exec.c361
1 files changed, 185 insertions, 176 deletions
diff --git a/plugin_exec.c b/plugin_exec.c
index 8b4af36..f9576e2 100644
--- a/plugin_exec.c
+++ b/plugin_exec.c
@@ -1,4 +1,4 @@
-/* $Id: plugin_exec.c,v 1.8 2005/01/18 06:30:23 reinelt Exp $
+/* $Id: plugin_exec.c,v 1.9 2005/05/08 04:32:44 reinelt Exp $
*
* plugin for external processes
*
@@ -27,6 +27,9 @@
*
*
* $Log: plugin_exec.c,v $
+ * Revision 1.9 2005/05/08 04:32:44 reinelt
+ * CodingStyle added and applied
+ *
* Revision 1.8 2005/01/18 06:30:23 reinelt
* added (C) to all copyright statements
*
@@ -90,13 +93,13 @@
#define SHM_SIZE 4096
typedef struct {
- int delay;
- int mutex;
- pid_t pid;
- int shmid;
- char *cmd;
- char *key;
- char *ret;
+ int delay;
+ int mutex;
+ pid_t pid;
+ int shmid;
+ char *cmd;
+ char *key;
+ char *ret;
} EXEC_THREAD;
static EXEC_THREAD Thread[NUM_THREADS];
@@ -106,204 +109,210 @@ static HASH EXEC;
/* x^0 + x^5 + x^12 */
-#define CRCPOLY 0x8408
-
-static unsigned short CRC (const char *s)
+#define CRCPOLY 0x8408
+
+static unsigned short CRC(const char *s)
{
- int i;
- unsigned short crc;
-
- /* seed value */
- crc = 0xffff;
-
- while (*s != '\0') {
- crc ^= *s++;
- for (i = 0; i < 8; i++)
- crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY : 0);
- }
- return crc;
+ int i;
+ unsigned short crc;
+
+ /* seed value */
+ crc = 0xffff;
+
+ while (*s != '\0') {
+ crc ^= *s++;
+ for (i = 0; i < 8; i++)
+ crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY : 0);
+ }
+ return crc;
}
-static void exec_thread (void *data)
+static void exec_thread(void *data)
{
- EXEC_THREAD *Thread = (EXEC_THREAD*)data;
- FILE *pipe;
- char buffer[SHM_SIZE];
- int len;
-
- /* use a safe path */
- putenv ("PATH=/usr/local/bin:/usr/bin:/bin");
-
- /* forever... */
- while (1) {
- pipe = popen(Thread->cmd, "r");
- if (pipe == NULL) {
- error("exec error: could not run pipe '%s': %s", Thread->cmd, strerror(errno));
- len = 0;
- } else {
- len = fread(buffer, 1, SHM_SIZE-1, pipe);
- if (len <= 0) {
- error("exec error: could not read from pipe '%s': %s", Thread->cmd, strerror(errno));
- len = 0;
- }
- pclose(pipe);
- }
-
- /* force trailing zero */
- buffer[len] = '\0';
-
- /* remove trailing CR/LF */
- while (len>0 && (buffer[len-1]=='\n' || buffer[len-1]=='\r')) {
- buffer[--len]='\0';
+ EXEC_THREAD *Thread = (EXEC_THREAD *) data;
+ FILE *pipe;
+ char buffer[SHM_SIZE];
+ int len;
+
+ /* use a safe path */
+ putenv("PATH=/usr/local/bin:/usr/bin:/bin");
+
+ /* forever... */
+ while (1) {
+ pipe = popen(Thread->cmd, "r");
+ if (pipe == NULL) {
+ error("exec error: could not run pipe '%s': %s", Thread->cmd, strerror(errno));
+ len = 0;
+ } else {
+ len = fread(buffer, 1, SHM_SIZE - 1, pipe);
+ if (len <= 0) {
+ error("exec error: could not read from pipe '%s': %s", Thread->cmd, strerror(errno));
+ len = 0;
+ }
+ pclose(pipe);
+ }
+
+ /* force trailing zero */
+ buffer[len] = '\0';
+
+ /* remove trailing CR/LF */
+ while (len > 0 && (buffer[len - 1] == '\n' || buffer[len - 1] == '\r')) {
+ buffer[--len] = '\0';
+ }
+
+ /* lock shared memory */
+ mutex_lock(Thread->mutex);
+ /* write data */
+ strncpy(Thread->ret, buffer, SHM_SIZE);
+ /* unlock shared memory */
+ mutex_unlock(Thread->mutex);
+ usleep(Thread->delay);
}
-
- /* lock shared memory */
- mutex_lock(Thread->mutex);
- /* write data */
- strncpy(Thread->ret, buffer, SHM_SIZE);
- /* unlock shared memory */
- mutex_unlock(Thread->mutex);
- usleep (Thread->delay);
- }
}
-static void destroy_exec_thread (const int n)
+static void destroy_exec_thread(const int n)
{
- if (Thread[n].mutex != 0) mutex_destroy(Thread[n].mutex);
- if (Thread[n].cmd) free (Thread[n].cmd);
- if (Thread[n].key) free (Thread[n].key);
- if (Thread[n].ret) shm_destroy (Thread[n].shmid, Thread[n].ret);
-
- Thread[n].delay = 0;
- Thread[n].mutex = 0;
- Thread[n].pid = 0;
- Thread[n].shmid = 0;
- Thread[n].cmd = NULL;
- Thread[n].key = NULL;
- Thread[n].ret = NULL;
+ if (Thread[n].mutex != 0)
+ mutex_destroy(Thread[n].mutex);
+ if (Thread[n].cmd)
+ free(Thread[n].cmd);
+ if (Thread[n].key)
+ free(Thread[n].key);
+ if (Thread[n].ret)
+ shm_destroy(Thread[n].shmid, Thread[n].ret);
+
+ Thread[n].delay = 0;
+ Thread[n].mutex = 0;
+ Thread[n].pid = 0;
+ Thread[n].shmid = 0;
+ Thread[n].cmd = NULL;
+ Thread[n].key = NULL;
+ Thread[n].ret = NULL;
}
-static int create_exec_thread (const char *cmd, const char *key, const int delay)
+static int create_exec_thread(const char *cmd, const char *key, const int delay)
{
- char name[10];
+ char name[10];
- if (max_thread >= NUM_THREADS) {
- error ("cannot create exec thread <%s>: thread buffer full!", cmd);
- return -1;
- }
-
- max_thread++;
- Thread[max_thread].delay = delay;
- Thread[max_thread].mutex = mutex_create();
- Thread[max_thread].pid = -1;
- Thread[max_thread].cmd = strdup(cmd);
- Thread[max_thread].key = strdup(key);
- Thread[max_thread].ret = NULL;
-
- /* create communication buffer */
- Thread[max_thread].shmid = shm_create ((void**)&Thread[max_thread].ret, SHM_SIZE);
-
- /* catch error */
- if (Thread[max_thread].shmid < 0) {
- error ("cannot create exec thread <%s>: shared memory allocation failed!", cmd);
- destroy_exec_thread (max_thread--);
- return -1;
- }
-
- /* create thread */
- qprintf(name, sizeof(name), "exec-%s", key);
- Thread[max_thread].pid = thread_create (name, exec_thread, &Thread[max_thread]);
-
- /* catch error */
- if (Thread[max_thread].pid < 0) {
- error ("cannot create exec thread <%s>: fork failed?!", cmd);
- destroy_exec_thread (max_thread--);
- return -1;
- }
-
- return 0;
-}
+ if (max_thread >= NUM_THREADS) {
+ error("cannot create exec thread <%s>: thread buffer full!", cmd);
+ return -1;
+ }
+ max_thread++;
+ Thread[max_thread].delay = delay;
+ Thread[max_thread].mutex = mutex_create();
+ Thread[max_thread].pid = -1;
+ Thread[max_thread].cmd = strdup(cmd);
+ Thread[max_thread].key = strdup(key);
+ Thread[max_thread].ret = NULL;
-static int do_exec (const char *cmd, const char *key, int delay)
-{
- int i, age;
-
- age = hash_age(&EXEC, key);
-
- if (age < 0) {
- hash_put (&EXEC, key, "");
- /* first-time call: create thread */
- if (delay < 10) {
- error ("exec(%s): delay %d is too short! using 10 msec", cmd, delay);
- delay = 10;
+ /* create communication buffer */
+ Thread[max_thread].shmid = shm_create((void **) &Thread[max_thread].ret, SHM_SIZE);
+
+ /* catch error */
+ if (Thread[max_thread].shmid < 0) {
+ error("cannot create exec thread <%s>: shared memory allocation failed!", cmd);
+ destroy_exec_thread(max_thread--);
+ return -1;
}
- if (create_exec_thread (cmd, key, 1000*delay)) {
- return -1;
+
+ /* create thread */
+ qprintf(name, sizeof(name), "exec-%s", key);
+ Thread[max_thread].pid = thread_create(name, exec_thread, &Thread[max_thread]);
+
+ /* catch error */
+ if (Thread[max_thread].pid < 0) {
+ error("cannot create exec thread <%s>: fork failed?!", cmd);
+ destroy_exec_thread(max_thread--);
+ return -1;
}
+
return 0;
- }
-
- /* reread every 10 msec only */
- if (age > 0 && age <= 10) return 0;
-
- /* find thread */
- for (i = 0; i <= max_thread; i++) {
- if (strcmp(key, Thread[i].key) == 0) {
- /* lock shared memory */
- mutex_lock(Thread[i].mutex);
- /* copy data */
- hash_put (&EXEC, key, Thread[i].ret);
- /* unlock shared memory */
- mutex_unlock(Thread[i].mutex);
- return 0;
+}
+
+
+static int do_exec(const char *cmd, const char *key, int delay)
+{
+ int i, age;
+
+ age = hash_age(&EXEC, key);
+
+ if (age < 0) {
+ hash_put(&EXEC, key, "");
+ /* first-time call: create thread */
+ if (delay < 10) {
+ error("exec(%s): delay %d is too short! using 10 msec", cmd, delay);
+ delay = 10;
+ }
+ if (create_exec_thread(cmd, key, 1000 * delay)) {
+ return -1;
+ }
+ return 0;
}
- }
-
- error ("internal error: could not find thread exec-%s", key);
- return -1;
+
+ /* reread every 10 msec only */
+ if (age > 0 && age <= 10)
+ return 0;
+
+ /* find thread */
+ for (i = 0; i <= max_thread; i++) {
+ if (strcmp(key, Thread[i].key) == 0) {
+ /* lock shared memory */
+ mutex_lock(Thread[i].mutex);
+ /* copy data */
+ hash_put(&EXEC, key, Thread[i].ret);
+ /* unlock shared memory */
+ mutex_unlock(Thread[i].mutex);
+ return 0;
+ }
+ }
+
+ error("internal error: could not find thread exec-%s", key);
+ return -1;
}
-static void my_exec (RESULT *result, RESULT *arg1, RESULT *arg2)
+static void my_exec(RESULT * result, RESULT * arg1, RESULT * arg2)
{
- char *cmd, key[5], *val;
- int delay;
-
- cmd = R2S(arg1);
- delay = (int)R2N(arg2);
-
- qprintf (key, sizeof(key), "%x", CRC(cmd));
-
- if (do_exec(cmd, key, delay) < 0) {
- SetResult(&result, R_STRING, "");
- return;
- }
-
- val = hash_get(&EXEC, key, NULL);
- if (val == NULL) val = "";
-
- SetResult(&result, R_STRING, val);
+ char *cmd, key[5], *val;
+ int delay;
+
+ cmd = R2S(arg1);
+ delay = (int) R2N(arg2);
+
+ qprintf(key, sizeof(key), "%x", CRC(cmd));
+
+ if (do_exec(cmd, key, delay) < 0) {
+ SetResult(&result, R_STRING, "");
+ return;
+ }
+
+ val = hash_get(&EXEC, key, NULL);
+ if (val == NULL)
+ val = "";
+
+ SetResult(&result, R_STRING, val);
}
-int plugin_init_exec (void)
+int plugin_init_exec(void)
{
- hash_create(&EXEC);
- AddFunction ("exec", 2, my_exec);
- return 0;
+ hash_create(&EXEC);
+ AddFunction("exec", 2, my_exec);
+ return 0;
}
-void plugin_exit_exec(void)
+void plugin_exit_exec(void)
{
- int i;
-
- for (i=0; i<=max_thread; i++) {
- destroy_exec_thread(i);
- }
-
- hash_destroy(&EXEC);
+ int i;
+
+ for (i = 0; i <= max_thread; i++) {
+ destroy_exec_thread(i);
+ }
+
+ hash_destroy(&EXEC);
}