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