/* $Id: plugin_exec.c 840 2007-09-09 12:17:42Z michael $ * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_exec.c $ * * plugin for external processes * * Copyright (C) 2004 Michael Reinelt * Copyright (C) 2004 The LCD4Linux Team * * based on the old 'exec' client which is * Copyright (C) 2001 Leopold Tötsch * * * 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 #include #include #include #include #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); } n92'>92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/* test_dvr.c - Test recording a TS from the dvr device.
 * usage: test_dvr PID [more PIDs...]
 *
 * Copyright (C) 2003 convergence GmbH
 * Johannes Stezenbach <js@convergence.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This program 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 Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

//hm, I haven't tested writing large files yet... maybe it works
#define _LARGEFILE64_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <inttypes.h>

#include <linux/dvb/dmx.h>

static unsigned long BUF_SIZE = 64 * 1024;
static unsigned long long total_bytes;

static void usage(void)
{
	fprintf(stderr, "usage: test_dvb file PID [more PIDs...]\n"
			"       record a partial TS stream consisting of TS packets\n"
			"       with the selected PIDs to the given file.\n"
			"       Use PID 0x2000 to record the full TS stream (if\n"
			"       the hardware supports it).\n"
			"       The demux and dvr devices used can be changed by setting\n"
			"       the environment variables DEMUX and DVR.\n"
			"       You can override the input buffer size by setting BUF_SIZE to\n"
			"       the number of bytes wanted.\n"
			"       Note: There is no output buffering, so writing to stdout is\n"
			"       not really supported, but you can try something like:\n"
			"       BUF_SIZE=188 ./test_dvr /dev/stdout 0 2>/dev/null | xxd\n"
			"       ./test_dvr /dev/stdout 0x100 0x110 2>/dev/null| xine stdin://mpeg2\n"
			"\n");
	exit(1);
}


static void process_data(int dvrfd, int tsfd)
{
	uint8_t buf[BUF_SIZE];
	int bytes, b2;

	bytes = read(dvrfd, buf, sizeof(buf));
	if (bytes < 0) {
		perror("read");
		if (errno == EOVERFLOW)
			return;
		fprintf(stderr, "exiting due to read() error\n");
		exit(1);
	}
	total_bytes += bytes;
	b2 = write(tsfd, buf, bytes);
	if (b2 == -1) {
		perror("write");
		exit(1);
	} else if (b2 < bytes)
		fprintf(stderr, "warning: read %d, but wrote only %d bytes\n", bytes, b2);
	else
		fprintf(stderr, "got %d bytes (%llu total)\n", bytes, total_bytes);
}

static int add_filter(unsigned int pid, const unsigned char* dmxdev)
{
	int fd;
	struct dmx_pes_filter_params f;

	fd = open(dmxdev, O_RDONLY);
	if (fd == -1) {
		perror("cannot open demux device");
		return 1;
	}

	memset(&f, 0, sizeof(f));
	f.pid = (uint16_t) pid;
	f.input = DMX_IN_FRONTEND;
	f.output = DMX_OUT_TS_TAP;
	f.pes_type = DMX_PES_OTHER;
	f.flags   = DMX_IMMEDIATE_START;

	if (ioctl(fd, DMX_SET_PES_FILTER, &f) == -1) {
		perror("DMX_SET_PES_FILTER");
		return 1;
	}
	fprintf(stderr, "set filter for PID 0x%04x on fd %d\n", pid, fd);

	//FIXME: don't leak the fd
	return 0;
}

int main(int argc, char *argv[])
{
	int dvrfd, tsfd;
	unsigned int pid;
	char *dmxdev = "/dev/dvb/adapter0/demux0";
	char *dvrdev = "/dev/dvb/adapter0/dvr0";
	int i;
	char *chkp;

	if (argc < 3)
		usage();

	if (getenv("DEMUX"))
		dmxdev = getenv("DEMUX");
	if (getenv("DVR"))
		dvrdev = getenv("DVR");

	fprintf(stderr, "using '%s' and '%s'\n"
		"writing to '%s'\n", dmxdev, dvrdev, argv[1]);
	tsfd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0664);
	if (tsfd == -1) {
		perror("cannot write output file");
		return 1;
	}

	dvrfd = open(dvrdev, O_RDONLY);
	if (dvrfd == -1) {
		perror("cannot open dvr device");
		return 1;
	}

	if (getenv("BUF_SIZE") && ((BUF_SIZE = strtoul(getenv("BUF_SIZE"), NULL, 0)) > 0))
		fprintf(stderr, "BUF_SIZE = %lu\n", BUF_SIZE);

	for (i = 2; i < argc; i++) {
		pid = strtoul(argv[i], &chkp, 0);
		if (pid > 0x2000 || chkp == argv[i])
			usage();
		fprintf(stderr, "adding filter for PID 0x%04x\n", pid);
		//FIXME: stop & close filters later...
		if (add_filter(pid, dmxdev) != 0)
			return 1;
	}

	for (;;) {
		process_data(dvrfd, tsfd);
	}

	close(dvrfd);
	return 0;
}