1 | /** $Id: VBoxGuestR3LibDaemonize.cpp 27748 2010-03-26 14:57:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, daemonize a process.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2009 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #if defined(RT_OS_DARWIN)
|
---|
36 | # error "PORTME"
|
---|
37 |
|
---|
38 | #elif defined(RT_OS_OS2)
|
---|
39 | # define INCL_BASE
|
---|
40 | # define INCL_ERRORS
|
---|
41 | # include <os2.h>
|
---|
42 |
|
---|
43 | # include <iprt/alloca.h>
|
---|
44 | # include <iprt/string.h>
|
---|
45 |
|
---|
46 | #elif defined(RT_OS_WINDOWS)
|
---|
47 | # error "PORTME"
|
---|
48 |
|
---|
49 | #else /* the unices */
|
---|
50 | # include <sys/types.h>
|
---|
51 | # include <sys/stat.h>
|
---|
52 | # include <stdio.h>
|
---|
53 | # include <fcntl.h>
|
---|
54 | # include <stdlib.h>
|
---|
55 | # include <unistd.h>
|
---|
56 | # include <signal.h>
|
---|
57 | # include <errno.h>
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | #include <iprt/file.h>
|
---|
61 | #include <iprt/process.h>
|
---|
62 | #include <iprt/string.h>
|
---|
63 | #include "VBGLR3Internal.h"
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Daemonize the process for running in the background.
|
---|
68 | *
|
---|
69 | * This is supposed to do the same job as the BSD daemon() call.
|
---|
70 | *
|
---|
71 | * @returns 0 on success
|
---|
72 | *
|
---|
73 | * @param fNoChDir Pass false to change working directory to root.
|
---|
74 | * @param fNoClose Pass false to redirect standard file streams to /dev/null.
|
---|
75 | *
|
---|
76 | * @todo Use RTProcDaemonize instead of this.
|
---|
77 | */
|
---|
78 | VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose)
|
---|
79 | {
|
---|
80 | #if defined(RT_OS_DARWIN)
|
---|
81 | # error "PORTME"
|
---|
82 |
|
---|
83 | #elif defined(RT_OS_OS2)
|
---|
84 | PPIB pPib;
|
---|
85 | PTIB pTib;
|
---|
86 | DosGetInfoBlocks(&pTib, &pPib);
|
---|
87 |
|
---|
88 | /* Get the full path to the executable. */
|
---|
89 | char szExe[CCHMAXPATH];
|
---|
90 | APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe);
|
---|
91 | if (rc)
|
---|
92 | return RTErrConvertFromOS2(rc);
|
---|
93 |
|
---|
94 | /* calc the length of the command line. */
|
---|
95 | char *pch = pPib->pib_pchcmd;
|
---|
96 | size_t cch0 = strlen(pch);
|
---|
97 | pch += cch0 + 1;
|
---|
98 | size_t cch1 = strlen(pch);
|
---|
99 | pch += cch1 + 1;
|
---|
100 | char *pchArgs;
|
---|
101 | if (cch1 && *pch)
|
---|
102 | {
|
---|
103 | do pch = strchr(pch, '\0') + 1;
|
---|
104 | while (*pch);
|
---|
105 |
|
---|
106 | size_t cchTotal = pch - pPib->pib_pchcmd;
|
---|
107 | pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0"));
|
---|
108 | memcpy(pchArgs, pPib->pib_pchcmd, cchTotal - 1);
|
---|
109 | memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0"));
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | size_t cchTotal = pch - pPib->pib_pchcmd + 1;
|
---|
114 | pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized "));
|
---|
115 | memcpy(pchArgs, pPib->pib_pchcmd, cch0 + 1);
|
---|
116 | pch = pchArgs + cch0 + 1;
|
---|
117 | memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1);
|
---|
118 | pch += sizeof(" --daemonized ") - 1;
|
---|
119 | if (cch1)
|
---|
120 | memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2);
|
---|
121 | else
|
---|
122 | pch[0] = pch[1] = '\0';
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* spawn a detach process */
|
---|
126 | char szObj[128];
|
---|
127 | RESULTCODES ResCodes = { 0, 0 };
|
---|
128 | szObj[0] = '\0';
|
---|
129 | rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
|
---|
130 | if (rc)
|
---|
131 | {
|
---|
132 | /** @todo Change this to some standard log/print error?? */
|
---|
133 | /* VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj); */
|
---|
134 | return RTErrConvertFromOS2(rc);
|
---|
135 | }
|
---|
136 | DosExit(EXIT_PROCESS, 0);
|
---|
137 | return VERR_GENERAL_FAILURE;
|
---|
138 |
|
---|
139 | #elif defined(RT_OS_WINDOWS)
|
---|
140 | # error "PORTME"
|
---|
141 |
|
---|
142 | #else /* the unices */
|
---|
143 | /*
|
---|
144 | * Fork the child process in a new session and quit the parent.
|
---|
145 | *
|
---|
146 | * - fork once and create a new session (setsid). This will detach us
|
---|
147 | * from the controlling tty meaning that we won't receive the SIGHUP
|
---|
148 | * (or any other signal) sent to that session.
|
---|
149 | * - The SIGHUP signal is ignored because the session/parent may throw
|
---|
150 | * us one before we get to the setsid.
|
---|
151 | * - When the parent exit(0) we will become an orphan and re-parented to
|
---|
152 | * the init process.
|
---|
153 | * - Because of the Linux / System V sematics of assigning the controlling
|
---|
154 | * tty automagically when a session leader first opens a tty, we will
|
---|
155 | * fork() once more on Linux to get rid of the session leadership role.
|
---|
156 | */
|
---|
157 |
|
---|
158 | struct sigaction OldSigAct;
|
---|
159 | struct sigaction SigAct;
|
---|
160 | RT_ZERO(SigAct);
|
---|
161 | SigAct.sa_handler = SIG_IGN;
|
---|
162 | int rcSigAct = sigaction(SIGHUP, &SigAct, &OldSigAct);
|
---|
163 |
|
---|
164 | pid_t pid = fork();
|
---|
165 | if (pid == -1)
|
---|
166 | return RTErrConvertFromErrno(errno);
|
---|
167 | if (pid != 0)
|
---|
168 | exit(0);
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * The orphaned child becomes is reparented to the init process.
|
---|
172 | * We create a new session for it (setsid), point the standard
|
---|
173 | * file descriptors to /dev/null, and change to the root directory.
|
---|
174 | */
|
---|
175 | pid_t newpgid = setsid();
|
---|
176 | int SavedErrno = errno;
|
---|
177 | if (rcSigAct != -1)
|
---|
178 | sigaction(SIGHUP, &OldSigAct, NULL);
|
---|
179 | if (newpgid == -1)
|
---|
180 | return RTErrConvertFromErrno(SavedErrno);
|
---|
181 |
|
---|
182 | if (!fNoClose)
|
---|
183 | {
|
---|
184 | /* Open stdin(0), stdout(1) and stderr(2) as /dev/null. */
|
---|
185 | int fd = open("/dev/null", O_RDWR);
|
---|
186 | if (fd == -1) /* paranoia */
|
---|
187 | {
|
---|
188 | close(STDIN_FILENO);
|
---|
189 | close(STDOUT_FILENO);
|
---|
190 | close(STDERR_FILENO);
|
---|
191 | fd = open("/dev/null", O_RDWR);
|
---|
192 | }
|
---|
193 | if (fd != -1)
|
---|
194 | {
|
---|
195 | dup2(fd, STDIN_FILENO);
|
---|
196 | dup2(fd, STDOUT_FILENO);
|
---|
197 | dup2(fd, STDERR_FILENO);
|
---|
198 | if (fd > 2)
|
---|
199 | close(fd);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (!fNoChDir)
|
---|
204 | chdir("/");
|
---|
205 |
|
---|
206 | /*
|
---|
207 | * Change the umask - this is non-standard daemon() behavior.
|
---|
208 | */
|
---|
209 | umask(027);
|
---|
210 |
|
---|
211 | # ifdef RT_OS_LINUX
|
---|
212 | /*
|
---|
213 | * And fork again to lose session leader status (non-standard daemon()
|
---|
214 | * behaviour).
|
---|
215 | */
|
---|
216 | pid = fork();
|
---|
217 | if (pid == -1)
|
---|
218 | return RTErrConvertFromErrno(errno);
|
---|
219 | if (pid != 0)
|
---|
220 | exit(0);
|
---|
221 | # endif /* RT_OS_LINUX */
|
---|
222 |
|
---|
223 | return VINF_SUCCESS;
|
---|
224 | #endif
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Creates a PID File and returns the open file descriptor.
|
---|
230 | *
|
---|
231 | * On DOS based system, file sharing (deny write) is used for locking the PID
|
---|
232 | * file.
|
---|
233 | *
|
---|
234 | * On Unix-y systems, an exclusive advisory lock is used for locking the PID
|
---|
235 | * file since the file sharing support is usually missing there.
|
---|
236 | *
|
---|
237 | * This API will overwrite any existing PID Files without a lock on them, on the
|
---|
238 | * assumption that they are stale files which an old process did not properly
|
---|
239 | * clean up.
|
---|
240 | *
|
---|
241 | * @returns IPRT status code.
|
---|
242 | * @param pszPath The path and filename to create the PID File under
|
---|
243 | * @param phFile Where to store the file descriptor of the open (and locked
|
---|
244 | * on Unix-y systems) PID File. On failure, or if another
|
---|
245 | * process owns the PID File, this will be set to NIL_RTFILE.
|
---|
246 | */
|
---|
247 | VBGLR3DECL(int) VbglR3PidFile(const char *pszPath, PRTFILE phFile)
|
---|
248 | {
|
---|
249 | AssertPtrReturn(pszPath, VERR_INVALID_PARAMETER);
|
---|
250 | AssertPtrReturn(phFile, VERR_INVALID_PARAMETER);
|
---|
251 | *phFile = NIL_RTFILE;
|
---|
252 |
|
---|
253 | RTFILE hPidFile;
|
---|
254 | int rc = RTFileOpen(&hPidFile, pszPath,
|
---|
255 | RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE
|
---|
256 | | (0644 << RTFILE_O_CREATE_MODE_SHIFT));
|
---|
257 | if (RT_SUCCESS(rc))
|
---|
258 | {
|
---|
259 | #if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
|
---|
260 | /** @todo using size 0 for locking means lock all on Posix.
|
---|
261 | * We should adopt this as our convention too, or something
|
---|
262 | * similar. */
|
---|
263 | rc = RTFileLock(hPidFile, RTFILE_LOCK_WRITE, 0, 0);
|
---|
264 | if (RT_FAILURE(rc))
|
---|
265 | RTFileClose(hPidFile);
|
---|
266 | else
|
---|
267 | #endif
|
---|
268 | {
|
---|
269 | char szBuf[256];
|
---|
270 | size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n",
|
---|
271 | RTProcSelf());
|
---|
272 | RTFileWrite(hPidFile, szBuf, cbPid, NULL);
|
---|
273 | *phFile = hPidFile;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | return rc;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Close and remove an open PID File.
|
---|
282 | *
|
---|
283 | * @param pszPath The path to the PID File,
|
---|
284 | * @param hFile The handle for the file. NIL_RTFILE is ignored as usual.
|
---|
285 | */
|
---|
286 | VBGLR3DECL(void) VbglR3ClosePidFile(const char *pszPath, RTFILE hFile)
|
---|
287 | {
|
---|
288 | AssertPtrReturnVoid(pszPath);
|
---|
289 | if (hFile != NIL_RTFILE)
|
---|
290 | {
|
---|
291 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
292 | RTFileWriteAt(hFile, 0, "-1", 2, NULL);
|
---|
293 | #else
|
---|
294 | RTFileDelete(pszPath);
|
---|
295 | #endif
|
---|
296 | RTFileClose(hFile);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|