1 | /** $Id: VBoxService-os2.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest Additions Service Skeleton.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Global Variables *
|
---|
20 | *******************************************************************************/
|
---|
21 | #ifdef RT_OS_OS2
|
---|
22 | # define INCL_BASE
|
---|
23 | # define INCL_ERRORS
|
---|
24 | # include <os2.h>
|
---|
25 | #endif
|
---|
26 | #include <errno.h>
|
---|
27 |
|
---|
28 | #include <iprt/string.h>
|
---|
29 | #include <iprt/alloca.h>
|
---|
30 | #include <VBox/VBoxGuest.h>
|
---|
31 | #include "VBoxServiceInternal.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * OS/2 emulation of the BSD daemon() call.
|
---|
36 | */
|
---|
37 | int daemon(int nochdir, int noclose)
|
---|
38 | {
|
---|
39 | PPIB pPib;
|
---|
40 | PTIB pTib;
|
---|
41 | DosGetInfoBlocks(&pTib, &pPib);
|
---|
42 |
|
---|
43 | /* Get the full path to the executable. */
|
---|
44 | char szExe[CCHMAXPATH];
|
---|
45 | APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe);
|
---|
46 | if (rc)
|
---|
47 | {
|
---|
48 | errno = EDOOFUS;
|
---|
49 | return -1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /* calc the length of the command line. */
|
---|
53 | char *pch = pPib->pib_pchcmd;
|
---|
54 | size_t cch0 = strlen(pch);
|
---|
55 | pch += cch0 + 1;
|
---|
56 | size_t cch1 = strlen(pch);
|
---|
57 | pch += cch1 + 1;
|
---|
58 | char *pchArgs;
|
---|
59 | if (cch1 && *pch)
|
---|
60 | {
|
---|
61 | do pch = strchr(pch, '\0') + 1;
|
---|
62 | while (*pch);
|
---|
63 |
|
---|
64 | size_t cchTotal = pch - pPib->pib_pchcmd;
|
---|
65 | pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0"));
|
---|
66 | memcpy(pchArgs, pPib->pib_pchcmd, cchTotal - 1);
|
---|
67 | memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0"));
|
---|
68 | }
|
---|
69 | else
|
---|
70 | {
|
---|
71 | size_t cchTotal = pch - pPib->pib_pchcmd + 1;
|
---|
72 | pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized "));
|
---|
73 | memcpy(pchArgs, pPib->pib_pchcmd, cch0 + 1);
|
---|
74 | pch = pchArgs + cch0 + 1;
|
---|
75 | memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1);
|
---|
76 | pch += sizeof(" --daemonized ") - 1;
|
---|
77 | if (cch1)
|
---|
78 | memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2);
|
---|
79 | else
|
---|
80 | pch[0] = pch[1] = '\0';
|
---|
81 | }
|
---|
82 |
|
---|
83 | /* spawn a detach process */
|
---|
84 | char szObj[128];
|
---|
85 | RESULTCODES ResCodes = { 0, 0 };
|
---|
86 | szObj[0] = '\0';
|
---|
87 | rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
|
---|
88 | if (rc)
|
---|
89 | {
|
---|
90 | VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj);
|
---|
91 | errno = EDOOFUS;
|
---|
92 | return -1;
|
---|
93 | }
|
---|
94 | DosExit(EXIT_PROCESS, 0);
|
---|
95 | return -1;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|