VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/RTProcDaemonize-generic.cpp@ 62477

Last change on this file since 62477 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1/* $Id: RTProcDaemonize-generic.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - RTProcDaemonize, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2010-2016 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 <iprt/process.h>
33#include "internal/iprt.h"
34
35#include <iprt/assert.h>
36#include <iprt/env.h>
37#include <iprt/err.h>
38#include <iprt/file.h>
39#include <iprt/mem.h>
40#include <iprt/string.h>
41#include "internal/process.h"
42
43
44RTR3DECL(int) RTProcDaemonize(const char * const *papszArgs, const char *pszDaemonizedOpt)
45{
46 /*
47 * Get the executable name.
48 * If this asserts, it's probably because rtR3Init hasn't been called.
49 */
50 char szExecPath[RTPATH_MAX];
51 AssertReturn(RTProcGetExecutablePath(szExecPath, sizeof(szExecPath)) == szExecPath, VERR_WRONG_ORDER);
52
53 /*
54 * Create a copy of the argument list with the daemonized option appended.
55 */
56 unsigned cArgs = 0;
57 while (papszArgs[cArgs])
58 cArgs++;
59
60 char const **papszNewArgs = (char const **)RTMemAlloc(sizeof(const char *) * (cArgs + 2));
61 if (!papszNewArgs)
62 return VERR_NO_MEMORY;
63 for (unsigned i = 0; i < cArgs; i++)
64 papszNewArgs[i] = papszArgs[i];
65 papszNewArgs[cArgs] = pszDaemonizedOpt;
66 papszNewArgs[cArgs + 1] = NULL;
67
68 /*
69 * Open the bitbucket handles and create the detached process.
70 */
71 RTHANDLE hStdIn;
72 int rc = RTFileOpenBitBucket(&hStdIn.u.hFile, RTFILE_O_READ);
73 if (RT_SUCCESS(rc))
74 {
75 hStdIn.enmType = RTHANDLETYPE_FILE;
76
77 RTHANDLE hStdOutAndErr;
78 rc = RTFileOpenBitBucket(&hStdOutAndErr.u.hFile, RTFILE_O_WRITE);
79 if (RT_SUCCESS(rc))
80 {
81 hStdOutAndErr.enmType = RTHANDLETYPE_FILE;
82
83 rc = RTProcCreateEx(szExecPath, papszNewArgs, RTENV_DEFAULT,
84 RTPROC_FLAGS_DETACHED | RTPROC_FLAGS_SAME_CONTRACT,
85 &hStdIn, &hStdOutAndErr, &hStdOutAndErr,
86 NULL /*pszAsUser*/, NULL /*pszPassword*/, NULL /*phProcess*/);
87
88 RTFileClose(hStdOutAndErr.u.hFile);
89 }
90
91 RTFileClose(hStdOutAndErr.u.hFile);
92 }
93 RTMemFree(papszNewArgs);
94 return rc;
95}
96
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