2007-07-30 20:53:16 +02:00
|
|
|
/*
|
|
|
|
* msiexec.exe implementation
|
|
|
|
*
|
|
|
|
* Copyright 2007 Google (James Hawkins)
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-01-23 12:16:36 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winsvc.h>
|
2007-07-30 20:53:16 +02:00
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msiexec);
|
|
|
|
|
|
|
|
static SERVICE_STATUS_HANDLE hstatus;
|
|
|
|
|
|
|
|
static HANDLE thread;
|
|
|
|
static HANDLE kill_event;
|
|
|
|
|
2008-12-13 12:04:08 +01:00
|
|
|
static void KillService(void)
|
2007-07-30 20:53:16 +02:00
|
|
|
{
|
|
|
|
WINE_TRACE("Killing service\n");
|
|
|
|
SetEvent(kill_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL UpdateSCMStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
|
|
|
|
DWORD dwServiceSpecificExitCode)
|
|
|
|
{
|
|
|
|
SERVICE_STATUS status;
|
|
|
|
|
|
|
|
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
status.dwCurrentState = dwCurrentState;
|
|
|
|
|
|
|
|
if (dwCurrentState == SERVICE_START_PENDING)
|
|
|
|
status.dwControlsAccepted = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
|
|
|
|
SERVICE_ACCEPT_PAUSE_CONTINUE |
|
|
|
|
SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dwServiceSpecificExitCode == 0)
|
|
|
|
{
|
|
|
|
status.dwWin32ExitCode = dwWin32ExitCode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = 0;
|
|
|
|
|
|
|
|
if (!SetServiceStatus(hstatus, &status))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to set service status\n");
|
|
|
|
KillService();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI ServiceCtrlHandler(DWORD code)
|
|
|
|
{
|
2013-12-11 09:16:07 +01:00
|
|
|
DWORD state = SERVICE_RUNNING;
|
|
|
|
|
2007-07-30 20:53:16 +02:00
|
|
|
WINE_TRACE("%d\n", code);
|
|
|
|
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
UpdateSCMStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
|
|
|
|
KillService();
|
2013-12-11 09:16:07 +01:00
|
|
|
state = SERVICE_STOPPED;
|
2013-12-12 06:39:56 +01:00
|
|
|
break;
|
2007-07-30 20:53:16 +02:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unhandled service control code: %d\n", code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-11 09:16:07 +01:00
|
|
|
UpdateSCMStatus(state, NO_ERROR, 0);
|
2007-07-30 20:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI ServiceExecutionThread(LPVOID param)
|
|
|
|
{
|
2013-12-11 09:16:01 +01:00
|
|
|
WaitForSingleObject(kill_event, INFINITE);
|
2007-07-30 20:53:16 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL StartServiceThread(void)
|
|
|
|
{
|
|
|
|
DWORD id;
|
|
|
|
|
|
|
|
thread = CreateThread(0, 0, ServiceExecutionThread, 0, 0, &id);
|
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to create thread\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI ServiceMain(DWORD argc, LPSTR *argv)
|
|
|
|
{
|
|
|
|
hstatus = RegisterServiceCtrlHandlerA("MSIServer", ServiceCtrlHandler);
|
|
|
|
if (!hstatus)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to register service ctrl handler\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0);
|
|
|
|
|
2008-09-03 11:30:44 +02:00
|
|
|
kill_event = CreateEventW(0, TRUE, FALSE, 0);
|
2007-07-30 20:53:16 +02:00
|
|
|
if (!kill_event)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to create event\n");
|
|
|
|
KillService();
|
2013-12-11 09:16:07 +01:00
|
|
|
UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0);
|
2007-07-30 20:53:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!StartServiceThread())
|
|
|
|
{
|
|
|
|
KillService();
|
2013-12-11 09:16:07 +01:00
|
|
|
UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0);
|
2007-07-30 20:53:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0);
|
|
|
|
|
|
|
|
WaitForSingleObject(kill_event, INFINITE);
|
|
|
|
KillService();
|
2013-12-11 09:16:07 +01:00
|
|
|
|
|
|
|
UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0);
|
2007-07-30 20:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD DoService(void)
|
|
|
|
{
|
|
|
|
char service_name[] = "MSIServer";
|
|
|
|
|
2008-09-03 11:30:44 +02:00
|
|
|
const SERVICE_TABLE_ENTRYA service[] =
|
2007-07-30 20:53:16 +02:00
|
|
|
{
|
|
|
|
{service_name, ServiceMain},
|
|
|
|
{NULL, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
WINE_TRACE("Starting MSIServer service\n");
|
|
|
|
|
2008-09-03 11:30:44 +02:00
|
|
|
if (!StartServiceCtrlDispatcherA(service))
|
2007-07-30 20:53:16 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to start MSIServer service\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|