VBoxService-win.cpp revision 942d1a32d70995e0b2d6fc8c109432e0613967ac
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync/* $Id$ */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync/** @file
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * VBoxService - Guest Additions Service Skeleton, Windows Specific Parts.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync/*
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync *
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * available from http://www.virtualbox.org. This file is free software;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * you can redistribute it and/or modify it under the terms of the GNU
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * General Public License (GPL) as published by the Free Software
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync *
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * additional information or have any questions.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync/*******************************************************************************
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync* Header Files *
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync*******************************************************************************/
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync#include <iprt/assert.h>
9d0256d0854254b2717550a64a6e5871a1123a0dvboxsync#include <iprt/err.h>
35396ee506ef68dd1c161f1ef2c3c0b68a146ff2vboxsync#include <VBox/VBoxGuestLib.h>
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync#include "VBoxServiceInternal.h"
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync#include <Windows.h>
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync#include <process.h>
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync#include <aclapi.h>
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsyncDWORD g_rcWinService = 0;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsyncSERVICE_STATUS_HANDLE g_hWinServiceStatus = NULL;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsyncvoid WINAPI VBoxServiceWinMain (DWORD argc, LPTSTR *argv);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsyncstatic SERVICE_TABLE_ENTRY const g_aServiceTable[]=
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync{
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {VBOXSERVICE_NAME, VBoxServiceWinMain},
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {NULL,NULL}
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync};
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync/**
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * @todo Format code style.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * @todo Add full unicode support.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * @todo Add event log capabilities / check return values.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync */
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncDWORD VBoxServiceWinAddAceToObjectsSecurityDescriptor(LPTSTR pszObjName,
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync SE_OBJECT_TYPE ObjectType,
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync LPTSTR pszTrustee,
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync TRUSTEE_FORM TrusteeForm,
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync DWORD dwAccessRights,
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync ACCESS_MODE AccessMode,
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync DWORD dwInheritance)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync{
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync DWORD dwRes = 0;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync PACL pOldDACL = NULL, pNewDACL = NULL;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync PSECURITY_DESCRIPTOR pSD = NULL;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync EXPLICIT_ACCESS ea;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (NULL == pszObjName)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return ERROR_INVALID_PARAMETER;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Get a pointer to the existing DACL. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync dwRes = GetNamedSecurityInfo(pszObjName, ObjectType,
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync DACL_SECURITY_INFORMATION,
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync NULL, NULL, &pOldDACL, NULL, &pSD);
87719ea227ff5749eed4101abe8c06c86f24690dvboxsync if (ERROR_SUCCESS != dwRes)
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync {
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync if (dwRes == ERROR_FILE_NOT_FOUND)
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync VBoxServiceError("AddAceToObjectsSecurityDescriptor: Object not found/installed: %s\n", pszObjName);
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync else
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync VBoxServiceError("AddAceToObjectsSecurityDescriptor: GetNamedSecurityInfo: Error %u\n", dwRes);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync goto Cleanup;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Initialize an EXPLICIT_ACCESS structure for the new ACE. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ea.grfAccessPermissions = dwAccessRights;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ea.grfAccessMode = AccessMode;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ea.grfInheritance= dwInheritance;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ea.Trustee.TrusteeForm = TrusteeForm;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ea.Trustee.ptstrName = pszTrustee;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Create a new ACL that merges the new ACE into the existing DACL. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
87719ea227ff5749eed4101abe8c06c86f24690dvboxsync if (ERROR_SUCCESS != dwRes)
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync {
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync VBoxServiceError("AddAceToObjectsSecurityDescriptor: SetEntriesInAcl: Error %u\n", dwRes);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync goto Cleanup;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Attach the new ACL as the object's DACL. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync dwRes = SetNamedSecurityInfo(pszObjName, ObjectType,
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync DACL_SECURITY_INFORMATION,
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync NULL, NULL, pNewDACL, NULL);
87719ea227ff5749eed4101abe8c06c86f24690dvboxsync if (ERROR_SUCCESS != dwRes)
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync {
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync VBoxServiceError("AddAceToObjectsSecurityDescriptor: SetNamedSecurityInfo: Error %u\n", dwRes);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync goto Cleanup;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync /** @todo get rid of that spaghetti jump ... */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsyncCleanup:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if(pSD != NULL)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync LocalFree((HLOCAL) pSD);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if(pNewDACL != NULL)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync LocalFree((HLOCAL) pNewDACL);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return dwRes;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync}
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncBOOL VBoxServiceWinSetStatus(DWORD dwStatus, DWORD dwCheckPoint)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync{
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (NULL == g_hWinServiceStatus) /* Program could be in testing mode, so no service environment available. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return FALSE;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
87719ea227ff5749eed4101abe8c06c86f24690dvboxsync VBoxServiceVerbose(2, "Setting service status to: %ld\n", dwStatus);
87719ea227ff5749eed4101abe8c06c86f24690dvboxsync g_rcWinService = dwStatus;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SERVICE_STATUS ss;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ss.dwCurrentState = g_rcWinService;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
87719ea227ff5749eed4101abe8c06c86f24690dvboxsync ss.dwWin32ExitCode = NO_ERROR;
87719ea227ff5749eed4101abe8c06c86f24690dvboxsync ss.dwServiceSpecificExitCode = 0; /* Not used */
87719ea227ff5749eed4101abe8c06c86f24690dvboxsync ss.dwCheckPoint = dwCheckPoint;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ss.dwWaitHint = 3000;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync return SetServiceStatus(g_hWinServiceStatus, &ss);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync}
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncint VBoxServiceWinInstall(void)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync{
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SC_HANDLE hService, hSCManager;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync TCHAR imagePath[MAX_PATH] = { 0 };
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync GetModuleFileName(NULL,imagePath,MAX_PATH);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(1, "Installing service ...\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (NULL == hSCManager) {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not open SCM! Error: %d\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return 1;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync hService = ::CreateService (hSCManager,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBOXSERVICE_NAME, VBOXSERVICE_FRIENDLY_NAME,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SERVICE_ALL_ACCESS,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync imagePath, NULL, NULL, NULL, NULL, NULL);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (NULL == hService) {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not create service! Error: %d\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync CloseServiceHandle(hSCManager);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return 1;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync else
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(0, "Service successfully installed!\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync CloseServiceHandle(hService);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync CloseServiceHandle(hSCManager);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return 0;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync}
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncint VBoxServiceWinUninstall(void)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync{
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SC_HANDLE hService, hSCManager;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(1, "Uninstalling service ...\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (NULL == hSCManager) {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not open SCM! Error: %d\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return 1;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync hService = OpenService (hSCManager, VBOXSERVICE_NAME, SERVICE_ALL_ACCESS );
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (NULL == hService) {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not open service! Error: %d\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync CloseServiceHandle (hSCManager);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return 1;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (FALSE == DeleteService (hService))
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not remove service! Error: %d\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync CloseServiceHandle (hService);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync CloseServiceHandle (hSCManager);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return 1;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync else
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync HKEY hKey = NULL;
f65e6cba3e74ffd3dc9e6053828dcc82b367e8devboxsync if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\System", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync RegDeleteKey(hKey, VBOXSERVICE_NAME);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync RegCloseKey(hKey);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(0, "Service successfully uninstalled!\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync CloseServiceHandle(hService);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync CloseServiceHandle(hSCManager);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return 0;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync}
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncint VBoxServiceWinStart(void)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync{
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync int rc = VINF_SUCCESS;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
36c5021c9ff03eb19d9818903cea95d2e43bd6bcvboxsync#ifndef TARGET_NT4
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Create a well-known SID for the "Builtin Users" group. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync PSID pBuiltinUsersSID = NULL;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_LOCAL_SID_AUTHORITY;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SECURITY_LOCAL_RID,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync 0, 0, 0, 0, 0, 0, 0,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync &pBuiltinUsersSID))
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("AllocateAndInitializeSid: Error %u\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync else
35396ee506ef68dd1c161f1ef2c3c0b68a146ff2vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync DWORD dwRes = VBoxServiceWinAddAceToObjectsSecurityDescriptor (TEXT("\\\\.\\VBoxMiniRdrDN"),
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SE_FILE_OBJECT,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync (LPTSTR)pBuiltinUsersSID,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync TRUSTEE_IS_SID,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync FILE_GENERIC_READ | FILE_GENERIC_WRITE,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SET_ACCESS,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync NO_INHERITANCE);
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync if (dwRes != ERROR_SUCCESS)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync if (dwRes == ERROR_FILE_NOT_FOUND)
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync {
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync /* If we don't find our "VBoxMiniRdrDN" (for Shared Folders) object above,
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync don't report an error; it just might be not installed. Otherwise this
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync would cause the SCM to hang on starting up the service. */
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync rc = VINF_SUCCESS;
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync }
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync else rc = RTErrConvertFromWin32(dwRes);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
36c5021c9ff03eb19d9818903cea95d2e43bd6bcvboxsync#endif
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (RT_SUCCESS(rc))
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Notify SCM *before* we're starting the services, because the last services
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync always starts in main thread (which causes the SCM to wait because of the non-responding
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync service). */
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync VBoxServiceWinSetStatus (SERVICE_RUNNING, 0);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /*
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * Check that at least one service is enabled.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync unsigned iMain = VBoxServiceGetStartedServices();
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync rc = VBoxServiceStartServices(iMain); /* Start all the services. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (RT_FAILURE(rc))
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync VBoxServiceWinSetStatus (SERVICE_STOPPED, 0);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (RT_FAILURE(rc))
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Service failed to start with rc=%Rrc!\n", rc);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return rc;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync}
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#ifdef TARGET_NT4
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncVOID WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl)
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#else
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncDWORD WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl,
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync DWORD dwEventType,
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync LPVOID lpEventData,
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync LPVOID lpContext)
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#endif
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync{
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync DWORD rc = NO_ERROR;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync VBoxServiceVerbose(2, "Control handler: Control=%ld\n", dwControl);
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#ifndef TARGET_NT4
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync VBoxServiceVerbose(2, "Control handler: EventType=%ld\n", dwEventType);
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#endif
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync switch (dwControl)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync case SERVICE_CONTROL_INTERROGATE:
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync VBoxServiceWinSetStatus(g_rcWinService, 0);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync case SERVICE_CONTROL_STOP:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync case SERVICE_CONTROL_SHUTDOWN:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync VBoxServiceWinSetStatus(SERVICE_STOP_PENDING, 0);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync rc = VBoxServiceStopServices();
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync VBoxServiceWinSetStatus(SERVICE_STOPPED, 0);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync case SERVICE_CONTROL_SESSIONCHANGE: /* Only Win XP and up. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#ifndef TARGET_NT4
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync switch (dwEventType)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /*case WTS_SESSION_LOGON:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(2, "A user has logged on to the session.\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync case WTS_SESSION_LOGOFF:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(2, "A user has logged off from the session.\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;*/
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync default:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#endif /* TARGET_NT4 */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync default:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(1, "Service control function not implemented: %ld\n", dwControl);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync rc = ERROR_CALL_NOT_IMPLEMENTED;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#ifndef TARGET_NT4
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync return rc;
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#endif
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync}
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsync
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncvoid WINAPI VBoxServiceWinMain(DWORD argc, LPTSTR *argv)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync{
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync int rc = VINF_SUCCESS;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(2, "Registering service control handler ...\n");
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#ifdef TARGET_NT4
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync g_hWinServiceStatus = RegisterServiceCtrlHandler (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler);
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#else
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync g_hWinServiceStatus = RegisterServiceCtrlHandlerEx (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler, NULL);
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#endif
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (NULL == g_hWinServiceStatus)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync DWORD dwErr = GetLastError();
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync switch (dwErr)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync case ERROR_INVALID_NAME:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Invalid service name!\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync case ERROR_SERVICE_DOES_NOT_EXIST:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Service does not exist!\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync default:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not register service control handle! Error: %ld\n", dwErr);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync break;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
35396ee506ef68dd1c161f1ef2c3c0b68a146ff2vboxsync else
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(2, "Service control handler registered.\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync rc = VBoxServiceWinStart();
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync }
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync}