capture_sync: use ws_strtou function.
[metze/wireshark/wip.git] / capchild / capture_sync.c
1 /* capture_sync.c
2  * Synchronisation between Wireshark capture parent and child instances
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #ifdef HAVE_LIBPCAP
26
27 #include <glib.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <signal.h>
33
34 #include <wsutil/strtoi.h>
35
36 #ifdef _WIN32
37 #include <wsutil/unicode-utils.h>
38 #include <wsutil/win32-utils.h>
39 #endif
40
41 #ifdef HAVE_SYS_WAIT_H
42 # include <sys/wait.h>
43 #endif
44
45 #include "caputils/capture-pcap-util.h"
46
47 #ifndef _WIN32
48 /*
49  * Define various POSIX macros (and, in the case of WCOREDUMP, non-POSIX
50  * macros) on UNIX systems that don't have them.
51  */
52 #ifndef WIFEXITED
53 # define WIFEXITED(status)      (((status) & 0177) == 0)
54 #endif
55 #ifndef WIFSTOPPED
56 # define WIFSTOPPED(status)     (((status) & 0177) == 0177)
57 #endif
58 #ifndef WIFSIGNALED
59 # define WIFSIGNALED(status)    (!WIFSTOPPED(status) && !WIFEXITED(status))
60 #endif
61 #ifndef WEXITSTATUS
62 # define WEXITSTATUS(status)    ((status) >> 8)
63 #endif
64 #ifndef WTERMSIG
65 # define WTERMSIG(status)       ((status) & 0177)
66 #endif
67 #ifndef WCOREDUMP
68 # define WCOREDUMP(status)      ((status) & 0200)
69 #endif
70 #ifndef WSTOPSIG
71 # define WSTOPSIG(status)       ((status) >> 8)
72 #endif
73 #endif /* _WIN32 */
74
75 #include <epan/packet.h>
76 #include <epan/prefs.h>
77
78 #include "globals.h"
79 #include "file.h"
80
81 #include "ui/capture.h"
82 #include <capchild/capture_sync.h>
83
84 #include "sync_pipe.h"
85
86 #ifdef _WIN32
87 #include "caputils/capture-wpcap.h"
88 #endif
89
90 #include "ui/ui_util.h"
91
92 #include <wsutil/filesystem.h>
93 #include <wsutil/file_util.h>
94 #include <wsutil/report_err.h>
95 #ifdef HAVE_EXTCAP
96 #include "extcap.h"
97 #endif
98 #include "log.h"
99
100 #ifdef _WIN32
101 #include <process.h>    /* For spawning child process */
102 #endif
103
104
105
106 #ifdef _WIN32
107 static void create_dummy_signal_pipe();
108 static HANDLE dummy_signal_pipe; /* Dummy named pipe which lets the child check for a dropped connection */
109 static gchar *dummy_control_id;
110 #else
111 static const char *sync_pipe_signame(int);
112 #endif
113
114
115 static gboolean sync_pipe_input_cb(gint source, gpointer user_data);
116 static int sync_pipe_wait_for_child(ws_process_id fork_child, gchar **msgp);
117 static void pipe_convert_header(const guchar *header, int header_len, char *indicator, int *block_len);
118 static ssize_t pipe_read_block(int pipe_fd, char *indicator, int len, char *msg,
119                            char **err_msg);
120
121 static void (*fetch_dumpcap_pid)(ws_process_id) = NULL;
122
123
124 void
125 capture_session_init(capture_session *cap_session, struct _capture_file *cf)
126 {
127     cap_session->cf                              = cf;
128     cap_session->fork_child                      = WS_INVALID_PID;   /* invalid process handle */
129 #ifdef _WIN32
130     cap_session->signal_pipe_write_fd            = -1;
131 #endif
132     cap_session->state                           = CAPTURE_STOPPED;
133 #ifndef _WIN32
134     cap_session->owner                           = getuid();
135     cap_session->group                           = getgid();
136 #endif
137     cap_session->count                           = 0;
138     cap_session->session_started                 = FALSE;
139 }
140
141 /* Append an arg (realloc) to an argc/argv array */
142 /* (add a string pointer to a NULL-terminated array of string pointers) */
143 static char **
144 sync_pipe_add_arg(char **args, int *argc, const char *arg)
145 {
146     /* Grow the array; "*argc" currently contains the number of string
147        pointers, *not* counting the NULL pointer at the end, so we have
148        to add 2 in order to get the new size of the array, including the
149        new pointer and the terminating NULL pointer. */
150     args = (char **)g_realloc( (gpointer) args, (*argc + 2) * sizeof (char *));
151
152     /* Stuff the pointer into the penultimate element of the array, which
153        is the one at the index specified by "*argc". */
154     args[*argc] = g_strdup(arg);
155     /* Now bump the count. */
156     (*argc)++;
157
158     /* We overwrite the NULL pointer; put it back right after the
159        element we added. */
160     args[*argc] = NULL;
161
162     return args;
163 }
164
165 /* Initialize an argument list and add dumpcap to it. */
166 static char **
167 init_pipe_args(int *argc) {
168     char **argv;
169     const char *progfile_dir;
170     char *exename;
171
172     progfile_dir = get_progfile_dir();
173     if (progfile_dir == NULL) {
174       return NULL;
175     }
176
177     /* Allocate the string pointer array with enough space for the
178        terminating NULL pointer. */
179     *argc = 0;
180     argv = (char **)g_malloc(sizeof (char *));
181     *argv = NULL;
182
183     /* take Wireshark's absolute program path and replace "Wireshark" with "dumpcap" */
184 #ifdef _WIN32
185     exename = g_strdup_printf("%s\\dumpcap.exe", progfile_dir);
186 #else
187     exename = g_strdup_printf("%s/dumpcap", progfile_dir);
188 #endif
189
190     /* Make that the first argument in the argument list (argv[0]). */
191     argv = sync_pipe_add_arg(argv, argc, exename);
192
193     /* sync_pipe_add_arg strdupes exename, so we should free our copy */
194     g_free(exename);
195
196     return argv;
197 }
198
199 #define ARGV_NUMBER_LEN 24
200 /* a new capture run: start a new dumpcap task and hand over parameters through command line */
201 gboolean
202 sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, info_data_t* cap_data, void (*update_cb)(void))
203 {
204     char ssnap[ARGV_NUMBER_LEN];
205     char scount[ARGV_NUMBER_LEN];
206     char sfilesize[ARGV_NUMBER_LEN];
207     char sfile_duration[ARGV_NUMBER_LEN];
208     char sring_num_files[ARGV_NUMBER_LEN];
209     char sautostop_files[ARGV_NUMBER_LEN];
210     char sautostop_filesize[ARGV_NUMBER_LEN];
211     char sautostop_duration[ARGV_NUMBER_LEN];
212 #ifdef HAVE_PCAP_REMOTE
213     char sauth[256];
214 #endif
215 #ifdef HAVE_PCAP_SETSAMPLING
216     char ssampling[ARGV_NUMBER_LEN];
217 #endif
218
219 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
220     char buffer_size[ARGV_NUMBER_LEN];
221 #endif
222
223 #ifdef _WIN32
224     HANDLE sync_pipe_read;                  /* pipe used to send messages from child to parent */
225     HANDLE sync_pipe_write;                 /* pipe used to send messages from child to parent */
226     int signal_pipe_write_fd;
227     HANDLE signal_pipe;                     /* named pipe used to send messages from parent to child (currently only stop) */
228     GString *args = g_string_sized_new(200);
229     gchar *quoted_arg;
230     gunichar2 *wcommandline;
231     SECURITY_ATTRIBUTES sa;
232     STARTUPINFO si;
233     PROCESS_INFORMATION pi;
234     char control_id[ARGV_NUMBER_LEN];
235     gchar *signal_pipe_name;
236 #else
237     char errmsg[1024+1];
238     int sync_pipe[2];                       /* pipe used to send messages from child to parent */
239     enum PIPES { PIPE_READ, PIPE_WRITE };   /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
240 #endif
241     int sync_pipe_read_fd;
242     int argc;
243     char **argv;
244     int i;
245     guint j;
246     interface_options interface_opts;
247
248     if (capture_opts->ifaces->len > 1)
249         capture_opts->use_pcapng = TRUE;
250     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_start");
251     capture_opts_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, capture_opts);
252
253     cap_session->fork_child = WS_INVALID_PID;
254
255 #ifdef HAVE_EXTCAP
256     if (!extcap_init_interfaces(capture_opts)) {
257         report_failure("Unable to init extcaps. (tmp fifo already exists?)");
258         return FALSE;
259     }
260
261 #endif
262
263     argv = init_pipe_args(&argc);
264     if (!argv) {
265         /* We don't know where to find dumpcap. */
266         report_failure("We don't know where to find dumpcap.");
267         return FALSE;
268     }
269
270     if (capture_opts->ifaces->len > 1)
271         argv = sync_pipe_add_arg(argv, &argc, "-t");
272
273     if (capture_opts->use_pcapng)
274         argv = sync_pipe_add_arg(argv, &argc, "-n");
275     else
276         argv = sync_pipe_add_arg(argv, &argc, "-P");
277
278     if (capture_opts->capture_comment) {
279         argv = sync_pipe_add_arg(argv, &argc, "--capture-comment");
280         argv = sync_pipe_add_arg(argv, &argc, capture_opts->capture_comment);
281     }
282
283     if (capture_opts->multi_files_on) {
284         if (capture_opts->has_autostop_filesize) {
285             argv = sync_pipe_add_arg(argv, &argc, "-b");
286             g_snprintf(sfilesize, ARGV_NUMBER_LEN, "filesize:%u",capture_opts->autostop_filesize);
287             argv = sync_pipe_add_arg(argv, &argc, sfilesize);
288         }
289
290         if (capture_opts->has_file_duration) {
291             argv = sync_pipe_add_arg(argv, &argc, "-b");
292             g_snprintf(sfile_duration, ARGV_NUMBER_LEN, "duration:%d",capture_opts->file_duration);
293             argv = sync_pipe_add_arg(argv, &argc, sfile_duration);
294         }
295
296         if (capture_opts->has_ring_num_files) {
297             argv = sync_pipe_add_arg(argv, &argc, "-b");
298             g_snprintf(sring_num_files, ARGV_NUMBER_LEN, "files:%d",capture_opts->ring_num_files);
299             argv = sync_pipe_add_arg(argv, &argc, sring_num_files);
300         }
301
302         if (capture_opts->has_autostop_files) {
303             argv = sync_pipe_add_arg(argv, &argc, "-a");
304             g_snprintf(sautostop_files, ARGV_NUMBER_LEN, "files:%d",capture_opts->autostop_files);
305             argv = sync_pipe_add_arg(argv, &argc, sautostop_files);
306         }
307     } else {
308         if (capture_opts->has_autostop_filesize) {
309             argv = sync_pipe_add_arg(argv, &argc, "-a");
310             g_snprintf(sautostop_filesize, ARGV_NUMBER_LEN, "filesize:%u",capture_opts->autostop_filesize);
311             argv = sync_pipe_add_arg(argv, &argc, sautostop_filesize);
312         }
313     }
314
315     if (capture_opts->has_autostop_packets) {
316         argv = sync_pipe_add_arg(argv, &argc, "-c");
317         g_snprintf(scount, ARGV_NUMBER_LEN, "%d",capture_opts->autostop_packets);
318         argv = sync_pipe_add_arg(argv, &argc, scount);
319     }
320
321     if (capture_opts->has_autostop_duration) {
322         argv = sync_pipe_add_arg(argv, &argc, "-a");
323         g_snprintf(sautostop_duration, ARGV_NUMBER_LEN, "duration:%d",capture_opts->autostop_duration);
324         argv = sync_pipe_add_arg(argv, &argc, sautostop_duration);
325     }
326
327     if (capture_opts->group_read_access) {
328         argv = sync_pipe_add_arg(argv, &argc, "-g");
329     }
330
331     for (j = 0; j < capture_opts->ifaces->len; j++) {
332         interface_opts = g_array_index(capture_opts->ifaces, interface_options, j);
333
334         argv = sync_pipe_add_arg(argv, &argc, "-i");
335 #ifdef HAVE_EXTCAP
336         if (interface_opts.extcap_fifo != NULL)
337             argv = sync_pipe_add_arg(argv, &argc, interface_opts.extcap_fifo);
338         else
339 #endif
340             argv = sync_pipe_add_arg(argv, &argc, interface_opts.name);
341
342         if (interface_opts.cfilter != NULL && strlen(interface_opts.cfilter) != 0) {
343             argv = sync_pipe_add_arg(argv, &argc, "-f");
344             argv = sync_pipe_add_arg(argv, &argc, interface_opts.cfilter);
345         }
346         if (interface_opts.snaplen != WTAP_MAX_PACKET_SIZE) {
347             argv = sync_pipe_add_arg(argv, &argc, "-s");
348             g_snprintf(ssnap, ARGV_NUMBER_LEN, "%d", interface_opts.snaplen);
349             argv = sync_pipe_add_arg(argv, &argc, ssnap);
350         }
351
352         if (interface_opts.linktype != -1) {
353             const char *linktype = linktype_val_to_name(interface_opts.linktype);
354             if ( linktype != NULL )
355             {
356                 argv = sync_pipe_add_arg(argv, &argc, "-y");
357                 argv = sync_pipe_add_arg(argv, &argc, linktype);
358             }
359         }
360
361         if (!interface_opts.promisc_mode) {
362             argv = sync_pipe_add_arg(argv, &argc, "-p");
363         }
364
365 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
366         if (interface_opts.buffer_size != DEFAULT_CAPTURE_BUFFER_SIZE) {
367             argv = sync_pipe_add_arg(argv, &argc, "-B");
368             if(interface_opts.buffer_size == 0x00)
369                 interface_opts.buffer_size = DEFAULT_CAPTURE_BUFFER_SIZE;
370             g_snprintf(buffer_size, ARGV_NUMBER_LEN, "%d", interface_opts.buffer_size);
371             argv = sync_pipe_add_arg(argv, &argc, buffer_size);
372         }
373 #endif
374
375 #ifdef HAVE_PCAP_CREATE
376         if (interface_opts.monitor_mode) {
377             argv = sync_pipe_add_arg(argv, &argc, "-I");
378         }
379 #endif
380
381 #ifdef HAVE_PCAP_REMOTE
382         if (interface_opts.datatx_udp)
383             argv = sync_pipe_add_arg(argv, &argc, "-u");
384
385         if (!interface_opts.nocap_rpcap)
386             argv = sync_pipe_add_arg(argv, &argc, "-r");
387
388         if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
389             argv = sync_pipe_add_arg(argv, &argc, "-A");
390             g_snprintf(sauth, sizeof(sauth), "%s:%s",
391                        interface_opts.auth_username,
392                        interface_opts.auth_password);
393             argv = sync_pipe_add_arg(argv, &argc, sauth);
394         }
395 #endif
396
397 #ifdef HAVE_PCAP_SETSAMPLING
398         if (interface_opts.sampling_method != CAPTURE_SAMP_NONE) {
399             argv = sync_pipe_add_arg(argv, &argc, "-m");
400             g_snprintf(ssampling, ARGV_NUMBER_LEN, "%s:%d",
401                        interface_opts.sampling_method == CAPTURE_SAMP_BY_COUNT ? "count" :
402                        interface_opts.sampling_method == CAPTURE_SAMP_BY_TIMER ? "timer" :
403                        "undef",
404                        interface_opts.sampling_param);
405             argv = sync_pipe_add_arg(argv, &argc, ssampling);
406         }
407 #endif
408     }
409
410     /* dumpcap should be running in capture child mode (hidden feature) */
411 #ifndef DEBUG_CHILD
412     argv = sync_pipe_add_arg(argv, &argc, "-Z");
413 #ifdef _WIN32
414     g_snprintf(control_id, ARGV_NUMBER_LEN, "%d", GetCurrentProcessId());
415     argv = sync_pipe_add_arg(argv, &argc, control_id);
416 #else
417     argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
418 #endif
419 #endif
420
421     if (capture_opts->save_file) {
422         argv = sync_pipe_add_arg(argv, &argc, "-w");
423         argv = sync_pipe_add_arg(argv, &argc, capture_opts->save_file);
424     }
425     for (i = 0; i < argc; i++) {
426         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "argv[%d]: %s", i, argv[i]);
427     }
428
429 #ifdef _WIN32
430     /* init SECURITY_ATTRIBUTES */
431     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
432     sa.bInheritHandle = TRUE;
433     sa.lpSecurityDescriptor = NULL;
434
435     /* Create a pipe for the child process */
436     /* (increase this value if you have trouble while fast capture file switches) */
437     if (! CreatePipe(&sync_pipe_read, &sync_pipe_write, &sa, 5120)) {
438         /* Couldn't create the pipe between parent and child. */
439         report_failure("Couldn't create sync pipe: %s",
440                        win32strerror(GetLastError()));
441         for (i = 0; i < argc; i++) {
442             g_free( (gpointer) argv[i]);
443         }
444         g_free( (gpointer) argv);
445         return FALSE;
446     }
447
448     /*
449      * Associate a C run-time file handle with the Windows HANDLE for the
450      * read side of the message pipe.
451      *
452      * (See http://www.flounder.com/handles.htm for information on various
453      * types of file handle in C/C++ on Windows.)
454      */
455     sync_pipe_read_fd = _open_osfhandle( (intptr_t) sync_pipe_read, _O_BINARY);
456     if (sync_pipe_read_fd == -1) {
457         /* Couldn't create the pipe between parent and child. */
458         report_failure("Couldn't get C file handle for sync pipe: %s", g_strerror(errno));
459         CloseHandle(sync_pipe_read);
460         CloseHandle(sync_pipe_write);
461         for (i = 0; i < argc; i++) {
462             g_free( (gpointer) argv[i]);
463         }
464         g_free(argv);
465         return FALSE;
466     }
467
468     /* Create the signal pipe */
469     signal_pipe_name = g_strdup_printf(SIGNAL_PIPE_FORMAT, control_id);
470     signal_pipe = CreateNamedPipe(utf_8to16(signal_pipe_name),
471                                   PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE, 1, 65535, 65535, 0, NULL);
472     g_free(signal_pipe_name);
473
474     if (signal_pipe == INVALID_HANDLE_VALUE) {
475         /* Couldn't create the signal pipe between parent and child. */
476         report_failure("Couldn't create signal pipe: %s",
477                        win32strerror(GetLastError()));
478         ws_close(sync_pipe_read_fd);    /* Should close sync_pipe_read */
479         CloseHandle(sync_pipe_write);
480         for (i = 0; i < argc; i++) {
481             g_free( (gpointer) argv[i]);
482         }
483         g_free( (gpointer) argv);
484         return FALSE;
485     }
486
487     /*
488      * Associate a C run-time file handle with the Windows HANDLE for the
489      * read side of the message pipe.
490      *
491      * (See http://www.flounder.com/handles.htm for information on various
492      * types of file handle in C/C++ on Windows.)
493      */
494     signal_pipe_write_fd = _open_osfhandle( (intptr_t) signal_pipe, _O_BINARY);
495     if (sync_pipe_read_fd == -1) {
496         /* Couldn't create the pipe between parent and child. */
497         report_failure("Couldn't get C file handle for sync pipe: %s", g_strerror(errno));
498         ws_close(sync_pipe_read_fd);    /* Should close sync_pipe_read */
499         CloseHandle(sync_pipe_write);
500         CloseHandle(signal_pipe);
501         for (i = 0; i < argc; i++) {
502             g_free( (gpointer) argv[i]);
503         }
504         g_free(argv);
505         return FALSE;
506     }
507
508     /* init STARTUPINFO & PROCESS_INFORMATION */
509     memset(&si, 0, sizeof(si));
510     si.cb           = sizeof(si);
511     memset(&pi, 0, sizeof(pi));
512 #ifdef DEBUG_CHILD
513     si.dwFlags = STARTF_USESHOWWINDOW;
514     si.wShowWindow  = SW_SHOW;
515 #else
516     si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
517     si.wShowWindow  = SW_HIDE;  /* this hides the console window */
518 #if defined(_WIN32)
519     /* needs first a check if NULL *
520      * otherwise wouldn't work with non extcap interfaces */
521     if(interface_opts.extcap_fifo != NULL)
522     {
523        if(strncmp(interface_opts.extcap_fifo,"\\\\.\\pipe\\",9)== 0)
524        {
525          si.hStdInput = extcap_get_win32_handle();
526        }
527     }
528     else
529 #endif
530        si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
531
532     si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
533     si.hStdError = sync_pipe_write;
534     /*si.hStdError = (HANDLE) _get_osfhandle(2);*/
535 #endif
536
537     /* convert args array into a single string */
538     /* XXX - could change sync_pipe_add_arg() instead */
539     /* there is a drawback here: the length is internally limited to 1024 bytes */
540     for(i=0; argv[i] != 0; i++) {
541         if(i != 0) g_string_append_c(args, ' ');    /* don't prepend a space before the path!!! */
542         quoted_arg = protect_arg(argv[i]);
543         g_string_append(args, quoted_arg);
544         g_free(quoted_arg);
545     }
546     wcommandline = g_utf8_to_utf16(args->str, (glong)args->len, NULL, NULL, NULL);
547
548     /* call dumpcap */
549     if(!CreateProcess(utf_8to16(argv[0]), wcommandline, NULL, NULL, TRUE,
550                       CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
551         report_failure("Couldn't run %s in child process: %s",
552                        args->str, win32strerror(GetLastError()));
553         ws_close(sync_pipe_read_fd);    /* Should close sync_pipe_read */
554         CloseHandle(sync_pipe_write);
555         CloseHandle(signal_pipe);
556         for (i = 0; i < argc; i++) {
557             g_free( (gpointer) argv[i]);
558         }
559         g_free( (gpointer) argv);
560         g_string_free(args, TRUE);
561         g_free(wcommandline);
562         return FALSE;
563     }
564     cap_session->fork_child = pi.hProcess;
565     /* We may need to store this and close it later */
566     CloseHandle(pi.hThread);
567     g_string_free(args, TRUE);
568     g_free(wcommandline);
569
570     cap_session->signal_pipe_write_fd = signal_pipe_write_fd;
571
572 #else /* _WIN32 */
573     if (pipe(sync_pipe) < 0) {
574         /* Couldn't create the pipe between parent and child. */
575         report_failure("Couldn't create sync pipe: %s", g_strerror(errno));
576         for (i = 0; i < argc; i++) {
577             g_free( (gpointer) argv[i]);
578         }
579         g_free(argv);
580         return FALSE;
581     }
582
583     if ((cap_session->fork_child = fork()) == 0) {
584         /*
585          * Child process - run dumpcap with the right arguments to make
586          * it just capture with the specified capture parameters
587          */
588         dup2(sync_pipe[PIPE_WRITE], 2);
589         ws_close(sync_pipe[PIPE_READ]);
590         execv(argv[0], argv);
591         g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
592                    argv[0], g_strerror(errno));
593         sync_pipe_errmsg_to_parent(2, errmsg, "");
594
595         /* Exit with "_exit()", so that we don't close the connection
596            to the X server (and cause stuff buffered up by our parent but
597            not yet sent to be sent, as that stuff should only be sent by
598            our parent).  We've sent an error message to the parent, so
599            we exit with an exit status of 1 (any exit status other than
600            0 or 1 will cause an additional message to report that exit
601            status, over and above the error message we sent to the parent). */
602         _exit(1);
603     }
604
605     if (fetch_dumpcap_pid && cap_session->fork_child > 0)
606         fetch_dumpcap_pid(cap_session->fork_child);
607
608     sync_pipe_read_fd = sync_pipe[PIPE_READ];
609 #endif
610
611     for (i = 0; i < argc; i++) {
612         g_free( (gpointer) argv[i]);
613     }
614
615     /* Parent process - read messages from the child process over the
616        sync pipe. */
617     g_free( (gpointer) argv);   /* free up arg array */
618
619     /* Close the write side of the pipe, so that only the child has it
620        open, and thus it completely closes, and thus returns to us
621        an EOF indication, if the child closes it (either deliberately
622        or by exiting abnormally). */
623 #ifdef _WIN32
624     CloseHandle(sync_pipe_write);
625 #else
626     ws_close(sync_pipe[PIPE_WRITE]);
627 #endif
628
629     if (cap_session->fork_child == WS_INVALID_PID) {
630         /* We couldn't even create the child process. */
631         report_failure("Couldn't create child process: %s", g_strerror(errno));
632         ws_close(sync_pipe_read_fd);
633 #ifdef _WIN32
634         ws_close(cap_session->signal_pipe_write_fd);
635 #endif
636         return FALSE;
637     }
638
639     cap_session->fork_child_status = 0;
640     cap_session->capture_opts = capture_opts;
641     cap_session->cap_data_info = cap_data;
642
643     /* we might wait for a moment till child is ready, so update screen now */
644     if (update_cb) update_cb();
645
646     /* We were able to set up to read the capture file;
647        arrange that our callback be called whenever it's possible
648        to read from the sync pipe, so that it's called when
649        the child process wants to tell us something. */
650
651     /* we have a running capture, now wait for the real capture filename */
652     pipe_input_set_handler(sync_pipe_read_fd, (gpointer) cap_session,
653                            &cap_session->fork_child, sync_pipe_input_cb);
654
655     return TRUE;
656 }
657
658 /*
659  * Open two pipes to dumpcap with the supplied arguments, one for its
660  * standard output and one for its standard error.
661  *
662  * On success, *msg is unchanged and 0 is returned; data_read_fd,
663  * message_read_fd, and fork_child point to the standard output pipe's
664  * file descriptor, the standard error pipe's file descriptor, and
665  * the child's PID/handle, respectively.
666  *
667  * On failure, *msg points to an error message for the failure, and -1 is
668  * returned, in which case *msg must be freed with g_free().
669  */
670 /* XXX - This duplicates a lot of code in sync_pipe_start() */
671 /* XXX - assumes PIPE_BUF_SIZE > SP_MAX_MSG_LEN */
672 #define PIPE_BUF_SIZE 5120
673 static int
674 sync_pipe_open_command(char** argv, int *data_read_fd,
675                        int *message_read_fd, ws_process_id *fork_child, gchar **msg, void(*update_cb)(void))
676 {
677     enum PIPES { PIPE_READ, PIPE_WRITE };   /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
678 #ifdef _WIN32
679     HANDLE sync_pipe[2];                    /* pipe used to send messages from child to parent */
680     HANDLE data_pipe[2];                    /* pipe used to send data from child to parent */
681     GString *args = g_string_sized_new(200);
682     gchar *quoted_arg;
683     gunichar2 *wcommandline;
684     SECURITY_ATTRIBUTES sa;
685     STARTUPINFO si;
686     PROCESS_INFORMATION pi;
687 #else
688     char errmsg[1024+1];
689     int sync_pipe[2];                       /* pipe used to send messages from child to parent */
690     int data_pipe[2];                       /* pipe used to send data from child to parent */
691 #endif
692     int i;
693     *fork_child = WS_INVALID_PID;
694     *data_read_fd = -1;
695     *message_read_fd = -1;
696     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_open_command");
697
698     if (!msg) {
699         /* We can't return anything */
700 #ifdef _WIN32
701         g_string_free(args, TRUE);
702 #endif
703         return -1;
704     }
705
706 #ifdef _WIN32
707     /* init SECURITY_ATTRIBUTES */
708     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
709     sa.bInheritHandle = TRUE;
710     sa.lpSecurityDescriptor = NULL;
711
712     /* Create a pipe for the child process to send us messages */
713     /* (increase this value if you have trouble while fast capture file switches) */
714     if (! CreatePipe(&sync_pipe[PIPE_READ], &sync_pipe[PIPE_WRITE], &sa, 5120)) {
715         /* Couldn't create the message pipe between parent and child. */
716         *msg = g_strdup_printf("Couldn't create sync pipe: %s",
717                                win32strerror(GetLastError()));
718         for (i = 0; argv[i] != NULL; i++) {
719             g_free( (gpointer) argv[i]);
720         }
721         g_free( (gpointer) argv);
722         return -1;
723     }
724
725     /*
726      * Associate a C run-time file handle with the Windows HANDLE for the
727      * read side of the message pipe.
728      *
729      * (See http://www.flounder.com/handles.htm for information on various
730      * types of file handle in C/C++ on Windows.)
731      */
732     *message_read_fd = _open_osfhandle( (intptr_t) sync_pipe[PIPE_READ], _O_BINARY);
733     if (*message_read_fd == -1) {
734         *msg = g_strdup_printf("Couldn't get C file handle for message read pipe: %s", g_strerror(errno));
735         CloseHandle(sync_pipe[PIPE_READ]);
736         CloseHandle(sync_pipe[PIPE_WRITE]);
737         for (i = 0; argv[i] != NULL; i++) {
738             g_free( (gpointer) argv[i]);
739         }
740         g_free( (gpointer) argv);
741         return -1;
742     }
743
744     /* Create a pipe for the child process to send us data */
745     /* (increase this value if you have trouble while fast capture file switches) */
746     if (! CreatePipe(&data_pipe[PIPE_READ], &data_pipe[PIPE_WRITE], &sa, 5120)) {
747         /* Couldn't create the message pipe between parent and child. */
748         *msg = g_strdup_printf("Couldn't create data pipe: %s",
749                                win32strerror(GetLastError()));
750         ws_close(*message_read_fd);    /* Should close sync_pipe[PIPE_READ] */
751         CloseHandle(sync_pipe[PIPE_WRITE]);
752         for (i = 0; argv[i] != NULL; i++) {
753             g_free( (gpointer) argv[i]);
754         }
755         g_free( (gpointer) argv);
756         return -1;
757     }
758
759     /*
760      * Associate a C run-time file handle with the Windows HANDLE for the
761      * read side of the data pipe.
762      *
763      * (See http://www.flounder.com/handles.htm for information on various
764      * types of file handle in C/C++ on Windows.)
765      */
766     *data_read_fd = _open_osfhandle( (intptr_t) data_pipe[PIPE_READ], _O_BINARY);
767     if (*data_read_fd == -1) {
768         *msg = g_strdup_printf("Couldn't get C file handle for data read pipe: %s", g_strerror(errno));
769         CloseHandle(data_pipe[PIPE_READ]);
770         CloseHandle(data_pipe[PIPE_WRITE]);
771         ws_close(*message_read_fd);    /* Should close sync_pipe[PIPE_READ] */
772         CloseHandle(sync_pipe[PIPE_WRITE]);
773         for (i = 0; argv[i] != NULL; i++) {
774             g_free( (gpointer) argv[i]);
775         }
776         g_free( (gpointer) argv);
777         return -1;
778     }
779
780     /* init STARTUPINFO & PROCESS_INFORMATION */
781     memset(&si, 0, sizeof(si));
782     si.cb           = sizeof(si);
783     memset(&pi, 0, sizeof(pi));
784 #ifdef DEBUG_CHILD
785     si.dwFlags = STARTF_USESHOWWINDOW;
786     si.wShowWindow  = SW_SHOW;
787 #else
788     si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
789     si.wShowWindow  = SW_HIDE;  /* this hides the console window */
790     si.hStdInput = NULL; /* handle for named pipe*/
791
792     si.hStdOutput = data_pipe[PIPE_WRITE];
793     si.hStdError = sync_pipe[PIPE_WRITE];
794 #endif
795
796     /* convert args array into a single string */
797     /* XXX - could change sync_pipe_add_arg() instead */
798     /* there is a drawback here: the length is internally limited to 1024 bytes */
799     for(i=0; argv[i] != 0; i++) {
800         if(i != 0) g_string_append_c(args, ' ');    /* don't prepend a space before the path!!! */
801         quoted_arg = protect_arg(argv[i]);
802         g_string_append(args, quoted_arg);
803         g_free(quoted_arg);
804     }
805     wcommandline = g_utf8_to_utf16(args->str, (glong)args->len, NULL, NULL, NULL);
806
807     /* call dumpcap */
808     if(!CreateProcess(utf_8to16(argv[0]), wcommandline, NULL, NULL, TRUE,
809                       CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
810         *msg = g_strdup_printf("Couldn't run %s in child process: %s",
811                                args->str, win32strerror(GetLastError()));
812         ws_close(*data_read_fd);       /* Should close data_pipe[PIPE_READ] */
813         CloseHandle(data_pipe[PIPE_WRITE]);
814         ws_close(*message_read_fd);    /* Should close sync_pipe[PIPE_READ] */
815         CloseHandle(sync_pipe[PIPE_WRITE]);
816         for (i = 0; argv[i] != NULL; i++) {
817             g_free( (gpointer) argv[i]);
818         }
819         g_free( (gpointer) argv);
820         g_string_free(args, TRUE);
821         g_free(wcommandline);
822         return -1;
823     }
824     *fork_child = pi.hProcess;
825     /* We may need to store this and close it later */
826     CloseHandle(pi.hThread);
827     g_string_free(args, TRUE);
828     g_free(wcommandline);
829 #else /* _WIN32 */
830     /* Create a pipe for the child process to send us messages */
831     if (pipe(sync_pipe) < 0) {
832         /* Couldn't create the message pipe between parent and child. */
833         *msg = g_strdup_printf("Couldn't create sync pipe: %s", g_strerror(errno));
834         for (i = 0; argv[i] != NULL; i++) {
835             g_free( (gpointer) argv[i]);
836         }
837         g_free(argv);
838         return -1;
839     }
840
841     /* Create a pipe for the child process to send us data */
842     if (pipe(data_pipe) < 0) {
843         /* Couldn't create the data pipe between parent and child. */
844         *msg = g_strdup_printf("Couldn't create data pipe: %s", g_strerror(errno));
845         ws_close(sync_pipe[PIPE_READ]);
846         ws_close(sync_pipe[PIPE_WRITE]);
847         for (i = 0; argv[i] != NULL; i++) {
848             g_free( (gpointer) argv[i]);
849         }
850         g_free(argv);
851         return -1;
852     }
853
854     if ((*fork_child = fork()) == 0) {
855         /*
856          * Child process - run dumpcap with the right arguments to make
857          * it just capture with the specified capture parameters
858          */
859         dup2(data_pipe[PIPE_WRITE], 1);
860         ws_close(data_pipe[PIPE_READ]);
861         ws_close(data_pipe[PIPE_WRITE]);
862         dup2(sync_pipe[PIPE_WRITE], 2);
863         ws_close(sync_pipe[PIPE_READ]);
864         ws_close(sync_pipe[PIPE_WRITE]);
865         execv(argv[0], argv);
866         g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
867                    argv[0], g_strerror(errno));
868         sync_pipe_errmsg_to_parent(2, errmsg, "");
869
870         /* Exit with "_exit()", so that we don't close the connection
871            to the X server (and cause stuff buffered up by our parent but
872            not yet sent to be sent, as that stuff should only be sent by
873            our parent).  We've sent an error message to the parent, so
874            we exit with an exit status of 1 (any exit status other than
875            0 or 1 will cause an additional message to report that exit
876            status, over and above the error message we sent to the parent). */
877         _exit(1);
878     }
879
880     if (fetch_dumpcap_pid && *fork_child > 0)
881         fetch_dumpcap_pid(*fork_child);
882
883     *data_read_fd = data_pipe[PIPE_READ];
884     *message_read_fd = sync_pipe[PIPE_READ];
885 #endif
886
887     for (i = 0; argv[i] != NULL; i++) {
888         g_free( (gpointer) argv[i]);
889     }
890
891     /* Parent process - read messages from the child process over the
892        sync pipe. */
893     g_free( (gpointer) argv);   /* free up arg array */
894
895     /* Close the write sides of the pipes, so that only the child has them
896        open, and thus they completely close, and thus return to us
897        an EOF indication, if the child closes them (either deliberately
898        or by exiting abnormally). */
899 #ifdef _WIN32
900     CloseHandle(data_pipe[PIPE_WRITE]);
901     CloseHandle(sync_pipe[PIPE_WRITE]);
902 #else
903     ws_close(data_pipe[PIPE_WRITE]);
904     ws_close(sync_pipe[PIPE_WRITE]);
905 #endif
906
907     if (*fork_child == WS_INVALID_PID) {
908         /* We couldn't even create the child process. */
909         *msg = g_strdup_printf("Couldn't create child process: %s", g_strerror(errno));
910         ws_close(*data_read_fd);
911         ws_close(*message_read_fd);
912         return -1;
913     }
914
915     /* we might wait for a moment till child is ready, so update screen now */
916     if (update_cb) update_cb();
917     return 0;
918 }
919
920 /*
921  * Close the pipes we're using to read from dumpcap, and wait for it
922  * to exit.  On success, *msgp is unchanged, and the exit status of
923  * dumpcap is returned.  On failure (which includes "dumpcap exited
924  * due to being killed by a signal or an exception"), *msgp points
925  * to an error message for the failure, and -1 is returned.  In the
926  * latter case, *msgp must be freed with g_free().
927  */
928 static int
929 sync_pipe_close_command(int *data_read_fd, int *message_read_fd,
930         ws_process_id *fork_child, gchar **msgp)
931 {
932     ws_close(*data_read_fd);
933     if (message_read_fd != NULL)
934         ws_close(*message_read_fd);
935
936 #ifdef _WIN32
937     /* XXX - Should we signal the child somehow? */
938     sync_pipe_kill(*fork_child);
939 #endif
940
941     return sync_pipe_wait_for_child(*fork_child, msgp);
942 }
943
944 /*
945  * Run dumpcap with the supplied arguments.
946  *
947  * On success, *data points to a buffer containing the dumpcap output,
948  * *primary_msg and *secondary_message are NULL, and 0 is returned; *data
949  * must be freed with g_free().
950  *
951  * On failure, *data is NULL, *primary_msg points to an error message,
952  * *secondary_msg either points to an additional error message or is
953  * NULL, and -1 is returned; *primary_msg, and *secondary_msg if not NULL,
954  * must be freed with g_free().
955  */
956 /* XXX - This duplicates a lot of code in sync_pipe_start() */
957 /* XXX - assumes PIPE_BUF_SIZE > SP_MAX_MSG_LEN */
958 #define PIPE_BUF_SIZE 5120
959 static int
960 sync_pipe_run_command_actual(char** argv, gchar **data, gchar **primary_msg,
961                       gchar **secondary_msg,  void(*update_cb)(void))
962 {
963     gchar *msg;
964     int data_pipe_read_fd, sync_pipe_read_fd, ret;
965     ws_process_id fork_child;
966     char *wait_msg;
967     gchar buffer[PIPE_BUF_SIZE+1] = {0};
968     ssize_t nread;
969     char indicator;
970     int  primary_msg_len;
971     char *primary_msg_text;
972     int  secondary_msg_len;
973     char *secondary_msg_text;
974     char *combined_msg;
975     GString *data_buf = NULL;
976     ssize_t count;
977
978     ret = sync_pipe_open_command(argv, &data_pipe_read_fd, &sync_pipe_read_fd,
979                                  &fork_child, &msg, update_cb);
980     if (ret == -1) {
981         *primary_msg = msg;
982         *secondary_msg = NULL;
983         *data = NULL;
984         return -1;
985     }
986
987     /*
988      * We were able to set up to read dumpcap's output.  Do so.
989      *
990      * First, wait for an SP_ERROR_MSG message or SP_SUCCESS message.
991      */
992     nread = pipe_read_block(sync_pipe_read_fd, &indicator, SP_MAX_MSG_LEN,
993                             buffer, primary_msg);
994     if(nread <= 0) {
995         /* We got a read error from the sync pipe, or we got no data at
996            all from the sync pipe, so we're not going to be getting any
997            data or error message from the child process.  Pick up its
998            exit status, and complain.
999
1000            We don't have to worry about killing the child, if the sync pipe
1001            returned an error. Usually this error is caused as the child killed
1002            itself while going down. Even in the rare cases that this isn't the
1003            case, the child will get an error when writing to the broken pipe
1004            the next time, cleaning itself up then. */
1005         ret = sync_pipe_wait_for_child(fork_child, &wait_msg);
1006         if(nread == 0) {
1007             /* We got an EOF from the sync pipe.  That means that it exited
1008                before giving us any data to read.  If ret is -1, we report
1009                that as a bad exit (e.g., exiting due to a signal); otherwise,
1010                we report it as a premature exit. */
1011             if (ret == -1)
1012                 *primary_msg = wait_msg;
1013             else
1014                 *primary_msg = g_strdup("Child dumpcap closed sync pipe prematurely");
1015         } else {
1016             /* We got an error from the sync pipe.  If ret is -1, report
1017                both the sync pipe I/O error and the wait error. */
1018             if (ret == -1) {
1019                 combined_msg = g_strdup_printf("%s\n\n%s", *primary_msg, wait_msg);
1020                 g_free(*primary_msg);
1021                 g_free(wait_msg);
1022                 *primary_msg = combined_msg;
1023             }
1024         }
1025         *secondary_msg = NULL;
1026         *data = NULL;
1027
1028         return -1;
1029     }
1030
1031     /* we got a valid message block from the child, process it */
1032     switch(indicator) {
1033
1034     case SP_ERROR_MSG:
1035         /*
1036          * Error from dumpcap; there will be a primary message and a
1037          * secondary message.
1038          */
1039
1040         /* convert primary message */
1041         pipe_convert_header((guchar*)buffer, 4, &indicator, &primary_msg_len);
1042         primary_msg_text = buffer+4;
1043         /* convert secondary message */
1044         pipe_convert_header((guchar*)primary_msg_text + primary_msg_len, 4, &indicator,
1045                             &secondary_msg_len);
1046         secondary_msg_text = primary_msg_text + primary_msg_len + 4;
1047         /* the capture child will close the sync_pipe, nothing to do */
1048
1049         /*
1050          * Pick up the child status.
1051          */
1052         ret = sync_pipe_close_command(&data_pipe_read_fd, &sync_pipe_read_fd,
1053                                       &fork_child, &msg);
1054         if (ret == -1) {
1055             /*
1056              * Child process failed unexpectedly, or wait failed; msg is the
1057              * error message.
1058              */
1059             *primary_msg = msg;
1060             *secondary_msg = NULL;
1061         } else {
1062             /*
1063              * Child process failed, but returned the expected exit status.
1064              * Return the messages it gave us, and indicate failure.
1065              */
1066             *primary_msg = g_strdup(primary_msg_text);
1067             *secondary_msg = g_strdup(secondary_msg_text);
1068             ret = -1;
1069         }
1070         *data = NULL;
1071         break;
1072
1073     case SP_SUCCESS:
1074         /* read the output from the command */
1075         data_buf = g_string_new("");
1076         while ((count = ws_read(data_pipe_read_fd, buffer, PIPE_BUF_SIZE)) > 0) {
1077             buffer[count] = '\0';
1078             g_string_append(data_buf, buffer);
1079         }
1080
1081         /*
1082          * Pick up the child status.
1083          */
1084         ret = sync_pipe_close_command(&data_pipe_read_fd, &sync_pipe_read_fd,
1085                                       &fork_child, &msg);
1086         if (ret == -1) {
1087             /*
1088              * Child process failed unexpectedly, or wait failed; msg is the
1089              * error message.
1090              */
1091             *primary_msg = msg;
1092             *secondary_msg = NULL;
1093             g_string_free(data_buf, TRUE);
1094             *data = NULL;
1095         } else {
1096             /*
1097              * Child process succeeded.
1098              */
1099             *primary_msg = NULL;
1100             *secondary_msg = NULL;
1101             *data = g_string_free(data_buf, FALSE);
1102         }
1103         break;
1104
1105     default:
1106         /*
1107          * Pick up the child status.
1108          */
1109         ret = sync_pipe_close_command(&data_pipe_read_fd, &sync_pipe_read_fd,
1110                                       &fork_child, &msg);
1111         if (ret == -1) {
1112             /*
1113              * Child process failed unexpectedly, or wait failed; msg is the
1114              * error message.
1115              */
1116             *primary_msg = msg;
1117             *secondary_msg = NULL;
1118         } else {
1119             /*
1120              * Child process returned an unknown status.
1121              */
1122             *primary_msg = g_strdup_printf("dumpcap process gave an unexpected message type: 0x%02x",
1123                                            indicator);
1124             *secondary_msg = NULL;
1125             ret = -1;
1126         }
1127         *data = NULL;
1128         break;
1129     }
1130     return ret;
1131 }
1132
1133 /* centralised logging and timing for sync_pipe_run_command_actual(),
1134 * redirects to sync_pipe_run_command_actual()
1135 */
1136 static int
1137 sync_pipe_run_command(char** argv, gchar **data, gchar **primary_msg,
1138                       gchar **secondary_msg, void (*update_cb)(void))
1139 {
1140     int ret, i;
1141     GTimeVal start_time;
1142     GTimeVal end_time;
1143     float elapsed;
1144     int logging_enabled;
1145
1146     /* check if logging is actually enabled, otherwise don't expend the CPU generating logging */
1147     logging_enabled=( (G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO) & G_LOG_LEVEL_MASK & prefs.console_log_level);
1148     if(logging_enabled){
1149         g_get_current_time(&start_time);
1150         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "sync_pipe_run_command() starts");
1151         for(i=0; argv[i] != 0; i++) {
1152             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  argv[%d]: %s", i, argv[i]);
1153         }
1154     }
1155     /* do the actual sync pipe run command */
1156     ret=sync_pipe_run_command_actual(argv, data, primary_msg, secondary_msg, update_cb);
1157
1158     if(logging_enabled){
1159         g_get_current_time(&end_time);
1160         elapsed = (float) ((end_time.tv_sec - start_time.tv_sec) +
1161                            ((end_time.tv_usec - start_time.tv_usec) / 1e6));
1162
1163         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "sync_pipe_run_command() ends, taking %.3fs, result=%d", elapsed, ret);
1164
1165     }
1166     return ret;
1167 }
1168
1169
1170 int
1171 sync_interface_set_80211_chan(const gchar *iface, const char *freq, const gchar *type,
1172                               const gchar *center_freq1, const gchar *center_freq2,
1173                               gchar **data, gchar **primary_msg,
1174                               gchar **secondary_msg, void (*update_cb)(void))
1175 {
1176     int argc, ret;
1177     char **argv;
1178     gchar *opt;
1179
1180     argv = init_pipe_args(&argc);
1181
1182     if (!argv) {
1183         *primary_msg = g_strdup("We don't know where to find dumpcap.");
1184         *secondary_msg = NULL;
1185         *data = NULL;
1186         return -1;
1187     }
1188
1189     argv = sync_pipe_add_arg(argv, &argc, "-i");
1190     argv = sync_pipe_add_arg(argv, &argc, iface);
1191
1192     if (type)
1193         opt = g_strdup_printf("%s,%s,%s,%s", freq, type, center_freq1, center_freq2);
1194     else
1195         opt = g_strdup(freq);
1196
1197     if (!opt) {
1198         *primary_msg = g_strdup("Out of mem.");
1199         *secondary_msg = NULL;
1200         *data = NULL;
1201         return -1;
1202     }
1203
1204     argv = sync_pipe_add_arg(argv, &argc, "-k");
1205     argv = sync_pipe_add_arg(argv, &argc, opt);
1206
1207 #ifndef DEBUG_CHILD
1208     /* Run dumpcap in capture child mode */
1209     argv = sync_pipe_add_arg(argv, &argc, "-Z");
1210     argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
1211 #endif
1212
1213     ret = sync_pipe_run_command(argv, data, primary_msg, secondary_msg, update_cb);
1214     g_free(opt);
1215     return ret;
1216 }
1217
1218 /*
1219  * Get the list of interfaces using dumpcap.
1220  *
1221  * On success, *data points to a buffer containing the dumpcap output,
1222  * *primary_msg and *secondary_msg are NULL, and 0 is returned.  *data
1223  * must be freed with g_free().
1224  *
1225  * On failure, *data is NULL, *primary_msg points to an error message,
1226  * *secondary_msg either points to an additional error message or is
1227  * NULL, and -1 is returned; *primary_msg, and *secondary_msg if not NULL,
1228  * must be freed with g_free().
1229  */
1230 int
1231 sync_interface_list_open(gchar **data, gchar **primary_msg,
1232                          gchar **secondary_msg, void (*update_cb)(void))
1233 {
1234     int argc;
1235     char **argv;
1236
1237     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_list_open");
1238
1239     argv = init_pipe_args(&argc);
1240
1241     if (!argv) {
1242         *primary_msg = g_strdup("We don't know where to find dumpcap..");
1243         *secondary_msg = NULL;
1244         *data = NULL;
1245         return -1;
1246     }
1247
1248     /* Ask for the interface list */
1249     argv = sync_pipe_add_arg(argv, &argc, "-D");
1250
1251 #ifndef DEBUG_CHILD
1252     /* Run dumpcap in capture child mode */
1253     argv = sync_pipe_add_arg(argv, &argc, "-Z");
1254     argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
1255 #endif
1256     return sync_pipe_run_command(argv, data, primary_msg, secondary_msg, update_cb);
1257 }
1258
1259 /*
1260  * Get the capabilities of an interface using dumpcap.
1261  *
1262  * On success, *data points to a buffer containing the dumpcap output,
1263  * *primary_msg and *secondary_msg are NULL, and 0 is returned.  *data
1264  * must be freed with g_free().
1265  *
1266  * On failure, *data is NULL, *primary_msg points to an error message,
1267  * *secondary_msg either points to an additional error message or is
1268  * NULL, and -1 is returned; *primary_msg, and *secondary_msg if not NULL,
1269  * must be freed with g_free().
1270  */
1271 int
1272 sync_if_capabilities_open(const gchar *ifname, gboolean monitor_mode, const gchar* auth,
1273                           gchar **data, gchar **primary_msg,
1274                           gchar **secondary_msg, void (*update_cb)(void))
1275 {
1276     int argc;
1277     char **argv;
1278
1279     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_if_capabilities_open");
1280
1281     argv = init_pipe_args(&argc);
1282
1283     if (!argv) {
1284         *primary_msg = g_strdup("We don't know where to find dumpcap.");
1285         *secondary_msg = NULL;
1286         *data = NULL;
1287         return -1;
1288     }
1289
1290     /* Ask for the interface capabilities */
1291     argv = sync_pipe_add_arg(argv, &argc, "-i");
1292     argv = sync_pipe_add_arg(argv, &argc, ifname);
1293     argv = sync_pipe_add_arg(argv, &argc, "-L");
1294     if (monitor_mode)
1295         argv = sync_pipe_add_arg(argv, &argc, "-I");
1296     if (auth) {
1297         argv = sync_pipe_add_arg(argv, &argc, "-A");
1298         argv = sync_pipe_add_arg(argv, &argc, auth);
1299     }
1300
1301 #ifndef DEBUG_CHILD
1302     /* Run dumpcap in capture child mode */
1303     argv = sync_pipe_add_arg(argv, &argc, "-Z");
1304     argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
1305 #endif
1306     return sync_pipe_run_command(argv, data, primary_msg, secondary_msg, update_cb);
1307 }
1308
1309 /*
1310  * Start getting interface statistics using dumpcap.  On success, read_fd
1311  * contains the file descriptor for the pipe's stdout, *msg is unchanged,
1312  * and zero is returned.  On failure, *msg will point to an error message
1313  * that must be g_free()d, and -1 will be returned.
1314  */
1315 int
1316 sync_interface_stats_open(int *data_read_fd, ws_process_id *fork_child, gchar **msg, void (*update_cb)(void))
1317 {
1318     int argc;
1319     char **argv;
1320     int message_read_fd, ret;
1321     char *wait_msg;
1322     gchar buffer[PIPE_BUF_SIZE+1] = {0};
1323     ssize_t nread;
1324     char indicator;
1325     int  primary_msg_len;
1326     char *primary_msg_text;
1327     int  secondary_msg_len;
1328     /*char *secondary_msg_text;*/
1329     char *combined_msg;
1330
1331     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_stats_open");
1332
1333     argv = init_pipe_args(&argc);
1334
1335     if (!argv) {
1336         *msg = g_strdup("We don't know where to find dumpcap.");
1337         return -1;
1338     }
1339
1340     /* Ask for the interface statistics */
1341     argv = sync_pipe_add_arg(argv, &argc, "-S");
1342
1343 #ifndef DEBUG_CHILD
1344     argv = sync_pipe_add_arg(argv, &argc, "-Z");
1345 #ifdef _WIN32
1346     create_dummy_signal_pipe();
1347     argv = sync_pipe_add_arg(argv, &argc, dummy_control_id);
1348 #else
1349     argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
1350 #endif
1351 #endif
1352     ret = sync_pipe_open_command(argv, data_read_fd, &message_read_fd,
1353                                  fork_child, msg, update_cb);
1354     if (ret == -1)
1355         return -1;
1356
1357     /*
1358      * We were able to set up to read dumpcap's output.  Do so.
1359      *
1360      * First, wait for an SP_ERROR_MSG message or SP_SUCCESS message.
1361      */
1362     nread = pipe_read_block(message_read_fd, &indicator, SP_MAX_MSG_LEN,
1363                             buffer, msg);
1364     if(nread <= 0) {
1365         /* We got a read error from the sync pipe, or we got no data at
1366            all from the sync pipe, so we're not going to be getting any
1367            data or error message from the child process.  Pick up its
1368            exit status, and complain.
1369
1370            We don't have to worry about killing the child, if the sync pipe
1371            returned an error. Usually this error is caused as the child killed
1372            itself while going down. Even in the rare cases that this isn't the
1373            case, the child will get an error when writing to the broken pipe
1374            the next time, cleaning itself up then. */
1375         ret = sync_pipe_wait_for_child(*fork_child, &wait_msg);
1376         ws_close(message_read_fd);
1377         ws_close(*data_read_fd);
1378         if(nread == 0) {
1379             /* We got an EOF from the sync pipe.  That means that it exited
1380                before giving us any data to read.  If ret is -1, we report
1381                that as a bad exit (e.g., exiting due to a signal); otherwise,
1382                we report it as a premature exit. */
1383             if (ret == -1)
1384                 *msg = wait_msg;
1385             else
1386                 *msg = g_strdup("Child dumpcap closed sync pipe prematurely");
1387         } else {
1388             /* We got an error from the sync pipe.  If ret is -1, report
1389                both the sync pipe I/O error and the wait error. */
1390             if (ret == -1) {
1391                 combined_msg = g_strdup_printf("%s\n\n%s", *msg, wait_msg);
1392                 g_free(*msg);
1393                 g_free(wait_msg);
1394                 *msg = combined_msg;
1395             }
1396         }
1397
1398         return -1;
1399     }
1400
1401     /* we got a valid message block from the child, process it */
1402     switch(indicator) {
1403
1404     case SP_ERROR_MSG:
1405         /*
1406          * Error from dumpcap; there will be a primary message and a
1407          * secondary message.
1408          */
1409
1410         /* convert primary message */
1411         pipe_convert_header((guchar*)buffer, 4, &indicator, &primary_msg_len);
1412         primary_msg_text = buffer+4;
1413         /* convert secondary message */
1414         pipe_convert_header((guchar*)primary_msg_text + primary_msg_len, 4, &indicator,
1415                             &secondary_msg_len);
1416         /*secondary_msg_text = primary_msg_text + primary_msg_len + 4;*/
1417         /* the capture child will close the sync_pipe, nothing to do */
1418
1419         /*
1420          * Pick up the child status.
1421          */
1422         ret = sync_pipe_close_command(data_read_fd, &message_read_fd,
1423                                       fork_child, msg);
1424         if (ret == -1) {
1425             /*
1426              * Child process failed unexpectedly, or wait failed; msg is the
1427              * error message.
1428              */
1429         } else {
1430             /*
1431              * Child process failed, but returned the expected exit status.
1432              * Return the messages it gave us, and indicate failure.
1433              */
1434             *msg = g_strdup(primary_msg_text);
1435             ret = -1;
1436         }
1437         break;
1438
1439     case SP_SUCCESS:
1440         /* Close the message pipe. */
1441         ws_close(message_read_fd);
1442         break;
1443
1444     default:
1445         /*
1446          * Pick up the child status.
1447          */
1448         ret = sync_pipe_close_command(data_read_fd, &message_read_fd,
1449                                       fork_child, msg);
1450         if (ret == -1) {
1451             /*
1452              * Child process failed unexpectedly, or wait failed; msg is the
1453              * error message.
1454              */
1455         } else {
1456             /*
1457              * Child process returned an unknown status.
1458              */
1459             *msg = g_strdup_printf("dumpcap process gave an unexpected message type: 0x%02x",
1460                                    indicator);
1461             ret = -1;
1462         }
1463         break;
1464     }
1465     return ret;
1466 }
1467
1468 /* Close down the stats process */
1469 int
1470 sync_interface_stats_close(int *read_fd, ws_process_id *fork_child, gchar **msg)
1471 {
1472 #ifndef _WIN32
1473     /*
1474      * Don't bother waiting for the child. sync_pipe_close_command
1475      * does this for us on Windows.
1476      */
1477     sync_pipe_kill(*fork_child);
1478 #endif
1479     return sync_pipe_close_command(read_fd, NULL, fork_child, msg);
1480 }
1481
1482 /* read a number of bytes from a pipe */
1483 /* (blocks until enough bytes read or an error occurs) */
1484 static ssize_t
1485 pipe_read_bytes(int pipe_fd, char *bytes, int required, char **msg)
1486 {
1487     ssize_t newly;
1488     ssize_t offset = 0;
1489     int error;
1490
1491     while(required) {
1492         newly = ws_read(pipe_fd, &bytes[offset], required);
1493         if (newly == 0) {
1494             /* EOF */
1495             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1496                   "read from pipe %d: EOF (capture closed?)", pipe_fd);
1497             *msg = 0;
1498             return offset;
1499         }
1500         if (newly < 0) {
1501             /* error */
1502             error = errno;
1503             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1504                   "read from pipe %d: error(%u): %s", pipe_fd, error,
1505                   g_strerror(error));
1506             *msg = g_strdup_printf("Error reading from sync pipe: %s",
1507                                    g_strerror(error));
1508             return newly;
1509         }
1510
1511         required -= (int)newly;
1512         offset += newly;
1513     }
1514
1515     *msg = NULL;
1516     return offset;
1517 }
1518
1519 static gboolean pipe_data_available(int pipe_fd) {
1520 #ifdef _WIN32 /* PeekNamedPipe */
1521     HANDLE hPipe = (HANDLE) _get_osfhandle(pipe_fd);
1522     DWORD bytes_avail;
1523
1524     if (hPipe == INVALID_HANDLE_VALUE)
1525         return FALSE;
1526
1527     if (! PeekNamedPipe(hPipe, NULL, 0, NULL, &bytes_avail, NULL))
1528         return FALSE;
1529
1530     if (bytes_avail > 0)
1531         return TRUE;
1532     return FALSE;
1533 #else /* select */
1534     fd_set rfds;
1535     struct timeval timeout;
1536
1537     FD_ZERO(&rfds);
1538     FD_SET(pipe_fd, &rfds);
1539     timeout.tv_sec = 0;
1540     timeout.tv_usec = 0;
1541
1542     if (select(pipe_fd+1, &rfds, NULL, NULL, &timeout) > 0)
1543         return TRUE;
1544
1545     return FALSE;
1546 #endif
1547 }
1548
1549 /* Read a line from a pipe, similar to fgets */
1550 int
1551 sync_pipe_gets_nonblock(int pipe_fd, char *bytes, int max) {
1552     ssize_t newly;
1553     int offset = -1;
1554
1555     while(offset < max - 1) {
1556         offset++;
1557         if (! pipe_data_available(pipe_fd))
1558             break;
1559         newly = ws_read(pipe_fd, &bytes[offset], 1);
1560         if (newly == 0) {
1561             /* EOF - not necessarily an error */
1562             break;
1563         } else if (newly == -1) {
1564             /* error */
1565             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1566                   "read from pipe %d: error(%u): %s", pipe_fd, errno, g_strerror(errno));
1567             return -1;
1568         } else if (bytes[offset] == '\n') {
1569             break;
1570         }
1571     }
1572
1573     if (offset >= 0)
1574         bytes[offset] = '\0';
1575
1576     return offset;
1577 }
1578
1579
1580 /* convert header values (indicator and 3-byte length) */
1581 static void
1582 pipe_convert_header(const guchar *header, int header_len, char *indicator, int *block_len) {
1583
1584     g_assert(header_len == 4);
1585
1586     /* convert header values */
1587     *indicator = header[0];
1588     *block_len = (header[1]&0xFF)<<16 | (header[2]&0xFF)<<8 | (header[3]&0xFF);
1589 }
1590
1591 /* read a message from the sending pipe in the standard format
1592    (1-byte message indicator, 3-byte message length (excluding length
1593    and indicator field), and the rest is the message) */
1594 static ssize_t
1595 pipe_read_block(int pipe_fd, char *indicator, int len, char *msg,
1596                 char **err_msg)
1597 {
1598     int required;
1599     ssize_t newly;
1600     gchar header[4];
1601
1602     /* read header (indicator and 3-byte length) */
1603     newly = pipe_read_bytes(pipe_fd, header, 4, err_msg);
1604     if(newly != 4) {
1605         if (newly == 0) {
1606             /*
1607              * Immediate EOF; if the capture child exits normally, this
1608              * is an "I'm done" indication, so don't report it as an
1609              * error.
1610              */
1611             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1612                   "read %d got an EOF", pipe_fd);
1613             return 0;
1614         }
1615         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1616               "read %d failed to read header: %lu", pipe_fd, (long)newly);
1617         if (newly != -1) {
1618             /*
1619              * Short read, but not an immediate EOF.
1620              */
1621             *err_msg = g_strdup_printf("Premature EOF reading from sync pipe: got only %ld bytes",
1622                                        (long)newly);
1623         }
1624         return -1;
1625     }
1626
1627     /* convert header values */
1628     pipe_convert_header((guchar*)header, 4, indicator, &required);
1629
1630     /* only indicator with no value? */
1631     if(required == 0) {
1632         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1633               "read %d indicator: %c empty value", pipe_fd, *indicator);
1634         return 4;
1635     }
1636
1637     /* does the data fit into the given buffer? */
1638     if(required > len) {
1639         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1640               "read %d length error, required %d > len %d, header: 0x%02x 0x%02x 0x%02x 0x%02x",
1641               pipe_fd, required, len,
1642               header[0], header[1], header[2], header[3]);
1643
1644         /* we have a problem here, try to read some more bytes from the pipe to debug where the problem really is */
1645         memcpy(msg, header, sizeof(header));
1646         newly = ws_read(pipe_fd, &msg[sizeof(header)], len-sizeof(header));
1647         if (newly < 0) { /* error */
1648             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1649                   "read from pipe %d: error(%u): %s", pipe_fd, errno, g_strerror(errno));
1650         }
1651         *err_msg = g_strdup_printf("Unknown message from dumpcap, try to show it as a string: %s",
1652                                    msg);
1653         return -1;
1654     }
1655     len = required;
1656
1657     /* read the actual block data */
1658     newly = pipe_read_bytes(pipe_fd, msg, required, err_msg);
1659     if(newly != required) {
1660         if (newly != -1) {
1661             *err_msg = g_strdup_printf("Unknown message from dumpcap, try to show it as a string: %s",
1662                                        msg);
1663         }
1664         return -1;
1665     }
1666
1667     /* XXX If message is "2part", the msg probably won't be sent to debug log correctly */
1668     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1669           "read %d ok indicator: %c len: %u msg: %s", pipe_fd, *indicator,
1670           len, msg);
1671     *err_msg = NULL;
1672     return newly + 4;
1673 }
1674
1675
1676 /* There's stuff to read from the sync pipe, meaning the child has sent
1677    us a message, or the sync pipe has closed, meaning the child has
1678    closed it (perhaps because it exited). */
1679 static gboolean
1680 sync_pipe_input_cb(gint source, gpointer user_data)
1681 {
1682     capture_session *cap_session = (capture_session *)user_data;
1683     int  ret;
1684     char buffer[SP_MAX_MSG_LEN+1] = {0};
1685     ssize_t nread;
1686     char indicator;
1687     int  primary_len;
1688     char *primary_msg;
1689     int  secondary_len;
1690     char *secondary_msg;
1691     char *wait_msg, *combined_msg;
1692     guint32 npackets = 0;
1693
1694     nread = pipe_read_block(source, &indicator, SP_MAX_MSG_LEN, buffer,
1695                             &primary_msg);
1696     if(nread <= 0) {
1697         /* We got a read error, or a bad message, or an EOF, from the sync pipe.
1698
1699            If we got a read error or a bad message, nread is -1 and
1700            primary_msg is set to point to an error message.  We don't
1701            have to worry about killing the child; usually this error
1702            is caused as the child killed  itself while going down.
1703            Even in the rare cases that this isn't the case, the child
1704            will get an error when writing to the broken pipe the next time,
1705            cleaning itself up then.
1706
1707            If we got an EOF, nread is 0 and primary_msg isn't set.  This
1708            is an indication that the capture is finished. */
1709         ret = sync_pipe_wait_for_child(cap_session->fork_child, &wait_msg);
1710         if(nread == 0) {
1711             /* We got an EOF from the sync pipe.  That means that the capture
1712                child exited, and not in the middle of a message; we treat
1713                that as an indication that it's done, and only report an
1714                error if ret is -1, in which case wait_msg is the error
1715                message. */
1716             if (ret == -1)
1717                 primary_msg = wait_msg;
1718         } else {
1719             /* We got an error from the sync pipe.  If ret is -1, report
1720                both the sync pipe I/O error and the wait error. */
1721             if (ret == -1) {
1722                 combined_msg = g_strdup_printf("%s\n\n%s", primary_msg, wait_msg);
1723                 g_free(primary_msg);
1724                 g_free(wait_msg);
1725                 primary_msg = combined_msg;
1726             }
1727         }
1728
1729         /* No more child process. */
1730         cap_session->fork_child = WS_INVALID_PID;
1731         cap_session->fork_child_status = ret;
1732
1733 #ifdef _WIN32
1734         ws_close(cap_session->signal_pipe_write_fd);
1735 #endif
1736 #ifdef HAVE_EXTCAP
1737         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: cleaning extcap pipe");
1738         extcap_if_cleanup(cap_session->capture_opts, &primary_msg);
1739 #endif
1740         capture_input_closed(cap_session, primary_msg);
1741         g_free(primary_msg);
1742         return FALSE;
1743     }
1744
1745     /* we got a valid message block from the child, process it */
1746     switch(indicator) {
1747     case SP_FILE:
1748         if(!capture_input_new_file(cap_session, buffer)) {
1749             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: file failed, closing capture");
1750
1751             /* We weren't able to open the new capture file; user has been
1752                alerted. Close the sync pipe. */
1753             ws_close(source);
1754
1755             /* The child has sent us a filename which we couldn't open.
1756
1757                This could mean that the child is creating and deleting files
1758                (ring buffer mode) faster than we can handle it.
1759
1760                That should only be the case for very fast file switches;
1761                We can't do much more than telling the child to stop.
1762                (This is the "emergency brake" if the user e.g. wants to
1763                switch files every second).
1764
1765                This can also happen if the user specified "-", meaning
1766                "standard output", as the capture file. */
1767             sync_pipe_stop(cap_session);
1768             capture_input_closed(cap_session, NULL);
1769             return FALSE;
1770         }
1771         break;
1772     case SP_PACKET_COUNT:
1773         if (!ws_strtou32(buffer, NULL, &npackets)) {
1774             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Invalid packets number: %s", buffer);
1775         }
1776         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: new packets %u", npackets);
1777         cap_session->count += npackets;
1778         capture_input_new_packets(cap_session, npackets);
1779         break;
1780     case SP_ERROR_MSG:
1781         /* convert primary message */
1782         pipe_convert_header((guchar*)buffer, 4, &indicator, &primary_len);
1783         primary_msg = buffer+4;
1784         /* convert secondary message */
1785         pipe_convert_header((guchar*)primary_msg + primary_len, 4, &indicator, &secondary_len);
1786         secondary_msg = primary_msg + primary_len + 4;
1787         /* message output */
1788         capture_input_error_message(cap_session, primary_msg, secondary_msg);
1789         /* the capture child will close the sync_pipe, nothing to do for now */
1790         /* (an error message doesn't mean we have to stop capturing) */
1791         break;
1792     case SP_BAD_FILTER: {
1793         char *ch=NULL;
1794         int indx=0;
1795
1796         ch = strtok(buffer, ":");
1797         if (ch) {
1798            indx = (int)strtol(ch, NULL, 10);
1799            ch = strtok(NULL, ":");
1800         }
1801         capture_input_cfilter_error_message(cap_session, indx, ch);
1802         /* the capture child will close the sync_pipe, nothing to do for now */
1803         break;
1804         }
1805     case SP_DROPS:
1806         capture_input_drops(cap_session, (guint32)strtoul(buffer, NULL, 10));
1807         break;
1808     default:
1809         g_assert_not_reached();
1810     }
1811
1812     return TRUE;
1813 }
1814
1815
1816
1817 /*
1818  * dumpcap is exiting; wait for it to exit.  On success, *msgp is
1819  * unchanged, and the exit status of dumpcap is returned.  On
1820  * failure (which includes "dumpcap exited due to being killed by
1821  * a signal or an exception"), *msgp points to an error message
1822  * for the failure, and -1 is returned.  In the latter case, *msgp
1823  * must be freed with g_free().
1824  */
1825 static int
1826 sync_pipe_wait_for_child(ws_process_id fork_child, gchar **msgp)
1827 {
1828     int fork_child_status;
1829 #ifndef _WIN32
1830     int retry_waitpid = 3;
1831 #endif
1832     int ret = -1;
1833     GTimeVal start_time;
1834     GTimeVal end_time;
1835     float elapsed;
1836
1837     /*
1838      * GLIB_CHECK_VERSION(2,28,0) adds g_get_real_time which could minimize or
1839      * replace this
1840      */
1841     g_get_current_time(&start_time);
1842
1843     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: wait till child closed");
1844     g_assert(fork_child != WS_INVALID_PID);
1845
1846     *msgp = NULL; /* assume no error */
1847 #ifdef _WIN32
1848     if (_cwait(&fork_child_status, (intptr_t) fork_child, _WAIT_CHILD) == -1) {
1849         *msgp = g_strdup_printf("Error from cwait(): %s", g_strerror(errno));
1850         ret = -1;
1851     } else {
1852         /*
1853          * The child exited; return its exit status.  Do not treat this as
1854          * an error.
1855          */
1856         ret = fork_child_status;
1857         if ((fork_child_status & 0xC0000000) == ERROR_SEVERITY_ERROR) {
1858             /* Probably an exception code */
1859             *msgp = g_strdup_printf("Child dumpcap process died: %s",
1860                                     win32strexception(fork_child_status));
1861             ret = -1;
1862         }
1863     }
1864 #else
1865     while (--retry_waitpid >= 0) {
1866         if (waitpid(fork_child, &fork_child_status, 0) != -1) {
1867             /* waitpid() succeeded */
1868             if (WIFEXITED(fork_child_status)) {
1869                 /*
1870                  * The child exited; return its exit status.  Do not treat this as
1871                  * an error.
1872                  */
1873                 ret = WEXITSTATUS(fork_child_status);
1874             } else if (WIFSTOPPED(fork_child_status)) {
1875                 /* It stopped, rather than exiting.  "Should not happen." */
1876                 *msgp = g_strdup_printf("Child dumpcap process stopped: %s",
1877                                         sync_pipe_signame(WSTOPSIG(fork_child_status)));
1878                 ret = -1;
1879             } else if (WIFSIGNALED(fork_child_status)) {
1880                 /* It died with a signal. */
1881                 *msgp = g_strdup_printf("Child dumpcap process died: %s%s",
1882                                         sync_pipe_signame(WTERMSIG(fork_child_status)),
1883                                         WCOREDUMP(fork_child_status) ? " - core dumped" : "");
1884                 ret = -1;
1885             } else {
1886                 /* What?  It had to either have exited, or stopped, or died with
1887                    a signal; what happened here? */
1888                 *msgp = g_strdup_printf("Bad status from waitpid(): %#o",
1889                                         fork_child_status);
1890                 ret = -1;
1891             }
1892         } else {
1893             /* waitpid() failed */
1894             if (errno == EINTR) {
1895                 /*
1896                  * Signal interrupted waitpid().
1897                  *
1898                  * If it's SIGALRM, we just want to keep waiting, in case
1899                  * there's some timer using it (e.g., in a GUI toolkit).
1900                  *
1901                  * If you ^C TShark (or Wireshark), that should deliver
1902                  * SIGINT to dumpcap as well.  dumpcap catches SIGINT,
1903                  * and should clean up and exit, so we should eventually
1904                  * see that and clean up and terminate.
1905                  *
1906                  * If we're sent a SIGTERM, we should (and do) catch it,
1907                  * and TShark, at least, calls sync_pipe_stop(). which
1908                  * kills dumpcap, so we should eventually see that and
1909                  * clean up and terminate.
1910                  */
1911                 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "sync_pipe_wait_for_child: waitpid returned EINTR. retrying.");
1912                 continue;
1913             } else if (errno == ECHILD) {
1914                 /*
1915                  * The process identified by fork_child either doesn't
1916                  * exist any more or isn't our child process (anymore?).
1917                  *
1918                  * echld might have already reaped the child.
1919                  */
1920                ret = fetch_dumpcap_pid ? 0 : -1;
1921             } else {
1922                 /* Unknown error. */
1923                 *msgp = g_strdup_printf("Error from waitpid(): %s", g_strerror(errno));
1924                 ret = -1;
1925             }
1926         }
1927         break;
1928     }
1929 #endif
1930
1931     g_get_current_time(&end_time);
1932     elapsed = (float) ((end_time.tv_sec - start_time.tv_sec) +
1933                        ((end_time.tv_usec - start_time.tv_usec) / 1e6));
1934     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: capture child closed after %.3fs", elapsed);
1935     return ret;
1936 }
1937
1938
1939 #ifndef _WIN32
1940 /* convert signal to corresponding name */
1941 static const char *
1942 sync_pipe_signame(int sig)
1943 {
1944     const char *sigmsg;
1945     static char sigmsg_buf[6+1+3+1];
1946
1947     switch (sig) {
1948
1949     case SIGHUP:
1950         sigmsg = "Hangup";
1951         break;
1952
1953     case SIGINT:
1954         sigmsg = "Interrupted";
1955         break;
1956
1957     case SIGQUIT:
1958         sigmsg = "Quit";
1959         break;
1960
1961     case SIGILL:
1962         sigmsg = "Illegal instruction";
1963         break;
1964
1965     case SIGTRAP:
1966         sigmsg = "Trace trap";
1967         break;
1968
1969     case SIGABRT:
1970         sigmsg = "Abort";
1971         break;
1972
1973     case SIGFPE:
1974         sigmsg = "Arithmetic exception";
1975         break;
1976
1977     case SIGKILL:
1978         sigmsg = "Killed";
1979         break;
1980
1981     case SIGBUS:
1982         sigmsg = "Bus error";
1983         break;
1984
1985     case SIGSEGV:
1986         sigmsg = "Segmentation violation";
1987         break;
1988
1989         /* http://metalab.unc.edu/pub/Linux/docs/HOWTO/GCC-HOWTO
1990            Linux is POSIX compliant.  These are not POSIX-defined signals ---
1991            ISO/IEC 9945-1:1990 (IEEE Std 1003.1-1990), paragraph B.3.3.1.1 sez:
1992
1993            ``The signals SIGBUS, SIGEMT, SIGIOT, SIGTRAP, and SIGSYS
1994            were omitted from POSIX.1 because their behavior is
1995            implementation dependent and could not be adequately catego-
1996            rized.  Conforming implementations may deliver these sig-
1997            nals, but must document the circumstances under which they
1998            are delivered and note any restrictions concerning their
1999            delivery.''
2000
2001            So we only check for SIGSYS on those systems that happen to
2002            implement them (a system can be POSIX-compliant and implement
2003            them, it's just that POSIX doesn't *require* a POSIX-compliant
2004            system to implement them).
2005         */
2006
2007 #ifdef SIGSYS
2008     case SIGSYS:
2009         sigmsg = "Bad system call";
2010         break;
2011 #endif
2012
2013     case SIGPIPE:
2014         sigmsg = "Broken pipe";
2015         break;
2016
2017     case SIGALRM:
2018         sigmsg = "Alarm clock";
2019         break;
2020
2021     case SIGTERM:
2022         sigmsg = "Terminated";
2023         break;
2024
2025     default:
2026         /* Returning a static buffer is ok in the context we use it here */
2027         g_snprintf(sigmsg_buf, sizeof sigmsg_buf, "Signal %d", sig);
2028         sigmsg = sigmsg_buf;
2029         break;
2030     }
2031     return sigmsg;
2032 }
2033 #endif
2034
2035
2036 #ifdef _WIN32
2037
2038 static void create_dummy_signal_pipe() {
2039     gchar *dummy_signal_pipe_name;
2040
2041     if (dummy_signal_pipe != NULL) return;
2042
2043     if (!dummy_control_id) {
2044         dummy_control_id = g_strdup_printf("%d.dummy", GetCurrentProcessId());
2045     }
2046
2047     /* Create the signal pipe */
2048     dummy_signal_pipe_name = g_strdup_printf(SIGNAL_PIPE_FORMAT, dummy_control_id);
2049     dummy_signal_pipe = CreateNamedPipe(utf_8to16(dummy_signal_pipe_name),
2050                                   PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE, 1, 65535, 65535, 0, NULL);
2051     g_free(dummy_signal_pipe_name);
2052 }
2053
2054 /* tell the child through the signal pipe that we want to quit the capture */
2055 static void
2056 signal_pipe_capquit_to_child(capture_session *cap_session)
2057 {
2058     const char quit_msg[] = "QUIT";
2059     int ret;
2060
2061     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "signal_pipe_capquit_to_child");
2062
2063     /* it doesn't matter *what* we send here, the first byte will stop the capture */
2064     /* simply sending a "QUIT" string */
2065     /*pipe_write_block(cap_session->signal_pipe_write_fd, SP_QUIT, quit_msg);*/
2066     ret = ws_write(cap_session->signal_pipe_write_fd, quit_msg, sizeof quit_msg);
2067     if(ret == -1) {
2068         g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
2069               "signal_pipe_capquit_to_child: %d header: error %s", cap_session->signal_pipe_write_fd, g_strerror(errno));
2070     }
2071 }
2072 #endif
2073
2074
2075 /* user wants to stop the capture run */
2076 void
2077 sync_pipe_stop(capture_session *cap_session)
2078 {
2079 #ifdef _WIN32
2080     int count;
2081     DWORD childstatus;
2082     gboolean terminate = TRUE;
2083 #endif
2084     if (cap_session->fork_child != WS_INVALID_PID) {
2085 #ifndef _WIN32
2086         /* send the SIGINT signal to close the capture child gracefully. */
2087         int sts = kill(cap_session->fork_child, SIGINT);
2088         if (sts != 0) {
2089             g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
2090                   "Sending SIGINT to child failed: %s\n", g_strerror(errno));
2091         }
2092 #else
2093 #define STOP_SLEEP_TIME 500 /* ms */
2094 #define STOP_CHECK_TIME 50
2095         /* First, use the special signal pipe to try to close the capture child
2096          * gracefully.
2097          */
2098         signal_pipe_capquit_to_child(cap_session);
2099
2100         /* Next, wait for the process to exit on its own */
2101         for (count = 0; count < STOP_SLEEP_TIME / STOP_CHECK_TIME; count++) {
2102             if (GetExitCodeProcess((HANDLE) cap_session->fork_child, &childstatus) &&
2103                 childstatus != STILL_ACTIVE) {
2104                 terminate = FALSE;
2105                 break;
2106             }
2107             Sleep(STOP_CHECK_TIME);
2108         }
2109
2110         /* Force the issue. */
2111         if (terminate) {
2112             g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
2113                   "sync_pipe_stop: forcing child to exit");
2114             sync_pipe_kill(cap_session->fork_child);
2115         }
2116 #endif
2117     }
2118 }
2119
2120
2121 /* Wireshark has to exit, force the capture child to close */
2122 void
2123 sync_pipe_kill(ws_process_id fork_child)
2124 {
2125     if (fork_child != WS_INVALID_PID) {
2126 #ifndef _WIN32
2127         int sts = kill(fork_child, SIGTERM);    /* SIGTERM so it can clean up if necessary */
2128         if (sts != 0) {
2129             g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
2130                   "Sending SIGTERM to child failed: %s\n", g_strerror(errno));
2131         }
2132 #else
2133         /* Remark: This is not the preferred method of closing a process!
2134          * the clean way would be getting the process id of the child process,
2135          * then getting window handle hWnd of that process (using EnumChildWindows),
2136          * and then do a SendMessage(hWnd, WM_CLOSE, 0, 0)
2137          *
2138          * Unfortunately, I don't know how to get the process id from the
2139          * handle.  OpenProcess will get an handle (not a window handle)
2140          * from the process ID; it will not get a window handle from the
2141          * process ID.  (How could it?  A process can have more than one
2142          * window.  For that matter, a process might have *no* windows,
2143          * as a process running dumpcap, the normal child process program,
2144          * probably does.)
2145          *
2146          * Hint: GenerateConsoleCtrlEvent() will only work if both processes are
2147          * running in the same console; that's not necessarily the case for
2148          * us, as we might not be running in a console.
2149          * And this also will require to have the process id.
2150          */
2151         TerminateProcess((HANDLE) (fork_child), 0);
2152
2153 #endif
2154     }
2155 }
2156
2157 void capture_sync_set_fetch_dumpcap_pid_cb(void(*cb)(ws_process_id pid)) {
2158     fetch_dumpcap_pid = cb;
2159 }
2160
2161 #endif /* HAVE_LIBPCAP */