root, oldver);
tvhinfo("config", "backup: running, output file %s", outfile);
- spawnv(argv[0], (void *)argv, 1, 1);
+ spawnv(argv[0], (void *)argv, NULL, 1, 1);
while ((code = spawn_reap(errtxt, sizeof(errtxt))) == -EAGAIN)
usleep(20000);
args[i] = s;
}
- spawnv(args[0], (void *)args, 1, 1);
+ spawnv(args[0], (void *)args, NULL, 1, 1);
htsstr_argsplit_free(args);
}
tvhlog(LOG_INFO, mod->id, "grab %s", mod->path);
/* Grab */
- outlen = spawn_and_give_stdout(mod->path, NULL, &rd, 1);
+ outlen = spawn_and_give_stdout(mod->path, NULL, &rd, NULL, 1);
if (outlen < 0)
goto error;
char *tmp, *tmp2 = NULL, *path;
/* Load data */
- if (spawn_and_give_stdout(XMLTV_FIND, NULL, &rd, 1) >= 0)
+ if (spawn_and_give_stdout(XMLTV_FIND, NULL, &rd, NULL, 1) >= 0)
outlen = file_readall(rd, &outbuf);
if (rd >= 0)
close(rd);
if (!(st.st_mode & S_IEXEC)) continue;
if (!S_ISREG(st.st_mode)) continue;
rd = -1;
- if (spawn_and_give_stdout(bin, argv, &rd, 1) >= 0 &&
+ if (spawn_and_give_stdout(bin, argv, &rd, NULL, 1) >= 0 &&
(outlen = file_readall(rd, &outbuf)) > 0) {
close(rd);
if (outbuf[outlen-1] == '\n') outbuf[outlen-1] = '\0';
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
+#include <signal.h>
#include <assert.h>
/*
{
char *argv[64], *f, *raw, *s, *p, *a;
int i = 1, rd;
+ pid_t pid;
if (strncmp(_raw, "pipe://", 7))
return -1;
}
argv[i] = NULL;
- if (spawn_and_give_stdout(raw, argv, &rd, 1)) {
+ if (spawn_and_give_stdout(raw, argv, &rd, &pid, 1)) {
tvherror("iptv", "Unable to start pipe '%s' (wrong executable?)", raw);
return -1;
}
fcntl(rd, F_SETFL, fcntl(rd, F_GETFL) | O_NONBLOCK);
im->mm_iptv_fd = rd;
+ im->im_data = (void *)(intptr_t)pid;
iptv_input_mux_started(im);
return 0;
( iptv_mux_t *im )
{
int rd = im->mm_iptv_fd;
+ pid_t pid = (intptr_t)im->im_data;
+ spawn_kill(pid, SIGKILL);
if (rd > 0)
close(rd);
im->mm_iptv_fd = -1;
int r, rd = im->mm_iptv_fd;
ssize_t res = 0;
char buf[64*1024];
+ pid_t pid;
- while (rd > 0 && res < 1024*1024) {
+ while (rd > 0 && res < sizeof(buf)) {
r = read(rd, buf, sizeof(buf));
if (r < 0) {
+ if (errno == EAGAIN)
+ break;
if (ERRNO_AGAIN(errno))
continue;
- break;
}
- if (!r) {
+ if (r <= 0) {
+ tvherror("iptv", "stdin pipe unexpectedly closed: %s",
+ r < 0 ? strerror(errno) : "No data");
close(rd);
+ pid = (intptr_t)im->im_data;
+ spawn_kill(pid, SIGKILL);
im->mm_iptv_fd = -1;
+ im->im_data = NULL;
break;
}
sbuf_append(&im->mm_iptv_buffer, buf, r);
while (spawn_reap(NULL, 0) != -EAGAIN) ;
}
+/**
+ * Kill the pid (only if waiting)
+ */
+int
+spawn_kill(pid_t pid, int sig)
+{
+ int r = -ESRCH;
+ spawn_t *s;
+
+ if (pid > 0) {
+ spawn_reaper();
+
+ pthread_mutex_lock(&spawn_mutex);
+ LIST_FOREACH(s, &spawns, link)
+ if(s->pid == pid)
+ break;
+ if (s) {
+ r = kill(pid, sig);
+ if (r < 0)
+ r = -errno;
+ }
+ pthread_mutex_unlock(&spawn_mutex);
+ }
+ return r;
+}
/**
* Enqueue a spawn on the pending spawn list
return s;
}
+
+
/**
* Execute the given program and return its standard output as file-descriptor (pipe).
*/
-
int
-spawn_and_give_stdout(const char *prog, char *argv[], int *rd, int redir_stderr)
+spawn_and_give_stdout(const char *prog, char *argv[], int *rd, pid_t *pid, int redir_stderr)
{
pid_t p;
int fd[2], f, maxfd;
close(fd[1]);
*rd = fd[0];
+ if (pid)
+ *pid = p;
return 0;
}
* The function will return the size of the buffer
*/
int
-spawnv(const char *prog, char *argv[], int redir_stdout, int redir_stderr)
+spawnv(const char *prog, char *argv[], pid_t *pid, int redir_stdout, int redir_stderr)
{
pid_t p, f, maxfd;
char bin[256];
spawn_enq(prog, p);
+ if (pid)
+ *pid = p;
+
return 0;
}
int find_exec ( const char *name, char *out, size_t len );
-int spawn_and_give_stdout(const char *prog, char *argv[], int *rd, int redir_stderr);
+int spawn_and_give_stdout(const char *prog, char *argv[], int *rd, pid_t *pid, int redir_stderr);
-int spawnv(const char *prog, char *argv[], int redir_stdout, int redir_stderr);
+int spawnv(const char *prog, char *argv[], pid_t *pid, int redir_stdout, int redir_stderr);
int spawn_reap(char *stxt, size_t stxtlen);
+int spawn_kill(pid_t pid, int sig);
+
void spawn_init(void);
void spawn_done(void);