From 41b620056c0d6c86129abd5961966d3a42ab1e39 Mon Sep 17 00:00:00 2001 From: hakril Date: Mon, 30 Dec 2024 06:58:07 -0800 Subject: [PATCH 1/4] nginx as windows service v1 --- src/core/nginx.c | 72 ++++++++++++++++ src/core/ngx_cycle.c | 2 + src/core/ngx_cycle.h | 1 + src/os/win32/ngx_process_cycle.c | 21 ++++- src/os/win32/ngx_service.c | 143 ++++++++++++++++++------------- 5 files changed, 177 insertions(+), 62 deletions(-) diff --git a/src/core/nginx.c b/src/core/nginx.c index 0deb27b7f..a1c25e36a 100644 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -183,6 +183,7 @@ ngx_module_t ngx_core_module = { static ngx_uint_t ngx_show_help; static ngx_uint_t ngx_show_version; static ngx_uint_t ngx_show_configure; +static ngx_uint_t ngx_service = 0; static u_char *ngx_prefix; static u_char *ngx_error_log; static u_char *ngx_conf_file; @@ -388,6 +389,57 @@ main(int argc, char *const *argv) } +#ifdef NGX_WIN32 +static ngx_int_t +ngx_install_windows_service() +{ + wchar_t nginx_path[1024]; + wchar_t nginx_service_command[1124]; + SC_HANDLE ScManagerHandle = NULL; + SC_HANDLE NginxServiceHandle = NULL; + + if (GetModuleFileNameW(NULL, nginx_path, 1024) == 1024) { + ngx_log_stderr(0, "GetModuleFileNameW() buffer to small for nginx path", ngx_errno); + } + printf("GetModuleFileNameW: %ls\n", nginx_path); + + swprintf_s(nginx_service_command, 1124, L"%ls -w", nginx_path); + printf("ServicePath: %ls\n", nginx_service_command); + + ScManagerHandle = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE); + if (ScManagerHandle == NULL) { + ngx_log_stderr(0, "OpenSCManagerW failed: errno == %d", ngx_errno); + } + + //"nginx" as service name is hardcoded is ngx_service : use a define ? + NginxServiceHandle = CreateServiceW(ScManagerHandle, + L"nginx", + L"Nginx server service", + SERVICE_ALL_ACCESS, + SERVICE_WIN32_OWN_PROCESS, + SERVICE_DEMAND_START, // Auto-start ? let user change it manually in windows interface ? + SERVICE_ERROR_NORMAL, + nginx_service_command, + NULL, + NULL, + NULL, + NULL, //L"NT AUTHORITY\\NetworkService", + NULL); + + if (NginxServiceHandle == NULL) { + ngx_log_stderr(0, "CreateServiceW failed: errno == %d", ngx_errno); + CloseServiceHandle(ScManagerHandle); + } else { + printf("Service created !\n"); + } + + CloseServiceHandle(NginxServiceHandle); + CloseServiceHandle(ScManagerHandle); + return 0; +} +#endif + + static void ngx_show_version_info(void) { @@ -929,6 +981,20 @@ ngx_get_options(int argc, char *const *argv) ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal); return NGX_ERROR; + #ifdef NGX_WIN32 + case 'i': + // TODO: remove this option if not a Windows build ? + ngx_install_windows_service(); + exit(0); // Do something else ? + + case 'w': + // Run as windows service + myprintf("Running as service\n"); + ngx_service = 1; // Error if other options (like -s) is provided ? + goto next; + + #endif + default: ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1)); return NGX_ERROR; @@ -1086,6 +1152,12 @@ ngx_process_options(ngx_cycle_t *cycle) cycle->log->log_level = NGX_LOG_INFO; } + #if NGX_WIN32 + if (ngx_service) { + cycle->service = 1; + } + #endif + return NGX_OK; } diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c index a75bdf878..cb6fabc28 100644 --- a/src/core/ngx_cycle.c +++ b/src/core/ngx_cycle.c @@ -82,6 +82,8 @@ ngx_init_cycle(ngx_cycle_t *old_cycle) cycle->log = log; cycle->old_cycle = old_cycle; + cycle->service = old_cycle->service; + cycle->conf_prefix.len = old_cycle->conf_prefix.len; cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix); if (cycle->conf_prefix.data == NULL) { diff --git a/src/core/ngx_cycle.h b/src/core/ngx_cycle.h index 0c47f25fe..67e0b7f68 100644 --- a/src/core/ngx_cycle.h +++ b/src/core/ngx_cycle.h @@ -83,6 +83,7 @@ struct ngx_cycle_s { ngx_str_t error_log; ngx_str_t lock_file; ngx_str_t hostname; + ngx_uint_t service; }; diff --git a/src/os/win32/ngx_process_cycle.c b/src/os/win32/ngx_process_cycle.c index a39335fd1..9865452ab 100644 --- a/src/os/win32/ngx_process_cycle.c +++ b/src/os/win32/ngx_process_cycle.c @@ -26,7 +26,11 @@ static ngx_thread_value_t __stdcall ngx_worker_thread(void *data); static ngx_thread_value_t __stdcall ngx_cache_manager_thread(void *data); static void ngx_cache_manager_process_handler(void); static ngx_thread_value_t __stdcall ngx_cache_loader_thread(void *data); +static ngx_thread_value_t __stdcall ngx_service_thread(void *data); +/* TODO: ngx_service.h */ + +ngx_int_t ngx_service(ngx_log_t *log); ngx_uint_t ngx_process; ngx_uint_t ngx_worker; @@ -46,7 +50,7 @@ ngx_uint_t ngx_exiting; HANDLE ngx_master_process_event; char ngx_master_process_event_name[NGX_PROCESS_SYNC_NAME]; -static HANDLE ngx_stop_event; +HANDLE ngx_stop_event; static char ngx_stop_event_name[NGX_PROCESS_SYNC_NAME]; static HANDLE ngx_quit_event; static char ngx_quit_event_name[NGX_PROCESS_SYNC_NAME]; @@ -68,6 +72,7 @@ ngx_master_process_cycle(ngx_cycle_t *cycle) ngx_int_t n; ngx_msec_t timer; ngx_uint_t live; + ngx_tid_t servicetid; HANDLE events[MAXIMUM_WAIT_OBJECTS]; ngx_sprintf((u_char *) ngx_master_process_event_name, @@ -116,6 +121,14 @@ ngx_master_process_cycle(ngx_cycle_t *cycle) ngx_close_listening_sockets(cycle); + if (cycle->service){ + // Create only if this thread does not already exists ? + if (ngx_create_thread(&servicetid, ngx_service_thread, NULL, cycle->log) != 0) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "Creating ngx_service_thread failed"); + exit(2); + } + } + if (ngx_start_worker_processes(cycle, NGX_PROCESS_RESPAWN) == 0) { exit(2); } @@ -754,7 +767,6 @@ ngx_worker_process_cycle(ngx_cycle_t *cycle, char *mevn) ngx_worker_process_exit(cycle); failed: - exit(2); } @@ -981,6 +993,11 @@ ngx_cache_loader_thread(void *data) return 0; } +static ngx_thread_value_t __stdcall +ngx_service_thread(void *data) +{ + return ngx_service((ngx_log_t*)data); +} void ngx_single_process_cycle(ngx_cycle_t *cycle) diff --git a/src/os/win32/ngx_service.c b/src/os/win32/ngx_service.c index 835d9cf00..19938d730 100644 --- a/src/os/win32/ngx_service.c +++ b/src/os/win32/ngx_service.c @@ -4,131 +4,154 @@ * Copyright (C) Nginx, Inc. */ - +#include +#include #define NGX_SERVICE_CONTROL_SHUTDOWN 128 #define NGX_SERVICE_CONTROL_REOPEN 129 +HANDLE ngx_stop_event; // Defined in src\os\win32\ngx_process_cycle.c +/* +void LpserviceMainFunctiona( + [in] DWORD dwNumServicesArgs, + [in] LPSTR *lpServiceArgVectors +) +*/ +void WINAPI service_main(DWORD dwNumServicesArgs, LPSTR *lpServiceArgVectors); -SERVICE_TABLE_ENTRY st[] = { - { "nginx", service_main }, +/* + +DWORD LphandlerFunctionEx( + [in] DWORD dwControl, + [in] DWORD dwEventType, + [in] LPVOID lpEventData, + [in] LPVOID lpContext +) + +*/ +DWORD WINAPI service_handler(DWORD control, DWORD type, void *data, void *ctx); + +SERVICE_STATUS_HANDLE service = 0; // Put this field in ngx_cycle ? + +SERVICE_TABLE_ENTRY service_table[] = { + { "nginx", service_main}, { NULL, NULL } }; ngx_int_t -ngx_service(ngx_log_t *log) +ngx_service() { /* primary thread */ - /* StartServiceCtrlDispatcher() should be called within 30 seconds */ - if (StartServiceCtrlDispatcher(st) == 0) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, + if (StartServiceCtrlDispatcher(service_table) == 0) { + ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno, "StartServiceCtrlDispatcher() failed"); return NGX_ERROR; } - return NGX_OK; } -void -service_main(u_int argc, char **argv) +void WINAPI +service_main(DWORD dwNumServicesArgs, LPSTR *lpServiceArgVectors) { SERVICE_STATUS status; - SERVICE_STATUS_HANDLE service; /* thread spawned by SCM */ - - service = RegisterServiceCtrlHandlerEx("nginx", service_handler, ctx); + service = RegisterServiceCtrlHandlerEx("nginx", service_handler, NULL); if (service == INVALID_HANDLE_VALUE) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, + ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno, "RegisterServiceCtrlHandlerEx() failed"); return; } + /* Use a more generic report_service_status ? */ status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; status.dwCurrentState = SERVICE_START_PENDING; - status.dwControlsAccepted = SERVICE_ACCEPT_STOP - |SERVICE_ACCEPT_PARAMCHANGE; + status.dwControlsAccepted = SERVICE_ACCEPT_STOP; status.dwWin32ExitCode = NO_ERROR; status.dwServiceSpecificExitCode = 0; status.dwCheckPoint = 1; status.dwWaitHint = 2000; /* SetServiceStatus() should be called within 80 seconds */ - if (SetServiceStatus(service, &status) == 0) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, + ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno, "SetServiceStatus() failed"); return; } /* init */ + // Do we have any init to wait for ? + // Init seems preety well advanced when the service thread is created status.dwCurrentState = SERVICE_RUNNING; status.dwCheckPoint = 0; status.dwWaitHint = 0; if (SetServiceStatus(service, &status) == 0) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, + ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno, "SetServiceStatus() failed"); return; } - /* call master or worker loop */ +} - /* - * master should use event notification and look status - * single should use iocp to get notifications from service handler - */ +int report_service_stop_status(DWORD dwCurrentState) +{ + SERVICE_STATUS status; + status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; + status.dwCurrentState = dwCurrentState; + status.dwControlsAccepted = 0; + status.dwWin32ExitCode = NO_ERROR; + status.dwServiceSpecificExitCode = 0; + status.dwCheckPoint = 0; + status.dwWaitHint = 0; + + /* SetServiceStatus() should be called within 80 seconds */ + if (SetServiceStatus(service, &status) == 0) { + return 1; + } + return 0; } -u_int -service_handler(u_int control, u_int type, void *data, void *ctx) + +DWORD WINAPI +service_handler(DWORD control, DWORD type, void *data, void *ctx) { - /* primary thread */ - - switch (control) { - - case SERVICE_CONTROL_INTERROGATE: - status = NGX_IOCP_INTERROGATE; - break; + switch (control) { + // case SERVICE_CONTROL_INTERROGATE: + // status = NGX_IOCP_INTERROGATE; + // break; + // case SERVICE_CONTROL_STOP: - status = NGX_IOCP_STOP; - break; - - case SERVICE_CONTROL_PARAMCHANGE: - status = NGX_IOCP_RECONFIGURE; - break; - - case NGX_SERVICE_CONTROL_SHUTDOWN: - status = NGX_IOCP_REOPEN; - break; - - case NGX_SERVICE_CONTROL_REOPEN: - status = NGX_IOCP_REOPEN; + report_service_stop_status(SERVICE_STOP_PENDING); + if (SetEvent(ngx_stop_event) == 0) { + ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno, + "SetEvent(ngx_stop_event) failed"); + } + report_service_stop_status(SERVICE_STOPPED); break; + // + // case SERVICE_CONTROL_PARAMCHANGE: + // status = NGX_IOCP_RECONFIGURE; + // break; + // + // case NGX_SERVICE_CONTROL_SHUTDOWN: + // status = NGX_IOCP_REOPEN; + // break; + // + // case NGX_SERVICE_CONTROL_REOPEN: + // status = NGX_IOCP_REOPEN; + // break; default: return ERROR_CALL_NOT_IMPLEMENTED; } - - if (ngx_single) { - if (PostQueuedCompletionStatus(iocp, ... status, ...) == 0) { - err = ngx_errno; - ngx_log_error(NGX_LOG_ALERT, log, err, - "PostQueuedCompletionStatus() failed"); - return err; - } - - } else { - Event - } - - return NO_ERROR; + return 0; } From 0cd3035d734e7d1eb07d7709f02421234836677d Mon Sep 17 00:00:00 2001 From: hakril Date: Mon, 30 Dec 2024 07:17:17 -0800 Subject: [PATCH 2/4] improve nginx service install commadline logic --- src/core/nginx.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/core/nginx.c b/src/core/nginx.c index a1c25e36a..e141fba30 100644 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -393,18 +393,17 @@ main(int argc, char *const *argv) static ngx_int_t ngx_install_windows_service() { - wchar_t nginx_path[1024]; - wchar_t nginx_service_command[1124]; + wchar_t *install_commandline = NULL; + wchar_t *i_opt_ptr = NULL; SC_HANDLE ScManagerHandle = NULL; SC_HANDLE NginxServiceHandle = NULL; - if (GetModuleFileNameW(NULL, nginx_path, 1024) == 1024) { - ngx_log_stderr(0, "GetModuleFileNameW() buffer to small for nginx path", ngx_errno); - } - printf("GetModuleFileNameW: %ls\n", nginx_path); - - swprintf_s(nginx_service_command, 1124, L"%ls -w", nginx_path); - printf("ServicePath: %ls\n", nginx_service_command); + // Replace install commandline by service run commandline by + // replacing the -i (install) option to a -w (windows service mode) + install_commandline = _wcsdup(GetCommandLineW()); + i_opt_ptr = wcsstr(install_commandline, L" -i"); + i_opt_ptr[2] = 'w'; + printf("Installing NGINX service with commandline: %ls\n", install_commandline); ScManagerHandle = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE); if (ScManagerHandle == NULL) { @@ -419,11 +418,11 @@ ngx_install_windows_service() SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, // Auto-start ? let user change it manually in windows interface ? SERVICE_ERROR_NORMAL, - nginx_service_command, + install_commandline, NULL, NULL, NULL, - NULL, //L"NT AUTHORITY\\NetworkService", + NULL, //L"NT AUTHORITY\\NetworkService", ? NULL); if (NginxServiceHandle == NULL) { @@ -989,7 +988,6 @@ ngx_get_options(int argc, char *const *argv) case 'w': // Run as windows service - myprintf("Running as service\n"); ngx_service = 1; // Error if other options (like -s) is provided ? goto next; From 27366328370ffa3aa649c1f5116860f2f77e5dd8 Mon Sep 17 00:00:00 2001 From: hakril Date: Mon, 30 Dec 2024 07:36:36 -0800 Subject: [PATCH 3/4] Fix formatting --- src/core/nginx.c | 90 ++++++++++++++++---------------- src/core/ngx_cycle.c | 2 +- src/core/ngx_cycle.h | 2 +- src/os/win32/ngx_process_cycle.c | 10 ++-- src/os/win32/ngx_service.c | 20 +++---- 5 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/core/nginx.c b/src/core/nginx.c index e141fba30..09ae42134 100644 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -394,9 +394,9 @@ static ngx_int_t ngx_install_windows_service() { wchar_t *install_commandline = NULL; - wchar_t *i_opt_ptr = NULL; - SC_HANDLE ScManagerHandle = NULL; - SC_HANDLE NginxServiceHandle = NULL; + wchar_t *i_opt_ptr = NULL; + SC_HANDLE ScManagerHandle = NULL; + SC_HANDLE NginxServiceHandle = NULL; // Replace install commandline by service run commandline by // replacing the -i (install) option to a -w (windows service mode) @@ -405,36 +405,36 @@ ngx_install_windows_service() i_opt_ptr[2] = 'w'; printf("Installing NGINX service with commandline: %ls\n", install_commandline); - ScManagerHandle = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE); - if (ScManagerHandle == NULL) { - ngx_log_stderr(0, "OpenSCManagerW failed: errno == %d", ngx_errno); - } + ScManagerHandle = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE); + if (ScManagerHandle == NULL) { + ngx_log_stderr(0, "OpenSCManagerW failed: errno == %d", ngx_errno); + } - //"nginx" as service name is hardcoded is ngx_service : use a define ? - NginxServiceHandle = CreateServiceW(ScManagerHandle, - L"nginx", - L"Nginx server service", - SERVICE_ALL_ACCESS, - SERVICE_WIN32_OWN_PROCESS, - SERVICE_DEMAND_START, // Auto-start ? let user change it manually in windows interface ? - SERVICE_ERROR_NORMAL, - install_commandline, - NULL, - NULL, - NULL, - NULL, //L"NT AUTHORITY\\NetworkService", ? - NULL); + //"nginx" as service name is hardcoded is ngx_service : use a define ? + NginxServiceHandle = CreateServiceW(ScManagerHandle, + L"nginx", + L"Nginx server service", + SERVICE_ALL_ACCESS, + SERVICE_WIN32_OWN_PROCESS, + SERVICE_DEMAND_START, // Auto-start ? let user change it manually in windows interface ? + SERVICE_ERROR_NORMAL, + install_commandline, + NULL, + NULL, + NULL, + NULL, //L"NT AUTHORITY\\NetworkService", ? + NULL); - if (NginxServiceHandle == NULL) { - ngx_log_stderr(0, "CreateServiceW failed: errno == %d", ngx_errno); - CloseServiceHandle(ScManagerHandle); - } else { - printf("Service created !\n"); - } + if (NginxServiceHandle == NULL) { + ngx_log_stderr(0, "CreateServiceW failed: errno == %d", ngx_errno); + CloseServiceHandle(ScManagerHandle); + } else { + printf("Service created !\n"); + } - CloseServiceHandle(NginxServiceHandle); - CloseServiceHandle(ScManagerHandle); - return 0; + CloseServiceHandle(NginxServiceHandle); + CloseServiceHandle(ScManagerHandle); + return 0; } #endif @@ -980,18 +980,18 @@ ngx_get_options(int argc, char *const *argv) ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal); return NGX_ERROR; - #ifdef NGX_WIN32 - case 'i': - // TODO: remove this option if not a Windows build ? - ngx_install_windows_service(); - exit(0); // Do something else ? + #ifdef NGX_WIN32 + case 'i': + // TODO: remove this option if not a Windows build ? + ngx_install_windows_service(); + exit(0); // Do something else ? - case 'w': - // Run as windows service - ngx_service = 1; // Error if other options (like -s) is provided ? - goto next; + case 'w': + // Run as windows service + ngx_service = 1; // Error if other options (like -s) is provided ? + goto next; - #endif + #endif default: ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1)); @@ -1150,11 +1150,11 @@ ngx_process_options(ngx_cycle_t *cycle) cycle->log->log_level = NGX_LOG_INFO; } - #if NGX_WIN32 - if (ngx_service) { - cycle->service = 1; - } - #endif + #if NGX_WIN32 + if (ngx_service) { + cycle->service = 1; + } + #endif return NGX_OK; } diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c index cb6fabc28..53eb4e343 100644 --- a/src/core/ngx_cycle.c +++ b/src/core/ngx_cycle.c @@ -82,7 +82,7 @@ ngx_init_cycle(ngx_cycle_t *old_cycle) cycle->log = log; cycle->old_cycle = old_cycle; - cycle->service = old_cycle->service; + cycle->service = old_cycle->service; cycle->conf_prefix.len = old_cycle->conf_prefix.len; cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix); diff --git a/src/core/ngx_cycle.h b/src/core/ngx_cycle.h index 67e0b7f68..cc8e14a5b 100644 --- a/src/core/ngx_cycle.h +++ b/src/core/ngx_cycle.h @@ -83,7 +83,7 @@ struct ngx_cycle_s { ngx_str_t error_log; ngx_str_t lock_file; ngx_str_t hostname; - ngx_uint_t service; + ngx_uint_t service; }; diff --git a/src/os/win32/ngx_process_cycle.c b/src/os/win32/ngx_process_cycle.c index 9865452ab..78c744c38 100644 --- a/src/os/win32/ngx_process_cycle.c +++ b/src/os/win32/ngx_process_cycle.c @@ -121,13 +121,13 @@ ngx_master_process_cycle(ngx_cycle_t *cycle) ngx_close_listening_sockets(cycle); - if (cycle->service){ + if (cycle->service){ // Create only if this thread does not already exists ? - if (ngx_create_thread(&servicetid, ngx_service_thread, NULL, cycle->log) != 0) { + if (ngx_create_thread(&servicetid, ngx_service_thread, NULL, cycle->log) != 0) { ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "Creating ngx_service_thread failed"); - exit(2); - } - } + exit(2); + } + } if (ngx_start_worker_processes(cycle, NGX_PROCESS_RESPAWN) == 0) { exit(2); diff --git a/src/os/win32/ngx_service.c b/src/os/win32/ngx_service.c index 19938d730..0755de150 100644 --- a/src/os/win32/ngx_service.c +++ b/src/os/win32/ngx_service.c @@ -101,7 +101,7 @@ service_main(DWORD dwNumServicesArgs, LPSTR *lpServiceArgVectors) int report_service_stop_status(DWORD dwCurrentState) { - SERVICE_STATUS status; + SERVICE_STATUS status; status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; status.dwCurrentState = dwCurrentState; @@ -128,24 +128,24 @@ service_handler(DWORD control, DWORD type, void *data, void *ctx) // case SERVICE_CONTROL_INTERROGATE: // status = NGX_IOCP_INTERROGATE; // break; - // + // case SERVICE_CONTROL_STOP: - report_service_stop_status(SERVICE_STOP_PENDING); - if (SetEvent(ngx_stop_event) == 0) { + report_service_stop_status(SERVICE_STOP_PENDING); + if (SetEvent(ngx_stop_event) == 0) { ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno, "SetEvent(ngx_stop_event) failed"); - } - report_service_stop_status(SERVICE_STOPPED); + } + report_service_stop_status(SERVICE_STOPPED); break; - // + // // case SERVICE_CONTROL_PARAMCHANGE: // status = NGX_IOCP_RECONFIGURE; // break; - // + // // case NGX_SERVICE_CONTROL_SHUTDOWN: // status = NGX_IOCP_REOPEN; // break; - // + // // case NGX_SERVICE_CONTROL_REOPEN: // status = NGX_IOCP_REOPEN; // break; @@ -153,5 +153,5 @@ service_handler(DWORD control, DWORD type, void *data, void *ctx) default: return ERROR_CALL_NOT_IMPLEMENTED; } - return 0; + return 0; } From 948624aba73b696aa5dcb826231784a97b966b00 Mon Sep 17 00:00:00 2001 From: hakril Date: Mon, 30 Dec 2024 07:39:52 -0800 Subject: [PATCH 4/4] Fix more formatting --- src/core/nginx.c | 11 +++++------ src/os/win32/ngx_service.c | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/core/nginx.c b/src/core/nginx.c index 09ae42134..11e5994f5 100644 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -980,18 +980,17 @@ ngx_get_options(int argc, char *const *argv) ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal); return NGX_ERROR; - #ifdef NGX_WIN32 +#ifdef NGX_WIN32 case 'i': - // TODO: remove this option if not a Windows build ? ngx_install_windows_service(); exit(0); // Do something else ? case 'w': // Run as windows service - ngx_service = 1; // Error if other options (like -s) is provided ? + ngx_service = 1; goto next; - #endif +#endif default: ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1)); @@ -1150,11 +1149,11 @@ ngx_process_options(ngx_cycle_t *cycle) cycle->log->log_level = NGX_LOG_INFO; } - #if NGX_WIN32 +#if NGX_WIN32 if (ngx_service) { cycle->service = 1; } - #endif +#endif return NGX_OK; } diff --git a/src/os/win32/ngx_service.c b/src/os/win32/ngx_service.c index 0755de150..dbb947275 100644 --- a/src/os/win32/ngx_service.c +++ b/src/os/win32/ngx_service.c @@ -153,5 +153,5 @@ service_handler(DWORD control, DWORD type, void *data, void *ctx) default: return ERROR_CALL_NOT_IMPLEMENTED; } - return 0; + return NO_ERROR; }