VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/process-creation-posix.cpp@ 36366

Last change on this file since 36366 was 35558, checked in by vboxsync, 14 years ago

Runtime/process: add a Solaris-specific flag to suppress changing the contract id (not used at the moment), and put a note in the runtime that the webservice self-daemonizing code relies on the fact that RTProcDaemonizeUsingFork keeps the contract id unchanged, in case someone cleans this up

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette