VBoxService-os2.cpp revision 0f0fa99ce6af43d7a38d97fdecc89eae0bf89fdf
/** $Id$ */
/** @file
* VBoxService - Guest Additions Service Skeleton.
*/
/*
* Copyright (C) 2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifdef __OS2__
# define INCL_BASE
# define INCL_ERRORS
# include <os2.h>
#endif
#include <errno.h>
#include <iprt/string.h>
#include <iprt/alloca.h>
#include <VBox/VBoxGuest.h>
#include "VBoxServiceInternal.h"
/**
* OS/2 emulation of the BSD daemon() call.
*/
int daemon(int nochdir, int noclose)
{
PPIB pPib;
PTIB pTib;
DosGetInfoBlocks(&pTib, &pPib);
/* Get the full path to the executable. */
char szExe[CCHMAXPATH];
APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe);
if (rc)
{
errno = EDOOFUS;
return -1;
}
/* calc the length of the command line. */
char *pch = pPib->pib_pchcmd;
size_t cch0 = strlen(pch);
pch += cch0 + 1;
size_t cch1 = strlen(pch);
pch += cch1 + 1;
char *pchArgs;
if (cch1 && *pch)
{
do pch = strchr(pch, '\0') + 1;
while (*pch);
size_t cchTotal = pch - pPib->pib_pchcmd;
pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0"));
memcpy(pchArgs, pPib->pib_pchcmd, cchTotal - 1);
memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0"));
}
else
{
size_t cchTotal = pch - pPib->pib_pchcmd + 1;
pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized "));
memcpy(pchArgs, pPib->pib_pchcmd, cch0 + 1);
pch = pchArgs + cch0 + 1;
memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1);
pch += sizeof(" --daemonized ") - 1;
if (cch1)
memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2);
else
pch[0] = pch[1] = '\0';
}
/* spawn a detach process */
char szObj[128];
RESULTCODES ResCodes = { 0, 0 };
szObj[0] = '\0';
rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
if (rc)
{
VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj);
errno = EDOOFUS;
return -1;
}
DosExit(EXIT_PROCESS, 0);
return -1;
}