VirtualBox

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

Last change on this file since 39032 was 39032, checked in by vboxsync, 13 years ago

IPRT: Fixed unused variable warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 22.9 KB
Line 
1/* $Id: process-creation-posix.cpp 39032 2011-10-19 11:08:50Z 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 int fCorrect = !strcmp(pszEncPasswd, pw->pw_passwd);
109 RTMemTmpFree(data);
110 if (!fCorrect)
111 return VERR_PERMISSION_DENIED;
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. */
151static 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
189static 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
199static 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
248RTR3DECL(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
257RTR3DECL(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_DETACHED | RTPROC_FLAGS_HIDDEN | RTPROC_FLAGS_SERVICE | RTPROC_FLAGS_SAME_CONTRACT | RTPROC_FLAGS_NO_PROFILE)), 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 return RTErrConvertFromErrno(errno);
343
344 pid_t pid = -1;
345
346 /*
347 * Take care of detaching the process.
348 *
349 * HACK ALERT! Put the process into a new process group with pgid = pid
350 * to make sure it differs from that of the parent process to ensure that
351 * the IPRT waitpid call doesn't race anyone (read XPCOM) doing group wide
352 * waits. setsid() includes the setpgid() functionality.
353 * 2010-10-11 XPCOM no longer waits for anything, but it cannot hurt.
354 */
355#ifndef RT_OS_OS2
356 if (fFlags & RTPROC_FLAGS_DETACHED)
357 {
358# ifdef RT_OS_SOLARIS
359 int templateFd = -1;
360 if (!(fFlags & RTPROC_FLAGS_SAME_CONTRACT))
361 {
362 templateFd = rtSolarisContractPreFork();
363 if (templateFd == -1)
364 return VERR_OPEN_FAILED;
365 }
366# endif /* RT_OS_SOLARIS */
367 pid = fork();
368 if (!pid)
369 {
370# ifdef RT_OS_SOLARIS
371 if (!(fFlags & RTPROC_FLAGS_SAME_CONTRACT))
372 rtSolarisContractPostForkChild(templateFd);
373# endif /* RT_OS_SOLARIS */
374 setsid(); /* see comment above */
375
376 pid = -1;
377 /* Child falls through to the actual spawn code below. */
378 }
379 else
380 {
381#ifdef RT_OS_SOLARIS
382 if (!(fFlags & RTPROC_FLAGS_SAME_CONTRACT))
383 rtSolarisContractPostForkParent(templateFd, pid);
384#endif /* RT_OS_SOLARIS */
385 if (pid > 0)
386 {
387 /* Must wait for the temporary process to avoid a zombie. */
388 int status = 0;
389 pid_t pidChild = 0;
390
391 /* Restart if we get interrupted. */
392 do
393 {
394 pidChild = waitpid(pid, &status, 0);
395 } while ( pidChild == -1
396 && errno == EINTR);
397
398 /* Assume that something wasn't found. No detailed info. */
399 if (status)
400 return VERR_PROCESS_NOT_FOUND;
401 if (phProcess)
402 *phProcess = 0;
403 return VINF_SUCCESS;
404 }
405 return RTErrConvertFromErrno(errno);
406 }
407 }
408#endif
409
410 /*
411 * Spawn the child.
412 *
413 * Any spawn code MUST not execute any atexit functions if it is for a
414 * detached process. It would lead to running the atexit functions which
415 * make only sense for the parent. libORBit e.g. gets confused by multiple
416 * execution. Remember, there was only a fork() so far, and until exec()
417 * is successfully run there is nothing which would prevent doing anything
418 * silly with the (duplicated) file descriptors.
419 */
420#ifdef HAVE_POSIX_SPAWN
421 /** @todo OS/2: implement DETACHED (BACKGROUND stuff), see VbglR3Daemonize. */
422 if ( uid == ~(uid_t)0
423 && gid == ~(gid_t)0)
424 {
425 /* Spawn attributes. */
426 posix_spawnattr_t Attr;
427 rc = posix_spawnattr_init(&Attr);
428 if (!rc)
429 {
430# ifndef RT_OS_OS2 /* We don't need this on OS/2 and I don't recall if it's actually implemented. */
431 rc = posix_spawnattr_setflags(&Attr, POSIX_SPAWN_SETPGROUP);
432 Assert(rc == 0);
433 if (!rc)
434 {
435 rc = posix_spawnattr_setpgroup(&Attr, 0 /* pg == child pid */);
436 Assert(rc == 0);
437 }
438# endif
439
440 /* File changes. */
441 posix_spawn_file_actions_t FileActions;
442 posix_spawn_file_actions_t *pFileActions = NULL;
443 if (aStdFds[0] != -1 || aStdFds[1] != -1 || aStdFds[2] != -1)
444 {
445 rc = posix_spawn_file_actions_init(&FileActions);
446 if (!rc)
447 {
448 pFileActions = &FileActions;
449 for (int i = 0; i < 3; i++)
450 {
451 int fd = aStdFds[i];
452 if (fd == -2)
453 rc = posix_spawn_file_actions_addclose(&FileActions, i);
454 else if (fd >= 0 && fd != i)
455 {
456 rc = posix_spawn_file_actions_adddup2(&FileActions, fd, i);
457 if (!rc)
458 {
459 for (int j = i + 1; j < 3; j++)
460 if (aStdFds[j] == fd)
461 {
462 fd = -1;
463 break;
464 }
465 if (fd >= 0)
466 rc = posix_spawn_file_actions_addclose(&FileActions, fd);
467 }
468 }
469 if (rc)
470 break;
471 }
472 }
473 }
474
475 if (!rc)
476 rc = posix_spawn(&pid, pszExec, pFileActions, &Attr, (char * const *)papszArgs,
477 (char * const *)papszEnv);
478
479 /* cleanup */
480 int rc2 = posix_spawnattr_destroy(&Attr); Assert(rc2 == 0); NOREF(rc2);
481 if (pFileActions)
482 {
483 rc2 = posix_spawn_file_actions_destroy(pFileActions);
484 Assert(rc2 == 0);
485 }
486
487 /* return on success.*/
488 if (!rc)
489 {
490 /* For a detached process this happens in the temp process, so
491 * it's not worth doing anything as this process must exit. */
492 if (fFlags & RTPROC_FLAGS_DETACHED)
493 _Exit(0);
494 if (phProcess)
495 *phProcess = pid;
496 return VINF_SUCCESS;
497 }
498 }
499 /* For a detached process this happens in the temp process, so
500 * it's not worth doing anything as this process must exit. */
501 if (fFlags & RTPROC_FLAGS_DETACHED)
502 _Exit(124);
503 }
504 else
505#endif
506 {
507#ifdef RT_OS_SOLARIS
508 int templateFd = rtSolarisContractPreFork();
509 if (templateFd == -1)
510 return VERR_OPEN_FAILED;
511#endif /* RT_OS_SOLARIS */
512 pid = fork();
513 if (!pid)
514 {
515#ifdef RT_OS_SOLARIS
516 rtSolarisContractPostForkChild(templateFd);
517#endif /* RT_OS_SOLARIS */
518 if (!(fFlags & RTPROC_FLAGS_DETACHED))
519 setpgid(0, 0); /* see comment above */
520
521 /*
522 * Change group and user if requested.
523 */
524#if 1 /** @todo This needs more work, see suplib/hardening. */
525 if (gid != ~(gid_t)0)
526 {
527 if (setgid(gid))
528 {
529 if (fFlags & RTPROC_FLAGS_DETACHED)
530 _Exit(126);
531 else
532 exit(126);
533 }
534 }
535
536 if (uid != ~(uid_t)0)
537 {
538 if (setuid(uid))
539 {
540 if (fFlags & RTPROC_FLAGS_DETACHED)
541 _Exit(126);
542 else
543 exit(126);
544 }
545 }
546#endif
547
548 /*
549 * Apply changes to the standard file descriptor and stuff.
550 */
551 for (int i = 0; i < 3; i++)
552 {
553 int fd = aStdFds[i];
554 if (fd == -2)
555 close(i);
556 else if (fd >= 0)
557 {
558 int rc2 = dup2(fd, i);
559 if (rc2 != i)
560 {
561 if (fFlags & RTPROC_FLAGS_DETACHED)
562 _Exit(125);
563 else
564 exit(125);
565 }
566 for (int j = i + 1; j < 3; j++)
567 if (aStdFds[j] == fd)
568 {
569 fd = -1;
570 break;
571 }
572 if (fd >= 0)
573 close(fd);
574 }
575 }
576
577 /*
578 * Finally, execute the requested program.
579 */
580 rc = execve(pszExec, (char * const *)papszArgs, (char * const *)papszEnv);
581 if (errno == ENOEXEC)
582 {
583 /* This can happen when trying to start a shell script without the magic #!/bin/sh */
584 RTAssertMsg2Weak("Cannot execute this binary format!\n");
585 }
586 else
587 RTAssertMsg2Weak("execve returns %d errno=%d\n", rc, errno);
588 RTAssertReleasePanic();
589 if (fFlags & RTPROC_FLAGS_DETACHED)
590 _Exit(127);
591 else
592 exit(127);
593 }
594#ifdef RT_OS_SOLARIS
595 rtSolarisContractPostForkParent(templateFd, pid);
596#endif /* RT_OS_SOLARIS */
597 if (pid > 0)
598 {
599 /* For a detached process this happens in the temp process, so
600 * it's not worth doing anything as this process must exit. */
601 if (fFlags & RTPROC_FLAGS_DETACHED)
602 _Exit(0);
603 if (phProcess)
604 *phProcess = pid;
605 return VINF_SUCCESS;
606 }
607 /* For a detached process this happens in the temp process, so
608 * it's not worth doing anything as this process must exit. */
609 if (fFlags & RTPROC_FLAGS_DETACHED)
610 _Exit(124);
611 return RTErrConvertFromErrno(errno);
612 }
613
614 return VERR_NOT_IMPLEMENTED;
615}
616
617
618RTR3DECL(int) RTProcDaemonizeUsingFork(bool fNoChDir, bool fNoClose, const char *pszPidfile)
619{
620 /*
621 * Fork the child process in a new session and quit the parent.
622 *
623 * - fork once and create a new session (setsid). This will detach us
624 * from the controlling tty meaning that we won't receive the SIGHUP
625 * (or any other signal) sent to that session.
626 * - The SIGHUP signal is ignored because the session/parent may throw
627 * us one before we get to the setsid.
628 * - When the parent exit(0) we will become an orphan and re-parented to
629 * the init process.
630 * - Because of the sometimes unexpected semantics of assigning the
631 * controlling tty automagically when a session leader first opens a tty,
632 * we will fork() once more to get rid of the session leadership role.
633 */
634
635 /* We start off by opening the pidfile, so that we can fail straight away
636 * if it already exists. */
637 int fdPidfile = -1;
638 if (pszPidfile != NULL)
639 {
640 /* @note the exclusive create is not guaranteed on all file
641 * systems (e.g. NFSv2) */
642 if ((fdPidfile = open(pszPidfile, O_RDWR | O_CREAT | O_EXCL, 0644)) == -1)
643 return RTErrConvertFromErrno(errno);
644 }
645
646 /* Ignore SIGHUP straight away. */
647 struct sigaction OldSigAct;
648 struct sigaction SigAct;
649 memset(&SigAct, 0, sizeof(SigAct));
650 SigAct.sa_handler = SIG_IGN;
651 int rcSigAct = sigaction(SIGHUP, &SigAct, &OldSigAct);
652
653 /* First fork, to become independent process. */
654 pid_t pid = fork();
655 if (pid == -1)
656 return RTErrConvertFromErrno(errno);
657 if (pid != 0)
658 {
659 /* Parent exits, no longer necessary. The child gets reparented
660 * to the init process. */
661 exit(0);
662 }
663
664 /* Create new session, fix up the standard file descriptors and the
665 * current working directory. */
666 /** @todo r=klaus the webservice uses this function and assumes that the
667 * contract id of the daemon is the same as that of the original process.
668 * Whenever this code is changed this must still remain possible. */
669 pid_t newpgid = setsid();
670 int SavedErrno = errno;
671 if (rcSigAct != -1)
672 sigaction(SIGHUP, &OldSigAct, NULL);
673 if (newpgid == -1)
674 return RTErrConvertFromErrno(SavedErrno);
675
676 if (!fNoClose)
677 {
678 /* Open stdin(0), stdout(1) and stderr(2) as /dev/null. */
679 int fd = open("/dev/null", O_RDWR);
680 if (fd == -1) /* paranoia */
681 {
682 close(STDIN_FILENO);
683 close(STDOUT_FILENO);
684 close(STDERR_FILENO);
685 fd = open("/dev/null", O_RDWR);
686 }
687 if (fd != -1)
688 {
689 dup2(fd, STDIN_FILENO);
690 dup2(fd, STDOUT_FILENO);
691 dup2(fd, STDERR_FILENO);
692 if (fd > 2)
693 close(fd);
694 }
695 }
696
697 if (!fNoChDir)
698 {
699 int rcIgnored = chdir("/");
700 NOREF(rcIgnored);
701 }
702
703 /* Second fork to lose session leader status. */
704 pid = fork();
705 if (pid == -1)
706 return RTErrConvertFromErrno(errno);
707
708 if (pid != 0)
709 {
710 /* Write the pid file, this is done in the parent, before exiting. */
711 if (fdPidfile != -1)
712 {
713 char szBuf[256];
714 size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", pid);
715 ssize_t cbIgnored = write(fdPidfile, szBuf, cbPid); NOREF(cbIgnored);
716 close(fdPidfile);
717 }
718 exit(0);
719 }
720
721 return VINF_SUCCESS;
722}
723
Note: See TracBrowser for help on using the repository browser.

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