unrealircd- supernets unrealircd source & configuration |
git clone git://git.acid.vegas/unrealircd.git |
Log | Files | Refs | Archive | README | LICENSE |
service.c (3802B)
1 /************************************************************************ 2 * IRC - Internet Relay Chat, windows/service.c 3 * Copyright (C) 2002-2004 Dominick Meglio (codemastr) 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 1, or (at your option) 8 * any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 */ 19 20 #include "unrealircd.h" 21 22 SERVICE_STATUS IRCDStatus; 23 SERVICE_STATUS_HANDLE IRCDStatusHandle; 24 25 /* Signal to rehash */ 26 #define IRCD_SERVICE_CONTROL_REHASH 128 27 28 MODVAR BOOL IsService = FALSE; 29 30 #define WIN32_VERSION BASE_VERSION "-" PATCH1 PATCH2 PATCH3 PATCH4 PATCH5 31 32 /* Places the service in the STOPPED state 33 * Parameters: 34 * code - The error code (or 0) 35 */ 36 void SetServiceStop(int code) 37 { 38 IRCDStatus.dwCurrentState = SERVICE_STOPPED; 39 IRCDStatus.dwCheckPoint = 0; 40 IRCDStatus.dwWaitHint = 0; 41 IRCDStatus.dwWin32ExitCode = code; 42 IRCDStatus.dwServiceSpecificExitCode = code; 43 SetServiceStatus(IRCDStatusHandle, &IRCDStatus); 44 } 45 46 /* Handles the service messages 47 * Parameters: 48 * opcode - The message to process 49 */ 50 VOID WINAPI IRCDCtrlHandler(DWORD opcode) 51 { 52 DWORD status; 53 int i; 54 Client *acptr; 55 56 /* Stopping */ 57 if (opcode == SERVICE_CONTROL_STOP) 58 { 59 IRCDStatus.dwCurrentState = SERVICE_STOP_PENDING; 60 SetServiceStatus(IRCDStatusHandle, &IRCDStatus); 61 62 /* for (i = 0; i <= LastSlot; i++) 63 { 64 if (!(acptr = local[i])) 65 continue; 66 if (IsUser(acptr)) 67 sendnotice(acptr, "Server Terminating."); 68 else if (IsServer(acptr)) 69 sendto_one(acptr, NULL, ":%s ERROR :Terminated", me.name); 70 } */ 71 unload_all_modules(); 72 /* for (i = LastSlot; i >= 0; i--) 73 if ((acptr = local[i]) && DBufLength(&acptr->local->sendQ) > 0) 74 (void)send_queued(acptr); */ 75 SetServiceStop(0); 76 } 77 /* Rehash */ 78 else if (opcode == IRCD_SERVICE_CONTROL_REHASH) 79 { 80 request_rehash(NULL); 81 } 82 83 SetServiceStatus(IRCDStatusHandle, &IRCDStatus); 84 } 85 86 /* Entry point function 87 * Parameters: 88 * dwArgc - Argument count 89 * lpszArgv - Arguments 90 */ 91 VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) 92 { 93 DWORD error = 0; 94 char path[MAX_PATH], *folder; 95 96 IsService = TRUE; 97 98 /* Initialize the service structure */ 99 IRCDStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; 100 IRCDStatus.dwCurrentState = SERVICE_START_PENDING; 101 IRCDStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN; 102 IRCDStatus.dwWin32ExitCode = NO_ERROR; 103 IRCDStatus.dwServiceSpecificExitCode = 0; 104 IRCDStatus.dwCheckPoint = 0; 105 IRCDStatus.dwWaitHint = 0; 106 107 GetModuleFileName(NULL,path,MAX_PATH); 108 folder = strrchr(path, '\\'); 109 *folder = 0; 110 chdir(path); 111 112 /* Go one level up, since we are currently in the bin\ subdir 113 * and we want to be in (f.e.) "C:\Program Files\UnrealIRCd 6" 114 */ 115 chdir(".."); 116 117 /* Register the service controller */ 118 IRCDStatusHandle = RegisterServiceCtrlHandler("UnrealIRCd", IRCDCtrlHandler); 119 120 GetOSName(OSName); 121 122 InitDebug(); 123 init_winsock(); 124 125 /* Initialize the IRCd */ 126 if ((error = InitUnrealIRCd(dwArgc, lpszArgv)) != 1) 127 { 128 SetServiceStop(error); 129 return; 130 } 131 132 /* Go into the running state */ 133 IRCDStatus.dwCurrentState = SERVICE_RUNNING; 134 IRCDStatus.dwCheckPoint = 0; 135 IRCDStatus.dwWaitHint = 0; 136 SetServiceStatus(IRCDStatusHandle, &IRCDStatus); 137 138 SocketLoop(0); 139 return; 140 }