forked from win32service/win32service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
win32service.c
1470 lines (1289 loc) · 63.4 KB
/
win32service.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 8 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2020 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Wez Furlong <wez@php.net> |
| Maintainer: Jean-Baptiste Nahan <jbnahan@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id: win32service.c 313721 2011-07-26 11:46:19Z rquadling $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_win32service.h"
#include "php_win32service_int.h"
#include "SAPI.h"
#include "winbase.h"
#define SERVICES_REG_BASE_PRIORITY "BasePriority"
#define SERVICES_REG_KEY_ROOT "SYSTEM\\CurrentControlSet\\Services\\"
/* gargh! service_main run from a new thread that we don't spawn, so we can't do this nicely */
static void *tmp_service_g = NULL;
zend_class_entry *Win32ServiceException_ce_ptr;
static DWORD WINAPI service_handler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
{
zend_win32service_globals *g = (zend_win32service_globals*)lpContext;
DWORD code = NO_ERROR;
g->args.dwControl = dwControl;
g->args.dwEventType = dwEventType;
g->args.lpEventData = lpEventData; /* not safe to touch without copying for later reference */
if (dwControl == SERVICE_CONTROL_STOP) {
g->st.dwCurrentState = SERVICE_STOP_PENDING;
}
SetServiceStatus(g->sh, &g->st);
return code;
}
static void WINAPI service_main(DWORD argc, char **argv)
{
zend_win32service_globals *g = (zend_win32service_globals*)tmp_service_g;
DWORD base_priority;
HKEY hKey;
char *service_key;
long registry_result = ERROR_SUCCESS;
DWORD dwType = REG_DWORD;
DWORD dwSize = sizeof(DWORD);
// Set the base priority for this service.
/*spprintf(&service_key, 0, "%s%s", SERVICES_REG_KEY_ROOT, g->service_name);
registry_result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, service_key, 0, KEY_ALL_ACCESS, &hKey);
if (ERROR_SUCCESS == registry_result) {
registry_result = RegQueryValueEx(hKey, SERVICES_REG_BASE_PRIORITY, 0, &dwType, (LPBYTE)&base_priority, &dwSize);
}
efree(service_key);
if (hKey) {
RegCloseKey(hKey);
}
if (ERROR_SUCCESS != registry_result) {
g->code = registry_result;
SetEvent(g->event);
return;
}
if(!SetPriorityClass(GetCurrentProcess(), base_priority)) {
g->code = GetLastError();
SetEvent(g->event);
return;
}*/
g->st.dwServiceType = SERVICE_WIN32;
g->st.dwCurrentState = SERVICE_START_PENDING;
g->st.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE |
SERVICE_ACCEPT_HARDWAREPROFILECHANGE | SERVICE_ACCEPT_NETBINDCHANGE |
SERVICE_ACCEPT_PARAMCHANGE | SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_SESSIONCHANGE |
SERVICE_ACCEPT_PRESHUTDOWN | SERVICE_ACCEPT_TIMECHANGE | SERVICE_ACCEPT_TRIGGEREVENT;
g->sh = RegisterServiceCtrlHandlerEx(g->service_name, service_handler, g);
if (g->sh == (SERVICE_STATUS_HANDLE)0) {
g->code = GetLastError();
SetEvent(g->event);
return;
}
g->code = NO_ERROR;
SetEvent(g->event);
}
static DWORD WINAPI svc_thread_proc(LPVOID _globals)
{
zend_win32service_globals *g = (zend_win32service_globals*)_globals;
tmp_service_g = g;
if (!StartServiceCtrlDispatcher(g->te)) {
g->code = GetLastError();
SetEvent(g->event);
return 1;
}
/* not reached until service_main returns */
return 0;
}
static void convert_error_to_exception(DWORD code, const char *message)
{
if (code == ERROR_ACCESS_DENIED) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error access denied (%s)", message);
return;
}
if (code == ERROR_CIRCULAR_DEPENDENCY) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error circular dependency (%s)", message);
return;
}
if (code == ERROR_DATABASE_DOES_NOT_EXIST) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error database does not exist (%s)", message);
return;
}
if (code == ERROR_DEPENDENT_SERVICES_RUNNING) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error dependent services running (%s)", message);
return;
}
if (code == ERROR_DUPLICATE_SERVICE_NAME) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error duplicate service name (%s)", message);
return;
}
if (code == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error failed service controller connect (%s)", message);
return;
}
if (code == ERROR_INSUFFICIENT_BUFFER) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error insufficient buffer (%s)", message);
return;
}
if (code == ERROR_INVALID_DATA) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error invalid data (%s)", message);
return;
}
if (code == ERROR_INVALID_HANDLE) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error invalid handle (%s)", message);
return;
}
if (code == ERROR_INVALID_LEVEL) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error invalid level (%s)", message);
return;
}
if (code == ERROR_INVALID_NAME) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error invalid name (%s)", message);
return;
}
if (code == ERROR_INVALID_PARAMETER) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error invalid parameter (%s)", message);
return;
}
if (code == ERROR_INVALID_SERVICE_ACCOUNT) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error invalid service account (%s)", message);
return;
}
if (code == ERROR_INVALID_SERVICE_CONTROL) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error invalid service control (%s)", message);
return;
}
if (code == ERROR_PATH_NOT_FOUND) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error path not found (%s)", message);
return;
}
if (code == ERROR_SERVICE_ALREADY_RUNNING) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service already running (%s)", message);
return;
}
if (code == ERROR_SERVICE_CANNOT_ACCEPT_CTRL) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service cannot accept ctrl (%s)", message);
return;
}
if (code == ERROR_SERVICE_DATABASE_LOCKED) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service database locked (%s)", message);
return;
}
if (code == ERROR_SERVICE_DEPENDENCY_DELETED) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service dependency deleted (%s)", message);
return;
}
if (code == ERROR_SERVICE_DEPENDENCY_FAIL) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service dependency fail (%s)", message);
return;
}
if (code == ERROR_SERVICE_DISABLED) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service disabled (%s)", message);
return;
}
if (code == ERROR_SERVICE_DOES_NOT_EXIST) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service does not exist (%s)", message);
return;
}
if (code == ERROR_SERVICE_EXISTS) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service exists (%s)", message);
return;
}
if (code == ERROR_SERVICE_LOGON_FAILED) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service logon failed (%s)", message);
return;
}
if (code == ERROR_SERVICE_MARKED_FOR_DELETE) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service marked for delete (%s)", message);
return;
}
if (code == ERROR_SERVICE_NO_THREAD) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service no thread (%s)", message);
return;
}
if (code == ERROR_SERVICE_NOT_ACTIVE) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service not active (%s)", message);
return;
}
if (code == ERROR_SERVICE_REQUEST_TIMEOUT) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service request timeout (%s)", message);
return;
}
if (code == ERROR_SHUTDOWN_IN_PROGRESS) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error shutdown in progress (%s)", message);
return;
}
if (code == ERROR_SERVICE_SPECIFIC_ERROR) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Error service specific error (%s)", message);
return;
}
if (code != NO_ERROR) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, code, "Unknow error no %d (%s)", code, message);
return;
}
}
/* {{{ proto bool win32_start_service_ctrl_dispatcher(string $name, [bool gracefulExit])
Registers the script with the SCM, so that it can act as the service with the given name */
static PHP_FUNCTION(win32_start_service_ctrl_dispatcher)
{
if (strcmp(sapi_module.name, "cli") != 0 && strcmp(sapi_module.name, "embed") != 0) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, 0, "This function work only when using the CLI SAPI and called into the service code.");
RETURN_THROWS();
}
char *name;
size_t name_len;
zend_bool gracefulExitParam=SVCG(gracefulExit);
if (SVCG(svc_thread)) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, 0, "Service ctrl dispatcher already running");
RETURN_THROWS();
}
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &name, &name_len, &gracefulExitParam)) {
RETURN_THROWS();
}
if (name_len == 0) {
zend_argument_value_error(1, "the value cannot be empty");
RETURN_THROWS();
}
SVCG(service_name) = estrdup(name);
SVCG(gracefulExit)=gracefulExitParam;
SVCG(te)[0].lpServiceName = SVCG(service_name);
SVCG(te)[0].lpServiceProc = service_main;
SVCG(event) = CreateEvent(NULL, TRUE, FALSE, NULL);
SVCG(svc_thread) = CreateThread(NULL, 0, svc_thread_proc, &SVCG(svc_thread), 0, &SVCG(svc_thread_id));
if (SVCG(svc_thread) == NULL) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, 0, "Failed to start dispatcher thread");
RETURN_THROWS();
}
if (WAIT_OBJECT_0 == WaitForSingleObject(SVCG(event), 30000)) {
if (SVCG(code) == NO_ERROR) {
RETURN_TRUE;
}
CloseHandle(SVCG(svc_thread));
SVCG(svc_thread) = NULL;
convert_error_to_exception(SVCG(code), "");
RETURN_THROWS();
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool win32_set_service_exit_mode([bool gracefulExit])
Set (and get) the exit mode of the service, when set to true the service
will shut down gracefuly when PHP exits, when set to false it will not shut
down gracefuly, this will mean that the service will count as having failed
and the recovery action will be run */
static PHP_FUNCTION(win32_set_service_exit_mode)
{
if (strcmp(sapi_module.name, "cli") != 0 && strcmp(sapi_module.name, "embed") != 0) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, 0, "This function work only when using the CLI SAPI and called into the service code.");
RETURN_THROWS();
}
zend_bool gracefulExitParam=SVCG(gracefulExit);
zend_bool old_gracefulExitParam=SVCG(gracefulExit);
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &gracefulExitParam)) {
RETURN_THROWS();
}
SVCG(gracefulExit)=gracefulExitParam;
RETURN_BOOL(old_gracefulExitParam);
}
/* }}} */
/* {{{ proto long win32_set_service_exit_code([int exitCode])
Set (and get) the exit code of the service, when the service will shut down
gracefuly when PHP exits it's not used, when the service will shut down not
gracefuly the int is used for exitCode and the recovery action will be run
if exit code is not zero */
static PHP_FUNCTION(win32_set_service_exit_code)
{
if (strcmp(sapi_module.name, "cli") != 0 && strcmp(sapi_module.name, "embed") != 0) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, 0, "This function work only when using the CLI SAPI and called into the service code.");
RETURN_THROWS();
}
zend_long exitCodeParam=SVCG(gracefulExit);
zend_long old_exitCodeParam=SVCG(gracefulExit);
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &exitCodeParam)) {
RETURN_THROWS();
}
SVCG(exitCode)=exitCodeParam;
RETURN_LONG(old_exitCodeParam);
}
/* }}} */
/* {{{ proto bool win32_set_service_status(int status, [int checkpoint])
Update the service status */
static PHP_FUNCTION(win32_set_service_status)
{
if (strcmp(sapi_module.name, "cli") != 0 && strcmp(sapi_module.name, "embed") != 0) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, 0, "This function work only when using the CLI SAPI and called into the service code.");
RETURN_THROWS();
}
long status;
long checkpoint = 0;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &status, &checkpoint)) {
RETURN_THROWS();
}
SVCG(st.dwCurrentState) = status;
/* CheckPoints are only valid for the SERVICE_*_PENDING statuses. */
if ((status == SERVICE_CONTINUE_PENDING) || (status == SERVICE_PAUSE_PENDING) || (status == SERVICE_START_PENDING) || (status == SERVICE_STOP_PENDING)) {
SVCG(st.dwCheckPoint) = checkpoint;
}
if (!SetServiceStatus(SVCG(sh), &SVCG(st))) {
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
} else {
RETURN_TRUE;
}
}
/* }}} */
/* {{{ proto void win32_create_service(array details [, string machine])
Creates a new service entry in the SCM database */
static PHP_FUNCTION(win32_create_service)
{
zval *tmp;
zval *details;
char *machine = NULL;
size_t machine_len;
char *service = NULL;
char *display;
char *user;
char *password;
char *path;
char *params;
long svc_type;
long start_type;
long error_control;
long recovery_action1;
long recovery_action2;
long recovery_action3;
long recovery_delay;
char *recovery_reboot_msg = NULL;
char *recovery_command = NULL;
long recovery_reset_period;
char *load_order;
char *deps = NULL;
char *desc;
BOOL delayed_start;
BOOL recovery_enabled;
SC_HANDLE hsvc, hmgr;
char *path_and_params;
SERVICE_DESCRIPTION srvc_desc;
SERVICE_DELAYED_AUTO_START_INFO srvc_delayed_start;
SERVICE_FAILURE_ACTIONS srvc_failure_infos;
SERVICE_FAILURE_ACTIONS_FLAG srvc_failure_action;
DWORD base_priority;
HKEY hKey;
char *service_key;
long registry_result;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "a|s!", &details, &machine, &machine_len)) {
RETURN_THROWS();
}
#define STR_DETAIL(name, var, def) \
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(details), name, sizeof(name)-1)) != NULL) { \
if (IS_NULL != Z_TYPE_P(tmp)) { \
convert_to_string_ex(tmp); \
} else { \
convert_to_null_ex(tmp); \
} \
if (strlen(Z_STRVAL_P(tmp)) != Z_STRLEN_P(tmp)) { \
php_error_docref(NULL, E_WARNING, "malformed " name); \
RETURN_FALSE; \
} \
var = Z_STRVAL_P(tmp); \
} else { \
var = def; \
}
#define INT_DETAIL(name, var, def) \
if ((tmp = zend_hash_find(Z_ARRVAL_P(details), zend_string_init(name, strlen(name), 0))) != NULL) { \
convert_to_long_ex(tmp); \
var = Z_LVAL_P(tmp); \
} else { \
var = def; \
}
#define BOOL_DETAIL(name, var, def) \
if ((tmp = zend_hash_find(Z_ARRVAL_P(details), zend_string_init(name, strlen(name), 0))) != NULL) { \
convert_to_boolean_ex(tmp); \
var = Z_TYPE_P(tmp) == IS_TRUE ? 1 : 0; \
} else { \
var = def; \
}
#define ARRAY_TO_STR_DETAIL(name, var, def) \
if ((tmp = zend_hash_find(Z_ARRVAL_P(details), zend_string_init(name, strlen(name), 0))) != NULL) { \
if (Z_TYPE_P(tmp) == IS_STRING) { \
STR_DETAIL(name, var, def); \
} else if (Z_TYPE_P(tmp) == IS_ARRAY) { \
HashTable * myht = Z_ARRVAL_P(tmp); \
uint32_t count = zend_array_count(myht); \
if (count == 0) { \
var = NULL; \
} else { \
zend_ulong num; \
zend_string *key; \
zval *val; \
uint32_t totalLenth = 0; \
char ** var2 = emalloc(sizeof(char*) * count); \
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) { \
convert_to_string_ex(val); \
totalLenth += strlen(Z_STRVAL_P(val)) + 1; \
var2[num] = Z_STRVAL_P(val); \
} ZEND_HASH_FOREACH_END(); \
totalLenth++; \
var = emalloc(sizeof(char)*totalLenth); \
uint32_t j = 0; \
for (uint32_t l = 0; l < count; l++) { \
for (uint32_t k = 0; k <= strlen(var2[l]); k++) { \
var[j] = var2[l][k]; \
j++; \
} \
var[j] = '\0'; \
} \
var[totalLenth - 1] = '\0'; \
} \
} else { \
var = def; \
} \
} else { \
var = def; \
}
STR_DETAIL(INFO_SERVICE, service, NULL);
STR_DETAIL(INFO_DISPLAY, display, NULL);
STR_DETAIL(INFO_USER, user, NULL);
STR_DETAIL(INFO_PASSWORD, password, "");
STR_DETAIL(INFO_PATH, path, NULL);
STR_DETAIL(INFO_PARAMS, params, "");
STR_DETAIL(INFO_LOAD_ORDER, load_order, NULL);
ARRAY_TO_STR_DETAIL(INFO_DEPENDENCIES, deps, NULL);
STR_DETAIL(INFO_DESCRIPTION, desc, NULL);
INT_DETAIL(INFO_SVC_TYPE, svc_type, SERVICE_WIN32_OWN_PROCESS);
INT_DETAIL(INFO_START_TYPE, start_type, SERVICE_AUTO_START);
INT_DETAIL(INFO_ERROR_CONTROL, error_control, SERVICE_ERROR_IGNORE);
BOOL_DETAIL(INFO_DELAYED_START, delayed_start, 0); /* Allow Vista+ delayed service start. */
INT_DETAIL(INFO_BASE_PRIORITY, base_priority, NORMAL_PRIORITY_CLASS);
INT_DETAIL(INFO_RECOVERY_DELAY, recovery_delay, 60000);
INT_DETAIL(INFO_RECOVERY_ACTION_1, recovery_action1, SC_ACTION_NONE);
INT_DETAIL(INFO_RECOVERY_ACTION_2, recovery_action2, SC_ACTION_NONE);
INT_DETAIL(INFO_RECOVERY_ACTION_3, recovery_action3, SC_ACTION_NONE);
INT_DETAIL(INFO_RECOVERY_RESET_PERIOD, recovery_reset_period, 86400);
BOOL_DETAIL(INFO_RECOVERY_ENABLED, recovery_enabled, 0);
STR_DETAIL(INFO_RECOVERY_REBOOT_MSG, recovery_reboot_msg, NULL);
STR_DETAIL(INFO_RECOVERY_COMMAND, recovery_command, NULL);
if (service == NULL) {
zend_argument_value_error(1, "the value for key '%s' is wrong", INFO_SERVICE);
RETURN_THROWS();
}
if (strlen(service) < 2) {
zend_argument_value_error(1, "the value for key '%s' cannot be empty", INFO_SERVICE);
RETURN_THROWS();
}
if (path == NULL) {
zend_argument_value_error(1, "the value for key '%s' is wrong", INFO_PATH);
RETURN_THROWS();
}
if (svc_type != SERVICE_WIN32_OWN_PROCESS &&
svc_type != SERVICE_INTERACTIVE_PROCESS &&
svc_type != SERVICE_WIN32_OWN_PROCESS_INTERACTIVE) {
zend_argument_value_error(1,
"the value %d for '%s' key is wrong, Use WIN32_SERVICE_WIN32_OWN_PROCESS, WIN32_SERVICE_INTERACTIVE_PROCESS or WIN32_SERVICE_WIN32_OWN_PROCESS_INTERACTIVE constants",
svc_type,
INFO_SVC_TYPE);
RETURN_THROWS();
}
if (start_type != SERVICE_BOOT_START &&
start_type != SERVICE_SYSTEM_START &&
start_type != SERVICE_AUTO_START &&
start_type != SERVICE_DEMAND_START &&
start_type != SERVICE_DISABLED) {
zend_argument_value_error(1,
"the value %d for '%s' key is wrong, Use WIN32_SERVICE_BOOT_START, WIN32_SERVICE_SYSTEM_START, WIN32_SERVICE_AUTO_START, WIN32_SERVICE_DEMAND_START or WIN32_SERVICE_DISABLED constants",
start_type,
INFO_START_TYPE);
RETURN_THROWS();
}
if (error_control != SERVICE_ERROR_IGNORE &&
error_control != SERVICE_ERROR_NORMAL &&
error_control != SERVICE_ERROR_SEVERE &&
error_control != SERVICE_ERROR_CRITICAL) {
zend_argument_value_error(1,
"the value %d for '%s' key is wrong, Use WIN32_SERVICE_ERROR_IGNORE, WIN32_SERVICE_ERROR_NORMAL, WIN32_SERVICE_ERROR_SEVERE or WIN32_SERVICE_ERROR_CRITICAL constants",
error_control,
INFO_ERROR_CONTROL);
RETURN_THROWS();
}
if (base_priority != ABOVE_NORMAL_PRIORITY_CLASS &&
base_priority != BELOW_NORMAL_PRIORITY_CLASS &&
base_priority != HIGH_PRIORITY_CLASS &&
base_priority != IDLE_PRIORITY_CLASS &&
base_priority != NORMAL_PRIORITY_CLASS &&
base_priority != REALTIME_PRIORITY_CLASS) {
zend_argument_value_error(1,
"the value %d for '%s' key is wrong, Use WIN32_ABOVE_NORMAL_PRIORITY_CLASS, WIN32_BELOW_NORMAL_PRIORITY_CLASS, WIN32_HIGH_PRIORITY_CLASS, WIN32_IDLE_PRIORITY_CLASS, WIN32_NORMAL_PRIORITY_CLASS or WIN32_REALTIME_PRIORITY_CLASS constants",
base_priority,
INFO_BASE_PRIORITY);
RETURN_THROWS();
}
if (recovery_delay < 0 || recovery_delay > ZEND_LONG_MAX) {
zend_argument_value_error(1,
"the value for key '%s' must between 0 and " ZEND_LONG_FMT ". Got %d.",
INFO_RECOVERY_DELAY,
ZEND_LONG_MAX,
recovery_delay);
RETURN_THROWS();
}
if (recovery_action1 != SC_ACTION_NONE && recovery_action1 != SC_ACTION_REBOOT && recovery_action1 != SC_ACTION_RESTART && recovery_action1 != SC_ACTION_RUN_COMMAND) {
zend_argument_value_error(1,
"the value %d for '%s' key is wrong. Use WIN32_SC_ACTION_NONE, WIN32_SC_ACTION_REBOOT, WIN32_SC_ACTION_RESTART or WIN32_SC_ACTION_RUN_COMMAND constants",
recovery_action1,
INFO_RECOVERY_ACTION_1);
RETURN_THROWS();
}
if (recovery_action2 != SC_ACTION_NONE && recovery_action2 != SC_ACTION_REBOOT && recovery_action2 != SC_ACTION_RESTART && recovery_action2 != SC_ACTION_RUN_COMMAND) {
zend_argument_value_error(1,
"the value %d for '%s' key is wrong. Use WIN32_SC_ACTION_NONE, WIN32_SC_ACTION_REBOOT, WIN32_SC_ACTION_RESTART or WIN32_SC_ACTION_RUN_COMMAND constants",
recovery_action2,
INFO_RECOVERY_ACTION_2);
RETURN_THROWS();
}
if (recovery_action3 != SC_ACTION_NONE && recovery_action3 != SC_ACTION_REBOOT && recovery_action3 != SC_ACTION_RESTART && recovery_action3 != SC_ACTION_RUN_COMMAND) {
zend_argument_value_error(1,
"the value %d for '%s' key is wrong. Use WIN32_SC_ACTION_NONE, WIN32_SC_ACTION_REBOOT, WIN32_SC_ACTION_RESTART or WIN32_SC_ACTION_RUN_COMMAND constants",
recovery_action3,
INFO_RECOVERY_ACTION_3);
RETURN_THROWS();
}
if (recovery_reset_period < 0 || recovery_reset_period > ZEND_LONG_MAX) {
zend_argument_value_error(1,
"the value for key '%s' must between 0 and " ZEND_LONG_FMT ". Got %d.",
INFO_RECOVERY_RESET_PERIOD,
ZEND_LONG_MAX,
recovery_reset_period);
RETURN_THROWS();
}
srvc_desc.lpDescription = desc;
srvc_delayed_start.fDelayedAutostart = delayed_start;
srvc_failure_infos.lpRebootMsg = NULL;
srvc_failure_infos.lpCommand = NULL;
srvc_failure_infos.dwResetPeriod = recovery_reset_period;
if (recovery_reboot_msg != NULL) {
srvc_failure_infos.lpRebootMsg = recovery_reboot_msg;
}
if (recovery_command != NULL) {
srvc_failure_infos.lpCommand = recovery_command;
}
srvc_failure_infos.cActions = 3;
SC_ACTION recovery_actions[3];
recovery_actions[0].Delay = recovery_delay;
recovery_actions[0].Type = recovery_action1;
recovery_actions[1].Delay = recovery_delay;
recovery_actions[1].Type = recovery_action2;
recovery_actions[2].Delay = recovery_delay;
recovery_actions[2].Type = recovery_action3;
srvc_failure_infos.lpsaActions = recovery_actions;
srvc_failure_action.fFailureActionsOnNonCrashFailures = recovery_enabled;
/* Connect to the SCManager. */
hmgr = OpenSCManager(machine, NULL, SC_MANAGER_ALL_ACCESS);
/* Quit if no connection. */
if (!hmgr) {
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
/* Build service path and parameters. */
if (path == NULL) {
DWORD len;
char buf[MAX_PATH];
len = GetModuleFileName(NULL, buf, sizeof(buf));
buf[len] = '\0';
if (strchr(buf, ' '))
spprintf(&path_and_params, 0, "\"%s\" %s", buf, params);
else
spprintf(&path_and_params, 0, "%s %s", buf, params);
} else {
if (strchr(path, ' '))
spprintf(&path_and_params, 0, "\"%s\" %s", path, params);
else
spprintf(&path_and_params, 0, "%s %s", path, params);
}
/* If interact with desktop is set and no username supplied (Only LocalSystem allows InteractWithDesktop) then pass the path and params through %COMSPEC% /C "..." */
if (SERVICE_INTERACTIVE_PROCESS & svc_type && user == NULL) {
spprintf(&path_and_params, 0, "\"%s\" /C \"%s\"", getenv("COMSPEC"), path_and_params);
}
/* Register the service. */
hsvc = CreateService(hmgr,
service,
display ? display : service,
SERVICE_ALL_ACCESS,
svc_type,
start_type,
error_control,
path_and_params,
load_order,
NULL,
(LPCSTR)deps,
(LPCSTR)user,
(LPCSTR)password);
efree(path_and_params);
/* If there was an error :
- Creating the service
- Setting the service description
- Setting the delayed start
then track the error. */
if (!hsvc) {
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "on create service");
RETURN_THROWS();
}
if (!ChangeServiceConfig2(hsvc, SERVICE_CONFIG_DESCRIPTION, &srvc_desc)) {
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "service partially configured, error when defining the description");
RETURN_THROWS();
}
if ((start_type & SERVICE_AUTO_START && !ChangeServiceConfig2(hsvc, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, &srvc_delayed_start))) {
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "service partially configured, error on change the start type");
RETURN_THROWS();
}
if (!ChangeServiceConfig2(hsvc, SERVICE_CONFIG_FAILURE_ACTIONS, &srvc_failure_infos)) {
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "service partially configured, error on change the failure action");
RETURN_THROWS();
}
if (!ChangeServiceConfig2(hsvc, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, &srvc_failure_action)) {
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "service partially configured, error on change the failure action flag");
RETURN_THROWS();
}
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
return;
/* Store the base_priority in the registry. */
spprintf(&service_key, 0, "%s%s", SERVICES_REG_KEY_ROOT, service);
if (ERROR_SUCCESS != (registry_result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, service_key, 0, KEY_ALL_ACCESS, &hKey))) {
convert_error_to_exception(registry_result, "On openning registry key to set base_priority");
RETURN_THROWS();
}
if (ERROR_SUCCESS != (registry_result = RegSetValueEx(hKey, SERVICES_REG_BASE_PRIORITY, 0, REG_DWORD, (CONST BYTE*)&base_priority, sizeof(REG_DWORD)))) {
convert_error_to_exception(registry_result, "On change the registry key value of base_priority");
RETURN_THROWS();
}
RegCloseKey(hKey);
efree(service_key);
}
/* }}} */
/* {{{ proto void win32_delete_service(string servicename [, string machine])
Deletes a service entry from the SCM database */
static PHP_FUNCTION(win32_delete_service)
{
char *machine = NULL;
char *service = NULL;
size_t machine_len = 0;
size_t service_len = 0;
SC_HANDLE hsvc;
SC_HANDLE hmgr;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!", &service, &service_len, &machine, &machine_len)) {
RETURN_THROWS();
}
if (service_len == 0) {
zend_argument_value_error(1, "the value cannot be empty");
RETURN_THROWS();
}
hmgr = OpenSCManager(machine, NULL, SC_MANAGER_ALL_ACCESS);
if (!hmgr) {
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
hsvc = OpenService(hmgr, service, DELETE);
if (!hsvc) {
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
if (!DeleteService(hsvc)) {
CloseServiceHandle(hmgr);
CloseServiceHandle(hsvc);
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
}
/* }}} */
/* {{{ proto long win32_get_last_control_message()
Returns the last control message that was sent to this service process */
static PHP_FUNCTION(win32_get_last_control_message)
{
if (strcmp(sapi_module.name, "cli") != 0 && strcmp(sapi_module.name, "embed") != 0) {
zend_throw_exception_ex(Win32ServiceException_ce_ptr, 0, "This function work only when using the CLI SAPI and called into the service code.");
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_NONE();
RETURN_LONG(SVCG(args.dwControl));
}
/* }}} */
/* {{{ proto array win32_query_service_status(string servicename [, string machine])
Queries the status of a service */
static PHP_FUNCTION(win32_query_service_status)
{
char *machine = NULL;
char *service = NULL;
size_t machine_len = 0;
size_t service_len = 0;
SC_HANDLE hsvc;
SC_HANDLE hmgr;
SERVICE_STATUS_PROCESS *st;
DWORD size;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!", &service, &service_len, &machine, &machine_len)) {
RETURN_THROWS();
}
if (service_len == 0) {
zend_argument_value_error(1, "the value cannot be empty");
RETURN_THROWS();
}
hmgr = OpenSCManager(machine, NULL, GENERIC_READ);
if (!hmgr) {
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
hsvc = OpenService(hmgr, service, SERVICE_QUERY_STATUS);
if (!hsvc) {
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
size = sizeof(*st);
st = emalloc(size);
if (!QueryServiceStatusEx(hsvc, SC_STATUS_PROCESS_INFO,
(LPBYTE)st, size, &size)) {
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
efree(st);
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
st = erealloc(st, size);
if (!QueryServiceStatusEx(hsvc, SC_STATUS_PROCESS_INFO,
(LPBYTE)st, size, &size)) {
efree(st);
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
}
/* map the struct to an array */
array_init(return_value);
add_assoc_long(return_value, "ServiceType", st->dwServiceType);
add_assoc_long(return_value, "CurrentState", st->dwCurrentState);
add_assoc_long(return_value, "ControlsAccepted", st->dwControlsAccepted);
add_assoc_long(return_value, "Win32ExitCode", st->dwWin32ExitCode);
add_assoc_long(return_value, "ServiceSpecificExitCode", st->dwServiceSpecificExitCode);
add_assoc_long(return_value, "CheckPoint", st->dwCheckPoint);
add_assoc_long(return_value, "WaitHint", st->dwWaitHint);
add_assoc_long(return_value, "ProcessId", st->dwProcessId);
add_assoc_long(return_value, "ServiceFlags", st->dwServiceFlags);
efree(st);
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
}
/* }}} */
/* {{{ proto void win32_start_service(string servicename [, string machine])
Starts a service */
static PHP_FUNCTION(win32_start_service)
{
char *machine = NULL;
char *service = NULL;
size_t machine_len = 0;
size_t service_len = 0;
SC_HANDLE hsvc;
SC_HANDLE hmgr;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!", &service, &service_len, &machine, &machine_len)) {
RETURN_THROWS();
}
if (service_len == 0) {
zend_argument_value_error(1, "the value cannot be empty");
RETURN_THROWS();
}
hmgr = OpenSCManager(machine, NULL, SC_MANAGER_CONNECT);
if (!hmgr) {
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
hsvc = OpenService(hmgr, service, SERVICE_START);
if (!hsvc) {
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
if (!StartService(hsvc, 0, NULL)) {
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "");
RETURN_THROWS();
}
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
}
/* }}} */
static void win32_handle_service_controls(INTERNAL_FUNCTION_PARAMETERS, long access, long status) /* {{{ */
{
char *machine = NULL;
char *service = NULL;
size_t machine_len = 0;
size_t service_len = 0;
SC_HANDLE hsvc;
SC_HANDLE hmgr;
SERVICE_STATUS st;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!", &service, &service_len, &machine, &machine_len)) {
RETURN_THROWS();
}
if (service_len == 0) {
zend_argument_value_error(1, "the value cannot be empty");
RETURN_THROWS();
}
hmgr = OpenSCManager(machine, NULL, SC_MANAGER_CONNECT);
if (!hmgr) {
convert_error_to_exception(GetLastError(), "on openning manager");
RETURN_THROWS();
}
hsvc = OpenService(hmgr, service, access);
if (!hsvc) {
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "on openning service");
RETURN_THROWS();
}
if (!ControlService(hsvc, status, &st)) {
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
convert_error_to_exception(GetLastError(), "on send control");
RETURN_THROWS();
}
CloseServiceHandle(hsvc);
CloseServiceHandle(hmgr);
}
/* {{{ proto void win32_stop_service(string servicename [, string machine])
Stops a service */
static PHP_FUNCTION(win32_stop_service)
{
win32_handle_service_controls(INTERNAL_FUNCTION_PARAM_PASSTHRU, SERVICE_STOP, SERVICE_CONTROL_STOP);
}
/* }}} */
/* {{{ proto void win32_pause_service(string servicename [, string machine])
Pauses a service */
static PHP_FUNCTION(win32_pause_service)
{