1 | /** $Id: VBoxGuestR3LibDaemonize.cpp 10316 2008-07-07 12:54:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, daemonize a process.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #if defined(RT_OS_DARWIN)
|
---|
27 | # error "PORTME"
|
---|
28 |
|
---|
29 | #elif defined(RT_OS_OS2)
|
---|
30 | # define INCL_BASE
|
---|
31 | # define INCL_ERRORS
|
---|
32 | # include <os2.h>
|
---|
33 |
|
---|
34 | # include <iprt/alloca.h>
|
---|
35 | # include <iprt/string.h>
|
---|
36 |
|
---|
37 | #elif defined(RT_OS_WINDOWS)
|
---|
38 | # error "PORTME"
|
---|
39 |
|
---|
40 | #else /* the unices */
|
---|
41 | # include <sys/types.h>
|
---|
42 | # include <sys/stat.h>
|
---|
43 | # include <stdio.h>
|
---|
44 | # include <fcntl.h>
|
---|
45 | # include <stdlib.h>
|
---|
46 | # include <unistd.h>
|
---|
47 | # include <signal.h>
|
---|
48 | # include <errno.h>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | #include <iprt/string.h>
|
---|
52 | #include <VBox/VBoxGuest.h>
|
---|
53 |
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Daemonize the process for running in the background.
|
---|
57 | *
|
---|
58 | * This is supposed to do the same job as the BSD daemon() call.
|
---|
59 | *
|
---|
60 | * @returns 0 on success
|
---|
61 | *
|
---|
62 | * @param fNoChDir Pass false to change working directory to root.
|
---|
63 | * @param fNoClose Pass false to redirect standard file streams to /dev/null.
|
---|
64 | */
|
---|
65 | VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose)
|
---|
66 | {
|
---|
67 | #if defined(RT_OS_DARWIN)
|
---|
68 | # error "PORTME"
|
---|
69 |
|
---|
70 | #elif defined(RT_OS_OS2)
|
---|
71 | PPIB pPib;
|
---|
72 | PTIB pTib;
|
---|
73 | DosGetInfoBlocks(&pTib, &pPib);
|
---|
74 |
|
---|
75 | /* Get the full path to the executable. */
|
---|
76 | char szExe[CCHMAXPATH];
|
---|
77 | APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe);
|
---|
78 | if (rc)
|
---|
79 | return RTErrConvertFromOS2(rc);
|
---|
80 |
|
---|
81 | /* calc the length of the command line. */
|
---|
82 | char *pch = pPib->pib_pchcmd;
|
---|
83 | size_t cch0 = strlen(pch);
|
---|
84 | pch += cch0 + 1;
|
---|
85 | size_t cch1 = strlen(pch);
|
---|
86 | pch += cch1 + 1;
|
---|
87 | char *pchArgs;
|
---|
88 | if (cch1 && *pch)
|
---|
89 | {
|
---|
90 | do pch = strchr(pch, '\0') + 1;
|
---|
91 | while (*pch);
|
---|
92 |
|
---|
93 | size_t cchTotal = pch - pPib->pib_pchcmd;
|
---|
94 | pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0"));
|
---|
95 | memcpy(pchArgs, pPib->pib_pchcmd, cchTotal - 1);
|
---|
96 | memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0"));
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | size_t cchTotal = pch - pPib->pib_pchcmd + 1;
|
---|
101 | pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized "));
|
---|
102 | memcpy(pchArgs, pPib->pib_pchcmd, cch0 + 1);
|
---|
103 | pch = pchArgs + cch0 + 1;
|
---|
104 | memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1);
|
---|
105 | pch += sizeof(" --daemonized ") - 1;
|
---|
106 | if (cch1)
|
---|
107 | memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2);
|
---|
108 | else
|
---|
109 | pch[0] = pch[1] = '\0';
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* spawn a detach process */
|
---|
113 | char szObj[128];
|
---|
114 | RESULTCODES ResCodes = { 0, 0 };
|
---|
115 | szObj[0] = '\0';
|
---|
116 | rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
|
---|
117 | if (rc)
|
---|
118 | {
|
---|
119 | /** @todo Change this to some standard log/print error?? */
|
---|
120 | /* VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj); */
|
---|
121 | return RTErrConvertFromOS2(rc);
|
---|
122 | }
|
---|
123 | DosExit(EXIT_PROCESS, 0);
|
---|
124 | return VERR_GENERAL_FAILURE;
|
---|
125 |
|
---|
126 | #elif defined(RT_OS_WINDOWS)
|
---|
127 | # error "PORTME"
|
---|
128 |
|
---|
129 | #else /* the unices */
|
---|
130 | /*
|
---|
131 | * Fork the child process in a new session and quit the parent.
|
---|
132 | *
|
---|
133 | * - fork once and create a new session (setsid). This will detach us
|
---|
134 | * from the controlling tty meaning that we won't receive the SIGHUP
|
---|
135 | * (or any other signal) sent to that session.
|
---|
136 | * - The SIGHUP signal is ignored because the session/parent may throw
|
---|
137 | * us one before we get to the setsid.
|
---|
138 | * - When the parent exit(0) we will become an orphan and re-parented to
|
---|
139 | * the init process.
|
---|
140 | * - Because of the Linux / System V sematics of assigning the controlling
|
---|
141 | * tty automagically when a session leader first opens a tty, we will
|
---|
142 | * fork() once more on Linux to get rid of the session leadership role.
|
---|
143 | */
|
---|
144 | struct sigaction OldSigAct;
|
---|
145 | struct sigaction SigAct;
|
---|
146 | memset(&SigAct, 0, sizeof(SigAct));
|
---|
147 | SigAct.sa_handler = SIG_IGN;
|
---|
148 | int rcSigAct = sigaction(SIGHUP, &SigAct, &OldSigAct);
|
---|
149 |
|
---|
150 | pid_t pid = fork();
|
---|
151 | if (pid == -1)
|
---|
152 | return RTErrConvertFromErrno(errno);
|
---|
153 | if (pid != 0)
|
---|
154 | exit(0);
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * The orphaned child becomes is reparented to the init process.
|
---|
158 | * We create a new session for it (setsid), point the standard
|
---|
159 | * file descriptors to /dev/null, and change to the root directory.
|
---|
160 | */
|
---|
161 | pid_t newpgid = setsid();
|
---|
162 | int SavedErrno = errno;
|
---|
163 | if (rcSigAct != -1)
|
---|
164 | sigaction(SIGHUP, &OldSigAct, NULL);
|
---|
165 | if (newpgid == -1)
|
---|
166 | return RTErrConvertFromErrno(SavedErrno);
|
---|
167 |
|
---|
168 | if (!fNoClose)
|
---|
169 | {
|
---|
170 | /* Open stdin(0), stdout(1) and stderr(2) as /dev/null. */
|
---|
171 | int fd = open("/dev/null", O_RDWR);
|
---|
172 | if (fd == -1) /* paranoia */
|
---|
173 | {
|
---|
174 | close(STDIN_FILENO);
|
---|
175 | close(STDOUT_FILENO);
|
---|
176 | close(STDERR_FILENO);
|
---|
177 | fd = open("/dev/null", O_RDWR);
|
---|
178 | }
|
---|
179 | if (fd != -1)
|
---|
180 | {
|
---|
181 | dup2(fd, STDIN_FILENO);
|
---|
182 | dup2(fd, STDOUT_FILENO);
|
---|
183 | dup2(fd, STDERR_FILENO);
|
---|
184 | if (fd > 2)
|
---|
185 | close(fd);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (!fNoChDir)
|
---|
190 | chdir("/");
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Change the umask - this is non-standard daemon() behavior.
|
---|
194 | */
|
---|
195 | umask(027);
|
---|
196 |
|
---|
197 | # ifdef RT_OS_LINUX
|
---|
198 | /*
|
---|
199 | * And fork again to lose session leader status (non-standard daemon()
|
---|
200 | * behaviour).
|
---|
201 | */
|
---|
202 | pid = fork();
|
---|
203 | if (pid == -1)
|
---|
204 | return RTErrConvertFromErrno(errno);
|
---|
205 | if (pid != 0)
|
---|
206 | exit(0);
|
---|
207 | # endif /* RT_OS_LINUX */
|
---|
208 |
|
---|
209 | return VINF_SUCCESS;
|
---|
210 | #endif
|
---|
211 | }
|
---|
212 |
|
---|