VBoxGuestR3LibDaemonize.cpp revision 30228873f5bf2e776fcfe1a73153f9207e9bbbc5
46f059bea92bedbd395793702c73946ead235586vboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, daemonize a process.
46f059bea92bedbd395793702c73946ead235586vboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
46f059bea92bedbd395793702c73946ead235586vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
46f059bea92bedbd395793702c73946ead235586vboxsync * available from http://www.virtualbox.org. This file is free software;
46f059bea92bedbd395793702c73946ead235586vboxsync * you can redistribute it and/or modify it under the terms of the GNU
46f059bea92bedbd395793702c73946ead235586vboxsync * General Public License (GPL) as published by the Free Software
46f059bea92bedbd395793702c73946ead235586vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * additional information or have any questions.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync/*******************************************************************************
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync* Header Files *
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync*******************************************************************************/
46f059bea92bedbd395793702c73946ead235586vboxsync#else /* the unices */
46f059bea92bedbd395793702c73946ead235586vboxsync * Daemonize the process for running in the background.
46f059bea92bedbd395793702c73946ead235586vboxsync * This is supposed to do the same job as the BSD daemon() call.
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * @returns 0 on success
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * @param fNoChDir Pass false to change working directory to root.
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * @param fNoClose Pass false to redirect standard file streams to /dev/null.
46f059bea92bedbd395793702c73946ead235586vboxsyncVBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose)
1bef0d3fd3b2fe689a4eaa9ebf0647b8bf7c662evboxsync /* Get the full path to the executable. */
46f059bea92bedbd395793702c73946ead235586vboxsync APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe);
46f059bea92bedbd395793702c73946ead235586vboxsync /* calc the length of the command line. */
46f059bea92bedbd395793702c73946ead235586vboxsync pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0"));
46f059bea92bedbd395793702c73946ead235586vboxsync memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0"));
46f059bea92bedbd395793702c73946ead235586vboxsync pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized "));
46f059bea92bedbd395793702c73946ead235586vboxsync memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1);
46f059bea92bedbd395793702c73946ead235586vboxsync memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2);
46f059bea92bedbd395793702c73946ead235586vboxsync /* spawn a detach process */
46f059bea92bedbd395793702c73946ead235586vboxsync rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
46f059bea92bedbd395793702c73946ead235586vboxsync /** @todo Change this to some standard log/print error?? */
46f059bea92bedbd395793702c73946ead235586vboxsync /* VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj); */
46f059bea92bedbd395793702c73946ead235586vboxsync#else /* the unices */
46f059bea92bedbd395793702c73946ead235586vboxsync * Fork the child process in a new session and quit the parent.
46f059bea92bedbd395793702c73946ead235586vboxsync * - fork once and create a new session (setsid). This will detach us
46f059bea92bedbd395793702c73946ead235586vboxsync * from the controlling tty meaning that we won't receive the SIGHUP
46f059bea92bedbd395793702c73946ead235586vboxsync * (or any other signal) sent to that session.
46f059bea92bedbd395793702c73946ead235586vboxsync * - The SIGHUP signal is ignored because the session/parent may throw
46f059bea92bedbd395793702c73946ead235586vboxsync * us one before we get to the setsid.
46f059bea92bedbd395793702c73946ead235586vboxsync * - When the parent exit(0) we will become an orphan and re-parented to
46f059bea92bedbd395793702c73946ead235586vboxsync * the init process.
46f059bea92bedbd395793702c73946ead235586vboxsync * - Because of the Linux / System V sematics of assigning the controlling
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * tty automagically when a session leader first opens a tty, we will
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * fork() once more on Linux to get rid of the session leadership role.
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync int rcSigAct = sigaction(SIGHUP, &SigAct, &OldSigAct);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * The orphaned child becomes is reparented to the init process.
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * We create a new session for it (setsid), point the standard
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * file descriptors to /dev/null, and change to the root directory.
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync /* Open stdin(0), stdout(1) and stderr(2) as /dev/null. */
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * Change the umask - this is non-standard daemon() behavior.
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * And fork again to lose session leader status (non-standard daemon()
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * behaviour).
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync# endif /* RT_OS_LINUX */