service.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * msiexec.exe implementation
  3. *
  4. * Copyright 2007 Google (James Hawkins)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #define WIN32_LEAN_AND_MEAN
  21. #include <stdio.h>
  22. #include <windows.h>
  23. #include <winsvc.h>
  24. #include "wine/debug.h"
  25. WINE_DEFAULT_DEBUG_CHANNEL(msiexec);
  26. static SERVICE_STATUS_HANDLE hstatus;
  27. static HANDLE thread;
  28. static HANDLE kill_event;
  29. static void KillService(void)
  30. {
  31. WINE_TRACE("Killing service\n");
  32. SetEvent(kill_event);
  33. }
  34. static BOOL UpdateSCMStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
  35. DWORD dwServiceSpecificExitCode)
  36. {
  37. SERVICE_STATUS status;
  38. status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  39. status.dwCurrentState = dwCurrentState;
  40. if (dwCurrentState == SERVICE_START_PENDING
  41. || dwCurrentState == SERVICE_STOP_PENDING
  42. || dwCurrentState == SERVICE_STOPPED)
  43. status.dwControlsAccepted = 0;
  44. else
  45. {
  46. status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
  47. SERVICE_ACCEPT_PAUSE_CONTINUE |
  48. SERVICE_ACCEPT_SHUTDOWN;
  49. }
  50. if (dwServiceSpecificExitCode == 0)
  51. {
  52. status.dwWin32ExitCode = dwWin32ExitCode;
  53. }
  54. else
  55. {
  56. status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
  57. }
  58. status.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
  59. status.dwCheckPoint = 0;
  60. status.dwWaitHint = 0;
  61. if (!SetServiceStatus(hstatus, &status))
  62. {
  63. fprintf(stderr, "Failed to set service status\n");
  64. KillService();
  65. return FALSE;
  66. }
  67. return TRUE;
  68. }
  69. static void WINAPI ServiceCtrlHandler(DWORD code)
  70. {
  71. WINE_TRACE("%d\n", code);
  72. switch (code)
  73. {
  74. case SERVICE_CONTROL_SHUTDOWN:
  75. case SERVICE_CONTROL_STOP:
  76. UpdateSCMStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
  77. KillService();
  78. break;
  79. default:
  80. fprintf(stderr, "Unhandled service control code: %d\n", code);
  81. UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0);
  82. break;
  83. }
  84. }
  85. static DWORD WINAPI ServiceExecutionThread(LPVOID param)
  86. {
  87. WaitForSingleObject(kill_event, INFINITE);
  88. return 0;
  89. }
  90. static BOOL StartServiceThread(void)
  91. {
  92. DWORD id;
  93. thread = CreateThread(0, 0, ServiceExecutionThread, 0, 0, &id);
  94. if (!thread)
  95. {
  96. fprintf(stderr, "Failed to create thread\n");
  97. return FALSE;
  98. }
  99. return TRUE;
  100. }
  101. static void WINAPI ServiceMain(DWORD argc, LPSTR *argv)
  102. {
  103. hstatus = RegisterServiceCtrlHandlerA("MSIServer", ServiceCtrlHandler);
  104. if (!hstatus)
  105. {
  106. fprintf(stderr, "Failed to register service ctrl handler\n");
  107. return;
  108. }
  109. UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0);
  110. kill_event = CreateEventW(0, TRUE, FALSE, 0);
  111. if (!kill_event)
  112. {
  113. fprintf(stderr, "Failed to create event\n");
  114. KillService();
  115. UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0);
  116. return;
  117. }
  118. if (!StartServiceThread())
  119. {
  120. KillService();
  121. UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0);
  122. return;
  123. }
  124. UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0);
  125. WaitForSingleObject(thread, INFINITE);
  126. UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0);
  127. }
  128. DWORD DoService(void)
  129. {
  130. char service_name[] = "MSIServer";
  131. const SERVICE_TABLE_ENTRYA service[] =
  132. {
  133. {service_name, ServiceMain},
  134. {NULL, NULL},
  135. };
  136. WINE_TRACE("Starting MSIServer service\n");
  137. if (!StartServiceCtrlDispatcherA(service))
  138. {
  139. fprintf(stderr, "Failed to start MSIServer service\n");
  140. return 1;
  141. }
  142. return 0;
  143. }