VBoxGuestR3LibDaemonize.cpp revision bd659c83e4074140e6f02eed6e2ef011121ab60d
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, daemonize a process.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2007 innotek GmbH
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * available from http://www.virtualbox.org. This file is free software;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * you can redistribute it and/or modify it under the terms of the GNU
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * General Public License (GPL) as published by the Free Software
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Header Files *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
f5e53763b0a581b0299e98028c6c52192eb06785vboxsync#else /* the unices */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Daemonize the process for running in the background.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This is supposed to do the same job as the BSD daemon() call.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns 0 on success
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param fNoChDir Pass false to change working directory to root.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param fNoClose Pass false to redirect standard file streams to /dev/null.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncVBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Get the full path to the executable. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync /* calc the length of the command line. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0"));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0"));
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized "));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1);
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2);
65697a26b524640b83828b715160c798c43a0424vboxsync /* spawn a detach process */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** @todo Change this to some standard log/print error?? */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj); */
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync#else /* the unices */
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync * Fork the child process and quit the parent.
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync * On Linux we'll fork once more at the end of it all just to be sure that
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync * we're not leaving any zombies behind. The SIGHUP stuff is ignored because
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * the parent may throw us one before we get to the setsid stuff one some
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * systems (BSD).
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rcSigAct = sigaction(SIGHUP, &SigAct, &OldSigAct);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * The orphaned child becomes is reparented to the init process.
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync * We create a new session for it (setsid), point the standard
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync * file descriptors to /dev/null, and change to the root directory.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Open stdin(0), stdout(1) and stderr(2) as /dev/null. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Change the umask - this is non-standard daemon() behavior.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** @todo why is umask set to 0 on linux? the solaris value is more sensible... */
65697a26b524640b83828b715160c798c43a0424vboxsync * And fork again to avoid zomibies and stuff (non-standard daemon() behaviour).
65697a26b524640b83828b715160c798c43a0424vboxsync# endif /* RT_OS_LINUX */