1 | /* $Id: process-posix.cpp 15026 2008-12-05 09:43:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Process, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 | * 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 | /*******************************************************************************
|
---|
34 | * Header Files *
|
---|
35 | *******************************************************************************/
|
---|
36 | #define LOG_GROUP RTLOGGROUP_PROCESS
|
---|
37 | #include <unistd.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <sys/types.h>
|
---|
41 | #include <sys/stat.h>
|
---|
42 | #include <sys/wait.h>
|
---|
43 | #include <fcntl.h>
|
---|
44 | #include <signal.h>
|
---|
45 | #if defined(RT_OS_LINUX) || defined(RT_OS_OS2)
|
---|
46 | # define HAVE_POSIX_SPAWN 1
|
---|
47 | #endif
|
---|
48 | #ifdef HAVE_POSIX_SPAWN
|
---|
49 | # include <spawn.h>
|
---|
50 | #endif
|
---|
51 | #ifdef RT_OS_DARWIN
|
---|
52 | # include <mach-o/dyld.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #include <iprt/process.h>
|
---|
56 | #include <iprt/string.h>
|
---|
57 | #include <iprt/assert.h>
|
---|
58 | #include <iprt/err.h>
|
---|
59 | #include <iprt/env.h>
|
---|
60 | #include "internal/process.h"
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | RTR3DECL(int) RTProcCreate(const char *pszExec, const char * const *papszArgs, RTENV Env, unsigned fFlags, PRTPROCESS pProcess)
|
---|
65 | {
|
---|
66 | int rc;
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Validate input.
|
---|
70 | */
|
---|
71 | AssertPtrReturn(pszExec, VERR_INVALID_POINTER);
|
---|
72 | AssertReturn(*pszExec, VERR_INVALID_PARAMETER);
|
---|
73 | AssertReturn(!(fFlags & ~RTPROC_FLAGS_DAEMONIZE), VERR_INVALID_PARAMETER);
|
---|
74 | AssertReturn(Env != NIL_RTENV, VERR_INVALID_PARAMETER);
|
---|
75 | const char * const *papszEnv = RTEnvGetExecEnvP(Env);
|
---|
76 | AssertPtrReturn(papszEnv, VERR_INVALID_HANDLE);
|
---|
77 | /* later: path searching. */
|
---|
78 |
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Check for execute access to the file.
|
---|
82 | */
|
---|
83 | if (access(pszExec, X_OK))
|
---|
84 | {
|
---|
85 | rc = RTErrConvertFromErrno(errno);
|
---|
86 | AssertMsgFailed(("'%s' %Rrc!\n", pszExec, rc));
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 |
|
---|
90 | #if 0
|
---|
91 | /*
|
---|
92 | * Squeeze gdb --args in front of what's being spawned.
|
---|
93 | */
|
---|
94 | unsigned cArgs = 0;
|
---|
95 | while (papszArgs[cArgs])
|
---|
96 | cArgs++;
|
---|
97 | cArgs += 3;
|
---|
98 | const char **papszArgsTmp = (const char **)alloca(cArgs * sizeof(char *));
|
---|
99 | papszArgsTmp[0] = "/usr/bin/gdb";
|
---|
100 | papszArgsTmp[1] = "--args";
|
---|
101 | papszArgsTmp[2] = pszExec;
|
---|
102 | for (unsigned i = 1; papszArgs[i]; i++)
|
---|
103 | papszArgsTmp[i + 2] = papszArgs[i];
|
---|
104 | papszArgsTmp[cArgs - 1] = NULL;
|
---|
105 | pszExec = papszArgsTmp[0];
|
---|
106 | papszArgs = papszArgsTmp;
|
---|
107 | #endif
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Spawn the child.
|
---|
111 | */
|
---|
112 | pid_t pid;
|
---|
113 | #ifdef HAVE_POSIX_SPAWN
|
---|
114 | if (!(fFlags & RTPROC_FLAGS_DAEMONIZE))
|
---|
115 | {
|
---|
116 | /** @todo check if it requires any of those two attributes, don't remember atm. */
|
---|
117 | rc = posix_spawn(&pid, pszExec, NULL, NULL, (char * const *)papszArgs,
|
---|
118 | (char * const *)papszEnv);
|
---|
119 | if (!rc)
|
---|
120 | {
|
---|
121 | if (pProcess)
|
---|
122 | *pProcess = pid;
|
---|
123 | return VINF_SUCCESS;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | else
|
---|
127 | #endif
|
---|
128 | {
|
---|
129 | pid = fork();
|
---|
130 | if (!pid)
|
---|
131 | {
|
---|
132 | if (fFlags & RTPROC_FLAGS_DAEMONIZE)
|
---|
133 | {
|
---|
134 | rc = RTProcDaemonize(true /* fNoChDir */, false /* fNoClose */, NULL /* pszPidFile */);
|
---|
135 | AssertReleaseMsgFailed(("RTProcDaemonize returns %Rrc errno=%d\n", rc, errno));
|
---|
136 | exit(127);
|
---|
137 | }
|
---|
138 | rc = execve(pszExec, (char * const *)papszArgs, (char * const *)papszEnv);
|
---|
139 | AssertReleaseMsgFailed(("execve returns %d errno=%d\n", rc, errno));
|
---|
140 | exit(127);
|
---|
141 | }
|
---|
142 | if (pid > 0)
|
---|
143 | {
|
---|
144 | if (pProcess)
|
---|
145 | *pProcess = pid;
|
---|
146 | return VINF_SUCCESS;
|
---|
147 | }
|
---|
148 | rc = errno;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /* failure, errno value in rc. */
|
---|
152 | AssertMsgFailed(("spawn/exec failed rc=%d\n", rc)); /* this migth be annoying... */
|
---|
153 | return RTErrConvertFromErrno(rc);
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | RTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
|
---|
158 | {
|
---|
159 | int rc;
|
---|
160 | do rc = RTProcWaitNoResume(Process, fFlags, pProcStatus);
|
---|
161 | while (rc == VERR_INTERRUPTED);
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 | RTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
|
---|
166 | {
|
---|
167 | /*
|
---|
168 | * Validate input.
|
---|
169 | */
|
---|
170 | if (Process <= 0)
|
---|
171 | {
|
---|
172 | AssertMsgFailed(("Invalid Process=%d\n", Process));
|
---|
173 | return VERR_INVALID_PARAMETER;
|
---|
174 | }
|
---|
175 | if (fFlags & ~(RTPROCWAIT_FLAGS_NOBLOCK | RTPROCWAIT_FLAGS_BLOCK))
|
---|
176 | {
|
---|
177 | AssertMsgFailed(("Invalid flags %#x\n", fFlags));
|
---|
178 | return VERR_INVALID_PARAMETER;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Performe the wait.
|
---|
183 | */
|
---|
184 | int iStatus = 0;
|
---|
185 | int rc = waitpid(Process, &iStatus, fFlags & RTPROCWAIT_FLAGS_NOBLOCK ? WNOHANG : 0);
|
---|
186 | if (rc > 0)
|
---|
187 | {
|
---|
188 | /*
|
---|
189 | * Fill in the status structure.
|
---|
190 | */
|
---|
191 | if (pProcStatus)
|
---|
192 | {
|
---|
193 | if (WIFEXITED(iStatus))
|
---|
194 | {
|
---|
195 | pProcStatus->enmReason = RTPROCEXITREASON_NORMAL;
|
---|
196 | pProcStatus->iStatus = WEXITSTATUS(iStatus);
|
---|
197 | }
|
---|
198 | else if (WIFSIGNALED(iStatus))
|
---|
199 | {
|
---|
200 | pProcStatus->enmReason = RTPROCEXITREASON_SIGNAL;
|
---|
201 | pProcStatus->iStatus = WTERMSIG(iStatus);
|
---|
202 | }
|
---|
203 | else
|
---|
204 | {
|
---|
205 | Assert(!WIFSTOPPED(iStatus));
|
---|
206 | pProcStatus->enmReason = RTPROCEXITREASON_ABEND;
|
---|
207 | pProcStatus->iStatus = iStatus;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | return VINF_SUCCESS;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * Child running?
|
---|
215 | */
|
---|
216 | if (!rc)
|
---|
217 | {
|
---|
218 | Assert(fFlags & RTPROCWAIT_FLAGS_NOBLOCK);
|
---|
219 | return VERR_PROCESS_RUNNING;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * Figure out which error to return.
|
---|
224 | */
|
---|
225 | int iErr = errno;
|
---|
226 | if (iErr == ECHILD)
|
---|
227 | return VERR_PROCESS_NOT_FOUND;
|
---|
228 | return RTErrConvertFromErrno(iErr);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | RTR3DECL(int) RTProcTerminate(RTPROCESS Process)
|
---|
233 | {
|
---|
234 | if (!kill(Process, SIGKILL))
|
---|
235 | return VINF_SUCCESS;
|
---|
236 | return RTErrConvertFromErrno(errno);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | RTR3DECL(uint64_t) RTProcGetAffinityMask()
|
---|
241 | {
|
---|
242 | // @todo
|
---|
243 | return 1;
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Daemonize the current process, making it a background process. The current
|
---|
249 | * process will exit if daemonizing is successful.
|
---|
250 | *
|
---|
251 | * @returns iprt status code.
|
---|
252 | * @param fNoChDir Pass false to change working directory to "/".
|
---|
253 | * @param fNoClose Pass false to redirect standard file streams to the null device.
|
---|
254 | * @param pszPidfile Path to a file to write the process id of the daemon
|
---|
255 | * process to. Daemonizing will fail if this file already
|
---|
256 | * exists or cannot be written. May be NULL.
|
---|
257 | */
|
---|
258 | RTR3DECL(int) RTProcDaemonize(bool fNoChDir, bool fNoClose, const char *pszPidfile)
|
---|
259 | {
|
---|
260 | /*
|
---|
261 | * Fork the child process in a new session and quit the parent.
|
---|
262 | *
|
---|
263 | * - fork once and create a new session (setsid). This will detach us
|
---|
264 | * from the controlling tty meaning that we won't receive the SIGHUP
|
---|
265 | * (or any other signal) sent to that session.
|
---|
266 | * - The SIGHUP signal is ignored because the session/parent may throw
|
---|
267 | * us one before we get to the setsid.
|
---|
268 | * - When the parent exit(0) we will become an orphan and re-parented to
|
---|
269 | * the init process.
|
---|
270 | * - Because of the sometimes unexpected semantics of assigning the
|
---|
271 | * controlling tty automagically when a session leader first opens a tty,
|
---|
272 | * we will fork() once more to get rid of the session leadership role.
|
---|
273 | */
|
---|
274 |
|
---|
275 | /* We start off by opening the pidfile, so that we can fail straight away
|
---|
276 | * if it already exists. */
|
---|
277 | int fdPidfile = -1;
|
---|
278 | if (pszPidfile != NULL)
|
---|
279 | {
|
---|
280 | /* @note the exclusive create is not guaranteed on all file
|
---|
281 | * systems (e.g. NFSv2) */
|
---|
282 | if ((fdPidfile = open(pszPidfile, O_RDWR | O_CREAT | O_EXCL, 0644)) == -1)
|
---|
283 | return RTErrConvertFromErrno(errno);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /* Ignore SIGHUP straight away. */
|
---|
287 | struct sigaction OldSigAct;
|
---|
288 | struct sigaction SigAct;
|
---|
289 | memset(&SigAct, 0, sizeof(SigAct));
|
---|
290 | SigAct.sa_handler = SIG_IGN;
|
---|
291 | int rcSigAct = sigaction(SIGHUP, &SigAct, &OldSigAct);
|
---|
292 |
|
---|
293 | /* First fork, to become independent process. */
|
---|
294 | pid_t pid = fork();
|
---|
295 | if (pid == -1)
|
---|
296 | return RTErrConvertFromErrno(errno);
|
---|
297 | if (pid != 0)
|
---|
298 | {
|
---|
299 | /* Parent exits, no longer necessary. Child creates gets reparented
|
---|
300 | * to the init process. */
|
---|
301 | exit(0);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /* Create new session, fix up the standard file descriptors and the
|
---|
305 | * current working directory. */
|
---|
306 | pid_t newpgid = setsid();
|
---|
307 | int SavedErrno = errno;
|
---|
308 | if (rcSigAct != -1)
|
---|
309 | sigaction(SIGHUP, &OldSigAct, NULL);
|
---|
310 | if (newpgid == -1)
|
---|
311 | return RTErrConvertFromErrno(SavedErrno);
|
---|
312 |
|
---|
313 | if (!fNoClose)
|
---|
314 | {
|
---|
315 | /* Open stdin(0), stdout(1) and stderr(2) as /dev/null. */
|
---|
316 | int fd = open("/dev/null", O_RDWR);
|
---|
317 | if (fd == -1) /* paranoia */
|
---|
318 | {
|
---|
319 | close(STDIN_FILENO);
|
---|
320 | close(STDOUT_FILENO);
|
---|
321 | close(STDERR_FILENO);
|
---|
322 | fd = open("/dev/null", O_RDWR);
|
---|
323 | }
|
---|
324 | if (fd != -1)
|
---|
325 | {
|
---|
326 | dup2(fd, STDIN_FILENO);
|
---|
327 | dup2(fd, STDOUT_FILENO);
|
---|
328 | dup2(fd, STDERR_FILENO);
|
---|
329 | if (fd > 2)
|
---|
330 | close(fd);
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (!fNoChDir)
|
---|
335 | chdir("/");
|
---|
336 |
|
---|
337 | /* Second fork to lose session leader status. */
|
---|
338 | pid = fork();
|
---|
339 | if (pid == -1)
|
---|
340 | return RTErrConvertFromErrno(errno);
|
---|
341 | if (pid != 0)
|
---|
342 | {
|
---|
343 | /* Write the pid file, this is done in the parent, before exiting. */
|
---|
344 | if (fdPidfile != -1)
|
---|
345 | {
|
---|
346 | char szBuf[256];
|
---|
347 | size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", pid);
|
---|
348 | write(fdPidfile, szBuf, cbPid);
|
---|
349 | close(fdPidfile);
|
---|
350 | }
|
---|
351 | exit(0);
|
---|
352 | }
|
---|
353 |
|
---|
354 | return VINF_SUCCESS;
|
---|
355 | }
|
---|
356 |
|
---|