1 | /* $Id: RTProcDaemonize-generic.cpp 27744 2010-03-26 14:51:56Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTProcDaemonize, generic implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. 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 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #define LOG_GROUP RTLOGGROUP_PROCESS
|
---|
36 | #include <iprt/process.h>
|
---|
37 | #include "internal/iprt.h"
|
---|
38 |
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/env.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/file.h>
|
---|
43 | #include <iprt/mem.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include "internal/process.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | RTR3DECL(int) RTProcDaemonize(const char * const *papszArgs, const char *pszDaemonizedOpt)
|
---|
49 | {
|
---|
50 | /*
|
---|
51 | * Get the executable name.
|
---|
52 | * If this asserts, it's probably because RTR3Init hasn't been called.
|
---|
53 | */
|
---|
54 | char szExecPath[RTPATH_MAX];
|
---|
55 | AssertReturn(RTProcGetExecutableName(szExecPath, sizeof(szExecPath)) == szExecPath, VERR_WRONG_ORDER);
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * Create a copy of the argument list with the daemonized option appended.
|
---|
59 | */
|
---|
60 | unsigned cArgs = 0;
|
---|
61 | while (papszArgs[cArgs])
|
---|
62 | cArgs++;
|
---|
63 |
|
---|
64 | char const **papszNewArgs = (char const **)RTMemAlloc(sizeof(const char *) * (cArgs + 2));
|
---|
65 | if (!papszNewArgs)
|
---|
66 | return VERR_NO_MEMORY;
|
---|
67 | for (unsigned i = 0; i < cArgs; i++)
|
---|
68 | papszNewArgs[i] = papszArgs[i];
|
---|
69 | papszNewArgs[cArgs] = pszDaemonizedOpt;
|
---|
70 | papszNewArgs[cArgs + 1] = NULL;
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Open the bitbucket handles and create the detached process.
|
---|
74 | */
|
---|
75 | RTHANDLE hStdIn;
|
---|
76 | int rc = RTFileOpenBitBucket(&hStdIn.u.hFile, RTFILE_O_READ);
|
---|
77 | if (RT_SUCCESS(rc))
|
---|
78 | {
|
---|
79 | hStdIn.enmType = RTHANDLETYPE_FILE;
|
---|
80 |
|
---|
81 | RTHANDLE hStdOutAndErr;
|
---|
82 | rc = RTFileOpenBitBucket(&hStdOutAndErr.u.hFile, RTFILE_O_WRITE);
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | {
|
---|
85 | hStdOutAndErr.enmType = RTHANDLETYPE_FILE;
|
---|
86 |
|
---|
87 | rc = RTProcCreateEx(szExecPath, papszNewArgs, RTENV_DEFAULT, RTPROC_FLAGS_DETACHED,
|
---|
88 | &hStdIn, &hStdOutAndErr, &hStdOutAndErr,
|
---|
89 | NULL /*pszAsUser*/, NULL /*pszPassword*/, NULL /*phProcess*/);
|
---|
90 |
|
---|
91 | RTFileClose(hStdOutAndErr.u.hFile);
|
---|
92 | }
|
---|
93 |
|
---|
94 | RTFileClose(hStdOutAndErr.u.hFile);
|
---|
95 | }
|
---|
96 | RTMemFree(papszNewArgs);
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|