Skip to content

Commit e96c86e

Browse files
jeffhostetlerdscho
authored andcommitted
run-command: create start_bg_command
Create a variation of `run_command()` and `start_command()` to launch a command into the background and optionally wait for it to become "ready" before returning. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent e3ac087 commit e96c86e

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

run-command.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,3 +1903,132 @@ void prepare_other_repo_env(struct strvec *env_array, const char *new_git_dir)
19031903
}
19041904
strvec_pushf(env_array, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir);
19051905
}
1906+
1907+
enum start_bg_result start_bg_command(struct child_process *cmd,
1908+
start_bg_wait_cb *wait_cb,
1909+
void *cb_data,
1910+
unsigned int timeout_sec)
1911+
{
1912+
enum start_bg_result sbgr = SBGR_ERROR;
1913+
int ret;
1914+
int wait_status;
1915+
pid_t pid_seen;
1916+
time_t time_limit;
1917+
1918+
/*
1919+
* We do not allow clean-on-exit because the child process
1920+
* should persist in the background and possibly/probably
1921+
* after this process exits. So we don't want to kill the
1922+
* child during our atexit routine.
1923+
*/
1924+
if (cmd->clean_on_exit)
1925+
BUG("start_bg_command() does not allow non-zero clean_on_exit");
1926+
1927+
if (!cmd->trace2_child_class)
1928+
cmd->trace2_child_class = "background";
1929+
1930+
ret = start_command(cmd);
1931+
if (ret) {
1932+
/*
1933+
* We assume that if `start_command()` fails, we
1934+
* either get a complete `trace2_child_start() /
1935+
* trace2_child_exit()` pair or it fails before the
1936+
* `trace2_child_start()` is emitted, so we do not
1937+
* need to worry about it here.
1938+
*
1939+
* We also assume that `start_command()` does not add
1940+
* us to the cleanup list. And that it calls
1941+
* calls `child_process_clear()`.
1942+
*/
1943+
sbgr = SBGR_ERROR;
1944+
goto done;
1945+
}
1946+
1947+
time(&time_limit);
1948+
time_limit += timeout_sec;
1949+
1950+
wait:
1951+
pid_seen = waitpid(cmd->pid, &wait_status, WNOHANG);
1952+
1953+
if (!pid_seen) {
1954+
/*
1955+
* The child is currently running. Ask the callback
1956+
* if the child is ready to do work or whether we
1957+
* should keep waiting for it to boot up.
1958+
*/
1959+
ret = (*wait_cb)(cmd, cb_data);
1960+
if (!ret) {
1961+
/*
1962+
* The child is running and "ready".
1963+
*/
1964+
trace2_child_ready(cmd, "ready");
1965+
sbgr = SBGR_READY;
1966+
goto done;
1967+
} else if (ret > 0) {
1968+
/*
1969+
* The callback said to give it more time to boot up
1970+
* (subject to our timeout limit).
1971+
*/
1972+
time_t now;
1973+
1974+
time(&now);
1975+
if (now < time_limit)
1976+
goto wait;
1977+
1978+
/*
1979+
* Our timeout has expired. We don't try to
1980+
* kill the child, but rather let it continue
1981+
* (hopefully) trying to startup.
1982+
*/
1983+
trace2_child_ready(cmd, "timeout");
1984+
sbgr = SBGR_TIMEOUT;
1985+
goto done;
1986+
} else {
1987+
/*
1988+
* The cb gave up on this child. It is still running,
1989+
* but our cb got an error trying to probe it.
1990+
*/
1991+
trace2_child_ready(cmd, "error");
1992+
sbgr = SBGR_CB_ERROR;
1993+
goto done;
1994+
}
1995+
}
1996+
1997+
else if (pid_seen == cmd->pid) {
1998+
int child_code = -1;
1999+
2000+
/*
2001+
* The child started, but exited or was terminated
2002+
* before becoming "ready".
2003+
*
2004+
* We try to match the behavior of `wait_or_whine()`
2005+
* WRT the handling of WIFSIGNALED() and WIFEXITED()
2006+
* and convert the child's status to a return code for
2007+
* tracing purposes and emit the `trace2_child_exit()`
2008+
* event.
2009+
*
2010+
* We do not want the wait_or_whine() error message
2011+
* because we will be called by client-side library
2012+
* routines.
2013+
*/
2014+
if (WIFEXITED(wait_status))
2015+
child_code = WEXITSTATUS(wait_status);
2016+
else if (WIFSIGNALED(wait_status))
2017+
child_code = WTERMSIG(wait_status) + 128;
2018+
trace2_child_exit(cmd, child_code);
2019+
2020+
sbgr = SBGR_DIED;
2021+
goto done;
2022+
}
2023+
2024+
else if (pid_seen < 0 && errno == EINTR)
2025+
goto wait;
2026+
2027+
trace2_child_exit(cmd, -1);
2028+
sbgr = SBGR_ERROR;
2029+
2030+
done:
2031+
child_process_clear(cmd);
2032+
invalidate_lstat_cache();
2033+
return sbgr;
2034+
}

run-command.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,4 +496,61 @@ int run_processes_parallel_tr2(int n, get_next_task_fn, start_failure_fn,
496496
*/
497497
void prepare_other_repo_env(struct strvec *env_array, const char *new_git_dir);
498498

499+
/**
500+
* Possible return values for start_bg_command().
501+
*/
502+
enum start_bg_result {
503+
/* child process is "ready" */
504+
SBGR_READY = 0,
505+
506+
/* child process could not be started */
507+
SBGR_ERROR,
508+
509+
/* callback error when testing for "ready" */
510+
SBGR_CB_ERROR,
511+
512+
/* timeout expired waiting for child to become "ready" */
513+
SBGR_TIMEOUT,
514+
515+
/* child process exited or was signalled before becomming "ready" */
516+
SBGR_DIED,
517+
};
518+
519+
/**
520+
* Callback used by start_bg_command() to ask whether the
521+
* child process is ready or needs more time to become "ready".
522+
*
523+
* The callback will receive the cmd and cb_data arguments given to
524+
* start_bg_command().
525+
*
526+
* Returns 1 is child needs more time (subject to the requested timeout).
527+
* Returns 0 if child is "ready".
528+
* Returns -1 on any error and cause start_bg_command() to also error out.
529+
*/
530+
typedef int(start_bg_wait_cb)(const struct child_process *cmd, void *cb_data);
531+
532+
/**
533+
* Start a command in the background. Wait long enough for the child
534+
* to become "ready" (as defined by the provided callback). Capture
535+
* immediate errors (like failure to start) and any immediate exit
536+
* status (such as a shutdown/signal before the child became "ready")
537+
* and return this like start_command().
538+
*
539+
* We run a custom wait loop using the provided callback to wait for
540+
* the child to start and become "ready". This is limited by the given
541+
* timeout value.
542+
*
543+
* If the child does successfully start and become "ready", we orphan
544+
* it into the background.
545+
*
546+
* The caller must not call finish_command().
547+
*
548+
* The opaque cb_data argument will be forwarded to the callback for
549+
* any instance data that it might require. This may be NULL.
550+
*/
551+
enum start_bg_result start_bg_command(struct child_process *cmd,
552+
start_bg_wait_cb *wait_cb,
553+
void *cb_data,
554+
unsigned int timeout_sec);
555+
499556
#endif

0 commit comments

Comments
 (0)