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.
42 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
47 /* forward declarations */
48 static void WIN32_Exit(void);
49 static void WIN32_Abort(int);
51 static unsigned int GetOSVersion();
52 void WIN32_svcstatusupdate(DWORD
, DWORD
);
53 void WINAPI
WIN32_svcHandler(DWORD
);
55 static int WIN32_StoreKey(const char *, DWORD
, unsigned char *, int);
56 static int WIN32_create_key(void);
57 static void WIN32_build_argv (char *);
59 extern "C" void WINAPI
SquidWinSvcMain(DWORD
, char **);
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
;
71 static int Squid_Aborting
= 0;
73 #undef NotifyAddrChange
74 typedef DWORD(WINAPI
* PFNotifyAddrChange
) (OUT PHANDLE
, IN LPOVERLAPPED
);
75 #define NOTIFYADDRCHANGE "NotifyAddrChange"
78 static SERVICE_STATUS svcStatus
;
79 static SERVICE_STATUS_HANDLE svcHandle
;
80 static int WIN32_argc
;
81 static char ** WIN32_argv
;
82 static char * WIN32_module_name
;
84 #define VENDOR "squid-cache.org"
85 static char VENDORString
[] = VENDOR
;
86 #define SOFTWARENAME PACKAGE_NAME
87 static char SOFTWARENAMEString
[] = SOFTWARENAME
;
88 #define SOFTWARE "SOFTWARE"
89 static char SOFTWAREString
[] = SOFTWARE
;
90 #define COMMANDLINE "CommandLine"
91 #define CONFIGFILE "ConfigFile"
92 #undef ChangeServiceConfig2
93 typedef BOOL (WINAPI
* PFChangeServiceConfig2
) (SC_HANDLE
, DWORD
, LPVOID
);
95 #define CHANGESERVICECONFIG2 "ChangeServiceConfig2W"
97 #define CHANGESERVICECONFIG2 "ChangeServiceConfig2A"
99 static SC_ACTION Squid_SCAction
[] = { { SC_ACTION_RESTART
, 60000 } };
100 static char Squid_ServiceDescriptionString
[] = SOFTWARENAME
" " VERSION
" WWW Proxy Server";
101 static SERVICE_DESCRIPTION Squid_ServiceDescription
= { Squid_ServiceDescriptionString
};
102 static SERVICE_FAILURE_ACTIONS Squid_ServiceFailureActions
= { INFINITE
, NULL
, NULL
, 1, Squid_SCAction
};
103 static char REGKEY
[256]=SOFTWARE
"\\"VENDOR
"\\"SOFTWARENAME
"\\";
104 static char *keys
[] = {
105 SOFTWAREString
, /* key[0] */
106 VENDORString
, /* key[1] */
107 SOFTWARENAMEString
, /* key[2] */
113 /* ====================================================================== */
114 /* LOCAL FUNCTIONS */
115 /* ====================================================================== */
117 #if USE_WIN32_SERVICE
119 WIN32_create_key(void)
127 hKey
= HKEY_LOCAL_MACHINE
;
131 /* Walk the tree, creating at each stage if necessary */
133 while (keys
[index
]) {
134 unsigned long result
;
135 rv
= RegCreateKeyEx(hKey
, keys
[index
], /* subkey */
138 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &hKeyNext
, &result
);
140 if (rv
!= ERROR_SUCCESS
) {
141 fprintf(stderr
, "RegCreateKeyEx(%s),%d\n", keys
[index
], (int) rv
);
145 /* Close the old key */
146 rv
= RegCloseKey(hKey
);
148 if (rv
!= ERROR_SUCCESS
) {
149 fprintf(stderr
, "RegCloseKey %d\n", (int) rv
);
152 /* Keep error status from RegCreateKeyEx, if any */
165 if (keys
[index
] == NULL
) {
166 /* Close the final key we opened, if we walked the entire
169 rv
= RegCloseKey(hKey
);
171 if (rv
!= ERROR_SUCCESS
) {
172 fprintf(stderr
, "RegCloseKey %d\n", (int) rv
);
175 /* Keep error status from RegCreateKeyEx, if any */
185 WIN32_StoreKey(const char *key
, DWORD type
, unsigned char *value
,
192 rv
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, REGKEY
, 0, KEY_WRITE
, &hKey
);
194 if (rv
== ERROR_FILE_NOT_FOUND
) {
195 /* Key could not be opened -- try to create it
198 if (WIN32_create_key() < 0) {
199 /* Creation failed (error already reported) */
203 /* Now it has been created we should be able to open it
205 rv
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, REGKEY
, 0, KEY_WRITE
, &hKey
);
207 if (rv
== ERROR_FILE_NOT_FOUND
) {
208 fprintf(stderr
, "Registry does not contain key %s after creation\n",
214 if (rv
!= ERROR_SUCCESS
) {
215 fprintf(stderr
, "RegOpenKeyEx HKLM\\%s, %d\n", REGKEY
, (int) rv
);
219 /* Now set the value and data */
220 rv
= RegSetValueEx(hKey
, key
, /* value key name */
223 value
, /* value data */
224 (DWORD
) value_size
); /* for size of "value" */
226 retval
= 0; /* Return value */
228 if (rv
!= ERROR_SUCCESS
) {
229 fprintf(stderr
, "RegQueryValueEx(key %s),%d\n", key
, (int) rv
);
232 fprintf(stderr
, "Registry stored HKLM\\%s\\%s value %s\n",
235 type
== REG_SZ
? value
: (unsigned char *) "(not displayable)");
238 /* Make sure we close the key even if there was an error storing
241 rv
= RegCloseKey(hKey
);
243 if (rv
!= ERROR_SUCCESS
) {
244 fprintf(stderr
, "RegCloseKey HKLM\\%s, %d\n", REGKEY
, (int) rv
);
247 /* Keep error status from RegQueryValueEx, if any */
255 /* Build argv, argc from string passed from Windows. */
256 static void WIN32_build_argv(char *cmd
)
262 WIN32_argv
= (char **) xmalloc ((WIN32_argc
+1) * sizeof (char *));
263 WIN32_argv
[0]=xstrdup(WIN32_module_name
);
264 /* Scan command line until there is nothing left. */
269 if (xisspace(*cmd
)) {
274 /* Found the beginning of an argument. */
278 ++cmd
; /* Skip over this character */
280 if (xisspace(*cmd
)) /* End of argument if space */
285 *cmd
++ = '\0'; /* Terminate `word' */
287 /* See if we need to allocate more space for argv */
288 if (WIN32_argc
>= argvlen
) {
289 argvlen
= WIN32_argc
+ 1;
290 WIN32_argv
= (char **) xrealloc (WIN32_argv
, (1 + argvlen
) * sizeof (char *));
293 /* Add word to argv file. */
294 WIN32_argv
[WIN32_argc
++] = word
;
297 WIN32_argv
[WIN32_argc
] = NULL
;
300 #endif /* USE_WIN32_SERVICE */
305 OSVERSIONINFOEX osvi
;
306 BOOL bOsVersionInfoEx
;
308 safe_free(WIN32_OS_string
);
309 memset(&osvi
, '\0', sizeof(OSVERSIONINFOEX
));
310 /* Try calling GetVersionEx using the OSVERSIONINFOEX structure.
311 * If that fails, try using the OSVERSIONINFO structure.
314 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOEX
);
316 if (!(bOsVersionInfoEx
= GetVersionEx((OSVERSIONINFO
*) & osvi
))) {
317 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
318 if (!GetVersionEx((OSVERSIONINFO
*) & osvi
))
321 switch (osvi
.dwPlatformId
) {
322 case VER_PLATFORM_WIN32_NT
:
323 if (osvi
.dwMajorVersion
<= 4) {
324 WIN32_OS_string
= xstrdup("Windows NT");
325 return _WIN_OS_WINNT
;
327 if ((osvi
.dwMajorVersion
== 5) && (osvi
.dwMinorVersion
== 0)) {
328 WIN32_OS_string
= xstrdup("Windows 2000");
329 return _WIN_OS_WIN2K
;
331 if ((osvi
.dwMajorVersion
== 5) && (osvi
.dwMinorVersion
== 1)) {
332 WIN32_OS_string
= xstrdup("Windows XP");
333 return _WIN_OS_WINXP
;
335 if ((osvi
.dwMajorVersion
== 5) && (osvi
.dwMinorVersion
== 2)) {
336 WIN32_OS_string
= xstrdup("Windows Server 2003");
337 return _WIN_OS_WINNET
;
339 if ((osvi
.dwMajorVersion
== 6) && (osvi
.dwMinorVersion
== 0)) {
340 if (osvi
.wProductType
== VER_NT_WORKSTATION
)
341 WIN32_OS_string
= xstrdup("Windows Vista");
343 WIN32_OS_string
= xstrdup("Windows Server 2008");
344 return _WIN_OS_WINLON
;
346 if ((osvi
.dwMajorVersion
== 6) && (osvi
.dwMinorVersion
== 1)) {
347 if (osvi
.wProductType
== VER_NT_WORKSTATION
)
348 WIN32_OS_string
= xstrdup("Windows 7");
350 WIN32_OS_string
= xstrdup("Windows Server 2008 R2");
353 if (((osvi
.dwMajorVersion
> 6)) || ((osvi
.dwMajorVersion
== 6) && (osvi
.dwMinorVersion
> 1))) {
354 if (osvi
.wProductType
== VER_NT_WORKSTATION
)
355 WIN32_OS_string
= xstrdup("Unknown Windows version, assuming Windows 7 capabilities");
357 WIN32_OS_string
= xstrdup("Unknown Windows version, assuming Windows Server 2008 R2 capabilities");
361 case VER_PLATFORM_WIN32_WINDOWS
:
362 if ((osvi
.dwMajorVersion
== 4) && (osvi
.dwMinorVersion
== 0)) {
363 WIN32_OS_string
= xstrdup("Windows 95");
364 return _WIN_OS_WIN95
;
366 if ((osvi
.dwMajorVersion
== 4) && (osvi
.dwMinorVersion
== 10)) {
367 WIN32_OS_string
= xstrdup("Windows 98");
368 return _WIN_OS_WIN98
;
370 if ((osvi
.dwMajorVersion
== 4) && (osvi
.dwMinorVersion
== 90)) {
371 WIN32_OS_string
= xstrdup("Windows Me");
372 return _WIN_OS_WINME
;
375 case VER_PLATFORM_WIN32s
:
376 WIN32_OS_string
= xstrdup("Windows 3.1 with WIN32S");
377 return _WIN_OS_WIN32S
;
383 WIN32_OS_string
= xstrdup("Unknown Windows system");
384 return _WIN_OS_UNKNOWN
;
387 /* ====================================================================== */
388 /* PUBLIC FUNCTIONS */
389 /* ====================================================================== */
394 #if USE_WIN32_SERVICE
395 svcStatus
.dwWin32ExitCode
= ERROR_SERVICE_SPECIFIC_ERROR
;
396 svcStatus
.dwServiceSpecificExitCode
= 1;
404 WIN32_IpAddrChangeMonitorExit()
406 DWORD status
= ERROR_SUCCESS
;
408 if (NotifyAddrChange_thread
!= INVALID_HANDLE_VALUE
) {
409 TerminateThread(NotifyAddrChange_thread
, status
);
410 CloseHandle(NotifyAddrChange_thread
);
418 #if USE_WIN32_SERVICE
420 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
421 if (!Squid_Aborting
) {
422 svcStatus
.dwCurrentState
= SERVICE_STOPPED
;
423 SetServiceStatus(svcHandle
, &svcStatus
);
429 DeleteCriticalSection(dbg_mutex
);
431 WIN32_ExceptionHandlerCleanup();
432 WIN32_IpAddrChangeMonitorExit();
437 WIN32_IpAddrChangeMonitor(LPVOID lpParam
)
440 HMODULE IPHLPAPIHandle
;
441 PFNotifyAddrChange NotifyAddrChange
;
443 if ((IPHLPAPIHandle
= GetModuleHandle("IPHLPAPI")) == NULL
)
444 IPHLPAPIHandle
= LoadLibrary("IPHLPAPI");
445 NotifyAddrChange
= (PFNotifyAddrChange
) GetProcAddress(IPHLPAPIHandle
, NOTIFYADDRCHANGE
);
448 Result
= NotifyAddrChange(NULL
, NULL
);
449 if (Result
!= NO_ERROR
) {
450 debugs(1, DBG_IMPORTANT
, "NotifyAddrChange error " << Result
);
453 debugs(1, DBG_IMPORTANT
, "Notification of IP address change received, requesting Squid reconfiguration ...");
460 WIN32_IpAddrChangeMonitorInit()
462 DWORD status
= ERROR_SUCCESS
;
463 DWORD threadID
= 0, ThrdParam
= 0;
465 if ((WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) && (Config
.onoff
.WIN32_IpAddrChangeMonitor
)) {
466 NotifyAddrChange_thread
= CreateThread(NULL
, 0, WIN32_IpAddrChangeMonitor
,
467 &ThrdParam
, 0, &threadID
);
468 if (NotifyAddrChange_thread
== NULL
) {
469 status
= GetLastError();
470 NotifyAddrChange_thread
= INVALID_HANDLE_VALUE
;
471 debugs(1, DBG_IMPORTANT
, "Failed to start IP monitor thread.");
473 debugs(1, 2, "Starting IP monitor thread [" << threadID
<< "] ...");
478 int WIN32_Subsystem_Init(int * argc
, char *** argv
)
480 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
481 _invalid_parameter_handler oldHandler
, newHandler
;
484 WIN32_OS_version
= GetOSVersion();
486 if ((WIN32_OS_version
== _WIN_OS_UNKNOWN
) || (WIN32_OS_version
== _WIN_OS_WIN32S
))
489 if (atexit(WIN32_Exit
) != 0)
492 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
494 newHandler
= Squid_Win32InvalidParameterHandler
;
496 oldHandler
= _set_invalid_parameter_handler(newHandler
);
498 _CrtSetReportMode(_CRT_ASSERT
, 0);
501 #if USE_WIN32_SERVICE
503 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
507 if (signal(SIGABRT
, WIN32_Abort
) == SIG_ERR
)
510 /* Register the service Handler function */
511 svcHandle
= RegisterServiceCtrlHandler(WIN32_Service_name
, WIN32_svcHandler
);
516 /* Set Process work dir to directory cointaining squid.exe */
517 GetModuleFileName(NULL
, path
, 512);
519 WIN32_module_name
=xstrdup(path
);
521 path
[strlen(path
) - 10] = '\0';
523 if (SetCurrentDirectory(path
) == 0)
526 safe_free(ConfigFile
);
528 /* get config file from Windows Registry */
529 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, REGKEY
, 0, KEY_QUERY_VALUE
, &hndKey
) == ERROR_SUCCESS
) {
533 Result
= RegQueryValueEx(hndKey
, CONFIGFILE
, NULL
, &Type
, NULL
, &Size
);
535 if (Result
== ERROR_SUCCESS
&& Size
) {
536 ConfigFile
= static_cast<char *>(xmalloc(Size
));
537 RegQueryValueEx(hndKey
, CONFIGFILE
, NULL
, &Type
, (unsigned char *)ConfigFile
, &Size
);
539 ConfigFile
= xstrdup(DefaultConfigFile
);
545 Result
= RegQueryValueEx(hndKey
, COMMANDLINE
, NULL
, &Type
, NULL
, &Size
);
547 if (Result
== ERROR_SUCCESS
&& Size
) {
548 WIN32_Service_Command_Line
= static_cast<char *>(xmalloc(Size
));
549 RegQueryValueEx(hndKey
, COMMANDLINE
, NULL
, &Type
, (unsigned char *)WIN32_Service_Command_Line
, &Size
);
551 WIN32_Service_Command_Line
= xstrdup("");
555 ConfigFile
= xstrdup(DefaultConfigFile
);
556 WIN32_Service_Command_Line
= xstrdup("");
559 WIN32_build_argv(WIN32_Service_Command_Line
);
562 /* Set Service Status to SERVICE_START_PENDING */
563 svcStatus
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
564 svcStatus
.dwCurrentState
= SERVICE_START_PENDING
;
565 svcStatus
.dwControlsAccepted
=
566 SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN
;
567 svcStatus
.dwWin32ExitCode
= 0;
568 svcStatus
.dwServiceSpecificExitCode
= 0;
569 svcStatus
.dwCheckPoint
= 0;
570 svcStatus
.dwWaitHint
= 10000;
571 SetServiceStatus(svcHandle
, &svcStatus
);
573 _setmaxstdio(Squid_MaxFD
);
577 #endif /* USE_WIN32_SERVICE */
578 if (Win32SockInit() < 0)
584 #if USE_WIN32_SERVICE
586 WIN32_svcstatusupdate(DWORD svcstate
, DWORD WaitHint
)
588 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
589 ++svcStatus
.dwCheckPoint
;
590 svcStatus
.dwWaitHint
= WaitHint
;
591 svcStatus
.dwCurrentState
= svcstate
;
592 SetServiceStatus(svcHandle
, &svcStatus
);
597 WIN32_svcHandler(DWORD Opcode
)
603 case _WIN_SQUID_SERVICE_CONTROL_STOP
:
605 case _WIN_SQUID_SERVICE_CONTROL_SHUTDOWN
:
606 /* Do whatever it takes to stop here. */
607 svcStatus
.dwWin32ExitCode
= 0;
608 svcStatus
.dwCurrentState
= SERVICE_STOP_PENDING
;
609 svcStatus
.dwCheckPoint
= 0;
610 svcStatus
.dwWaitHint
= 10000;
613 if (!SetServiceStatus(svcHandle
, &svcStatus
)) {
614 status
= GetLastError();
615 debugs(1, DBG_IMPORTANT
, "SetServiceStatus error " << status
);
618 debugs(1, DBG_IMPORTANT
, "Leaving Squid service");
621 case _WIN_SQUID_SERVICE_CONTROL_INTERROGATE
:
622 /* Fall through to send current status. */
624 if (!SetServiceStatus(svcHandle
, &svcStatus
)) {
625 status
= GetLastError();
626 debugs(1, DBG_IMPORTANT
, "SetServiceStatus error " << status
);
631 case _WIN_SQUID_SERVICE_CONTROL_ROTATE
:
632 rotate_logs(SIGUSR1
);
635 case _WIN_SQUID_SERVICE_CONTROL_RECONFIGURE
:
639 case _WIN_SQUID_SERVICE_CONTROL_DEBUG
:
640 sigusr2_handle(SIGUSR2
);
643 case _WIN_SQUID_SERVICE_CONTROL_INTERRUPT
:
644 /* Do whatever it takes to stop here. */
645 svcStatus
.dwWin32ExitCode
= 0;
646 svcStatus
.dwCurrentState
= SERVICE_STOP_PENDING
;
647 svcStatus
.dwCheckPoint
= 0;
648 svcStatus
.dwWaitHint
= 10000;
651 if (!SetServiceStatus(svcHandle
, &svcStatus
)) {
652 status
= GetLastError();
653 debugs(1, DBG_IMPORTANT
, "SetServiceStatus error " << status
);
656 debugs(1, DBG_IMPORTANT
, "Leaving Squid service");
660 debugs(1, DBG_IMPORTANT
, "Unrecognized opcode " << Opcode
);
667 WIN32_RemoveService()
669 SC_HANDLE schService
;
670 SC_HANDLE schSCManager
;
672 if (!WIN32_Service_name
)
673 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
675 strcat(REGKEY
, WIN32_Service_name
);
677 keys
[4] = WIN32_Service_name
;
679 schSCManager
= OpenSCManager(NULL
, /* machine (NULL == local) */
680 NULL
, /* database (NULL == default) */
681 SC_MANAGER_ALL_ACCESS
/* access required */
685 fprintf(stderr
, "OpenSCManager failed\n");
687 schService
= OpenService(schSCManager
, WIN32_Service_name
, SERVICE_ALL_ACCESS
);
689 if (schService
== NULL
)
690 fprintf(stderr
, "OpenService failed\n");
692 /* Could not open the service */
694 /* try to stop the service */
696 if (ControlService(schService
, _WIN_SQUID_SERVICE_CONTROL_STOP
,
700 while (QueryServiceStatus(schService
, &svcStatus
)) {
701 if (svcStatus
.dwCurrentState
== SERVICE_STOP_PENDING
)
708 /* now remove the service */
709 if (DeleteService(schService
) == 0)
710 fprintf(stderr
, "DeleteService failed.\n");
712 printf("Service %s deleted successfully.\n",
715 CloseServiceHandle(schService
);
718 CloseServiceHandle(schSCManager
);
723 WIN32_SetServiceCommandLine()
725 if (!WIN32_Service_name
)
726 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
728 strcat(REGKEY
, WIN32_Service_name
);
730 keys
[4] = WIN32_Service_name
;
732 /* Now store the Service Command Line in the registry */
733 WIN32_StoreKey(COMMANDLINE
, REG_SZ
, (unsigned char *) WIN32_Command_Line
, strlen(WIN32_Command_Line
) + 1);
737 WIN32_InstallService()
739 SC_HANDLE schService
;
740 SC_HANDLE schSCManager
;
741 char ServicePath
[512];
745 if (!WIN32_Service_name
)
746 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
748 strcat(REGKEY
, WIN32_Service_name
);
750 keys
[4] = WIN32_Service_name
;
752 if ((lenpath
= GetModuleFileName(NULL
, ServicePath
, 512)) == 0) {
753 fprintf(stderr
, "Can't get executable path\n");
757 snprintf(szPath
, sizeof(szPath
), "%s %s:%s", ServicePath
, _WIN_SQUID_SERVICE_OPTION
, WIN32_Service_name
);
758 schSCManager
= OpenSCManager(NULL
, /* machine (NULL == local) */
759 NULL
, /* database (NULL == default) */
760 SC_MANAGER_ALL_ACCESS
/* access required */
764 fprintf(stderr
, "OpenSCManager failed\n");
767 schService
= CreateService(schSCManager
, /* SCManager database */
768 WIN32_Service_name
, /* name of service */
769 WIN32_Service_name
, /* name to display */
770 SERVICE_ALL_ACCESS
, /* desired access */
771 SERVICE_WIN32_OWN_PROCESS
, /* service type */
772 SERVICE_AUTO_START
, /* start type */
773 SERVICE_ERROR_NORMAL
, /* error control type */
774 (const char *) szPath
, /* service's binary */
775 NULL
, /* no load ordering group */
776 NULL
, /* no tag identifier */
777 "Tcpip\0AFD\0", /* dependencies */
778 NULL
, /* LocalSystem account */
779 NULL
); /* no password */
782 if (WIN32_OS_version
> _WIN_OS_WINNT
) {
783 HMODULE ADVAPI32Handle
;
784 PFChangeServiceConfig2 ChangeServiceConfig2
;
785 DWORD dwInfoLevel
= SERVICE_CONFIG_DESCRIPTION
;
787 ADVAPI32Handle
= GetModuleHandle("advapi32");
788 ChangeServiceConfig2
= (PFChangeServiceConfig2
) GetProcAddress(ADVAPI32Handle
, CHANGESERVICECONFIG2
);
789 ChangeServiceConfig2(schService
, dwInfoLevel
, &Squid_ServiceDescription
);
790 dwInfoLevel
= SERVICE_CONFIG_FAILURE_ACTIONS
;
791 ChangeServiceConfig2(schService
, dwInfoLevel
, &Squid_ServiceFailureActions
);
794 CloseServiceHandle(schService
);
795 /* Now store the config file location in the registry */
798 ConfigFile
= xstrdup(DefaultConfigFile
);
800 WIN32_StoreKey(CONFIGFILE
, REG_SZ
, (unsigned char *) ConfigFile
, strlen(ConfigFile
) + 1);
802 printf("Squid Cache version %s for %s\n", version_string
,
805 printf("installed successfully as %s Windows System Service.\n",
809 ("To run, start it from the Services Applet of Control Panel.\n");
811 printf("Don't forget to edit squid.conf before starting it.\n\n");
813 fprintf(stderr
, "CreateService failed\n");
817 CloseServiceHandle(schSCManager
);
822 WIN32_sendSignal(int WIN32_signal
)
824 SERVICE_STATUS ssStatus
;
825 DWORD fdwAccess
, fdwControl
;
826 SC_HANDLE schService
;
827 SC_HANDLE schSCManager
;
829 if (!WIN32_Service_name
)
830 WIN32_Service_name
= xstrdup(_WIN_SQUID_DEFAULT_SERVICE_NAME
);
832 schSCManager
= OpenSCManager(NULL
, /* machine (NULL == local) */
833 NULL
, /* database (NULL == default) */
834 SC_MANAGER_ALL_ACCESS
/* access required */
838 fprintf(stderr
, "OpenSCManager failed\n");
842 /* The required service object access depends on the control. */
843 switch (WIN32_signal
) {
845 case 0: /* SIGNULL */
846 fdwAccess
= SERVICE_INTERROGATE
;
847 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_INTERROGATE
;
851 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
852 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_ROTATE
;
856 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
857 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_DEBUG
;
861 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
862 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_RECONFIGURE
;
866 fdwAccess
= SERVICE_STOP
;
867 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_STOP
;
873 fdwAccess
= SERVICE_USER_DEFINED_CONTROL
;
874 fdwControl
= _WIN_SQUID_SERVICE_CONTROL_INTERRUPT
;
881 /* Open a handle to the service. */
882 schService
= OpenService(schSCManager
, /* SCManager database */
883 WIN32_Service_name
, /* name of service */
884 fdwAccess
); /* specify access */
886 if (schService
== NULL
) {
887 fprintf(stderr
, "%s: ERROR: Could not open Service %s\n", APP_SHORTNAME
,
891 /* Send a control value to the service. */
893 if (!ControlService(schService
, /* handle of service */
894 fdwControl
, /* control value to send */
895 &ssStatus
)) { /* address of status info */
896 fprintf(stderr
, "%s: ERROR: Could not Control Service %s\n",
897 APP_SHORTNAME
, WIN32_Service_name
);
900 /* Print the service status. */
901 printf("\nStatus of %s Service:\n", WIN32_Service_name
);
902 printf(" Service Type: 0x%lx\n", ssStatus
.dwServiceType
);
903 printf(" Current State: 0x%lx\n", ssStatus
.dwCurrentState
);
904 printf(" Controls Accepted: 0x%lx\n", ssStatus
.dwControlsAccepted
);
905 printf(" Exit Code: %ld\n", ssStatus
.dwWin32ExitCode
);
906 printf(" Service Specific Exit Code: %ld\n",
907 ssStatus
.dwServiceSpecificExitCode
);
908 printf(" Check Point: %ld\n", ssStatus
.dwCheckPoint
);
909 printf(" Wait Hint: %ld\n", ssStatus
.dwWaitHint
);
912 CloseServiceHandle(schService
);
915 CloseServiceHandle(schSCManager
);
918 int main(int argc
, char **argv
)
920 SERVICE_TABLE_ENTRY DispatchTable
[] = {
921 {NULL
, SquidWinSvcMain
},
925 char stderr_path
[256];
927 if ((argc
== 2) && strstr(argv
[1], _WIN_SQUID_SERVICE_OPTION
)) {
928 strcpy(stderr_path
, argv
[0]);
929 strcat(stderr_path
,".log");
930 freopen(stderr_path
, "w", stderr
);
931 setmode(fileno(stderr
), O_TEXT
);
932 WIN32_run_mode
= _WIN_SQUID_RUN_MODE_SERVICE
;
934 if (!(c
=strchr(argv
[1],':'))) {
935 fprintf(stderr
, "Bad Service Parameter: %s\n", argv
[1]);
939 WIN32_Service_name
= xstrdup(c
+1);
940 DispatchTable
[0].lpServiceName
=WIN32_Service_name
;
941 strcat(REGKEY
, WIN32_Service_name
);
942 keys
[4] = WIN32_Service_name
;
944 if (!StartServiceCtrlDispatcher(DispatchTable
)) {
945 fprintf(stderr
, "StartServiceCtrlDispatcher error = %ld\n",
950 WIN32_run_mode
= _WIN_SQUID_RUN_MODE_INTERACTIVE
;
953 return SquidMain(argc
, argv
);
959 #endif /* USE_WIN32_SERVICE */
961 static int Win32SockInit(void)
963 int iVersionRequested
;
966 int optlen
= sizeof(opt
);
968 if (s_iInitCount
> 0) {
971 } else if (s_iInitCount
< 0)
972 return (s_iInitCount
);
974 /* s_iInitCount == 0. Do the initailization */
975 iVersionRequested
= MAKEWORD(2, 0);
977 err
= WSAStartup((WORD
) iVersionRequested
, &wsaData
);
981 return (s_iInitCount
);
984 if (LOBYTE(wsaData
.wVersion
) != 2 ||
985 HIBYTE(wsaData
.wVersion
) != 0) {
988 return (s_iInitCount
);
991 if (WIN32_OS_version
!=_WIN_OS_WINNT
) {
992 if (::getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *)&opt
, &optlen
)) {
995 return (s_iInitCount
);
997 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
999 if (::setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
)) {
1002 return (s_iInitCount
);
1007 WIN32_Socks_initialized
= 1;
1009 return (s_iInitCount
);
1012 static void Win32SockCleanup(void)
1014 if (--s_iInitCount
== 0)
1020 void Squid_Win32InvalidParameterHandler(const wchar_t* expression
, const wchar_t* function
, const wchar_t* file
, unsigned int line
, uintptr_t pReserved
)