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. scmdatabase_unlock(db);
  451. WaitForSingleObject(process->process, INFINITE);
  452. scmdatabase_lock(db);
  453. list_remove(&process->entry);
  454. list_add_tail(&db->processes, &process->entry);
  455. release_process(process);
  456. }
  457. scmdatabase_unlock(db);
  458. }
  459. BOOL validate_service_name(LPCWSTR name)
  460. {
  461. return (name && name[0] && !wcschr(name, '/') && !wcschr(name, '\\'));
  462. }
  463. BOOL validate_service_config(struct service_entry *entry)
  464. {
  465. if (entry->config.dwServiceType & SERVICE_WIN32 && (entry->config.lpBinaryPathName == NULL || !entry->config.lpBinaryPathName[0]))
  466. {
  467. WINE_ERR("Service %s is Win32 but has no image path set\n", wine_dbgstr_w(entry->name));
  468. return FALSE;
  469. }
  470. switch (entry->config.dwServiceType)
  471. {
  472. case SERVICE_KERNEL_DRIVER:
  473. case SERVICE_FILE_SYSTEM_DRIVER:
  474. case SERVICE_WIN32_OWN_PROCESS:
  475. case SERVICE_WIN32_SHARE_PROCESS:
  476. /* No problem */
  477. break;
  478. case SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS:
  479. case SERVICE_WIN32_SHARE_PROCESS | SERVICE_INTERACTIVE_PROCESS:
  480. /* These can be only run as LocalSystem */
  481. if (entry->config.lpServiceStartName && wcsicmp(entry->config.lpServiceStartName, SZ_LOCAL_SYSTEM) != 0)
  482. {
  483. WINE_ERR("Service %s is interactive but has a start name\n", wine_dbgstr_w(entry->name));
  484. return FALSE;
  485. }
  486. break;
  487. default:
  488. WINE_ERR("Service %s has an unknown service type (0x%x)\n", wine_dbgstr_w(entry->name), entry->config.dwServiceType);
  489. return FALSE;
  490. }
  491. /* StartType can only be a single value (if several values are mixed the result is probably not what was intended) */
  492. if (entry->config.dwStartType > SERVICE_DISABLED)
  493. {
  494. WINE_ERR("Service %s has an unknown start type\n", wine_dbgstr_w(entry->name));
  495. return FALSE;
  496. }
  497. /* SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services */
  498. if (((entry->config.dwStartType == SERVICE_BOOT_START) || (entry->config.dwStartType == SERVICE_SYSTEM_START)) &&
  499. ((entry->config.dwServiceType & SERVICE_WIN32_OWN_PROCESS) || (entry->config.dwServiceType & SERVICE_WIN32_SHARE_PROCESS)))
  500. {
  501. WINE_ERR("Service %s - SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services\n", wine_dbgstr_w(entry->name));
  502. return FALSE;
  503. }
  504. if (entry->config.lpServiceStartName == NULL)
  505. entry->config.lpServiceStartName = strdupW(SZ_LOCAL_SYSTEM);
  506. return TRUE;
  507. }
  508. struct service_entry *scmdatabase_find_service(struct scmdatabase *db, LPCWSTR name)
  509. {
  510. struct service_entry *service;
  511. LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
  512. {
  513. if (wcsicmp(name, service->name) == 0)
  514. return service;
  515. }
  516. return NULL;
  517. }
  518. struct service_entry *scmdatabase_find_service_by_displayname(struct scmdatabase *db, LPCWSTR name)
  519. {
  520. struct service_entry *service;
  521. LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
  522. {
  523. if (service->config.lpDisplayName && wcsicmp(name, service->config.lpDisplayName) == 0)
  524. return service;
  525. }
  526. return NULL;
  527. }
  528. struct process_entry *grab_process(struct process_entry *process)
  529. {
  530. if (process)
  531. InterlockedIncrement(&process->ref_count);
  532. return process;
  533. }
  534. void release_process(struct process_entry *process)
  535. {
  536. struct scmdatabase *db = process->db;
  537. scmdatabase_lock(db);
  538. if (InterlockedDecrement(&process->ref_count) == 0)
  539. {
  540. scmdatabase_remove_process(db, process);
  541. free_process_entry(process);
  542. }
  543. scmdatabase_unlock(db);
  544. }
  545. struct service_entry *grab_service(struct service_entry *service)
  546. {
  547. if (service)
  548. InterlockedIncrement(&service->ref_count);
  549. return service;
  550. }
  551. void release_service(struct service_entry *service)
  552. {
  553. struct scmdatabase *db = service->db;
  554. scmdatabase_lock(db);
  555. if (InterlockedDecrement(&service->ref_count) == 0 && is_marked_for_delete(service))
  556. {
  557. scmdatabase_remove_service(db, service);
  558. free_service_entry(service);
  559. }
  560. scmdatabase_unlock(db);
  561. }
  562. static DWORD scmdatabase_create(struct scmdatabase **db)
  563. {
  564. DWORD err;
  565. *db = HeapAlloc(GetProcessHeap(), 0, sizeof(**db));
  566. if (!*db)
  567. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  568. (*db)->service_start_lock = FALSE;
  569. list_init(&(*db)->processes);
  570. list_init(&(*db)->services);
  571. InitializeCriticalSection(&(*db)->cs);
  572. (*db)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": scmdatabase");
  573. err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, SZ_SERVICES_KEY, 0, NULL,
  574. REG_OPTION_NON_VOLATILE, MAXIMUM_ALLOWED, NULL,
  575. &(*db)->root_key, NULL);
  576. if (err != ERROR_SUCCESS)
  577. HeapFree(GetProcessHeap(), 0, *db);
  578. return err;
  579. }
  580. static void scmdatabase_destroy(struct scmdatabase *db)
  581. {
  582. RegCloseKey(db->root_key);
  583. db->cs.DebugInfo->Spare[0] = 0;
  584. DeleteCriticalSection(&db->cs);
  585. HeapFree(GetProcessHeap(), 0, db);
  586. }
  587. static DWORD scmdatabase_load_services(struct scmdatabase *db)
  588. {
  589. DWORD err;
  590. int i;
  591. for (i = 0; TRUE; i++)
  592. {
  593. WCHAR szName[MAX_SERVICE_NAME];
  594. struct service_entry *entry;
  595. HKEY hServiceKey;
  596. err = RegEnumKeyW(db->root_key, i, szName, MAX_SERVICE_NAME);
  597. if (err == ERROR_NO_MORE_ITEMS)
  598. break;
  599. if (err != 0)
  600. {
  601. WINE_ERR("Error %d reading key %d name - skipping\n", err, i);
  602. continue;
  603. }
  604. err = service_create(szName, &entry);
  605. if (err != ERROR_SUCCESS)
  606. break;
  607. WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName));
  608. err = RegOpenKeyExW(db->root_key, szName, 0, KEY_READ, &hServiceKey);
  609. if (err == ERROR_SUCCESS)
  610. {
  611. err = load_service_config(hServiceKey, entry);
  612. RegCloseKey(hServiceKey);
  613. }
  614. if (err != ERROR_SUCCESS)
  615. {
  616. WINE_ERR("Error %d reading registry key for service %s - skipping\n", err, wine_dbgstr_w(szName));
  617. free_service_entry(entry);
  618. continue;
  619. }
  620. if (entry->config.dwServiceType == 0)
  621. {
  622. /* Maybe an application only wrote some configuration in the service key. Continue silently */
  623. WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName));
  624. free_service_entry(entry);
  625. continue;
  626. }
  627. if (!validate_service_config(entry))
  628. {
  629. WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName));
  630. free_service_entry(entry);
  631. continue;
  632. }
  633. entry->status.dwServiceType = entry->config.dwServiceType;
  634. entry->db = db;
  635. list_add_tail(&db->services, &entry->entry);
  636. release_service(entry);
  637. }
  638. return ERROR_SUCCESS;
  639. }
  640. BOOL scmdatabase_lock_startup(struct scmdatabase *db, int timeout)
  641. {
  642. while (InterlockedCompareExchange(&db->service_start_lock, TRUE, FALSE))
  643. {
  644. if (timeout != INFINITE)
  645. {
  646. timeout -= 10;
  647. if (timeout <= 0) return FALSE;
  648. }
  649. Sleep(10);
  650. }
  651. return TRUE;
  652. }
  653. void scmdatabase_unlock_startup(struct scmdatabase *db)
  654. {
  655. InterlockedCompareExchange(&db->service_start_lock, FALSE, TRUE);
  656. }
  657. void scmdatabase_lock(struct scmdatabase *db)
  658. {
  659. EnterCriticalSection(&db->cs);
  660. }
  661. void scmdatabase_unlock(struct scmdatabase *db)
  662. {
  663. LeaveCriticalSection(&db->cs);
  664. }
  665. void service_lock(struct service_entry *service)
  666. {
  667. EnterCriticalSection(&service->db->cs);
  668. }
  669. void service_unlock(struct service_entry *service)
  670. {
  671. LeaveCriticalSection(&service->db->cs);
  672. }
  673. /* only one service started at a time, so there is no race on the registry
  674. * value here */
  675. static LPWSTR service_get_pipe_name(void)
  676. {
  677. static const WCHAR format[] = { '\\','\\','.','\\','p','i','p','e','\\',
  678. 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
  679. static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
  680. static DWORD service_current = 0;
  681. DWORD len, value = -1;
  682. LONG ret;
  683. DWORD type;
  684. len = sizeof(value);
  685. ret = RegQueryValueExW(service_current_key, NULL, NULL, &type,
  686. (BYTE *)&value, &len);
  687. if (ret == ERROR_SUCCESS && type == REG_DWORD)
  688. service_current = max(service_current, value + 1);
  689. RegSetValueExW(service_current_key, NULL, 0, REG_DWORD,
  690. (BYTE *)&service_current, sizeof(service_current));
  691. swprintf(name, ARRAY_SIZE(name), format, service_current);
  692. service_current++;
  693. return name;
  694. }
  695. static DWORD get_service_binary_path(const struct service_entry *service_entry, WCHAR **path)
  696. {
  697. DWORD size = ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, NULL, 0);
  698. *path = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
  699. if (!*path)
  700. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  701. ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, *path, size);
  702. /* if service image is configured to systemdir, redirect it to wow64 systemdir */
  703. if (service_entry->is_wow64 && !(service_entry->config.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER)))
  704. {
  705. WCHAR system_dir[MAX_PATH], *redirected;
  706. DWORD len;
  707. GetSystemDirectoryW( system_dir, MAX_PATH );
  708. len = lstrlenW( system_dir );
  709. if (wcsnicmp( system_dir, *path, len ))
  710. return ERROR_SUCCESS;
  711. GetSystemWow64DirectoryW( system_dir, MAX_PATH );
  712. redirected = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( *path ) + lstrlenW( system_dir ))*sizeof(WCHAR));
  713. if (!redirected)
  714. {
  715. HeapFree( GetProcessHeap(), 0, *path );
  716. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  717. }
  718. lstrcpyW( redirected, system_dir );
  719. lstrcatW( redirected, &(*path)[len] );
  720. HeapFree( GetProcessHeap(), 0, *path );
  721. *path = redirected;
  722. TRACE("redirected to %s\n", debugstr_w(redirected));
  723. }
  724. return ERROR_SUCCESS;
  725. }
  726. static DWORD get_winedevice_binary_path(struct service_entry *service_entry, WCHAR **path, BOOL *is_wow64)
  727. {
  728. static const WCHAR winedeviceW[] = {'\\','w','i','n','e','d','e','v','i','c','e','.','e','x','e',0};
  729. WCHAR system_dir[MAX_PATH];
  730. DWORD type;
  731. if (!is_win64)
  732. *is_wow64 = FALSE;
  733. else if (GetBinaryTypeW(*path, &type))
  734. *is_wow64 = (type == SCS_32BIT_BINARY);
  735. else
  736. *is_wow64 = service_entry->is_wow64;
  737. GetSystemDirectoryW(system_dir, MAX_PATH);
  738. HeapFree(GetProcessHeap(), 0, *path);
  739. if (!(*path = HeapAlloc(GetProcessHeap(), 0, lstrlenW(system_dir) * sizeof(WCHAR) + sizeof(winedeviceW))))
  740. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  741. lstrcpyW(*path, system_dir);
  742. lstrcatW(*path, winedeviceW);
  743. return ERROR_SUCCESS;
  744. }
  745. static struct process_entry *get_winedevice_process(struct service_entry *service_entry, WCHAR *path, BOOL is_wow64)
  746. {
  747. struct service_entry *winedevice_entry;
  748. if (!service_entry->config.lpLoadOrderGroup)
  749. return NULL;
  750. LIST_FOR_EACH_ENTRY(winedevice_entry, &service_entry->db->services, struct service_entry, entry)
  751. {
  752. if (winedevice_entry->status.dwCurrentState != SERVICE_START_PENDING &&
  753. winedevice_entry->status.dwCurrentState != SERVICE_RUNNING) continue;
  754. if (!winedevice_entry->process) continue;
  755. if (winedevice_entry->is_wow64 != is_wow64) continue;
  756. if (!winedevice_entry->config.lpBinaryPathName) continue;
  757. if (lstrcmpW(winedevice_entry->config.lpBinaryPathName, path)) continue;
  758. if (!winedevice_entry->config.lpLoadOrderGroup) continue;
  759. if (lstrcmpW(winedevice_entry->config.lpLoadOrderGroup, service_entry->config.lpLoadOrderGroup)) continue;
  760. return grab_process(winedevice_entry->process);
  761. }
  762. return NULL;
  763. }
  764. static DWORD add_winedevice_service(const struct service_entry *service, WCHAR *path, BOOL is_wow64,
  765. struct service_entry **entry)
  766. {
  767. static const WCHAR format[] = {'W','i','n','e','d','e','v','i','c','e','%','u',0};
  768. static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
  769. static DWORD current = 0;
  770. struct scmdatabase *db = service->db;
  771. DWORD err;
  772. for (;;)
  773. {
  774. swprintf(name, ARRAY_SIZE(name), format, ++current);
  775. if (!scmdatabase_find_service(db, name)) break;
  776. }
  777. err = service_create(name, entry);
  778. if (err != ERROR_SUCCESS)
  779. return err;
  780. (*entry)->is_wow64 = is_wow64;
  781. (*entry)->config.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  782. (*entry)->config.dwStartType = SERVICE_DEMAND_START;
  783. (*entry)->status.dwServiceType = (*entry)->config.dwServiceType;
  784. if (!((*entry)->config.lpBinaryPathName = strdupW(path)))
  785. goto error;
  786. if (!((*entry)->config.lpServiceStartName = strdupW(SZ_LOCAL_SYSTEM)))
  787. goto error;
  788. if (!((*entry)->config.lpDisplayName = strdupW(name)))
  789. goto error;
  790. if (service->config.lpLoadOrderGroup &&
  791. !((*entry)->config.lpLoadOrderGroup = strdupW(service->config.lpLoadOrderGroup)))
  792. goto error;
  793. (*entry)->db = db;
  794. list_add_tail(&db->services, &(*entry)->entry);
  795. mark_for_delete(*entry);
  796. return ERROR_SUCCESS;
  797. error:
  798. free_service_entry(*entry);
  799. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  800. }
  801. static DWORD service_start_process(struct service_entry *service_entry, struct process_entry **new_process,
  802. BOOL *shared_process)
  803. {
  804. struct process_entry *process;
  805. PROCESS_INFORMATION pi;
  806. STARTUPINFOW si;
  807. BOOL is_wow64 = FALSE;
  808. HANDLE token;
  809. WCHAR *path;
  810. DWORD err;
  811. BOOL r;
  812. service_lock(service_entry);
  813. if ((process = service_entry->process))
  814. {
  815. if (WaitForSingleObject(process->process, 0) == WAIT_TIMEOUT)
  816. {
  817. service_unlock(service_entry);
  818. return ERROR_SERVICE_ALREADY_RUNNING;
  819. }
  820. service_entry->process = NULL;
  821. process->use_count--;
  822. release_process(process);
  823. }
  824. service_entry->force_shutdown = FALSE;
  825. if ((err = get_service_binary_path(service_entry, &path)))
  826. {
  827. service_unlock(service_entry);
  828. return err;
  829. }
  830. if (service_entry->config.dwServiceType == SERVICE_KERNEL_DRIVER ||
  831. service_entry->config.dwServiceType == SERVICE_FILE_SYSTEM_DRIVER)
  832. {
  833. struct service_entry *winedevice_entry;
  834. WCHAR *group;
  835. if ((err = get_winedevice_binary_path(service_entry, &path, &is_wow64)))
  836. {
  837. service_unlock(service_entry);
  838. HeapFree(GetProcessHeap(), 0, path);
  839. return err;
  840. }
  841. if ((process = get_winedevice_process(service_entry, path, is_wow64)))
  842. {
  843. HeapFree(GetProcessHeap(), 0, path);
  844. goto found;
  845. }
  846. err = add_winedevice_service(service_entry, path, is_wow64, &winedevice_entry);
  847. HeapFree(GetProcessHeap(), 0, path);
  848. if (err != ERROR_SUCCESS)
  849. {
  850. service_unlock(service_entry);
  851. return err;
  852. }
  853. group = strdupW(winedevice_entry->config.lpLoadOrderGroup);
  854. service_unlock(service_entry);
  855. err = service_start(winedevice_entry, group != NULL, (const WCHAR **)&group);
  856. HeapFree(GetProcessHeap(), 0, group);
  857. if (err != ERROR_SUCCESS)
  858. {
  859. release_service(winedevice_entry);
  860. return err;
  861. }
  862. service_lock(service_entry);
  863. process = grab_process(winedevice_entry->process);
  864. release_service(winedevice_entry);
  865. if (!process)
  866. {
  867. service_unlock(service_entry);
  868. return ERROR_SERVICE_REQUEST_TIMEOUT;
  869. }
  870. found:
  871. service_entry->status.dwCurrentState = SERVICE_START_PENDING;
  872. service_entry->status.dwControlsAccepted = 0;
  873. ResetEvent(service_entry->status_changed_event);
  874. service_entry->process = grab_process(process);
  875. service_entry->shared_process = *shared_process = TRUE;
  876. process->use_count++;
  877. service_unlock(service_entry);
  878. err = WaitForSingleObject(process->control_mutex, 30000);
  879. if (err != WAIT_OBJECT_0)
  880. {
  881. release_process(process);
  882. return ERROR_SERVICE_REQUEST_TIMEOUT;
  883. }
  884. *new_process = process;
  885. return ERROR_SUCCESS;
  886. }
  887. if ((err = process_create(service_get_pipe_name(), &process)))
  888. {
  889. WINE_ERR("failed to create process object for %s, error = %u\n",
  890. wine_dbgstr_w(service_entry->name), err);
  891. service_unlock(service_entry);
  892. HeapFree(GetProcessHeap(), 0, path);
  893. return err;
  894. }
  895. ZeroMemory(&si, sizeof(STARTUPINFOW));
  896. si.cb = sizeof(STARTUPINFOW);
  897. if (!(service_entry->config.dwServiceType & SERVICE_INTERACTIVE_PROCESS))
  898. {
  899. 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};
  900. si.lpDesktop = desktopW;
  901. }
  902. if (!environment && OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &token))
  903. {
  904. WCHAR val[16];
  905. CreateEnvironmentBlock(&environment, token, FALSE);
  906. if (GetEnvironmentVariableW( L"WINEBOOTSTRAPMODE", val, ARRAY_SIZE(val) ))
  907. {
  908. UNICODE_STRING name, value;
  909. RtlInitUnicodeString( &name, L"WINEBOOTSTRAPMODE" );
  910. RtlInitUnicodeString( &value, val );
  911. RtlSetEnvironmentVariable( (WCHAR **)&environment, &name, &value );
  912. }
  913. CloseHandle(token);
  914. }
  915. service_entry->status.dwCurrentState = SERVICE_START_PENDING;
  916. service_entry->status.dwControlsAccepted = 0;
  917. ResetEvent(service_entry->status_changed_event);
  918. scmdatabase_add_process(service_entry->db, process);
  919. service_entry->process = grab_process(process);
  920. service_entry->shared_process = *shared_process = FALSE;
  921. process->use_count++;
  922. service_unlock(service_entry);
  923. r = CreateProcessW(NULL, path, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, environment, NULL, &si, &pi);
  924. HeapFree(GetProcessHeap(), 0, path);
  925. if (!r)
  926. {
  927. err = GetLastError();
  928. process_terminate(process);
  929. release_process(process);
  930. return err;
  931. }
  932. process->process_id = pi.dwProcessId;
  933. process->process = pi.hProcess;
  934. CloseHandle( pi.hThread );
  935. *new_process = process;
  936. return ERROR_SUCCESS;
  937. }
  938. static DWORD service_wait_for_startup(struct service_entry *service, struct process_entry *process)
  939. {
  940. HANDLE handles[2] = { service->status_changed_event, process->process };
  941. DWORD result;
  942. result = WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout );
  943. if (result != WAIT_OBJECT_0)
  944. return ERROR_SERVICE_REQUEST_TIMEOUT;
  945. service_lock(service);
  946. result = service->status.dwCurrentState;
  947. service_unlock(service);
  948. return (result == SERVICE_START_PENDING || result == SERVICE_RUNNING) ?
  949. ERROR_SUCCESS : ERROR_SERVICE_REQUEST_TIMEOUT;
  950. }
  951. /******************************************************************************
  952. * process_send_start_message
  953. */
  954. static DWORD process_send_start_message(struct process_entry *process, BOOL shared_process,
  955. const WCHAR *name, const WCHAR **argv, DWORD argc)
  956. {
  957. OVERLAPPED overlapped;
  958. DWORD i, len, result;
  959. WCHAR *str, *p;
  960. WINE_TRACE("%p %s %p %d\n", process, wine_dbgstr_w(name), argv, argc);
  961. overlapped.hEvent = process->overlapped_event;
  962. if (!ConnectNamedPipe(process->control_pipe, &overlapped))
  963. {
  964. if (GetLastError() == ERROR_IO_PENDING)
  965. {
  966. HANDLE handles[2];
  967. handles[0] = process->overlapped_event;
  968. handles[1] = process->process;
  969. if (WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout ) != WAIT_OBJECT_0)
  970. CancelIo(process->control_pipe);
  971. if (!HasOverlappedIoCompleted( &overlapped ))
  972. {
  973. WINE_ERR("service %s failed to start\n", wine_dbgstr_w(name));
  974. return ERROR_SERVICE_REQUEST_TIMEOUT;
  975. }
  976. }
  977. else if (GetLastError() != ERROR_PIPE_CONNECTED)
  978. {
  979. WINE_ERR("pipe connect failed\n");
  980. return ERROR_SERVICE_REQUEST_TIMEOUT;
  981. }
  982. }
  983. len = lstrlenW(name) + 1;
  984. for (i = 0; i < argc; i++)
  985. len += lstrlenW(argv[i])+1;
  986. len = (len + 1) * sizeof(WCHAR);
  987. if (!(str = HeapAlloc(GetProcessHeap(), 0, len)))
  988. return ERROR_NOT_ENOUGH_SERVER_MEMORY;
  989. p = str;
  990. lstrcpyW(p, name);
  991. p += lstrlenW(name) + 1;
  992. for (i = 0; i < argc; i++)
  993. {
  994. lstrcpyW(p, argv[i]);
  995. p += lstrlenW(p) + 1;
  996. }
  997. *p = 0;
  998. if (!process_send_control(process, shared_process, name,
  999. SERVICE_CONTROL_START, (const BYTE *)str, len, &result))
  1000. result = ERROR_SERVICE_REQUEST_TIMEOUT;
  1001. HeapFree(GetProcessHeap(), 0, str);
  1002. return result;
  1003. }
  1004. DWORD service_start(struct service_entry *service, DWORD service_argc, LPCWSTR *service_argv)
  1005. {
  1006. struct process_entry *process = NULL;
  1007. BOOL shared_process;
  1008. DWORD err;
  1009. err = service_start_process(service, &process, &shared_process);
  1010. if (err == ERROR_SUCCESS)
  1011. {
  1012. err = process_send_start_message(process, shared_process, service->name, service_argv, service_argc);
  1013. if (err == ERROR_SUCCESS)
  1014. err = service_wait_for_startup(service, process);
  1015. if (err != ERROR_SUCCESS)
  1016. {
  1017. service_lock(service);
  1018. if (service->process)
  1019. {
  1020. service->status.dwCurrentState = SERVICE_STOPPED;
  1021. service->process = NULL;
  1022. if (!--process->use_count) process_terminate(process);
  1023. release_process(process);
  1024. }
  1025. service_unlock(service);
  1026. }
  1027. ReleaseMutex(process->control_mutex);
  1028. release_process(process);
  1029. }
  1030. WINE_TRACE("returning %d\n", err);
  1031. return err;
  1032. }
  1033. void process_terminate(struct process_entry *process)
  1034. {
  1035. struct scmdatabase *db = process->db;
  1036. struct service_entry *service;
  1037. scmdatabase_lock(db);
  1038. TerminateProcess(process->process, 0);
  1039. LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
  1040. {
  1041. if (service->process != process) continue;
  1042. service->status.dwCurrentState = SERVICE_STOPPED;
  1043. service->process = NULL;
  1044. process->use_count--;
  1045. release_process(process);
  1046. }
  1047. scmdatabase_unlock(db);
  1048. }
  1049. static void load_registry_parameters(void)
  1050. {
  1051. static const WCHAR controlW[] =
  1052. { 'S','y','s','t','e','m','\\',
  1053. 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
  1054. 'C','o','n','t','r','o','l',0 };
  1055. static const WCHAR pipetimeoutW[] =
  1056. {'S','e','r','v','i','c','e','s','P','i','p','e','T','i','m','e','o','u','t',0};
  1057. static const WCHAR killtimeoutW[] =
  1058. {'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};
  1059. static const WCHAR autostartdelayW[] =
  1060. {'A','u','t','o','S','t','a','r','t','D','e','l','a','y',0};
  1061. HKEY key;
  1062. WCHAR buffer[64];
  1063. DWORD type, count, val;
  1064. if (RegOpenKeyW( HKEY_LOCAL_MACHINE, controlW, &key )) return;
  1065. count = sizeof(buffer);
  1066. if (!RegQueryValueExW( key, pipetimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
  1067. type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
  1068. service_pipe_timeout = val;
  1069. count = sizeof(buffer);
  1070. if (!RegQueryValueExW( key, killtimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
  1071. type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
  1072. service_kill_timeout = val;
  1073. count = sizeof(val);
  1074. if (!RegQueryValueExW( key, autostartdelayW, NULL, &type, (BYTE *)&val, &count ) && type == REG_DWORD)
  1075. autostart_delay = val;
  1076. RegCloseKey( key );
  1077. }
  1078. int __cdecl main(int argc, char *argv[])
  1079. {
  1080. static const WCHAR service_current_key_str[] = { 'S','Y','S','T','E','M','\\',
  1081. 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
  1082. 'C','o','n','t','r','o','l','\\',
  1083. 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
  1084. static const WCHAR svcctl_started_event[] = SVCCTL_STARTED_EVENT;
  1085. HANDLE started_event;
  1086. DWORD err;
  1087. started_event = CreateEventW(NULL, TRUE, FALSE, svcctl_started_event);
  1088. err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, service_current_key_str, 0,
  1089. NULL, REG_OPTION_VOLATILE, KEY_SET_VALUE | KEY_QUERY_VALUE, NULL,
  1090. &service_current_key, NULL);
  1091. if (err != ERROR_SUCCESS)
  1092. return err;
  1093. load_registry_parameters();
  1094. err = scmdatabase_create(&active_database);
  1095. if (err != ERROR_SUCCESS)
  1096. return err;
  1097. if ((err = scmdatabase_load_services(active_database)) != ERROR_SUCCESS)
  1098. return err;
  1099. if ((err = RPC_Init()) == ERROR_SUCCESS)
  1100. {
  1101. scmdatabase_autostart_services(active_database);
  1102. SetEvent(started_event);
  1103. WaitForSingleObject(exit_event, INFINITE);
  1104. scmdatabase_wait_terminate(active_database);
  1105. if (delayed_autostart_cleanup)
  1106. {
  1107. CloseThreadpoolCleanupGroupMembers(delayed_autostart_cleanup, TRUE, NULL);
  1108. CloseThreadpoolCleanupGroup(delayed_autostart_cleanup);
  1109. }
  1110. RPC_Stop();
  1111. }
  1112. scmdatabase_destroy(active_database);
  1113. if (environment)
  1114. DestroyEnvironmentBlock(environment);
  1115. WINE_TRACE("services.exe exited with code %d\n", err);
  1116. return err;
  1117. }