VBoxGuestR3LibDaemonize.cpp revision 30228873f5bf2e776fcfe1a73153f9207e9bbbc5
46f059bea92bedbd395793702c73946ead235586vboxsync/** $Id$ */
46f059bea92bedbd395793702c73946ead235586vboxsync/** @file
46f059bea92bedbd395793702c73946ead235586vboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, daemonize a process.
46f059bea92bedbd395793702c73946ead235586vboxsync */
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync/*
46f059bea92bedbd395793702c73946ead235586vboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
46f059bea92bedbd395793702c73946ead235586vboxsync *
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 *
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.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync */
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync/*******************************************************************************
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync* Header Files *
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync*******************************************************************************/
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync#if defined(RT_OS_DARWIN)
46f059bea92bedbd395793702c73946ead235586vboxsync# error "PORTME"
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync#elif defined(RT_OS_OS2)
46f059bea92bedbd395793702c73946ead235586vboxsync# define INCL_BASE
46f059bea92bedbd395793702c73946ead235586vboxsync# define INCL_ERRORS
46f059bea92bedbd395793702c73946ead235586vboxsync# include <os2.h>
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync# include <iprt/alloca.h>
46f059bea92bedbd395793702c73946ead235586vboxsync# include <iprt/string.h>
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync#elif defined(RT_OS_WINDOWS)
46f059bea92bedbd395793702c73946ead235586vboxsync# error "PORTME"
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync#else /* the unices */
46f059bea92bedbd395793702c73946ead235586vboxsync# include <sys/types.h>
46f059bea92bedbd395793702c73946ead235586vboxsync# include <sys/stat.h>
46f059bea92bedbd395793702c73946ead235586vboxsync# include <stdio.h>
46f059bea92bedbd395793702c73946ead235586vboxsync# include <fcntl.h>
46f059bea92bedbd395793702c73946ead235586vboxsync# include <stdlib.h>
46f059bea92bedbd395793702c73946ead235586vboxsync# include <unistd.h>
46f059bea92bedbd395793702c73946ead235586vboxsync# include <signal.h>
46f059bea92bedbd395793702c73946ead235586vboxsync# include <errno.h>
46f059bea92bedbd395793702c73946ead235586vboxsync#endif
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync#include <iprt/string.h>
46f059bea92bedbd395793702c73946ead235586vboxsync#include <VBox/VBoxGuest.h>
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync/**
46f059bea92bedbd395793702c73946ead235586vboxsync * Daemonize the process for running in the background.
46f059bea92bedbd395793702c73946ead235586vboxsync *
46f059bea92bedbd395793702c73946ead235586vboxsync * This is supposed to do the same job as the BSD daemon() call.
46f059bea92bedbd395793702c73946ead235586vboxsync *
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * @returns 0 on success
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync *
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * @param fNoChDir Pass false to change working directory to root.
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * @param fNoClose Pass false to redirect standard file streams to /dev/null.
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync */
46f059bea92bedbd395793702c73946ead235586vboxsyncVBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose)
46f059bea92bedbd395793702c73946ead235586vboxsync{
46f059bea92bedbd395793702c73946ead235586vboxsync#if defined(RT_OS_DARWIN)
46f059bea92bedbd395793702c73946ead235586vboxsync# error "PORTME"
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync#elif defined(RT_OS_OS2)
46f059bea92bedbd395793702c73946ead235586vboxsync PPIB pPib;
46f059bea92bedbd395793702c73946ead235586vboxsync PTIB pTib;
46f059bea92bedbd395793702c73946ead235586vboxsync DosGetInfoBlocks(&pTib, &pPib);
46f059bea92bedbd395793702c73946ead235586vboxsync
1bef0d3fd3b2fe689a4eaa9ebf0647b8bf7c662evboxsync /* Get the full path to the executable. */
46f059bea92bedbd395793702c73946ead235586vboxsync char szExe[CCHMAXPATH];
46f059bea92bedbd395793702c73946ead235586vboxsync APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe);
46f059bea92bedbd395793702c73946ead235586vboxsync if (rc)
46f059bea92bedbd395793702c73946ead235586vboxsync return RTErrConvertFromOS2(rc);
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync /* calc the length of the command line. */
46f059bea92bedbd395793702c73946ead235586vboxsync char *pch = pPib->pib_pchcmd;
46f059bea92bedbd395793702c73946ead235586vboxsync size_t cch0 = strlen(pch);
46f059bea92bedbd395793702c73946ead235586vboxsync pch += cch0 + 1;
46f059bea92bedbd395793702c73946ead235586vboxsync size_t cch1 = strlen(pch);
46f059bea92bedbd395793702c73946ead235586vboxsync pch += cch1 + 1;
46f059bea92bedbd395793702c73946ead235586vboxsync char *pchArgs;
46f059bea92bedbd395793702c73946ead235586vboxsync if (cch1 && *pch)
46f059bea92bedbd395793702c73946ead235586vboxsync {
46f059bea92bedbd395793702c73946ead235586vboxsync do pch = strchr(pch, '\0') + 1;
46f059bea92bedbd395793702c73946ead235586vboxsync while (*pch);
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync size_t cchTotal = pch - pPib->pib_pchcmd;
46f059bea92bedbd395793702c73946ead235586vboxsync pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0"));
46f059bea92bedbd395793702c73946ead235586vboxsync memcpy(pchArgs, pPib->pib_pchcmd, cchTotal - 1);
46f059bea92bedbd395793702c73946ead235586vboxsync memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0"));
46f059bea92bedbd395793702c73946ead235586vboxsync }
46f059bea92bedbd395793702c73946ead235586vboxsync else
46f059bea92bedbd395793702c73946ead235586vboxsync {
46f059bea92bedbd395793702c73946ead235586vboxsync size_t cchTotal = pch - pPib->pib_pchcmd + 1;
46f059bea92bedbd395793702c73946ead235586vboxsync pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized "));
46f059bea92bedbd395793702c73946ead235586vboxsync memcpy(pchArgs, pPib->pib_pchcmd, cch0 + 1);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync pch = pchArgs + cch0 + 1;
46f059bea92bedbd395793702c73946ead235586vboxsync memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1);
46f059bea92bedbd395793702c73946ead235586vboxsync pch += sizeof(" --daemonized ") - 1;
46f059bea92bedbd395793702c73946ead235586vboxsync if (cch1)
46f059bea92bedbd395793702c73946ead235586vboxsync memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync else
46f059bea92bedbd395793702c73946ead235586vboxsync pch[0] = pch[1] = '\0';
46f059bea92bedbd395793702c73946ead235586vboxsync }
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync /* spawn a detach process */
46f059bea92bedbd395793702c73946ead235586vboxsync char szObj[128];
46f059bea92bedbd395793702c73946ead235586vboxsync RESULTCODES ResCodes = { 0, 0 };
46f059bea92bedbd395793702c73946ead235586vboxsync szObj[0] = '\0';
46f059bea92bedbd395793702c73946ead235586vboxsync rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
46f059bea92bedbd395793702c73946ead235586vboxsync if (rc)
46f059bea92bedbd395793702c73946ead235586vboxsync {
46f059bea92bedbd395793702c73946ead235586vboxsync /** @todo Change this to some standard log/print error?? */
46f059bea92bedbd395793702c73946ead235586vboxsync /* VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj); */
46f059bea92bedbd395793702c73946ead235586vboxsync return RTErrConvertFromOS2(rc);
46f059bea92bedbd395793702c73946ead235586vboxsync }
46f059bea92bedbd395793702c73946ead235586vboxsync DosExit(EXIT_PROCESS, 0);
46f059bea92bedbd395793702c73946ead235586vboxsync return VERR_GENERAL_FAILURE;
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync#elif defined(RT_OS_WINDOWS)
46f059bea92bedbd395793702c73946ead235586vboxsync# error "PORTME"
46f059bea92bedbd395793702c73946ead235586vboxsync
46f059bea92bedbd395793702c73946ead235586vboxsync#else /* the unices */
46f059bea92bedbd395793702c73946ead235586vboxsync /*
46f059bea92bedbd395793702c73946ead235586vboxsync * Fork the child process in a new session and quit the parent.
46f059bea92bedbd395793702c73946ead235586vboxsync *
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 */
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync struct sigaction OldSigAct;
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync struct sigaction SigAct;
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync memset(&SigAct, 0, sizeof(SigAct));
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync SigAct.sa_handler = SIG_IGN;
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync int rcSigAct = sigaction(SIGHUP, &SigAct, &OldSigAct);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync pid_t pid = fork();
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (pid == -1)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync return RTErrConvertFromErrno(errno);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (pid != 0)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync exit(0);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync /*
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 */
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync pid_t newpgid = setsid();
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync int SavedErrno = errno;
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (rcSigAct != -1)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync sigaction(SIGHUP, &OldSigAct, NULL);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (newpgid == -1)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync return RTErrConvertFromErrno(SavedErrno);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (!fNoClose)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync {
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync /* Open stdin(0), stdout(1) and stderr(2) as /dev/null. */
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync int fd = open("/dev/null", O_RDWR);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (fd == -1) /* paranoia */
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync {
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync close(STDIN_FILENO);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync close(STDOUT_FILENO);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync close(STDERR_FILENO);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync fd = open("/dev/null", O_RDWR);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync }
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (fd != -1)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync {
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync dup2(fd, STDIN_FILENO);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync dup2(fd, STDOUT_FILENO);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync dup2(fd, STDERR_FILENO);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (fd > 2)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync close(fd);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync }
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync }
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (!fNoChDir)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync chdir("/");
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync /*
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * Change the umask - this is non-standard daemon() behavior.
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync */
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync umask(027);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync# ifdef RT_OS_LINUX
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync /*
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * And fork again to lose session leader status (non-standard daemon()
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync * behaviour).
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync */
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync pid = fork();
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (pid == -1)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync return RTErrConvertFromErrno(errno);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync if (pid != 0)
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync exit(0);
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync# endif /* RT_OS_LINUX */
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync return VINF_SUCCESS;
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync#endif
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync}
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync
df2e37f58f19997178bb9be53867a17509a3a2f9vboxsync