xref: /freebsd/contrib/libpcap/rpcapd/win32-svc.c (revision a50d73d5782a351ad83e8d1f84d11720a12e70d3)
1 /*
2  * Copyright (c) 2002 - 2003
3  * NetGroup, Politecnico di Torino (Italy)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the Politecnico di Torino nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 
34 #include "rpcapd.h"
35 #include <pcap.h>		// for PCAP_ERRBUF_SIZE
36 #include "fmtutils.h"
37 #include "portability.h"
38 #include "fileconf.h"
39 #include "log.h"
40 
41 #include "win32-svc.h"	// for Win32 service stuff
42 
43 static SERVICE_STATUS_HANDLE service_status_handle;
44 static SERVICE_STATUS service_status;
45 
46 static void WINAPI svc_main(DWORD argc, char **argv);
47 static void WINAPI svc_control_handler(DWORD Opcode);
48 static void update_svc_status(DWORD state, DWORD progress_indicator);
49 
50 BOOL svc_start(void)
51 {
52 	BOOL rc;
53 	SERVICE_TABLE_ENTRY ste[] =
54 	{
55 		{ PROGRAM_NAME, svc_main },
56 		{ NULL, NULL }
57 	};
58 	char string[PCAP_ERRBUF_SIZE];
59 
60 	// This call is blocking. A new thread is created which will launch
61 	// the svc_main() function
62 	if ((rc = StartServiceCtrlDispatcher(ste)) == 0) {
63 		pcap_fmt_errmsg_for_win32_err(string, sizeof (string),
64 		    GetLastError(), "StartServiceCtrlDispatcher() failed");
65 		rpcapd_log(LOGPRIO_ERROR, "%s", string);
66 	}
67 
68 	return rc; // FALSE if this is not started as a service
69 }
70 
71 static void WINAPI
72 svc_control_handler(DWORD Opcode)
73 {
74 	switch(Opcode)
75 	{
76 		case SERVICE_CONTROL_STOP:
77 			//
78 			// XXX - is this sufficient to clean up the service?
79 			// To be really honest, only the main socket and
80 			// such these stuffs are cleared; however the threads
81 			// that are running are not stopped.
82 			// This can be seen by placing a breakpoint at the
83 			// end of svc_main(), in which you will see that is
84 			// never reached. However, as soon as you set the
85 			// service status to "stopped",	the
86 			// StartServiceCtrlDispatcher() returns and the main
87 			// thread ends. Then, Win32 has a good automatic
88 			// cleanup, so that all the threads which are still
89 			// running are stopped when the main thread ends.
90 			//
91 			send_shutdown_notification();
92 
93 			update_svc_status(SERVICE_STOP_PENDING, 0);
94 			break;
95 
96 		/*
97 			Pause and Continue have an usual meaning and they are used just to be able
98 			to change the running parameters at run-time. In other words, they act
99 			like the SIGHUP signal on UNIX. All the running threads continue to run and
100 			they are not paused at all.
101 			Particularly,
102 			- PAUSE does nothing
103 			- CONTINUE re-reads the configuration file and creates the new threads that
104 			can be needed according to the new configuration.
105 		*/
106 		case SERVICE_CONTROL_PAUSE:
107 			update_svc_status(SERVICE_PAUSED, 0);
108 			break;
109 
110 		case SERVICE_CONTROL_CONTINUE:
111 			update_svc_status(SERVICE_RUNNING, 0);
112 			//
113 			// Tell the main loop to re-read the configuration.
114 			//
115 			send_reread_configuration_notification();
116 			break;
117 
118 		case SERVICE_CONTROL_INTERROGATE:
119 			// Fall through to send current status.
120 			//	WARNING: not implemented
121 			update_svc_status(SERVICE_RUNNING, 0);
122 			MessageBox(NULL, "Not implemented", "warning", MB_OK);
123 			break;
124 
125 		case SERVICE_CONTROL_PARAMCHANGE:
126 			//
127 			// Tell the main loop to re-read the configuration.
128 			//
129 			send_reread_configuration_notification();
130 			break;
131 	}
132 
133 	// Send current status.
134 	return;
135 }
136 
137 static void WINAPI
138 svc_main(DWORD argc, char **argv)
139 {
140 	service_status_handle = RegisterServiceCtrlHandler(PROGRAM_NAME, svc_control_handler);
141 
142 	if (!service_status_handle)
143 		return;
144 
145 	service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
146 	service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_PARAMCHANGE;
147 	// | SERVICE_ACCEPT_SHUTDOWN ;
148 	update_svc_status(SERVICE_RUNNING, 0);
149 
150 	//
151 	// Service requests until we're told to stop.
152 	//
153 	main_startup();
154 
155 	//
156 	// It returned, so we were told to stop.
157 	//
158 	update_svc_status(SERVICE_STOPPED, 0);
159 }
160 
161 static void
162 update_svc_status(DWORD state, DWORD progress_indicator)
163 {
164 	service_status.dwWin32ExitCode = NO_ERROR;
165 	service_status.dwCurrentState = state;
166 	service_status.dwCheckPoint = progress_indicator;
167 	service_status.dwWaitHint = 0;
168 	SetServiceStatus(service_status_handle, &service_status);
169 }
170 
171 /*
172 sc create rpcapd DisplayName= "Remote Packet Capture Protocol v.0 (experimental)" binpath= "C:\cvsroot\winpcap\wpcap\PRJ\Debug\rpcapd -d -f rpcapd.ini"
173 sc description rpcapd "Allows to capture traffic on this host from a remote machine."
174 */
175