Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
spawn: introduce tvh_fopen() to close fds for spawned processes
  • Loading branch information
perexg committed Nov 17, 2014
1 parent a563d78 commit 7548fc5
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/dvr/dvr_cutpoints.c
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/filebundle.c
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/httpc.c
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/imagecache.c
Expand Up @@ -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) */
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions src/spawn.c
Expand Up @@ -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;
Expand All @@ -476,7 +479,10 @@ spawnv(const char *prog, char *argv[])
exit(1);
}

pthread_mutex_unlock(&fork_lock);

spawn_enq(prog, p);

return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/tvheadend.h
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/tvhlog.c
Expand Up @@ -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);
}
Expand Down
17 changes: 16 additions & 1 deletion src/wrappers.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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*);
Expand Down

0 comments on commit 7548fc5

Please sign in to comment.