1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
59 /* This module ALONE requires the window message API from user.h
60 * and the default APR include of windows.h will omit it, so
61 * preload the API symbols now...
65 #define _WIN32_WINNT 0x0400
86 #include "http_conf_globals.h"
88 #include "mpm_winnt.h"
89 #include "apr_strings.h"
91 static const char * service_name = NULL;
93 /* ### should be namespace-protected */
94 const char * display_name = NULL;
98 HANDLE mpm_thread; /* primary thread handle of the apache server */
99 HANDLE service_thread; /* thread service/monitor handle */
100 DWORD service_thread_id;/* thread service/monitor ID */
101 HANDLE signal_monitor; /* service monitor thread signal event */
102 SERVICE_STATUS ssStatus;
103 SERVICE_STATUS_HANDLE hServiceStatus;
106 static int ReportStatusToSCMgr(int currentState, int exitCode, int waitHint);
108 /* The service configuration's is stored under the following trees:
110 * HKLM\System\CurrentControlSet\Services\[service name]
113 * \ImagePath (NT Only)
114 * \Parameters\ConfigArgs
116 * For Win9x, the launch service command is stored under:
118 * HKLM\Software\Microsoft\Windows\CurrentVersion\RunServices\[service name]
122 /* exit() for Win32 is macro mapped (horrible, we agree) that allows us
123 * to catch the non-zero conditions and inform the console process that
124 * the application died, and hang on to the console a bit longer.
126 * The macro only maps for http_main.c and other sources that include
127 * the service.h header, so we best assume it's an error to exit from
128 * _any_ other module.
130 * If real_exit_code is reset to 0, it will not be set or trigger this
131 * behavior on exit. All service and child processes are expected to
132 * reset this flag to zero to avoid undesireable side effects.
134 int real_exit_code = 1;
136 void hold_console_open_on_error(void)
143 char *msg = "Note the errors or messages above, "
144 "and press the <ESC> key to exit. ";
145 CONSOLE_SCREEN_BUFFER_INFO coninfo;
151 hConIn = GetStdHandle(STD_INPUT_HANDLE);
152 hConErr = GetStdHandle(STD_ERROR_HANDLE);
153 if ((hConIn == INVALID_HANDLE_VALUE) || (hConErr == INVALID_HANDLE_VALUE))
155 if (!WriteConsole(hConErr, msg, strlen(msg), &result, NULL) || !result)
157 if (!GetConsoleScreenBufferInfo(hConErr, &coninfo))
159 if (!SetConsoleMode(hConIn, ENABLE_MOUSE_INPUT | 0x80))
165 while (PeekConsoleInput(hConIn, &in, 1, &result) && result)
167 if (!ReadConsoleInput(hConIn, &in, 1, &result) || !result)
169 if ((in.EventType == KEY_EVENT) && in.Event.KeyEvent.bKeyDown
170 && (in.Event.KeyEvent.uChar.AsciiChar == 27))
172 if (in.EventType == MOUSE_EVENT
173 && (in.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK))
176 remains = ((start + 30) - time(NULL));
177 sprintf (count, "%d...", remains);
178 if (!SetConsoleCursorPosition(hConErr, coninfo.dwCursorPosition))
180 if (!WriteConsole(hConErr, count, strlen(count), &result, NULL)
184 while ((remains > 0) && WaitForSingleObject(hConIn, 1000) != WAIT_FAILED);
187 static BOOL die_on_logoff = FALSE;
189 static LRESULT CALLBACK monitor_service_9x_proc(HWND hWnd, UINT msg,
190 WPARAM wParam, LPARAM lParam)
192 /* This is the WndProc procedure for our invisible window.
193 * When the user shuts down the system, this window is sent
194 * a signal WM_ENDSESSION. We clean up by signaling Apache
195 * to shut down, and idle until Apache's primary thread quits.
197 if ((msg == WM_ENDSESSION)
198 && (die_on_logoff || (lParam != ENDSESSION_LOGOFF)))
202 /* Don't leave this message until we are dead! */
203 WaitForSingleObject(globdat.mpm_thread, 30000);
206 return (DefWindowProc(hWnd, msg, wParam, lParam));
209 static DWORD WINAPI monitor_service_9x_thread(void *service_name)
211 /* When running as a service under Windows 9x, there is no console
212 * window present, and no ConsoleCtrlHandler to call when the system
213 * is shutdown. If the WatchWindow thread is created with a NULL
214 * service_name argument, then the ...SystemMonitor window class is
215 * used to create the "Apache" window to watch for logoff and shutdown.
216 * If the service_name is provided, the ...ServiceMonitor window class
217 * is used to create the window named by the service_name argument,
218 * and the logoff message is ignored.
224 wc.style = CS_GLOBALCLASS;
225 wc.lpfnWndProc = monitor_service_9x_proc;
231 wc.hbrBackground = NULL;
232 wc.lpszMenuName = NULL;
234 wc.lpszClassName = "ApacheWin95ServiceMonitor";
236 wc.lpszClassName = "ApacheWin95SystemMonitor";
238 die_on_logoff = service_name ? FALSE : TRUE;
240 if (!RegisterClass(&wc))
242 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(),
243 NULL, "Could not register window class for WatchWindow");
244 SetEvent(globdat.signal_monitor);
245 globdat.service_thread_id = 0;
249 /* Create an invisible window */
250 hwndMain = CreateWindow(wc.lpszClassName,
251 service_name ? (char *) service_name : "Apache",
252 WS_OVERLAPPEDWINDOW & ~WS_VISIBLE,
253 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
254 CW_USEDEFAULT, NULL, NULL, NULL, NULL);
258 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(),
259 NULL, "Could not create WatchWindow");
260 SetEvent(globdat.signal_monitor);
261 globdat.service_thread_id = 0;
265 /* If we succeed, eliminate the console window.
266 * Signal the parent we are all set up, and
267 * watch the message queue while the window lives.
270 SetEvent((HANDLE) globdat.signal_monitor);
271 while (GetMessage(&msg, NULL, 0, 0))
273 if (msg.message == WM_CLOSE)
274 DestroyWindow(hwndMain);
276 TranslateMessage(&msg);
277 DispatchMessage(&msg);
280 globdat.service_thread_id = 0;
285 static BOOL CALLBACK console_control_handler(DWORD ctrl_type)
289 case CTRL_BREAK_EVENT:
290 fprintf(stderr, "Apache server restarting...\n");
294 fprintf(stderr, "Apache server interrupted...\n");
295 /* for Interrupt signals, shut down the server.
296 * Tell the system we have dealt with the signal
297 * without waiting for Apache to terminate.
302 case CTRL_CLOSE_EVENT:
303 case CTRL_LOGOFF_EVENT:
304 case CTRL_SHUTDOWN_EVENT:
305 /* for Terminate signals, shut down the server.
306 * Wait for Apache to terminate, but respond
307 * after a reasonable time to tell the system
308 * that we did attempt to shut ourself down.
309 * THESE EVENTS WILL NOT OCCUR UNDER WIN9x!
311 fprintf(stderr, "Apache server shutdown initiated...\n");
317 /* We should never get here, but this is (mostly) harmless */
322 static void stop_console_handler(void)
324 SetConsoleCtrlHandler(console_control_handler, FALSE);
328 void mpm_start_console_handler(void)
330 SetConsoleCtrlHandler(console_control_handler, TRUE);
331 atexit(stop_console_handler);
335 /* Special situation - children of services need to mind their
336 * P's & Q's and wait quietly, ignoring the mean OS signaling
337 * shutdown and other horrors, to kill them gracefully...
340 static BOOL CALLBACK child_control_handler(DWORD ctrl_type)
345 case CTRL_BREAK_EVENT:
346 /* for Interrupt signals, ignore them.
347 * The system will also signal the parent process,
348 * which will terminate Apache.
352 case CTRL_CLOSE_EVENT:
353 case CTRL_LOGOFF_EVENT:
354 case CTRL_SHUTDOWN_EVENT:
355 /* for Shutdown signals, ignore them, but... .
356 * The system will also signal the parent process,
357 * which will terminate Apache, so we need to wait.
363 /* We should never get here, but this is (mostly) harmless */
368 static void stop_child_console_handler(void)
370 SetConsoleCtrlHandler(child_control_handler, FALSE);
374 void mpm_start_child_console_handler(void)
376 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
381 SetConsoleCtrlHandler(child_control_handler, TRUE);
382 atexit(stop_child_console_handler);
387 /**********************************
388 WinNT service control management
389 **********************************/
391 static int ReportStatusToSCMgr(int currentState, int exitCode, int waitHint)
393 static int checkPoint = 1;
394 int rv = APR_SUCCESS;
396 if (globdat.hServiceStatus)
398 if (currentState == SERVICE_RUNNING)
399 globdat.ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
401 globdat.ssStatus.dwControlsAccepted = 0;
403 globdat.ssStatus.dwCurrentState = currentState;
404 globdat.ssStatus.dwWin32ExitCode = exitCode;
406 if ( ( currentState == SERVICE_RUNNING ) ||
407 ( currentState == SERVICE_STOPPED ) )
409 globdat.ssStatus.dwWaitHint = 0;
410 globdat.ssStatus.dwCheckPoint = 0;
415 globdat.ssStatus.dwWaitHint = waitHint;
416 globdat.ssStatus.dwCheckPoint = ++checkPoint;
418 rv = SetServiceStatus(globdat.hServiceStatus, &globdat.ssStatus);
424 /* handle the SCM's ControlService() callbacks to our service */
426 static VOID WINAPI service_nt_ctrl(DWORD dwCtrlCode)
428 if (dwCtrlCode == SERVICE_CONTROL_STOP)
431 globdat.ssStatus.dwCurrentState = SERVICE_STOP_PENDING;
432 ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 3000);
435 if (dwCtrlCode == SERVICE_APACHE_RESTART)
438 globdat.ssStatus.dwCurrentState = SERVICE_START_PENDING;
439 ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000);
443 ReportStatusToSCMgr(globdat.ssStatus.dwCurrentState, NO_ERROR, 0);
447 long __stdcall service_stderr_thread(LPVOID hPipe)
449 HANDLE hPipeRead = (HANDLE) hPipe;
452 char *errmsg = errbuf;
453 const char *errarg[9];
458 errarg[0] = "The Apache service named";
459 errarg[1] = display_name;
460 errarg[2] = "reported the following error:\r\n>>>";
462 errarg[4] = "<<<\r\n before the error.log file could be opened.\r\n";
463 errarg[5] = "More information may be available in the error.log file.";
468 /* What are we going to do in here, bail on the user? not. */
469 if (!RegCreateKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services"
470 "\\EventLog\\Application\\Apache Service", &hk))
472 /* The stock message file */
473 char *netmsgkey = "%SystemRoot%\\System32\\netmsg.dll";
474 DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
475 EVENTLOG_INFORMATION_TYPE;
477 RegSetValueEx(hk, "EventMessageFile", 0, REG_EXPAND_SZ,
478 (LPBYTE) netmsgkey, strlen(netmsgkey) + 1);
480 RegSetValueEx(hk, "TypesSupported", 0, REG_DWORD,
481 (LPBYTE) &dwData, sizeof(dwData));
485 hEventSource = RegisterEventSource(NULL, "Apache Service");
487 while (ReadFile(hPipeRead, errmsg, 1, &errres, NULL) && (errres == 1))
489 if ((errmsg > errbuf) || !isspace(*errmsg))
493 if ((*(errmsg - 1) == '\n') || (errlen == sizeof(errbuf) - 1))
495 while (errlen && isspace(errbuf[errlen - 1]))
497 errbuf[errlen] = '\0';
499 /* Generic message: '%1 %2 %3 %4 %5 %6 %7 %8 %9'
500 * The event code in netmsg.dll is 3299
502 ReportEvent(hEventSource, EVENTLOG_ERROR_TYPE, 0,
503 3299, NULL, 9, 0, errarg, NULL);
510 CloseHandle(hPipeRead);
515 /* service_nt_main_fn is outside of the call stack and outside of the
516 * primary server thread... so now we _really_ need a placeholder!
517 * The winnt_rewrite_args has created and shared mpm_new_argv with us.
519 extern apr_array_header_t *mpm_new_argv;
521 static void __stdcall service_nt_main_fn(DWORD argc, LPTSTR *argv)
524 HANDLE hCurrentProcess;
525 HANDLE hPipeRead = NULL;
526 HANDLE hPipeWrite = NULL;
530 SECURITY_ATTRIBUTES sa = {0};
532 /* args and service names live in the same pool */
533 mpm_service_set_name(mpm_new_argv->cont, argv[0]);
535 globdat.ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
536 globdat.ssStatus.dwCurrentState = SERVICE_START_PENDING;
537 globdat.ssStatus.dwServiceSpecificExitCode = 0;
538 globdat.ssStatus.dwCheckPoint = 1;
540 if (!(globdat.hServiceStatus = RegisterServiceCtrlHandler(argv[0], service_nt_ctrl)))
542 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(),
543 NULL, "Failure registering service handler");
544 PulseEvent(globdat.signal_monitor);
548 ReportStatusToSCMgr(globdat.ssStatus.dwCurrentState, // service state
549 NO_ERROR, // exit code
550 3000); // wait hint, 3 seconds more
552 /* Create a pipe to send stderr messages to the system error log */
553 hCurrentProcess = GetCurrentProcess();
554 if (CreatePipe(&hPipeRead, &hPipeWrite, &sa, 0))
556 if (DuplicateHandle(hCurrentProcess, hPipeRead, hCurrentProcess,
557 &hPipeReadDup, 0, FALSE, DUPLICATE_SAME_ACCESS))
559 CloseHandle(hPipeRead);
560 hPipeRead = hPipeReadDup;
561 thread = CreateThread(NULL, 0, service_stderr_thread,
562 (LPVOID) hPipeRead, 0, &threadid);
569 SetStdHandle(STD_ERROR_HANDLE, hPipeWrite);
571 fh = _open_osfhandle((long) STD_ERROR_HANDLE,
572 _O_WRONLY | _O_BINARY);
573 dup2(fh, STDERR_FILENO);
574 fl = _fdopen(STDERR_FILENO, "wcb");
575 memcpy(stderr, fl, sizeof(FILE));
579 CloseHandle(hPipeRead);
580 CloseHandle(hPipeWrite);
586 CloseHandle(hPipeRead);
587 CloseHandle(hPipeWrite);
592 /* We need to append all the command arguments passed via StartService()
593 * to our running service... which just got here via the SCM...
594 * but we hvae no interest in argv[0] for the mpm_new_argv list.
598 const char **cmb_data;
600 mpm_new_argv->nalloc = mpm_new_argv->nelts + argc - 1;
601 cmb_data = apr_palloc(mpm_new_argv->cont,
602 mpm_new_argv->nalloc * sizeof(const char *));
604 /* mpm_new_argv remains first (of lower significance) */
605 memcpy (cmb_data, mpm_new_argv->elts,
606 mpm_new_argv->elt_size * mpm_new_argv->nelts);
608 /* Service args follow from StartService() invocation */
609 memcpy (cmb_data + mpm_new_argv->nelts, argv + 1,
610 mpm_new_argv->elt_size * (argc - 1));
612 /* The replacement arg list is complete */
613 mpm_new_argv->elts = (char *)cmb_data;
614 mpm_new_argv->nelts = mpm_new_argv->nalloc;
617 /* Let the main thread continue now... but hang on to the
618 * signal_monitor event so we can take further action
620 SetEvent(globdat.signal_monitor);
622 waitfor[0] = globdat.signal_monitor;
623 waitfor[1] = globdat.mpm_thread;
624 WaitForMultipleObjects(2, waitfor, FALSE, INFINITE);
625 /* The process is ready to terminate, or already has */
627 CloseHandle(hPipeWrite);
631 DWORD WINAPI service_nt_dispatch_thread(LPVOID nada)
633 apr_status_t rv = APR_SUCCESS;
635 SERVICE_TABLE_ENTRY dispatchTable[] =
637 { "", service_nt_main_fn },
641 if (!StartServiceCtrlDispatcher(dispatchTable))
643 /* This is a genuine failure of the SCM. */
644 rv = apr_get_os_error();
645 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
646 "Error starting service control dispatcher");
649 globdat.service_thread_id = 0;
654 apr_status_t mpm_service_set_name(apr_pool_t *p, const char *name)
658 service_name = apr_palloc(p, strlen(name) + 1);
659 apr_collapse_spaces((char*) service_name, name);
660 key_name = apr_psprintf(p, SERVICECONFIG, service_name);
661 if (ap_registry_get_value(p, key_name, "DisplayName", (char**)&display_name) == APR_SUCCESS)
664 /* Take the given literal name if there is no service entry */
665 display_name = apr_pstrdup(p, name);
670 apr_status_t mpm_merge_service_args(apr_pool_t *p,
671 apr_array_header_t *args,
674 apr_array_header_t *svc_args = NULL;
675 char conf_key[MAX_PATH];
676 const char **cmb_data;
679 apr_snprintf(conf_key, sizeof(conf_key), SERVICEPARAMS, service_name);
680 rv = ap_registry_get_array(p, conf_key, "ConfigArgs", &svc_args);
681 if (rv != APR_SUCCESS) {
682 if (rv == ERROR_FILE_NOT_FOUND) {
683 ap_log_error(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, NULL,
684 "No ConfigArgs registered for %s, perhaps "
685 "this service is not installed?",
693 if (!svc_args || svc_args->nelts == 0) {
694 return (APR_SUCCESS);
697 /* Now we have the service_name arg, and the mpm_runservice_nt()
698 * call appended the arguments passed by StartService(), so it's
699 * time to _prepend_ the default arguments for the server from
700 * the service's default arguments (all others override them)...
702 args->nalloc = args->nelts + svc_args->nelts;
703 cmb_data = apr_palloc(p, args->nalloc * sizeof(const char *));
705 /* First three args (argv[0], -f, path) remain first */
706 memcpy(cmb_data, args->elts, args->elt_size * fixed_args);
708 /* Service args follow from service registry array */
709 memcpy(cmb_data + fixed_args, svc_args->elts,
710 svc_args->elt_size * svc_args->nelts);
712 /* Remaining new args follow */
713 memcpy(cmb_data + fixed_args + svc_args->nelts,
714 (const char **)args->elts + fixed_args,
715 args->elt_size * (args->nelts - fixed_args));
717 args->elts = (char *)cmb_data;
718 args->nelts = args->nalloc;
724 void service_stopped(void)
726 /* Still have a thread & window to clean up, so signal now */
727 if (globdat.service_thread_id)
729 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
731 ReportStatusToSCMgr(SERVICE_STOPPED, // service state
732 NO_ERROR, // exit code
735 /* Cause the service_nt_main_fn to complete */
736 SetEvent(globdat.signal_monitor);
738 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
740 RegisterServiceProcess(0, 0);
741 PostThreadMessage(globdat.service_thread_id, WM_CLOSE, 0, 0);
744 WaitForSingleObject(globdat.service_thread, 5000);
745 CloseHandle(globdat.service_thread);
750 apr_status_t mpm_service_to_start(void)
754 globdat.mpm_thread = GetCurrentThread();
756 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
758 globdat.signal_monitor = CreateEvent(NULL, FALSE, FALSE, NULL);
759 if (globdat.signal_monitor)
760 globdat.service_thread = CreateThread(NULL, 0,
761 service_nt_dispatch_thread,
763 &globdat.service_thread_id);
765 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
767 if (!RegisterServiceProcess(0, 1))
768 return GetLastError();
770 globdat.signal_monitor = CreateEvent(NULL, FALSE, FALSE, NULL);
771 if (globdat.signal_monitor)
772 globdat.service_thread = CreateThread(NULL, 0,
773 monitor_service_9x_thread,
774 (LPVOID) service_name, 0,
775 &globdat.service_thread_id);
778 if (globdat.signal_monitor && globdat.service_thread)
780 waitfor[0] = globdat.signal_monitor;
781 waitfor[1] = globdat.service_thread;
783 /* SetEvent(globdat.signal_monitor) to clean up the SCM thread */
784 if (WaitForMultipleObjects(2, waitfor, FALSE, 10000) != WAIT_OBJECT_0) {
785 CloseHandle(globdat.service_thread);
786 return APR_ENOTHREAD;
790 if (globdat.service_thread_id)
791 atexit(service_stopped);
792 else if (globdat.service_thread)
793 CloseHandle(globdat.service_thread);
799 apr_status_t mpm_service_started(void)
801 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
803 ReportStatusToSCMgr(SERVICE_RUNNING, // service state
804 NO_ERROR, // exit code
811 void mpm_service_stopping(void)
813 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
814 ReportStatusToSCMgr(SERVICE_STOP_PENDING, // service state
815 NO_ERROR, // exit code
820 apr_status_t mpm_service_install(apr_pool_t *ptemp, int argc,
821 const char * const * argv)
823 char key_name[MAX_PATH];
824 char exe_path[MAX_PATH];
828 printf("Installing the %s service\n", display_name);
830 if (GetModuleFileName(NULL, exe_path, sizeof(exe_path)) == 0)
832 apr_status_t rv = apr_get_os_error();
833 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
834 "GetModuleFileName failed");
838 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
840 SC_HANDLE schService;
841 SC_HANDLE schSCManager;
843 // TODO: Determine the minimum permissions required for security
844 schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
845 SC_MANAGER_ALL_ACCESS);
847 rv = apr_get_os_error();
848 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
849 "Failed to open the WinNT service manager");
853 launch_cmd = apr_psprintf(ptemp, "\"%s\" -k runservice", exe_path);
855 /* RPCSS is the Remote Procedure Call (RPC) Locator required for DCOM
856 * communication pipes. I am far from convinced we should add this to
857 * the default service dependencies, but be warned that future apache
858 * modules or ISAPI dll's may depend on it.
860 schService = CreateService(schSCManager, // SCManager database
861 service_name, // name of service
862 display_name, // name to display
863 SERVICE_ALL_ACCESS, // access required
864 SERVICE_WIN32_OWN_PROCESS, // service type
865 SERVICE_AUTO_START, // start type
866 SERVICE_ERROR_NORMAL, // error control type
867 launch_cmd, // service's binary
868 NULL, // no load svc group
869 NULL, // no tag identifier
870 "Tcpip\0Afd\0", // dependencies
871 NULL, // use SYSTEM account
872 NULL); // no password
876 rv = apr_get_os_error();
877 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
878 "Failed to create WinNT Service Profile");
879 CloseServiceHandle(schSCManager);
883 CloseServiceHandle(schService);
884 CloseServiceHandle(schSCManager);
886 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
888 /* Store the launch command in the registry */
889 launch_cmd = apr_psprintf(ptemp, "\"%s\" -n %s -k runservice",
890 exe_path, service_name);
891 rv = ap_registry_store_value(SERVICECONFIG9X, service_name, launch_cmd);
892 if (rv != APR_SUCCESS) {
893 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
894 "%s: Failed to add the RunServices registry entry.",
899 apr_snprintf(key_name, sizeof(key_name), SERVICECONFIG, service_name);
900 rv = ap_registry_store_value(key_name, "DisplayName", display_name);
901 if (rv != APR_SUCCESS) {
902 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
903 "%s: Failed to store DisplayName in the registry.",
909 /* For both WinNT & Win9x store the service ConfigArgs in the registry...
911 apr_snprintf(key_name, sizeof(key_name), SERVICEPARAMS, service_name);
912 rv = ap_registry_store_array(ptemp, key_name, "ConfigArgs", argc, argv);
913 if (rv != APR_SUCCESS) {
914 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
915 "%s: Failed to store the ConfigArgs in the registry.",
919 printf("The %s service is successfully installed.\n", display_name);
923 apr_status_t mpm_service_uninstall(void)
925 char key_name[MAX_PATH];
928 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
930 SC_HANDLE schService;
931 SC_HANDLE schSCManager;
933 printf("Removing the %s service\n", display_name);
935 // TODO: Determine the minimum permissions required for security
936 schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
937 SC_MANAGER_ALL_ACCESS);
939 rv = apr_get_os_error();
940 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
941 "Failed to open the WinNT service manager.");
945 schService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
948 rv = apr_get_os_error();
949 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
950 "%s: OpenService failed", display_name);
954 /* assure the service is stopped before continuing
956 * This may be out of order... we might not be able to be
957 * granted all access if the service is running anyway.
959 * And do we want to make it *this easy* for them
960 * to uninstall their service unintentionally?
962 // ap_stop_service(schService);
964 if (DeleteService(schService) == 0) {
965 rv = apr_get_os_error();
966 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
967 "%s: Failed to delete the service.", display_name);
971 CloseServiceHandle(schService);
972 CloseServiceHandle(schSCManager);
974 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
976 printf("Removing the %s service\n", display_name);
978 /* TODO: assure the service is stopped before continuing */
980 if (ap_registry_delete_value(SERVICECONFIG9X, service_name)) {
981 rv = apr_get_os_error();
982 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
983 "%s: Failed to remove the RunServices registry "
984 "entry.", display_name);
988 /* we blast Services/us, not just the Services/us/Parameters branch */
989 apr_snprintf(key_name, sizeof(key_name), SERVICECONFIG, service_name);
990 if (ap_registry_delete_key(key_name))
992 rv = apr_get_os_error();
993 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
994 "%s: Failed to remove the service config from the "
995 "registry.", display_name);
999 printf("The %s service has been removed successfully.\n", display_name);
1004 /* signal_service_transition is a simple thunk to signal the service
1005 * and monitor it's successful transition. If the signal passed is 0,
1006 * then the caller is assumed to already have performed some service
1007 * operation to be monitored (such as StartService), and no actual
1008 * ControlService signal is sent.
1011 static int signal_service_transition(SC_HANDLE schService, DWORD signal, DWORD pending, DWORD complete)
1013 if (signal && !ControlService(schService, signal, &globdat.ssStatus))
1018 if (!QueryServiceStatus(schService, &globdat.ssStatus))
1020 } while (globdat.ssStatus.dwCurrentState == pending);
1022 return (globdat.ssStatus.dwCurrentState == complete);
1026 apr_status_t mpm_service_start(apr_pool_t *ptemp, int argc,
1027 const char * const * argv)
1031 printf("Starting the %s service\n", display_name);
1033 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
1035 const char **start_argv;
1036 SC_HANDLE schService;
1037 SC_HANDLE schSCManager;
1039 // TODO: Determine the minimum permissions required for security
1040 schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
1041 SC_MANAGER_ALL_ACCESS);
1042 if (!schSCManager) {
1043 rv = apr_get_os_error();
1044 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
1045 "Failed to open the WinNT service manager");
1049 schService = OpenService(schSCManager, service_name,
1050 SERVICE_START | SERVICE_QUERY_STATUS);
1052 rv = apr_get_os_error();
1053 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
1054 "%s: Failed to open the service.", display_name);
1055 CloseServiceHandle(schSCManager);
1059 if (QueryServiceStatus(schService, &globdat.ssStatus)
1060 && (globdat.ssStatus.dwCurrentState == SERVICE_RUNNING)) {
1061 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL,
1062 "Service %s is already started!", display_name);
1063 CloseServiceHandle(schService);
1064 CloseServiceHandle(schSCManager);
1069 start_argv = apr_palloc(ptemp, argc * sizeof(const char **));
1070 start_argv[0] = service_name;
1072 memcpy(start_argv + 1, argv, (argc - 1) * sizeof(const char **));
1075 if (StartService(schService, argc, start_argv)
1076 && signal_service_transition(schService, 0, /* test only */
1077 SERVICE_START_PENDING,
1081 if (rv != APR_SUCCESS)
1082 rv = apr_get_os_error();
1084 CloseServiceHandle(schService);
1085 CloseServiceHandle(schSCManager);
1087 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
1089 STARTUPINFO si; /* Filled in prior to call to CreateProcess */
1090 PROCESS_INFORMATION pi; /* filled in on call to CreateProcess */
1091 char exe_path[MAX_PATH];
1095 /* Locate the active top level window named service_name
1096 * provided the class is ApacheWin95ServiceMonitor
1098 if (FindWindow("ApacheWin95ServiceMonitor", service_name)) {
1099 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL,
1100 "Service %s is already started!", display_name);
1104 /* This may not appear intuitive, but Win9x will not allow a process
1105 * to detach from the console without releasing the entire console.
1106 * Ergo, we must spawn a new process for the service to get back our
1108 * The config is pre-flighted, so there should be no danger of failure.
1111 if (GetModuleFileName(NULL, exe_path, sizeof(exe_path)) == 0)
1113 apr_status_t rv = apr_get_os_error();
1114 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
1115 "GetModuleFileName failed");
1119 pCommand = apr_psprintf(ptemp, "\"%s\" -n %s -k runservice",
1120 exe_path, service_name);
1121 for (i = 0; i < argc; ++i) {
1122 pCommand = apr_pstrcat(ptemp, pCommand,
1123 " \"", argv[i], "\"", NULL);
1126 memset(&si, 0, sizeof(si));
1127 memset(&pi, 0, sizeof(pi));
1129 si.dwFlags = STARTF_USESHOWWINDOW;
1130 si.wShowWindow = SW_HIDE; /* This might be redundant */
1133 if (CreateProcess(NULL, pCommand, NULL, NULL, FALSE,
1134 DETACHED_PROCESS, /* Creation flags */
1135 NULL, NULL, &si, &pi))
1138 while (GetExitCodeProcess(pi.hProcess, &code) == STILL_ACTIVE) {
1139 if (FindWindow("ApacheWin95ServiceMonitor", service_name)) {
1147 if (rv != APR_SUCCESS)
1148 rv = apr_get_os_error();
1150 CloseHandle(pi.hProcess);
1151 CloseHandle(pi.hThread);
1154 if (rv == APR_SUCCESS)
1155 printf("The %s service is running.\n", display_name);
1157 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
1158 "%s: Failed to start the service process.",
1165 /* signal is zero to stop, non-zero for restart */
1167 void mpm_signal_service(apr_pool_t *ptemp, int signal)
1169 int success = FALSE;
1171 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
1173 SC_HANDLE schService;
1174 SC_HANDLE schSCManager;
1176 schSCManager = OpenSCManager(NULL, NULL, // default machine & database
1177 SC_MANAGER_ALL_ACCESS);
1179 if (!schSCManager) {
1180 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL,
1181 "Failed to open the NT Service Manager");
1185 schService = OpenService(schSCManager, service_name,
1186 SERVICE_ALL_ACCESS);
1188 if (schService == NULL) {
1189 /* Could not open the service */
1190 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL,
1191 "Failed to open the %s Service", display_name);
1192 CloseServiceHandle(schSCManager);
1196 if (!QueryServiceStatus(schService, &globdat.ssStatus)) {
1197 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL,
1198 "Query of Service %s failed", display_name);
1199 CloseServiceHandle(schService);
1200 CloseServiceHandle(schSCManager);
1204 if (!signal && (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED)) {
1205 printf("The %s service is not started.\n", display_name);
1206 CloseServiceHandle(schService);
1207 CloseServiceHandle(schSCManager);
1211 printf("The %s service is %s.\n", display_name,
1212 signal ? "restarting" : "stopping");
1215 success = signal_service_transition(schService,
1216 SERVICE_CONTROL_STOP,
1217 SERVICE_STOP_PENDING,
1219 else if (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED) {
1220 mpm_service_start(ptemp, 0, NULL);
1221 CloseServiceHandle(schService);
1222 CloseServiceHandle(schSCManager);
1226 success = signal_service_transition(schService,
1227 SERVICE_APACHE_RESTART,
1228 SERVICE_START_PENDING,
1231 CloseServiceHandle(schService);
1232 CloseServiceHandle(schSCManager);
1234 else /* !isWindowsNT() */
1239 /* Locate the active top level window named service_name
1240 * provided the class is ApacheWin95ServiceMonitor
1242 hwnd = FindWindow("ApacheWin95ServiceMonitor", service_name);
1243 if (hwnd && GetWindowThreadProcessId(hwnd, &service_pid))
1244 globdat.ssStatus.dwCurrentState = SERVICE_RUNNING;
1247 globdat.ssStatus.dwCurrentState = SERVICE_STOPPED;
1249 printf("The %s service is not started.\n", display_name);
1254 printf("The %s service is %s.\n", display_name,
1255 signal ? "restarting" : "stopping");
1257 apr_snprintf(prefix, sizeof(prefix), "ap%ld", (long)service_pid);
1258 setup_signal_names(prefix);
1263 ap_start_shutdown();
1266 if (!IsWindow(hwnd)) {
1275 /* TODO: Aught to add a little test to the restart logic, and
1276 * store the restart counter in the window's user dword.
1277 * Then we can hang on and report a successful restart. But
1278 * that's a project for another day.
1280 if (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED) {
1281 mpm_service_start(ptemp, 0, NULL);
1286 ap_start_restart(1);
1292 printf("The %s service has %s.\n", display_name,
1293 signal ? "restarted" : "stopped");
1295 printf("Failed to %s the %s service.\n",
1296 signal ? "restart" : "stop", display_name);