1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* This module ALONE requires the window message API from user.h
* and the default APR include of windows.h will omit it, so
* preload the API symbols now...
*/
#define _WINUSER_
#include "apr.h"
#include "apr_strings.h"
#include "apr_lib.h"
#if APR_HAS_UNICODE_FS
#include "arch/win32/apr_arch_utf8.h"
#include "arch/win32/apr_arch_misc.h"
#include <wchar.h>
#endif
#include "httpd.h"
#include "http_log.h"
#include "mpm_winnt.h"
#include "ap_regkey.h"
#ifdef NOUSER
#undef NOUSER
#endif
#undef _WINUSER_
#include <winuser.h>
#include <time.h>
APLOG_USE_MODULE(mpm_winnt);
/* Todo; clear up statics */
static char *mpm_service_name = NULL;
static char *mpm_display_name = NULL;
#if APR_HAS_UNICODE_FS
static apr_wchar_t *mpm_service_name_w;
#endif
typedef struct nt_service_ctx_t
{
HANDLE mpm_thread; /* primary thread handle of the apache server */
HANDLE service_thread; /* thread service/monitor handle */
DWORD service_thread_id;/* thread service/monitor ID */
HANDLE service_init; /* controller thread init mutex */
HANDLE service_term; /* NT service thread kill signal */
SERVICE_STATUS ssStatus;
SERVICE_STATUS_HANDLE hServiceStatus;
} nt_service_ctx_t;
static nt_service_ctx_t globdat;
static int ReportStatusToSCMgr(int currentState, int waitHint,
nt_service_ctx_t *ctx);
/* Rather than repeat this logic throughout, create an either-or wide or narrow
* implementation because we don't actually pass strings to OpenSCManager.
* This election is based on build time defines and runtime os version test.
*/
#undef OpenSCManager
typedef SC_HANDLE (WINAPI *fpt_OpenSCManager)(const void *lpMachine,
const void *lpDatabase,
DWORD dwAccess);
static fpt_OpenSCManager pfn_OpenSCManager = NULL;
static APR_INLINE SC_HANDLE OpenSCManager(const void *lpMachine,
const void *lpDatabase,
DWORD dwAccess)
{
if (!pfn_OpenSCManager) {
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
pfn_OpenSCManager = (fpt_OpenSCManager)OpenSCManagerW;
#endif
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
pfn_OpenSCManager = (fpt_OpenSCManager)OpenSCManagerA;
#endif
}
return (*(pfn_OpenSCManager))(lpMachine, lpDatabase, dwAccess);
}
/* exit() for Win32 is macro mapped (horrible, we agree) that allows us
* to catch the non-zero conditions and inform the console process that
* the application died, and hang on to the console a bit longer.
*
* The macro only maps for http_main.c and other sources that include
* the service.h header, so we best assume it's an error to exit from
* _any_ other module.
*
* If ap_real_exit_code is reset to 0, it will not be set or trigger this
* behavior on exit. All service and child processes are expected to
* reset this flag to zero to avoid undesirable side effects.
*/
AP_DECLARE_DATA int ap_real_exit_code = 1;
void hold_console_open_on_error(void)
{
HANDLE hConIn;
HANDLE hConErr;
DWORD result;
time_t start;
time_t remains;
char *msg = "Note the errors or messages above, "
"and press the <ESC> key to exit. ";
CONSOLE_SCREEN_BUFFER_INFO coninfo;
INPUT_RECORD in;
char count[16];
if (!ap_real_exit_code)
return;
hConIn = GetStdHandle(STD_INPUT_HANDLE);
hConErr = GetStdHandle(STD_ERROR_HANDLE);
if ((hConIn == INVALID_HANDLE_VALUE) || (hConErr == INVALID_HANDLE_VALUE))
return;
if (!WriteConsole(hConErr, msg, (DWORD)strlen(msg), &result, NULL)
|| !result)
return;
if (!GetConsoleScreenBufferInfo(hConErr, &coninfo))
return;
if (!SetConsoleMode(hConIn, ENABLE_MOUSE_INPUT | 0x80))
return;
start = time(NULL);
do
{
while (PeekConsoleInput(hConIn, &in, 1, &result) && result)
{
if (!ReadConsoleInput(hConIn, &in, 1, &result) || !result)
return;
if ((in.EventType == KEY_EVENT) && in.Event.KeyEvent.bKeyDown
&& (in.Event.KeyEvent.uChar.AsciiChar == 27))
return;
if (in.EventType == MOUSE_EVENT
&& (in.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK))
return;
}
remains = ((start + 30) - time(NULL));
sprintf(count, "%d...",
(int)remains); /* 30 or less, so can't overflow int */
if (!SetConsoleCursorPosition(hConErr, coninfo.dwCursorPosition))
return;
if (!WriteConsole(hConErr, count, (DWORD)strlen(count), &result, NULL)
|| !result)
return;
}
while ((remains > 0) && WaitForSingleObject(hConIn, 1000) != WAIT_FAILED);
}
static BOOL CALLBACK console_control_handler(DWORD ctrl_type)
{
switch (ctrl_type)
{
case CTRL_BREAK_EVENT:
fprintf(stderr, "Apache server restarting...\n");
ap_signal_parent(SIGNAL_PARENT_RESTART);
return TRUE;
case CTRL_C_EVENT:
fprintf(stderr, "Apache server interrupted...\n");
/* for Interrupt signals, shut down the server.
* Tell the system we have dealt with the signal
* without waiting for Apache to terminate.
*/
ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
return TRUE;
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
/* for Terminate signals, shut down the server.
* Wait for Apache to terminate, but respond
* after a reasonable time to tell the system
* that we did attempt to shut ourself down.
*/
fprintf(stderr, "Apache server shutdown initiated...\n");
ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
Sleep(30000);
return TRUE;
}
/* We should never get here, but this is (mostly) harmless */
return FALSE;
}
static void stop_console_handler(void)
{
SetConsoleCtrlHandler(console_control_handler, FALSE);
}
void mpm_start_console_handler(void)
{
SetConsoleCtrlHandler(console_control_handler, TRUE);
atexit(stop_console_handler);
}
void mpm_start_child_console_handler(void)
{
FreeConsole();
}
/**********************************
WinNT service control management
**********************************/
static int ReportStatusToSCMgr(int currentState, int waitHint,
nt_service_ctx_t *ctx)
{
int rv = APR_SUCCESS;
if (ctx->hServiceStatus)
{
if (currentState == SERVICE_RUNNING) {
ctx->ssStatus.dwWaitHint = 0;
ctx->ssStatus.dwCheckPoint = 0;
ctx->ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN;
}
else if (currentState == SERVICE_STOPPED) {
ctx->ssStatus.dwWaitHint = 0;
ctx->ssStatus.dwCheckPoint = 0;
/* An unexpected exit? Better to error! */
if (ctx->ssStatus.dwCurrentState != SERVICE_STOP_PENDING
&& !ctx->ssStatus.dwServiceSpecificExitCode)
ctx->ssStatus.dwServiceSpecificExitCode = 1;
if (ctx->ssStatus.dwServiceSpecificExitCode)
ctx->ssStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
}
else {
++ctx->ssStatus.dwCheckPoint;
ctx->ssStatus.dwControlsAccepted = 0;
if(waitHint)
ctx->ssStatus.dwWaitHint = waitHint;
}
ctx->ssStatus.dwCurrentState = currentState;
rv = SetServiceStatus(ctx->hServiceStatus, &ctx->ssStatus);
}
return(rv);
}
/* Note this works on Win2000 and later due to ChangeServiceConfig2
* Continue to test its existence, but at least drop the feature
* of revising service description tags prior to Win2000.
*/
/* borrowed from mpm_winnt.c */
extern apr_pool_t *pconf;
static void set_service_description(void)
{
const char *full_description;
SC_HANDLE schSCManager;
/* Nothing to do if we are a console
*/
if (!mpm_service_name)
return;
/* Time to fix up the description, upon each successful restart
*/
full_description = ap_get_server_description();
if ((ChangeServiceConfig2) &&
(schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT)))
{
SC_HANDLE schService;
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
schService = OpenServiceW(schSCManager,
(LPCWSTR)mpm_service_name_w,
SERVICE_CHANGE_CONFIG);
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
schService = OpenService(schSCManager, mpm_service_name,
SERVICE_CHANGE_CONFIG);
}
#endif
if (schService) {
/* Cast is necessary, ChangeServiceConfig2 handles multiple
* object types, some volatile, some not.
*/
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
apr_size_t slen = strlen(full_description) + 1;
apr_size_t wslen = slen;
apr_wchar_t *full_description_w =
(apr_wchar_t*)apr_palloc(pconf,
wslen * sizeof(apr_wchar_t));
apr_status_t rv = apr_conv_utf8_to_ucs2(full_description, &slen,
full_description_w,
&wslen);
if ((rv != APR_SUCCESS) || slen
|| ChangeServiceConfig2W(schService, 1
/*SERVICE_CONFIG_DESCRIPTION*/,
(LPVOID) &full_description_w))
full_description = NULL;
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
if (ChangeServiceConfig2(schService,
1 /* SERVICE_CONFIG_DESCRIPTION */,
(LPVOID) &full_description))
full_description = NULL;
}
#endif
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}
/* handle the SCM's ControlService() callbacks to our service */
static DWORD WINAPI service_nt_ctrl(DWORD dwCtrlCode, DWORD dwEventType,
LPVOID lpEventData, LPVOID lpContext)
{
nt_service_ctx_t *ctx = lpContext;
/* SHUTDOWN is offered before STOP, accept the first opportunity */
if ((dwCtrlCode == SERVICE_CONTROL_STOP)
|| (dwCtrlCode == SERVICE_CONTROL_SHUTDOWN))
{
ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
ReportStatusToSCMgr(SERVICE_STOP_PENDING, 30000, ctx);
return (NO_ERROR);
}
if (dwCtrlCode == SERVICE_APACHE_RESTART)
{
ap_signal_parent(SIGNAL_PARENT_RESTART);
ReportStatusToSCMgr(SERVICE_START_PENDING, 30000, ctx);
return (NO_ERROR);
}
if (dwCtrlCode == SERVICE_CONTROL_INTERROGATE) {
ReportStatusToSCMgr(globdat.ssStatus.dwCurrentState, 0, ctx);
return (NO_ERROR);
}
return (ERROR_CALL_NOT_IMPLEMENTED);
}
/* service_nt_main_fn is outside of the call stack and outside of the
* primary server thread... so now we _really_ need a placeholder!
* The winnt_rewrite_args has created and shared mpm_new_argv with us.
*/
extern apr_array_header_t *mpm_new_argv;
#if APR_HAS_UNICODE_FS
static void __stdcall service_nt_main_fn_w(DWORD argc, LPWSTR *argv)
{
const char *ignored;
nt_service_ctx_t *ctx = &globdat;
char *service_name;
apr_size_t wslen = wcslen(argv[0]) + 1;
apr_size_t slen = wslen * 3 - 2;
service_name = malloc(slen);
(void)apr_conv_ucs2_to_utf8(argv[0], &wslen, service_name, &slen);
/* args and service names live in the same pool */
mpm_service_set_name(mpm_new_argv->pool, &ignored, service_name);
memset(&ctx->ssStatus, 0, sizeof(ctx->ssStatus));
ctx->ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ctx->ssStatus.dwCurrentState = SERVICE_START_PENDING;
ctx->ssStatus.dwCheckPoint = 1;
if (!(ctx->hServiceStatus =
RegisterServiceCtrlHandlerExW(argv[0], service_nt_ctrl, ctx)))
{
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(00365) "Failure registering service handler");
return;
}
/* Report status, no errors, and buy 3 more seconds */
ReportStatusToSCMgr(SERVICE_START_PENDING, 30000, ctx);
/* We need to append all the command arguments passed via StartService()
* to our running service... which just got here via the SCM...
* but we have no interest in argv[0] for the mpm_new_argv list.
*/
if (argc > 1)
{
char **cmb_data, **cmb;
DWORD i;
mpm_new_argv->nalloc = mpm_new_argv->nelts + argc - 1;
cmb_data = malloc(mpm_new_argv->nalloc * sizeof(const char *));
/* mpm_new_argv remains first (of lower significance) */
memcpy (cmb_data, mpm_new_argv->elts,
mpm_new_argv->elt_size * mpm_new_argv->nelts);
/* Service args follow from StartService() invocation */
memcpy (cmb_data + mpm_new_argv->nelts, argv + 1,
mpm_new_argv->elt_size * (argc - 1));
cmb = cmb_data + mpm_new_argv->nelts;
for (i = 1; i < argc; ++i)
{
wslen = wcslen(argv[i]) + 1;
slen = wslen * 3 - 2;
service_name = malloc(slen);
(void)apr_conv_ucs2_to_utf8(argv[i], &wslen, *(cmb++), &slen);
}
/* The replacement arg list is complete */
mpm_new_argv->elts = (char *)cmb_data;
mpm_new_argv->nelts = mpm_new_argv->nalloc;
}
/* Let the main thread continue now... but hang on to the
* signal_monitor event so we can take further action
*/
SetEvent(ctx->service_init);
WaitForSingleObject(ctx->service_term, INFINITE);
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
static void __stdcall service_nt_main_fn(DWORD argc, LPSTR *argv)
{
const char *ignored;
nt_service_ctx_t *ctx = &globdat;
/* args and service names live in the same pool */
mpm_service_set_name(mpm_new_argv->pool, &ignored, argv[0]);
memset(&ctx->ssStatus, 0, sizeof(ctx->ssStatus));
ctx->ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ctx->ssStatus.dwCurrentState = SERVICE_START_PENDING;
ctx->ssStatus.dwCheckPoint = 1;
if (!(ctx->hServiceStatus =
RegisterServiceCtrlHandlerExA(argv[0], service_nt_ctrl, ctx)))
{
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(10008) "Failure registering service handler");
return;
}
/* Report status, no errors, and buy 3 more seconds */
ReportStatusToSCMgr(SERVICE_START_PENDING, 30000, ctx);
/* We need to append all the command arguments passed via StartService()
* to our running service... which just got here via the SCM...
* but we have no interest in argv[0] for the mpm_new_argv list.
*/
if (argc > 1)
{
char **cmb_data;
mpm_new_argv->nalloc = mpm_new_argv->nelts + argc - 1;
cmb_data = malloc(mpm_new_argv->nalloc * sizeof(const char *));
/* mpm_new_argv remains first (of lower significance) */
memcpy (cmb_data, mpm_new_argv->elts,
mpm_new_argv->elt_size * mpm_new_argv->nelts);
/* Service args follow from StartService() invocation */
memcpy (cmb_data + mpm_new_argv->nelts, argv + 1,
mpm_new_argv->elt_size * (argc - 1));
/* The replacement arg list is complete */
mpm_new_argv->elts = (char *)cmb_data;
mpm_new_argv->nelts = mpm_new_argv->nalloc;
}
/* Let the main thread continue now... but hang on to the
* signal_monitor event so we can take further action
*/
SetEvent(ctx->service_init);
WaitForSingleObject(ctx->service_term, INFINITE);
}
#endif
static DWORD WINAPI service_nt_dispatch_thread(LPVOID nada)
{
#if APR_HAS_UNICODE_FS
SERVICE_TABLE_ENTRYW dispatchTable_w[] =
{
{ L"", service_nt_main_fn_w },
{ NULL, NULL }
};
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
SERVICE_TABLE_ENTRYA dispatchTable[] =
{
{ "", service_nt_main_fn },
{ NULL, NULL }
};
#endif
apr_status_t rv;
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
rv = StartServiceCtrlDispatcherW(dispatchTable_w);
#endif
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
rv = StartServiceCtrlDispatcherA(dispatchTable);
#endif
if (rv) {
rv = APR_SUCCESS;
}
else {
/* This is a genuine failure of the SCM. */
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00366) "Error starting Windows service control "
"dispatcher");
}
return (rv);
}
/* The service configuration's is stored under the following trees:
*
* HKLM\System\CurrentControlSet\Services\[service name]
*
* \DisplayName
* \ImagePath
* \Parameters\ConfigArgs
*/
apr_status_t mpm_service_set_name(apr_pool_t *p, const char **display_name,
const char *set_name)
{
char key_name[MAX_PATH];
ap_regkey_t *key;
apr_status_t rv;
/* ### Needs improvement, on Win2K the user can _easily_
* change the display name to a string that doesn't reflect
* the internal service name + whitespace!
*/
mpm_service_name = apr_palloc(p, strlen(set_name) + 1);
apr_collapse_spaces((char*) mpm_service_name, set_name);
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
apr_size_t slen = strlen(mpm_service_name) + 1;
apr_size_t wslen = slen;
mpm_service_name_w = apr_palloc(p, wslen * sizeof(apr_wchar_t));
rv = apr_conv_utf8_to_ucs2(mpm_service_name, &slen,
mpm_service_name_w, &wslen);
if (rv != APR_SUCCESS)
return rv;
else if (slen)
return APR_ENAMETOOLONG;
}
#endif /* APR_HAS_UNICODE_FS */
apr_snprintf(key_name, sizeof(key_name), SERVICECONFIG, mpm_service_name);
rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, key_name,
APR_READ, pconf);
if (rv == APR_SUCCESS) {
rv = ap_regkey_value_get(&mpm_display_name, key, "DisplayName", pconf);
ap_regkey_close(key);
}
if (rv != APR_SUCCESS) {
/* Take the given literal name if there is no service entry */
mpm_display_name = apr_pstrdup(p, set_name);
}
*display_name = mpm_display_name;
return rv;
}
apr_status_t mpm_merge_service_args(apr_pool_t *p,
apr_array_header_t *args,
int fixed_args)
{
apr_array_header_t *svc_args = NULL;
char conf_key[MAX_PATH];
char **cmb_data;
apr_status_t rv;
ap_regkey_t *key;
apr_snprintf(conf_key, sizeof(conf_key), SERVICEPARAMS, mpm_service_name);
rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, conf_key, APR_READ, p);
if (rv == APR_SUCCESS) {
rv = ap_regkey_value_array_get(&svc_args, key, "ConfigArgs", p);
ap_regkey_close(key);
}
if (rv != APR_SUCCESS) {
if (rv == ERROR_FILE_NOT_FOUND) {
ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL, APLOGNO(00367)
"No ConfigArgs registered for the '%s' service, "
"perhaps this service is not installed?",
mpm_service_name);
return APR_SUCCESS;
}
else
return (rv);
}
if (!svc_args || svc_args->nelts == 0) {
return (APR_SUCCESS);
}
/* Now we have the mpm_service_name arg, and the mpm_runservice_nt()
* call appended the arguments passed by StartService(), so it's
* time to _prepend_ the default arguments for the server from
* the service's default arguments (all others override them)...
*/
args->nalloc = args->nelts + svc_args->nelts;
cmb_data = malloc(args->nalloc * sizeof(const char *));
/* First three args (argv[0], -f, path) remain first */
memcpy(cmb_data, args->elts, args->elt_size * fixed_args);
/* Service args follow from service registry array */
memcpy(cmb_data + fixed_args, svc_args->elts,
svc_args->elt_size * svc_args->nelts);
/* Remaining new args follow */
memcpy(cmb_data + fixed_args + svc_args->nelts,
(const char **)args->elts + fixed_args,
args->elt_size * (args->nelts - fixed_args));
args->elts = (char *)cmb_data;
args->nelts = args->nalloc;
return APR_SUCCESS;
}
static void service_stopped(void)
{
/* Still have a thread & window to clean up, so signal now */
if (globdat.service_thread)
{
/* Stop logging to the event log */
mpm_nt_eventlog_stderr_flush();
/* Cause the service_nt_main_fn to complete */
ReleaseMutex(globdat.service_term);
ReportStatusToSCMgr(SERVICE_STOPPED, 0, &globdat);
WaitForSingleObject(globdat.service_thread, 5000);
CloseHandle(globdat.service_thread);
}
}
apr_status_t mpm_service_to_start(const char **display_name, apr_pool_t *p)
{
HANDLE hProc = GetCurrentProcess();
HANDLE hThread = GetCurrentThread();
HANDLE waitfor[2];
/* Prevent holding open the (hidden) console */
ap_real_exit_code = 0;
/* GetCurrentThread returns a psuedo-handle, we need
* a real handle for another thread to wait upon.
*/
if (!DuplicateHandle(hProc, hThread, hProc, &(globdat.mpm_thread),
0, FALSE, DUPLICATE_SAME_ACCESS)) {
return APR_ENOTHREAD;
}
globdat.service_init = CreateEvent(NULL, FALSE, FALSE, NULL);
globdat.service_term = CreateMutex(NULL, TRUE, NULL);
if (!globdat.service_init || !globdat.service_term) {
return APR_EGENERAL;
}
globdat.service_thread = CreateThread(NULL, 65536,
service_nt_dispatch_thread,
NULL, stack_res_flag,
&globdat.service_thread_id);
if (!globdat.service_thread) {
return APR_ENOTHREAD;
}
waitfor[0] = globdat.service_init;
waitfor[1] = globdat.service_thread;
/* Wait for controlling thread init or termination */
if (WaitForMultipleObjects(2, waitfor, FALSE, 10000) != WAIT_OBJECT_0) {
return APR_ENOTHREAD;
}
atexit(service_stopped);
*display_name = mpm_display_name;
return APR_SUCCESS;
}
apr_status_t mpm_service_started(void)
{
set_service_description();
ReportStatusToSCMgr(SERVICE_RUNNING, 0, &globdat);
return APR_SUCCESS;
}
void mpm_service_stopping(void)
{
ReportStatusToSCMgr(SERVICE_STOP_PENDING, 30000, &globdat);
}
apr_status_t mpm_service_install(apr_pool_t *ptemp, int argc,
const char * const * argv, int reconfig)
{
char key_name[MAX_PATH];
char *launch_cmd;
ap_regkey_t *key;
apr_status_t rv;
SC_HANDLE schService;
SC_HANDLE schSCManager;
DWORD rc;
#if APR_HAS_UNICODE_FS
apr_wchar_t *display_name_w;
apr_wchar_t *launch_cmd_w;
#endif
fprintf(stderr, reconfig ? "Reconfiguring the '%s' service\n"
: "Installing the '%s' service\n",
mpm_display_name);
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
apr_size_t slen = strlen(mpm_display_name) + 1;
apr_size_t wslen = slen;
display_name_w = apr_palloc(ptemp, wslen * sizeof(apr_wchar_t));
rv = apr_conv_utf8_to_ucs2(mpm_display_name, &slen,
display_name_w, &wslen);
if (rv != APR_SUCCESS)
return rv;
else if (slen)
return APR_ENAMETOOLONG;
launch_cmd_w = apr_palloc(ptemp, (MAX_PATH + 17) * sizeof(apr_wchar_t));
launch_cmd_w[0] = L'"';
rc = GetModuleFileNameW(NULL, launch_cmd_w + 1, MAX_PATH);
wcscpy(launch_cmd_w + rc + 1, L"\" -k runservice");
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
launch_cmd = apr_palloc(ptemp, MAX_PATH + 17);
launch_cmd[0] = '"';
rc = GetModuleFileName(NULL, launch_cmd + 1, MAX_PATH);
strcpy(launch_cmd + rc + 1, "\" -k runservice");
}
#endif
if (rc == 0) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00368) "GetModuleFileName failed");
return rv;
}
schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
SC_MANAGER_CREATE_SERVICE);
if (!schSCManager) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00369) "Failed to open the Windows service "
"manager, perhaps you forgot to log in as Administrator?");
return (rv);
}
if (reconfig) {
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
schService = OpenServiceW(schSCManager, mpm_service_name_w,
SERVICE_CHANGE_CONFIG);
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
schService = OpenService(schSCManager, mpm_service_name,
SERVICE_CHANGE_CONFIG);
}
#endif
if (!schService) {
rv = apr_get_os_error();
CloseServiceHandle(schSCManager);
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00373) "Failed to open the '%s' service",
mpm_display_name);
return (rv);
}
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
rc = ChangeServiceConfigW(schService,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
launch_cmd_w, NULL, NULL,
L"Tcpip\0Afd\0", NULL, NULL,
display_name_w);
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
rc = ChangeServiceConfig(schService,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
launch_cmd, NULL, NULL,
"Tcpip\0Afd\0", NULL, NULL,
mpm_display_name);
}
#endif
if (!rc) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(02652) "ChangeServiceConfig failed");
/* !schService aborts configuration below */
CloseServiceHandle(schService);
schService = NULL;
}
}
else {
/* RPCSS is the Remote Procedure Call (RPC) Locator required
* for DCOM communication pipes. I am far from convinced we
* should add this to the default service dependencies, but
* be warned that future apache modules or ISAPI dll's may
* depend on it.
*/
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
schService = CreateServiceW(schSCManager, // SCManager database
mpm_service_name_w, // name of service
display_name_w, // name to display
SERVICE_ALL_ACCESS, // access required
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
launch_cmd_w, // service's binary
NULL, // no load svc group
NULL, // no tag identifier
L"Tcpip\0Afd\0", // dependencies
NULL, // use SYSTEM account
NULL); // no password
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
schService = CreateService(schSCManager, // SCManager database
mpm_service_name, // name of service
mpm_display_name, // name to display
SERVICE_ALL_ACCESS, // access required
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
launch_cmd, // service's binary
NULL, // no load svc group
NULL, // no tag identifier
"Tcpip\0Afd\0", // dependencies
NULL, // use SYSTEM account
NULL); // no password
}
#endif
if (!schService)
{
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00370) "Failed to create the '%s' service",
mpm_display_name);
CloseServiceHandle(schSCManager);
return (rv);
}
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
set_service_description();
/* Store the service ConfigArgs in the registry...
*/
apr_snprintf(key_name, sizeof(key_name), SERVICEPARAMS, mpm_service_name);
rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, key_name,
APR_READ | APR_WRITE | APR_CREATE, pconf);
if (rv == APR_SUCCESS) {
rv = ap_regkey_value_array_set(key, "ConfigArgs", argc, argv, pconf);
ap_regkey_close(key);
}
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00371) "Failed to store ConfigArgs for the "
"'%s' service in the registry.", mpm_display_name);
return (rv);
}
fprintf(stderr, "The '%s' service is successfully installed.\n",
mpm_display_name);
return APR_SUCCESS;
}
apr_status_t mpm_service_uninstall(void)
{
apr_status_t rv;
SC_HANDLE schService;
SC_HANDLE schSCManager;
fprintf(stderr, "Removing the '%s' service\n", mpm_display_name);
schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
SC_MANAGER_CONNECT);
if (!schSCManager) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(10009) "Failed to open the Windows service "
"manager, perhaps you forgot to log in as Administrator?");
return (rv);
}
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
schService = OpenServiceW(schSCManager, mpm_service_name_w, DELETE);
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
schService = OpenService(schSCManager, mpm_service_name, DELETE);
}
#endif
if (!schService) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(10010) "Failed to open the '%s' service",
mpm_display_name);
return (rv);
}
/* assure the service is stopped before continuing
*
* This may be out of order... we might not be able to be
* granted all access if the service is running anyway.
*
* And do we want to make it *this easy* for them
* to uninstall their service unintentionally?
*/
/* ap_stop_service(schService);
*/
if (DeleteService(schService) == 0) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00374) "Failed to delete the '%s' service",
mpm_display_name);
return (rv);
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
fprintf(stderr, "The '%s' service has been removed successfully.\n",
mpm_display_name);
return APR_SUCCESS;
}
/* signal_service_transition is a simple thunk to signal the service
* and monitor its successful transition. If the signal passed is 0,
* then the caller is assumed to already have performed some service
* operation to be monitored (such as StartService), and no actual
* ControlService signal is sent.
*/
static int signal_service_transition(SC_HANDLE schService, DWORD signal,
DWORD pending, DWORD complete)
{
if (signal && !ControlService(schService, signal, &globdat.ssStatus))
return FALSE;
do {
Sleep(1000);
if (!QueryServiceStatus(schService, &globdat.ssStatus))
return FALSE;
} while (globdat.ssStatus.dwCurrentState == pending);
return (globdat.ssStatus.dwCurrentState == complete);
}
apr_status_t mpm_service_start(apr_pool_t *ptemp, int argc,
const char * const * argv)
{
apr_status_t rv;
SC_HANDLE schService;
SC_HANDLE schSCManager;
fprintf(stderr, "Starting the '%s' service\n", mpm_display_name);
schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
SC_MANAGER_CONNECT);
if (!schSCManager) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(10011) "Failed to open the Windows service "
"manager, perhaps you forgot to log in as Administrator?");
return (rv);
}
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
schService = OpenServiceW(schSCManager, mpm_service_name_w,
SERVICE_START | SERVICE_QUERY_STATUS);
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
schService = OpenService(schSCManager, mpm_service_name,
SERVICE_START | SERVICE_QUERY_STATUS);
}
#endif
if (!schService) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(10012) "Failed to open the '%s' service",
mpm_display_name);
CloseServiceHandle(schSCManager);
return (rv);
}
if (QueryServiceStatus(schService, &globdat.ssStatus)
&& (globdat.ssStatus.dwCurrentState == SERVICE_RUNNING)) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL,
APLOGNO(00377) "The '%s' service is already started!",
mpm_display_name);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return 0;
}
rv = APR_EINIT;
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
LPWSTR *start_argv_w = malloc((argc + 1) * sizeof(LPCWSTR));
int i;
for (i = 0; i < argc; ++i)
{
apr_size_t slen = strlen(argv[i]) + 1;
apr_size_t wslen = slen;
start_argv_w[i] = malloc(wslen * sizeof(WCHAR));
rv = apr_conv_utf8_to_ucs2(argv[i], &slen, start_argv_w[i], &wslen);
if (rv != APR_SUCCESS)
return rv;
else if (slen)
return APR_ENAMETOOLONG;
}
start_argv_w[argc] = NULL;
if (StartServiceW(schService, argc, start_argv_w)
&& signal_service_transition(schService, 0, /* test only */
SERVICE_START_PENDING,
SERVICE_RUNNING))
rv = APR_SUCCESS;
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
char **start_argv = malloc((argc + 1) * sizeof(const char *));
memcpy(start_argv, argv, argc * sizeof(const char *));
start_argv[argc] = NULL;
if (StartService(schService, argc, start_argv)
&& signal_service_transition(schService, 0, /* test only */
SERVICE_START_PENDING,
SERVICE_RUNNING))
rv = APR_SUCCESS;
}
#endif
if (rv != APR_SUCCESS)
rv = apr_get_os_error();
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
if (rv == APR_SUCCESS)
fprintf(stderr, "The '%s' service is running.\n", mpm_display_name);
else
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL, APLOGNO(00378)
"Failed to start the '%s' service",
mpm_display_name);
return rv;
}
/* signal is zero to stop, non-zero for restart */
void mpm_signal_service(apr_pool_t *ptemp, int signal)
{
int success = FALSE;
SC_HANDLE schService;
SC_HANDLE schSCManager;
schSCManager = OpenSCManager(NULL, NULL, /* default machine & database */
SC_MANAGER_CONNECT);
if (!schSCManager) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(10013) "Failed to open the Windows service "
"manager, perhaps you forgot to log in as Administrator?");
return;
}
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
schService = OpenServiceW(schSCManager, mpm_service_name_w,
SERVICE_INTERROGATE | SERVICE_QUERY_STATUS |
SERVICE_USER_DEFINED_CONTROL |
SERVICE_START | SERVICE_STOP);
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
schService = OpenService(schSCManager, mpm_service_name,
SERVICE_INTERROGATE | SERVICE_QUERY_STATUS |
SERVICE_USER_DEFINED_CONTROL |
SERVICE_START | SERVICE_STOP);
}
#endif
if (schService == NULL) {
/* Could not open the service */
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(10014) "Failed to open the '%s' service",
mpm_display_name);
CloseServiceHandle(schSCManager);
return;
}
if (!QueryServiceStatus(schService, &globdat.ssStatus)) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(00381) "Query of the '%s' service failed",
mpm_display_name);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
if (!signal && (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED)) {
fprintf(stderr, "The '%s' service is not started.\n", mpm_display_name);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
fprintf(stderr, signal ? "The '%s' service is restarting.\n"
: "The '%s' service is stopping.\n",
mpm_display_name);
if (!signal)
success = signal_service_transition(schService,
SERVICE_CONTROL_STOP,
SERVICE_STOP_PENDING,
SERVICE_STOPPED);
else if (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED) {
mpm_service_start(ptemp, 0, NULL);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
else
success = signal_service_transition(schService,
SERVICE_APACHE_RESTART,
SERVICE_START_PENDING,
SERVICE_RUNNING);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
if (success)
fprintf(stderr, signal ? "The '%s' service has restarted.\n"
: "The '%s' service has stopped.\n",
mpm_display_name);
else
fprintf(stderr, signal ? "Failed to restart the '%s' service.\n"
: "Failed to stop the '%s' service.\n",
mpm_display_name);
}
|