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