3 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
4 * inspired by previous work by Romeo Anghelache & Eric Stern.
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "squid_windows.h"
43 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
48 /* forward declarations */
49 static void WIN32_Exit(void);
50 static void WIN32_Abort(int);
52 static unsigned int GetOSVersion();
53 void WIN32_svcstatusupdate(DWORD
, DWORD
);
54 void WINAPI
WIN32_svcHandler(DWORD
);
56 static int WIN32_StoreKey(const char *, DWORD
, unsigned char *, int);
57 static int WIN32_create_key(void);
58 static void WIN32_build_argv (char *);
60 extern "C" void WINAPI
SquidWinSvcMain(DWORD
, char **);
62 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
63 void Squid_Win32InvalidParameterHandler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
65 static int Win32SockInit(void);
66 static void Win32SockCleanup(void);
67 SQUIDCEXTERN LPCRITICAL_SECTION dbg_mutex
;
68 void WIN32_ExceptionHandlerCleanup(void);
69 static int s_iInitCount
= 0;
70 static HANDLE NotifyAddrChange_thread
= INVALID_HANDLE_VALUE
;
72 static int Squid_Aborting
= 0;
74 #undef NotifyAddrChange
75 typedef DWORD(WINAPI
* PFNotifyAddrChange
) (OUT PHANDLE
, IN LPOVERLAPPED
);
76 #define NOTIFYADDRCHANGE "NotifyAddrChange"
79 static SERVICE_STATUS svcStatus
;
80 static SERVICE_STATUS_HANDLE svcHandle
;
81 static int WIN32_argc
;
82 static char ** WIN32_argv
;
83 static char * WIN32_module_name
;
85 #define VENDOR "squid-cache.org"
86 static char VENDORString
[] = VENDOR
;
87 #define SOFTWARENAME PACKAGE_NAME
88 static char SOFTWARENAMEString
[] = SOFTWARENAME
;
89 #define SOFTWARE "SOFTWARE"
90 static char SOFTWAREString
[] = SOFTWARE
;
91 #define COMMANDLINE "CommandLine"
92 #define CONFIGFILE "ConfigFile"
93 #undef ChangeServiceConfig2
94 typedef BOOL (WINAPI
* PFChangeServiceConfig2
) (SC_HANDLE
, DWORD
, LPVOID
);
96 #define CHANGESERVICECONFIG2 "ChangeServiceConfig2W"
98 #define CHANGESERVICECONFIG2 "ChangeServiceConfig2A"
100 static SC_ACTION Squid_SCAction
[] = { { SC_ACTION_RESTART
, 60000 } };
101 static char Squid_ServiceDescriptionString
[] = SOFTWARENAME
" " VERSION
" WWW Proxy Server";
102 static SERVICE_DESCRIPTION Squid_ServiceDescription
= { Squid_ServiceDescriptionString
};
103 static SERVICE_FAILURE_ACTIONS Squid_ServiceFailureActions
= { INFINITE
, NULL
, NULL
, 1, Squid_SCAction
};
104 static char REGKEY
[256]=SOFTWARE
"\\"VENDOR
"\\"SOFTWARENAME
"\\";
105 static char *keys
[] = {
106 SOFTWAREString
, /* key[0] */
107 VENDORString
, /* key[1] */
108 SOFTWARENAMEString
, /* key[2] */
114 /* ====================================================================== */
115 /* LOCAL FUNCTIONS */
116 /* ====================================================================== */
118 #if USE_WIN32_SERVICE
120 WIN32_create_key(void)
128 hKey
= HKEY_LOCAL_MACHINE
;
132 /* Walk the tree, creating at each stage if necessary */
134 while (keys
[index
]) {
135 unsigned long result
;
136 rv
= RegCreateKeyEx(hKey
, keys
[index
], /* subkey */
139 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &hKeyNext
, &result
);
141 if (rv
!= ERROR_SUCCESS
) {
142 fprintf(stderr
, "RegCreateKeyEx(%s),%d\n", keys
[index
], (int) rv
);
146 /* Close the old key */
147 rv
= RegCloseKey(hKey
);
149 if (rv
!= ERROR_SUCCESS
) {
150 fprintf(stderr
, "RegCloseKey %d\n", (int) rv
);
153 /* Keep error status from RegCreateKeyEx, if any */
166 if (keys
[index
] == NULL
) {
167 /* Close the final key we opened, if we walked the entire
170 rv
= RegCloseKey(hKey
);
172 if (rv
!= ERROR_SUCCESS
) {
173 fprintf(stderr
, "RegCloseKey %d\n", (int) rv
);
176 /* Keep error status from RegCreateKeyEx, if any */
186 WIN32_StoreKey(const char *key
, DWORD type
, unsigned char *value
,
193 rv
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, REGKEY
, 0, KEY_WRITE
, &hKey
);
195 if (rv
== ERROR_FILE_NOT_FOUND
) {
196 /* Key could not be opened -- try to create it
199 if (WIN32_create_key() < 0) {
200 /* Creation failed (error already reported) */
204 /* Now it has been created we should be able to open it
206 rv
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, REGKEY
, 0, KEY_WRITE
, &hKey
);
208 if (rv
== ERROR_FILE_NOT_FOUND
) {
209 fprintf(stderr
, "Registry does not contain key %s after creation\n",
215 if (rv
!= ERROR_SUCCESS
) {
216 fprintf(stderr
, "RegOpenKeyEx HKLM\\%s, %d\n", REGKEY
, (int) rv
);
220 /* Now set the value and data */
221 rv
= RegSetValueEx(hKey
, key
, /* value key name */
224 value
, /* value data */
225 (DWORD
) value_size
); /* for size of "value" */
227 retval
= 0; /* Return value */
229 if (rv
!= ERROR_SUCCESS
) {
230 fprintf(stderr
, "RegQueryValueEx(key %s),%d\n", key
, (int) rv
);
233 fprintf(stderr
, "Registry stored HKLM\\%s\\%s value %s\n",
236 type
== REG_SZ
? value
: (unsigned char *) "(not displayable)");
239 /* Make sure we close the key even if there was an error storing
242 rv
= RegCloseKey(hKey
);
244 if (rv
!= ERROR_SUCCESS
) {
245 fprintf(stderr
, "RegCloseKey HKLM\\%s, %d\n", REGKEY
, (int) rv
);
248 /* Keep error status from RegQueryValueEx, if any */
256 /* Build argv, argc from string passed from Windows. */
257 static void WIN32_build_argv(char *cmd
)
263 WIN32_argv
= (char **) xmalloc ((WIN32_argc
+1) * sizeof (char *));
264 WIN32_argv
[0]=xstrdup(WIN32_module_name
);
265 /* Scan command line until there is nothing left. */
270 if (xisspace(*cmd
)) {
275 /* Found the beginning of an argument. */
279 ++cmd
; /* Skip over this character */
281 if (xisspace(*cmd
)) /* End of argument if space */
286 *cmd
++ = '\0'; /* Terminate `word' */
288 /* See if we need to allocate more space for argv */
289 if (WIN32_argc
>= argvlen
) {
290 argvlen
= WIN32_argc
+ 1;
291 WIN32_argv
= (char **) xrealloc (WIN32_argv
, (1 + argvlen
) * sizeof (char *));
294 /* Add word to argv file. */
295 WIN32_argv
[WIN32_argc
++] = word
;
298 WIN32_argv
[WIN32_argc
] = NULL
;
301 #endif /* USE_WIN32_SERVICE */
306 OSVERSIONINFOEX osvi
;
307 BOOL bOsVersionInfoEx
;
309 safe_free(WIN32_OS_string
);
310 memset(&osvi
, '\0', sizeof(OSVERSIONINFOEX
));
311 /* Try calling GetVersionEx using the OSVERSIONINFOEX structure.
312 * If that fails, try using the OSVERSIONINFO structure.
315 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOEX
);
317 if (!(bOsVersionInfoEx
= GetVersionEx((OSVERSIONINFO
*) & osvi
))) {
318 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
319 if (!GetVersionEx((OSVERSIONINFO
*) & osvi
))
322 switch (osvi
.dwPlatformId
) {
323 case VER_PLATFORM_WIN32_NT
:
324 if (osvi
.dwMajorVersion
<= 4) {
325 WIN32_OS_string
= xstrdup("Windows NT");
326 return _WIN_OS_WINNT
;
328 if ((osvi
.dwMajorVersion
== 5) && (osvi
.dwMinorVersion
== 0)) {
329 WIN32_OS_string
= xstrdup("Windows 2000");
330 return _WIN_OS_WIN2K
;
332 if ((osvi
.dwMajorVersion
== 5) && (osvi
.dwMinorVersion
== 1)) {
333 WIN32_OS_string
= xstrdup("Windows XP");
334 return _WIN_OS_WINXP
;
336 if ((osvi
.dwMajorVersion
== 5) && (osvi
.dwMinorVersion
== 2)) {
337 WIN32_OS_string
= xstrdup("Windows Server 2003");
338 return _WIN_OS_WINNET
;
340 if ((osvi
.dwMajorVersion
== 6) && (osvi
.dwMinorVersion
== 0)) {
341 if (osvi
.wProductType
== VER_NT_WORKSTATION
)
342 WIN32_OS_string
= xstrdup("Windows Vista");
344 WIN32_OS_string
= xstrdup("Windows Server 2008");
345 return _WIN_OS_WINLON
;
347 if ((osvi
.dwMajorVersion
== 6) && (osvi
.dwMinorVersion
== 1)) {
348 if (osvi
.wProductType
== VER_NT_WORKSTATION
)
349 WIN32_OS_string
= xstrdup("Windows 7");
351 WIN32_OS_string
= xstrdup("Windows Server 2008 R2");
354 if (((osvi
.dwMajorVersion
> 6)) || ((osvi
.dwMajorVersion
== 6) && (osvi
.dwMinorVersion
> 1))) {
355 if (osvi
.wProductType
== VER_NT_WORKSTATION
)
356 WIN32_OS_string
= xstrdup("Unknown Windows version, assuming Windows 7 capabilities");
358 WIN32_OS_string
= xstrdup("Unknown Windows version, assuming Windows Server 2008 R2 capabilities");
362 case VER_PLATFORM_WIN32_WINDOWS
:
363 if ((osvi
.dwMajorVersion
== 4) && (osvi
.dwMinorVersion
== 0)) {
364 WIN32_OS_string
= xstrdup("Windows 95");
365 return _WIN_OS_WIN95
;
367 if ((osvi
.dwMajorVersion
== 4) && (osvi
.dwMinorVersion
== 10)) {
368 WIN32_OS_string
= xstrdup("Windows 98");
369 return _WIN_OS_WIN98
;
371 if ((osvi
.dwMajorVersion
== 4) && (osvi
.dwMinorVersion
== 90)) {
372 WIN32_OS_string
= xstrdup("Windows Me");
373 return _WIN_OS_WINME
;
376 case VER_PLATFORM_WIN32s
:
377 WIN32_OS_string
= xstrdup("Windows 3.1 with WIN32S");
378 return _WIN_OS_WIN32S
;
384 WIN32_OS_string
= xstrdup("Unknown Windows system");
385 return _WIN_OS_UNKNOWN
;
388 /* ====================================================================== */
389 /* PUBLIC FUNCTIONS */
390 /* ====================================================================== */
395 #if USE_WIN32_SERVICE
396 svcStatus
.dwWin32ExitCode
= ERROR_SERVICE_SPECIFIC_ERROR
;
397 svcStatus
.dwServiceSpecificExitCode
= 1;
405 WIN32_IpAddrChangeMonitorExit()
407 DWORD status
= ERROR_SUCCESS
;
409 if (NotifyAddrChange_thread
!= INVALID_HANDLE_VALUE
) {
410 TerminateThread(NotifyAddrChange_thread
, status
);
411 CloseHandle(NotifyAddrChange_thread
);
419 #if USE_WIN32_SERVICE
421 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
422 if (!Squid_Aborting
) {
423 svcStatus
.dwCurrentState
= SERVICE_STOPPED
;
424 SetServiceStatus(svcHandle
, &svcStatus
);
430 DeleteCriticalSection(dbg_mutex
);
432 WIN32_ExceptionHandlerCleanup();
433 WIN32_IpAddrChangeMonitorExit();
438 WIN32_IpAddrChangeMonitor(LPVOID lpParam
)
441 HMODULE IPHLPAPIHandle
;
442 PFNotifyAddrChange NotifyAddrChange
;
444 if ((IPHLPAPIHandle
= GetModuleHandle("IPHLPAPI")) == NULL
)
445 IPHLPAPIHandle
= LoadLibrary("IPHLPAPI");
446 NotifyAddrChange
= (PFNotifyAddrChange
) GetProcAddress(IPHLPAPIHandle
, NOTIFYADDRCHANGE
);
449 Result
= NotifyAddrChange(NULL
, NULL
);
450 if (Result
!= NO_ERROR
) {
451 debugs(1, DBG_IMPORTANT
, "NotifyAddrChange error " << Result
);
454 debugs(1, DBG_IMPORTANT
, "Notification of IP address change received, requesting Squid reconfiguration ...");
461 WIN32_IpAddrChangeMonitorInit()
463 DWORD status
= ERROR_SUCCESS
;
464 DWORD threadID
= 0, ThrdParam
= 0;
466 if ((WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) && (Config
.onoff
.WIN32_IpAddrChangeMonitor
)) {
467 NotifyAddrChange_thread
= CreateThread(NULL
, 0, WIN32_IpAddrChangeMonitor
,
468 &ThrdParam
, 0, &threadID
);
469 if (NotifyAddrChange_thread
== NULL
) {
470 status
= GetLastError();
471 NotifyAddrChange_thread
= INVALID_HANDLE_VALUE
;
472 debugs(1, DBG_IMPORTANT
, "Failed to start IP monitor thread.");
474 debugs(1, 2, "Starting IP monitor thread [" << threadID
<< "] ...");
479 int WIN32_Subsystem_Init(int * argc
, char *** argv
)
481 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
482 _invalid_parameter_handler oldHandler
, newHandler
;
485 WIN32_OS_version
= GetOSVersion();
487 if ((WIN32_OS_version
== _WIN_OS_UNKNOWN
) || (WIN32_OS_version
== _WIN_OS_WIN32S
))
490 if (atexit(WIN32_Exit
) != 0)
493 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
495 newHandler
= Squid_Win32InvalidParameterHandler
;
497 oldHandler
= _set_invalid_parameter_handler(newHandler
);
499 _CrtSetReportMode(_CRT_ASSERT
, 0);
502 #if USE_WIN32_SERVICE
504 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
508 if (signal(SIGABRT
, WIN32_Abort
) == SIG_ERR
)
511 /* Register the service Handler function */
512 svcHandle
= RegisterServiceCtrlHandler(WIN32_Service_name
, WIN32_svcHandler
);
517 /* Set Process work dir to directory cointaining squid.exe */
518 GetModuleFileName(NULL
, path
, 512);
520 WIN32_module_name
=xstrdup(path
);
522 path
[strlen(path
) - 10] = '\0';
524 if (SetCurrentDirectory(path
) == 0)
527 safe_free(ConfigFile
);
529 /* get config file from Windows Registry */
530 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, REGKEY
, 0, KEY_QUERY_VALUE
, &hndKey
) == ERROR_SUCCESS
) {
534 Result
= RegQueryValueEx(hndKey
, CONFIGFILE
, NULL
, &Type
, NULL
, &Size
);
536 if (Result
== ERROR_SUCCESS
&& Size
) {
537 ConfigFile
= static_cast<char *>(xmalloc(Size
));
538 RegQueryValueEx(hndKey
, CONFIGFILE
, NULL
, &Type
, (unsigned char *)ConfigFile
, &Size
);
540 ConfigFile
= xstrdup(DefaultConfigFile
);
546 Result
= RegQueryValueEx(hndKey
, COMMANDLINE
, NULL
, &Type
, NULL
, &Size
);
548 if (Result
== ERROR_SUCCESS
&& Size
) {
549 WIN32_Service_Command_Line
= static_cast<char *>(xmalloc(Size
));
550 RegQueryValueEx(hndKey
, COMMANDLINE
, NULL
, &Type
, (unsigned char *)WIN32_Service_Command_Line
, &Size
);
552 WIN32_Service_Command_Line
= xstrdup("");
556 ConfigFile
= xstrdup(DefaultConfigFile
);
557 WIN32_Service_Command_Line
= xstrdup("");
560 WIN32_build_argv(WIN32_Service_Command_Line
);
563 /* Set Service Status to SERVICE_START_PENDING */
564 svcStatus
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
565 svcStatus
.dwCurrentState
= SERVICE_START_PENDING
;
566 svcStatus
.dwControlsAccepted
=
567 SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN
;
568 svcStatus
.dwWin32ExitCode
= 0;
569 svcStatus
.dwServiceSpecificExitCode
= 0;
570 svcStatus
.dwCheckPoint
= 0;
571 svcStatus
.dwWaitHint
= 10000;
572 SetServiceStatus(svcHandle
, &svcStatus
);
574 _setmaxstdio(Squid_MaxFD
);
578 #endif /* USE_WIN32_SERVICE */
579 if (Win32SockInit() < 0)
585 #if USE_WIN32_SERVICE
587 WIN32_svcstatusupdate(DWORD svcstate
, DWORD WaitHint
)
589 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
590 ++svcStatus
.dwCheckPoint
;
591 svcStatus
.dwWaitHint
= WaitHint
;
592 svcStatus
.dwCurrentState
= svcstate
;
593 SetServiceStatus(svcHandle
, &svcStatus
);
598 WIN32_svcHandler(DWORD Opcode
)
604 case _WIN_SQUID_SERVICE_CONTROL_STOP
:
606 case _WIN_SQUID_SERVICE_CONTROL_SHUTDOWN
:
607 /* Do whatever it takes to stop here. */
608 svcStatus
.dwWin32ExitCode
= 0;
609 svcStatus
.dwCurrentState
= SERVICE_STOP_PENDING
;
610 svcStatus
.dwCheckPoint
= 0;
611 svcStatus
.dwWaitHint
= 10000;
614 if (!SetServiceStatus(svcHandle
, &svcStatus
)) {
615 status
= GetLastError();
616 debugs(1, DBG_IMPORTANT
, "SetServiceStatus error " << status
);
619 debugs(1, DBG_IMPORTANT
, "Leaving Squid service");
622 case _WIN_SQUID_SERVICE_CONTROL_INTERROGATE
:
623 /* Fall through to send current status. */
625 if (!SetServiceStatus(svcHandle
, &svcStatus
)) {
626 status
= GetLastError();
627 debugs(1, DBG_IMPORTANT
, "SetServiceStatus error " << status
);
632 case _WIN_SQUID_SERVICE_CONTROL_ROTATE
:
633 rotate_logs(SIGUSR1
);
636 case _WIN_SQUID_SERVICE_CONTROL_RECONFIGURE
:
640 case _WIN_SQUID_SERVICE_CONTROL_DEBUG
:
641 sigusr2_handle(SIGUSR2
);
644 case _WIN_SQUID_SERVICE_CONTROL_INTERRUPT
:
645 /* Do whatever it takes to stop here. */
646 svcStatus
.dwWin32ExitCode
= 0;
647 svcStatus
.dwCurrentState
= SERVICE_STOP_PENDING
;
648 svcStatus
.dwCheckPoint
= 0;
649 svcStatus
.dwWaitHint
= 10000;
652 if (!SetServiceStatus(svcHandle
, &svcStatus
)) {
653 status
= GetLastError();
654 debugs(1, DBG_IMPORTANT
, "SetServiceStatus error " << status
);
657 debugs(1, DBG_IMPORTANT
, "Leaving Squid service");
661 debugs(1, DBG_IMPORTANT
, "Unrecognized opcode " << Opcode
);
668 WIN32_RemoveService()
670 SC_HANDLE schService
;
671 SC_HANDLE schSCManager
;
673 if (!WIN32_Service_name
)
674 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
676 strcat(REGKEY
, WIN32_Service_name
);
678 keys
[4] = WIN32_Service_name
;
680 schSCManager
= OpenSCManager(NULL
, /* machine (NULL == local) */
681 NULL
, /* database (NULL == default) */
682 SC_MANAGER_ALL_ACCESS
/* access required */
686 fprintf(stderr
, "OpenSCManager failed\n");
688 schService
= OpenService(schSCManager
, WIN32_Service_name
, SERVICE_ALL_ACCESS
);
690 if (schService
== NULL
)
691 fprintf(stderr
, "OpenService failed\n");
693 /* Could not open the service */
695 /* try to stop the service */
697 if (ControlService(schService
, _WIN_SQUID_SERVICE_CONTROL_STOP
,
701 while (QueryServiceStatus(schService
, &svcStatus
)) {
702 if (svcStatus
.dwCurrentState
== SERVICE_STOP_PENDING
)
709 /* now remove the service */
710 if (DeleteService(schService
) == 0)
711 fprintf(stderr
, "DeleteService failed.\n");
713 printf("Service %s deleted successfully.\n",
716 CloseServiceHandle(schService
);
719 CloseServiceHandle(schSCManager
);
724 WIN32_SetServiceCommandLine()
726 if (!WIN32_Service_name
)
727 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
729 strcat(REGKEY
, WIN32_Service_name
);
731 keys
[4] = WIN32_Service_name
;
733 /* Now store the Service Command Line in the registry */
734 WIN32_StoreKey(COMMANDLINE
, REG_SZ
, (unsigned char *) WIN32_Command_Line
, strlen(WIN32_Command_Line
) + 1);
738 WIN32_InstallService()
740 SC_HANDLE schService
;
741 SC_HANDLE schSCManager
;
742 char ServicePath
[512];
746 if (!WIN32_Service_name
)
747 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
749 strcat(REGKEY
, WIN32_Service_name
);
751 keys
[4] = WIN32_Service_name
;
753 if ((lenpath
= GetModuleFileName(NULL
, ServicePath
, 512)) == 0) {
754 fprintf(stderr
, "Can't get executable path\n");
758 snprintf(szPath
, sizeof(szPath
), "%s %s:%s", ServicePath
, _WIN_SQUID_SERVICE_OPTION
, WIN32_Service_name
);
759 schSCManager
= OpenSCManager(NULL
, /* machine (NULL == local) */
760 NULL
, /* database (NULL == default) */
761 SC_MANAGER_ALL_ACCESS
/* access required */
765 fprintf(stderr
, "OpenSCManager failed\n");
768 schService
= CreateService(schSCManager
, /* SCManager database */
769 WIN32_Service_name
, /* name of service */
770 WIN32_Service_name
, /* name to display */
771 SERVICE_ALL_ACCESS
, /* desired access */
772 SERVICE_WIN32_OWN_PROCESS
, /* service type */
773 SERVICE_AUTO_START
, /* start type */
774 SERVICE_ERROR_NORMAL
, /* error control type */
775 (const char *) szPath
, /* service's binary */
776 NULL
, /* no load ordering group */
777 NULL
, /* no tag identifier */
778 "Tcpip\0AFD\0", /* dependencies */
779 NULL
, /* LocalSystem account */
780 NULL
); /* no password */
783 if (WIN32_OS_version
> _WIN_OS_WINNT
) {
784 HMODULE ADVAPI32Handle
;
785 PFChangeServiceConfig2 ChangeServiceConfig2
;
786 DWORD dwInfoLevel
= SERVICE_CONFIG_DESCRIPTION
;
788 ADVAPI32Handle
= GetModuleHandle("advapi32");
789 ChangeServiceConfig2
= (PFChangeServiceConfig2
) GetProcAddress(ADVAPI32Handle
, CHANGESERVICECONFIG2
);
790 ChangeServiceConfig2(schService
, dwInfoLevel
, &Squid_ServiceDescription
);
791 dwInfoLevel
= SERVICE_CONFIG_FAILURE_ACTIONS
;
792 ChangeServiceConfig2(schService
, dwInfoLevel
, &Squid_ServiceFailureActions
);
795 CloseServiceHandle(schService
);
796 /* Now store the config file location in the registry */
799 ConfigFile
= xstrdup(DefaultConfigFile
);
801 WIN32_StoreKey(CONFIGFILE
, REG_SZ
, (unsigned char *) ConfigFile
, strlen(ConfigFile
) + 1);
803 printf("Squid Cache version %s for %s\n", version_string
,
806 printf("installed successfully as %s Windows System Service.\n",
810 ("To run, start it from the Services Applet of Control Panel.\n");
812 printf("Don't forget to edit squid.conf before starting it.\n\n");
814 fprintf(stderr
, "CreateService failed\n");
818 CloseServiceHandle(schSCManager
);
823 WIN32_sendSignal(int WIN32_signal
)
825 SERVICE_STATUS ssStatus
;
826 DWORD fdwAccess
, fdwControl
;
827 SC_HANDLE schService
;
828 SC_HANDLE schSCManager
;
830 if (!WIN32_Service_name
)
831 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
833 schSCManager
= OpenSCManager(NULL
, /* machine (NULL == local) */
834 NULL
, /* database (NULL == default) */
835 SC_MANAGER_ALL_ACCESS
/* access required */
839 fprintf(stderr
, "OpenSCManager failed\n");
843 /* The required service object access depends on the control. */
844 switch (WIN32_signal
) {
846 case 0: /* SIGNULL */
847 fdwAccess
= SERVICE_INTERROGATE
;
848 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_INTERROGATE
;
852 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
853 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_ROTATE
;
857 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
858 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_DEBUG
;
862 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
863 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_RECONFIGURE
;
867 fdwAccess
= SERVICE_STOP
;
868 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_STOP
;
874 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
875 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_INTERRUPT
;
882 /* Open a handle to the service. */
883 schService
= OpenService(schSCManager
, /* SCManager database */
884 WIN32_Service_name
, /* name of service */
885 fdwAccess
); /* specify access */
887 if (schService
== NULL
) {
888 fprintf(stderr
, "%s: ERROR: Could not open Service %s\n", APP_SHORTNAME
,
892 /* Send a control value to the service. */
894 if (!ControlService(schService
, /* handle of service */
895 fdwControl
, /* control value to send */
896 &ssStatus
)) { /* address of status info */
897 fprintf(stderr
, "%s: ERROR: Could not Control Service %s\n",
898 APP_SHORTNAME
, WIN32_Service_name
);
901 /* Print the service status. */
902 printf("\nStatus of %s Service:\n", WIN32_Service_name
);
903 printf(" Service Type: 0x%lx\n", ssStatus
.dwServiceType
);
904 printf(" Current State: 0x%lx\n", ssStatus
.dwCurrentState
);
905 printf(" Controls Accepted: 0x%lx\n", ssStatus
.dwControlsAccepted
);
906 printf(" Exit Code: %ld\n", ssStatus
.dwWin32ExitCode
);
907 printf(" Service Specific Exit Code: %ld\n",
908 ssStatus
.dwServiceSpecificExitCode
);
909 printf(" Check Point: %ld\n", ssStatus
.dwCheckPoint
);
910 printf(" Wait Hint: %ld\n", ssStatus
.dwWaitHint
);
913 CloseServiceHandle(schService
);
916 CloseServiceHandle(schSCManager
);
919 int main(int argc
, char **argv
)
921 SERVICE_TABLE_ENTRY DispatchTable
[] = {
922 {NULL
, SquidWinSvcMain
},
926 char stderr_path
[256];
928 if ((argc
== 2) && strstr(argv
[1], _WIN_SQUID_SERVICE_OPTION
)) {
929 strcpy(stderr_path
, argv
[0]);
930 strcat(stderr_path
,".log");
931 freopen(stderr_path
, "w", stderr
);
932 setmode(fileno(stderr
), O_TEXT
);
933 WIN32_run_mode
= _WIN_SQUID_RUN_MODE_SERVICE
;
935 if (!(c
=strchr(argv
[1],':'))) {
936 fprintf(stderr
, "Bad Service Parameter: %s\n", argv
[1]);
940 WIN32_Service_name
= xstrdup(c
+1);
941 DispatchTable
[0].lpServiceName
=WIN32_Service_name
;
942 strcat(REGKEY
, WIN32_Service_name
);
943 keys
[4] = WIN32_Service_name
;
945 if (!StartServiceCtrlDispatcher(DispatchTable
)) {
946 fprintf(stderr
, "StartServiceCtrlDispatcher error = %ld\n",
951 WIN32_run_mode
= _WIN_SQUID_RUN_MODE_INTERACTIVE
;
954 return SquidMain(argc
, argv
);
960 #endif /* USE_WIN32_SERVICE */
962 static int Win32SockInit(void)
964 int iVersionRequested
;
967 int optlen
= sizeof(opt
);
969 if (s_iInitCount
> 0) {
972 } else if (s_iInitCount
< 0)
973 return (s_iInitCount
);
975 /* s_iInitCount == 0. Do the initailization */
976 iVersionRequested
= MAKEWORD(2, 0);
978 err
= WSAStartup((WORD
) iVersionRequested
, &wsaData
);
982 return (s_iInitCount
);
985 if (LOBYTE(wsaData
.wVersion
) != 2 ||
986 HIBYTE(wsaData
.wVersion
) != 0) {
989 return (s_iInitCount
);
992 if (WIN32_OS_version
!=_WIN_OS_WINNT
) {
993 if (::getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *)&opt
, &optlen
)) {
996 return (s_iInitCount
);
998 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
1000 if (::setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
)) {
1003 return (s_iInitCount
);
1008 WIN32_Socks_initialized
= 1;
1010 return (s_iInitCount
);
1013 static void Win32SockCleanup(void)
1015 if (--s_iInitCount
== 0)
1021 void Squid_Win32InvalidParameterHandler(const wchar_t* expression
, const wchar_t* function
, const wchar_t* file
, unsigned int line
, uintptr_t pReserved
)