1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2001 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...
69 #include "mpm_winnt.h"
70 #include "apr_strings.h"
79 static char *mpm_service_name = NULL;
80 static char *mpm_display_name = NULL;
84 HANDLE mpm_thread; /* primary thread handle of the apache server */
85 HANDLE service_thread; /* thread service/monitor handle */
86 DWORD service_thread_id;/* thread service/monitor ID */
87 HANDLE signal_monitor; /* service monitor thread signal event */
88 SERVICE_STATUS ssStatus;
89 SERVICE_STATUS_HANDLE hServiceStatus;
92 static int ReportStatusToSCMgr(int currentState, int exitCode, int waitHint);
94 /* The service configuration's is stored under the following trees:
96 * HKLM\System\CurrentControlSet\Services\[service name]
99 * \ImagePath (NT Only)
100 * \Parameters\ConfigArgs
102 * For Win9x, the launch service command is stored under:
104 * HKLM\Software\Microsoft\Windows\CurrentVersion\RunServices\[service name]
108 /* exit() for Win32 is macro mapped (horrible, we agree) that allows us
109 * to catch the non-zero conditions and inform the console process that
110 * the application died, and hang on to the console a bit longer.
112 * The macro only maps for http_main.c and other sources that include
113 * the service.h header, so we best assume it's an error to exit from
114 * _any_ other module.
116 * If real_exit_code is reset to 0, it will not be set or trigger this
117 * behavior on exit. All service and child processes are expected to
118 * reset this flag to zero to avoid undesireable side effects.
120 int real_exit_code = 1;
122 void hold_console_open_on_error(void)
129 char *msg = "Note the errors or messages above, "
130 "and press the <ESC> key to exit. ";
131 CONSOLE_SCREEN_BUFFER_INFO coninfo;
137 hConIn = GetStdHandle(STD_INPUT_HANDLE);
138 hConErr = GetStdHandle(STD_ERROR_HANDLE);
139 if ((hConIn == INVALID_HANDLE_VALUE) || (hConErr == INVALID_HANDLE_VALUE))
141 if (!WriteConsole(hConErr, msg, strlen(msg), &result, NULL) || !result)
143 if (!GetConsoleScreenBufferInfo(hConErr, &coninfo))
145 if (!SetConsoleMode(hConIn, ENABLE_MOUSE_INPUT | 0x80))
151 while (PeekConsoleInput(hConIn, &in, 1, &result) && result)
153 if (!ReadConsoleInput(hConIn, &in, 1, &result) || !result)
155 if ((in.EventType == KEY_EVENT) && in.Event.KeyEvent.bKeyDown
156 && (in.Event.KeyEvent.uChar.AsciiChar == 27))
158 if (in.EventType == MOUSE_EVENT
159 && (in.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK))
162 remains = ((start + 30) - time(NULL));
163 sprintf (count, "%d...", remains);
164 if (!SetConsoleCursorPosition(hConErr, coninfo.dwCursorPosition))
166 if (!WriteConsole(hConErr, count, strlen(count), &result, NULL)
170 while ((remains > 0) && WaitForSingleObject(hConIn, 1000) != WAIT_FAILED);
173 static BOOL die_on_logoff = FALSE;
175 static LRESULT CALLBACK monitor_service_9x_proc(HWND hWnd, UINT msg,
176 WPARAM wParam, LPARAM lParam)
178 /* This is the WndProc procedure for our invisible window.
179 * When the user shuts down the system, this window is sent
180 * a signal WM_ENDSESSION. We clean up by signaling Apache
181 * to shut down, and idle until Apache's primary thread quits.
183 if ((msg == WM_ENDSESSION)
184 && (die_on_logoff || (lParam != ENDSESSION_LOGOFF)))
188 /* Don't leave this message until we are dead! */
189 WaitForSingleObject(globdat.mpm_thread, 30000);
192 return (DefWindowProc(hWnd, msg, wParam, lParam));
195 static DWORD WINAPI monitor_service_9x_thread(void *service_name)
197 /* When running as a service under Windows 9x, there is no console
198 * window present, and no ConsoleCtrlHandler to call when the system
199 * is shutdown. If the WatchWindow thread is created with a NULL
200 * service_name argument, then the ...SystemMonitor window class is
201 * used to create the "Apache" window to watch for logoff and shutdown.
202 * If the service_name is provided, the ...ServiceMonitor window class
203 * is used to create the window named by the service_name argument,
204 * and the logoff message is ignored.
210 wc.style = CS_GLOBALCLASS;
211 wc.lpfnWndProc = monitor_service_9x_proc;
217 wc.hbrBackground = NULL;
218 wc.lpszMenuName = NULL;
220 wc.lpszClassName = "ApacheWin95ServiceMonitor";
222 wc.lpszClassName = "ApacheWin95SystemMonitor";
224 die_on_logoff = service_name ? FALSE : TRUE;
226 if (!RegisterClass(&wc))
228 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(),
229 NULL, "Could not register window class for WatchWindow");
230 SetEvent(globdat.signal_monitor);
231 globdat.service_thread_id = 0;
235 /* Create an invisible window */
236 hwndMain = CreateWindow(wc.lpszClassName,
237 service_name ? (char *) service_name : "Apache",
238 WS_OVERLAPPEDWINDOW & ~WS_VISIBLE,
239 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
240 CW_USEDEFAULT, NULL, NULL, NULL, NULL);
244 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(),
245 NULL, "Could not create WatchWindow");
246 SetEvent(globdat.signal_monitor);
247 globdat.service_thread_id = 0;
251 /* If we succeed, eliminate the console window.
252 * Signal the parent we are all set up, and
253 * watch the message queue while the window lives.
256 SetEvent((HANDLE) globdat.signal_monitor);
257 while (GetMessage(&msg, NULL, 0, 0))
259 if (msg.message == WM_CLOSE)
260 DestroyWindow(hwndMain);
262 TranslateMessage(&msg);
263 DispatchMessage(&msg);
266 globdat.service_thread_id = 0;
271 static BOOL CALLBACK console_control_handler(DWORD ctrl_type)
275 case CTRL_BREAK_EVENT:
276 fprintf(stderr, "Apache server restarting...\n");
280 fprintf(stderr, "Apache server interrupted...\n");
281 /* for Interrupt signals, shut down the server.
282 * Tell the system we have dealt with the signal
283 * without waiting for Apache to terminate.
288 case CTRL_CLOSE_EVENT:
289 case CTRL_LOGOFF_EVENT:
290 case CTRL_SHUTDOWN_EVENT:
291 /* for Terminate signals, shut down the server.
292 * Wait for Apache to terminate, but respond
293 * after a reasonable time to tell the system
294 * that we did attempt to shut ourself down.
295 * THESE EVENTS WILL NOT OCCUR UNDER WIN9x!
297 fprintf(stderr, "Apache server shutdown initiated...\n");
303 /* We should never get here, but this is (mostly) harmless */
308 static void stop_console_handler(void)
310 SetConsoleCtrlHandler(console_control_handler, FALSE);
314 void mpm_start_console_handler(void)
316 SetConsoleCtrlHandler(console_control_handler, TRUE);
317 atexit(stop_console_handler);
321 /* Special situation - children of services need to mind their
322 * P's & Q's and wait quietly, ignoring the mean OS signaling
323 * shutdown and other horrors, to kill them gracefully...
326 static BOOL CALLBACK child_control_handler(DWORD ctrl_type)
331 case CTRL_BREAK_EVENT:
332 /* for Interrupt signals, ignore them.
333 * The system will also signal the parent process,
334 * which will terminate Apache.
338 case CTRL_CLOSE_EVENT:
339 case CTRL_LOGOFF_EVENT:
340 case CTRL_SHUTDOWN_EVENT:
341 /* for Shutdown signals, ignore them, but... .
342 * The system will also signal the parent process,
343 * which will terminate Apache, so we need to wait.
349 /* We should never get here, but this is (mostly) harmless */
354 static void stop_child_console_handler(void)
356 SetConsoleCtrlHandler(child_control_handler, FALSE);
360 void mpm_start_child_console_handler(void)
362 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
367 SetConsoleCtrlHandler(child_control_handler, TRUE);
368 atexit(stop_child_console_handler);
373 /**********************************
374 WinNT service control management
375 **********************************/
377 static int ReportStatusToSCMgr(int currentState, int exitCode, int waitHint)
379 static int checkPoint = 1;
380 int rv = APR_SUCCESS;
382 if (globdat.hServiceStatus)
384 if (currentState == SERVICE_RUNNING)
385 globdat.ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
387 globdat.ssStatus.dwControlsAccepted = 0;
389 globdat.ssStatus.dwCurrentState = currentState;
390 globdat.ssStatus.dwWin32ExitCode = exitCode;
392 if ( ( currentState == SERVICE_RUNNING ) ||
393 ( currentState == SERVICE_STOPPED ) )
395 globdat.ssStatus.dwWaitHint = 0;
396 globdat.ssStatus.dwCheckPoint = 0;
401 globdat.ssStatus.dwWaitHint = waitHint;
402 globdat.ssStatus.dwCheckPoint = ++checkPoint;
404 rv = SetServiceStatus(globdat.hServiceStatus, &globdat.ssStatus);
410 /* handle the SCM's ControlService() callbacks to our service */
412 static VOID WINAPI service_nt_ctrl(DWORD dwCtrlCode)
414 if (dwCtrlCode == SERVICE_CONTROL_STOP)
417 globdat.ssStatus.dwCurrentState = SERVICE_STOP_PENDING;
418 ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 3000);
421 if (dwCtrlCode == SERVICE_APACHE_RESTART)
424 globdat.ssStatus.dwCurrentState = SERVICE_START_PENDING;
425 ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000);
429 ReportStatusToSCMgr(globdat.ssStatus.dwCurrentState, NO_ERROR, 0);
433 long __stdcall service_stderr_thread(LPVOID hPipe)
435 HANDLE hPipeRead = (HANDLE) hPipe;
438 char *errmsg = errbuf;
439 const char *errarg[9];
444 errarg[0] = "The Apache service named";
445 errarg[1] = mpm_display_name;
446 errarg[2] = "reported the following error:\r\n>>>";
448 errarg[4] = "<<<\r\n before the error.log file could be opened.\r\n";
449 errarg[5] = "More information may be available in the error.log file.";
454 /* What are we going to do in here, bail on the user? not. */
455 if (!RegCreateKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services"
456 "\\EventLog\\Application\\Apache Service", &hk))
458 /* The stock message file */
459 char *netmsgkey = "%SystemRoot%\\System32\\netmsg.dll";
460 DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
461 EVENTLOG_INFORMATION_TYPE;
463 RegSetValueEx(hk, "EventMessageFile", 0, REG_EXPAND_SZ,
464 (LPBYTE) netmsgkey, strlen(netmsgkey) + 1);
466 RegSetValueEx(hk, "TypesSupported", 0, REG_DWORD,
467 (LPBYTE) &dwData, sizeof(dwData));
471 hEventSource = RegisterEventSource(NULL, "Apache Service");
473 while (ReadFile(hPipeRead, errmsg, 1, &errres, NULL) && (errres == 1))
475 if ((errmsg > errbuf) || !isspace(*errmsg))
479 if ((*(errmsg - 1) == '\n') || (errlen == sizeof(errbuf) - 1))
481 while (errlen && isspace(errbuf[errlen - 1]))
483 errbuf[errlen] = '\0';
485 /* Generic message: '%1 %2 %3 %4 %5 %6 %7 %8 %9'
486 * The event code in netmsg.dll is 3299
488 ReportEvent(hEventSource, EVENTLOG_ERROR_TYPE, 0,
489 3299, NULL, 9, 0, errarg, NULL);
496 CloseHandle(hPipeRead);
501 /* service_nt_main_fn is outside of the call stack and outside of the
502 * primary server thread... so now we _really_ need a placeholder!
503 * The winnt_rewrite_args has created and shared mpm_new_argv with us.
505 extern apr_array_header_t *mpm_new_argv;
507 static void __stdcall service_nt_main_fn(DWORD argc, LPTSTR *argv)
510 HANDLE hCurrentProcess;
511 HANDLE hPipeRead = NULL;
512 HANDLE hPipeWrite = NULL;
516 SECURITY_ATTRIBUTES sa = {0};
519 /* args and service names live in the same pool */
520 mpm_service_set_name(mpm_new_argv->cont, &ignored, argv[0]);
522 globdat.ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
523 globdat.ssStatus.dwCurrentState = SERVICE_START_PENDING;
524 globdat.ssStatus.dwServiceSpecificExitCode = 0;
525 globdat.ssStatus.dwCheckPoint = 1;
527 if (!(globdat.hServiceStatus = RegisterServiceCtrlHandler(argv[0], service_nt_ctrl)))
529 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(),
530 NULL, "Failure registering service handler");
531 PulseEvent(globdat.signal_monitor);
535 ReportStatusToSCMgr(globdat.ssStatus.dwCurrentState, // service state
536 NO_ERROR, // exit code
537 3000); // wait hint, 3 seconds more
539 /* Create a pipe to send stderr messages to the system error log */
540 hCurrentProcess = GetCurrentProcess();
541 if (CreatePipe(&hPipeRead, &hPipeWrite, &sa, 0))
543 if (DuplicateHandle(hCurrentProcess, hPipeRead, hCurrentProcess,
544 &hPipeReadDup, 0, FALSE, DUPLICATE_SAME_ACCESS))
546 CloseHandle(hPipeRead);
547 hPipeRead = hPipeReadDup;
548 thread = CreateThread(NULL, 0, service_stderr_thread,
549 (LPVOID) hPipeRead, 0, &threadid);
556 SetStdHandle(STD_ERROR_HANDLE, hPipeWrite);
558 fh = _open_osfhandle((long) STD_ERROR_HANDLE,
559 _O_WRONLY | _O_BINARY);
560 dup2(fh, STDERR_FILENO);
561 fl = _fdopen(STDERR_FILENO, "wcb");
562 memcpy(stderr, fl, sizeof(FILE));
566 CloseHandle(hPipeRead);
567 CloseHandle(hPipeWrite);
573 CloseHandle(hPipeRead);
574 CloseHandle(hPipeWrite);
579 /* We need to append all the command arguments passed via StartService()
580 * to our running service... which just got here via the SCM...
581 * but we hvae no interest in argv[0] for the mpm_new_argv list.
587 mpm_new_argv->nalloc = mpm_new_argv->nelts + argc - 1;
588 cmb_data = apr_palloc(mpm_new_argv->cont,
589 mpm_new_argv->nalloc * sizeof(const char *));
591 /* mpm_new_argv remains first (of lower significance) */
592 memcpy (cmb_data, mpm_new_argv->elts,
593 mpm_new_argv->elt_size * mpm_new_argv->nelts);
595 /* Service args follow from StartService() invocation */
596 memcpy (cmb_data + mpm_new_argv->nelts, argv + 1,
597 mpm_new_argv->elt_size * (argc - 1));
599 /* The replacement arg list is complete */
600 mpm_new_argv->elts = (char *)cmb_data;
601 mpm_new_argv->nelts = mpm_new_argv->nalloc;
604 /* Let the main thread continue now... but hang on to the
605 * signal_monitor event so we can take further action
607 SetEvent(globdat.signal_monitor);
609 waitfor[0] = globdat.signal_monitor;
610 waitfor[1] = globdat.mpm_thread;
611 WaitForMultipleObjects(2, waitfor, FALSE, INFINITE);
612 /* The process is ready to terminate, or already has */
614 CloseHandle(hPipeWrite);
618 DWORD WINAPI service_nt_dispatch_thread(LPVOID nada)
620 apr_status_t rv = APR_SUCCESS;
622 SERVICE_TABLE_ENTRY dispatchTable[] =
624 { "", service_nt_main_fn },
628 if (!StartServiceCtrlDispatcher(dispatchTable))
630 /* This is a genuine failure of the SCM. */
631 rv = apr_get_os_error();
632 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
633 "Error starting service control dispatcher");
636 globdat.service_thread_id = 0;
641 apr_status_t mpm_service_set_name(apr_pool_t *p, const char **display_name,
642 const char *set_name)
647 /* ### Needs improvement, on Win2K the user can _easily_
648 * change the display name to a string that doesn't reflect
649 * the internal service name + whitespace!
651 mpm_service_name = apr_palloc(p, strlen(set_name) + 1);
652 apr_collapse_spaces((char*) mpm_service_name, set_name);
653 key_name = apr_psprintf(p, SERVICECONFIG, mpm_service_name);
654 rv = ap_registry_get_value(p, key_name, "DisplayName", &mpm_display_name);
655 if (rv != APR_SUCCESS) {
656 /* Take the given literal name if there is no service entry */
657 mpm_display_name = apr_pstrdup(p, set_name);
659 *display_name = mpm_display_name;
664 apr_status_t mpm_merge_service_args(apr_pool_t *p,
665 apr_array_header_t *args,
668 apr_array_header_t *svc_args = NULL;
669 char conf_key[MAX_PATH];
673 apr_snprintf(conf_key, sizeof(conf_key), SERVICEPARAMS, mpm_service_name);
674 rv = ap_registry_get_array(p, conf_key, "ConfigArgs", &svc_args);
675 if (rv != APR_SUCCESS) {
676 if (rv == ERROR_FILE_NOT_FOUND) {
677 ap_log_error(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, NULL,
678 "No ConfigArgs registered for %s, perhaps "
679 "this service is not installed?",
687 if (!svc_args || svc_args->nelts == 0) {
688 return (APR_SUCCESS);
691 /* Now we have the mpm_service_name arg, and the mpm_runservice_nt()
692 * call appended the arguments passed by StartService(), so it's
693 * time to _prepend_ the default arguments for the server from
694 * the service's default arguments (all others override them)...
696 args->nalloc = args->nelts + svc_args->nelts;
697 cmb_data = apr_palloc(p, args->nalloc * sizeof(const char *));
699 /* First three args (argv[0], -f, path) remain first */
700 memcpy(cmb_data, args->elts, args->elt_size * fixed_args);
702 /* Service args follow from service registry array */
703 memcpy(cmb_data + fixed_args, svc_args->elts,
704 svc_args->elt_size * svc_args->nelts);
706 /* Remaining new args follow */
707 memcpy(cmb_data + fixed_args + svc_args->nelts,
708 (const char **)args->elts + fixed_args,
709 args->elt_size * (args->nelts - fixed_args));
711 args->elts = (char *)cmb_data;
712 args->nelts = args->nalloc;
718 void service_stopped(void)
720 /* Still have a thread & window to clean up, so signal now */
721 if (globdat.service_thread_id)
723 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
725 ReportStatusToSCMgr(SERVICE_STOPPED, // service state
726 NO_ERROR, // exit code
729 /* Cause the service_nt_main_fn to complete */
730 SetEvent(globdat.signal_monitor);
732 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
734 RegisterServiceProcess(0, 0);
735 PostThreadMessage(globdat.service_thread_id, WM_CLOSE, 0, 0);
738 WaitForSingleObject(globdat.service_thread, 5000);
739 CloseHandle(globdat.service_thread);
744 apr_status_t mpm_service_to_start(const char **display_name)
748 globdat.mpm_thread = GetCurrentThread();
750 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
752 globdat.signal_monitor = CreateEvent(NULL, FALSE, FALSE, NULL);
753 if (globdat.signal_monitor)
754 globdat.service_thread = CreateThread(NULL, 0,
755 service_nt_dispatch_thread,
757 &globdat.service_thread_id);
759 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
761 if (!RegisterServiceProcess(0, 1))
762 return GetLastError();
764 globdat.signal_monitor = CreateEvent(NULL, FALSE, FALSE, NULL);
765 if (globdat.signal_monitor)
766 globdat.service_thread = CreateThread(NULL, 0,
767 monitor_service_9x_thread,
768 (LPVOID) mpm_service_name, 0,
769 &globdat.service_thread_id);
772 if (globdat.signal_monitor && globdat.service_thread)
774 waitfor[0] = globdat.signal_monitor;
775 waitfor[1] = globdat.service_thread;
777 /* SetEvent(globdat.signal_monitor) to clean up the SCM thread */
778 if (WaitForMultipleObjects(2, waitfor, FALSE, 10000) != WAIT_OBJECT_0) {
779 CloseHandle(globdat.service_thread);
780 return APR_ENOTHREAD;
784 if (globdat.service_thread_id)
785 atexit(service_stopped);
786 else if (globdat.service_thread)
787 CloseHandle(globdat.service_thread);
789 *display_name = mpm_display_name;
794 apr_status_t mpm_service_started(void)
796 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
798 ReportStatusToSCMgr(SERVICE_RUNNING, // service state
799 NO_ERROR, // exit code
806 void mpm_service_stopping(void)
808 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
809 ReportStatusToSCMgr(SERVICE_STOP_PENDING, // service state
810 NO_ERROR, // exit code
815 apr_status_t mpm_service_install(apr_pool_t *ptemp, int argc,
816 const char * const * argv)
818 char key_name[MAX_PATH];
819 char exe_path[MAX_PATH];
823 printf("Installing the %s service\n", mpm_display_name);
825 if (GetModuleFileName(NULL, exe_path, sizeof(exe_path)) == 0)
827 apr_status_t rv = apr_get_os_error();
828 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
829 "GetModuleFileName failed");
833 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
835 SC_HANDLE schService;
836 SC_HANDLE schSCManager;
838 // TODO: Determine the minimum permissions required for security
839 schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
840 SC_MANAGER_ALL_ACCESS);
842 rv = apr_get_os_error();
843 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
844 "Failed to open the WinNT service manager");
848 launch_cmd = apr_psprintf(ptemp, "\"%s\" -k runservice", exe_path);
850 /* RPCSS is the Remote Procedure Call (RPC) Locator required for DCOM
851 * communication pipes. I am far from convinced we should add this to
852 * the default service dependencies, but be warned that future apache
853 * modules or ISAPI dll's may depend on it.
855 schService = CreateService(schSCManager, // SCManager database
856 mpm_service_name, // name of service
857 mpm_display_name, // name to display
858 SERVICE_ALL_ACCESS, // access required
859 SERVICE_WIN32_OWN_PROCESS, // service type
860 SERVICE_AUTO_START, // start type
861 SERVICE_ERROR_NORMAL, // error control type
862 launch_cmd, // service's binary
863 NULL, // no load svc group
864 NULL, // no tag identifier
865 "Tcpip\0Afd\0", // dependencies
866 NULL, // use SYSTEM account
867 NULL); // no password
871 rv = apr_get_os_error();
872 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
873 "Failed to create WinNT Service Profile");
874 CloseServiceHandle(schSCManager);
878 CloseServiceHandle(schService);
879 CloseServiceHandle(schSCManager);
881 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
883 /* Store the launch command in the registry */
884 launch_cmd = apr_psprintf(ptemp, "\"%s\" -n %s -k runservice",
885 exe_path, mpm_service_name);
886 rv = ap_registry_store_value(SERVICECONFIG9X, mpm_service_name, launch_cmd);
887 if (rv != APR_SUCCESS) {
888 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
889 "%s: Failed to add the RunServices registry entry.",
894 apr_snprintf(key_name, sizeof(key_name), SERVICECONFIG, mpm_service_name);
895 rv = ap_registry_store_value(key_name, "DisplayName", mpm_display_name);
896 if (rv != APR_SUCCESS) {
897 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
898 "%s: Failed to store DisplayName in the registry.",
904 /* For both WinNT & Win9x store the service ConfigArgs in the registry...
906 apr_snprintf(key_name, sizeof(key_name), SERVICEPARAMS, mpm_service_name);
907 rv = ap_registry_store_array(ptemp, key_name, "ConfigArgs", argc, argv);
908 if (rv != APR_SUCCESS) {
909 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
910 "%s: Failed to store the ConfigArgs in the registry.",
914 printf("The %s service is successfully installed.\n", mpm_display_name);
919 apr_status_t mpm_service_uninstall(void)
921 char key_name[MAX_PATH];
924 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
926 SC_HANDLE schService;
927 SC_HANDLE schSCManager;
929 printf("Removing the %s service\n", mpm_display_name);
931 // TODO: Determine the minimum permissions required for security
932 schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
933 SC_MANAGER_ALL_ACCESS);
935 rv = apr_get_os_error();
936 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
937 "Failed to open the WinNT service manager.");
941 schService = OpenService(schSCManager, mpm_service_name, SERVICE_ALL_ACCESS);
944 rv = apr_get_os_error();
945 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
946 "%s: OpenService failed", mpm_display_name);
950 /* assure the service is stopped before continuing
952 * This may be out of order... we might not be able to be
953 * granted all access if the service is running anyway.
955 * And do we want to make it *this easy* for them
956 * to uninstall their service unintentionally?
958 // ap_stop_service(schService);
960 if (DeleteService(schService) == 0) {
961 rv = apr_get_os_error();
962 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
963 "%s: Failed to delete the service.", mpm_display_name);
967 CloseServiceHandle(schService);
968 CloseServiceHandle(schSCManager);
970 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
972 printf("Removing the %s service\n", mpm_display_name);
974 /* TODO: assure the service is stopped before continuing */
976 if (ap_registry_delete_value(SERVICECONFIG9X, mpm_service_name)) {
977 rv = apr_get_os_error();
978 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
979 "%s: Failed to remove the RunServices registry "
980 "entry.", mpm_display_name);
984 /* we blast Services/us, not just the Services/us/Parameters branch */
985 apr_snprintf(key_name, sizeof(key_name), SERVICECONFIG, mpm_service_name);
986 if (ap_registry_delete_key(key_name))
988 rv = apr_get_os_error();
989 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
990 "%s: Failed to remove the service config from the "
991 "registry.", mpm_display_name);
995 printf("The %s service has been removed successfully.\n", mpm_display_name);
1000 /* signal_service_transition is a simple thunk to signal the service
1001 * and monitor it's successful transition. If the signal passed is 0,
1002 * then the caller is assumed to already have performed some service
1003 * operation to be monitored (such as StartService), and no actual
1004 * ControlService signal is sent.
1007 static int signal_service_transition(SC_HANDLE schService, DWORD signal, DWORD pending, DWORD complete)
1009 if (signal && !ControlService(schService, signal, &globdat.ssStatus))
1014 if (!QueryServiceStatus(schService, &globdat.ssStatus))
1016 } while (globdat.ssStatus.dwCurrentState == pending);
1018 return (globdat.ssStatus.dwCurrentState == complete);
1022 apr_status_t mpm_service_start(apr_pool_t *ptemp, int argc,
1023 const char * const * argv)
1027 printf("Starting the %s service\n", mpm_display_name);
1029 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
1032 SC_HANDLE schService;
1033 SC_HANDLE schSCManager;
1035 // TODO: Determine the minimum permissions required for security
1036 schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
1037 SC_MANAGER_ALL_ACCESS);
1038 if (!schSCManager) {
1039 rv = apr_get_os_error();
1040 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
1041 "Failed to open the WinNT service manager");
1045 schService = OpenService(schSCManager, mpm_service_name,
1046 SERVICE_START | SERVICE_QUERY_STATUS);
1048 rv = apr_get_os_error();
1049 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
1050 "%s: Failed to open the service.", mpm_display_name);
1051 CloseServiceHandle(schSCManager);
1055 if (QueryServiceStatus(schService, &globdat.ssStatus)
1056 && (globdat.ssStatus.dwCurrentState == SERVICE_RUNNING)) {
1057 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL,
1058 "Service %s is already started!", mpm_display_name);
1059 CloseServiceHandle(schService);
1060 CloseServiceHandle(schSCManager);
1065 start_argv = apr_palloc(ptemp, argc * sizeof(const char **));
1066 start_argv[0] = mpm_service_name;
1068 memcpy(start_argv + 1, argv, (argc - 1) * sizeof(const char **));
1071 if (StartService(schService, argc, start_argv)
1072 && signal_service_transition(schService, 0, /* test only */
1073 SERVICE_START_PENDING,
1077 if (rv != APR_SUCCESS)
1078 rv = apr_get_os_error();
1080 CloseServiceHandle(schService);
1081 CloseServiceHandle(schSCManager);
1083 else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
1085 STARTUPINFO si; /* Filled in prior to call to CreateProcess */
1086 PROCESS_INFORMATION pi; /* filled in on call to CreateProcess */
1087 char exe_path[MAX_PATH];
1091 /* Locate the active top level window named service_name
1092 * provided the class is ApacheWin95ServiceMonitor
1094 if (FindWindow("ApacheWin95ServiceMonitor", mpm_service_name)) {
1095 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL,
1096 "Service %s is already started!", mpm_display_name);
1100 /* This may not appear intuitive, but Win9x will not allow a process
1101 * to detach from the console without releasing the entire console.
1102 * Ergo, we must spawn a new process for the service to get back our
1104 * The config is pre-flighted, so there should be no danger of failure.
1107 if (GetModuleFileName(NULL, exe_path, sizeof(exe_path)) == 0)
1109 apr_status_t rv = apr_get_os_error();
1110 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
1111 "GetModuleFileName failed");
1115 pCommand = apr_psprintf(ptemp, "\"%s\" -n %s -k runservice",
1116 exe_path, mpm_service_name);
1117 for (i = 0; i < argc; ++i) {
1118 pCommand = apr_pstrcat(ptemp, pCommand,
1119 " \"", argv[i], "\"", NULL);
1122 memset(&si, 0, sizeof(si));
1123 memset(&pi, 0, sizeof(pi));
1125 si.dwFlags = STARTF_USESHOWWINDOW;
1126 si.wShowWindow = SW_HIDE; /* This might be redundant */
1129 if (CreateProcess(NULL, pCommand, NULL, NULL, FALSE,
1130 DETACHED_PROCESS, /* Creation flags */
1131 NULL, NULL, &si, &pi))
1134 while (GetExitCodeProcess(pi.hProcess, &code) == STILL_ACTIVE) {
1135 if (FindWindow("ApacheWin95ServiceMonitor", mpm_service_name)) {
1143 if (rv != APR_SUCCESS)
1144 rv = apr_get_os_error();
1146 CloseHandle(pi.hProcess);
1147 CloseHandle(pi.hThread);
1150 if (rv == APR_SUCCESS)
1151 printf("The %s service is running.\n", mpm_display_name);
1153 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
1154 "%s: Failed to start the service process.",
1161 /* signal is zero to stop, non-zero for restart */
1163 void mpm_signal_service(apr_pool_t *ptemp, int signal)
1165 int success = FALSE;
1167 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
1169 SC_HANDLE schService;
1170 SC_HANDLE schSCManager;
1172 schSCManager = OpenSCManager(NULL, NULL, // default machine & database
1173 SC_MANAGER_ALL_ACCESS);
1175 if (!schSCManager) {
1176 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL,
1177 "Failed to open the NT Service Manager");
1181 schService = OpenService(schSCManager, mpm_service_name,
1182 SERVICE_ALL_ACCESS);
1184 if (schService == NULL) {
1185 /* Could not open the service */
1186 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL,
1187 "Failed to open the %s Service", mpm_display_name);
1188 CloseServiceHandle(schSCManager);
1192 if (!QueryServiceStatus(schService, &globdat.ssStatus)) {
1193 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL,
1194 "Query of Service %s failed", mpm_display_name);
1195 CloseServiceHandle(schService);
1196 CloseServiceHandle(schSCManager);
1200 if (!signal && (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED)) {
1201 printf("The %s service is not started.\n", mpm_display_name);
1202 CloseServiceHandle(schService);
1203 CloseServiceHandle(schSCManager);
1207 printf("The %s service is %s.\n", mpm_display_name,
1208 signal ? "restarting" : "stopping");
1211 success = signal_service_transition(schService,
1212 SERVICE_CONTROL_STOP,
1213 SERVICE_STOP_PENDING,
1215 else if (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED) {
1216 mpm_service_start(ptemp, 0, NULL);
1217 CloseServiceHandle(schService);
1218 CloseServiceHandle(schSCManager);
1222 success = signal_service_transition(schService,
1223 SERVICE_APACHE_RESTART,
1224 SERVICE_START_PENDING,
1227 CloseServiceHandle(schService);
1228 CloseServiceHandle(schSCManager);
1230 else /* !isWindowsNT() */
1235 /* Locate the active top level window named service_name
1236 * provided the class is ApacheWin95ServiceMonitor
1238 hwnd = FindWindow("ApacheWin95ServiceMonitor", mpm_service_name);
1239 if (hwnd && GetWindowThreadProcessId(hwnd, &service_pid))
1240 globdat.ssStatus.dwCurrentState = SERVICE_RUNNING;
1243 globdat.ssStatus.dwCurrentState = SERVICE_STOPPED;
1245 printf("The %s service is not started.\n", mpm_display_name);
1250 printf("The %s service is %s.\n", mpm_display_name,
1251 signal ? "restarting" : "stopping");
1253 apr_snprintf(prefix, sizeof(prefix), "ap%ld", (long)service_pid);
1254 setup_signal_names(prefix);
1259 ap_start_shutdown();
1262 if (!IsWindow(hwnd)) {
1271 /* TODO: Aught to add a little test to the restart logic, and
1272 * store the restart counter in the window's user dword.
1273 * Then we can hang on and report a successful restart. But
1274 * that's a project for another day.
1276 if (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED) {
1277 mpm_service_start(ptemp, 0, NULL);
1282 ap_start_restart(1);
1288 printf("The %s service has %s.\n", mpm_display_name,
1289 signal ? "restarted" : "stopped");
1291 printf("Failed to %s the %s service.\n",
1292 signal ? "restart" : "stop", mpm_display_name);