6 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
7 * inspired by previous work by Romeo Anghelache & Eric Stern.
9 * SQUID Web Proxy Cache http://www.squid-cache.org/
10 * ----------------------------------------------------------
12 * Squid is the result of efforts by numerous individuals from
13 * the Internet community; see the CONTRIBUTORS file for full
14 * details. Many organizations have provided support for Squid's
15 * development; see the SPONSORS file for full details. Squid is
16 * Copyrighted (C) 2001 by the Regents of the University of
17 * California; see the COPYRIGHT file for full details. Squid
18 * incorporates software developed and/or copyrighted by other
19 * sources; see the CREDITS file for full details.
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38 #include "squid_windows.h"
45 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
50 static unsigned int GetOSVersion();
51 void WIN32_svcstatusupdate(DWORD
, DWORD
);
52 void WINAPI
WIN32_svcHandler(DWORD
);
54 static int WIN32_StoreKey(const char *, DWORD
, unsigned char *, int);
55 static int WIN32_create_key(void);
56 static void WIN32_build_argv (char *);
58 extern "C" void WINAPI
SquidWinSvcMain(DWORD
, char **);
60 #if defined(_SQUID_MSWIN_)
61 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
62 void Squid_Win32InvalidParameterHandler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
64 static int Win32SockInit(void);
65 static void Win32SockCleanup(void);
66 SQUIDCEXTERN LPCRITICAL_SECTION dbg_mutex
;
67 void WIN32_ExceptionHandlerCleanup(void);
68 static int s_iInitCount
= 0;
69 static HANDLE NotifyAddrChange_thread
= INVALID_HANDLE_VALUE
;
70 #endif /* _SQUID_MSWIN_ */
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;
406 WIN32_IpAddrChangeMonitorExit()
408 DWORD status
= ERROR_SUCCESS
;
410 if (NotifyAddrChange_thread
!= INVALID_HANDLE_VALUE
) {
411 TerminateThread(NotifyAddrChange_thread
, status
);
412 CloseHandle(NotifyAddrChange_thread
);
423 #if USE_WIN32_SERVICE
425 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
426 if (!Squid_Aborting
) {
427 svcStatus
.dwCurrentState
= SERVICE_STOPPED
;
428 SetServiceStatus(svcHandle
, &svcStatus
);
435 DeleteCriticalSection(dbg_mutex
);
437 WIN32_ExceptionHandlerCleanup();
438 WIN32_IpAddrChangeMonitorExit();
447 WIN32_IpAddrChangeMonitor(LPVOID lpParam
)
450 HMODULE IPHLPAPIHandle
;
451 PFNotifyAddrChange NotifyAddrChange
;
453 if ((IPHLPAPIHandle
= GetModuleHandle("IPHLPAPI")) == NULL
)
454 IPHLPAPIHandle
= LoadLibrary("IPHLPAPI");
455 NotifyAddrChange
= (PFNotifyAddrChange
) GetProcAddress(IPHLPAPIHandle
, NOTIFYADDRCHANGE
);
458 Result
= NotifyAddrChange(NULL
, NULL
);
459 if (Result
!= NO_ERROR
) {
460 debugs(1, 1, "NotifyAddrChange error " << Result
);
463 debugs(1, 1, "Notification of IP address change received, requesting Squid reconfiguration ...");
470 WIN32_IpAddrChangeMonitorInit()
472 DWORD status
= ERROR_SUCCESS
;
473 DWORD threadID
= 0, ThrdParam
= 0;
475 if ((WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) && (Config
.onoff
.WIN32_IpAddrChangeMonitor
)) {
476 NotifyAddrChange_thread
= CreateThread(NULL
, 0, WIN32_IpAddrChangeMonitor
,
477 &ThrdParam
, 0, &threadID
);
478 if (NotifyAddrChange_thread
== NULL
) {
479 status
= GetLastError();
480 NotifyAddrChange_thread
= INVALID_HANDLE_VALUE
;
481 debugs(1, 1, "Failed to start IP monitor thread.");
483 debugs(1, 2, "Starting IP monitor thread [" << threadID
<< "] ...");
487 #endif /* _SQUID_MSWIN_ */
489 int WIN32_Subsystem_Init(int * argc
, char *** argv
)
491 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
492 _invalid_parameter_handler oldHandler
, newHandler
;
495 WIN32_OS_version
= GetOSVersion();
497 if ((WIN32_OS_version
== _WIN_OS_UNKNOWN
) || (WIN32_OS_version
== _WIN_OS_WIN32S
))
500 if (atexit(WIN32_Exit
) != 0)
503 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
505 newHandler
= Squid_Win32InvalidParameterHandler
;
507 oldHandler
= _set_invalid_parameter_handler(newHandler
);
509 _CrtSetReportMode(_CRT_ASSERT
, 0);
512 #if USE_WIN32_SERVICE
514 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
518 if (signal(SIGABRT
, WIN32_Abort
) == SIG_ERR
)
521 /* Register the service Handler function */
522 svcHandle
= RegisterServiceCtrlHandler(WIN32_Service_name
, WIN32_svcHandler
);
527 /* Set Process work dir to directory cointaining squid.exe */
528 GetModuleFileName(NULL
, path
, 512);
530 WIN32_module_name
=xstrdup(path
);
532 path
[strlen(path
) - 10] = '\0';
534 if (SetCurrentDirectory(path
) == 0)
537 safe_free(ConfigFile
);
539 /* get config file from Windows Registry */
540 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, REGKEY
, 0, KEY_QUERY_VALUE
, &hndKey
) == ERROR_SUCCESS
) {
544 Result
= RegQueryValueEx(hndKey
, CONFIGFILE
, NULL
, &Type
, NULL
, &Size
);
546 if (Result
== ERROR_SUCCESS
&& Size
) {
547 ConfigFile
= static_cast<char *>(xmalloc(Size
));
548 RegQueryValueEx(hndKey
, CONFIGFILE
, NULL
, &Type
, (unsigned char *)ConfigFile
, &Size
);
550 ConfigFile
= xstrdup(DefaultConfigFile
);
556 Result
= RegQueryValueEx(hndKey
, COMMANDLINE
, NULL
, &Type
, NULL
, &Size
);
558 if (Result
== ERROR_SUCCESS
&& Size
) {
559 WIN32_Service_Command_Line
= static_cast<char *>(xmalloc(Size
));
560 RegQueryValueEx(hndKey
, COMMANDLINE
, NULL
, &Type
, (unsigned char *)WIN32_Service_Command_Line
, &Size
);
562 WIN32_Service_Command_Line
= xstrdup("");
566 ConfigFile
= xstrdup(DefaultConfigFile
);
567 WIN32_Service_Command_Line
= xstrdup("");
570 WIN32_build_argv(WIN32_Service_Command_Line
);
573 /* Set Service Status to SERVICE_START_PENDING */
574 svcStatus
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
575 svcStatus
.dwCurrentState
= SERVICE_START_PENDING
;
576 svcStatus
.dwControlsAccepted
=
577 SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN
;
578 svcStatus
.dwWin32ExitCode
= 0;
579 svcStatus
.dwServiceSpecificExitCode
= 0;
580 svcStatus
.dwCheckPoint
= 0;
581 svcStatus
.dwWaitHint
= 10000;
582 SetServiceStatus(svcHandle
, &svcStatus
);
585 _setmaxstdio(Squid_MaxFD
);
590 #endif /* USE_WIN32_SERVICE */
592 if (Win32SockInit() < 0)
600 #if USE_WIN32_SERVICE
602 WIN32_svcstatusupdate(DWORD svcstate
, DWORD WaitHint
)
604 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
605 svcStatus
.dwCheckPoint
++;
606 svcStatus
.dwWaitHint
= WaitHint
;
607 svcStatus
.dwCurrentState
= svcstate
;
608 SetServiceStatus(svcHandle
, &svcStatus
);
613 WIN32_svcHandler(DWORD Opcode
)
619 case _WIN_SQUID_SERVICE_CONTROL_STOP
:
621 case _WIN_SQUID_SERVICE_CONTROL_SHUTDOWN
:
622 /* Do whatever it takes to stop here. */
623 svcStatus
.dwWin32ExitCode
= 0;
624 svcStatus
.dwCurrentState
= SERVICE_STOP_PENDING
;
625 svcStatus
.dwCheckPoint
= 0;
626 svcStatus
.dwWaitHint
= 10000;
629 if (!SetServiceStatus(svcHandle
, &svcStatus
)) {
630 status
= GetLastError();
631 debugs(1, 1, "SetServiceStatus error " << status
);
634 debugs(1, 1, "Leaving Squid service");
637 case _WIN_SQUID_SERVICE_CONTROL_INTERROGATE
:
638 /* Fall through to send current status. */
640 if (!SetServiceStatus(svcHandle
, &svcStatus
)) {
641 status
= GetLastError();
642 debugs(1, 1, "SetServiceStatus error " << status
);
647 case _WIN_SQUID_SERVICE_CONTROL_ROTATE
:
648 rotate_logs(SIGUSR1
);
651 case _WIN_SQUID_SERVICE_CONTROL_RECONFIGURE
:
655 case _WIN_SQUID_SERVICE_CONTROL_DEBUG
:
656 sigusr2_handle(SIGUSR2
);
659 case _WIN_SQUID_SERVICE_CONTROL_INTERRUPT
:
660 /* Do whatever it takes to stop here. */
661 svcStatus
.dwWin32ExitCode
= 0;
662 svcStatus
.dwCurrentState
= SERVICE_STOP_PENDING
;
663 svcStatus
.dwCheckPoint
= 0;
664 svcStatus
.dwWaitHint
= 10000;
667 if (!SetServiceStatus(svcHandle
, &svcStatus
)) {
668 status
= GetLastError();
669 debugs(1, 1, "SetServiceStatus error " << status
);
672 debugs(1, 1, "Leaving Squid service");
676 debugs(1, 1, "Unrecognized opcode " << Opcode
);
683 WIN32_RemoveService()
685 SC_HANDLE schService
;
686 SC_HANDLE schSCManager
;
688 if (!WIN32_Service_name
)
689 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
691 strcat(REGKEY
, WIN32_Service_name
);
693 keys
[4] = WIN32_Service_name
;
695 schSCManager
= OpenSCManager(NULL
, /* machine (NULL == local) */
696 NULL
, /* database (NULL == default) */
697 SC_MANAGER_ALL_ACCESS
/* access required */
701 fprintf(stderr
, "OpenSCManager failed\n");
703 schService
= OpenService(schSCManager
, WIN32_Service_name
, SERVICE_ALL_ACCESS
);
705 if (schService
== NULL
)
706 fprintf(stderr
, "OpenService failed\n");
708 /* Could not open the service */
710 /* try to stop the service */
712 if (ControlService(schService
, _WIN_SQUID_SERVICE_CONTROL_STOP
,
716 while (QueryServiceStatus(schService
, &svcStatus
)) {
717 if (svcStatus
.dwCurrentState
== SERVICE_STOP_PENDING
)
724 /* now remove the service */
725 if (DeleteService(schService
) == 0)
726 fprintf(stderr
, "DeleteService failed.\n");
728 printf("Service %s deleted successfully.\n",
731 CloseServiceHandle(schService
);
734 CloseServiceHandle(schSCManager
);
739 WIN32_SetServiceCommandLine()
741 if (!WIN32_Service_name
)
742 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
744 strcat(REGKEY
, WIN32_Service_name
);
746 keys
[4] = WIN32_Service_name
;
748 /* Now store the Service Command Line in the registry */
749 WIN32_StoreKey(COMMANDLINE
, REG_SZ
, (unsigned char *) WIN32_Command_Line
, strlen(WIN32_Command_Line
) + 1);
753 WIN32_InstallService()
755 SC_HANDLE schService
;
756 SC_HANDLE schSCManager
;
757 char ServicePath
[512];
761 if (!WIN32_Service_name
)
762 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
764 strcat(REGKEY
, WIN32_Service_name
);
766 keys
[4] = WIN32_Service_name
;
768 if ((lenpath
= GetModuleFileName(NULL
, ServicePath
, 512)) == 0) {
769 fprintf(stderr
, "Can't get executable path\n");
773 snprintf(szPath
, sizeof(szPath
), "%s %s:%s", ServicePath
, _WIN_SQUID_SERVICE_OPTION
, WIN32_Service_name
);
774 schSCManager
= OpenSCManager(NULL
, /* machine (NULL == local) */
775 NULL
, /* database (NULL == default) */
776 SC_MANAGER_ALL_ACCESS
/* access required */
780 fprintf(stderr
, "OpenSCManager failed\n");
783 schService
= CreateService(schSCManager
, /* SCManager database */
784 WIN32_Service_name
, /* name of service */
785 WIN32_Service_name
, /* name to display */
786 SERVICE_ALL_ACCESS
, /* desired access */
787 SERVICE_WIN32_OWN_PROCESS
, /* service type */
788 SERVICE_AUTO_START
, /* start type */
789 SERVICE_ERROR_NORMAL
, /* error control type */
790 (const char *) szPath
, /* service's binary */
791 NULL
, /* no load ordering group */
792 NULL
, /* no tag identifier */
793 "Tcpip\0AFD\0", /* dependencies */
794 NULL
, /* LocalSystem account */
795 NULL
); /* no password */
798 if (WIN32_OS_version
> _WIN_OS_WINNT
) {
799 HMODULE ADVAPI32Handle
;
800 PFChangeServiceConfig2 ChangeServiceConfig2
;
801 DWORD dwInfoLevel
= SERVICE_CONFIG_DESCRIPTION
;
803 ADVAPI32Handle
= GetModuleHandle("advapi32");
804 ChangeServiceConfig2
= (PFChangeServiceConfig2
) GetProcAddress(ADVAPI32Handle
, CHANGESERVICECONFIG2
);
805 ChangeServiceConfig2(schService
, dwInfoLevel
, &Squid_ServiceDescription
);
806 dwInfoLevel
= SERVICE_CONFIG_FAILURE_ACTIONS
;
807 ChangeServiceConfig2(schService
, dwInfoLevel
, &Squid_ServiceFailureActions
);
810 CloseServiceHandle(schService
);
811 /* Now store the config file location in the registry */
814 ConfigFile
= xstrdup(DefaultConfigFile
);
816 WIN32_StoreKey(CONFIGFILE
, REG_SZ
, (unsigned char *) ConfigFile
, strlen(ConfigFile
) + 1);
818 printf("Squid Cache version %s for %s\n", version_string
,
821 printf("installed successfully as %s Windows System Service.\n",
825 ("To run, start it from the Services Applet of Control Panel.\n");
827 printf("Don't forget to edit squid.conf before starting it.\n\n");
829 fprintf(stderr
, "CreateService failed\n");
833 CloseServiceHandle(schSCManager
);
838 WIN32_sendSignal(int WIN32_signal
)
840 SERVICE_STATUS ssStatus
;
841 DWORD fdwAccess
, fdwControl
;
842 SC_HANDLE schService
;
843 SC_HANDLE schSCManager
;
845 if (!WIN32_Service_name
)
846 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
848 schSCManager
= OpenSCManager(NULL
, /* machine (NULL == local) */
849 NULL
, /* database (NULL == default) */
850 SC_MANAGER_ALL_ACCESS
/* access required */
854 fprintf(stderr
, "OpenSCManager failed\n");
858 /* The required service object access depends on the control. */
859 switch (WIN32_signal
) {
861 case 0: /* SIGNULL */
862 fdwAccess
= SERVICE_INTERROGATE
;
863 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_INTERROGATE
;
867 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
868 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_ROTATE
;
872 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
873 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_DEBUG
;
877 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
878 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_RECONFIGURE
;
882 fdwAccess
= SERVICE_STOP
;
883 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_STOP
;
889 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
890 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_INTERRUPT
;
897 /* Open a handle to the service. */
898 schService
= OpenService(schSCManager
, /* SCManager database */
899 WIN32_Service_name
, /* name of service */
900 fdwAccess
); /* specify access */
902 if (schService
== NULL
) {
903 fprintf(stderr
, "%s: ERROR: Could not open Service %s\n", APP_SHORTNAME
,
907 /* Send a control value to the service. */
909 if (!ControlService(schService
, /* handle of service */
910 fdwControl
, /* control value to send */
911 &ssStatus
)) { /* address of status info */
912 fprintf(stderr
, "%s: ERROR: Could not Control Service %s\n",
913 APP_SHORTNAME
, WIN32_Service_name
);
916 /* Print the service status. */
917 printf("\nStatus of %s Service:\n", WIN32_Service_name
);
918 printf(" Service Type: 0x%lx\n", ssStatus
.dwServiceType
);
919 printf(" Current State: 0x%lx\n", ssStatus
.dwCurrentState
);
920 printf(" Controls Accepted: 0x%lx\n", ssStatus
.dwControlsAccepted
);
921 printf(" Exit Code: %ld\n", ssStatus
.dwWin32ExitCode
);
922 printf(" Service Specific Exit Code: %ld\n",
923 ssStatus
.dwServiceSpecificExitCode
);
924 printf(" Check Point: %ld\n", ssStatus
.dwCheckPoint
);
925 printf(" Wait Hint: %ld\n", ssStatus
.dwWaitHint
);
928 CloseServiceHandle(schService
);
931 CloseServiceHandle(schSCManager
);
934 int main(int argc
, char **argv
)
936 SERVICE_TABLE_ENTRY DispatchTable
[] = {
937 {NULL
, SquidWinSvcMain
},
941 char stderr_path
[256];
943 if ((argc
== 2) && strstr(argv
[1], _WIN_SQUID_SERVICE_OPTION
)) {
944 strcpy(stderr_path
, argv
[0]);
945 strcat(stderr_path
,".log");
946 freopen(stderr_path
, "w", stderr
);
947 setmode(fileno(stderr
), O_TEXT
);
948 WIN32_run_mode
= _WIN_SQUID_RUN_MODE_SERVICE
;
950 if (!(c
=strchr(argv
[1],':'))) {
951 fprintf(stderr
, "Bad Service Parameter: %s\n", argv
[1]);
955 WIN32_Service_name
= xstrdup(c
+1);
956 DispatchTable
[0].lpServiceName
=WIN32_Service_name
;
957 strcat(REGKEY
, WIN32_Service_name
);
958 keys
[4] = WIN32_Service_name
;
960 if (!StartServiceCtrlDispatcher(DispatchTable
)) {
961 fprintf(stderr
, "StartServiceCtrlDispatcher error = %ld\n",
966 WIN32_run_mode
= _WIN_SQUID_RUN_MODE_INTERACTIVE
;
973 return SquidMain(argc
, argv
);
979 #endif /* USE_WIN32_SERVICE */
981 #if defined(_SQUID_MSWIN_)
982 static int Win32SockInit(void)
984 int iVersionRequested
;
987 int optlen
= sizeof(opt
);
989 if (s_iInitCount
> 0) {
992 } else if (s_iInitCount
< 0)
993 return (s_iInitCount
);
995 /* s_iInitCount == 0. Do the initailization */
996 iVersionRequested
= MAKEWORD(2, 0);
998 err
= WSAStartup((WORD
) iVersionRequested
, &wsaData
);
1002 return (s_iInitCount
);
1005 if (LOBYTE(wsaData
.wVersion
) != 2 ||
1006 HIBYTE(wsaData
.wVersion
) != 0) {
1009 return (s_iInitCount
);
1012 if (WIN32_OS_version
!=_WIN_OS_WINNT
) {
1013 if (::getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *)&opt
, &optlen
)) {
1016 return (s_iInitCount
);
1018 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
1020 if (::setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
)) {
1023 return (s_iInitCount
);
1028 WIN32_Socks_initialized
= 1;
1030 return (s_iInitCount
);
1033 static void Win32SockCleanup(void)
1035 if (--s_iInitCount
== 0)
1041 void Squid_Win32InvalidParameterHandler(const wchar_t* expression
, const wchar_t* function
, const wchar_t* file
, unsigned int line
, uintptr_t pReserved
)
1046 #endif /* SQUID_MSWIN_ */