mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-09-13 11:15:08 +00:00
virExec: Delay daemonizing as long as possible.
This way the caller can catch more errors (e.g. from a hook callback) from the intermediate process.
This commit is contained in:
parent
a4d0a3fd50
commit
5314dffc85
@ -1,3 +1,7 @@
|
|||||||
|
Mon May 4 11:42:01 EDT 2009 Cole Robinson <crobinso@redhat.com>
|
||||||
|
|
||||||
|
* src/util.c: virExec: Delay daemonizing as long as possible.
|
||||||
|
|
||||||
Mon May 4 11:40:27 EDT 2009 Cole Robinson <crobinso@redhat.com>
|
Mon May 4 11:40:27 EDT 2009 Cole Robinson <crobinso@redhat.com>
|
||||||
|
|
||||||
* src/util.c: Add some documentation to __virExec and virExec
|
* src/util.c: Add some documentation to __virExec and virExec
|
||||||
|
56
src/util.c
56
src/util.c
@ -445,7 +445,7 @@ __virExec(virConnectPtr conn,
|
|||||||
if (pthread_sigmask(SIG_SETMASK, &newmask, NULL) != 0) {
|
if (pthread_sigmask(SIG_SETMASK, &newmask, NULL) != 0) {
|
||||||
virReportSystemError(conn, errno,
|
virReportSystemError(conn, errno,
|
||||||
"%s", _("cannot unblock signals"));
|
"%s", _("cannot unblock signals"));
|
||||||
return -1;
|
_exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
openmax = sysconf (_SC_OPEN_MAX);
|
openmax = sysconf (_SC_OPEN_MAX);
|
||||||
@ -458,31 +458,6 @@ __virExec(virConnectPtr conn,
|
|||||||
!FD_ISSET(i, keepfd)))
|
!FD_ISSET(i, keepfd)))
|
||||||
close(i);
|
close(i);
|
||||||
|
|
||||||
if (flags & VIR_EXEC_DAEMON) {
|
|
||||||
if (setsid() < 0) {
|
|
||||||
virReportSystemError(conn, errno,
|
|
||||||
"%s", _("cannot become session leader"));
|
|
||||||
_exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (chdir("/") < 0) {
|
|
||||||
virReportSystemError(conn, errno,
|
|
||||||
"%s", _("cannot change to root directory: %s"));
|
|
||||||
_exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
pid = fork();
|
|
||||||
if (pid < 0) {
|
|
||||||
virReportSystemError(conn, errno,
|
|
||||||
"%s", _("cannot fork child process"));
|
|
||||||
_exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pid > 0)
|
|
||||||
_exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (dup2(infd >= 0 ? infd : null, STDIN_FILENO) < 0) {
|
if (dup2(infd >= 0 ? infd : null, STDIN_FILENO) < 0) {
|
||||||
virReportSystemError(conn, errno,
|
virReportSystemError(conn, errno,
|
||||||
"%s", _("failed to setup stdin file handle"));
|
"%s", _("failed to setup stdin file handle"));
|
||||||
@ -513,6 +488,33 @@ __virExec(virConnectPtr conn,
|
|||||||
if (hook)
|
if (hook)
|
||||||
if ((hook)(data) != 0)
|
if ((hook)(data) != 0)
|
||||||
_exit(1);
|
_exit(1);
|
||||||
|
|
||||||
|
/* Daemonize as late as possible, so the parent process can detect
|
||||||
|
* the above errors with wait* */
|
||||||
|
if (flags & VIR_EXEC_DAEMON) {
|
||||||
|
if (setsid() < 0) {
|
||||||
|
virReportSystemError(conn, errno,
|
||||||
|
"%s", _("cannot become session leader"));
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chdir("/") < 0) {
|
||||||
|
virReportSystemError(conn, errno,
|
||||||
|
"%s", _("cannot change to root directory: %s"));
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pid = fork();
|
||||||
|
if (pid < 0) {
|
||||||
|
virReportSystemError(conn, errno,
|
||||||
|
"%s", _("cannot fork child process"));
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid > 0)
|
||||||
|
_exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
if (envp)
|
if (envp)
|
||||||
execve(argv[0], (char **) argv, (char**)envp);
|
execve(argv[0], (char **) argv, (char**)envp);
|
||||||
else
|
else
|
||||||
@ -524,8 +526,6 @@ __virExec(virConnectPtr conn,
|
|||||||
|
|
||||||
_exit(1);
|
_exit(1);
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
/* This is cleanup of parent process only - child
|
/* This is cleanup of parent process only - child
|
||||||
should never jump here on error */
|
should never jump here on error */
|
||||||
|
Loading…
Reference in New Issue
Block a user