aboutsummaryrefslogtreecommitdiffstats
path: root/ChangeLog (follow)
AgeCommit message (Collapse)AuthorFilesLines
2004-03-13[lcd4linux @ 2004-03-13 19:06:01 by reinelt]reinelt1-0/+88
ChangeLog and Status update; small glitch in plugin_seti fixed. git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@399 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2004-02-29[lcd4linux @ 2004-02-29 17:09:53 by reinelt]reinelt1-0/+211
ChangeLog updated Version number bumped to 0.10.0 git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@381 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2004-01-21[lcd4linux @ 2004-01-21 11:32:48 by reinelt]reinelt1-0/+81
changelog commited git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@331 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2004-01-15[lcd4linux @ 2004-01-15 04:32:14 by reinelt]reinelt1-0/+101
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@312 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2004-01-06[lcd4linux @ 2004-01-06 17:37:00 by reinelt]reinelt1-0/+20
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@289 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2004-01-02[lcd4linux @ 2004-01-02 14:18:54 by reinelt]reinelt1-0/+97
Changelog, TODO updated git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@284 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-10-08[lcd4linux @ 2003-10-08 14:21:10 by reinelt]reinelt1-0/+68
Changelog; small type in parport.c git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@263 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-09-19[lcd4linux @ 2003-09-19 08:40:32 by reinelt]reinelt1-0/+7
increased version number to 0.9.12 port locking is done as /var/lock/LCK..usb_tts_0 for /dev/usb/tts/0 git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@246 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-09-13[lcd4linux @ 2003-09-13 07:20:51 by reinelt]reinelt1-0/+20
Changelog git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@244 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-09-10[lcd4linux @ 2003-09-10 15:09:21 by reinelt]reinelt1-0/+28
ChangeLog git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@238 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-08-24[lcd4linux @ 2003-08-24 05:28:31 by reinelt]reinelt1-119/+156
ChangeLog git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@229 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-08-17[lcd4linux @ 2003-08-17 06:57:04 by reinelt]reinelt1-0/+19
complete rewrite of the Crystalfontz driver git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@219 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-08-14[lcd4linux @ 2003-08-14 03:48:54 by reinelt]reinelt1-0/+15
ChangeLog git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@214 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-08-08[lcd4linux @ 2003-08-08 23:11:34 by reinelt]reinelt1-0/+7
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@210 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-08-08[lcd4linux @ 2003-08-08 06:58:06 by reinelt]reinelt1-0/+5
improved forking git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@207 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2003-08-08[lcd4linux @ 2003-08-08 05:42:51 by reinelt]reinelt1-0/+927
uClibc compatibility issue; small glitch in dvb.c git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@206 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
2002-12-08[lcd4linux @ 2002-12-08 07:36:06 by reinelt]reinelt1-0/+0
autoconf/automake cleanup git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@175 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
.highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/* $Id$
 * $URL$
 *
 * plugin for external processes
 *
 * Copyright (C) 2004 Michael Reinelt <michael@reinelt.co.at>
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * based on the old 'exec' client which is
 * Copyright (C) 2001 Leopold T�tsch <lt@toetsch.at>
 *
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * LCD4Linux is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* 
 * exported functions:
 *
 * int plugin_init_exec (void)
 *  adds functions to start external pocesses
 *
 */


#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "debug.h"
#include "plugin.h"
#include "hash.h"
#include "cfg.h"
#include "thread.h"
#include "qprintf.h"


#define NUM_THREADS 16
#define SHM_SIZE 4096

typedef struct {
    int delay;
    int mutex;
    pid_t pid;
    int shmid;
    char *cmd;
    char *key;
    char *ret;
} EXEC_THREAD;

static EXEC_THREAD Thread[NUM_THREADS];
static int max_thread = -1;

static HASH EXEC;


/* x^0 + x^5 + x^12 */
#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;
}


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';
	}

	/* 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)
{
    thread_destroy(Thread[n].pid);

    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)
{
    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;
}


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;
    }

    /* 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)
{
    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)
{
    hash_create(&EXEC);
    AddFunction("exec", 2, my_exec);
    return 0;
}


void plugin_exit_exec(void)
{
    int i;

    for (i = 0; i <= max_thread; i++) {
	destroy_exec_thread(i);
    }

    hash_destroy(&EXEC);
}