annotate src/os/win32/ngx_service.c @ 220:4f81b931e9ff

nginx-0.0.1-2004-01-08-11:47:17 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 08 Jan 2004 08:47:17 +0000
parents
children da8c5707af39
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
220
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
2 #define NGX_SERVICE_CONTROL_SHUTDOWN 128
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
3 #define NGX_SERVICE_CONTROL_REOPEN 129
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
5
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
6 SERVICE_TABLE_ENTRY st[] = {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
7 { "nginx", service_main },
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
8 { NULL, NULL }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
9 };
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
10
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
11
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
12 ngx_int_t ngx_service(ngx_log_t *log)
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
13 {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
14 /* primary thread */
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
15
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
16 /* StartServiceCtrlDispatcher() shouxpdl be called within 30 seconds */
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
17
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
18 if (StartServiceCtrlDispatcher(st) == 0) {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
19 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
20 "StartServiceCtrlDispatcher() failed");
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
21 return NGX_ERROR;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
22 }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
23
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
24 return NGX_OK;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
25 }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
26
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
27
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
28 void service_main(u_int argc, char **argv)
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
29 {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
30 SERVICE_STATUS status;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
31 SERVICE_STATUS_HANDLE service;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
32
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
33 /* thread spawned by SCM */
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
34
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
35 service = RegisterServiceCtrlHandlerEx("nginx", service_handler, ctx);
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
36 if (service == INVALID_HANDLE_VALUE) {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
37 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
38 "RegisterServiceCtrlHandlerEx() failed");
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
39 return;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
40 }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
41
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
42 status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
43 status.dwCurrentState = SERVICE_START_PENDING;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
44 status.dwControlsAccepted = SERVICE_ACCEPT_STOP
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
45 |SERVICE_ACCEPT_PARAMCHANGE;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
46 status.dwWin32ExitCode = NO_ERROR;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
47 status.dwServiceSpecificExitCode = 0;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
48 status.dwCheckPoint = 1;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
49 status.dwWaitHint = 2000;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
50
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
51 /* SetServiceStatus() should be called within 80 seconds */
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
52
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
53 if (SetServiceStatus(service, &status) == 0) {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
54 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
55 "SetServiceStatus() failed");
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
56 return;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
57 }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
58
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
59 /* init */
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
60
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
61 status.dwCurrentState = SERVICE_RUNNING;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
62 status.dwCheckPoint = 0;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
63 status.dwWaitHint = 0;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
64
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
65 if (SetServiceStatus(service, &status) == 0) {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
66 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
67 "SetServiceStatus() failed");
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
68 return;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
69 }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
70
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
71 /* call master or worker loop */
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
72
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
73 /*
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
74 * master should use event notification and look status
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
75 * single should use iocp to get notifications from service handler
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
76 */
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
77
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
78 }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
79
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
80
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
81 u_int service_handler(u_int control, u_int type, void *data, void *ctx)
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
82 {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
83 /* primary thread */
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
84
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
85 switch(control) {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
86
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
87 case SERVICE_CONTROL_INTERROGATE:
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
88 status = NGX_IOCP_INTERROGATE;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
89 break;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
90
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
91 case SERVICE_CONTROL_STOP:
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
92 status = NGX_IOCP_STOP;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
93 break;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
94
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
95 case SERVICE_CONTROL_PARAMCHANGE:
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
96 status = NGX_IOCP_RECONFIGURE;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
97 break;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
98
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
99 case NGX_SERVICE_CONTROL_SHUTDOWN:
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
100 status = NGX_IOCP_REOPEN;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
101 break;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
102
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
103 case NGX_SERVICE_CONTROL_REOPEN:
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
104 status = NGX_IOCP_REOPEN;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
105 break;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
106
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
107 default:
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
108 return ERROR_CALL_NOT_IMPLEMENTED;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
109 }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
110
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
111 if (ngx_single) {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
112 if (PostQueuedCompletionStatus(iocp, ... status, ...) == 0) {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
113 err = ngx_errno;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
114 ngx_log_error(NGX_LOG_ALERT, log, err,
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
115 "PostQueuedCompletionStatus() failed");
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
116 return err;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
117 }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
118
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
119 } else {
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
120 Event
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
121 }
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
122
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
123 return NO_ERROR;
4f81b931e9ff nginx-0.0.1-2004-01-08-11:47:17 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
124 }