sharkd: fix a memory leak on Windows introduced in gfe06aad
[metze/wireshark/wip.git] / sharkd_daemon.c
1 /* sharkd_daemon.c
2  *
3  * Copyright (C) 2016 Jakub Zawadzki
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <config.h>
25
26 #include <glib.h>
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <signal.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #ifdef _WIN32
39 #include <wsutil/unicode-utils.h>
40 #include <wsutil/filesystem.h>
41 #endif
42
43 #include <wsutil/socket.h>
44
45 #ifdef HAVE_NETINET_IN_H
46 #include <netinet/in.h>
47 #endif
48
49 #ifndef _WIN32
50 #include <sys/un.h>
51 #include <netinet/tcp.h>
52 #endif
53
54 #include <wsutil/strtoi.h>
55
56 #include "sharkd.h"
57
58 #ifdef _WIN32
59 /* for windows support TCP sockets */
60 # define SHARKD_TCP_SUPPORT
61 #else
62 /* for other system support only local sockets */
63 # define SHARKD_UNIX_SUPPORT
64 #endif
65
66 static int _use_stdinout = 0;
67 static socket_handle_t _server_fd = INVALID_SOCKET;
68
69 static socket_handle_t
70 socket_init(char *path)
71 {
72         socket_handle_t fd = INVALID_SOCKET;
73
74 #ifdef _WIN32
75         WSADATA wsaData;
76
77         WSAStartup(MAKEWORD(1, 1), &wsaData);
78 #endif
79
80 #ifdef SHARKD_UNIX_SUPPORT
81         if (!strncmp(path, "unix:", 5))
82         {
83                 struct sockaddr_un s_un;
84                 socklen_t s_un_len;
85
86                 path += 5;
87
88                 if (strlen(path) + 1 > sizeof(s_un.sun_path))
89                         return INVALID_SOCKET;
90
91                 fd = socket(AF_UNIX, SOCK_STREAM, 0);
92                 if (fd == INVALID_SOCKET)
93                         return INVALID_SOCKET;
94
95                 memset(&s_un, 0, sizeof(s_un));
96                 s_un.sun_family = AF_UNIX;
97                 g_strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path));
98
99                 s_un_len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(s_un.sun_path));
100
101                 if (s_un.sun_path[0] == '@')
102                         s_un.sun_path[0] = '\0';
103
104                 if (bind(fd, (struct sockaddr *) &s_un, s_un_len))
105                 {
106                         closesocket(fd);
107                         return INVALID_SOCKET;
108                 }
109         }
110         else
111 #endif
112
113 #ifdef SHARKD_TCP_SUPPORT
114         if (!strncmp(path, "tcp:", 4))
115         {
116                 struct sockaddr_in s_in;
117                 int one = 1;
118                 char *port_sep;
119                 guint16 port;
120
121                 path += 4;
122
123                 port_sep = strchr(path, ':');
124                 if (!port_sep)
125                         return INVALID_SOCKET;
126
127                 *port_sep = '\0';
128
129                 if (ws_strtou16(port_sep + 1, NULL, &port) == FALSE)
130                         return INVALID_SOCKET;
131
132 #ifdef _WIN32
133                 /* Need to use WSASocket() to disable overlapped I/O operations,
134                    this way on windows SOCKET can be used as HANDLE for stdin/stdout */
135                 fd = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
136 #else
137                 fd = socket(AF_INET, SOCK_STREAM, 0);
138 #endif
139                 if (fd == INVALID_SOCKET)
140                         return INVALID_SOCKET;
141
142                 s_in.sin_family = AF_INET;
143                 s_in.sin_addr.s_addr = inet_addr(path);
144                 s_in.sin_port = g_htons(port);
145                 *port_sep = ':';
146
147                 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
148
149                 if (bind(fd, (struct sockaddr *) &s_in, sizeof(struct sockaddr_in)))
150                 {
151                         closesocket(fd);
152                         return INVALID_SOCKET;
153                 }
154         }
155         else
156 #endif
157         {
158                 return INVALID_SOCKET;
159         }
160
161         if (listen(fd, SOMAXCONN))
162         {
163                 closesocket(fd);
164                 return INVALID_SOCKET;
165         }
166
167         return fd;
168 }
169
170 int
171 sharkd_init(int argc, char **argv)
172 {
173 #ifndef _WIN32
174         pid_t pid;
175 #endif
176         socket_handle_t fd;
177
178         if (argc != 2)
179         {
180                 fprintf(stderr, "Usage: %s <socket>\n", argv[0]);
181                 fprintf(stderr, "\n");
182
183                 fprintf(stderr, "<socket> examples:\n");
184 #ifdef SHARKD_UNIX_SUPPORT
185                 fprintf(stderr, " - unix:/tmp/sharkd.sock - listen on unix file /tmp/sharkd.sock\n");
186 #endif
187 #ifdef SHARKD_TCP_SUPPORT
188                 fprintf(stderr, " - tcp:127.0.0.1:4446 - listen on TCP port 4446\n");
189 #endif
190                 fprintf(stderr, "\n");
191                 return -1;
192         }
193
194 #ifndef _WIN32
195         signal(SIGCHLD, SIG_IGN);
196 #endif
197
198         if (!strcmp(argv[1], "-"))
199         {
200                 _use_stdinout = 1;
201         }
202         else
203         {
204                 fd = socket_init(argv[1]);
205                 if (fd == INVALID_SOCKET)
206                         return -1;
207                 _server_fd = fd;
208         }
209
210 #ifndef _WIN32
211         /* all good - try to daemonize */
212         pid = fork();
213         if (pid == -1)
214                 fprintf(stderr, "cannot go to background fork() failed: %s\n", g_strerror(errno));
215
216         if (pid != 0)
217         {
218                 /* parent */
219                 exit(0);
220         }
221 #endif
222
223         return 0;
224 }
225
226 int
227 sharkd_loop(void)
228 {
229         if (_use_stdinout)
230         {
231                 return sharkd_session_main();
232         }
233
234         while (1)
235         {
236 #ifndef _WIN32
237                 pid_t pid;
238 #else
239                 PROCESS_INFORMATION pi;
240                 STARTUPINFO si;
241                 char *exename;
242                 gunichar2 *commandline;
243 #endif
244                 socket_handle_t fd;
245
246                 fd = accept(_server_fd, NULL, NULL);
247                 if (fd == INVALID_SOCKET)
248                 {
249                         fprintf(stderr, "cannot accept(): %s\n", g_strerror(errno));
250                         continue;
251                 }
252
253                 /* wireshark is not ready for handling multiple capture files in single process, so fork(), and handle it in separate process */
254 #ifndef _WIN32
255                 pid = fork();
256                 if (pid == 0)
257                 {
258                         /* redirect stdin, stdout to socket */
259                         dup2(fd, 0);
260                         dup2(fd, 1);
261                         close(fd);
262
263                         exit(sharkd_session_main());
264                 }
265
266                 if (pid == -1)
267                 {
268                         fprintf(stderr, "cannot fork(): %s\n", g_strerror(errno));
269                 }
270
271 #else
272                 memset(&pi, 0, sizeof(pi));
273                 memset(&si, 0, sizeof(si));
274
275                 si.cb = sizeof(si);
276                 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
277                 si.hStdInput = (HANDLE) fd;
278                 si.hStdOutput = (HANDLE) fd;
279                 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
280
281                 exename = g_strdup_printf("%s\\%s", get_progfile_dir(), "sharkd.exe");
282                 commandline = g_utf8_to_utf16("sharkd.exe -", -1, NULL, NULL, NULL);
283
284                 if (!CreateProcess(utf_8to16(exename), commandline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
285                 {
286                         fprintf(stderr, "CreateProcess(%s) failed\n", exename);
287                 }
288                 else
289                 {
290                         CloseHandle(pi.hThread);
291                 }
292
293                 g_free(exename);
294                 g_free(commandline);
295 #endif
296
297                 closesocket(fd);
298         }
299         return 0;
300 }
301
302 /*
303  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
304  *
305  * Local variables:
306  * c-basic-offset: 8
307  * tab-width: 8
308  * indent-tabs-mode: t
309  * End:
310  *
311  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
312  * :indentSize=8:tabSize=8:noTabs=false:
313  */