diff --git a/src/dvr/dvr_cutpoints.c b/src/dvr/dvr_cutpoints.c index 8b061760..cf3ebba7 100644 --- a/src/dvr/dvr_cutpoints.c +++ b/src/dvr/dvr_cutpoints.c @@ -145,7 +145,7 @@ dvr_parse_file dvr_cutpoint_t *cp = NULL; float frate = 0.0; char line[DVR_MAX_CUTPOINT_LINE]; - FILE *file = fopen(path, "r"); + FILE *file = tvh_fopen(path, "r"); if (file == NULL) return -1; diff --git a/src/filebundle.c b/src/filebundle.c index df8d93bc..a69f0bb8 100644 --- a/src/filebundle.c +++ b/src/filebundle.c @@ -413,7 +413,7 @@ fb_file *fb_open2 } else { char path[512]; snprintf(path, sizeof(path), "%s/%s", dir->d.root, name); - FILE *fp = fopen(path, "rb"); + FILE *fp = tvh_fopen(path, "rb"); if (fp) { struct stat st; stat(path, &st); diff --git a/src/httpc.c b/src/httpc.c index 167924c4..ffb98416 100644 --- a/src/httpc.c +++ b/src/httpc.c @@ -1642,7 +1642,7 @@ http_client_testsuite_run( void ) path = getenv("TVHEADEND_HTTPC_TEST"); if (path == NULL) path = TVHEADEND_DATADIR "/support/httpc-test.txt"; - fp = fopen(path, "r"); + fp = tvh_fopen(path, "r"); if (fp == NULL) { tvhlog(LOG_NOTICE, "httpc", "Test: unable to open '%s': %s", path, strerror(errno)); return; diff --git a/src/imagecache.c b/src/imagecache.c index c6c1f6de..0462474d 100644 --- a/src/imagecache.c +++ b/src/imagecache.c @@ -156,7 +156,7 @@ imagecache_image_fetch ( imagecache_image_t *img ) if (hts_settings_makedirs(path)) goto error; snprintf(tmp, sizeof(tmp), "%s.tmp", path); - if (!(fp = fopen(tmp, "wb"))) + if (!(fp = tvh_fopen(tmp, "wb"))) goto error; /* Fetch (release lock, incase of delays) */ diff --git a/src/main.c b/src/main.c index 682901f5..b2eac04b 100644 --- a/src/main.c +++ b/src/main.c @@ -685,7 +685,7 @@ main(int argc, char **argv) htsp_init(opt_bindaddr); // bind to ports only if (opt_fork) - pidfile = fopen(opt_pidpath, "w+"); + pidfile = tvh_fopen(opt_pidpath, "w+"); /* Set priviledges */ if(opt_fork || opt_group || opt_user) { diff --git a/src/spawn.c b/src/spawn.c index e910f9cc..419fa459 100644 --- a/src/spawn.c +++ b/src/spawn.c @@ -457,9 +457,12 @@ spawnv(const char *prog, char *argv[]) if(!argv) argv = (void *)local_argv; if (!argv[0]) argv[0] = (char*)prog; + pthread_mutex_lock(&fork_lock); + p = fork(); if(p == -1) { + pthread_mutex_unlock(&fork_lock); tvherror("spawn", "Unable to fork() for \"%s\" -- %s", prog, strerror(errno)); return -1; @@ -476,7 +479,10 @@ spawnv(const char *prog, char *argv[]) exit(1); } + pthread_mutex_unlock(&fork_lock); + spawn_enq(prog, p); + return 0; } diff --git a/src/tvheadend.h b/src/tvheadend.h index e548341b..2859d61b 100644 --- a/src/tvheadend.h +++ b/src/tvheadend.h @@ -620,6 +620,8 @@ void tvh_pipe_close(th_pipe_t *pipe); int tvh_write(int fd, const void *buf, size_t len); +FILE *tvh_fopen(const char *filename, const char *mode); + void hexdump(const char *pfx, const uint8_t *data, int len); uint32_t tvh_crc32(const uint8_t *data, size_t datalen, uint32_t crc); diff --git a/src/tvhlog.c b/src/tvhlog.c index 21af4dc1..312b45f5 100644 --- a/src/tvhlog.c +++ b/src/tvhlog.c @@ -202,7 +202,7 @@ tvhlog_process if (options & TVHLOG_OPT_DBG_FILE || msg->severity < LOG_DEBUG) { const char *ltxt = logtxtmeta[msg->severity][0]; if (!*fp) - *fp = fopen(path, "a"); + *fp = tvh_fopen(path, "a"); if (*fp) fprintf(*fp, "%s [%7s]:%s\n", t, ltxt, msg->msg); } diff --git a/src/wrappers.c b/src/wrappers.c index 4bf3fa06..ddee69e3 100644 --- a/src/wrappers.c +++ b/src/wrappers.c @@ -28,7 +28,6 @@ tvh_open(const char *pathname, int flags, mode_t mode) return fd; } - int tvh_socket(int domain, int type, int protocol) { @@ -90,6 +89,22 @@ tvh_write(int fd, const void *buf, size_t len) return len ? 1 : 0; } +FILE * +tvh_fopen(const char *filename, const char *mode) +{ + FILE *f; + int fd; + pthread_mutex_lock(&fork_lock); + f = fopen(filename, mode); + if (f) { + fd = fileno(f); + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); + } + pthread_mutex_unlock(&fork_lock); + return f; +} + + struct thread_state { void *(*run)(void*);