sc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 2010 Hans Leidekker
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. */
  18. #define WIN32_LEAN_AND_MEAN
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <windows.h>
  23. #include <winsvc.h>
  24. #include "wine/debug.h"
  25. WINE_DEFAULT_DEBUG_CHANNEL(sc);
  26. struct create_params
  27. {
  28. const WCHAR *displayname;
  29. const WCHAR *binpath;
  30. const WCHAR *group;
  31. const WCHAR *depend;
  32. const WCHAR *obj;
  33. const WCHAR *password;
  34. DWORD type;
  35. DWORD start;
  36. DWORD error;
  37. BOOL tag;
  38. };
  39. static BOOL parse_create_params( int argc, const WCHAR *argv[], struct create_params *cp )
  40. {
  41. unsigned int i;
  42. cp->displayname = NULL;
  43. cp->type = SERVICE_WIN32_OWN_PROCESS;
  44. cp->start = SERVICE_DEMAND_START;
  45. cp->error = SERVICE_ERROR_NORMAL;
  46. cp->binpath = NULL;
  47. cp->group = NULL;
  48. cp->tag = FALSE;
  49. cp->depend = NULL;
  50. cp->obj = NULL;
  51. cp->password = NULL;
  52. for (i = 0; i < argc; i++)
  53. {
  54. if (!wcsicmp( argv[i], L"displayname=" ) && i < argc - 1) cp->displayname = argv[i + 1];
  55. if (!wcsicmp( argv[i], L"binpath=" ) && i < argc - 1) cp->binpath = argv[i + 1];
  56. if (!wcsicmp( argv[i], L"group=" ) && i < argc - 1) cp->group = argv[i + 1];
  57. if (!wcsicmp( argv[i], L"depend=" ) && i < argc - 1) cp->depend = argv[i + 1];
  58. if (!wcsicmp( argv[i], L"obj=" ) && i < argc - 1) cp->obj = argv[i + 1];
  59. if (!wcsicmp( argv[i], L"password=" ) && i < argc - 1) cp->password = argv[i + 1];
  60. if (!wcsicmp( argv[i], L"tag=" ) && i < argc - 1)
  61. {
  62. if (!wcsicmp( argv[i], L"yes" ))
  63. {
  64. WINE_FIXME("tag argument not supported\n");
  65. cp->tag = TRUE;
  66. }
  67. }
  68. if (!wcsicmp( argv[i], L"type=" ) && i < argc - 1)
  69. {
  70. if (!wcsicmp( argv[i + 1], L"own" )) cp->type = SERVICE_WIN32_OWN_PROCESS;
  71. if (!wcsicmp( argv[i + 1], L"share" )) cp->type = SERVICE_WIN32_SHARE_PROCESS;
  72. if (!wcsicmp( argv[i + 1], L"kernel" )) cp->type = SERVICE_KERNEL_DRIVER;
  73. if (!wcsicmp( argv[i + 1], L"filesys" )) cp->type = SERVICE_FILE_SYSTEM_DRIVER;
  74. if (!wcsicmp( argv[i + 1], L"rec" )) cp->type = SERVICE_RECOGNIZER_DRIVER;
  75. if (!wcsicmp( argv[i + 1], L"interact" )) cp->type |= SERVICE_INTERACTIVE_PROCESS;
  76. }
  77. if (!wcsicmp( argv[i], L"start=" ) && i < argc - 1)
  78. {
  79. if (!wcsicmp( argv[i + 1], L"boot" )) cp->start = SERVICE_BOOT_START;
  80. if (!wcsicmp( argv[i + 1], L"system" )) cp->start = SERVICE_SYSTEM_START;
  81. if (!wcsicmp( argv[i + 1], L"auto" )) cp->start = SERVICE_AUTO_START;
  82. if (!wcsicmp( argv[i + 1], L"demand" )) cp->start = SERVICE_DEMAND_START;
  83. if (!wcsicmp( argv[i + 1], L"disabled" )) cp->start = SERVICE_DISABLED;
  84. }
  85. if (!wcsicmp( argv[i], L"error=" ) && i < argc - 1)
  86. {
  87. if (!wcsicmp( argv[i + 1], L"normal" )) cp->error = SERVICE_ERROR_NORMAL;
  88. if (!wcsicmp( argv[i + 1], L"severe" )) cp->error = SERVICE_ERROR_SEVERE;
  89. if (!wcsicmp( argv[i + 1], L"critical" )) cp->error = SERVICE_ERROR_CRITICAL;
  90. if (!wcsicmp( argv[i + 1], L"ignore" )) cp->error = SERVICE_ERROR_IGNORE;
  91. }
  92. }
  93. if (!cp->binpath) return FALSE;
  94. return TRUE;
  95. }
  96. static BOOL parse_failure_actions( const WCHAR *arg, SERVICE_FAILURE_ACTIONSW *fa )
  97. {
  98. unsigned int i, count;
  99. WCHAR *actions, *p;
  100. actions = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( arg ) + 1) * sizeof(WCHAR) );
  101. if (!actions) return FALSE;
  102. lstrcpyW( actions, arg );
  103. for (p = actions, count = 0; *p; p++)
  104. {
  105. if (*p == '/')
  106. {
  107. count++;
  108. *p = 0;
  109. }
  110. }
  111. count = count / 2 + 1;
  112. fa->cActions = count;
  113. fa->lpsaActions = HeapAlloc( GetProcessHeap(), 0, fa->cActions * sizeof(SC_ACTION) );
  114. if (!fa->lpsaActions)
  115. {
  116. HeapFree( GetProcessHeap(), 0, actions );
  117. return FALSE;
  118. }
  119. p = actions;
  120. for (i = 0; i < count; i++)
  121. {
  122. if (!wcsicmp( p, L"run" )) fa->lpsaActions[i].Type = SC_ACTION_RUN_COMMAND;
  123. else if (!wcsicmp( p, L"restart" )) fa->lpsaActions[i].Type = SC_ACTION_RESTART;
  124. else if (!wcsicmp( p, L"reboot" )) fa->lpsaActions[i].Type = SC_ACTION_REBOOT;
  125. else fa->lpsaActions[i].Type = SC_ACTION_NONE;
  126. p += lstrlenW( p ) + 1;
  127. fa->lpsaActions[i].Delay = wcstol( p, NULL, 10 );
  128. p += lstrlenW( p ) + 1;
  129. }
  130. HeapFree( GetProcessHeap(), 0, actions );
  131. return TRUE;
  132. }
  133. static BOOL parse_failure_params( int argc, const WCHAR *argv[], SERVICE_FAILURE_ACTIONSW *fa )
  134. {
  135. unsigned int i;
  136. fa->dwResetPeriod = 0;
  137. fa->lpRebootMsg = NULL;
  138. fa->lpCommand = NULL;
  139. fa->cActions = 0;
  140. fa->lpsaActions = NULL;
  141. for (i = 0; i < argc; i++)
  142. {
  143. if (!wcsicmp( argv[i], L"reset=" ) && i < argc - 1) fa->dwResetPeriod = wcstol( argv[i + 1], NULL, 10 );
  144. if (!wcsicmp( argv[i], L"reboot=" ) && i < argc - 1) fa->lpRebootMsg = (WCHAR *)argv[i + 1];
  145. if (!wcsicmp( argv[i], L"command=" ) && i < argc - 1) fa->lpCommand = (WCHAR *)argv[i + 1];
  146. if (!wcsicmp( argv[i], L"actions=" ))
  147. {
  148. if (i == argc - 1) return FALSE;
  149. if (!parse_failure_actions( argv[i + 1], fa )) return FALSE;
  150. }
  151. }
  152. return TRUE;
  153. }
  154. static void usage( void )
  155. {
  156. WINE_MESSAGE( "Usage: sc command servicename [parameter= value ...]\n" );
  157. exit( 1 );
  158. }
  159. int __cdecl wmain( int argc, const WCHAR *argv[] )
  160. {
  161. SC_HANDLE manager, service;
  162. SERVICE_STATUS status;
  163. BOOL ret = FALSE;
  164. if (argc < 3) usage();
  165. if (argv[2][0] == '\\' && argv[2][1] == '\\')
  166. {
  167. WINE_FIXME("server argument not supported\n");
  168. return 1;
  169. }
  170. manager = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS );
  171. if (!manager)
  172. {
  173. WINE_ERR("failed to open service manager\n");
  174. return 1;
  175. }
  176. if (!wcsicmp( argv[1], L"create" ))
  177. {
  178. struct create_params cp;
  179. if (argc < 4)
  180. {
  181. CloseServiceHandle( manager );
  182. usage();
  183. }
  184. if (!parse_create_params( argc - 3, argv + 3, &cp ))
  185. {
  186. WINE_WARN("failed to parse create parameters\n");
  187. CloseServiceHandle( manager );
  188. return 1;
  189. }
  190. service = CreateServiceW( manager, argv[2], cp.displayname, SERVICE_ALL_ACCESS,
  191. cp.type, cp.start, cp.error, cp.binpath, cp.group, NULL,
  192. cp.depend, cp.obj, cp.password );
  193. if (service)
  194. {
  195. CloseServiceHandle( service );
  196. ret = TRUE;
  197. }
  198. else WINE_TRACE("failed to create service %u\n", GetLastError());
  199. }
  200. else if (!wcsicmp( argv[1], L"description" ))
  201. {
  202. service = OpenServiceW( manager, argv[2], SERVICE_CHANGE_CONFIG );
  203. if (service)
  204. {
  205. SERVICE_DESCRIPTIONW sd;
  206. sd.lpDescription = argc > 3 ? (WCHAR *)argv[3] : NULL;
  207. ret = ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &sd );
  208. if (!ret) WINE_TRACE("failed to set service description %u\n", GetLastError());
  209. CloseServiceHandle( service );
  210. }
  211. else WINE_TRACE("failed to open service %u\n", GetLastError());
  212. }
  213. else if (!wcsicmp( argv[1], L"failure" ))
  214. {
  215. service = OpenServiceW( manager, argv[2], SERVICE_CHANGE_CONFIG );
  216. if (service)
  217. {
  218. SERVICE_FAILURE_ACTIONSW sfa;
  219. if (parse_failure_params( argc - 3, argv + 3, &sfa ))
  220. {
  221. ret = ChangeServiceConfig2W( service, SERVICE_CONFIG_FAILURE_ACTIONS, &sfa );
  222. if (!ret) WINE_TRACE("failed to set service failure actions %u\n", GetLastError());
  223. HeapFree( GetProcessHeap(), 0, sfa.lpsaActions );
  224. }
  225. else
  226. WINE_WARN("failed to parse failure parameters\n");
  227. CloseServiceHandle( service );
  228. }
  229. else WINE_TRACE("failed to open service %u\n", GetLastError());
  230. }
  231. else if (!wcsicmp( argv[1], L"delete" ))
  232. {
  233. service = OpenServiceW( manager, argv[2], DELETE );
  234. if (service)
  235. {
  236. ret = DeleteService( service );
  237. if (!ret) WINE_TRACE("failed to delete service %u\n", GetLastError());
  238. CloseServiceHandle( service );
  239. }
  240. else WINE_TRACE("failed to open service %u\n", GetLastError());
  241. }
  242. else if (!wcsicmp( argv[1], L"start" ))
  243. {
  244. service = OpenServiceW( manager, argv[2], SERVICE_START );
  245. if (service)
  246. {
  247. ret = StartServiceW( service, argc - 3, argv + 3 );
  248. if (!ret) WINE_TRACE("failed to start service %u\n", GetLastError());
  249. CloseServiceHandle( service );
  250. }
  251. else WINE_TRACE("failed to open service %u\n", GetLastError());
  252. }
  253. else if (!wcsicmp( argv[1], L"stop" ))
  254. {
  255. service = OpenServiceW( manager, argv[2], SERVICE_STOP );
  256. if (service)
  257. {
  258. ret = ControlService( service, SERVICE_CONTROL_STOP, &status );
  259. if (!ret) WINE_TRACE("failed to stop service %u\n", GetLastError());
  260. CloseServiceHandle( service );
  261. }
  262. else WINE_TRACE("failed to open service %u\n", GetLastError());
  263. }
  264. else if (!wcsicmp( argv[1], L"sdset" ))
  265. {
  266. WINE_FIXME("SdSet command not supported, faking success\n");
  267. ret = TRUE;
  268. }
  269. else
  270. WINE_FIXME("command not supported\n");
  271. CloseServiceHandle( manager );
  272. return !ret;
  273. }