1 | /* $Id: process-posix.cpp 33009 2010-10-08 12:37:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Process, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 | /*******************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *******************************************************************************/
|
---|
32 | #define LOG_GROUP RTLOGGROUP_PROCESS
|
---|
33 | #include <unistd.h>
|
---|
34 | #include <stdlib.h>
|
---|
35 | #include <errno.h>
|
---|
36 | #include <sys/types.h>
|
---|
37 | #include <sys/stat.h>
|
---|
38 | #include <sys/wait.h>
|
---|
39 | #include <fcntl.h>
|
---|
40 | #include <signal.h>
|
---|
41 | #if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
|
---|
42 | # include <crypt.h>
|
---|
43 | # include <pwd.h>
|
---|
44 | # include <shadow.h>
|
---|
45 | #endif
|
---|
46 | #if defined(RT_OS_LINUX) || defined(RT_OS_OS2)
|
---|
47 | /* While Solaris has posix_spawn() of course we don't want to use it as
|
---|
48 | * we need to have the child in a different process contract, no matter
|
---|
49 | * whether it is started detached or not. */
|
---|
50 | # define HAVE_POSIX_SPAWN 1
|
---|
51 | #endif
|
---|
52 | #ifdef HAVE_POSIX_SPAWN
|
---|
53 | # include <spawn.h>
|
---|
54 | #endif
|
---|
55 | #ifdef RT_OS_DARWIN
|
---|
56 | # include <mach-o/dyld.h>
|
---|
57 | #endif
|
---|
58 | #ifdef RT_OS_SOLARIS
|
---|
59 | # include <limits.h>
|
---|
60 | # include <sys/ctfs.h>
|
---|
61 | # include <sys/contract/process.h>
|
---|
62 | # include <libcontract.h>
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | #include <iprt/process.h>
|
---|
66 | #include "internal/iprt.h"
|
---|
67 |
|
---|
68 | #include <iprt/assert.h>
|
---|
69 | #include <iprt/env.h>
|
---|
70 | #include <iprt/err.h>
|
---|
71 | #include <iprt/file.h>
|
---|
72 | #include <iprt/pipe.h>
|
---|
73 | #include <iprt/socket.h>
|
---|
74 | #include <iprt/string.h>
|
---|
75 | #include <iprt/mem.h>
|
---|
76 | #include "internal/process.h"
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Check the credentials and return the gid/uid of user.
|
---|
81 | *
|
---|
82 | * @param pszUser username
|
---|
83 | * @param pszPasswd password
|
---|
84 | * @param gid where to store the GID of the user
|
---|
85 | * @param uid where to store the UID of the user
|
---|
86 | * @returns IPRT status code
|
---|
87 | */
|
---|
88 | static int rtCheckCredentials(const char *pszUser, const char *pszPasswd, gid_t *gid, uid_t *uid)
|
---|
89 | {
|
---|
90 | #if defined(RT_OS_LINUX)
|
---|
91 | struct passwd *pw;
|
---|
92 |
|
---|
93 | pw = getpwnam(pszUser);
|
---|
94 | if (!pw)
|
---|
95 | return VERR_PERMISSION_DENIED;
|
---|
96 |
|
---|
97 | if (!pszPasswd)
|
---|
98 | pszPasswd = "";
|
---|
99 |
|
---|
100 | struct spwd *spwd;
|
---|
101 | /* works only if /etc/shadow is accessible */
|
---|
102 | spwd = getspnam(pszUser);
|
---|
103 | if (spwd)
|
---|
104 | pw->pw_passwd = spwd->sp_pwdp;
|
---|
105 |
|
---|
106 | /* be reentrant */
|
---|
107 | struct crypt_data *data = (struct crypt_data*)RTMemTmpAllocZ(sizeof(*data));
|
---|
108 | char *pszEncPasswd = crypt_r(pszPasswd, pw->pw_passwd, data);
|
---|
109 | if (strcmp(pszEncPasswd, pw->pw_passwd))
|
---|
110 | return VERR_PERMISSION_DENIED;
|
---|
111 | RTMemTmpFree(data);
|
---|
112 |
|
---|
113 | *gid = pw->pw_gid;
|
---|
114 | *uid = pw->pw_uid;
|
---|
115 | return VINF_SUCCESS;
|
---|
116 |
|
---|
117 | #elif defined(RT_OS_SOLARIS)
|
---|
118 | struct passwd *ppw, pw;
|
---|
119 | char szBuf[1024];
|
---|
120 |
|
---|
121 | if (getpwnam_r(pszUser, &pw, szBuf, sizeof(szBuf), &ppw) != 0 || ppw == NULL)
|
---|
122 | return VERR_PERMISSION_DENIED;
|
---|
123 |
|
---|
124 | if (!pszPasswd)
|
---|
125 | pszPasswd = "";
|
---|
126 |
|
---|
127 | struct spwd spwd;
|
---|
128 | char szPwdBuf[1024];
|
---|
129 | /* works only if /etc/shadow is accessible */
|
---|
130 | if (getspnam_r(pszUser, &spwd, szPwdBuf, sizeof(szPwdBuf)) != NULL)
|
---|
131 | ppw->pw_passwd = spwd.sp_pwdp;
|
---|
132 |
|
---|
133 | char *pszEncPasswd = crypt(pszPasswd, ppw->pw_passwd);
|
---|
134 | if (strcmp(pszEncPasswd, ppw->pw_passwd))
|
---|
135 | return VERR_PERMISSION_DENIED;
|
---|
136 |
|
---|
137 | *gid = ppw->pw_gid;
|
---|
138 | *uid = ppw->pw_uid;
|
---|
139 | return VINF_SUCCESS;
|
---|
140 |
|
---|
141 | #else
|
---|
142 | return VERR_PERMISSION_DENIED;
|
---|
143 | #endif
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | #ifdef RT_OS_SOLARIS
|
---|
148 | /** @todo the error reporting of the Solaris process contract code could be
|
---|
149 | * a lot better, but essentially it is not meant to run into errors after
|
---|
150 | * the debugging phase. */
|
---|
151 | static int rtSolarisContractPreFork(void)
|
---|
152 | {
|
---|
153 | int templateFd = open64(CTFS_ROOT "/process/template", O_RDWR);
|
---|
154 | if (templateFd < 0)
|
---|
155 | return -1;
|
---|
156 |
|
---|
157 | /* Set template parameters and event sets. */
|
---|
158 | if (ct_pr_tmpl_set_param(templateFd, CT_PR_PGRPONLY))
|
---|
159 | {
|
---|
160 | close(templateFd);
|
---|
161 | return -1;
|
---|
162 | }
|
---|
163 | if (ct_pr_tmpl_set_fatal(templateFd, CT_PR_EV_HWERR))
|
---|
164 | {
|
---|
165 | close(templateFd);
|
---|
166 | return -1;
|
---|
167 | }
|
---|
168 | if (ct_tmpl_set_critical(templateFd, 0))
|
---|
169 | {
|
---|
170 | close(templateFd);
|
---|
171 | return -1;
|
---|
172 | }
|
---|
173 | if (ct_tmpl_set_informative(templateFd, CT_PR_EV_HWERR))
|
---|
174 | {
|
---|
175 | close(templateFd);
|
---|
176 | return -1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /* Make this the active template for the process. */
|
---|
180 | if (ct_tmpl_activate(templateFd))
|
---|
181 | {
|
---|
182 | close(templateFd);
|
---|
183 | return -1;
|
---|
184 | }
|
---|
185 |
|
---|
186 | return templateFd;
|
---|
187 | }
|
---|
188 |
|
---|
189 | static void rtSolarisContractPostForkChild(int templateFd)
|
---|
190 | {
|
---|
191 | if (templateFd == -1)
|
---|
192 | return;
|
---|
193 |
|
---|
194 | /* Clear the active template. */
|
---|
195 | ct_tmpl_clear(templateFd);
|
---|
196 | close(templateFd);
|
---|
197 | }
|
---|
198 |
|
---|
199 | static void rtSolarisContractPostForkParent(int templateFd, pid_t pid)
|
---|
200 | {
|
---|
201 | if (templateFd == -1)
|
---|
202 | return;
|
---|
203 |
|
---|
204 | /* Clear the active template. */
|
---|
205 | int cleared = ct_tmpl_clear(templateFd);
|
---|
206 | close(templateFd);
|
---|
207 |
|
---|
208 | /* If the clearing failed or the fork failed there's nothing more to do. */
|
---|
209 | if (cleared || pid <= 0)
|
---|
210 | return;
|
---|
211 |
|
---|
212 | /* Look up the contract which was created by this thread. */
|
---|
213 | int statFd = open64(CTFS_ROOT "/process/latest", O_RDONLY);
|
---|
214 | if (statFd == -1)
|
---|
215 | return;
|
---|
216 | ct_stathdl_t statHdl;
|
---|
217 | if (ct_status_read(statFd, CTD_COMMON, &statHdl))
|
---|
218 | {
|
---|
219 | close(statFd);
|
---|
220 | return;
|
---|
221 | }
|
---|
222 | ctid_t ctId = ct_status_get_id(statHdl);
|
---|
223 | ct_status_free(statHdl);
|
---|
224 | close(statFd);
|
---|
225 | if (ctId < 0)
|
---|
226 | return;
|
---|
227 |
|
---|
228 | /* Abandon this contract we just created. */
|
---|
229 | char ctlPath[PATH_MAX];
|
---|
230 | size_t len = snprintf(ctlPath, sizeof(ctlPath),
|
---|
231 | CTFS_ROOT "/process/%d/ctl", ctId);
|
---|
232 | if (len >= sizeof(ctlPath))
|
---|
233 | return;
|
---|
234 | int ctlFd = open64(ctlPath, O_WRONLY);
|
---|
235 | if (statFd == -1)
|
---|
236 | return;
|
---|
237 | if (ct_ctl_abandon(ctlFd) < 0)
|
---|
238 | {
|
---|
239 | close(ctlFd);
|
---|
240 | return;
|
---|
241 | }
|
---|
242 | close(ctlFd);
|
---|
243 | }
|
---|
244 |
|
---|
245 | #endif /* RT_OS_SOLARIS */
|
---|
246 |
|
---|
247 |
|
---|
248 | RTR3DECL(int) RTProcCreate(const char *pszExec, const char * const *papszArgs, RTENV Env, unsigned fFlags, PRTPROCESS pProcess)
|
---|
249 | {
|
---|
250 | return RTProcCreateEx(pszExec, papszArgs, Env, fFlags,
|
---|
251 | NULL, NULL, NULL, /* standard handles */
|
---|
252 | NULL /*pszAsUser*/, NULL /* pszPassword*/,
|
---|
253 | pProcess);
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | RTR3DECL(int) RTProcCreateEx(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
|
---|
258 | PCRTHANDLE phStdIn, PCRTHANDLE phStdOut, PCRTHANDLE phStdErr, const char *pszAsUser,
|
---|
259 | const char *pszPassword, PRTPROCESS phProcess)
|
---|
260 | {
|
---|
261 | int rc;
|
---|
262 |
|
---|
263 | /*
|
---|
264 | * Input validation
|
---|
265 | */
|
---|
266 | AssertPtrReturn(pszExec, VERR_INVALID_POINTER);
|
---|
267 | AssertReturn(*pszExec, VERR_INVALID_PARAMETER);
|
---|
268 | AssertReturn(!(fFlags & ~(RTPROC_FLAGS_DAEMONIZE_DEPRECATED | RTPROC_FLAGS_DETACHED | RTPROC_FLAGS_SERVICE)), VERR_INVALID_PARAMETER);
|
---|
269 | AssertReturn(!(fFlags & RTPROC_FLAGS_DETACHED) || !phProcess, VERR_INVALID_PARAMETER);
|
---|
270 | AssertReturn(hEnv != NIL_RTENV, VERR_INVALID_PARAMETER);
|
---|
271 | const char * const *papszEnv = RTEnvGetExecEnvP(hEnv);
|
---|
272 | AssertPtrReturn(papszEnv, VERR_INVALID_HANDLE);
|
---|
273 | AssertPtrReturn(papszArgs, VERR_INVALID_PARAMETER);
|
---|
274 | /** @todo search the PATH (add flag for this). */
|
---|
275 | AssertPtrNullReturn(pszAsUser, VERR_INVALID_POINTER);
|
---|
276 | AssertReturn(!pszAsUser || *pszAsUser, VERR_INVALID_PARAMETER);
|
---|
277 | AssertReturn(!pszPassword || pszAsUser, VERR_INVALID_PARAMETER);
|
---|
278 | AssertPtrNullReturn(pszPassword, VERR_INVALID_POINTER);
|
---|
279 |
|
---|
280 | /*
|
---|
281 | * Get the file descriptors for the handles we've been passed.
|
---|
282 | */
|
---|
283 | PCRTHANDLE paHandles[3] = { phStdIn, phStdOut, phStdErr };
|
---|
284 | int aStdFds[3] = { -1, -1, -1 };
|
---|
285 | for (int i = 0; i < 3; i++)
|
---|
286 | {
|
---|
287 | if (paHandles[i])
|
---|
288 | {
|
---|
289 | AssertPtrReturn(paHandles[i], VERR_INVALID_POINTER);
|
---|
290 | switch (paHandles[i]->enmType)
|
---|
291 | {
|
---|
292 | case RTHANDLETYPE_FILE:
|
---|
293 | aStdFds[i] = paHandles[i]->u.hFile != NIL_RTFILE
|
---|
294 | ? (int)RTFileToNative(paHandles[i]->u.hFile)
|
---|
295 | : -2 /* close it */;
|
---|
296 | break;
|
---|
297 |
|
---|
298 | case RTHANDLETYPE_PIPE:
|
---|
299 | aStdFds[i] = paHandles[i]->u.hPipe != NIL_RTPIPE
|
---|
300 | ? (int)RTPipeToNative(paHandles[i]->u.hPipe)
|
---|
301 | : -2 /* close it */;
|
---|
302 | break;
|
---|
303 |
|
---|
304 | case RTHANDLETYPE_SOCKET:
|
---|
305 | aStdFds[i] = paHandles[i]->u.hSocket != NIL_RTSOCKET
|
---|
306 | ? (int)RTSocketToNative(paHandles[i]->u.hSocket)
|
---|
307 | : -2 /* close it */;
|
---|
308 | break;
|
---|
309 |
|
---|
310 | default:
|
---|
311 | AssertMsgFailedReturn(("%d: %d\n", i, paHandles[i]->enmType), VERR_INVALID_PARAMETER);
|
---|
312 | }
|
---|
313 | /** @todo check the close-on-execness of these handles? */
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | for (int i = 0; i < 3; i++)
|
---|
318 | if (aStdFds[i] == i)
|
---|
319 | aStdFds[i] = -1;
|
---|
320 |
|
---|
321 | for (int i = 0; i < 3; i++)
|
---|
322 | AssertMsgReturn(aStdFds[i] < 0 || aStdFds[i] > i,
|
---|
323 | ("%i := %i not possible because we're lazy\n", i, aStdFds[i]),
|
---|
324 | VERR_NOT_SUPPORTED);
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * Resolve the user id if specified.
|
---|
328 | */
|
---|
329 | uid_t uid = ~(uid_t)0;
|
---|
330 | gid_t gid = ~(gid_t)0;
|
---|
331 | if (pszAsUser)
|
---|
332 | {
|
---|
333 | rc = rtCheckCredentials(pszAsUser, pszPassword, &gid, &uid);
|
---|
334 | if (RT_FAILURE(rc))
|
---|
335 | return rc;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /*
|
---|
339 | * Check for execute access to the file.
|
---|
340 | */
|
---|
341 | if (access(pszExec, X_OK))
|
---|
342 | {
|
---|
343 | rc = RTErrConvertFromErrno(errno);
|
---|
344 | AssertMsgFailed(("'%s' %Rrc!\n", pszExec, rc));
|
---|
345 | return rc;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /*
|
---|
349 | * Spawn the child.
|
---|
350 | *
|
---|
351 | * HACK ALERT! Put the process into a new process group with pgid = pid
|
---|
352 | * to make sure it differs from that of the parent process to ensure that
|
---|
353 | * the IPRT waipit call doesn't race anyone (read XPCOM) doing group wide
|
---|
354 | * waits.
|
---|
355 | */
|
---|
356 | pid_t pid = -1;
|
---|
357 | #ifdef HAVE_POSIX_SPAWN
|
---|
358 | /** @todo OS/2: implement DETACHED (BACKGROUND stuff), see VbglR3Daemonize. */
|
---|
359 | /** @todo Try do the detach thing with posix spawn. */
|
---|
360 | if ( !(fFlags & (RTPROC_FLAGS_DAEMONIZE_DEPRECATED | RTPROC_FLAGS_DETACHED))
|
---|
361 | && uid == ~(uid_t)0
|
---|
362 | && gid == ~(gid_t)0
|
---|
363 | )
|
---|
364 | {
|
---|
365 | /* Spawn attributes. */
|
---|
366 | posix_spawnattr_t Attr;
|
---|
367 | rc = posix_spawnattr_init(&Attr);
|
---|
368 | if (!rc)
|
---|
369 | {
|
---|
370 | # ifndef RT_OS_OS2 /* We don't need this on OS/2 and I don't recall if it's actually implemented. */
|
---|
371 | rc = posix_spawnattr_setflags(&Attr, POSIX_SPAWN_SETPGROUP);
|
---|
372 | Assert(rc == 0);
|
---|
373 | if (!rc)
|
---|
374 | {
|
---|
375 | rc = posix_spawnattr_setpgroup(&Attr, 0 /* pg == child pid */);
|
---|
376 | Assert(rc == 0);
|
---|
377 | }
|
---|
378 | # endif
|
---|
379 |
|
---|
380 | /* File changes. */
|
---|
381 | posix_spawn_file_actions_t FileActions;
|
---|
382 | posix_spawn_file_actions_t *pFileActions = NULL;
|
---|
383 | if (aStdFds[0] != -1 || aStdFds[1] != -1 || aStdFds[2] != -1)
|
---|
384 | {
|
---|
385 | rc = posix_spawn_file_actions_init(&FileActions);
|
---|
386 | if (!rc)
|
---|
387 | {
|
---|
388 | pFileActions = &FileActions;
|
---|
389 | for (int i = 0; i < 3; i++)
|
---|
390 | {
|
---|
391 | int fd = aStdFds[i];
|
---|
392 | if (fd == -2)
|
---|
393 | rc = posix_spawn_file_actions_addclose(&FileActions, i);
|
---|
394 | else if (fd >= 0 && fd != i)
|
---|
395 | {
|
---|
396 | rc = posix_spawn_file_actions_adddup2(&FileActions, fd, i);
|
---|
397 | if (!rc)
|
---|
398 | {
|
---|
399 | for (int j = i + 1; j < 3; j++)
|
---|
400 | if (aStdFds[j] == fd)
|
---|
401 | {
|
---|
402 | fd = -1;
|
---|
403 | break;
|
---|
404 | }
|
---|
405 | if (fd >= 0)
|
---|
406 | rc = posix_spawn_file_actions_addclose(&FileActions, fd);
|
---|
407 | }
|
---|
408 | }
|
---|
409 | if (rc)
|
---|
410 | break;
|
---|
411 | }
|
---|
412 | }
|
---|
413 | }
|
---|
414 |
|
---|
415 | if (!rc)
|
---|
416 | rc = posix_spawn(&pid, pszExec, pFileActions, &Attr, (char * const *)papszArgs,
|
---|
417 | (char * const *)papszEnv);
|
---|
418 |
|
---|
419 | /* cleanup */
|
---|
420 | int rc2 = posix_spawnattr_destroy(&Attr); Assert(rc2 == 0); NOREF(rc2);
|
---|
421 | if (pFileActions)
|
---|
422 | {
|
---|
423 | rc2 = posix_spawn_file_actions_destroy(pFileActions);
|
---|
424 | Assert(rc2 == 0);
|
---|
425 | }
|
---|
426 |
|
---|
427 | /* return on success.*/
|
---|
428 | if (!rc)
|
---|
429 | {
|
---|
430 | if (phProcess)
|
---|
431 | *phProcess = pid;
|
---|
432 | return VINF_SUCCESS;
|
---|
433 | }
|
---|
434 | }
|
---|
435 | }
|
---|
436 | else
|
---|
437 | #endif
|
---|
438 | {
|
---|
439 | #ifdef RT_OS_SOLARIS
|
---|
440 | int templateFd = rtSolarisContractPreFork();
|
---|
441 | if (templateFd == -1)
|
---|
442 | return VERR_OPEN_FAILED;
|
---|
443 | #endif /* RT_OS_SOLARIS */
|
---|
444 | pid = fork();
|
---|
445 | if (!pid)
|
---|
446 | {
|
---|
447 | #ifdef RT_OS_SOLARIS
|
---|
448 | rtSolarisContractPostForkChild(templateFd);
|
---|
449 | #endif /* RT_OS_SOLARIS */
|
---|
450 | setpgid(0, 0); /* see comment above */
|
---|
451 |
|
---|
452 | /*
|
---|
453 | * Change group and user if requested.
|
---|
454 | */
|
---|
455 | #if 1 /** @todo This needs more work, see suplib/hardening. */
|
---|
456 | if (gid != ~(gid_t)0)
|
---|
457 | {
|
---|
458 | if (setgid(gid))
|
---|
459 | exit(126);
|
---|
460 | }
|
---|
461 |
|
---|
462 | if (uid != ~(uid_t)0)
|
---|
463 | {
|
---|
464 | if (setuid(uid))
|
---|
465 | exit(126);
|
---|
466 | }
|
---|
467 | #endif
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * Apply changes to the standard file descriptor and stuff.
|
---|
471 | */
|
---|
472 | for (int i = 0; i < 3; i++)
|
---|
473 | {
|
---|
474 | int fd = aStdFds[i];
|
---|
475 | if (fd == -2)
|
---|
476 | close(i);
|
---|
477 | else if (fd >= 0)
|
---|
478 | {
|
---|
479 | int rc2 = dup2(fd, i);
|
---|
480 | if (rc2 != i)
|
---|
481 | exit(125);
|
---|
482 | for (int j = i + 1; j < 3; j++)
|
---|
483 | if (aStdFds[j] == fd)
|
---|
484 | {
|
---|
485 | fd = -1;
|
---|
486 | break;
|
---|
487 | }
|
---|
488 | if (fd >= 0)
|
---|
489 | close(fd);
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | /*
|
---|
494 | * Daemonize the process if requested.
|
---|
495 | */
|
---|
496 | if (fFlags & (RTPROC_FLAGS_DAEMONIZE_DEPRECATED | RTPROC_FLAGS_DETACHED))
|
---|
497 | {
|
---|
498 | rc = RTProcDaemonizeUsingFork(true /*fNoChDir*/,
|
---|
499 | !(fFlags & RTPROC_FLAGS_DAEMONIZE_DEPRECATED) /*fNoClose*/,
|
---|
500 | NULL /* pszPidFile */);
|
---|
501 | if (RT_FAILURE(rc))
|
---|
502 | {
|
---|
503 | /* parent */
|
---|
504 | AssertReleaseMsgFailed(("RTProcDaemonize returns %Rrc errno=%d\n", rc, errno));
|
---|
505 | exit(127);
|
---|
506 | }
|
---|
507 | /* daemonized child */
|
---|
508 | }
|
---|
509 |
|
---|
510 | /*
|
---|
511 | * Finally, execute the requested program.
|
---|
512 | */
|
---|
513 | rc = execve(pszExec, (char * const *)papszArgs, (char * const *)papszEnv);
|
---|
514 | if (errno == ENOEXEC)
|
---|
515 | {
|
---|
516 | /* This can happen when trying to start a shell script without the magic #!/bin/sh */
|
---|
517 | RTAssertMsg2Weak("Cannot execute this binary format!\n");
|
---|
518 | }
|
---|
519 | else
|
---|
520 | RTAssertMsg2Weak("execve returns %d errno=%d\n", rc, errno);
|
---|
521 | RTAssertReleasePanic();
|
---|
522 | exit(127);
|
---|
523 | }
|
---|
524 | #ifdef RT_OS_SOLARIS
|
---|
525 | rtSolarisContractPostForkParent(templateFd, pid);
|
---|
526 | #endif /* RT_OS_SOLARIS */
|
---|
527 | if (pid > 0)
|
---|
528 | {
|
---|
529 | if (phProcess)
|
---|
530 | *phProcess = pid;
|
---|
531 | else if (fFlags & (RTPROC_FLAGS_DAEMONIZE_DEPRECATED | RTPROC_FLAGS_DETACHED))
|
---|
532 | {
|
---|
533 | /* If the process is detached straight away wait for the
|
---|
534 | * intermediate process to exit (it should do that quickly)
|
---|
535 | * if the caller didn't want to know the PID. If it wants the
|
---|
536 | * PID it's his job to wait for it or he gets a zombie. */
|
---|
537 | waitpid(pid, NULL, 0);
|
---|
538 | }
|
---|
539 | return VINF_SUCCESS;
|
---|
540 | }
|
---|
541 | rc = errno;
|
---|
542 | }
|
---|
543 |
|
---|
544 |
|
---|
545 | return VERR_NOT_IMPLEMENTED;
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 | RTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
|
---|
550 | {
|
---|
551 | int rc;
|
---|
552 | do rc = RTProcWaitNoResume(Process, fFlags, pProcStatus);
|
---|
553 | while (rc == VERR_INTERRUPTED);
|
---|
554 | return rc;
|
---|
555 | }
|
---|
556 |
|
---|
557 | RTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
|
---|
558 | {
|
---|
559 | /*
|
---|
560 | * Validate input.
|
---|
561 | */
|
---|
562 | if (Process <= 0)
|
---|
563 | {
|
---|
564 | AssertMsgFailed(("Invalid Process=%d\n", Process));
|
---|
565 | return VERR_INVALID_PARAMETER;
|
---|
566 | }
|
---|
567 | if (fFlags & ~(RTPROCWAIT_FLAGS_NOBLOCK | RTPROCWAIT_FLAGS_BLOCK))
|
---|
568 | {
|
---|
569 | AssertMsgFailed(("Invalid flags %#x\n", fFlags));
|
---|
570 | return VERR_INVALID_PARAMETER;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Perform the wait.
|
---|
575 | */
|
---|
576 | int iStatus = 0;
|
---|
577 | int rc = waitpid(Process, &iStatus, fFlags & RTPROCWAIT_FLAGS_NOBLOCK ? WNOHANG : 0);
|
---|
578 | if (rc > 0)
|
---|
579 | {
|
---|
580 | /*
|
---|
581 | * Fill in the status structure.
|
---|
582 | */
|
---|
583 | if (pProcStatus)
|
---|
584 | {
|
---|
585 | if (WIFEXITED(iStatus))
|
---|
586 | {
|
---|
587 | pProcStatus->enmReason = RTPROCEXITREASON_NORMAL;
|
---|
588 | pProcStatus->iStatus = WEXITSTATUS(iStatus);
|
---|
589 | }
|
---|
590 | else if (WIFSIGNALED(iStatus))
|
---|
591 | {
|
---|
592 | pProcStatus->enmReason = RTPROCEXITREASON_SIGNAL;
|
---|
593 | pProcStatus->iStatus = WTERMSIG(iStatus);
|
---|
594 | }
|
---|
595 | else
|
---|
596 | {
|
---|
597 | Assert(!WIFSTOPPED(iStatus));
|
---|
598 | pProcStatus->enmReason = RTPROCEXITREASON_ABEND;
|
---|
599 | pProcStatus->iStatus = iStatus;
|
---|
600 | }
|
---|
601 | }
|
---|
602 | return VINF_SUCCESS;
|
---|
603 | }
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * Child running?
|
---|
607 | */
|
---|
608 | if (!rc)
|
---|
609 | {
|
---|
610 | Assert(fFlags & RTPROCWAIT_FLAGS_NOBLOCK);
|
---|
611 | return VERR_PROCESS_RUNNING;
|
---|
612 | }
|
---|
613 |
|
---|
614 | /*
|
---|
615 | * Figure out which error to return.
|
---|
616 | */
|
---|
617 | int iErr = errno;
|
---|
618 | if (iErr == ECHILD)
|
---|
619 | return VERR_PROCESS_NOT_FOUND;
|
---|
620 | return RTErrConvertFromErrno(iErr);
|
---|
621 | }
|
---|
622 |
|
---|
623 |
|
---|
624 | RTR3DECL(int) RTProcTerminate(RTPROCESS Process)
|
---|
625 | {
|
---|
626 | if (!kill(Process, SIGKILL))
|
---|
627 | return VINF_SUCCESS;
|
---|
628 | return RTErrConvertFromErrno(errno);
|
---|
629 | }
|
---|
630 |
|
---|
631 |
|
---|
632 | RTR3DECL(uint64_t) RTProcGetAffinityMask()
|
---|
633 | {
|
---|
634 | // @todo
|
---|
635 | return 1;
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|
639 | RTR3DECL(int) RTProcDaemonizeUsingFork(bool fNoChDir, bool fNoClose, const char *pszPidfile)
|
---|
640 | {
|
---|
641 | /*
|
---|
642 | * Fork the child process in a new session and quit the parent.
|
---|
643 | *
|
---|
644 | * - fork once and create a new session (setsid). This will detach us
|
---|
645 | * from the controlling tty meaning that we won't receive the SIGHUP
|
---|
646 | * (or any other signal) sent to that session.
|
---|
647 | * - The SIGHUP signal is ignored because the session/parent may throw
|
---|
648 | * us one before we get to the setsid.
|
---|
649 | * - When the parent exit(0) we will become an orphan and re-parented to
|
---|
650 | * the init process.
|
---|
651 | * - Because of the sometimes unexpected semantics of assigning the
|
---|
652 | * controlling tty automagically when a session leader first opens a tty,
|
---|
653 | * we will fork() once more to get rid of the session leadership role.
|
---|
654 | */
|
---|
655 |
|
---|
656 | /* We start off by opening the pidfile, so that we can fail straight away
|
---|
657 | * if it already exists. */
|
---|
658 | int fdPidfile = -1;
|
---|
659 | if (pszPidfile != NULL)
|
---|
660 | {
|
---|
661 | /* @note the exclusive create is not guaranteed on all file
|
---|
662 | * systems (e.g. NFSv2) */
|
---|
663 | if ((fdPidfile = open(pszPidfile, O_RDWR | O_CREAT | O_EXCL, 0644)) == -1)
|
---|
664 | return RTErrConvertFromErrno(errno);
|
---|
665 | }
|
---|
666 |
|
---|
667 | /* Ignore SIGHUP straight away. */
|
---|
668 | struct sigaction OldSigAct;
|
---|
669 | struct sigaction SigAct;
|
---|
670 | memset(&SigAct, 0, sizeof(SigAct));
|
---|
671 | SigAct.sa_handler = SIG_IGN;
|
---|
672 | int rcSigAct = sigaction(SIGHUP, &SigAct, &OldSigAct);
|
---|
673 |
|
---|
674 | /* First fork, to become independent process. */
|
---|
675 | pid_t pid = fork();
|
---|
676 | if (pid == -1)
|
---|
677 | return RTErrConvertFromErrno(errno);
|
---|
678 | if (pid != 0)
|
---|
679 | {
|
---|
680 | /* Parent exits, no longer necessary. The child gets reparented
|
---|
681 | * to the init process. */
|
---|
682 | exit(0);
|
---|
683 | }
|
---|
684 |
|
---|
685 | /* Create new session, fix up the standard file descriptors and the
|
---|
686 | * current working directory. */
|
---|
687 | pid_t newpgid = setsid();
|
---|
688 | int SavedErrno = errno;
|
---|
689 | if (rcSigAct != -1)
|
---|
690 | sigaction(SIGHUP, &OldSigAct, NULL);
|
---|
691 | if (newpgid == -1)
|
---|
692 | return RTErrConvertFromErrno(SavedErrno);
|
---|
693 |
|
---|
694 | if (!fNoClose)
|
---|
695 | {
|
---|
696 | /* Open stdin(0), stdout(1) and stderr(2) as /dev/null. */
|
---|
697 | int fd = open("/dev/null", O_RDWR);
|
---|
698 | if (fd == -1) /* paranoia */
|
---|
699 | {
|
---|
700 | close(STDIN_FILENO);
|
---|
701 | close(STDOUT_FILENO);
|
---|
702 | close(STDERR_FILENO);
|
---|
703 | fd = open("/dev/null", O_RDWR);
|
---|
704 | }
|
---|
705 | if (fd != -1)
|
---|
706 | {
|
---|
707 | dup2(fd, STDIN_FILENO);
|
---|
708 | dup2(fd, STDOUT_FILENO);
|
---|
709 | dup2(fd, STDERR_FILENO);
|
---|
710 | if (fd > 2)
|
---|
711 | close(fd);
|
---|
712 | }
|
---|
713 | }
|
---|
714 |
|
---|
715 | if (!fNoChDir)
|
---|
716 | {
|
---|
717 | int rcChdir = chdir("/");
|
---|
718 | }
|
---|
719 |
|
---|
720 | /* Second fork to lose session leader status. */
|
---|
721 | pid = fork();
|
---|
722 | if (pid == -1)
|
---|
723 | return RTErrConvertFromErrno(errno);
|
---|
724 |
|
---|
725 | if (pid != 0)
|
---|
726 | {
|
---|
727 | /* Write the pid file, this is done in the parent, before exiting. */
|
---|
728 | if (fdPidfile != -1)
|
---|
729 | {
|
---|
730 | char szBuf[256];
|
---|
731 | size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", pid);
|
---|
732 | int rcWrite = write(fdPidfile, szBuf, cbPid);
|
---|
733 | close(fdPidfile);
|
---|
734 | }
|
---|
735 | exit(0);
|
---|
736 | }
|
---|
737 |
|
---|
738 | return VINF_SUCCESS;
|
---|
739 | }
|
---|
740 |
|
---|