spawn_reap: wait for specified pid, check correctly return codes from waitpid(), fixes #2766

This commit is contained in:
Jaroslav Kysela 2015-04-23 09:03:59 +02:00
parent 9d042c2418
commit 926e04eacc
3 changed files with 11 additions and 6 deletions

View file

@ -1178,6 +1178,7 @@ dobackup(const char *oldver)
const char *root = hts_settings_get_root();
char errtxt[128];
const char **arg;
pid_t pid;
int code;
tvhinfo("config", "backup: migrating config from %s (running %s)",
@ -1211,10 +1212,10 @@ dobackup(const char *oldver)
root, oldver);
tvhinfo("config", "backup: running, output file %s", outfile);
if (spawnv(argv[0], (void *)argv, NULL, 1, 1)) {
if (spawnv(argv[0], (void *)argv, &pid, 1, 1)) {
code = -ENOENT;
} else {
while ((code = spawn_reap(errtxt, sizeof(errtxt))) == -EAGAIN)
while ((code = spawn_reap(pid, errtxt, sizeof(errtxt))) == -EAGAIN)
usleep(20000);
}

View file

@ -230,13 +230,17 @@ find_exec ( const char *name, char *out, size_t len )
* Reap one child
*/
int
spawn_reap(char *stxt, size_t stxtlen)
spawn_reap(pid_t wpid, char *stxt, size_t stxtlen)
{
pid_t pid;
int status, res;
spawn_t *s;
pid = waitpid(-1, &status, WNOHANG);
pid = waitpid(wpid, &status, WNOHANG);
if(pid < 0 && ERRNO_AGAIN(errno))
return -EAGAIN;
if(pid < 0)
return -errno;
if(pid < 1)
return -EAGAIN;
@ -279,7 +283,7 @@ spawn_reap(char *stxt, size_t stxtlen)
static void
spawn_reaper(void)
{
while (spawn_reap(NULL, 0) != -EAGAIN) ;
while (spawn_reap(-1, NULL, 0) != -EAGAIN) ;
}
/**

View file

@ -34,7 +34,7 @@ int spawn_and_give_stdout(const char *prog, char *argv[], char *envp[],
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_reap(pid_t pid, char *stxt, size_t stxtlen);
int spawn_kill(pid_t pid, int sig);