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
|
/* Windows Service related function definitions. */
#ifdef WIN32
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/library/system.h>
#include <stdlib.h> /* calloc() */
#include <stdio.h> /* sprintf() */
#include <windows.h>
#include <process.h> /* _beginthreadex() */
#include <net-snmp/library/winservice.h>
#ifdef mingw32 /* MinGW doesn't fully support exception handling. */
#define TRY if(1)
#define LEAVE goto labelFIN
#define FINALLY do { \
labelFIN: \
; \
} while(0); if(1)
#else
#define TRY __try
#define LEAVE __leave
#define FINALLY __finally
#endif /* mingw32 */
#if defined(WIN32) && defined(HAVE_WIN32_PLATFORM_SDK) && !defined(mingw32)
#pragma comment(lib, "iphlpapi.lib")
#endif
#if defined(WIN32) && !defined(mingw32)
#ifdef USING_WINEXTDLL_MODULE
#pragma comment(lib, "snmpapi.lib")
#pragma comment(lib, "mgmtapi.lib")
#endif
#endif
/*
* Error levels returned when registering or unregistering the service
*/
enum service_status {
SERVICE_ERROR_NONE = 0,
SERVICE_ERROR_SCM_OPEN = 1, /* Can not open SCM */
SERVICE_ERROR_CREATE_SERVICE = 2, /* Can not create service */
SERVICE_ERROR_CREATE_REGISTRY_ENTRIES = 3, /* Can not create registry entries */
SERVICE_ERROR_OPEN_SERVICE = 4, /* Can not open service (service does not exist) */
};
/*
* Define Message catalog ID
* MessageId: DISPLAY_MSG
* MessageText: %1.
*/
enum { DISPLAY_MSG = 0x00000064L };
/*
* Hint Value to SCM to wait before sending successive commands to service
*/
enum { SCM_WAIT_INTERVAL = 7000 };
static void WINAPI ServiceMain(DWORD argc, char *argv[]);
static void WINAPI ControlHandler(DWORD dwControl);
static void ProcessServiceStop(void);
static void ProcessServicePause(void);
static void ProcessServiceContinue(void);
static void ProcessServiceInterrogate(void);
static BOOL SetSimpleSecurityAttributes(SECURITY_ATTRIBUTES
*pSecurityAttr);
static void FreeSecurityAttributes(SECURITY_ATTRIBUTES *pSecurityAttr);
static unsigned WINAPI ThreadFunction(void *lpParam);
/*
* External global variables used here
*/
/*
* Application Name
* This should be declared by the application, which wants to register as
* windows service
*/
extern char *app_name_long;
/*
* Declare global variable
*/
/*
* Flag to indicate whether process is running as Service
*/
static BOOL g_fRunningAsService;
/*
* Variable to maintain Current Service status
*/
static SERVICE_STATUS ServiceStatus;
/*
* Service Handle
*/
static SERVICE_STATUS_HANDLE hServiceStatus;
/*
* Service Table Entry
*/
SERVICE_TABLE_ENTRY ServiceTableEntry[] = {
{NULL, ServiceMain}, /* Service Main function */
{NULL, NULL}
};
/*
* Handle to Thread, to implement Pause, Resume and Stop functions
*/
static HANDLE hServiceThread = NULL; /* Thread Handle */
/*
* Holds calling partys Function Entry point, that should start
* when entering service mode
*/
static int (*ServiceEntryPoint)(int Argc, char *Argv[]);
/*
* To hold Stop Function address, to be called when STOP request
* received from the SCM
*/
static void (*StopFunction)(void);
/*
* To update windows service status to SCM
*/
static BOOL UpdateServiceStatus(DWORD dwStatus, DWORD dwErrorCode,
DWORD dwWaitHint);
/*
* To Report current service status to SCM
*/
static BOOL ReportCurrentServiceStatus(void);
void ProcessError(WORD eventLogType, const char *pszMessage,
int useGetLastError, int quiet);
#ifndef HAVE__BEGINTHREADEX
static uintptr_t
_beginthreadex(void *security, unsigned stack_size,
unsigned (__stdcall *start_address) (void *), void *arglist,
unsigned initflag, unsigned *thrdaddr)
{
return (uintptr_t) CreateThread(security, stack_size,
(LPTHREAD_START_ROUTINE)start_address,
arglist, initflag, (LPDWORD)thrdaddr);
}
#if 0
static void
_endthreadex(unsigned retval)
{
ExitThread(retval);
}
#endif
#endif
/*
* To register as Windows Service with SCM(Service Control Manager)
* Input - Service Name, Service Display Name,Service Description and
* Service startup arguments
*/
int
RegisterService(const char *lpszServiceName,
const char *lpszServiceDisplayName,
const char *lpszServiceDescription,
InputParams *StartUpArg, int quiet)
{ /* Startup argument to the service */
char szServicePath[MAX_PATH]; /* To hold module File name */
char MsgErrorString[1024]; /* Message or Error string */
char szServiceCommand[MAX_PATH + 9]; /* Command to execute */
SC_HANDLE hSCManager = NULL;
SC_HANDLE hService = NULL;
static const char szRegAppLogKey[] =
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
char szRegKey[512];
HKEY hKey = NULL; /* Key to registry entry */
HKEY hParamKey = NULL; /* To store startup parameters */
DWORD dwData; /* Type of logging supported */
int i, j; /* Loop variables */
int exitStatus = 0;
GetModuleFileName(NULL, szServicePath, MAX_PATH);
TRY {
/*
* Open Service Control Manager handle
*/
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL) {
ProcessError(EVENTLOG_ERROR_TYPE,
"Can't open SCM (Service Control Manager)", 1,
quiet);
exitStatus = SERVICE_ERROR_SCM_OPEN;
LEAVE;
}
/*
* Generate the command to be executed by the SCM
*/
snprintf(szServiceCommand, sizeof(szServiceCommand),
"\"%s\" %s", szServicePath, "-service");
/*
* Create the desired service
*/
hService = CreateService(hSCManager, lpszServiceName, lpszServiceDisplayName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, szServiceCommand, NULL, /* load-order group */
NULL, /* group member tag */
NULL, /* dependencies */
NULL, /* account */
NULL); /* password */
if (hService == NULL) {
snprintf(MsgErrorString, sizeof(MsgErrorString),
"%s %s", "Can't create service",
lpszServiceDisplayName);
ProcessError(EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
exitStatus = SERVICE_ERROR_CREATE_SERVICE;
LEAVE;
}
/*
* Create registry entries for the event log
*/
/*
* Create registry Application event log key
*/
snprintf(szRegKey, sizeof(szRegKey), "%s%s", szRegAppLogKey,
lpszServiceName);
/*
* Create registry key
*/
if (RegCreateKey(HKEY_LOCAL_MACHINE, szRegKey, &hKey) !=
ERROR_SUCCESS) {
snprintf(MsgErrorString, sizeof(MsgErrorString),
"%s %s",
"is unable to create registry entries",
lpszServiceDisplayName);
ProcessError(EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
exitStatus = SERVICE_ERROR_CREATE_REGISTRY_ENTRIES;
LEAVE;
}
/*
* Add Event ID message file name to the 'EventMessageFile' subkey
*/
RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ,
(const void *) szServicePath,
strlen(szServicePath) + sizeof(char));
/*
* Set the supported types flags.
*/
dwData =
EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
EVENTLOG_INFORMATION_TYPE;
RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD,
(const void *) &dwData, sizeof(dwData));
/*
* Close Registry key
*/
RegCloseKey(hKey);
/*
* Set Service Description String and save startup parameters if present
*/
if (lpszServiceDescription != NULL || StartUpArg->Argc > 2) {
/*
* Create Registry Key path
*/
snprintf(szRegKey, sizeof(szRegKey),
"SYSTEM\\CurrentControlSet\\Services\\%s",
app_name_long);
hKey = NULL;
/*
* Open Registry key using Create and Set access.
*/
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0, KEY_WRITE,
&hKey) != ERROR_SUCCESS) {
snprintf(MsgErrorString, sizeof(MsgErrorString),
"%s %s",
"is unable to create registry entries",
lpszServiceDisplayName);
ProcessError(EVENTLOG_ERROR_TYPE, MsgErrorString, 1,
quiet);
exitStatus = SERVICE_ERROR_CREATE_REGISTRY_ENTRIES;
LEAVE;
}
/*
* Create description subkey and the set value
*/
if (lpszServiceDescription != NULL) {
if (RegSetValueEx(hKey, "Description", 0, REG_SZ,
(const void *) lpszServiceDescription,
strlen(lpszServiceDescription) +
sizeof(char)) != ERROR_SUCCESS) {
snprintf(MsgErrorString, sizeof(MsgErrorString),
"%s %s",
"is unable to create registry entries",
lpszServiceDisplayName);
ProcessError(EVENTLOG_ERROR_TYPE, MsgErrorString, 1,
quiet);
exitStatus = SERVICE_ERROR_CREATE_REGISTRY_ENTRIES;
LEAVE;
};
}
/*
* Save startup arguments if they are present
*/
if (StartUpArg->Argc > 2) {
/*
* Create Subkey parameters
*/
if (RegCreateKeyEx
(hKey, "Parameters", 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
&hParamKey, NULL) != ERROR_SUCCESS) {
snprintf(MsgErrorString, sizeof(MsgErrorString),
"%s %s",
"is unable to create registry entries",
lpszServiceDisplayName);
ProcessError(EVENTLOG_ERROR_TYPE, MsgErrorString, 1,
quiet);
exitStatus = SERVICE_ERROR_CREATE_REGISTRY_ENTRIES;
LEAVE;
}
/*
* Save parameters
*/
/*
* Loop through arguments
*/
if (quiet) /* Make sure we don't store -quiet arg */
i = 3;
else
i = 2;
for (j = 1; i < StartUpArg->Argc; i++, j++) {
snprintf(szRegKey, sizeof(szRegKey), "%s%d", "Param",
j);
/*
* Create registry key
*/
if (RegSetValueEx
(hParamKey, szRegKey, 0, REG_SZ,
(const void *) StartUpArg->Argv[i],
strlen(StartUpArg->Argv[i]) +
sizeof(char)) != ERROR_SUCCESS) {
snprintf(MsgErrorString, sizeof(MsgErrorString),
"%s %s",
"is unable to create registry entries",
lpszServiceDisplayName);
ProcessError(EVENTLOG_ERROR_TYPE, MsgErrorString,
1, quiet);
exitStatus = SERVICE_ERROR_CREATE_REGISTRY_ENTRIES;
LEAVE;
};
}
}
/*
* Everything is set, delete hKey
*/
RegCloseKey(hParamKey);
RegCloseKey(hKey);
}
/*
* Ready to log messages
*/
/*
* Successfully registered as service
*/
snprintf(MsgErrorString, sizeof(MsgErrorString), "%s %s",
lpszServiceName, "successfully registered as a service");
/*
* Log message to eventlog
*/
ProcessError(EVENTLOG_INFORMATION_TYPE, MsgErrorString, 0, quiet);
}
FINALLY {
if (hSCManager)
CloseServiceHandle(hSCManager);
if (hService)
CloseServiceHandle(hService);
if (hKey)
RegCloseKey(hKey);
if (hParamKey)
RegCloseKey(hParamKey);
}
return (exitStatus);
}
/*
* Unregister the service with the Windows SCM
* Input - ServiceName
*/
int
UnregisterService(const char *lpszServiceName, int quiet)
{
char MsgErrorString[1024]; /* Message or Error string */
SC_HANDLE hSCManager = NULL; /* SCM handle */
SC_HANDLE hService = NULL; /* Service Handle */
SERVICE_STATUS sStatus;
static const char szRegAppLogKey[] =
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
char szRegKey[512];
int exitStatus = 0;
/*
* HKEY hKey = NULL; ?* Key to registry entry
*/
TRY {
/*
* Open Service Control Manager
*/
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL) {
ProcessError(EVENTLOG_ERROR_TYPE,
"Can't open SCM (Service Control Manager)", 1,
quiet);
exitStatus = SERVICE_ERROR_SCM_OPEN;
LEAVE;
}
/*
* Open registered service
*/
hService =
OpenService(hSCManager, lpszServiceName, SERVICE_ALL_ACCESS);
if (hService == NULL) {
snprintf(MsgErrorString, sizeof(MsgErrorString),
"%s %s", "Can't open service", lpszServiceName);
ProcessError(EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
exitStatus = SERVICE_ERROR_OPEN_SERVICE;
LEAVE;
}
/*
* Query service status
* If running stop before deleting
*/
if (QueryServiceStatus(hService, &sStatus)) {
if (sStatus.dwCurrentState == SERVICE_RUNNING
|| sStatus.dwCurrentState == SERVICE_PAUSED) {
ControlService(hService, SERVICE_CONTROL_STOP, &sStatus);
}
};
/*
* Delete the service
*/
if (DeleteService(hService) == FALSE) {
snprintf(MsgErrorString, sizeof(MsgErrorString),
"%s %s", "Can't delete service", lpszServiceName);
/*
* Log message to eventlog
*/
ProcessError(EVENTLOG_ERROR_TYPE, MsgErrorString, 0, quiet);
LEAVE;
}
/*
* Log "Service deleted successfully " message to eventlog
*/
snprintf(MsgErrorString, sizeof(MsgErrorString), "%s %s",
lpszServiceName, "service deleted");
ProcessError(EVENTLOG_INFORMATION_TYPE, MsgErrorString, 0, quiet);
/*
* Delete registry entries for EventLog
*/
snprintf(szRegKey, sizeof(szRegKey), "%s%s", szRegAppLogKey,
lpszServiceName);
RegDeleteKey(HKEY_LOCAL_MACHINE, szRegKey);
}
/*
* Delete the handles
*/
FINALLY {
if (hService)
CloseServiceHandle(hService);
if (hSCManager)
CloseServiceHandle(hSCManager);
}
return (exitStatus);
}
/*
* Write a message to the Windows event log.
*/
static void
WriteToEventLog(WORD wType, const char *pszFormat, ...)
{
char szMessage[512];
const char *LogStr[1];
va_list ArgList;
HANDLE hEventSource = NULL;
va_start(ArgList, pszFormat);
vsnprintf(szMessage, sizeof(szMessage), pszFormat, ArgList);
va_end(ArgList);
LogStr[0] = szMessage;
hEventSource = RegisterEventSource(NULL, app_name_long);
if (hEventSource == NULL)
return;
ReportEvent(hEventSource, wType, 0,
DISPLAY_MSG, NULL, 1, 0, LogStr, NULL);
DeregisterEventSource(hEventSource);
}
/*
* Pre-process the second command-line argument from the user.
* Service related options are:
* -register - registers the service
* -unregister - unregisters the service
* -service - run as service
* other command-line arguments are ignored here.
*
* Return: Type indicating the option specified
*/
enum net_snmp_cmd_line_action
ParseCmdLineForServiceOption(int argc, char *argv[], int *quiet)
{
enum net_snmp_cmd_line_action nReturn = RUN_AS_CONSOLE; /* default is to run as a console application */
if (argc >= 2) {
/*
* second argument present
*/
if (strcasecmp("-register", argv[1]) == 0) {
nReturn = REGISTER_SERVICE;
}
else if (strcasecmp("-unregister", argv[1]) == 0) {
nReturn = UN_REGISTER_SERVICE;
}
else if (strcasecmp("-service", argv[1]) == 0) {
nReturn = RUN_AS_SERVICE;
}
}
if (argc >= 3) {
/*
* third argument present
*/
if (strcasecmp("-quiet", argv[2]) == 0) {
*quiet = 1;
}
}
return nReturn;
}
/*
* Write error message to event log, console or pop-up window.
*
* If useGetLastError is 1, the last error returned from GetLastError()
* is appended to pszMessage, separated by a ": ".
*
* eventLogType: MessageBox equivalent:
*
* EVENTLOG_INFORMATION_TYPE MB_ICONASTERISK
* EVENTLOG_WARNING_TYPE MB_ICONEXCLAMATION
* EVENTLOG_ERROR_TYPE MB_ICONSTOP
*
*/
void
ProcessError(WORD eventLogType, const char *pszMessage,
int useGetLastError, int quiet)
{
HANDLE hEventSource = NULL;
char pszMessageFull[1024]; /* Combined pszMessage and GetLastError */
/*
* If useGetLastError enabled, generate text from GetLastError() and append to
* pszMessageFull
*/
if (useGetLastError) {
char *pErrorMsgTemp = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &pErrorMsgTemp, 0, NULL);
snprintf(pszMessageFull, sizeof(pszMessageFull), "%s: %s",
pszMessage, pErrorMsgTemp);
if (pErrorMsgTemp) {
LocalFree(pErrorMsgTemp);
pErrorMsgTemp = NULL;
}
} else {
snprintf(pszMessageFull, sizeof(pszMessageFull), "%s", pszMessage);
}
hEventSource = RegisterEventSource(NULL, app_name_long);
if (hEventSource != NULL) {
const char *LogStr[1];
LogStr[0] = pszMessageFull;
if (ReportEvent(hEventSource, eventLogType, 0, DISPLAY_MSG, /* just output the text to the event log */
NULL, 1, 0, LogStr, NULL)) {
} else {
char *pErrorMsgTemp = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &pErrorMsgTemp, 0, NULL);
fprintf(stderr,
"Could NOT lot to Event Log. Error returned from ReportEvent(): %s\n",
pErrorMsgTemp);
if (pErrorMsgTemp) {
LocalFree(pErrorMsgTemp);
pErrorMsgTemp = NULL;
}
}
DeregisterEventSource(hEventSource);
}
if (quiet) {
fprintf(stderr, "%s\n", pszMessageFull);
} else {
switch (eventLogType) {
case EVENTLOG_INFORMATION_TYPE:
MessageBox(NULL, pszMessageFull, app_name_long,
MB_ICONASTERISK);
break;
case EVENTLOG_WARNING_TYPE:
MessageBox(NULL, pszMessageFull, app_name_long,
MB_ICONEXCLAMATION);
break;
case EVENTLOG_ERROR_TYPE:
MessageBox(NULL, pszMessageFull, app_name_long, MB_ICONSTOP);
break;
default:
MessageBox(NULL, pszMessageFull, app_name_long,
EVENTLOG_WARNING_TYPE);
break;
}
}
}
/*
* Update current service status.
* Sends the current service status to the SCM. Also updates
* the global service status structure.
*/
static BOOL
UpdateServiceStatus(DWORD dwStatus, DWORD dwErrorCode, DWORD dwWaitHint)
{
static DWORD dwCheckpoint = 1;
DWORD dwControls =
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
if (g_fRunningAsService == FALSE)
return FALSE;
ZeroMemory(&ServiceStatus, sizeof(ServiceStatus));
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = dwStatus;
ServiceStatus.dwWaitHint = dwWaitHint;
if (dwErrorCode) {
ServiceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
ServiceStatus.dwServiceSpecificExitCode = dwErrorCode;
}
/*
* special cases that depend on the new state
*/
switch (dwStatus) {
case SERVICE_START_PENDING:
dwControls = 0;
break;
case SERVICE_RUNNING:
case SERVICE_STOPPED:
dwCheckpoint = 0;
break;
}
ServiceStatus.dwCheckPoint = dwCheckpoint++;
ServiceStatus.dwControlsAccepted = dwControls;
return ReportCurrentServiceStatus();
}
/*
* Reports current service status to SCM
*/
static BOOL
ReportCurrentServiceStatus()
{
return SetServiceStatus(hServiceStatus, &ServiceStatus);
}
/*
* ServiceMain function.
*/
static void WINAPI
ServiceMain(DWORD argc, char *argv[])
{
SECURITY_ATTRIBUTES SecurityAttributes;
unsigned threadId;
/*
* Input arguments
*/
DWORD ArgCount = 0;
char **ArgArray = NULL;
char szRegKey[512];
HKEY hParamKey = NULL;
DWORD TotalParams = 0;
int i;
InputParams ThreadInputParams;
/*
* Build the Input parameters to pass to worker thread
*/
/*
* SCM sends Service Name as first arg, increment to point
* arguments user specified while starting control agent
*/
/*
* Read registry parameter
*/
ArgCount = 1;
/*
* Create registry key path
*/
snprintf(szRegKey, sizeof(szRegKey), "%s%s\\%s",
"SYSTEM\\CurrentControlSet\\Services\\", app_name_long,
"Parameters");
if (RegOpenKeyEx
(HKEY_LOCAL_MACHINE, szRegKey, 0, KEY_ALL_ACCESS,
&hParamKey) == ERROR_SUCCESS) {
/*
* Read startup configuration information
*/
/*
* Find number of subkeys inside parameters
*/
if (RegQueryInfoKey(hParamKey, NULL, NULL, 0,
NULL, NULL, NULL, &TotalParams,
NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
if (TotalParams != 0) {
ArgCount += TotalParams;
/*
* Allocate memory to hold strings
*/
ArgArray = calloc(ArgCount, sizeof(ArgArray[0]));
if (ArgArray == 0) {
WriteToEventLog(EVENTLOG_ERROR_TYPE,
"Resource failure");
return;
}
/*
* Copy first argument
*/
ArgArray[0] = strdup(argv[0]);
for (i = 1; i <= TotalParams; i++) {
DWORD dwErrorcode;
DWORD nSize;
DWORD nRegkeyType;
char *szValue;
/*
* Create Subkey value name
*/
snprintf(szRegKey, sizeof(szRegKey), "%s%d", "Param",
i);
/*
* Query subkey.
*/
nSize = 0;
dwErrorcode =
RegQueryValueEx(hParamKey, szRegKey, NULL,
&nRegkeyType, NULL, &nSize);
if (dwErrorcode == ERROR_SUCCESS) {
if (nRegkeyType == REG_SZ
|| nRegkeyType == REG_EXPAND_SZ) {
szValue = malloc(nSize + sizeof(szValue[0]));
if (szValue) {
dwErrorcode =
RegQueryValueEx(hParamKey, szRegKey,
NULL, &nRegkeyType,
(void *) szValue,
&nSize);
if (dwErrorcode == ERROR_SUCCESS) {
szValue[nSize] = 0;
ArgArray[i] = szValue;
} else {
free(szValue);
WriteToEventLog(EVENTLOG_ERROR_TYPE,
"Querying registry key %s failed: error code %ld",
szRegKey, dwErrorcode);
}
} else
WriteToEventLog(EVENTLOG_ERROR_TYPE,
"Querying registry key %s failed: out of memory",
szRegKey);
} else
WriteToEventLog(EVENTLOG_ERROR_TYPE,
"Type %ld of registry key %s is incorrect",
nRegkeyType, szRegKey);
} else
WriteToEventLog(EVENTLOG_ERROR_TYPE,
"Querying registry key %s failed: error code %ld",
szRegKey, dwErrorcode);
if (!ArgArray[i]) {
TotalParams = ArgCount = i;
break;
}
}
}
}
RegCloseKey(hParamKey);
}
if (ArgCount == 1) {
/*
* No startup args are given
*/
ThreadInputParams.Argc = argc;
ThreadInputParams.Argv = argv;
}
else {
ThreadInputParams.Argc = ArgCount;
ThreadInputParams.Argv = ArgArray;
}
/*
* Register Service Control Handler
*/
hServiceStatus =
RegisterServiceCtrlHandler(app_name_long, ControlHandler);
if (hServiceStatus == 0) {
WriteToEventLog(EVENTLOG_ERROR_TYPE,
"RegisterServiceCtrlHandler failed");
return;
}
/*
* Update the service status to START_PENDING.
*/
UpdateServiceStatus(SERVICE_START_PENDING, NO_ERROR,
SCM_WAIT_INTERVAL);
/*
* Start the worker thread, which does the majority of the work .
*/
TRY {
if (SetSimpleSecurityAttributes(&SecurityAttributes) == FALSE) {
WriteToEventLog(EVENTLOG_ERROR_TYPE,
"Couldn't init security attributes");
LEAVE;
}
hServiceThread =
(void *) _beginthreadex(&SecurityAttributes, 0,
ThreadFunction,
(void *) &ThreadInputParams, 0,
&threadId);
if (hServiceThread == NULL) {
WriteToEventLog(EVENTLOG_ERROR_TYPE,
"Couldn't start worker thread");
LEAVE;
}
/*
* Set service status to SERVICE_RUNNING.
*/
UpdateServiceStatus(SERVICE_RUNNING, NO_ERROR, SCM_WAIT_INTERVAL);
/*
* Wait until the worker thread finishes.
*/
WaitForSingleObject(hServiceThread, INFINITE);
}
FINALLY {
/*
* Release resources
*/
UpdateServiceStatus(SERVICE_STOPPED, NO_ERROR, SCM_WAIT_INTERVAL);
if (hServiceThread)
CloseHandle(hServiceThread);
FreeSecurityAttributes(&SecurityAttributes);
/*
* Free allocated argument list
*/
if (ArgCount > 1 && ArgArray != NULL) {
/*
* Free all strings
*/
for (i = 0; i < ArgCount; i++) {
free(ArgArray[i]);
}
free(ArgArray);
}
}
}
/*
* Function to start as Windows service
* The calling party should specify their entry point as input parameter
* Returns TRUE if the Service is started successfully
*/
BOOL
RunAsService(int (*ServiceFunction)(int, char **))
{
/*
* Set the ServiceEntryPoint
*/
ServiceEntryPoint = ServiceFunction;
/*
* By default, mark as Running as a service
*/
g_fRunningAsService = TRUE;
/*
* Initialize ServiceTableEntry table
*/
ServiceTableEntry[0].lpServiceName = app_name_long; /* Application Name */
/*
* Call SCM via StartServiceCtrlDispatcher to run as Service
* * If the function returns TRUE we are running as Service,
*/
if (StartServiceCtrlDispatcher(ServiceTableEntry) == FALSE) {
g_fRunningAsService = FALSE;
/*
* Some other error has occurred.
*/
WriteToEventLog(EVENTLOG_ERROR_TYPE,
"Couldn't start service - %s", app_name_long);
}
return g_fRunningAsService;
}
/*
* Service control handler function
* Responds to SCM commands/requests
* This service handles 4 commands
* - interrogate, pause, continue and stop.
*/
void WINAPI
ControlHandler(DWORD dwControl)
{
switch (dwControl) {
case SERVICE_CONTROL_INTERROGATE:
ProcessServiceInterrogate();
break;
case SERVICE_CONTROL_PAUSE:
ProcessServicePause();
break;
case SERVICE_CONTROL_CONTINUE:
ProcessServiceContinue();
break;
case SERVICE_CONTROL_STOP:
ProcessServiceStop();
break;
}
}
/*
* To stop the service.
* If a stop function was registered, invoke it,
* otherwise terminate the worker thread.
* After stopping, Service status is set to STOP in
* main loop
*/
void
ProcessServiceStop(void)
{
UpdateServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, SCM_WAIT_INTERVAL);
if (StopFunction != NULL) {
(*StopFunction) ();
}
else {
TerminateThread(hServiceThread, 0);
}
}
/*
* Returns the current state of the service to the SCM.
*/
void
ProcessServiceInterrogate(void)
{
ReportCurrentServiceStatus();
}
/*
* To Create a security descriptor with a NULL ACL, which
* allows unlimited access. Returns a SECURITY_ATTRIBUTES
* structure that contains the security descriptor.
* The structure contains a dynamically allocated security
* descriptor that must be freed either manually, or by
* calling FreeSecurityAttributes
*/
BOOL
SetSimpleSecurityAttributes(SECURITY_ATTRIBUTES *pSecurityAttr)
{
BOOL fReturn = FALSE;
SECURITY_DESCRIPTOR *pSecurityDesc = NULL;
/*
* If an invalid address is passed as a parameter, return
* FALSE right away.
*/
if (!pSecurityAttr)
return FALSE;
pSecurityDesc =
(SECURITY_DESCRIPTOR *) LocalAlloc(LPTR,
SECURITY_DESCRIPTOR_MIN_LENGTH);
if (!pSecurityDesc)
return FALSE;
fReturn =
InitializeSecurityDescriptor(pSecurityDesc,
SECURITY_DESCRIPTOR_REVISION);
if (fReturn != FALSE) {
fReturn =
SetSecurityDescriptorDacl(pSecurityDesc, TRUE, NULL, FALSE);
}
if (fReturn != FALSE) {
pSecurityAttr->nLength = sizeof(SECURITY_ATTRIBUTES);
pSecurityAttr->lpSecurityDescriptor = pSecurityDesc;
pSecurityAttr->bInheritHandle = TRUE;
}
else {
/*
* Couldn't initialize or set security descriptor.
*/
LocalFree(pSecurityDesc);
}
return fReturn;
}
/*
* This function Frees the security descriptor, if any was created.
*/
void
FreeSecurityAttributes(SECURITY_ATTRIBUTES *pSecurityAttr)
{
if (pSecurityAttr && pSecurityAttr->lpSecurityDescriptor)
LocalFree(pSecurityAttr->lpSecurityDescriptor);
}
/*
* This function runs in the worker thread
* until an exit is forced, or until the SCM issues the STOP command.
* Invokes registered service function
* Returns when called registered function returns
*
* Input:
* lpParam contains argc and argv, pass to service main function
*/
unsigned WINAPI
ThreadFunction(void *lpParam)
{
InputParams *pInputArg = (InputParams *) lpParam;
return (*ServiceEntryPoint) (pInputArg->Argc, pInputArg->Argv);
}
/*
* This function is called to register an application-specific function
* which is invoked when the SCM stops the worker thread.
*/
void
RegisterStopFunction(void (*StopFunc)(void))
{
StopFunction = StopFunc;
}
/*
* SCM pause command invokes this function
* If the service is not running, this function does nothing.
* Otherwise, suspend the worker thread and update the status.
*/
void
ProcessServicePause(void)
{
if (ServiceStatus.dwCurrentState == SERVICE_RUNNING) {
UpdateServiceStatus(SERVICE_PAUSE_PENDING, NO_ERROR,
SCM_WAIT_INTERVAL);
if (SuspendThread(hServiceThread) != -1) {
UpdateServiceStatus(SERVICE_PAUSED, NO_ERROR,
SCM_WAIT_INTERVAL);
}
}
}
/*
* SCM resume command invokes this function
* If the service is not paused, this function does nothing.
* Otherwise, resume the worker thread and update the status.
*/
void
ProcessServiceContinue(void)
{
if (ServiceStatus.dwCurrentState == SERVICE_PAUSED) {
UpdateServiceStatus(SERVICE_CONTINUE_PENDING, NO_ERROR,
SCM_WAIT_INTERVAL);
if (ResumeThread(hServiceThread) != -1) {
UpdateServiceStatus(SERVICE_RUNNING, NO_ERROR,
SCM_WAIT_INTERVAL);
}
}
}
#endif /* WIN32 */
|