VBoxService-win.cpp revision 942d1a32d70995e0b2d6fc8c109432e0613967ac
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * VBoxService - Guest Additions Service Skeleton, Windows Specific Parts.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
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 * 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* Header Files *
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync*******************************************************************************/
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsyncvoid WINAPI VBoxServiceWinMain (DWORD argc, LPTSTR *argv);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * @todo Format code style.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * @todo Add full unicode support.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * @todo Add event log capabilities / check return values.
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncDWORD VBoxServiceWinAddAceToObjectsSecurityDescriptor(LPTSTR pszObjName,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Get a pointer to the existing DACL. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync dwRes = GetNamedSecurityInfo(pszObjName, ObjectType,
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync VBoxServiceError("AddAceToObjectsSecurityDescriptor: Object not found/installed: %s\n", pszObjName);
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync VBoxServiceError("AddAceToObjectsSecurityDescriptor: GetNamedSecurityInfo: Error %u\n", dwRes);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Initialize an EXPLICIT_ACCESS structure for the new ACE. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Create a new ACL that merges the new ACE into the existing DACL. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync VBoxServiceError("AddAceToObjectsSecurityDescriptor: SetEntriesInAcl: Error %u\n", dwRes);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Attach the new ACL as the object's DACL. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync dwRes = SetNamedSecurityInfo(pszObjName, ObjectType,
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync VBoxServiceError("AddAceToObjectsSecurityDescriptor: SetNamedSecurityInfo: Error %u\n", dwRes);
3b884f2d817ce2af98e0c2c3fed82ade29e991b8vboxsync /** @todo get rid of that spaghetti jump ... */
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncBOOL VBoxServiceWinSetStatus(DWORD dwStatus, DWORD dwCheckPoint)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync if (NULL == g_hWinServiceStatus) /* Program could be in testing mode, so no service environment available. */
87719ea227ff5749eed4101abe8c06c86f24690dvboxsync VBoxServiceVerbose(2, "Setting service status to: %ld\n", dwStatus);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not open SCM! Error: %d\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not create service! Error: %d\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(0, "Service successfully installed!\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(1, "Uninstalling service ...\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not open SCM! Error: %d\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync hService = OpenService (hSCManager, VBOXSERVICE_NAME, SERVICE_ALL_ACCESS );
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not open service! Error: %d\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not remove service! Error: %d\n", GetLastError());
f65e6cba3e74ffd3dc9e6053828dcc82b367e8devboxsync if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\System", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) {
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(0, "Service successfully uninstalled!\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /* Create a well-known SID for the "Builtin Users" group. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_LOCAL_SID_AUTHORITY;
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync 0, 0, 0, 0, 0, 0, 0,
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("AllocateAndInitializeSid: Error %u\n", GetLastError());
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync DWORD dwRes = VBoxServiceWinAddAceToObjectsSecurityDescriptor (TEXT("\\\\.\\VBoxMiniRdrDN"),
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. */
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). */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync * Check that at least one service is enabled.
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync rc = VBoxServiceStartServices(iMain); /* Start all the services. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Service failed to start with rc=%Rrc!\n", rc);
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncVOID WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl)
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncDWORD WINAPI VBoxServiceWinCtrlHandler(DWORD dwControl,
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync VBoxServiceVerbose(2, "Control handler: Control=%ld\n", dwControl);
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync VBoxServiceVerbose(2, "Control handler: EventType=%ld\n", dwEventType);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync case SERVICE_CONTROL_SESSIONCHANGE: /* Only Win XP and up. */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync /*case WTS_SESSION_LOGON:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(2, "A user has logged on to the session.\n");
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync case WTS_SESSION_LOGOFF:
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(2, "A user has logged off from the session.\n");
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync#endif /* TARGET_NT4 */
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(1, "Service control function not implemented: %ld\n", dwControl);
942d1a32d70995e0b2d6fc8c109432e0613967acvboxsyncvoid WINAPI VBoxServiceWinMain(DWORD argc, LPTSTR *argv)
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(2, "Registering service control handler ...\n");
1222ff94540f6426532c1f1714f0717fdcf92e46vboxsync g_hWinServiceStatus = RegisterServiceCtrlHandler (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync g_hWinServiceStatus = RegisterServiceCtrlHandlerEx (VBOXSERVICE_NAME, VBoxServiceWinCtrlHandler, NULL);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceError("Could not register service control handle! Error: %ld\n", dwErr);
7a61a5714b9a39ac3bd59e52b0843ef498350a35vboxsync VBoxServiceVerbose(2, "Service control handler registered.\n");