forked from kirillkovalenko/nssm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.cpp
1450 lines (1190 loc) · 56.9 KB
/
settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "nssm.h"
/* XXX: (value && value->string) is probably bogus because value is probably never null */
/* Affinity. */
#define NSSM_AFFINITY_ALL _T("All")
/* Default value. */
#define NSSM_DEFAULT_STRING _T("Default")
extern const TCHAR *exit_action_strings[];
extern const TCHAR *startup_strings[];
extern const TCHAR *priority_strings[];
extern const TCHAR *hook_event_strings[];
extern const TCHAR *hook_action_strings[];
/* Does the parameter refer to the default value of the setting? */
static inline int is_default(const TCHAR *value) {
return (str_equiv(value, NSSM_DEFAULT_STRING) || str_equiv(value, _T("*")) || ! value[0]);
}
/* What type of parameter is it parameter? */
static inline bool is_string_type(const unsigned long type) {
return (type == REG_MULTI_SZ || type == REG_EXPAND_SZ || type == REG_SZ);
}
static inline bool is_numeric_type(const unsigned long type) {
return (type == REG_DWORD);
}
static int value_from_string(const TCHAR *name, value_t *value, const TCHAR *string) {
size_t len = _tcslen(string);
if (! len++) {
value->string = 0;
return 0;
}
value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
if (! value->string) {
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()"));
return -1;
}
if (_sntprintf_s(value->string, len, _TRUNCATE, _T("%s"), string) < 0) {
HeapFree(GetProcessHeap(), 0, value->string);
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()"));
return -1;
}
return 1;
}
/* Functions to manage NSSM-specific settings in the registry. */
static int setting_set_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
if (! key) return -1;
unsigned long number;
long error;
/* Resetting to default? */
if (! value || ! value->string) {
error = RegDeleteValue(key, name);
if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
return -1;
}
if (str_number(value->string, &number)) return -1;
if (default_value && number == PtrToUlong(default_value)) {
error = RegDeleteValue(key, name);
if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
return -1;
}
if (set_number(key, (TCHAR *) name, number)) return -1;
return 1;
}
static int setting_get_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
return get_number(key, (TCHAR *) name, &value->numeric, false);
}
static int setting_set_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
if (! key) return -1;
long error;
/* Resetting to default? */
if (! value || ! value->string) {
if (default_value) value->string = (TCHAR *) default_value;
else {
error = RegDeleteValue(key, name);
if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
return -1;
}
}
if (default_value && _tcslen((TCHAR *) default_value) && str_equiv(value->string, (TCHAR *) default_value)) {
error = RegDeleteValue(key, name);
if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
return -1;
}
if (set_expand_string(key, (TCHAR *) name, value->string)) return -1;
return 1;
}
static int setting_get_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
TCHAR buffer[VALUE_LENGTH];
if (get_string(key, (TCHAR *) name, (TCHAR *) buffer, (unsigned long) sizeof(buffer), false, false, false)) return -1;
return value_from_string(name, value, buffer);
}
static int setting_not_dumpable(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
return 0;
}
static int setting_dump_string(const TCHAR *service_name, void *param, const TCHAR *name, const value_t *value, const TCHAR *additional) {
TCHAR quoted_service_name[SERVICE_NAME_LENGTH * 2];
TCHAR quoted_value[VALUE_LENGTH * 2];
TCHAR quoted_additional[VALUE_LENGTH * 2];
TCHAR quoted_nssm[EXE_LENGTH * 2];
if (quote(service_name, quoted_service_name, _countof(quoted_service_name))) return 1;
if (additional) {
if (_tcslen(additional)) {
if (quote(additional, quoted_additional, _countof(quoted_additional))) return 3;
}
else _sntprintf_s(quoted_additional, _countof(quoted_additional), _TRUNCATE, _T("\"\""));
}
else quoted_additional[0] = _T('\0');
unsigned long type = (unsigned long) param;
if (is_string_type(type)) {
if (_tcslen(value->string)) {
if (quote(value->string, quoted_value, _countof(quoted_value))) return 2;
}
else _sntprintf_s(quoted_value, _countof(quoted_value), _TRUNCATE, _T("\"\""));
}
else if (is_numeric_type(type)) _sntprintf_s(quoted_value, _countof(quoted_value), _TRUNCATE, _T("%lu"), value->numeric);
else return 2;
if (quote(nssm_exe(), quoted_nssm, _countof(quoted_nssm))) return 3;
if (_tcslen(quoted_additional)) _tprintf(_T("%s set %s %s %s %s\n"), quoted_nssm, quoted_service_name, name, quoted_additional, quoted_value);
else _tprintf(_T("%s set %s %s %s\n"), quoted_nssm, quoted_service_name, name, quoted_value);
return 0;
}
static int setting_set_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
unsigned long exitcode;
TCHAR *code;
TCHAR action_string[ACTION_LEN];
if (additional) {
/* Default action? */
if (is_default(additional)) code = 0;
else {
if (str_number(additional, &exitcode)) return -1;
code = (TCHAR *) additional;
}
}
HKEY key = open_registry(service_name, name, KEY_WRITE);
if (! key) return -1;
long error;
int ret = 1;
/* Resetting to default? */
if (value && value->string) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), value->string);
else {
if (code) {
/* Delete explicit action. */
error = RegDeleteValue(key, code);
RegCloseKey(key);
if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, code, service_name, error_string(error));
return -1;
}
else {
/* Explicitly keep the default action. */
if (default_value) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), (TCHAR *) default_value);
ret = 0;
}
}
/* Validate the string. */
for (int i = 0; exit_action_strings[i]; i++) {
if (! _tcsnicmp((const TCHAR *) action_string, exit_action_strings[i], ACTION_LEN)) {
if (default_value && str_equiv(action_string, (TCHAR *) default_value)) ret = 0;
if (RegSetValueEx(key, code, 0, REG_SZ, (const unsigned char *) exit_action_strings[i], (unsigned long) (_tcslen(action_string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
print_message(stderr, NSSM_MESSAGE_SETVALUE_FAILED, code, service_name, error_string(GetLastError()));
RegCloseKey(key);
return -1;
}
RegCloseKey(key);
return ret;
}
}
print_message(stderr, NSSM_MESSAGE_INVALID_EXIT_ACTION, action_string);
for (int i = 0; exit_action_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), exit_action_strings[i]);
return -1;
}
static int setting_get_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
unsigned long exitcode = 0;
unsigned long *code = 0;
if (additional) {
if (! is_default(additional)) {
if (str_number(additional, &exitcode)) return -1;
code = &exitcode;
}
}
TCHAR action_string[ACTION_LEN];
bool default_action;
if (get_exit_action(service_name, code, action_string, &default_action)) return -1;
value_from_string(name, value, action_string);
if (default_action && ! _tcsnicmp((const TCHAR *) action_string, (TCHAR *) default_value, ACTION_LEN)) return 0;
return 1;
}
static int setting_dump_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
int errors = 0;
HKEY key = open_registry(service_name, NSSM_REG_EXIT, KEY_READ);
if (! key) return -1;
TCHAR code[16];
unsigned long index = 0;
while (true) {
int ret = enumerate_registry_values(key, &index, code, _countof(code));
if (ret == ERROR_NO_MORE_ITEMS) break;
if (ret != ERROR_SUCCESS) continue;
bool valid = true;
int i;
for (i = 0; i < _countof(code); i++) {
if (! code[i]) break;
if (code[i] >= _T('0') || code[i] <= _T('9')) continue;
valid = false;
break;
}
if (! valid) continue;
TCHAR *additional = (code[i]) ? code : NSSM_DEFAULT_STRING;
ret = setting_get_exit_action(service_name, 0, name, default_value, value, additional);
if (ret == 1) {
if (setting_dump_string(service_name, (void *) REG_SZ, name, value, additional)) errors++;
}
else if (ret < 0) errors++;
}
if (errors) return -1;
return 0;
}
static inline bool split_hook_name(const TCHAR *hook_name, TCHAR *hook_event, TCHAR *hook_action) {
TCHAR *s;
for (s = (TCHAR *) hook_name; *s; s++) {
if (*s == _T('/')) {
*s = _T('\0');
_sntprintf_s(hook_event, HOOK_NAME_LENGTH, _TRUNCATE, _T("%s"), hook_name);
*s++ = _T('/');
_sntprintf_s(hook_action, HOOK_NAME_LENGTH, _TRUNCATE, _T("%s"), s);
return valid_hook_name(hook_event, hook_action, false);
}
}
print_message(stderr, NSSM_MESSAGE_INVALID_HOOK_NAME, hook_name);
return false;
}
static int setting_set_hook(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
TCHAR hook_event[HOOK_NAME_LENGTH];
TCHAR hook_action[HOOK_NAME_LENGTH];
if (! split_hook_name(additional, hook_event, hook_action)) return -1;
TCHAR *cmd;
if (value && value->string) cmd = value->string;
else cmd = _T("");
if (set_hook(service_name, hook_event, hook_action, cmd)) return -1;
if (! _tcslen(cmd)) return 0;
return 1;
}
static int setting_get_hook(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
TCHAR hook_event[HOOK_NAME_LENGTH];
TCHAR hook_action[HOOK_NAME_LENGTH];
if (! split_hook_name(additional, hook_event, hook_action)) return -1;
TCHAR cmd[CMD_LENGTH];
if (get_hook(service_name, hook_event, hook_action, cmd, sizeof(cmd))) return -1;
value_from_string(name, value, cmd);
if (! _tcslen(cmd)) return 0;
return 1;
}
static int setting_dump_hooks(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
int i, j;
int errors = 0;
for (i = 0; hook_event_strings[i]; i++) {
const TCHAR *hook_event = hook_event_strings[i];
for (j = 0; hook_action_strings[j]; j++) {
const TCHAR *hook_action = hook_action_strings[j];
if (! valid_hook_name(hook_event, hook_action, true)) continue;
TCHAR hook_name[HOOK_NAME_LENGTH];
_sntprintf_s(hook_name, _countof(hook_name), _TRUNCATE, _T("%s/%s"), hook_event, hook_action);
int ret = setting_get_hook(service_name, param, name, default_value, value, hook_name);
if (ret != 1) {
if (ret < 0) errors++;
continue;
}
if (setting_dump_string(service_name, (void *) REG_SZ, name, value, hook_name)) errors++;
}
}
if (errors) return -1;
return 0;
}
static int setting_set_affinity(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
if (! key) return -1;
long error;
__int64 mask;
__int64 system_affinity = 0LL;
if (value && value->string) {
DWORD_PTR affinity;
if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, (DWORD_PTR *) &system_affinity)) system_affinity = ~0;
if (is_default(value->string) || str_equiv(value->string, NSSM_AFFINITY_ALL)) mask = 0LL;
else if (affinity_string_to_mask(value->string, &mask)) {
print_message(stderr, NSSM_MESSAGE_BOGUS_AFFINITY_MASK, value->string, num_cpus() - 1);
return -1;
}
}
else mask = 0LL;
if (! mask) {
error = RegDeleteValue(key, name);
if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
return -1;
}
/* Canonicalise. */
TCHAR *canon = 0;
if (affinity_mask_to_string(mask, &canon)) canon = value->string;
__int64 effective_affinity = mask & system_affinity;
if (effective_affinity != mask) {
/* Requested CPUs did not intersect with available CPUs? */
if (! effective_affinity) mask = effective_affinity = system_affinity;
TCHAR *system = 0;
if (! affinity_mask_to_string(system_affinity, &system)) {
TCHAR *effective = 0;
if (! affinity_mask_to_string(effective_affinity, &effective)) {
print_message(stderr, NSSM_MESSAGE_EFFECTIVE_AFFINITY_MASK, value->string, system, effective);
HeapFree(GetProcessHeap(), 0, effective);
}
HeapFree(GetProcessHeap(), 0, system);
}
}
if (RegSetValueEx(key, name, 0, REG_SZ, (const unsigned char *) canon, (unsigned long) (_tcslen(canon) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
if (canon != value->string) HeapFree(GetProcessHeap(), 0, canon);
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, name, error_string(GetLastError()), 0);
return -1;
}
if (canon != value->string) HeapFree(GetProcessHeap(), 0, canon);
return 1;
}
static int setting_get_affinity(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
if (! key) return -1;
unsigned long type;
TCHAR *buffer = 0;
unsigned long buflen = 0;
int ret = RegQueryValueEx(key, name, 0, &type, 0, &buflen);
if (ret == ERROR_FILE_NOT_FOUND) {
if (value_from_string(name, value, NSSM_AFFINITY_ALL) == 1) return 0;
return -1;
}
if (ret != ERROR_SUCCESS) return -1;
if (type != REG_SZ) return -1;
buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen);
if (! buffer) {
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("affinity"), _T("setting_get_affinity"));
return -1;
}
if (get_string(key, (TCHAR *) name, buffer, buflen, false, false, true)) {
HeapFree(GetProcessHeap(), 0, buffer);
return -1;
}
__int64 affinity;
if (affinity_string_to_mask(buffer, &affinity)) {
print_message(stderr, NSSM_MESSAGE_BOGUS_AFFINITY_MASK, buffer, num_cpus() - 1);
HeapFree(GetProcessHeap(), 0, buffer);
return -1;
}
HeapFree(GetProcessHeap(), 0, buffer);
/* Canonicalise. */
if (affinity_mask_to_string(affinity, &buffer)) {
if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
return -1;
}
ret = value_from_string(name, value, buffer);
HeapFree(GetProcessHeap(), 0, buffer);
return ret;
}
static int setting_set_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
if (! param) return -1;
TCHAR *string = 0;
TCHAR *unformatted = 0;
unsigned long envlen;
unsigned long newlen = 0;
int op = 0;
if (value && value->string && value->string[0]) {
string = value->string;
switch (string[0]) {
case _T('+'): op = 1; break;
case _T('-'): op = -1; break;
case _T(':'): string++; break;
}
}
if (op) {
string++;
TCHAR *env = 0;
if (get_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1;
if (env) {
int ret;
if (op > 0) ret = append_to_environment_block(env, envlen, string, &unformatted, &newlen);
else ret = remove_from_environment_block(env, envlen, string, &unformatted, &newlen);
if (envlen) HeapFree(GetProcessHeap(), 0, env);
if (ret) return -1;
string = unformatted;
}
else {
/*
No existing environment.
We can't remove from an empty environment so just treat an add
operation as setting a new string.
*/
if (op < 0) return 0;
op = 0;
}
}
if (! string || ! string[0]) {
long error = RegDeleteValue(key, name);
if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
return -1;
}
if (! op) {
if (unformat_double_null(string, (unsigned long) _tcslen(string), &unformatted, &newlen)) return -1;
}
if (test_environment(unformatted)) {
HeapFree(GetProcessHeap(), 0, unformatted);
print_message(stderr, NSSM_GUI_INVALID_ENVIRONMENT);
return -1;
}
if (RegSetValueEx(key, name, 0, REG_MULTI_SZ, (const unsigned char *) unformatted, (unsigned long) newlen * sizeof(TCHAR)) != ERROR_SUCCESS) {
if (newlen) HeapFree(GetProcessHeap(), 0, unformatted);
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV, error_string(GetLastError()), 0);
return -1;
}
if (newlen) HeapFree(GetProcessHeap(), 0, unformatted);
return 1;
}
static int setting_get_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
if (! param) return -1;
TCHAR *env = 0;
unsigned long envlen;
if (get_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1;
if (! envlen) return 0;
TCHAR *formatted;
unsigned long newlen;
if (format_double_null(env, envlen, &formatted, &newlen)) return -1;
int ret;
if (additional) {
/* Find named environment variable. */
TCHAR *s;
size_t len = _tcslen(additional);
for (s = env; *s; s++) {
/* Look for <additional>=<string> NULL NULL */
if (! _tcsnicmp(s, additional, len) && s[len] == _T('=')) {
/* Strip <key>= */
s += len + 1;
ret = value_from_string(name, value, s);
HeapFree(GetProcessHeap(), 0, env);
return ret;
}
/* Skip this string. */
for ( ; *s; s++);
}
HeapFree(GetProcessHeap(), 0, env);
return 0;
}
HeapFree(GetProcessHeap(), 0, env);
ret = value_from_string(name, value, formatted);
if (newlen) HeapFree(GetProcessHeap(), 0, formatted);
return ret;
}
static int setting_dump_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
int errors = 0;
HKEY key = (HKEY) param;
if (! param) return -1;
TCHAR *env = 0;
unsigned long envlen;
if (get_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1;
if (! envlen) return 0;
TCHAR *s;
for (s = env; *s; s++) {
size_t len = _tcslen(s) + 2;
value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
if (! value->string) {
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dump"), _T("setting_dump_environment"));
break;
}
_sntprintf_s(value->string, len, _TRUNCATE, _T("%c%s"), (s > env) ? _T('+') : _T(':'), s);
if (setting_dump_string(service_name, (void *) REG_SZ, name, value, 0)) errors++;
HeapFree(GetProcessHeap(), 0, value->string);
value->string = 0;
for ( ; *s; s++);
}
HeapFree(GetProcessHeap(), 0, env);
if (errors) return 1;
return 0;
}
static int setting_set_priority(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
if (! param) return -1;
TCHAR *priority_string;
int i;
long error;
if (value && value->string) priority_string = value->string;
else if (default_value) priority_string = (TCHAR *) default_value;
else {
error = RegDeleteValue(key, name);
if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
return -1;
}
for (i = 0; priority_strings[i]; i++) {
if (! str_equiv(priority_strings[i], priority_string)) continue;
if (default_value && str_equiv(priority_string, (TCHAR *) default_value)) {
error = RegDeleteValue(key, name);
if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
return -1;
}
if (set_number(key, (TCHAR *) name, priority_index_to_constant(i))) return -1;
return 1;
}
print_message(stderr, NSSM_MESSAGE_INVALID_PRIORITY, priority_string);
for (i = 0; priority_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), priority_strings[i]);
return -1;
}
static int setting_get_priority(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = (HKEY) param;
if (! param) return -1;
unsigned long constant;
switch (get_number(key, (TCHAR *) name, &constant, false)) {
case 0:
if (value_from_string(name, value, (const TCHAR *) default_value) == -1) return -1;
return 0;
case -1: return -1;
}
return value_from_string(name, value, priority_strings[priority_constant_to_index(constant)]);
}
static int setting_dump_priority(const TCHAR *service_name, void *key_ptr, const TCHAR *name, void *setting_ptr, value_t *value, const TCHAR *additional) {
settings_t *setting = (settings_t *) setting_ptr;
int ret = setting_get_priority(service_name, key_ptr, name, (void *) setting->default_value, value, 0);
if (ret != 1) return ret;
return setting_dump_string(service_name, (void *) REG_SZ, name, value, 0);
}
/* Functions to manage native service settings. */
static int native_set_dependon(const TCHAR *service_name, SC_HANDLE service_handle, TCHAR **dependencies, unsigned long *dependencieslen, value_t *value, int type) {
*dependencieslen = 0;
if (! value || ! value->string || ! value->string[0]) return 0;
TCHAR *string = value->string;
unsigned long buflen;
int op = 0;
switch (string[0]) {
case _T('+'): op = 1; break;
case _T('-'): op = -1; break;
case _T(':'): string++; break;
}
if (op) {
string++;
TCHAR *buffer = 0;
if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, type)) return -1;
if (buffer) {
int ret;
if (op > 0) ret = append_to_dependencies(buffer, buflen, string, dependencies, dependencieslen, type);
else ret = remove_from_dependencies(buffer, buflen, string, dependencies, dependencieslen, type);
if (buflen) HeapFree(GetProcessHeap(), 0, buffer);
return ret;
}
else {
/*
No existing list.
We can't remove from an empty list so just treat an add
operation as setting a new string.
*/
if (op < 0) return 0;
op = 0;
}
}
if (! op) {
TCHAR *unformatted = 0;
unsigned long newlen;
if (unformat_double_null(string, (unsigned long) _tcslen(string), &unformatted, &newlen)) return -1;
if (type == DEPENDENCY_GROUPS) {
/* Prepend group identifier. */
unsigned long missing = 0;
TCHAR *canon = unformatted;
size_t canonlen = 0;
TCHAR *s;
for (s = unformatted; *s; s++) {
if (*s != SC_GROUP_IDENTIFIER) missing++;
size_t len = _tcslen(s);
canonlen += len + 1;
s += len;
}
if (missing) {
/* Missing identifiers plus double NULL terminator. */
canonlen += missing + 1;
canon = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, canonlen * sizeof(TCHAR));
if (! canon) {
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("native_set_dependon"));
if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
return -1;
}
size_t i = 0;
for (s = unformatted; *s; s++) {
if (*s != SC_GROUP_IDENTIFIER) canon[i++] = SC_GROUP_IDENTIFIER;
size_t len = _tcslen(s);
memmove(canon + i, s, (len + 1) * sizeof(TCHAR));
i += len + 1;
s += len;
}
HeapFree(GetProcessHeap(), 0, unformatted);
unformatted = canon;
newlen = (unsigned long) canonlen;
}
}
*dependencies = unformatted;
*dependencieslen = newlen;
}
return 0;
}
static int native_set_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
SC_HANDLE service_handle = (SC_HANDLE) param;
if (! service_handle) return -1;
/*
Get existing service dependencies because we must set both types together.
*/
TCHAR *services_buffer;
unsigned long services_buflen;
if (get_service_dependencies(service_name, service_handle, &services_buffer, &services_buflen, DEPENDENCY_SERVICES)) return -1;
if (! value || ! value->string || ! value->string[0]) {
if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, services_buffer, 0, 0, 0)) {
print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
return -1;
}
if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
return 0;
}
/* Update the group list. */
TCHAR *groups_buffer;
unsigned long groups_buflen;
if (native_set_dependon(service_name, service_handle, &groups_buffer, &groups_buflen, value, DEPENDENCY_GROUPS)) return -1;
TCHAR *dependencies;
if (services_buflen > 2) {
dependencies = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (groups_buflen + services_buflen) * sizeof(TCHAR));
if (! dependencies) {
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dependencies"), _T("native_set_dependongroup"));
if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
return -1;
}
memmove(dependencies, services_buffer, services_buflen * sizeof(TCHAR));
memmove(dependencies + services_buflen - 1, groups_buffer, groups_buflen * sizeof(TCHAR));
}
else dependencies = groups_buffer;
int ret = 1;
if (set_service_dependencies(service_name, service_handle, dependencies)) ret = -1;
if (dependencies != groups_buffer) HeapFree(GetProcessHeap(), 0, dependencies);
if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
return ret;
}
static int native_get_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
SC_HANDLE service_handle = (SC_HANDLE) param;
if (! service_handle) return -1;
TCHAR *buffer;
unsigned long buflen;
if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_GROUPS)) return -1;
int ret;
if (buflen) {
TCHAR *formatted;
unsigned long newlen;
if (format_double_null(buffer, buflen, &formatted, &newlen)) {
HeapFree(GetProcessHeap(), 0, buffer);
return -1;
}
ret = value_from_string(name, value, formatted);
HeapFree(GetProcessHeap(), 0, formatted);
HeapFree(GetProcessHeap(), 0, buffer);
}
else {
value->string = 0;
ret = 0;
}
return ret;
}
static int setting_dump_dependon(const TCHAR *service_name, SC_HANDLE service_handle, const TCHAR *name, int type, value_t *value) {
int errors = 0;
TCHAR *dependencies = 0;
unsigned long dependencieslen;
if (get_service_dependencies(service_name, service_handle, &dependencies, &dependencieslen, type)) return -1;
if (! dependencieslen) return 0;
TCHAR *s;
for (s = dependencies; *s; s++) {
size_t len = _tcslen(s) + 2;
value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
if (! value->string) {
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dump"), _T("setting_dump_dependon"));
break;
}
_sntprintf_s(value->string, len, _TRUNCATE, _T("%c%s"), (s > dependencies) ? _T('+') : _T(':'), s);
if (setting_dump_string(service_name, (void *) REG_SZ, name, value, 0)) errors++;
HeapFree(GetProcessHeap(), 0, value->string);
value->string = 0;
for ( ; *s; s++);
}
HeapFree(GetProcessHeap(), 0, dependencies);
if (errors) return 1;
return 0;
}
static int native_dump_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
return setting_dump_dependon(service_name, (SC_HANDLE) param, name, DEPENDENCY_GROUPS, value);
}
static int native_set_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
SC_HANDLE service_handle = (SC_HANDLE) param;
if (! service_handle) return -1;
/*
Get existing group dependencies because we must set both types together.
*/
TCHAR *groups_buffer;
unsigned long groups_buflen;
if (get_service_dependencies(service_name, service_handle, &groups_buffer, &groups_buflen, DEPENDENCY_GROUPS)) return -1;
if (! value || ! value->string || ! value->string[0]) {
if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, groups_buffer, 0, 0, 0)) {
print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
return -1;
}
if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
return 0;
}
/* Update the service list. */
TCHAR *services_buffer;
unsigned long services_buflen;
if (native_set_dependon(service_name, service_handle, &services_buffer, &services_buflen, value, DEPENDENCY_SERVICES)) return -1;
TCHAR *dependencies;
if (groups_buflen > 2) {
dependencies = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (services_buflen + groups_buflen) * sizeof(TCHAR));
if (! dependencies) {
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dependencies"), _T("native_set_dependonservice"));
if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
return -1;
}
memmove(dependencies, services_buffer, services_buflen * sizeof(TCHAR));
memmove(dependencies + services_buflen - 1, groups_buffer, groups_buflen * sizeof(TCHAR));
}
else dependencies = services_buffer;
int ret = 1;
if (set_service_dependencies(service_name, service_handle, dependencies)) ret = -1;
if (dependencies != services_buffer) HeapFree(GetProcessHeap(), 0, dependencies);
if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
return ret;
}
static int native_get_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
SC_HANDLE service_handle = (SC_HANDLE) param;
if (! service_handle) return -1;
TCHAR *buffer;
unsigned long buflen;
if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_SERVICES)) return -1;
int ret;
if (buflen) {
TCHAR *formatted;
unsigned long newlen;
if (format_double_null(buffer, buflen, &formatted, &newlen)) {
HeapFree(GetProcessHeap(), 0, buffer);
return -1;
}
ret = value_from_string(name, value, formatted);
HeapFree(GetProcessHeap(), 0, formatted);
HeapFree(GetProcessHeap(), 0, buffer);
}
else {
value->string = 0;
ret = 0;
}
return ret;
}
static int native_dump_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
return setting_dump_dependon(service_name, (SC_HANDLE) param, name, DEPENDENCY_SERVICES, value);
}
int native_set_description(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
SC_HANDLE service_handle = (SC_HANDLE) param;
if (! service_handle) return -1;
TCHAR *description = 0;
if (value) description = value->string;
if (set_service_description(service_name, service_handle, description)) return -1;
if (description && description[0]) return 1;
return 0;
}
int native_get_description(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
SC_HANDLE service_handle = (SC_HANDLE) param;
if (! service_handle) return -1;
TCHAR buffer[VALUE_LENGTH];
if (get_service_description(service_name, service_handle, _countof(buffer), buffer)) return -1;
if (buffer[0]) return value_from_string(name, value, buffer);
value->string = 0;
return 0;
}
int native_set_displayname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
SC_HANDLE service_handle = (SC_HANDLE) param;
if (! service_handle) return -1;
TCHAR *displayname = 0;
if (value && value->string) displayname = value->string;
else displayname = (TCHAR *) service_name;
if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, displayname)) {
print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
return -1;
}
/*
If the display name and service name differ only in case,
ChangeServiceConfig() will return success but the display name will be
set to the service name, NOT the value passed to the function.
This appears to be a quirk of Windows rather than a bug here.
*/
if (displayname != service_name && ! str_equiv(displayname, service_name)) return 1;
return 0;
}
int native_get_displayname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
SC_HANDLE service_handle = (SC_HANDLE) param;
if (! service_handle) return -1;
QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
if (! qsc) return -1;
int ret = value_from_string(name, value, qsc->lpDisplayName);
HeapFree(GetProcessHeap(), 0, qsc);
return ret;
}
int native_set_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {