[5824] | 1 | /** $Id: VBoxGuestR3LibDaemonize.cpp 98475 2023-02-03 19:27:47Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[6445] | 3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, daemonize a process.
|
---|
[5824] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2007-2023 Oracle and/or its affiliates.
|
---|
[5824] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[8155] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[26425] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[26425] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[5824] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[45459] | 41 | #if defined(RT_OS_OS2)
|
---|
[5824] | 42 | # define INCL_BASE
|
---|
| 43 | # define INCL_ERRORS
|
---|
| 44 | # include <os2.h>
|
---|
[6356] | 45 |
|
---|
| 46 | # include <iprt/alloca.h>
|
---|
| 47 | # include <iprt/string.h>
|
---|
[6445] | 48 |
|
---|
| 49 | #elif defined(RT_OS_WINDOWS)
|
---|
| 50 | # error "PORTME"
|
---|
| 51 |
|
---|
| 52 | #else /* the unices */
|
---|
[6356] | 53 | # include <sys/types.h>
|
---|
| 54 | # include <sys/stat.h>
|
---|
[53421] | 55 | # include <sys/wait.h>
|
---|
[6356] | 56 | # include <stdio.h>
|
---|
[6445] | 57 | # include <fcntl.h>
|
---|
[6356] | 58 | # include <stdlib.h>
|
---|
| 59 | # include <unistd.h>
|
---|
| 60 | # include <signal.h>
|
---|
[6445] | 61 | # include <errno.h>
|
---|
[6246] | 62 | #endif
|
---|
[5824] | 63 |
|
---|
[18320] | 64 | #include <iprt/process.h>
|
---|
[21211] | 65 | #include <iprt/string.h>
|
---|
[98472] | 66 | #include <VBox/err.h>
|
---|
| 67 | #include <VBox/log.h>
|
---|
| 68 |
|
---|
[68650] | 69 | #include "VBoxGuestR3LibInternal.h"
|
---|
[5824] | 70 |
|
---|
[6235] | 71 |
|
---|
[5824] | 72 | /**
|
---|
[6445] | 73 | * Daemonize the process for running in the background.
|
---|
[6246] | 74 | *
|
---|
[6445] | 75 | * This is supposed to do the same job as the BSD daemon() call.
|
---|
[6246] | 76 | *
|
---|
[6356] | 77 | * @returns 0 on success
|
---|
| 78 | *
|
---|
[98472] | 79 | * @param fNoChDir Pass false to change working directory to root.
|
---|
| 80 | * @param fNoClose Pass false to redirect standard file streams to /dev/null.
|
---|
| 81 | * @param fRespawn Restart the daemonised process after five seconds if it
|
---|
| 82 | * terminates abnormally.
|
---|
| 83 | * @param pcRespawn Where to store a count of how often we have respawned,
|
---|
| 84 | * intended for avoiding error spamming. Optional.
|
---|
| 85 | * @param fReturnOnUpdate If True, this function will return control to caller when
|
---|
| 86 | * child process will terminate with exit code of VBGLR3EXITCODERELOAD,
|
---|
| 87 | * indicating that Guest Additions update has been started and this running
|
---|
| 88 | * process will be asked to be restarted by arrival of the next SIGUSR1
|
---|
| 89 | * signal (caller should wait for SIGUSR1). If False, this functions will
|
---|
| 90 | * never return, but rather exit() when child process terminates with
|
---|
| 91 | * exit code 0.
|
---|
| 92 | * @param pfUpdateStarted A flag which passed to caller if fReturnOnUpdate is True (can be NULL).
|
---|
| 93 | * @param szPidfile Optional path to parent process' pidfile (can be NULL).
|
---|
| 94 | * @param phPidfile Optional path to parent process' pidfile handle (can not be NULL if
|
---|
| 95 | * szPidfile was specified).
|
---|
[27748] | 96 | *
|
---|
| 97 | * @todo Use RTProcDaemonize instead of this.
|
---|
[53421] | 98 | * @todo Implement fRespawn on OS/2.
|
---|
| 99 | * @todo Make the respawn interval configurable. But not until someone
|
---|
| 100 | * actually needs that.
|
---|
[6356] | 101 | */
|
---|
[98472] | 102 | VBGLR3DECL(int) VbglR3DaemonizeEx(bool fNoChDir, bool fNoClose, bool fRespawn, unsigned *pcRespawn,
|
---|
| 103 | bool fReturnOnUpdate, bool *pfUpdateStarted, const char *szPidfile,
|
---|
| 104 | RTFILE *phPidfile)
|
---|
[6356] | 105 | {
|
---|
[45459] | 106 | #if defined(RT_OS_OS2)
|
---|
[6356] | 107 | PPIB pPib;
|
---|
| 108 | PTIB pTib;
|
---|
| 109 | DosGetInfoBlocks(&pTib, &pPib);
|
---|
| 110 |
|
---|
[98472] | 111 | RT_NOREF(fReturnOnUpdate);
|
---|
| 112 | RT_NOREF(pfUpdateStarted);
|
---|
| 113 | RT_NOREF(szPidfile);
|
---|
| 114 | RT_NOREF(phPidfile);
|
---|
| 115 |
|
---|
[53421] | 116 | AssertRelease(!fRespawn);
|
---|
[6356] | 117 | /* Get the full path to the executable. */
|
---|
| 118 | char szExe[CCHMAXPATH];
|
---|
| 119 | APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe);
|
---|
| 120 | if (rc)
|
---|
[6445] | 121 | return RTErrConvertFromOS2(rc);
|
---|
[6356] | 122 |
|
---|
| 123 | /* calc the length of the command line. */
|
---|
| 124 | char *pch = pPib->pib_pchcmd;
|
---|
| 125 | size_t cch0 = strlen(pch);
|
---|
| 126 | pch += cch0 + 1;
|
---|
| 127 | size_t cch1 = strlen(pch);
|
---|
| 128 | pch += cch1 + 1;
|
---|
| 129 | char *pchArgs;
|
---|
| 130 | if (cch1 && *pch)
|
---|
| 131 | {
|
---|
| 132 | do pch = strchr(pch, '\0') + 1;
|
---|
| 133 | while (*pch);
|
---|
| 134 |
|
---|
| 135 | size_t cchTotal = pch - pPib->pib_pchcmd;
|
---|
| 136 | pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0"));
|
---|
| 137 | memcpy(pchArgs, pPib->pib_pchcmd, cchTotal - 1);
|
---|
| 138 | memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0"));
|
---|
| 139 | }
|
---|
| 140 | else
|
---|
| 141 | {
|
---|
| 142 | size_t cchTotal = pch - pPib->pib_pchcmd + 1;
|
---|
| 143 | pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized "));
|
---|
| 144 | memcpy(pchArgs, pPib->pib_pchcmd, cch0 + 1);
|
---|
| 145 | pch = pchArgs + cch0 + 1;
|
---|
| 146 | memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1);
|
---|
| 147 | pch += sizeof(" --daemonized ") - 1;
|
---|
| 148 | if (cch1)
|
---|
| 149 | memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2);
|
---|
| 150 | else
|
---|
| 151 | pch[0] = pch[1] = '\0';
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /* spawn a detach process */
|
---|
| 155 | char szObj[128];
|
---|
| 156 | RESULTCODES ResCodes = { 0, 0 };
|
---|
| 157 | szObj[0] = '\0';
|
---|
| 158 | rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
|
---|
| 159 | if (rc)
|
---|
| 160 | {
|
---|
| 161 | /** @todo Change this to some standard log/print error?? */
|
---|
| 162 | /* VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj); */
|
---|
[6445] | 163 | return RTErrConvertFromOS2(rc);
|
---|
[6356] | 164 | }
|
---|
| 165 | DosExit(EXIT_PROCESS, 0);
|
---|
[6445] | 166 | return VERR_GENERAL_FAILURE;
|
---|
[6356] | 167 |
|
---|
[6445] | 168 | #elif defined(RT_OS_WINDOWS)
|
---|
| 169 | # error "PORTME"
|
---|
[6356] | 170 |
|
---|
[6445] | 171 | #else /* the unices */
|
---|
| 172 | /*
|
---|
[10316] | 173 | * Fork the child process in a new session and quit the parent.
|
---|
[6445] | 174 | *
|
---|
[10316] | 175 | * - fork once and create a new session (setsid). This will detach us
|
---|
| 176 | * from the controlling tty meaning that we won't receive the SIGHUP
|
---|
| 177 | * (or any other signal) sent to that session.
|
---|
| 178 | * - The SIGHUP signal is ignored because the session/parent may throw
|
---|
| 179 | * us one before we get to the setsid.
|
---|
| 180 | * - When the parent exit(0) we will become an orphan and re-parented to
|
---|
| 181 | * the init process.
|
---|
[33540] | 182 | * - Because of the Linux / System V semantics of assigning the controlling
|
---|
[10316] | 183 | * tty automagically when a session leader first opens a tty, we will
|
---|
| 184 | * fork() once more on Linux to get rid of the session leadership role.
|
---|
[6445] | 185 | */
|
---|
[10635] | 186 |
|
---|
[6445] | 187 | struct sigaction OldSigAct;
|
---|
| 188 | struct sigaction SigAct;
|
---|
[24320] | 189 | RT_ZERO(SigAct);
|
---|
[6445] | 190 | SigAct.sa_handler = SIG_IGN;
|
---|
| 191 | int rcSigAct = sigaction(SIGHUP, &SigAct, &OldSigAct);
|
---|
| 192 |
|
---|
[6356] | 193 | pid_t pid = fork();
|
---|
[6445] | 194 | if (pid == -1)
|
---|
| 195 | return RTErrConvertFromErrno(errno);
|
---|
| 196 | if (pid != 0)
|
---|
[6356] | 197 | exit(0);
|
---|
| 198 |
|
---|
| 199 | /*
|
---|
[6445] | 200 | * The orphaned child becomes is reparented to the init process.
|
---|
| 201 | * We create a new session for it (setsid), point the standard
|
---|
| 202 | * file descriptors to /dev/null, and change to the root directory.
|
---|
[6356] | 203 | */
|
---|
| 204 | pid_t newpgid = setsid();
|
---|
[6445] | 205 | int SavedErrno = errno;
|
---|
| 206 | if (rcSigAct != -1)
|
---|
| 207 | sigaction(SIGHUP, &OldSigAct, NULL);
|
---|
| 208 | if (newpgid == -1)
|
---|
| 209 | return RTErrConvertFromErrno(SavedErrno);
|
---|
[6356] | 210 |
|
---|
[6445] | 211 | if (!fNoClose)
|
---|
[6356] | 212 | {
|
---|
[6445] | 213 | /* Open stdin(0), stdout(1) and stderr(2) as /dev/null. */
|
---|
[6356] | 214 | int fd = open("/dev/null", O_RDWR);
|
---|
[6445] | 215 | if (fd == -1) /* paranoia */
|
---|
| 216 | {
|
---|
| 217 | close(STDIN_FILENO);
|
---|
| 218 | close(STDOUT_FILENO);
|
---|
| 219 | close(STDERR_FILENO);
|
---|
| 220 | fd = open("/dev/null", O_RDWR);
|
---|
| 221 | }
|
---|
| 222 | if (fd != -1)
|
---|
| 223 | {
|
---|
| 224 | dup2(fd, STDIN_FILENO);
|
---|
| 225 | dup2(fd, STDOUT_FILENO);
|
---|
| 226 | dup2(fd, STDERR_FILENO);
|
---|
| 227 | if (fd > 2)
|
---|
| 228 | close(fd);
|
---|
| 229 | }
|
---|
[6356] | 230 | }
|
---|
| 231 |
|
---|
[6445] | 232 | if (!fNoChDir)
|
---|
[62882] | 233 | {
|
---|
| 234 | int rcShutUpGcc = chdir("/");
|
---|
| 235 | RT_NOREF_PV(rcShutUpGcc);
|
---|
| 236 | }
|
---|
[6356] | 237 |
|
---|
[6445] | 238 | /*
|
---|
| 239 | * Change the umask - this is non-standard daemon() behavior.
|
---|
| 240 | */
|
---|
[6356] | 241 | umask(027);
|
---|
[6445] | 242 |
|
---|
| 243 | # ifdef RT_OS_LINUX
|
---|
| 244 | /*
|
---|
[10308] | 245 | * And fork again to lose session leader status (non-standard daemon()
|
---|
| 246 | * behaviour).
|
---|
[6445] | 247 | */
|
---|
| 248 | pid = fork();
|
---|
| 249 | if (pid == -1)
|
---|
| 250 | return RTErrConvertFromErrno(errno);
|
---|
| 251 | if (pid != 0)
|
---|
| 252 | exit(0);
|
---|
[98475] | 253 | # endif /* RT_OS_LINUX */
|
---|
[98472] | 254 |
|
---|
| 255 | /* Check if another instance is already running. */
|
---|
| 256 | if (szPidfile != NULL)
|
---|
| 257 | {
|
---|
| 258 | if (phPidfile != NULL)
|
---|
| 259 | {
|
---|
| 260 | int rc = VbglR3PidfileWait(szPidfile, phPidfile, 5000);
|
---|
| 261 |
|
---|
| 262 | /* Another instance of process is already running. */
|
---|
| 263 | if (rc == VERR_FILE_LOCK_VIOLATION)
|
---|
| 264 | {
|
---|
| 265 | LogRel(("cannot aquire pidfile %s, exitting\n", szPidfile));
|
---|
| 266 | exit(1);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | /* Unable to lock on pidfile. */
|
---|
| 270 | if (RT_FAILURE(rc))
|
---|
| 271 | exit(1);
|
---|
| 272 | }
|
---|
| 273 | else
|
---|
| 274 | return VERR_INVALID_PARAMETER;
|
---|
| 275 | }
|
---|
[6445] | 276 |
|
---|
[53421] | 277 | if (fRespawn)
|
---|
[54010] | 278 | {
|
---|
[53421] | 279 | /* We implement re-spawning as a third fork(), with the parent process
|
---|
| 280 | * monitoring the child and re-starting it after a delay if it exits
|
---|
| 281 | * abnormally. */
|
---|
[54010] | 282 | unsigned cRespawn = 0;
|
---|
[53421] | 283 | for (;;)
|
---|
| 284 | {
|
---|
| 285 | int iStatus, rcWait;
|
---|
| 286 |
|
---|
[54010] | 287 | if (pcRespawn != NULL)
|
---|
| 288 | *pcRespawn = cRespawn;
|
---|
[53421] | 289 | pid = fork();
|
---|
| 290 | if (pid == -1)
|
---|
| 291 | return RTErrConvertFromErrno(errno);
|
---|
| 292 | if (pid == 0)
|
---|
| 293 | return VINF_SUCCESS;
|
---|
| 294 | do
|
---|
| 295 | rcWait = waitpid(pid, &iStatus, 0);
|
---|
| 296 | while (rcWait == -1 && errno == EINTR);
|
---|
| 297 | if (rcWait == -1)
|
---|
| 298 | exit(1);
|
---|
[98472] | 299 | if (WIFEXITED(iStatus))
|
---|
| 300 | {
|
---|
| 301 | if (WEXITSTATUS(iStatus) == 0)
|
---|
| 302 | exit(0);
|
---|
| 303 | else if (fReturnOnUpdate && WEXITSTATUS(iStatus) == VBGLR3EXITCODERELOAD)
|
---|
| 304 | {
|
---|
| 305 | /* Tell caller that update has been started. */
|
---|
| 306 | if (pfUpdateStarted != NULL)
|
---|
| 307 | *pfUpdateStarted = true;
|
---|
| 308 |
|
---|
| 309 | return VINF_SUCCESS;
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
[53421] | 312 | sleep(5);
|
---|
[54010] | 313 | ++cRespawn;
|
---|
[53421] | 314 | }
|
---|
[54010] | 315 | }
|
---|
[6445] | 316 | return VINF_SUCCESS;
|
---|
[6356] | 317 | #endif
|
---|
| 318 | }
|
---|
[98472] | 319 |
|
---|
| 320 | /**
|
---|
| 321 | * A wrapper function for VbglR3DaemonizeEx.
|
---|
| 322 | */
|
---|
| 323 | VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose, bool fRespawn, unsigned *pcRespawn)
|
---|
| 324 | {
|
---|
| 325 | return VbglR3DaemonizeEx(fNoChDir, fNoClose, fRespawn, pcRespawn, false, NULL, NULL, NULL);
|
---|
| 326 | }
|
---|