service.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. #include "nssm.h"
  2. bool is_admin;
  3. bool use_critical_section;
  4. extern imports_t imports;
  5. extern settings_t settings[];
  6. const TCHAR *exit_action_strings[] = { _T("Restart"), _T("Ignore"), _T("Exit"), _T("Suicide"), 0 };
  7. const TCHAR *startup_strings[] = { _T("SERVICE_AUTO_START"), _T("SERVICE_DELAYED_AUTO_START"), _T("SERVICE_DEMAND_START"), _T("SERVICE_DISABLED"), 0 };
  8. const TCHAR *priority_strings[] = { _T("REALTIME_PRIORITY_CLASS"), _T("HIGH_PRIORITY_CLASS"), _T("ABOVE_NORMAL_PRIORITY_CLASS"), _T("NORMAL_PRIORITY_CLASS"), _T("BELOW_NORMAL_PRIORITY_CLASS"), _T("IDLE_PRIORITY_CLASS"), 0 };
  9. typedef struct {
  10. int first;
  11. int last;
  12. } list_t;
  13. /*
  14. Check the status in response to a control.
  15. Returns: 1 if the status is expected, eg STOP following CONTROL_STOP.
  16. 0 if the status is desired, eg STOPPED following CONTROL_STOP.
  17. -1 if the status is undesired, eg STOPPED following CONTROL_START.
  18. */
  19. static inline int service_control_response(unsigned long control, unsigned long status) {
  20. switch (control) {
  21. case NSSM_SERVICE_CONTROL_START:
  22. switch (status) {
  23. case SERVICE_START_PENDING:
  24. return 1;
  25. case SERVICE_RUNNING:
  26. return 0;
  27. default:
  28. return -1;
  29. }
  30. case SERVICE_CONTROL_STOP:
  31. case SERVICE_CONTROL_SHUTDOWN:
  32. switch (status) {
  33. case SERVICE_STOP_PENDING:
  34. return 1;
  35. case SERVICE_STOPPED:
  36. return 0;
  37. default:
  38. return -1;
  39. }
  40. case SERVICE_CONTROL_PAUSE:
  41. switch (status) {
  42. case SERVICE_PAUSE_PENDING:
  43. return 1;
  44. case SERVICE_PAUSED:
  45. return 0;
  46. default:
  47. return -1;
  48. }
  49. case SERVICE_CONTROL_CONTINUE:
  50. switch (status) {
  51. case SERVICE_CONTINUE_PENDING:
  52. return 1;
  53. case SERVICE_RUNNING:
  54. return 0;
  55. default:
  56. return -1;
  57. }
  58. case SERVICE_CONTROL_INTERROGATE:
  59. return 0;
  60. }
  61. return 0;
  62. }
  63. static inline int await_service_control_response(unsigned long control, SC_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long initial_status) {
  64. int tries = 0;
  65. while (QueryServiceStatus(service_handle, service_status)) {
  66. int response = service_control_response(control, service_status->dwCurrentState);
  67. /* Alas we can't WaitForSingleObject() on an SC_HANDLE. */
  68. if (! response) return response;
  69. if (response > 0 || service_status->dwCurrentState == initial_status) {
  70. if (++tries > 10) return response;
  71. Sleep(50 * tries);
  72. }
  73. else return response;
  74. }
  75. return -1;
  76. }
  77. int affinity_mask_to_string(__int64 mask, TCHAR **string) {
  78. if (! string) return 1;
  79. if (! mask) {
  80. *string = 0;
  81. return 0;
  82. }
  83. __int64 i, n;
  84. /* SetProcessAffinityMask() accepts a mask of up to 64 processors. */
  85. list_t set[64];
  86. for (n = 0; n < _countof(set); n++) set[n].first = set[n].last = -1;
  87. for (i = 0, n = 0; i < _countof(set); i++) {
  88. if (mask & (1LL << i)) {
  89. if (set[n].first == -1) set[n].first = set[n].last = (int) i;
  90. else if (set[n].last == (int) i - 1) set[n].last = (int) i;
  91. else {
  92. n++;
  93. set[n].first = set[n].last = (int) i;
  94. }
  95. }
  96. }
  97. /* Worst case is 2x2 characters for first and last CPU plus - and/or , */
  98. size_t len = (size_t) (n + 1) * 6;
  99. *string = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(TCHAR));
  100. if (! string) return 2;
  101. size_t s = 0;
  102. int ret;
  103. for (i = 0; i <= n; i++) {
  104. if (i) (*string)[s++] = _T(',');
  105. ret = _sntprintf_s(*string + s, 3, _TRUNCATE, _T("%u"), set[i].first);
  106. if (ret < 0) {
  107. HeapFree(GetProcessHeap(), 0, *string);
  108. *string = 0;
  109. return 3;
  110. }
  111. else s += ret;
  112. if (set[i].last != set[i].first) {
  113. ret =_sntprintf_s(*string + s, 4, _TRUNCATE, _T("%c%u"), (set[i].last == set[i].first + 1) ? _T(',') : _T('-'), set[i].last);
  114. if (ret < 0) {
  115. HeapFree(GetProcessHeap(), 0, *string);
  116. *string = 0;
  117. return 4;
  118. }
  119. else s += ret;
  120. }
  121. }
  122. return 0;
  123. }
  124. int affinity_string_to_mask(TCHAR *string, __int64 *mask) {
  125. if (! mask) return 1;
  126. *mask = 0LL;
  127. if (! string) return 0;
  128. list_t set[64];
  129. TCHAR *s = string;
  130. TCHAR *end;
  131. int ret;
  132. int i;
  133. int n = 0;
  134. unsigned long number;
  135. for (n = 0; n < _countof(set); n++) set[n].first = set[n].last = -1;
  136. n = 0;
  137. while (*s) {
  138. ret = str_number(s, &number, &end);
  139. s = end;
  140. if (ret == 0 || ret == 2) {
  141. if (number >= _countof(set)) return 2;
  142. set[n].first = set[n].last = (int) number;
  143. switch (*s) {
  144. case 0:
  145. break;
  146. case _T(','):
  147. n++;
  148. s++;
  149. break;
  150. case _T('-'):
  151. if (! *(++s)) return 3;
  152. ret = str_number(s, &number, &end);
  153. if (ret == 0 || ret == 2) {
  154. s = end;
  155. if (! *s || *s == _T(',')) {
  156. set[n].last = (int) number;
  157. if (! *s) break;
  158. n++;
  159. s++;
  160. }
  161. else return 3;
  162. }
  163. else return 3;
  164. break;
  165. default:
  166. return 3;
  167. }
  168. }
  169. else return 4;
  170. }
  171. for (i = 0; i <= n; i++) {
  172. for (int j = set[i].first; j <= set[i].last; j++) (__int64) *mask |= (1LL << (__int64) j);
  173. }
  174. return 0;
  175. }
  176. inline unsigned long priority_mask() {
  177. return REALTIME_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | ABOVE_NORMAL_PRIORITY_CLASS | NORMAL_PRIORITY_CLASS | BELOW_NORMAL_PRIORITY_CLASS | IDLE_PRIORITY_CLASS;
  178. }
  179. int priority_constant_to_index(unsigned long constant) {
  180. switch (constant & priority_mask()) {
  181. case REALTIME_PRIORITY_CLASS: return NSSM_REALTIME_PRIORITY;
  182. case HIGH_PRIORITY_CLASS: return NSSM_HIGH_PRIORITY;
  183. case ABOVE_NORMAL_PRIORITY_CLASS: return NSSM_ABOVE_NORMAL_PRIORITY;
  184. case BELOW_NORMAL_PRIORITY_CLASS: return NSSM_BELOW_NORMAL_PRIORITY;
  185. case IDLE_PRIORITY_CLASS: return NSSM_IDLE_PRIORITY;
  186. }
  187. return NSSM_NORMAL_PRIORITY;
  188. }
  189. unsigned long priority_index_to_constant(int index) {
  190. switch (index) {
  191. case NSSM_REALTIME_PRIORITY: return REALTIME_PRIORITY_CLASS;
  192. case NSSM_HIGH_PRIORITY: return HIGH_PRIORITY_CLASS;
  193. case NSSM_ABOVE_NORMAL_PRIORITY: return ABOVE_NORMAL_PRIORITY_CLASS;
  194. case NSSM_BELOW_NORMAL_PRIORITY: return BELOW_NORMAL_PRIORITY_CLASS;
  195. case NSSM_IDLE_PRIORITY: return IDLE_PRIORITY_CLASS;
  196. }
  197. return NORMAL_PRIORITY_CLASS;
  198. }
  199. static inline unsigned long throttle_milliseconds(unsigned long throttle) {
  200. /* pow() operates on doubles. */
  201. unsigned long ret = 1; for (unsigned long i = 1; i < throttle; i++) ret *= 2;
  202. return ret * 1000;
  203. }
  204. /*
  205. Wrapper to be called in a new thread so that we can acknowledge a STOP
  206. control immediately.
  207. */
  208. static unsigned long WINAPI shutdown_service(void *arg) {
  209. return stop_service((nssm_service_t *) arg, 0, true, true);
  210. }
  211. /* Connect to the service manager */
  212. SC_HANDLE open_service_manager(unsigned long access) {
  213. SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, access);
  214. if (! ret) {
  215. if (is_admin) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENSCMANAGER_FAILED, 0);
  216. return 0;
  217. }
  218. return ret;
  219. }
  220. /* Open a service by name or display name. */
  221. SC_HANDLE open_service(SC_HANDLE services, TCHAR *service_name, unsigned long access, TCHAR *canonical_name, unsigned long canonical_namelen) {
  222. SC_HANDLE service_handle = OpenService(services, service_name, access);
  223. if (service_handle) {
  224. if (canonical_name && canonical_name != service_name) {
  225. TCHAR displayname[SERVICE_NAME_LENGTH];
  226. unsigned long displayname_len = (unsigned long) _countof(displayname);
  227. GetServiceDisplayName(services, service_name, displayname, &displayname_len);
  228. unsigned long keyname_len = canonical_namelen;
  229. GetServiceKeyName(services, displayname, canonical_name, &keyname_len);
  230. }
  231. return service_handle;
  232. }
  233. unsigned long error = GetLastError();
  234. if (error != ERROR_SERVICE_DOES_NOT_EXIST) {
  235. print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED, error_string(GetLastError()));
  236. return 0;
  237. }
  238. /* We can't look for a display name because there's no buffer to store it. */
  239. if (! canonical_name) {
  240. print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED, error_string(GetLastError()));
  241. return 0;
  242. }
  243. unsigned long bufsize, required, count, i;
  244. unsigned long resume = 0;
  245. EnumServicesStatus(services, SERVICE_DRIVER | SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL, 0, 0, &required, &count, &resume);
  246. error = GetLastError();
  247. if (error != ERROR_MORE_DATA) {
  248. print_message(stderr, NSSM_MESSAGE_ENUMSERVICESSTATUS_FAILED, error_string(GetLastError()));
  249. return 0;
  250. }
  251. ENUM_SERVICE_STATUS *status = (ENUM_SERVICE_STATUS *) HeapAlloc(GetProcessHeap(), 0, required);
  252. if (! status) {
  253. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("ENUM_SERVICE_STATUS"), _T("open_service()"));
  254. return 0;
  255. }
  256. bufsize = required;
  257. while (true) {
  258. /*
  259. EnumServicesStatus() returns:
  260. 1 when it retrieved data and there's no more data to come.
  261. 0 and sets last error to ERROR_MORE_DATA when it retrieved data and
  262. there's more data to come.
  263. 0 and sets last error to something else on error.
  264. */
  265. int ret = EnumServicesStatus(services, SERVICE_DRIVER | SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL, status, bufsize, &required, &count, &resume);
  266. if (! ret) {
  267. error = GetLastError();
  268. if (error != ERROR_MORE_DATA) {
  269. HeapFree(GetProcessHeap(), 0, status);
  270. print_message(stderr, NSSM_MESSAGE_ENUMSERVICESSTATUS_FAILED, error_string(GetLastError()));
  271. return 0;
  272. }
  273. }
  274. for (i = 0; i < count; i++) {
  275. if (str_equiv(status[i].lpDisplayName, service_name)) {
  276. if (_sntprintf_s(canonical_name, canonical_namelen, _TRUNCATE, _T("%s"), status[i].lpServiceName) < 0) {
  277. HeapFree(GetProcessHeap(), 0, status);
  278. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canonical_name"), _T("open_service()"));
  279. return 0;
  280. }
  281. HeapFree(GetProcessHeap(), 0, status);
  282. return open_service(services, canonical_name, access, 0, 0);
  283. }
  284. }
  285. if (ret) break;
  286. }
  287. /* Recurse so we can get an error message. */
  288. return open_service(services, service_name, access, 0, 0);
  289. }
  290. QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *service_name, SC_HANDLE service_handle) {
  291. QUERY_SERVICE_CONFIG *qsc;
  292. unsigned long bufsize;
  293. unsigned long error;
  294. QueryServiceConfig(service_handle, 0, 0, &bufsize);
  295. error = GetLastError();
  296. if (error == ERROR_INSUFFICIENT_BUFFER) {
  297. qsc = (QUERY_SERVICE_CONFIG *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufsize);
  298. if (! qsc) {
  299. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("QUERY_SERVICE_CONFIG"), _T("query_service_config()"), 0);
  300. return 0;
  301. }
  302. }
  303. else {
  304. print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG_FAILED, service_name, error_string(error), 0);
  305. return 0;
  306. }
  307. if (! QueryServiceConfig(service_handle, qsc, bufsize, &bufsize)) {
  308. HeapFree(GetProcessHeap(), 0, qsc);
  309. print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG_FAILED, service_name, error_string(GetLastError()), 0);
  310. return 0;
  311. }
  312. return qsc;
  313. }
  314. int set_service_dependencies(const TCHAR *service_name, SC_HANDLE service_handle, TCHAR *buffer) {
  315. TCHAR *dependencies = _T("");
  316. unsigned long num_dependencies = 0;
  317. if (buffer && buffer[0]) {
  318. SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
  319. if (! services) {
  320. print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  321. return 1;
  322. }
  323. /*
  324. Count the dependencies then allocate a buffer big enough for their
  325. canonical names, ie n * SERVICE_NAME_LENGTH.
  326. */
  327. TCHAR *s;
  328. TCHAR *groups = 0;
  329. for (s = buffer; *s; s++) {
  330. num_dependencies++;
  331. if (*s == SC_GROUP_IDENTIFIER) groups = s;
  332. while (*s) s++;
  333. }
  334. /* At least one dependency is a group so we need to verify them. */
  335. if (groups) {
  336. HKEY key;
  337. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, NSSM_REGISTRY_GROUPS, 0, KEY_READ, &key)) {
  338. _ftprintf(stderr, _T("%s: %s\n"), NSSM_REGISTRY_GROUPS, error_string(GetLastError()));
  339. return 2;
  340. }
  341. unsigned long type;
  342. unsigned long groupslen;
  343. unsigned long ret = RegQueryValueEx(key, NSSM_REG_GROUPS, 0, &type, NULL, &groupslen);
  344. if (ret == ERROR_SUCCESS) {
  345. groups = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, groupslen);
  346. if (! groups) {
  347. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("groups"), _T("set_service_dependencies()"));
  348. return 3;
  349. }
  350. ret = RegQueryValueEx(key, NSSM_REG_GROUPS, 0, &type, (unsigned char *) groups, &groupslen);
  351. if (ret != ERROR_SUCCESS) {
  352. _ftprintf(stderr, _T("%s\\%s: %s"), NSSM_REGISTRY_GROUPS, NSSM_REG_GROUPS, error_string(GetLastError()));
  353. HeapFree(GetProcessHeap(), 0, groups);
  354. RegCloseKey(key);
  355. return 4;
  356. }
  357. }
  358. else if (ret != ERROR_FILE_NOT_FOUND) {
  359. _ftprintf(stderr, _T("%s\\%s: %s"), NSSM_REGISTRY_GROUPS, NSSM_REG_GROUPS, error_string(GetLastError()));
  360. RegCloseKey(key);
  361. return 4;
  362. }
  363. RegCloseKey(key);
  364. }
  365. unsigned long dependencieslen = (num_dependencies * SERVICE_NAME_LENGTH) + 2;
  366. dependencies = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dependencieslen * sizeof(TCHAR));
  367. size_t i = 0;
  368. TCHAR dependency[SERVICE_NAME_LENGTH];
  369. for (s = buffer; *s; s++) {
  370. /* Group? */
  371. if (*s == SC_GROUP_IDENTIFIER) {
  372. TCHAR *group = s + 1;
  373. bool ok = false;
  374. if (*group) {
  375. for (TCHAR *g = groups; *g; g++) {
  376. if (str_equiv(g, group)) {
  377. ok = true;
  378. /* Set canonical name. */
  379. memmove(group, g, _tcslen(g) * sizeof(TCHAR));
  380. break;
  381. }
  382. while (*g) g++;
  383. }
  384. }
  385. if (ok) _sntprintf_s(dependency, _countof(dependency), _TRUNCATE, _T("%s"), s);
  386. else {
  387. HeapFree(GetProcessHeap(), 0, dependencies);
  388. if (groups) HeapFree(GetProcessHeap(), 0, groups);
  389. _ftprintf(stderr, _T("%s: %s"), s, error_string(ERROR_SERVICE_DEPENDENCY_DELETED));
  390. return 5;
  391. }
  392. }
  393. else {
  394. SC_HANDLE dependency_handle = open_service(services, s, SERVICE_QUERY_STATUS, dependency, _countof(dependency));
  395. if (! dependency_handle) {
  396. HeapFree(GetProcessHeap(), 0, dependencies);
  397. if (groups) HeapFree(GetProcessHeap(), 0, groups);
  398. CloseServiceHandle(services);
  399. _ftprintf(stderr, _T("%s: %s"), s, error_string(ERROR_SERVICE_DEPENDENCY_DELETED));
  400. return 5;
  401. }
  402. }
  403. size_t len = _tcslen(dependency) + 1;
  404. memmove(dependencies + i, dependency, len * sizeof(TCHAR));
  405. i += len;
  406. while (*s) s++;
  407. }
  408. if (groups) HeapFree(GetProcessHeap(), 0, groups);
  409. CloseServiceHandle(services);
  410. }
  411. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, dependencies, 0, 0, 0)) {
  412. if (num_dependencies) HeapFree(GetProcessHeap(), 0, dependencies);
  413. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  414. return -1;
  415. }
  416. if (num_dependencies) HeapFree(GetProcessHeap(), 0, dependencies);
  417. return 0;
  418. }
  419. int get_service_dependencies(const TCHAR *service_name, SC_HANDLE service_handle, TCHAR **buffer, unsigned long *bufsize, int type) {
  420. if (! buffer) return 1;
  421. if (! bufsize) return 2;
  422. *buffer = 0;
  423. *bufsize = 0;
  424. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  425. if (! qsc) return 3;
  426. if (! qsc->lpDependencies) return 0;
  427. if (! qsc->lpDependencies[0]) return 0;
  428. /* lpDependencies is doubly NULL terminated. */
  429. while (qsc->lpDependencies[*bufsize]) {
  430. while (qsc->lpDependencies[*bufsize]) ++*bufsize;
  431. ++*bufsize;
  432. }
  433. *bufsize += 2;
  434. *buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *bufsize * sizeof(TCHAR));
  435. if (! *buffer) {
  436. *bufsize = 0;
  437. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("lpDependencies"), _T("get_service_dependencies()"));
  438. return 4;
  439. }
  440. if (type == DEPENDENCY_ALL) memmove(*buffer, qsc->lpDependencies, *bufsize * sizeof(TCHAR));
  441. else {
  442. TCHAR *s;
  443. size_t i = 0;
  444. *bufsize = 0;
  445. for (s = qsc->lpDependencies; *s; s++) {
  446. /* Only copy the appropriate type of dependency. */
  447. if ((*s == SC_GROUP_IDENTIFIER && type & DEPENDENCY_GROUPS) || (*s != SC_GROUP_IDENTIFIER && type & DEPENDENCY_SERVICES)) {
  448. size_t len = _tcslen(s) + 1;
  449. *bufsize += (unsigned long) len;
  450. memmove(*buffer + i, s, len * sizeof(TCHAR));
  451. i += len;
  452. }
  453. while (*s) s++;
  454. }
  455. ++*bufsize;
  456. }
  457. HeapFree(GetProcessHeap(), 0, qsc);
  458. return 0;
  459. }
  460. int get_service_dependencies(const TCHAR *service_name, SC_HANDLE service_handle, TCHAR **buffer, unsigned long *bufsize) {
  461. return get_service_dependencies(service_name, service_handle, buffer, bufsize, DEPENDENCY_ALL);
  462. }
  463. int set_service_description(const TCHAR *service_name, SC_HANDLE service_handle, TCHAR *buffer) {
  464. SERVICE_DESCRIPTION description;
  465. ZeroMemory(&description, sizeof(description));
  466. /*
  467. lpDescription must be NULL if we aren't changing, the new description
  468. or "".
  469. */
  470. if (buffer && buffer[0]) description.lpDescription = buffer;
  471. else description.lpDescription = _T("");
  472. if (ChangeServiceConfig2(service_handle, SERVICE_CONFIG_DESCRIPTION, &description)) return 0;
  473. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SERVICE_CONFIG_DESCRIPTION_FAILED, service_name, error_string(GetLastError()), 0);
  474. return 1;
  475. }
  476. int get_service_description(const TCHAR *service_name, SC_HANDLE service_handle, unsigned long len, TCHAR *buffer) {
  477. if (! buffer) return 1;
  478. unsigned long bufsize;
  479. QueryServiceConfig2(service_handle, SERVICE_CONFIG_DESCRIPTION, 0, 0, &bufsize);
  480. unsigned long error = GetLastError();
  481. if (error == ERROR_INSUFFICIENT_BUFFER) {
  482. SERVICE_DESCRIPTION *description = (SERVICE_DESCRIPTION *) HeapAlloc(GetProcessHeap(), 0, bufsize);
  483. if (! description) {
  484. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("SERVICE_CONFIG_DESCRIPTION"), _T("get_service_description()"));
  485. return 2;
  486. }
  487. if (QueryServiceConfig2(service_handle, SERVICE_CONFIG_DESCRIPTION, (unsigned char *) description, bufsize, &bufsize)) {
  488. if (description->lpDescription) _sntprintf_s(buffer, len, _TRUNCATE, _T("%s"), description->lpDescription);
  489. else ZeroMemory(buffer, len * sizeof(TCHAR));
  490. HeapFree(GetProcessHeap(), 0, description);
  491. return 0;
  492. }
  493. else {
  494. HeapFree(GetProcessHeap(), 0, description);
  495. print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG2_FAILED, service_name, _T("SERVICE_CONFIG_DESCRIPTION"), error_string(error));
  496. return 3;
  497. }
  498. }
  499. else {
  500. print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG2_FAILED, service_name, _T("SERVICE_CONFIG_DESCRIPTION"), error_string(error));
  501. return 4;
  502. }
  503. return 0;
  504. }
  505. int get_service_startup(const TCHAR *service_name, SC_HANDLE service_handle, const QUERY_SERVICE_CONFIG *qsc, unsigned long *startup) {
  506. if (! qsc) return 1;
  507. switch (qsc->dwStartType) {
  508. case SERVICE_DEMAND_START: *startup = NSSM_STARTUP_MANUAL; break;
  509. case SERVICE_DISABLED: *startup = NSSM_STARTUP_DISABLED; break;
  510. default: *startup = NSSM_STARTUP_AUTOMATIC;
  511. }
  512. if (*startup != NSSM_STARTUP_AUTOMATIC) return 0;
  513. /* Check for delayed start. */
  514. unsigned long bufsize;
  515. unsigned long error;
  516. QueryServiceConfig2(service_handle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, 0, 0, &bufsize);
  517. error = GetLastError();
  518. if (error == ERROR_INSUFFICIENT_BUFFER) {
  519. SERVICE_DELAYED_AUTO_START_INFO *info = (SERVICE_DELAYED_AUTO_START_INFO *) HeapAlloc(GetProcessHeap(), 0, bufsize);
  520. if (! info) {
  521. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("SERVICE_DELAYED_AUTO_START_INFO"), _T("get_service_startup()"));
  522. return 2;
  523. }
  524. if (QueryServiceConfig2(service_handle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, (unsigned char *) info, bufsize, &bufsize)) {
  525. if (info->fDelayedAutostart) *startup = NSSM_STARTUP_DELAYED;
  526. HeapFree(GetProcessHeap(), 0, info);
  527. return 0;
  528. }
  529. else {
  530. error = GetLastError();
  531. if (error != ERROR_INVALID_LEVEL) {
  532. print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG2_FAILED, service_name, _T("SERVICE_CONFIG_DELAYED_AUTO_START_INFO"), error_string(error));
  533. return 3;
  534. }
  535. }
  536. }
  537. else if (error != ERROR_INVALID_LEVEL) {
  538. print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG2_FAILED, service_name, _T("SERVICE_DELAYED_AUTO_START_INFO"), error_string(error));
  539. return 3;
  540. }
  541. return 0;
  542. }
  543. int get_service_username(const TCHAR *service_name, const QUERY_SERVICE_CONFIG *qsc, TCHAR **username, size_t *usernamelen) {
  544. if (! username) return 1;
  545. if (! usernamelen) return 1;
  546. *username = 0;
  547. *usernamelen = 0;
  548. if (! qsc) return 1;
  549. if (qsc->lpServiceStartName[0]) {
  550. if (is_localsystem(qsc->lpServiceStartName)) return 0;
  551. size_t len = _tcslen(qsc->lpServiceStartName);
  552. *username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR));
  553. if (! *username) {
  554. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("username"), _T("get_service_username()"));
  555. return 2;
  556. }
  557. memmove(*username, qsc->lpServiceStartName, (len + 1) * sizeof(TCHAR));
  558. *usernamelen = len;
  559. }
  560. return 0;
  561. }
  562. /* Set default values which aren't zero. */
  563. void set_nssm_service_defaults(nssm_service_t *service) {
  564. if (! service) return;
  565. service->type = SERVICE_WIN32_OWN_PROCESS;
  566. service->priority = NORMAL_PRIORITY_CLASS;
  567. service->stdin_sharing = NSSM_STDIN_SHARING;
  568. service->stdin_disposition = NSSM_STDIN_DISPOSITION;
  569. service->stdin_flags = NSSM_STDIN_FLAGS;
  570. service->stdout_sharing = NSSM_STDOUT_SHARING;
  571. service->stdout_disposition = NSSM_STDOUT_DISPOSITION;
  572. service->stdout_flags = NSSM_STDOUT_FLAGS;
  573. service->stderr_sharing = NSSM_STDERR_SHARING;
  574. service->stderr_disposition = NSSM_STDERR_DISPOSITION;
  575. service->stderr_flags = NSSM_STDERR_FLAGS;
  576. service->throttle_delay = NSSM_RESET_THROTTLE_RESTART;
  577. service->stop_method = ~0;
  578. service->kill_console_delay = NSSM_KILL_CONSOLE_GRACE_PERIOD;
  579. service->kill_window_delay = NSSM_KILL_WINDOW_GRACE_PERIOD;
  580. service->kill_threads_delay = NSSM_KILL_THREADS_GRACE_PERIOD;
  581. service->kill_process_tree = 1;
  582. }
  583. /* Allocate and zero memory for a service. */
  584. nssm_service_t *alloc_nssm_service() {
  585. nssm_service_t *service = (nssm_service_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(nssm_service_t));
  586. if (! service) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("alloc_nssm_service()"), 0);
  587. return service;
  588. }
  589. /* Free memory for a service. */
  590. void cleanup_nssm_service(nssm_service_t *service) {
  591. if (! service) return;
  592. if (service->username) HeapFree(GetProcessHeap(), 0, service->username);
  593. if (service->password) {
  594. SecureZeroMemory(service->password, service->passwordlen);
  595. HeapFree(GetProcessHeap(), 0, service->password);
  596. }
  597. if (service->dependencies) HeapFree(GetProcessHeap(), 0, service->dependencies);
  598. if (service->env) HeapFree(GetProcessHeap(), 0, service->env);
  599. if (service->env_extra) HeapFree(GetProcessHeap(), 0, service->env_extra);
  600. if (service->handle) CloseServiceHandle(service->handle);
  601. if (service->process_handle) CloseHandle(service->process_handle);
  602. if (service->wait_handle) UnregisterWait(service->wait_handle);
  603. if (service->throttle_section_initialised) DeleteCriticalSection(&service->throttle_section);
  604. if (service->throttle_timer) CloseHandle(service->throttle_timer);
  605. if (service->initial_env) FreeEnvironmentStrings(service->initial_env);
  606. HeapFree(GetProcessHeap(), 0, service);
  607. }
  608. /* About to install the service */
  609. int pre_install_service(int argc, TCHAR **argv) {
  610. nssm_service_t *service = alloc_nssm_service();
  611. set_nssm_service_defaults(service);
  612. if (argc) _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), argv[0]);
  613. /* Show the dialogue box if we didn't give the service name and path */
  614. if (argc < 2) return nssm_gui(IDD_INSTALL, service);
  615. if (! service) {
  616. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("service"), _T("pre_install_service()"));
  617. return 1;
  618. }
  619. _sntprintf_s(service->exe, _countof(service->exe), _TRUNCATE, _T("%s"), argv[1]);
  620. /* Arguments are optional */
  621. size_t flagslen = 0;
  622. size_t s = 0;
  623. int i;
  624. for (i = 2; i < argc; i++) flagslen += _tcslen(argv[i]) + 1;
  625. if (! flagslen) flagslen = 1;
  626. if (flagslen > _countof(service->flags)) {
  627. print_message(stderr, NSSM_MESSAGE_FLAGS_TOO_LONG);
  628. return 2;
  629. }
  630. for (i = 2; i < argc; i++) {
  631. size_t len = _tcslen(argv[i]);
  632. memmove(service->flags + s, argv[i], len * sizeof(TCHAR));
  633. s += len;
  634. if (i < argc - 1) service->flags[s++] = _T(' ');
  635. }
  636. /* Work out directory name */
  637. _sntprintf_s(service->dir, _countof(service->dir), _TRUNCATE, _T("%s"), service->exe);
  638. strip_basename(service->dir);
  639. int ret = install_service(service);
  640. cleanup_nssm_service(service);
  641. return ret;
  642. }
  643. /* About to edit the service. */
  644. int pre_edit_service(int argc, TCHAR **argv) {
  645. /* Require service name. */
  646. if (argc < 2) return usage(1);
  647. /* Are we editing on the command line? */
  648. enum { MODE_EDITING, MODE_GETTING, MODE_SETTING, MODE_RESETTING } mode = MODE_EDITING;
  649. const TCHAR *verb = argv[0];
  650. const TCHAR *service_name = argv[1];
  651. bool getting = false;
  652. bool unsetting = false;
  653. /* Minimum number of arguments. */
  654. int mandatory = 2;
  655. /* Index of first value. */
  656. int remainder = 3;
  657. int i;
  658. if (str_equiv(verb, _T("get"))) {
  659. mandatory = 3;
  660. mode = MODE_GETTING;
  661. }
  662. else if (str_equiv(verb, _T("set"))) {
  663. mandatory = 4;
  664. mode = MODE_SETTING;
  665. }
  666. else if (str_equiv(verb, _T("reset")) || str_equiv(verb, _T("unset"))) {
  667. mandatory = 3;
  668. mode = MODE_RESETTING;
  669. }
  670. if (argc < mandatory) return usage(1);
  671. const TCHAR *parameter = 0;
  672. settings_t *setting = 0;
  673. TCHAR *additional;
  674. /* Validate the parameter. */
  675. if (mandatory > 2) {
  676. bool additional_mandatory = false;
  677. parameter = argv[2];
  678. for (i = 0; settings[i].name; i++) {
  679. setting = &settings[i];
  680. if (! str_equiv(setting->name, parameter)) continue;
  681. if (((setting->additional & ADDITIONAL_GETTING) && mode == MODE_GETTING) || ((setting->additional & ADDITIONAL_SETTING) && mode == MODE_SETTING) || ((setting->additional & ADDITIONAL_RESETTING) && mode == MODE_RESETTING)) {
  682. additional_mandatory = true;
  683. mandatory++;
  684. }
  685. break;
  686. }
  687. if (! settings[i].name) {
  688. print_message(stderr, NSSM_MESSAGE_INVALID_PARAMETER, parameter);
  689. for (i = 0; settings[i].name; i++) _ftprintf(stderr, _T("%s\n"), settings[i].name);
  690. return 1;
  691. }
  692. additional = 0;
  693. if (additional_mandatory) {
  694. if (argc < mandatory) {
  695. print_message(stderr, NSSM_MESSAGE_MISSING_SUBPARAMETER, parameter);
  696. return 1;
  697. }
  698. additional = argv[3];
  699. remainder = 4;
  700. }
  701. else if (str_equiv(setting->name, NSSM_NATIVE_OBJECTNAME) && mode == MODE_SETTING) {
  702. additional = argv[3];
  703. remainder = 4;
  704. }
  705. else {
  706. additional = argv[remainder];
  707. if (argc < mandatory) return usage(1);
  708. }
  709. }
  710. nssm_service_t *service = alloc_nssm_service();
  711. _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), service_name);
  712. /* Open service manager */
  713. SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
  714. if (! services) {
  715. print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  716. return 2;
  717. }
  718. /* Try to open the service */
  719. unsigned long access = SERVICE_QUERY_CONFIG;
  720. if (mode != MODE_GETTING) access |= SERVICE_CHANGE_CONFIG;
  721. service->handle = open_service(services, service->name, access, service->name, _countof(service->name));
  722. if (! service->handle) {
  723. CloseServiceHandle(services);
  724. return 3;
  725. }
  726. /* Get system details. */
  727. QUERY_SERVICE_CONFIG *qsc = query_service_config(service->name, service->handle);
  728. if (! qsc) {
  729. CloseServiceHandle(service->handle);
  730. CloseServiceHandle(services);
  731. return 4;
  732. }
  733. service->type = qsc->dwServiceType;
  734. if (! (service->type & SERVICE_WIN32_OWN_PROCESS)) {
  735. if (mode != MODE_GETTING) {
  736. HeapFree(GetProcessHeap(), 0, qsc);
  737. CloseServiceHandle(service->handle);
  738. CloseServiceHandle(services);
  739. print_message(stderr, NSSM_MESSAGE_CANNOT_EDIT, service->name, NSSM_WIN32_OWN_PROCESS, 0);
  740. return 3;
  741. }
  742. }
  743. if (get_service_startup(service->name, service->handle, qsc, &service->startup)) {
  744. if (mode != MODE_GETTING) {
  745. HeapFree(GetProcessHeap(), 0, qsc);
  746. CloseServiceHandle(service->handle);
  747. CloseServiceHandle(services);
  748. return 4;
  749. }
  750. }
  751. if (get_service_username(service->name, qsc, &service->username, &service->usernamelen)) {
  752. if (mode != MODE_GETTING) {
  753. HeapFree(GetProcessHeap(), 0, qsc);
  754. CloseServiceHandle(service->handle);
  755. CloseServiceHandle(services);
  756. return 5;
  757. }
  758. }
  759. _sntprintf_s(service->displayname, _countof(service->displayname), _TRUNCATE, _T("%s"), qsc->lpDisplayName);
  760. /* Get the canonical service name. We open it case insensitively. */
  761. unsigned long bufsize = _countof(service->name);
  762. GetServiceKeyName(services, service->displayname, service->name, &bufsize);
  763. /* Remember the executable in case it isn't NSSM. */
  764. _sntprintf_s(service->image, _countof(service->image), _TRUNCATE, _T("%s"), qsc->lpBinaryPathName);
  765. HeapFree(GetProcessHeap(), 0, qsc);
  766. /* Get extended system details. */
  767. if (get_service_description(service->name, service->handle, _countof(service->description), service->description)) {
  768. if (mode != MODE_GETTING) {
  769. CloseServiceHandle(service->handle);
  770. CloseServiceHandle(services);
  771. return 6;
  772. }
  773. }
  774. if (get_service_dependencies(service->name, service->handle, &service->dependencies, &service->dependencieslen)) {
  775. if (mode != MODE_GETTING) {
  776. CloseServiceHandle(service->handle);
  777. CloseServiceHandle(services);
  778. return 7;
  779. }
  780. }
  781. /* Get NSSM details. */
  782. get_parameters(service, 0);
  783. CloseServiceHandle(services);
  784. if (! service->exe[0]) {
  785. service->native = true;
  786. if (mode != MODE_GETTING) print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE, service->name, NSSM, service->image);
  787. }
  788. /* Editing with the GUI. */
  789. if (mode == MODE_EDITING) {
  790. nssm_gui(IDD_EDIT, service);
  791. return 0;
  792. }
  793. /* Trying to manage App* parameters for a non-NSSM service. */
  794. if (! setting->native && service->native) {
  795. CloseServiceHandle(service->handle);
  796. print_message(stderr, NSSM_MESSAGE_NATIVE_PARAMETER, setting->name, NSSM);
  797. return 1;
  798. }
  799. HKEY key;
  800. value_t value;
  801. int ret;
  802. if (mode == MODE_GETTING) {
  803. if (! service->native) {
  804. key = open_registry(service->name, KEY_READ);
  805. if (! key) return 4;
  806. }
  807. if (setting->native) ret = get_setting(service->name, service->handle, setting, &value, additional);
  808. else ret = get_setting(service->name, key, setting, &value, additional);
  809. if (ret < 0) {
  810. CloseServiceHandle(service->handle);
  811. return 5;
  812. }
  813. switch (setting->type) {
  814. case REG_EXPAND_SZ:
  815. case REG_MULTI_SZ:
  816. case REG_SZ:
  817. _tprintf(_T("%s\n"), value.string ? value.string : _T(""));
  818. HeapFree(GetProcessHeap(), 0, value.string);
  819. break;
  820. case REG_DWORD:
  821. _tprintf(_T("%u\n"), value.numeric);
  822. break;
  823. }
  824. if (! service->native) RegCloseKey(key);
  825. CloseServiceHandle(service->handle);
  826. return 0;
  827. }
  828. /* Build the value. */
  829. if (mode == MODE_RESETTING) {
  830. /* Unset the parameter. */
  831. value.string = 0;
  832. }
  833. else if (remainder == argc) {
  834. value.string = 0;
  835. }
  836. else {
  837. /* Set the parameter. */
  838. size_t len = 0;
  839. size_t delimiterlen = (setting->additional & ADDITIONAL_CRLF) ? 2 : 1;
  840. for (i = remainder; i < argc; i++) len += _tcslen(argv[i]) + delimiterlen;
  841. len++;
  842. value.string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  843. if (! value.string) {
  844. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("value"), _T("edit_service()"));
  845. CloseServiceHandle(service->handle);
  846. return 2;
  847. }
  848. size_t s = 0;
  849. for (i = remainder; i < argc; i++) {
  850. size_t len = _tcslen(argv[i]);
  851. memmove(value.string + s, argv[i], len * sizeof(TCHAR));
  852. s += len;
  853. if (i < argc - 1) {
  854. if (setting->additional & ADDITIONAL_CRLF) {
  855. value.string[s++] = _T('\r');
  856. value.string[s++] = _T('\n');
  857. }
  858. else value.string[s++] = _T(' ');
  859. }
  860. }
  861. value.string[s] = _T('\0');
  862. }
  863. if (! service->native) {
  864. key = open_registry(service->name, KEY_WRITE);
  865. if (! key) {
  866. if (value.string) HeapFree(GetProcessHeap(), 0, value.string);
  867. return 4;
  868. }
  869. }
  870. if (setting->native) ret = set_setting(service->name, service->handle, setting, &value, additional);
  871. else ret = set_setting(service->name, key, setting, &value, additional);
  872. if (value.string) HeapFree(GetProcessHeap(), 0, value.string);
  873. if (ret < 0) {
  874. if (! service->native) RegCloseKey(key);
  875. CloseServiceHandle(service->handle);
  876. return 6;
  877. }
  878. if (! service->native) RegCloseKey(key);
  879. CloseServiceHandle(service->handle);
  880. return 0;
  881. }
  882. /* About to remove the service */
  883. int pre_remove_service(int argc, TCHAR **argv) {
  884. nssm_service_t *service = alloc_nssm_service();
  885. set_nssm_service_defaults(service);
  886. if (argc) _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), argv[0]);
  887. /* Show dialogue box if we didn't pass service name and "confirm" */
  888. if (argc < 2) return nssm_gui(IDD_REMOVE, service);
  889. if (str_equiv(argv[1], _T("confirm"))) {
  890. int ret = remove_service(service);
  891. cleanup_nssm_service(service);
  892. return ret;
  893. }
  894. print_message(stderr, NSSM_MESSAGE_PRE_REMOVE_SERVICE);
  895. return 100;
  896. }
  897. /* Install the service */
  898. int install_service(nssm_service_t *service) {
  899. if (! service) return 1;
  900. /* Open service manager */
  901. SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE);
  902. if (! services) {
  903. print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  904. cleanup_nssm_service(service);
  905. return 2;
  906. }
  907. /* Get path of this program */
  908. GetModuleFileName(0, service->image, _countof(service->image));
  909. /* Create the service - settings will be changed in edit_service() */
  910. service->handle = CreateService(services, service->name, service->name, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, service->image, 0, 0, 0, 0, 0);
  911. if (! service->handle) {
  912. print_message(stderr, NSSM_MESSAGE_CREATESERVICE_FAILED, error_string(GetLastError()));
  913. CloseServiceHandle(services);
  914. return 5;
  915. }
  916. if (edit_service(service, false)) {
  917. DeleteService(service->handle);
  918. CloseServiceHandle(services);
  919. return 6;
  920. }
  921. print_message(stdout, NSSM_MESSAGE_SERVICE_INSTALLED, service->name);
  922. /* Cleanup */
  923. CloseServiceHandle(services);
  924. return 0;
  925. }
  926. /* Edit the service. */
  927. int edit_service(nssm_service_t *service, bool editing) {
  928. if (! service) return 1;
  929. /*
  930. The only two valid flags for service type are SERVICE_WIN32_OWN_PROCESS
  931. and SERVICE_INTERACTIVE_PROCESS.
  932. */
  933. service->type &= SERVICE_INTERACTIVE_PROCESS;
  934. service->type |= SERVICE_WIN32_OWN_PROCESS;
  935. /* Startup type. */
  936. unsigned long startup;
  937. switch (service->startup) {
  938. case NSSM_STARTUP_MANUAL: startup = SERVICE_DEMAND_START; break;
  939. case NSSM_STARTUP_DISABLED: startup = SERVICE_DISABLED; break;
  940. default: startup = SERVICE_AUTO_START;
  941. }
  942. /* Display name. */
  943. if (! service->displayname[0]) _sntprintf_s(service->displayname, _countof(service->displayname), _TRUNCATE, _T("%s"), service->name);
  944. /*
  945. Username must be NULL if we aren't changing or an account name.
  946. We must explicitly use LOCALSYSTEM to change it when we are editing.
  947. Password must be NULL if we aren't changing, a password or "".
  948. Empty passwords are valid but we won't allow them in the GUI.
  949. */
  950. TCHAR *username = 0;
  951. TCHAR *canon = 0;
  952. TCHAR *password = 0;
  953. if (service->usernamelen) {
  954. username = service->username;
  955. if (canonicalise_username(username, &canon)) return 5;
  956. if (service->passwordlen) password = service->password;
  957. }
  958. else if (editing) username = canon = NSSM_LOCALSYSTEM_ACCOUNT;
  959. if (well_known_username(canon)) password = _T("");
  960. else {
  961. if (grant_logon_as_service(canon)) {
  962. if (canon != username) HeapFree(GetProcessHeap(), 0, canon);
  963. print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username);
  964. return 5;
  965. }
  966. }
  967. TCHAR *dependencies = _T("");
  968. if (service->dependencieslen) dependencies = 0; /* Change later. */
  969. if (! ChangeServiceConfig(service->handle, service->type, startup, SERVICE_NO_CHANGE, 0, 0, 0, dependencies, canon, password, service->displayname)) {
  970. if (canon != username) HeapFree(GetProcessHeap(), 0, canon);
  971. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  972. return 5;
  973. }
  974. if (canon != username) HeapFree(GetProcessHeap(), 0, canon);
  975. if (service->dependencieslen) {
  976. if (set_service_dependencies(service->name, service->handle, service->dependencies)) return 5;
  977. }
  978. if (service->description[0] || editing) {
  979. set_service_description(service->name, service->handle, service->description);
  980. }
  981. SERVICE_DELAYED_AUTO_START_INFO delayed;
  982. ZeroMemory(&delayed, sizeof(delayed));
  983. if (service->startup == NSSM_STARTUP_DELAYED) delayed.fDelayedAutostart = 1;
  984. else delayed.fDelayedAutostart = 0;
  985. /* Delayed startup isn't supported until Vista. */
  986. if (! ChangeServiceConfig2(service->handle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, &delayed)) {
  987. unsigned long error = GetLastError();
  988. /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */
  989. if (error != ERROR_INVALID_LEVEL) {
  990. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SERVICE_CONFIG_DELAYED_AUTO_START_INFO_FAILED, service->name, error_string(error), 0);
  991. }
  992. }
  993. /* Don't mess with parameters which aren't ours. */
  994. if (! service->native) {
  995. /* Now we need to put the parameters into the registry */
  996. if (create_parameters(service, editing)) {
  997. print_message(stderr, NSSM_MESSAGE_CREATE_PARAMETERS_FAILED);
  998. return 6;
  999. }
  1000. set_service_recovery(service);
  1001. }
  1002. return 0;
  1003. }
  1004. /* Control a service. */
  1005. int control_service(unsigned long control, int argc, TCHAR **argv) {
  1006. if (argc < 1) return usage(1);
  1007. TCHAR *service_name = argv[0];
  1008. TCHAR canonical_name[SERVICE_NAME_LENGTH];
  1009. SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
  1010. if (! services) {
  1011. print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  1012. return 2;
  1013. }
  1014. unsigned long access = SERVICE_QUERY_STATUS;
  1015. switch (control) {
  1016. case NSSM_SERVICE_CONTROL_START:
  1017. access |= SERVICE_START;
  1018. break;
  1019. case SERVICE_CONTROL_CONTINUE:
  1020. case SERVICE_CONTROL_PAUSE:
  1021. access |= SERVICE_PAUSE_CONTINUE;
  1022. break;
  1023. case SERVICE_CONTROL_STOP:
  1024. access |= SERVICE_STOP;
  1025. break;
  1026. case NSSM_SERVICE_CONTROL_ROTATE:
  1027. access |= SERVICE_USER_DEFINED_CONTROL;
  1028. break;
  1029. }
  1030. SC_HANDLE service_handle = open_service(services, service_name, access, canonical_name, _countof(canonical_name));
  1031. if (! service_handle) {
  1032. CloseServiceHandle(services);
  1033. return 3;
  1034. }
  1035. int ret;
  1036. unsigned long error;
  1037. SERVICE_STATUS service_status;
  1038. if (control == NSSM_SERVICE_CONTROL_START) {
  1039. unsigned long initial_status = SERVICE_STOPPED;
  1040. ret = StartService(service_handle, (unsigned long) argc, (const TCHAR **) argv);
  1041. error = GetLastError();
  1042. CloseServiceHandle(services);
  1043. if (error == ERROR_IO_PENDING) {
  1044. /*
  1045. Older versions of Windows return immediately with ERROR_IO_PENDING
  1046. indicate that the operation is still in progress. Newer versions
  1047. will return it if there really is a delay.
  1048. */
  1049. ret = 1;
  1050. error = ERROR_SUCCESS;
  1051. }
  1052. if (ret) {
  1053. int response = await_service_control_response(control, service_handle, &service_status, initial_status);
  1054. CloseServiceHandle(service_handle);
  1055. if (response) {
  1056. print_message(stderr, NSSM_MESSAGE_BAD_CONTROL_RESPONSE, canonical_name, service_status_text(service_status.dwCurrentState), service_control_text(control));
  1057. return 1;
  1058. }
  1059. else _tprintf(_T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));
  1060. return 0;
  1061. }
  1062. else {
  1063. CloseServiceHandle(service_handle);
  1064. _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));
  1065. return 1;
  1066. }
  1067. }
  1068. else if (control == SERVICE_CONTROL_INTERROGATE) {
  1069. /*
  1070. We could actually send an INTERROGATE control but that won't return
  1071. any information if the service is stopped and we don't care about
  1072. the extra details it might give us in any case. So we'll fake it.
  1073. */
  1074. ret = QueryServiceStatus(service_handle, &service_status);
  1075. error = GetLastError();
  1076. if (ret) {
  1077. _tprintf(_T("%s\n"), service_status_text(service_status.dwCurrentState));
  1078. return 0;
  1079. }
  1080. else {
  1081. _ftprintf(stderr, _T("%s: %s\n"), canonical_name, error_string(error));
  1082. return 1;
  1083. }
  1084. }
  1085. else {
  1086. ret = ControlService(service_handle, control, &service_status);
  1087. unsigned long initial_status = service_status.dwCurrentState;
  1088. error = GetLastError();
  1089. CloseServiceHandle(services);
  1090. if (error == ERROR_IO_PENDING) {
  1091. ret = 1;
  1092. error = ERROR_SUCCESS;
  1093. }
  1094. if (ret) {
  1095. int response = await_service_control_response(control, service_handle, &service_status, initial_status);
  1096. CloseServiceHandle(service_handle);
  1097. if (response) {
  1098. print_message(stderr, NSSM_MESSAGE_BAD_CONTROL_RESPONSE, canonical_name, service_status_text(service_status.dwCurrentState), service_control_text(control));
  1099. return 1;
  1100. }
  1101. else _tprintf(_T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));
  1102. return 0;
  1103. }
  1104. else {
  1105. CloseServiceHandle(service_handle);
  1106. _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));
  1107. if (error == ERROR_SERVICE_NOT_ACTIVE) {
  1108. if (control == SERVICE_CONTROL_SHUTDOWN || control == SERVICE_CONTROL_STOP) return 0;
  1109. }
  1110. return 1;
  1111. }
  1112. }
  1113. }
  1114. /* Remove the service */
  1115. int remove_service(nssm_service_t *service) {
  1116. if (! service) return 1;
  1117. /* Open service manager */
  1118. SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
  1119. if (! services) {
  1120. print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  1121. return 2;
  1122. }
  1123. /* Try to open the service */
  1124. service->handle = open_service(services, service->name, DELETE, service->name, _countof(service->name));
  1125. if (! service->handle) {
  1126. CloseServiceHandle(services);
  1127. return 3;
  1128. }
  1129. /* Get the canonical service name. We open it case insensitively. */
  1130. unsigned long bufsize = _countof(service->displayname);
  1131. GetServiceDisplayName(services, service->name, service->displayname, &bufsize);
  1132. bufsize = _countof(service->name);
  1133. GetServiceKeyName(services, service->displayname, service->name, &bufsize);
  1134. /* Try to delete the service */
  1135. if (! DeleteService(service->handle)) {
  1136. print_message(stderr, NSSM_MESSAGE_DELETESERVICE_FAILED);
  1137. CloseServiceHandle(services);
  1138. return 4;
  1139. }
  1140. /* Cleanup */
  1141. CloseServiceHandle(services);
  1142. print_message(stdout, NSSM_MESSAGE_SERVICE_REMOVED, service->name);
  1143. return 0;
  1144. }
  1145. /* Service initialisation */
  1146. void WINAPI service_main(unsigned long argc, TCHAR **argv) {
  1147. nssm_service_t *service = alloc_nssm_service();
  1148. if (! service) return;
  1149. if (_sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), argv[0]) < 0) {
  1150. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("service->name"), _T("service_main()"), 0);
  1151. return;
  1152. }
  1153. /* We can use a condition variable in a critical section on Vista or later. */
  1154. if (imports.SleepConditionVariableCS && imports.WakeConditionVariable) use_critical_section = true;
  1155. else use_critical_section = false;
  1156. /* Initialise status */
  1157. ZeroMemory(&service->status, sizeof(service->status));
  1158. service->status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
  1159. service->status.dwControlsAccepted = SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
  1160. service->status.dwWin32ExitCode = NO_ERROR;
  1161. service->status.dwServiceSpecificExitCode = 0;
  1162. service->status.dwCheckPoint = 0;
  1163. service->status.dwWaitHint = NSSM_WAITHINT_MARGIN;
  1164. /* Signal we AREN'T running the server */
  1165. service->process_handle = 0;
  1166. service->pid = 0;
  1167. /* Register control handler */
  1168. service->status_handle = RegisterServiceCtrlHandlerEx(NSSM, service_control_handler, (void *) service);
  1169. if (! service->status_handle) {
  1170. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_REGISTERSERVICECTRLHANDER_FAILED, error_string(GetLastError()), 0);
  1171. return;
  1172. }
  1173. log_service_control(service->name, 0, true);
  1174. service->status.dwCurrentState = SERVICE_START_PENDING;
  1175. service->status.dwWaitHint = service->throttle_delay + NSSM_WAITHINT_MARGIN;
  1176. SetServiceStatus(service->status_handle, &service->status);
  1177. if (is_admin) {
  1178. /* Try to create the exit action parameters; we don't care if it fails */
  1179. create_exit_action(service->name, exit_action_strings[0], false);
  1180. SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT);
  1181. if (services) {
  1182. service->handle = open_service(services, service->name, SERVICE_CHANGE_CONFIG, 0, 0);
  1183. set_service_recovery(service);
  1184. CloseServiceHandle(services);
  1185. }
  1186. }
  1187. /* Used for signalling a resume if the service pauses when throttled. */
  1188. if (use_critical_section) {
  1189. InitializeCriticalSection(&service->throttle_section);
  1190. service->throttle_section_initialised = true;
  1191. }
  1192. else {
  1193. service->throttle_timer = CreateWaitableTimer(0, 1, 0);
  1194. if (! service->throttle_timer) {
  1195. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_CREATEWAITABLETIMER_FAILED, service->name, error_string(GetLastError()), 0);
  1196. }
  1197. }
  1198. /* Remember our initial environment. */
  1199. service->initial_env = GetEnvironmentStrings();
  1200. monitor_service(service);
  1201. }
  1202. /* Make sure service recovery actions are taken where necessary */
  1203. void set_service_recovery(nssm_service_t *service) {
  1204. SERVICE_FAILURE_ACTIONS_FLAG flag;
  1205. ZeroMemory(&flag, sizeof(flag));
  1206. flag.fFailureActionsOnNonCrashFailures = true;
  1207. /* This functionality was added in Vista so the call may fail */
  1208. if (! ChangeServiceConfig2(service->handle, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, &flag)) {
  1209. unsigned long error = GetLastError();
  1210. /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */
  1211. if (error != ERROR_INVALID_LEVEL) {
  1212. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SERVICE_CONFIG_FAILURE_ACTIONS_FAILED, service->name, error_string(error), 0);
  1213. }
  1214. }
  1215. }
  1216. int monitor_service(nssm_service_t *service) {
  1217. /* Set service status to started */
  1218. int ret = start_service(service);
  1219. if (ret) {
  1220. TCHAR code[16];
  1221. _sntprintf_s(code, _countof(code), _TRUNCATE, _T("%d"), ret);
  1222. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_START_SERVICE_FAILED, service->exe, service->name, ret, 0);
  1223. return ret;
  1224. }
  1225. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_STARTED_SERVICE, service->exe, service->flags, service->name, service->dir, 0);
  1226. /* Monitor service */
  1227. if (! RegisterWaitForSingleObject(&service->wait_handle, service->process_handle, end_service, (void *) service, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) {
  1228. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_REGISTERWAITFORSINGLEOBJECT_FAILED, service->name, service->exe, error_string(GetLastError()), 0);
  1229. }
  1230. return 0;
  1231. }
  1232. TCHAR *service_control_text(unsigned long control) {
  1233. switch (control) {
  1234. /* HACK: there is no SERVICE_CONTROL_START constant */
  1235. case NSSM_SERVICE_CONTROL_START: return _T("START");
  1236. case SERVICE_CONTROL_STOP: return _T("STOP");
  1237. case SERVICE_CONTROL_SHUTDOWN: return _T("SHUTDOWN");
  1238. case SERVICE_CONTROL_PAUSE: return _T("PAUSE");
  1239. case SERVICE_CONTROL_CONTINUE: return _T("CONTINUE");
  1240. case SERVICE_CONTROL_INTERROGATE: return _T("INTERROGATE");
  1241. case NSSM_SERVICE_CONTROL_ROTATE: return _T("ROTATE");
  1242. case SERVICE_CONTROL_POWEREVENT: return _T("POWEREVENT");
  1243. default: return 0;
  1244. }
  1245. }
  1246. TCHAR *service_status_text(unsigned long status) {
  1247. switch (status) {
  1248. case SERVICE_STOPPED: return _T("SERVICE_STOPPED");
  1249. case SERVICE_START_PENDING: return _T("SERVICE_START_PENDING");
  1250. case SERVICE_STOP_PENDING: return _T("SERVICE_STOP_PENDING");
  1251. case SERVICE_RUNNING: return _T("SERVICE_RUNNING");
  1252. case SERVICE_CONTINUE_PENDING: return _T("SERVICE_CONTINUE_PENDING");
  1253. case SERVICE_PAUSE_PENDING: return _T("SERVICE_PAUSE_PENDING");
  1254. case SERVICE_PAUSED: return _T("SERVICE_PAUSED");
  1255. default: return 0;
  1256. }
  1257. }
  1258. void log_service_control(TCHAR *service_name, unsigned long control, bool handled) {
  1259. TCHAR *text = service_control_text(control);
  1260. unsigned long event;
  1261. if (! text) {
  1262. /* "0x" + 8 x hex + NULL */
  1263. text = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, 11 * sizeof(TCHAR));
  1264. if (! text) {
  1265. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("control code"), _T("log_service_control()"), 0);
  1266. return;
  1267. }
  1268. if (_sntprintf_s(text, 11, _TRUNCATE, _T("0x%08x"), control) < 0) {
  1269. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("control code"), _T("log_service_control()"), 0);
  1270. HeapFree(GetProcessHeap(), 0, text);
  1271. return;
  1272. }
  1273. event = NSSM_EVENT_SERVICE_CONTROL_UNKNOWN;
  1274. }
  1275. else if (handled) event = NSSM_EVENT_SERVICE_CONTROL_HANDLED;
  1276. else event = NSSM_EVENT_SERVICE_CONTROL_NOT_HANDLED;
  1277. log_event(EVENTLOG_INFORMATION_TYPE, event, service_name, text, 0);
  1278. if (event == NSSM_EVENT_SERVICE_CONTROL_UNKNOWN) {
  1279. HeapFree(GetProcessHeap(), 0, text);
  1280. }
  1281. }
  1282. /* Service control handler */
  1283. unsigned long WINAPI service_control_handler(unsigned long control, unsigned long event, void *data, void *context) {
  1284. nssm_service_t *service = (nssm_service_t *) context;
  1285. switch (control) {
  1286. case SERVICE_CONTROL_INTERROGATE:
  1287. /* We always keep the service status up-to-date so this is a no-op. */
  1288. return NO_ERROR;
  1289. case SERVICE_CONTROL_SHUTDOWN:
  1290. case SERVICE_CONTROL_STOP:
  1291. log_service_control(service->name, control, true);
  1292. /*
  1293. We MUST acknowledge the stop request promptly but we're committed to
  1294. waiting for the application to exit. Spawn a new thread to wait
  1295. while we acknowledge the request.
  1296. */
  1297. if (! CreateThread(NULL, 0, shutdown_service, context, 0, NULL)) {
  1298. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETHREAD_FAILED, error_string(GetLastError()), 0);
  1299. /*
  1300. We couldn't create a thread to tidy up so we'll have to force the tidyup
  1301. to complete in time in this thread.
  1302. */
  1303. service->kill_console_delay = NSSM_KILL_CONSOLE_GRACE_PERIOD;
  1304. service->kill_window_delay = NSSM_KILL_WINDOW_GRACE_PERIOD;
  1305. service->kill_threads_delay = NSSM_KILL_THREADS_GRACE_PERIOD;
  1306. stop_service(service, 0, true, true);
  1307. }
  1308. return NO_ERROR;
  1309. case SERVICE_CONTROL_CONTINUE:
  1310. log_service_control(service->name, control, true);
  1311. service->throttle = 0;
  1312. if (use_critical_section) imports.WakeConditionVariable(&service->throttle_condition);
  1313. else {
  1314. if (! service->throttle_timer) return ERROR_CALL_NOT_IMPLEMENTED;
  1315. ZeroMemory(&service->throttle_duetime, sizeof(service->throttle_duetime));
  1316. SetWaitableTimer(service->throttle_timer, &service->throttle_duetime, 0, 0, 0, 0);
  1317. }
  1318. /* We can't continue if the application is running! */
  1319. if (! service->process_handle) service->status.dwCurrentState = SERVICE_CONTINUE_PENDING;
  1320. service->status.dwWaitHint = throttle_milliseconds(service->throttle) + NSSM_WAITHINT_MARGIN;
  1321. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESET_THROTTLE, service->name, 0);
  1322. SetServiceStatus(service->status_handle, &service->status);
  1323. return NO_ERROR;
  1324. case SERVICE_CONTROL_PAUSE:
  1325. /*
  1326. We don't accept pause messages but it isn't possible to register
  1327. only for continue messages so we have to handle this case.
  1328. */
  1329. log_service_control(service->name, control, false);
  1330. return ERROR_CALL_NOT_IMPLEMENTED;
  1331. case NSSM_SERVICE_CONTROL_ROTATE:
  1332. log_service_control(service->name, control, true);
  1333. if (service->rotate_stdout_online == NSSM_ROTATE_ONLINE) service->rotate_stdout_online = NSSM_ROTATE_ONLINE_ASAP;
  1334. if (service->rotate_stderr_online == NSSM_ROTATE_ONLINE) service->rotate_stderr_online = NSSM_ROTATE_ONLINE_ASAP;
  1335. return NO_ERROR;
  1336. case SERVICE_CONTROL_POWEREVENT:
  1337. if (event != PBT_APMRESUMEAUTOMATIC) {
  1338. log_service_control(service->name, control, false);
  1339. return NO_ERROR;
  1340. }
  1341. log_service_control(service->name, control, true);
  1342. end_service((void *) service, false);
  1343. return NO_ERROR;
  1344. }
  1345. /* Unknown control */
  1346. log_service_control(service->name, control, false);
  1347. return ERROR_CALL_NOT_IMPLEMENTED;
  1348. }
  1349. /* Start the service */
  1350. int start_service(nssm_service_t *service) {
  1351. service->stopping = false;
  1352. service->allow_restart = true;
  1353. if (service->process_handle) return 0;
  1354. /* Allocate a STARTUPINFO structure for a new process */
  1355. STARTUPINFO si;
  1356. ZeroMemory(&si, sizeof(si));
  1357. si.cb = sizeof(si);
  1358. /* Allocate a PROCESSINFO structure for the process */
  1359. PROCESS_INFORMATION pi;
  1360. ZeroMemory(&pi, sizeof(pi));
  1361. /* Get startup parameters */
  1362. int ret = get_parameters(service, &si);
  1363. if (ret) {
  1364. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_PARAMETERS_FAILED, service->name, 0);
  1365. return stop_service(service, 2, true, true);
  1366. }
  1367. /* Launch executable with arguments */
  1368. TCHAR cmd[CMD_LENGTH];
  1369. if (_sntprintf_s(cmd, _countof(cmd), _TRUNCATE, _T("\"%s\" %s"), service->exe, service->flags) < 0) {
  1370. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("command line"), _T("start_service"), 0);
  1371. return stop_service(service, 2, true, true);
  1372. }
  1373. throttle_restart(service);
  1374. /* Set the environment. */
  1375. if (service->env) duplicate_environment(service->env);
  1376. if (service->env_extra) set_environment_block(service->env_extra);
  1377. /* Set up I/O redirection. */
  1378. if (get_output_handles(service, &si)) {
  1379. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0);
  1380. if (! service->no_console) FreeConsole();
  1381. close_output_handles(&si);
  1382. return stop_service(service, 4, true, true);
  1383. }
  1384. bool inherit_handles = false;
  1385. if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true;
  1386. unsigned long flags = service->priority & priority_mask();
  1387. if (service->affinity) flags |= CREATE_SUSPENDED;
  1388. if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, 0, service->dir, &si, &pi)) {
  1389. unsigned long exitcode = 3;
  1390. unsigned long error = GetLastError();
  1391. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service->name, service->exe, error_string(error), 0);
  1392. close_output_handles(&si);
  1393. duplicate_environment_strings(service->initial_env);
  1394. return stop_service(service, exitcode, true, true);
  1395. }
  1396. service->process_handle = pi.hProcess;
  1397. service->pid = pi.dwProcessId;
  1398. if (get_process_creation_time(service->process_handle, &service->creation_time)) ZeroMemory(&service->creation_time, sizeof(service->creation_time));
  1399. close_output_handles(&si);
  1400. if (! service->no_console) FreeConsole();
  1401. /* Restore our environment. */
  1402. duplicate_environment_strings(service->initial_env);
  1403. if (service->affinity) {
  1404. /*
  1405. We are explicitly storing service->affinity as a 64-bit unsigned integer
  1406. so that we can parse it regardless of whether we're running in 32-bit
  1407. or 64-bit mode. The arguments to SetProcessAffinityMask(), however, are
  1408. defined as type DWORD_PTR and hence limited to 32 bits on a 32-bit system
  1409. (or when running the 32-bit NSSM).
  1410. The result is a lot of seemingly-unnecessary casting throughout the code
  1411. and potentially confusion when we actually try to start the service.
  1412. Having said that, however, it's unlikely that we're actually going to
  1413. run in 32-bit mode on a system which has more than 32 CPUs so the
  1414. likelihood of seeing a confusing situation is somewhat diminished.
  1415. */
  1416. DWORD_PTR affinity, system_affinity;
  1417. if (GetProcessAffinityMask(service->process_handle, &affinity, &system_affinity)) affinity = service->affinity & system_affinity;
  1418. else {
  1419. affinity = (DWORD_PTR) service->affinity;
  1420. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GETPROCESSAFFINITYMASK_FAILED, service->name, error_string(GetLastError()), 0);
  1421. }
  1422. if (! SetProcessAffinityMask(service->process_handle, affinity)) {
  1423. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_SETPROCESSAFFINITYMASK_FAILED, service->name, error_string(GetLastError()), 0);
  1424. }
  1425. ResumeThread(pi.hThread);
  1426. }
  1427. /*
  1428. Wait for a clean startup before changing the service status to RUNNING
  1429. but be mindful of the fact that we are blocking the service control manager
  1430. so abandon the wait before too much time has elapsed.
  1431. */
  1432. if (await_service_status_change(service, SERVICE_START_PENDING, _T("start_service"), service->throttle_delay) == 1) service->throttle = 0;
  1433. /* Signal successful start */
  1434. service->status.dwCurrentState = SERVICE_RUNNING;
  1435. SetServiceStatus(service->status_handle, &service->status);
  1436. /* Continue waiting for a clean startup. */
  1437. if (deadline == WAIT_TIMEOUT) {
  1438. if (service->throttle_delay > delay) {
  1439. if (WaitForSingleObject(service->process_handle, service->throttle_delay - delay) == WAIT_TIMEOUT) service->throttle = 0;
  1440. }
  1441. else service->throttle = 0;
  1442. }
  1443. /* Ensure the restart delay is always applied. */
  1444. if (service->restart_delay && ! service->throttle) service->throttle++;
  1445. return 0;
  1446. }
  1447. /* Stop the service */
  1448. int stop_service(nssm_service_t *service, unsigned long exitcode, bool graceful, bool default_action) {
  1449. service->allow_restart = false;
  1450. if (service->wait_handle) {
  1451. UnregisterWait(service->wait_handle);
  1452. service->wait_handle = 0;
  1453. }
  1454. service->rotate_stdout_online = service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;
  1455. if (default_action && ! exitcode && ! graceful) {
  1456. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_GRACEFUL_SUICIDE, service->name, service->exe, exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_REALLY], 0);
  1457. graceful = true;
  1458. }
  1459. /* Signal we are stopping */
  1460. if (graceful) {
  1461. service->status.dwCurrentState = SERVICE_STOP_PENDING;
  1462. service->status.dwWaitHint = NSSM_WAITHINT_MARGIN;
  1463. SetServiceStatus(service->status_handle, &service->status);
  1464. }
  1465. /* Nothing to do if service isn't running */
  1466. if (service->pid) {
  1467. /* Shut down service */
  1468. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_TERMINATEPROCESS, service->name, service->exe, 0);
  1469. kill_process(service, service->process_handle, service->pid, 0);
  1470. }
  1471. else log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_PROCESS_ALREADY_STOPPED, service->name, service->exe, 0);
  1472. end_service((void *) service, true);
  1473. /* Signal we stopped */
  1474. if (graceful) {
  1475. service->status.dwCurrentState = SERVICE_STOPPED;
  1476. if (exitcode) {
  1477. service->status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
  1478. service->status.dwServiceSpecificExitCode = exitcode;
  1479. }
  1480. else {
  1481. service->status.dwWin32ExitCode = NO_ERROR;
  1482. service->status.dwServiceSpecificExitCode = 0;
  1483. }
  1484. SetServiceStatus(service->status_handle, &service->status);
  1485. }
  1486. return exitcode;
  1487. }
  1488. /* Callback function triggered when the server exits */
  1489. void CALLBACK end_service(void *arg, unsigned char why) {
  1490. nssm_service_t *service = (nssm_service_t *) arg;
  1491. if (service->stopping) return;
  1492. service->stopping = true;
  1493. service->rotate_stdout_online = service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;
  1494. /* Use now as a dummy exit time. */
  1495. GetSystemTimeAsFileTime(&service->exit_time);
  1496. /* Check exit code */
  1497. unsigned long exitcode = 0;
  1498. TCHAR code[16];
  1499. if (service->process_handle) {
  1500. GetExitCodeProcess(service->process_handle, &exitcode);
  1501. /* Check real exit time. */
  1502. if (exitcode != STILL_ACTIVE) get_process_exit_time(service->process_handle, &service->exit_time);
  1503. CloseHandle(service->process_handle);
  1504. }
  1505. service->process_handle = 0;
  1506. /*
  1507. Log that the service ended BEFORE logging about killing the process
  1508. tree. See below for the possible values of the why argument.
  1509. */
  1510. if (! why) {
  1511. _sntprintf_s(code, _countof(code), _TRUNCATE, _T("%lu"), exitcode);
  1512. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, service->exe, service->name, code, 0);
  1513. }
  1514. /* Clean up. */
  1515. if (exitcode == STILL_ACTIVE) exitcode = 0;
  1516. if (service->pid && service->kill_process_tree) kill_process_tree(service, service->pid, exitcode, service->pid);
  1517. service->pid = 0;
  1518. /*
  1519. The why argument is true if our wait timed out or false otherwise.
  1520. Our wait is infinite so why will never be true when called by the system.
  1521. If it is indeed true, assume we were called from stop_service() because
  1522. this is a controlled shutdown, and don't take any restart action.
  1523. */
  1524. if (why) return;
  1525. if (! service->allow_restart) return;
  1526. /* What action should we take? */
  1527. int action = NSSM_EXIT_RESTART;
  1528. TCHAR action_string[ACTION_LEN];
  1529. bool default_action;
  1530. if (! get_exit_action(service->name, &exitcode, action_string, &default_action)) {
  1531. for (int i = 0; exit_action_strings[i]; i++) {
  1532. if (! _tcsnicmp((const TCHAR *) action_string, exit_action_strings[i], ACTION_LEN)) {
  1533. action = i;
  1534. break;
  1535. }
  1536. }
  1537. }
  1538. switch (action) {
  1539. /* Try to restart the service or return failure code to service manager */
  1540. case NSSM_EXIT_RESTART:
  1541. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_RESTART, service->name, code, exit_action_strings[action], service->exe, 0);
  1542. while (monitor_service(service)) {
  1543. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_RESTART_SERVICE_FAILED, service->exe, service->name, 0);
  1544. Sleep(30000);
  1545. }
  1546. break;
  1547. /* Do nothing, just like srvany would */
  1548. case NSSM_EXIT_IGNORE:
  1549. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_IGNORE, service->name, code, exit_action_strings[action], service->exe, 0);
  1550. Sleep(INFINITE);
  1551. break;
  1552. /* Tell the service manager we are finished */
  1553. case NSSM_EXIT_REALLY:
  1554. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_REALLY, service->name, code, exit_action_strings[action], 0);
  1555. stop_service(service, exitcode, true, default_action);
  1556. break;
  1557. /* Fake a crash so pre-Vista service managers will run recovery actions. */
  1558. case NSSM_EXIT_UNCLEAN:
  1559. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_UNCLEAN, service->name, code, exit_action_strings[action], 0);
  1560. stop_service(service, exitcode, false, default_action);
  1561. free_imports();
  1562. exit(exitcode);
  1563. break;
  1564. }
  1565. }
  1566. void throttle_restart(nssm_service_t *service) {
  1567. /* This can't be a restart if the service is already running. */
  1568. if (! service->throttle++) return;
  1569. unsigned long ms;
  1570. unsigned long throttle_ms = throttle_milliseconds(service->throttle);
  1571. TCHAR threshold[8], milliseconds[8];
  1572. if (service->restart_delay > throttle_ms) ms = service->restart_delay;
  1573. else ms = throttle_ms;
  1574. if (service->throttle > 7) service->throttle = 8;
  1575. _sntprintf_s(milliseconds, _countof(milliseconds), _TRUNCATE, _T("%lu"), ms);
  1576. if (service->throttle == 1 && service->restart_delay > throttle_ms) log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESTART_DELAY, service->name, milliseconds, 0);
  1577. else {
  1578. _sntprintf_s(threshold, _countof(threshold), _TRUNCATE, _T("%lu"), service->throttle_delay);
  1579. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service->name, threshold, milliseconds, 0);
  1580. }
  1581. if (use_critical_section) EnterCriticalSection(&service->throttle_section);
  1582. else if (service->throttle_timer) {
  1583. ZeroMemory(&service->throttle_duetime, sizeof(service->throttle_duetime));
  1584. service->throttle_duetime.QuadPart = 0 - (ms * 10000LL);
  1585. SetWaitableTimer(service->throttle_timer, &service->throttle_duetime, 0, 0, 0, 0);
  1586. }
  1587. service->status.dwCurrentState = SERVICE_PAUSED;
  1588. SetServiceStatus(service->status_handle, &service->status);
  1589. if (use_critical_section) {
  1590. imports.SleepConditionVariableCS(&service->throttle_condition, &service->throttle_section, ms);
  1591. LeaveCriticalSection(&service->throttle_section);
  1592. }
  1593. else {
  1594. if (service->throttle_timer) WaitForSingleObject(service->throttle_timer, INFINITE);
  1595. else Sleep(ms);
  1596. }
  1597. }
  1598. /*
  1599. When responding to a stop (or any other) request we need to set dwWaitHint to
  1600. the number of milliseconds we expect the operation to take, and optionally
  1601. increase dwCheckPoint. If dwWaitHint milliseconds elapses without the
  1602. operation completing or dwCheckPoint increasing, the system will consider the
  1603. service to be hung.
  1604. However the system will consider the service to be hung after 30000
  1605. milliseconds regardless of the value of dwWaitHint if dwCheckPoint has not
  1606. changed. Therefore if we want to wait longer than that we must periodically
  1607. increase dwCheckPoint.
  1608. Furthermore, it will consider the service to be hung after 60000 milliseconds
  1609. regardless of the value of dwCheckPoint unless dwWaitHint is increased every
  1610. time dwCheckPoint is also increased.
  1611. Our strategy then is to retrieve the initial dwWaitHint and wait for
  1612. NSSM_SERVICE_STATUS_DEADLINE milliseconds. If the process is still running
  1613. and we haven't finished waiting we increment dwCheckPoint and add whichever is
  1614. smaller of NSSM_SERVICE_STATUS_DEADLINE or the remaining timeout to
  1615. dwWaitHint.
  1616. Only doing both these things will prevent the system from killing the service.
  1617. Returns: 1 if the wait timed out.
  1618. 0 if the wait completed.
  1619. -1 on error.
  1620. */
  1621. int await_service_status_change(nssm_service_t *service, unsigned long status, unsigned long desired, TCHAR *function_name, unsigned long timeout) {
  1622. unsigned long interval;
  1623. unsigned long waithint;
  1624. unsigned long ret;
  1625. unsigned long waited;
  1626. TCHAR interval_milliseconds[16];
  1627. TCHAR timeout_milliseconds[16];
  1628. TCHAR waited_milliseconds[16];
  1629. TCHAR *function = function_name;
  1630. /* Add brackets to function name. */
  1631. size_t funclen = _tcslen(function_name) + 3;
  1632. TCHAR *func = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, funclen * sizeof(TCHAR));
  1633. if (func) {
  1634. if (_sntprintf_s(func, funclen, _TRUNCATE, _T("%s()"), function_name) > -1) function = func;
  1635. }
  1636. _sntprintf_s(timeout_milliseconds, _countof(timeout_milliseconds), _TRUNCATE, _T("%lu"), timeout);
  1637. waithint = service->status.dwWaitHint;
  1638. waited = 0;
  1639. while (waited < timeout) {
  1640. interval = timeout - waited;
  1641. if (interval > NSSM_SERVICE_STATUS_DEADLINE) interval = NSSM_SERVICE_STATUS_DEADLINE;
  1642. service->status.dwCurrentState = control;
  1643. service->status.dwWaitHint += interval;
  1644. service->status.dwCheckPoint++;
  1645. SetServiceStatus(service->status_handle, &service->status);
  1646. if (waited) {
  1647. _sntprintf_s(waited_milliseconds, _countof(waited_milliseconds), _TRUNCATE, _T("%lu"), waited);
  1648. _sntprintf_s(interval_milliseconds, _countof(interval_milliseconds), _TRUNCATE, _T("%lu"), interval);
  1649. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_AWAITING_SERVICE, function, service->name, waited_milliseconds, interval_milliseconds, timeout_milliseconds, service_status_text(status), 0);
  1650. }
  1651. switch (WaitForSingleObject(service->process_handle, interval)) {
  1652. case WAIT_OBJECT_0:
  1653. ret = 0;
  1654. goto awaited;
  1655. case WAIT_TIMEOUT:
  1656. ret = 1;
  1657. break;
  1658. default:
  1659. ret = -1;
  1660. goto awaited;
  1661. }
  1662. waited += interval;
  1663. }
  1664. awaited:
  1665. if (func) HeapFree(GetProcessHeap(), 0, func);
  1666. return ret;
  1667. }