SUPSvc.cpp revision 1c2c968fd241148110002d75b2c0fdeddc211e14
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox Support Service - Common Code.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * available from http://www.virtualbox.org. This file is free software;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * you can redistribute it and/or modify it under the terms of the GNU
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * General Public License (GPL) as published by the Free Software
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The contents of this file may alternatively be used under the terms
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * of the Common Development and Distribution License Version 1.0
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox OSE distribution, in which case the provisions of the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * CDDL are applicable instead of those of the GPL.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * You may elect to license modified versions of this file under the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * terms and conditions of either the GPL or the CDDL or both.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * additional information or have any questions.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Header Files *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Structures and Typedefs *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Service state.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Service descriptor.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The service name. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync const char *pszName;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The service state. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The instance handle returned by pfnCreate. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Create the service (don't start it).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VBox status code, log entry is written on failure.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param ppvInstance Where to store the instance handle.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync DECLCALLBACKMEMBER(int, pfnCreate)(void **ppvInstance);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Start the service.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VBox status code, log entry is written on failure.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pvInstance The instance handle.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync DECLCALLBACKMEMBER(void, pfnStart)(void *pvInstance);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Attempt to stop a running service.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This should fail if there are active clients. A stopped service
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * can be restarted by calling pfnStart.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VBox status code, log entry is written on failure.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pvInstance The instance handle.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync DECLCALLBACKMEMBER(int, pfnTryStop)(void *pvInstance);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Destroy the service, stopping first it if necessary.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pvInstance The instance handle.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fRunning Whether the service is running or not.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync DECLCALLBACKMEMBER(void, pfnStopAndDestroy)(void *pvInstance, bool fRunning);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Pointer to a service descriptor. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Pointer to a const service descriptor. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Global Variables *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Instantiates and starts the services.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VBox status code. Done bitching on failure.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Validate that all services are in the NotCreated state.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned i;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (g_aServices[i].enmState != kSupSvcServiceState_NotCreated)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supSvcLogError("service %s in state %d, expected state %d (NotCreated)",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].pszName, g_aServices[i].enmState, kSupSvcServiceState_NotCreated);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Create all the services, then start them.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync Log(("supSvcCreateAndStartServices: %s -> %Rrc\n", g_aServices[i].pszName, rc));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].enmState = kSupSvcServiceState_Paused;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].pfnStart(g_aServices[i].pvInstance);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].enmState = kSupSvcServiceState_Running;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Destroy any services we managed to instantiate.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync while (i-- > 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].pfnStopAndDestroy(g_aServices[i].pvInstance, false /* fRunning */);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].enmState = kSupSvcServiceState_NotCreated;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogFlow(("supSvcCreateAndStartServices: returns %Rrc\n", rc));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Checks if it's possible to stop the services.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VBox status code, done bitching on failure.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Check that the services are all created and count the running ones.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned i;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned cRunning = 0;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (g_aServices[i].enmState == kSupSvcServiceState_Running)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else if (g_aServices[i].enmState == kSupSvcServiceState_NotCreated)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supSvcLogError("service %s in state %d (NotCreated), expected pause or running",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].pszName, g_aServices[i].enmState, kSupSvcServiceState_NotCreated);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VINF_SUCCESS; /* all stopped, nothing to do. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync Assert(cRunning == RT_ELEMENTS(g_aServices)); /* all or nothing */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Try stop them in reverse of start order.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync while (i-- > 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = g_aServices[i].pfnTryStop(g_aServices[i].pvInstance);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync Log(("supSvcTryStopServices: %s -> %Rrc\n", g_aServices[i].pszName, rc));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].enmState = kSupSvcServiceState_Paused;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Failed, restart the ones we succeeded in stopping. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].pfnStart(g_aServices[i].pvInstance);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].enmState = kSupSvcServiceState_Running;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogFlow(("supSvcTryStopServices: returns %Rrc\n", rc));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Stops and destroys the services.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Stop and destroy the service in reverse of start order.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync while (i-- > 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (g_aServices[i].enmState != kSupSvcServiceState_NotCreated)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].pfnStopAndDestroy(g_aServices[i].pvInstance,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].enmState == kSupSvcServiceState_Running);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aServices[i].enmState = kSupSvcServiceState_NotCreated;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Logs the message to the appropirate system log.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * In debug builds this will also put it in the debug log.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszMsg The log string.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @remarks This may later be replaced by the release logger and callback destination(s).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Logs the message to the appropirate system log.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * In debug builds this will also put it in the debug log.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFormat The log string. No trailing newline.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param ... Format arguments.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @todo This should later be replaced by the release logger and callback destination(s).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncvoid supSvcLogErrorV(const char *pszFormat, va_list va)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Logs the error message to the appropirate system log.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * In debug builds this will also put it in the debug log.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFormat The log string. No trailing newline.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param ... Format arguments.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @todo This should later be replaced by the release logger and callback destination(s).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Deals with RTGetOpt failure, bitching in the system log.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns 1
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszAction The action name.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param rc The RTGetOpt return value.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param argc The argument count.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param argv The argument vector.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param iArg The argument index.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pValue The value returned by RTGetOpt.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncint supSvcLogGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTOPTIONUNION pValue)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supSvcLogError("%s - RTGetOpt failure, %Rrc (%d): %s",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Bitch about too many arguments (after RTGetOpt stops) in the system log.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns 1
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszAction The action name.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param argc The argument count.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param argv The argument vector.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param iArg The argument index.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncint supSvcLogTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supSvcLogError("%s - Too many arguments: %s", pszAction, argv[iArg]);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Prints an error message to the screen.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFormat The message format string.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param va Format arguments.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncvoid supSvcDisplayErrorV(const char *pszFormat, va_list va)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync Log(("supSvcDisplayErrorV: %s", pszFormat)); /** @todo format it! */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Prints an error message to the screen.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFormat The message format string.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param ... Format arguments.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Deals with RTGetOpt failure.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns 1
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszAction The action name.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param rc The RTGetOpt return value.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param argc The argument count.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param argv The argument vector.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param iArg The argument index.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pValue The value returned by RTGetOpt.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncint supSvcDisplayGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTOPTIONUNION pValue)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supSvcDisplayError("%s - RTGetOpt failure, %Rrc (%d): %s\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Bitch about too many arguments (after RTGetOpt stops).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns 1
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszAction The action name.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param argc The argument count.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param argv The argument vector.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param iArg The argument index.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncint supSvcDisplayTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supSvcDisplayError("%s - Too many arguments: %s\n", pszAction, argv[iArg]);