services.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. /*
  2. * Services - controls services keeps track of their state
  3. *
  4. * Copyright 2007 Google (Mikolaj Zalewski)
  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 <stdarg.h>
  22. #include <stdio.h>
  23. #include <assert.h>
  24. #include <windows.h>
  25. #include <winsvc.h>
  26. #include <winternl.h>
  27. #include <rpc.h>
  28. #include <userenv.h>
  29. #include <setupapi.h>
  30. #include "wine/debug.h"
  31. #include "wine/heap.h"
  32. #include "svcctl.h"
  33. #include "services.h"
  34. #define MAX_SERVICE_NAME 260
  35. WINE_DEFAULT_DEBUG_CHANNEL(service);
  36. struct scmdatabase *active_database;
  37. DWORD service_pipe_timeout = 10000;
  38. DWORD service_kill_timeout = 60000;
  39. static DWORD default_preshutdown_timeout = 180000;
  40. static DWORD autostart_delay = 120000;
  41. static void *environment = NULL;
  42. static HKEY service_current_key = NULL;
  43. static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
  44. static const WCHAR SZ_LOCAL_SYSTEM[] = {'L','o','c','a','l','S','y','s','t','e','m',0};
  45. /* Registry constants */
  46. static const WCHAR SZ_SERVICES_KEY[] = { 'S','y','s','t','e','m','\\',
  47. 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
  48. 'S','e','r','v','i','c','e','s',0 };
  49. /* Service key values names */
  50. static const WCHAR SZ_DISPLAY_NAME[] = {'D','i','s','p','l','a','y','N','a','m','e',0 };
  51. static const WCHAR SZ_TYPE[] = {'T','y','p','e',0 };
  52. static const WCHAR SZ_START[] = {'S','t','a','r','t',0 };
  53. static const WCHAR SZ_ERROR[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0 };
  54. static const WCHAR SZ_IMAGE_PATH[] = {'I','m','a','g','e','P','a','t','h',0};
  55. static const WCHAR SZ_GROUP[] = {'G','r','o','u','p',0};
  56. static const WCHAR SZ_DEPEND_ON_SERVICE[] = {'D','e','p','e','n','d','O','n','S','e','r','v','i','c','e',0};
  57. static const WCHAR SZ_DEPEND_ON_GROUP[] = {'D','e','p','e','n','d','O','n','G','r','o','u','p',0};
  58. static const WCHAR SZ_OBJECT_NAME[] = {'O','b','j','e','c','t','N','a','m','e',0};
  59. static const WCHAR SZ_TAG[] = {'T','a','g',0};
  60. static const WCHAR SZ_DESCRIPTION[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
  61. static const WCHAR SZ_PRESHUTDOWN[] = {'P','r','e','s','h','u','t','d','o','w','n','T','i','m','e','o','u','t',0};
  62. static const WCHAR SZ_WOW64[] = {'W','O','W','6','4',0};
  63. static const WCHAR SZ_DELAYED_AUTOSTART[] = {'D','e','l','a','y','e','d','A','u','t','o','S','t','a','r','t',0};
  64. static DWORD process_create(const WCHAR *name, struct process_entry **entry)
  65. {
  66. DWORD err;
  67. *entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**entry));
  68. if (!*entry)
  69. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  70. (*entry)->ref_count = 1;
  71. (*entry)->control_mutex = CreateMutexW(NULL, TRUE, NULL);
  72. if (!(*entry)->control_mutex)
  73. goto error;
  74. (*entry)->overlapped_event = CreateEventW(NULL, TRUE, FALSE, NULL);
  75. if (!(*entry)->overlapped_event)
  76. goto error;
  77. (*entry)->control_pipe = CreateNamedPipeW(name, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  78. PIPE_TYPE_BYTE|PIPE_WAIT, 1, 256, 256, 10000, NULL);
  79. if ((*entry)->control_pipe == INVALID_HANDLE_VALUE)
  80. goto error;
  81. /* all other fields are zero */
  82. return ERROR_SUCCESS;
  83. error:
  84. err = GetLastError();
  85. if ((*entry)->control_mutex)
  86. CloseHandle((*entry)->control_mutex);
  87. if ((*entry)->overlapped_event)
  88. CloseHandle((*entry)->overlapped_event);
  89. HeapFree(GetProcessHeap(), 0, *entry);
  90. return err;
  91. }
  92. static void free_process_entry(struct process_entry *entry)
  93. {
  94. CloseHandle(entry->process);
  95. CloseHandle(entry->control_mutex);
  96. CloseHandle(entry->control_pipe);
  97. CloseHandle(entry->overlapped_event);
  98. HeapFree(GetProcessHeap(), 0, entry);
  99. }
  100. DWORD service_create(LPCWSTR name, struct service_entry **entry)
  101. {
  102. *entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**entry));
  103. if (!*entry)
  104. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  105. (*entry)->name = strdupW(name);
  106. list_init(&(*entry)->handles);
  107. if (!(*entry)->name)
  108. {
  109. HeapFree(GetProcessHeap(), 0, *entry);
  110. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  111. }
  112. (*entry)->status_changed_event = CreateEventW(NULL, TRUE, FALSE, NULL);
  113. if (!(*entry)->status_changed_event)
  114. {
  115. HeapFree(GetProcessHeap(), 0, (*entry)->name);
  116. HeapFree(GetProcessHeap(), 0, *entry);
  117. return GetLastError();
  118. }
  119. (*entry)->ref_count = 1;
  120. (*entry)->status.dwCurrentState = SERVICE_STOPPED;
  121. (*entry)->status.dwWin32ExitCode = ERROR_SERVICE_NEVER_STARTED;
  122. (*entry)->preshutdown_timeout = default_preshutdown_timeout;
  123. /* all other fields are zero */
  124. return ERROR_SUCCESS;
  125. }
  126. void free_service_entry(struct service_entry *entry)
  127. {
  128. assert(list_empty(&entry->handles));
  129. CloseHandle(entry->status_changed_event);
  130. HeapFree(GetProcessHeap(), 0, entry->name);
  131. HeapFree(GetProcessHeap(), 0, entry->config.lpBinaryPathName);
  132. HeapFree(GetProcessHeap(), 0, entry->config.lpDependencies);
  133. HeapFree(GetProcessHeap(), 0, entry->config.lpLoadOrderGroup);
  134. HeapFree(GetProcessHeap(), 0, entry->config.lpServiceStartName);
  135. HeapFree(GetProcessHeap(), 0, entry->config.lpDisplayName);
  136. HeapFree(GetProcessHeap(), 0, entry->description);
  137. HeapFree(GetProcessHeap(), 0, entry->dependOnServices);
  138. HeapFree(GetProcessHeap(), 0, entry->dependOnGroups);
  139. if (entry->process) release_process(entry->process);
  140. HeapFree(GetProcessHeap(), 0, entry);
  141. }
  142. static DWORD load_service_config(HKEY hKey, struct service_entry *entry)
  143. {
  144. DWORD err, value = 0;
  145. WCHAR *wptr;
  146. if ((err = load_reg_string(hKey, SZ_IMAGE_PATH, TRUE, &entry->config.lpBinaryPathName)) != 0)
  147. return err;
  148. if ((err = load_reg_string(hKey, SZ_GROUP, 0, &entry->config.lpLoadOrderGroup)) != 0)
  149. return err;
  150. if ((err = load_reg_string(hKey, SZ_OBJECT_NAME, TRUE, &entry->config.lpServiceStartName)) != 0)
  151. return err;
  152. if ((err = load_reg_string(hKey, SZ_DISPLAY_NAME, 0, &entry->config.lpDisplayName)) != 0)
  153. return err;
  154. if ((err = load_reg_string(hKey, SZ_DESCRIPTION, 0, &entry->description)) != 0)
  155. return err;
  156. if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_SERVICE, TRUE, &entry->dependOnServices)) != 0)
  157. return err;
  158. if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_GROUP, FALSE, &entry->dependOnGroups)) != 0)
  159. return err;
  160. if ((err = load_reg_dword(hKey, SZ_TYPE, &entry->config.dwServiceType)) != 0)
  161. return err;
  162. if ((err = load_reg_dword(hKey, SZ_START, &entry->config.dwStartType)) != 0)
  163. return err;
  164. if ((err = load_reg_dword(hKey, SZ_ERROR, &entry->config.dwErrorControl)) != 0)
  165. return err;
  166. if ((err = load_reg_dword(hKey, SZ_TAG, &entry->config.dwTagId)) != 0)
  167. return err;
  168. if ((err = load_reg_dword(hKey, SZ_PRESHUTDOWN, &entry->preshutdown_timeout)) != 0)
  169. return err;
  170. if (load_reg_dword(hKey, SZ_WOW64, &value) == 0 && value == 1)
  171. entry->is_wow64 = TRUE;
  172. if (load_reg_dword(hKey, SZ_DELAYED_AUTOSTART, &value) == 0 && value == 1)
  173. entry->delayed_autostart = TRUE;
  174. WINE_TRACE("Image path = %s\n", wine_dbgstr_w(entry->config.lpBinaryPathName) );
  175. WINE_TRACE("Group = %s\n", wine_dbgstr_w(entry->config.lpLoadOrderGroup) );
  176. WINE_TRACE("Service account name = %s\n", wine_dbgstr_w(entry->config.lpServiceStartName) );
  177. WINE_TRACE("Display name = %s\n", wine_dbgstr_w(entry->config.lpDisplayName) );
  178. WINE_TRACE("Service dependencies : %s\n", entry->dependOnServices[0] ? "" : "(none)");
  179. for (wptr = entry->dependOnServices; *wptr; wptr += lstrlenW(wptr) + 1)
  180. WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
  181. WINE_TRACE("Group dependencies : %s\n", entry->dependOnGroups[0] ? "" : "(none)");
  182. for (wptr = entry->dependOnGroups; *wptr; wptr += lstrlenW(wptr) + 1)
  183. WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
  184. return ERROR_SUCCESS;
  185. }
  186. static DWORD reg_set_string_value(HKEY hKey, LPCWSTR value_name, LPCWSTR string)
  187. {
  188. if (!string)
  189. {
  190. DWORD err;
  191. err = RegDeleteValueW(hKey, value_name);
  192. if (err != ERROR_FILE_NOT_FOUND)
  193. return err;
  194. return ERROR_SUCCESS;
  195. }
  196. return RegSetValueExW(hKey, value_name, 0, REG_SZ, (const BYTE*)string, sizeof(WCHAR)*(lstrlenW(string) + 1));
  197. }
  198. static DWORD reg_set_multisz_value(HKEY hKey, LPCWSTR value_name, LPCWSTR string)
  199. {
  200. const WCHAR *ptr;
  201. if (!string)
  202. {
  203. DWORD err;
  204. err = RegDeleteValueW(hKey, value_name);
  205. if (err != ERROR_FILE_NOT_FOUND)
  206. return err;
  207. return ERROR_SUCCESS;
  208. }
  209. ptr = string;
  210. while (*ptr) ptr += lstrlenW(ptr) + 1;
  211. return RegSetValueExW(hKey, value_name, 0, REG_MULTI_SZ, (const BYTE*)string, sizeof(WCHAR)*(ptr - string + 1));
  212. }
  213. DWORD save_service_config(struct service_entry *entry)
  214. {
  215. DWORD err;
  216. HKEY hKey = NULL;
  217. err = RegCreateKeyW(entry->db->root_key, entry->name, &hKey);
  218. if (err != ERROR_SUCCESS)
  219. goto cleanup;
  220. if ((err = reg_set_string_value(hKey, SZ_DISPLAY_NAME, entry->config.lpDisplayName)) != 0)
  221. goto cleanup;
  222. if ((err = reg_set_string_value(hKey, SZ_IMAGE_PATH, entry->config.lpBinaryPathName)) != 0)
  223. goto cleanup;
  224. if ((err = reg_set_string_value(hKey, SZ_GROUP, entry->config.lpLoadOrderGroup)) != 0)
  225. goto cleanup;
  226. if ((err = reg_set_string_value(hKey, SZ_OBJECT_NAME, entry->config.lpServiceStartName)) != 0)
  227. goto cleanup;
  228. if ((err = reg_set_string_value(hKey, SZ_DESCRIPTION, entry->description)) != 0)
  229. goto cleanup;
  230. if ((err = reg_set_multisz_value(hKey, SZ_DEPEND_ON_SERVICE, entry->dependOnServices)) != 0)
  231. goto cleanup;
  232. if ((err = reg_set_multisz_value(hKey, SZ_DEPEND_ON_GROUP, entry->dependOnGroups)) != 0)
  233. goto cleanup;
  234. if ((err = RegSetValueExW(hKey, SZ_START, 0, REG_DWORD, (LPBYTE)&entry->config.dwStartType, sizeof(DWORD))) != 0)
  235. goto cleanup;
  236. if ((err = RegSetValueExW(hKey, SZ_ERROR, 0, REG_DWORD, (LPBYTE)&entry->config.dwErrorControl, sizeof(DWORD))) != 0)
  237. goto cleanup;
  238. if ((err = RegSetValueExW(hKey, SZ_TYPE, 0, REG_DWORD, (LPBYTE)&entry->config.dwServiceType, sizeof(DWORD))) != 0)
  239. goto cleanup;
  240. if ((err = RegSetValueExW(hKey, SZ_PRESHUTDOWN, 0, REG_DWORD, (LPBYTE)&entry->preshutdown_timeout, sizeof(DWORD))) != 0)
  241. goto cleanup;
  242. if ((err = RegSetValueExW(hKey, SZ_PRESHUTDOWN, 0, REG_DWORD, (LPBYTE)&entry->preshutdown_timeout, sizeof(DWORD))) != 0)
  243. goto cleanup;
  244. if (entry->is_wow64)
  245. {
  246. const DWORD is_wow64 = 1;
  247. if ((err = RegSetValueExW(hKey, SZ_WOW64, 0, REG_DWORD, (LPBYTE)&is_wow64, sizeof(DWORD))) != 0)
  248. goto cleanup;
  249. }
  250. if (entry->config.dwTagId)
  251. err = RegSetValueExW(hKey, SZ_TAG, 0, REG_DWORD, (LPBYTE)&entry->config.dwTagId, sizeof(DWORD));
  252. else
  253. err = RegDeleteValueW(hKey, SZ_TAG);
  254. if (err != 0 && err != ERROR_FILE_NOT_FOUND)
  255. goto cleanup;
  256. err = ERROR_SUCCESS;
  257. cleanup:
  258. RegCloseKey(hKey);
  259. return err;
  260. }
  261. static void scmdatabase_add_process(struct scmdatabase *db, struct process_entry *process)
  262. {
  263. process->db = db;
  264. list_add_tail(&db->processes, &process->entry);
  265. }
  266. static void scmdatabase_remove_process(struct scmdatabase *db, struct process_entry *process)
  267. {
  268. list_remove(&process->entry);
  269. process->entry.next = process->entry.prev = NULL;
  270. }
  271. DWORD scmdatabase_add_service(struct scmdatabase *db, struct service_entry *service)
  272. {
  273. int err;
  274. service->db = db;
  275. if ((err = save_service_config(service)) != ERROR_SUCCESS)
  276. {
  277. WINE_ERR("Couldn't store service configuration: error %u\n", err);
  278. return ERROR_GEN_FAILURE;
  279. }
  280. list_add_tail(&db->services, &service->entry);
  281. return ERROR_SUCCESS;
  282. }
  283. static void scmdatabase_remove_service(struct scmdatabase *db, struct service_entry *service)
  284. {
  285. RegDeleteTreeW(db->root_key, service->name);
  286. list_remove(&service->entry);
  287. service->entry.next = service->entry.prev = NULL;
  288. }
  289. static int __cdecl compare_tags(const void *a, const void *b)
  290. {
  291. struct service_entry *service_a = *(struct service_entry **)a;
  292. struct service_entry *service_b = *(struct service_entry **)b;
  293. return service_a->config.dwTagId - service_b->config.dwTagId;
  294. }
  295. static PTP_CLEANUP_GROUP delayed_autostart_cleanup;
  296. struct delayed_autostart_params
  297. {
  298. unsigned int count;
  299. struct service_entry **services;
  300. };
  301. static void CALLBACK delayed_autostart_cancel_callback(void *object, void *userdata)
  302. {
  303. struct delayed_autostart_params *params = object;
  304. while(params->count--)
  305. release_service(params->services[params->count]);
  306. heap_free(params->services);
  307. heap_free(params);
  308. }
  309. static void CALLBACK delayed_autostart_callback(TP_CALLBACK_INSTANCE *instance, void *context,
  310. TP_TIMER *timer)
  311. {
  312. struct delayed_autostart_params *params = context;
  313. struct service_entry *service;
  314. unsigned int i;
  315. DWORD err;
  316. scmdatabase_lock_startup(active_database, INFINITE);
  317. for (i = 0; i < params->count; i++)
  318. {
  319. service = params->services[i];
  320. if (service->status.dwCurrentState == SERVICE_STOPPED)
  321. {
  322. TRACE("Starting delayed auto-start service %s\n", debugstr_w(service->name));
  323. err = service_start(service, 0, NULL);
  324. if (err != ERROR_SUCCESS)
  325. FIXME("Delayed auto-start service %s failed to start: %d\n",
  326. wine_dbgstr_w(service->name), err);
  327. }
  328. release_service(service);
  329. }
  330. scmdatabase_unlock_startup(active_database);
  331. heap_free(params->services);
  332. heap_free(params);
  333. CloseThreadpoolTimer(timer);
  334. }
  335. static BOOL schedule_delayed_autostart(struct service_entry **services, unsigned int count)
  336. {
  337. struct delayed_autostart_params *params;
  338. TP_CALLBACK_ENVIRON environment;
  339. LARGE_INTEGER timestamp;
  340. TP_TIMER *timer;
  341. FILETIME ft;
  342. if (!(delayed_autostart_cleanup = CreateThreadpoolCleanupGroup()))
  343. {
  344. ERR("CreateThreadpoolCleanupGroup failed with error %u\n", GetLastError());
  345. return FALSE;
  346. }
  347. if (!(params = heap_alloc(sizeof(*params)))) return FALSE;
  348. params->count = count;
  349. params->services = services;
  350. memset(&environment, 0, sizeof(environment));
  351. environment.Version = 1;
  352. environment.CleanupGroup = delayed_autostart_cleanup;
  353. environment.CleanupGroupCancelCallback = delayed_autostart_cancel_callback;
  354. timestamp.QuadPart = (ULONGLONG)autostart_delay * -10000;
  355. ft.dwLowDateTime = timestamp.u.LowPart;
  356. ft.dwHighDateTime = timestamp.u.HighPart;
  357. if (!(timer = CreateThreadpoolTimer(delayed_autostart_callback, params, &environment)))
  358. {
  359. ERR("CreateThreadpoolWait failed: %u\n", GetLastError());
  360. heap_free(params);
  361. return FALSE;
  362. }
  363. SetThreadpoolTimer(timer, &ft, 0, 0);
  364. return TRUE;
  365. }
  366. static BOOL is_root_pnp_service(HDEVINFO set, const struct service_entry *service)
  367. {
  368. SP_DEVINFO_DATA device = {sizeof(device)};
  369. WCHAR name[MAX_SERVICE_NAME];
  370. unsigned int i;
  371. for (i = 0; SetupDiEnumDeviceInfo(set, i, &device); ++i)
  372. {
  373. if (SetupDiGetDeviceRegistryPropertyW(set, &device, SPDRP_SERVICE, NULL,
  374. (BYTE *)name, sizeof(name), NULL)
  375. && !wcsicmp(name, service->name))
  376. {
  377. return TRUE;
  378. }
  379. }
  380. return FALSE;
  381. }
  382. static void scmdatabase_autostart_services(struct scmdatabase *db)
  383. {
  384. static const WCHAR rootW[] = {'R','O','O','T',0};
  385. struct service_entry **services_list;
  386. unsigned int i = 0;
  387. unsigned int size = 32;
  388. unsigned int delayed_cnt = 0;
  389. struct service_entry *service;
  390. HDEVINFO set;
  391. services_list = HeapAlloc(GetProcessHeap(), 0, size * sizeof(services_list[0]));
  392. if (!services_list)
  393. return;
  394. if ((set = SetupDiGetClassDevsW( NULL, rootW, NULL, DIGCF_ALLCLASSES )) == INVALID_HANDLE_VALUE)
  395. WINE_ERR("Failed to enumerate devices, error %#x.\n", GetLastError());
  396. scmdatabase_lock(db);
  397. LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
  398. {
  399. if (service->config.dwStartType == SERVICE_BOOT_START ||
  400. service->config.dwStartType == SERVICE_SYSTEM_START ||
  401. service->config.dwStartType == SERVICE_AUTO_START ||
  402. (set != INVALID_HANDLE_VALUE && is_root_pnp_service(set, service)))
  403. {
  404. if (i+1 >= size)
  405. {
  406. struct service_entry **slist_new;
  407. size *= 2;
  408. slist_new = HeapReAlloc(GetProcessHeap(), 0, services_list, size * sizeof(services_list[0]));
  409. if (!slist_new)
  410. break;
  411. services_list = slist_new;
  412. }
  413. services_list[i++] = grab_service(service);
  414. }
  415. }
  416. size = i;
  417. scmdatabase_unlock(db);
  418. qsort(services_list, size, sizeof(services_list[0]), compare_tags);
  419. scmdatabase_lock_startup(db, INFINITE);
  420. for (i = 0; i < size; i++)
  421. {
  422. DWORD err;
  423. service = services_list[i];
  424. if (service->delayed_autostart)
  425. {
  426. TRACE("delayed starting %s\n", wine_dbgstr_w(service->name));
  427. services_list[delayed_cnt++] = service;
  428. continue;
  429. }
  430. err = service_start(service, 0, NULL);
  431. if (err != ERROR_SUCCESS)
  432. WINE_FIXME("Auto-start service %s failed to start: %d\n",
  433. wine_dbgstr_w(service->name), err);
  434. release_service(service);
  435. }
  436. scmdatabase_unlock_startup(db);
  437. if (!delayed_cnt || !schedule_delayed_autostart(services_list, delayed_cnt))
  438. heap_free(services_list);
  439. SetupDiDestroyDeviceInfoList(set);
  440. }
  441. static void scmdatabase_wait_terminate(struct scmdatabase *db)
  442. {
  443. struct list pending = LIST_INIT(pending);
  444. void *ptr;
  445. scmdatabase_lock(db);
  446. list_move_tail(&pending, &db->processes);
  447. while ((ptr = list_head(&pending)))
  448. {
  449. struct process_entry *process = grab_process(LIST_ENTRY(ptr, struct process_entry, entry));
  450. process_terminate(process);
  451. scmdatabase_unlock(db);
  452. WaitForSingleObject(process->process, INFINITE);
  453. scmdatabase_lock(db);
  454. list_remove(&process->entry);
  455. list_add_tail(&db->processes, &process->entry);
  456. release_process(process);
  457. }
  458. scmdatabase_unlock(db);
  459. }
  460. BOOL validate_service_name(LPCWSTR name)
  461. {
  462. return (name && name[0] && !wcschr(name, '/') && !wcschr(name, '\\'));
  463. }
  464. BOOL validate_service_config(struct service_entry *entry)
  465. {
  466. if (entry->config.dwServiceType & SERVICE_WIN32 && (entry->config.lpBinaryPathName == NULL || !entry->config.lpBinaryPathName[0]))
  467. {
  468. WINE_ERR("Service %s is Win32 but has no image path set\n", wine_dbgstr_w(entry->name));
  469. return FALSE;
  470. }
  471. switch (entry->config.dwServiceType)
  472. {
  473. case SERVICE_KERNEL_DRIVER:
  474. case SERVICE_FILE_SYSTEM_DRIVER:
  475. case SERVICE_WIN32_OWN_PROCESS:
  476. case SERVICE_WIN32_SHARE_PROCESS:
  477. /* No problem */
  478. break;
  479. case SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS:
  480. case SERVICE_WIN32_SHARE_PROCESS | SERVICE_INTERACTIVE_PROCESS:
  481. /* These can be only run as LocalSystem */
  482. if (entry->config.lpServiceStartName && wcsicmp(entry->config.lpServiceStartName, SZ_LOCAL_SYSTEM) != 0)
  483. {
  484. WINE_ERR("Service %s is interactive but has a start name\n", wine_dbgstr_w(entry->name));
  485. return FALSE;
  486. }
  487. break;
  488. default:
  489. WINE_ERR("Service %s has an unknown service type (0x%x)\n", wine_dbgstr_w(entry->name), entry->config.dwServiceType);
  490. return FALSE;
  491. }
  492. /* StartType can only be a single value (if several values are mixed the result is probably not what was intended) */
  493. if (entry->config.dwStartType > SERVICE_DISABLED)
  494. {
  495. WINE_ERR("Service %s has an unknown start type\n", wine_dbgstr_w(entry->name));
  496. return FALSE;
  497. }
  498. /* SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services */
  499. if (((entry->config.dwStartType == SERVICE_BOOT_START) || (entry->config.dwStartType == SERVICE_SYSTEM_START)) &&
  500. ((entry->config.dwServiceType & SERVICE_WIN32_OWN_PROCESS) || (entry->config.dwServiceType & SERVICE_WIN32_SHARE_PROCESS)))
  501. {
  502. WINE_ERR("Service %s - SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services\n", wine_dbgstr_w(entry->name));
  503. return FALSE;
  504. }
  505. if (entry->config.lpServiceStartName == NULL)
  506. entry->config.lpServiceStartName = strdupW(SZ_LOCAL_SYSTEM);
  507. return TRUE;
  508. }
  509. struct service_entry *scmdatabase_find_service(struct scmdatabase *db, LPCWSTR name)
  510. {
  511. struct service_entry *service;
  512. LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
  513. {
  514. if (wcsicmp(name, service->name) == 0)
  515. return service;
  516. }
  517. return NULL;
  518. }
  519. struct service_entry *scmdatabase_find_service_by_displayname(struct scmdatabase *db, LPCWSTR name)
  520. {
  521. struct service_entry *service;
  522. LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
  523. {
  524. if (service->config.lpDisplayName && wcsicmp(name, service->config.lpDisplayName) == 0)
  525. return service;
  526. }
  527. return NULL;
  528. }
  529. struct process_entry *grab_process(struct process_entry *process)
  530. {
  531. if (process)
  532. InterlockedIncrement(&process->ref_count);
  533. return process;
  534. }
  535. void release_process(struct process_entry *process)
  536. {
  537. struct scmdatabase *db = process->db;
  538. scmdatabase_lock(db);
  539. if (InterlockedDecrement(&process->ref_count) == 0)
  540. {
  541. scmdatabase_remove_process(db, process);
  542. free_process_entry(process);
  543. }
  544. scmdatabase_unlock(db);
  545. }
  546. struct service_entry *grab_service(struct service_entry *service)
  547. {
  548. if (service)
  549. InterlockedIncrement(&service->ref_count);
  550. return service;
  551. }
  552. void release_service(struct service_entry *service)
  553. {
  554. struct scmdatabase *db = service->db;
  555. scmdatabase_lock(db);
  556. if (InterlockedDecrement(&service->ref_count) == 0 && is_marked_for_delete(service))
  557. {
  558. scmdatabase_remove_service(db, service);
  559. free_service_entry(service);
  560. }
  561. scmdatabase_unlock(db);
  562. }
  563. static DWORD scmdatabase_create(struct scmdatabase **db)
  564. {
  565. DWORD err;
  566. *db = HeapAlloc(GetProcessHeap(), 0, sizeof(**db));
  567. if (!*db)
  568. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  569. (*db)->service_start_lock = FALSE;
  570. list_init(&(*db)->processes);
  571. list_init(&(*db)->services);
  572. InitializeCriticalSection(&(*db)->cs);
  573. (*db)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": scmdatabase");
  574. err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, SZ_SERVICES_KEY, 0, NULL,
  575. REG_OPTION_NON_VOLATILE, MAXIMUM_ALLOWED, NULL,
  576. &(*db)->root_key, NULL);
  577. if (err != ERROR_SUCCESS)
  578. HeapFree(GetProcessHeap(), 0, *db);
  579. return err;
  580. }
  581. static void scmdatabase_destroy(struct scmdatabase *db)
  582. {
  583. RegCloseKey(db->root_key);
  584. db->cs.DebugInfo->Spare[0] = 0;
  585. DeleteCriticalSection(&db->cs);
  586. HeapFree(GetProcessHeap(), 0, db);
  587. }
  588. static DWORD scmdatabase_load_services(struct scmdatabase *db)
  589. {
  590. DWORD err;
  591. int i;
  592. for (i = 0; TRUE; i++)
  593. {
  594. WCHAR szName[MAX_SERVICE_NAME];
  595. struct service_entry *entry;
  596. HKEY hServiceKey;
  597. err = RegEnumKeyW(db->root_key, i, szName, MAX_SERVICE_NAME);
  598. if (err == ERROR_NO_MORE_ITEMS)
  599. break;
  600. if (err != 0)
  601. {
  602. WINE_ERR("Error %d reading key %d name - skipping\n", err, i);
  603. continue;
  604. }
  605. err = service_create(szName, &entry);
  606. if (err != ERROR_SUCCESS)
  607. break;
  608. WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName));
  609. err = RegOpenKeyExW(db->root_key, szName, 0, KEY_READ, &hServiceKey);
  610. if (err == ERROR_SUCCESS)
  611. {
  612. err = load_service_config(hServiceKey, entry);
  613. RegCloseKey(hServiceKey);
  614. }
  615. if (err != ERROR_SUCCESS)
  616. {
  617. WINE_ERR("Error %d reading registry key for service %s - skipping\n", err, wine_dbgstr_w(szName));
  618. free_service_entry(entry);
  619. continue;
  620. }
  621. if (entry->config.dwServiceType == 0)
  622. {
  623. /* Maybe an application only wrote some configuration in the service key. Continue silently */
  624. WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName));
  625. free_service_entry(entry);
  626. continue;
  627. }
  628. if (!validate_service_config(entry))
  629. {
  630. WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName));
  631. free_service_entry(entry);
  632. continue;
  633. }
  634. entry->status.dwServiceType = entry->config.dwServiceType;
  635. entry->db = db;
  636. list_add_tail(&db->services, &entry->entry);
  637. release_service(entry);
  638. }
  639. return ERROR_SUCCESS;
  640. }
  641. BOOL scmdatabase_lock_startup(struct scmdatabase *db, int timeout)
  642. {
  643. while (InterlockedCompareExchange(&db->service_start_lock, TRUE, FALSE))
  644. {
  645. if (timeout != INFINITE)
  646. {
  647. timeout -= 10;
  648. if (timeout <= 0) return FALSE;
  649. }
  650. Sleep(10);
  651. }
  652. return TRUE;
  653. }
  654. void scmdatabase_unlock_startup(struct scmdatabase *db)
  655. {
  656. InterlockedCompareExchange(&db->service_start_lock, FALSE, TRUE);
  657. }
  658. void scmdatabase_lock(struct scmdatabase *db)
  659. {
  660. EnterCriticalSection(&db->cs);
  661. }
  662. void scmdatabase_unlock(struct scmdatabase *db)
  663. {
  664. LeaveCriticalSection(&db->cs);
  665. }
  666. void service_lock(struct service_entry *service)
  667. {
  668. EnterCriticalSection(&service->db->cs);
  669. }
  670. void service_unlock(struct service_entry *service)
  671. {
  672. LeaveCriticalSection(&service->db->cs);
  673. }
  674. /* only one service started at a time, so there is no race on the registry
  675. * value here */
  676. static LPWSTR service_get_pipe_name(void)
  677. {
  678. static const WCHAR format[] = { '\\','\\','.','\\','p','i','p','e','\\',
  679. 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
  680. static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
  681. static DWORD service_current = 0;
  682. DWORD len, value = -1;
  683. LONG ret;
  684. DWORD type;
  685. len = sizeof(value);
  686. ret = RegQueryValueExW(service_current_key, NULL, NULL, &type,
  687. (BYTE *)&value, &len);
  688. if (ret == ERROR_SUCCESS && type == REG_DWORD)
  689. service_current = max(service_current, value + 1);
  690. RegSetValueExW(service_current_key, NULL, 0, REG_DWORD,
  691. (BYTE *)&service_current, sizeof(service_current));
  692. swprintf(name, ARRAY_SIZE(name), format, service_current);
  693. service_current++;
  694. return name;
  695. }
  696. static DWORD get_service_binary_path(const struct service_entry *service_entry, WCHAR **path)
  697. {
  698. DWORD size = ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, NULL, 0);
  699. *path = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
  700. if (!*path)
  701. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  702. ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, *path, size);
  703. /* if service image is configured to systemdir, redirect it to wow64 systemdir */
  704. if (service_entry->is_wow64 && !(service_entry->config.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER)))
  705. {
  706. WCHAR system_dir[MAX_PATH], *redirected;
  707. DWORD len;
  708. GetSystemDirectoryW( system_dir, MAX_PATH );
  709. len = lstrlenW( system_dir );
  710. if (wcsnicmp( system_dir, *path, len ))
  711. return ERROR_SUCCESS;
  712. GetSystemWow64DirectoryW( system_dir, MAX_PATH );
  713. redirected = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( *path ) + lstrlenW( system_dir ))*sizeof(WCHAR));
  714. if (!redirected)
  715. {
  716. HeapFree( GetProcessHeap(), 0, *path );
  717. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  718. }
  719. lstrcpyW( redirected, system_dir );
  720. lstrcatW( redirected, &(*path)[len] );
  721. HeapFree( GetProcessHeap(), 0, *path );
  722. *path = redirected;
  723. TRACE("redirected to %s\n", debugstr_w(redirected));
  724. }
  725. return ERROR_SUCCESS;
  726. }
  727. static DWORD get_winedevice_binary_path(struct service_entry *service_entry, WCHAR **path, BOOL *is_wow64)
  728. {
  729. static const WCHAR winedeviceW[] = {'\\','w','i','n','e','d','e','v','i','c','e','.','e','x','e',0};
  730. WCHAR system_dir[MAX_PATH];
  731. DWORD type;
  732. if (!is_win64)
  733. *is_wow64 = FALSE;
  734. else if (GetBinaryTypeW(*path, &type))
  735. *is_wow64 = (type == SCS_32BIT_BINARY);
  736. else
  737. *is_wow64 = service_entry->is_wow64;
  738. GetSystemDirectoryW(system_dir, MAX_PATH);
  739. HeapFree(GetProcessHeap(), 0, *path);
  740. if (!(*path = HeapAlloc(GetProcessHeap(), 0, lstrlenW(system_dir) * sizeof(WCHAR) + sizeof(winedeviceW))))
  741. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  742. lstrcpyW(*path, system_dir);
  743. lstrcatW(*path, winedeviceW);
  744. return ERROR_SUCCESS;
  745. }
  746. static struct process_entry *get_winedevice_process(struct service_entry *service_entry, WCHAR *path, BOOL is_wow64)
  747. {
  748. struct service_entry *winedevice_entry;
  749. if (!service_entry->config.lpLoadOrderGroup)
  750. return NULL;
  751. LIST_FOR_EACH_ENTRY(winedevice_entry, &service_entry->db->services, struct service_entry, entry)
  752. {
  753. if (winedevice_entry->status.dwCurrentState != SERVICE_START_PENDING &&
  754. winedevice_entry->status.dwCurrentState != SERVICE_RUNNING) continue;
  755. if (!winedevice_entry->process) continue;
  756. if (winedevice_entry->is_wow64 != is_wow64) continue;
  757. if (!winedevice_entry->config.lpBinaryPathName) continue;
  758. if (lstrcmpW(winedevice_entry->config.lpBinaryPathName, path)) continue;
  759. if (!winedevice_entry->config.lpLoadOrderGroup) continue;
  760. if (lstrcmpW(winedevice_entry->config.lpLoadOrderGroup, service_entry->config.lpLoadOrderGroup)) continue;
  761. return grab_process(winedevice_entry->process);
  762. }
  763. return NULL;
  764. }
  765. static DWORD add_winedevice_service(const struct service_entry *service, WCHAR *path, BOOL is_wow64,
  766. struct service_entry **entry)
  767. {
  768. static const WCHAR format[] = {'W','i','n','e','d','e','v','i','c','e','%','u',0};
  769. static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
  770. static DWORD current = 0;
  771. struct scmdatabase *db = service->db;
  772. DWORD err;
  773. for (;;)
  774. {
  775. swprintf(name, ARRAY_SIZE(name), format, ++current);
  776. if (!scmdatabase_find_service(db, name)) break;
  777. }
  778. err = service_create(name, entry);
  779. if (err != ERROR_SUCCESS)
  780. return err;
  781. (*entry)->is_wow64 = is_wow64;
  782. (*entry)->config.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  783. (*entry)->config.dwStartType = SERVICE_DEMAND_START;
  784. (*entry)->status.dwServiceType = (*entry)->config.dwServiceType;
  785. if (!((*entry)->config.lpBinaryPathName = strdupW(path)))
  786. goto error;
  787. if (!((*entry)->config.lpServiceStartName = strdupW(SZ_LOCAL_SYSTEM)))
  788. goto error;
  789. if (!((*entry)->config.lpDisplayName = strdupW(name)))
  790. goto error;
  791. if (service->config.lpLoadOrderGroup &&
  792. !((*entry)->config.lpLoadOrderGroup = strdupW(service->config.lpLoadOrderGroup)))
  793. goto error;
  794. (*entry)->db = db;
  795. list_add_tail(&db->services, &(*entry)->entry);
  796. mark_for_delete(*entry);
  797. return ERROR_SUCCESS;
  798. error:
  799. free_service_entry(*entry);
  800. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  801. }
  802. static DWORD service_start_process(struct service_entry *service_entry, struct process_entry **new_process,
  803. BOOL *shared_process)
  804. {
  805. struct process_entry *process;
  806. PROCESS_INFORMATION pi;
  807. STARTUPINFOW si;
  808. BOOL is_wow64 = FALSE;
  809. HANDLE token;
  810. WCHAR *path;
  811. DWORD err;
  812. BOOL r;
  813. service_lock(service_entry);
  814. if ((process = service_entry->process))
  815. {
  816. if (WaitForSingleObject(process->process, 0) == WAIT_TIMEOUT)
  817. {
  818. service_unlock(service_entry);
  819. return ERROR_SERVICE_ALREADY_RUNNING;
  820. }
  821. service_entry->process = NULL;
  822. process->use_count--;
  823. release_process(process);
  824. }
  825. service_entry->force_shutdown = FALSE;
  826. if ((err = get_service_binary_path(service_entry, &path)))
  827. {
  828. service_unlock(service_entry);
  829. return err;
  830. }
  831. if (service_entry->config.dwServiceType == SERVICE_KERNEL_DRIVER ||
  832. service_entry->config.dwServiceType == SERVICE_FILE_SYSTEM_DRIVER)
  833. {
  834. struct service_entry *winedevice_entry;
  835. WCHAR *group;
  836. if ((err = get_winedevice_binary_path(service_entry, &path, &is_wow64)))
  837. {
  838. service_unlock(service_entry);
  839. HeapFree(GetProcessHeap(), 0, path);
  840. return err;
  841. }
  842. if ((process = get_winedevice_process(service_entry, path, is_wow64)))
  843. {
  844. HeapFree(GetProcessHeap(), 0, path);
  845. goto found;
  846. }
  847. err = add_winedevice_service(service_entry, path, is_wow64, &winedevice_entry);
  848. HeapFree(GetProcessHeap(), 0, path);
  849. if (err != ERROR_SUCCESS)
  850. {
  851. service_unlock(service_entry);
  852. return err;
  853. }
  854. group = strdupW(winedevice_entry->config.lpLoadOrderGroup);
  855. service_unlock(service_entry);
  856. err = service_start(winedevice_entry, group != NULL, (const WCHAR **)&group);
  857. HeapFree(GetProcessHeap(), 0, group);
  858. if (err != ERROR_SUCCESS)
  859. {
  860. release_service(winedevice_entry);
  861. return err;
  862. }
  863. service_lock(service_entry);
  864. process = grab_process(winedevice_entry->process);
  865. release_service(winedevice_entry);
  866. if (!process)
  867. {
  868. service_unlock(service_entry);
  869. return ERROR_SERVICE_REQUEST_TIMEOUT;
  870. }
  871. found:
  872. service_entry->status.dwCurrentState = SERVICE_START_PENDING;
  873. service_entry->status.dwControlsAccepted = 0;
  874. ResetEvent(service_entry->status_changed_event);
  875. service_entry->process = grab_process(process);
  876. service_entry->shared_process = *shared_process = TRUE;
  877. process->use_count++;
  878. service_unlock(service_entry);
  879. err = WaitForSingleObject(process->control_mutex, 30000);
  880. if (err != WAIT_OBJECT_0)
  881. {
  882. release_process(process);
  883. return ERROR_SERVICE_REQUEST_TIMEOUT;
  884. }
  885. *new_process = process;
  886. return ERROR_SUCCESS;
  887. }
  888. if ((err = process_create(service_get_pipe_name(), &process)))
  889. {
  890. WINE_ERR("failed to create process object for %s, error = %u\n",
  891. wine_dbgstr_w(service_entry->name), err);
  892. service_unlock(service_entry);
  893. HeapFree(GetProcessHeap(), 0, path);
  894. return err;
  895. }
  896. ZeroMemory(&si, sizeof(STARTUPINFOW));
  897. si.cb = sizeof(STARTUPINFOW);
  898. if (!(service_entry->config.dwServiceType & SERVICE_INTERACTIVE_PROCESS))
  899. {
  900. static WCHAR desktopW[] = {'_','_','w','i','n','e','s','e','r','v','i','c','e','_','w','i','n','s','t','a','t','i','o','n','\\','D','e','f','a','u','l','t',0};
  901. si.lpDesktop = desktopW;
  902. }
  903. if (!environment && OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &token))
  904. {
  905. WCHAR val[16];
  906. CreateEnvironmentBlock(&environment, token, FALSE);
  907. if (GetEnvironmentVariableW( L"WINEBOOTSTRAPMODE", val, ARRAY_SIZE(val) ))
  908. {
  909. UNICODE_STRING name, value;
  910. RtlInitUnicodeString( &name, L"WINEBOOTSTRAPMODE" );
  911. RtlInitUnicodeString( &value, val );
  912. RtlSetEnvironmentVariable( (WCHAR **)&environment, &name, &value );
  913. }
  914. CloseHandle(token);
  915. }
  916. service_entry->status.dwCurrentState = SERVICE_START_PENDING;
  917. service_entry->status.dwControlsAccepted = 0;
  918. ResetEvent(service_entry->status_changed_event);
  919. scmdatabase_add_process(service_entry->db, process);
  920. service_entry->process = grab_process(process);
  921. service_entry->shared_process = *shared_process = FALSE;
  922. process->use_count++;
  923. service_unlock(service_entry);
  924. r = CreateProcessW(NULL, path, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, environment, NULL, &si, &pi);
  925. HeapFree(GetProcessHeap(), 0, path);
  926. if (!r)
  927. {
  928. err = GetLastError();
  929. process_terminate(process);
  930. release_process(process);
  931. return err;
  932. }
  933. process->process_id = pi.dwProcessId;
  934. process->process = pi.hProcess;
  935. CloseHandle( pi.hThread );
  936. *new_process = process;
  937. return ERROR_SUCCESS;
  938. }
  939. static DWORD service_wait_for_startup(struct service_entry *service, struct process_entry *process)
  940. {
  941. HANDLE handles[2] = { service->status_changed_event, process->process };
  942. DWORD result;
  943. result = WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout );
  944. if (result != WAIT_OBJECT_0)
  945. return ERROR_SERVICE_REQUEST_TIMEOUT;
  946. service_lock(service);
  947. result = service->status.dwCurrentState;
  948. service_unlock(service);
  949. return (result == SERVICE_START_PENDING || result == SERVICE_RUNNING) ?
  950. ERROR_SUCCESS : ERROR_SERVICE_REQUEST_TIMEOUT;
  951. }
  952. /******************************************************************************
  953. * process_send_start_message
  954. */
  955. static DWORD process_send_start_message(struct process_entry *process, BOOL shared_process,
  956. const WCHAR *name, const WCHAR **argv, DWORD argc)
  957. {
  958. OVERLAPPED overlapped;
  959. DWORD i, len, result;
  960. WCHAR *str, *p;
  961. WINE_TRACE("%p %s %p %d\n", process, wine_dbgstr_w(name), argv, argc);
  962. overlapped.hEvent = process->overlapped_event;
  963. if (!ConnectNamedPipe(process->control_pipe, &overlapped))
  964. {
  965. if (GetLastError() == ERROR_IO_PENDING)
  966. {
  967. HANDLE handles[2];
  968. handles[0] = process->overlapped_event;
  969. handles[1] = process->process;
  970. if (WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout ) != WAIT_OBJECT_0)
  971. CancelIo(process->control_pipe);
  972. if (!HasOverlappedIoCompleted( &overlapped ))
  973. {
  974. WINE_ERR("service %s failed to start\n", wine_dbgstr_w(name));
  975. return ERROR_SERVICE_REQUEST_TIMEOUT;
  976. }
  977. }
  978. else if (GetLastError() != ERROR_PIPE_CONNECTED)
  979. {
  980. WINE_ERR("pipe connect failed\n");
  981. return ERROR_SERVICE_REQUEST_TIMEOUT;
  982. }
  983. }
  984. len = lstrlenW(name) + 1;
  985. for (i = 0; i < argc; i++)
  986. len += lstrlenW(argv[i])+1;
  987. len = (len + 1) * sizeof(WCHAR);
  988. if (!(str = HeapAlloc(GetProcessHeap(), 0, len)))
  989. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  990. p = str;
  991. lstrcpyW(p, name);
  992. p += lstrlenW(name) + 1;
  993. for (i = 0; i < argc; i++)
  994. {
  995. lstrcpyW(p, argv[i]);
  996. p += lstrlenW(p) + 1;
  997. }
  998. *p = 0;
  999. if (!process_send_control(process, shared_process, name,
  1000. SERVICE_CONTROL_START, (const BYTE *)str, len, &result))
  1001. result = ERROR_SERVICE_REQUEST_TIMEOUT;
  1002. HeapFree(GetProcessHeap(), 0, str);
  1003. return result;
  1004. }
  1005. DWORD service_start(struct service_entry *service, DWORD service_argc, LPCWSTR *service_argv)
  1006. {
  1007. struct process_entry *process = NULL;
  1008. BOOL shared_process;
  1009. DWORD err;
  1010. err = service_start_process(service, &process, &shared_process);
  1011. if (err == ERROR_SUCCESS)
  1012. {
  1013. err = process_send_start_message(process, shared_process, service->name, service_argv, service_argc);
  1014. if (err == ERROR_SUCCESS)
  1015. err = service_wait_for_startup(service, process);
  1016. if (err != ERROR_SUCCESS)
  1017. {
  1018. service_lock(service);
  1019. if (service->process)
  1020. {
  1021. service->status.dwCurrentState = SERVICE_STOPPED;
  1022. service->process = NULL;
  1023. if (!--process->use_count) process_terminate(process);
  1024. release_process(process);
  1025. }
  1026. service_unlock(service);
  1027. }
  1028. ReleaseMutex(process->control_mutex);
  1029. release_process(process);
  1030. }
  1031. WINE_TRACE("returning %d\n", err);
  1032. return err;
  1033. }
  1034. void process_terminate(struct process_entry *process)
  1035. {
  1036. struct scmdatabase *db = process->db;
  1037. struct service_entry *service;
  1038. scmdatabase_lock(db);
  1039. TerminateProcess(process->process, 0);
  1040. LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
  1041. {
  1042. if (service->process != process) continue;
  1043. service->status.dwCurrentState = SERVICE_STOPPED;
  1044. service->process = NULL;
  1045. process->use_count--;
  1046. release_process(process);
  1047. }
  1048. scmdatabase_unlock(db);
  1049. }
  1050. static void load_registry_parameters(void)
  1051. {
  1052. static const WCHAR controlW[] =
  1053. { 'S','y','s','t','e','m','\\',
  1054. 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
  1055. 'C','o','n','t','r','o','l',0 };
  1056. static const WCHAR pipetimeoutW[] =
  1057. {'S','e','r','v','i','c','e','s','P','i','p','e','T','i','m','e','o','u','t',0};
  1058. static const WCHAR killtimeoutW[] =
  1059. {'W','a','i','t','T','o','K','i','l','l','S','e','r','v','i','c','e','T','i','m','e','o','u','t',0};
  1060. static const WCHAR autostartdelayW[] =
  1061. {'A','u','t','o','S','t','a','r','t','D','e','l','a','y',0};
  1062. HKEY key;
  1063. WCHAR buffer[64];
  1064. DWORD type, count, val;
  1065. if (RegOpenKeyW( HKEY_LOCAL_MACHINE, controlW, &key )) return;
  1066. count = sizeof(buffer);
  1067. if (!RegQueryValueExW( key, pipetimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
  1068. type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
  1069. service_pipe_timeout = val;
  1070. count = sizeof(buffer);
  1071. if (!RegQueryValueExW( key, killtimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
  1072. type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
  1073. service_kill_timeout = val;
  1074. count = sizeof(val);
  1075. if (!RegQueryValueExW( key, autostartdelayW, NULL, &type, (BYTE *)&val, &count ) && type == REG_DWORD)
  1076. autostart_delay = val;
  1077. RegCloseKey( key );
  1078. }
  1079. int __cdecl main(int argc, char *argv[])
  1080. {
  1081. static const WCHAR service_current_key_str[] = { 'S','Y','S','T','E','M','\\',
  1082. 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
  1083. 'C','o','n','t','r','o','l','\\',
  1084. 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
  1085. static const WCHAR svcctl_started_event[] = SVCCTL_STARTED_EVENT;
  1086. HANDLE started_event;
  1087. DWORD err;
  1088. started_event = CreateEventW(NULL, TRUE, FALSE, svcctl_started_event);
  1089. err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, service_current_key_str, 0,
  1090. NULL, REG_OPTION_VOLATILE, KEY_SET_VALUE | KEY_QUERY_VALUE, NULL,
  1091. &service_current_key, NULL);
  1092. if (err != ERROR_SUCCESS)
  1093. return err;
  1094. load_registry_parameters();
  1095. err = scmdatabase_create(&active_database);
  1096. if (err != ERROR_SUCCESS)
  1097. return err;
  1098. if ((err = scmdatabase_load_services(active_database)) != ERROR_SUCCESS)
  1099. return err;
  1100. if ((err = RPC_Init()) == ERROR_SUCCESS)
  1101. {
  1102. scmdatabase_autostart_services(active_database);
  1103. SetEvent(started_event);
  1104. WaitForSingleObject(exit_event, INFINITE);
  1105. scmdatabase_wait_terminate(active_database);
  1106. if (delayed_autostart_cleanup)
  1107. {
  1108. CloseThreadpoolCleanupGroupMembers(delayed_autostart_cleanup, TRUE, NULL);
  1109. CloseThreadpoolCleanupGroup(delayed_autostart_cleanup);
  1110. }
  1111. RPC_Stop();
  1112. }
  1113. scmdatabase_destroy(active_database);
  1114. if (environment)
  1115. DestroyEnvironmentBlock(environment);
  1116. WINE_TRACE("services.exe exited with code %d\n", err);
  1117. return err;
  1118. }