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