spawn: introduce tvh_fopen() to close fds for spawned processes

This commit is contained in:
Jaroslav Kysela 2014-11-17 14:22:40 +01:00
parent a563d786e9
commit 7548fc5c3e
9 changed files with 30 additions and 7 deletions

View file

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

View file

@ -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);

View file

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

View file

@ -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) */

View file

@ -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) {

View file

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

View file

@ -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);

View file

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

View file

@ -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*);