2 * Synchronisation between Wireshark capture parent and child instances
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
45 #include <wsutil/unicode-utils.h>
48 #ifdef HAVE_SYS_WAIT_H
49 # include <sys/wait.h>
52 #include "capture-pcap-util.h"
56 * Define various POSIX macros (and, in the case of WCOREDUMP, non-POSIX
57 * macros) on UNIX systems that don't have them.
60 # define WIFEXITED(status) (((status) & 0177) == 0)
63 # define WIFSTOPPED(status) (((status) & 0177) == 0177)
66 # define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
69 # define WEXITSTATUS(status) ((status) >> 8)
72 # define WTERMSIG(status) ((status) & 0177)
75 # define WCOREDUMP(status) ((status) & 0200)
78 # define WSTOPSIG(status) ((status) >> 8)
82 #include <epan/packet.h>
83 #include <epan/prefs.h>
87 #include <epan/filesystem.h>
90 #include "capture_sync.h"
92 #include "sync_pipe.h"
95 #include "capture-wpcap.h"
98 #include "ui/ui_util.h"
100 #include <wsutil/file_util.h>
101 #include <wsutil/report_err.h>
105 #include <process.h> /* For spawning child process */
111 static void create_dummy_signal_pipe();
112 static HANDLE dummy_signal_pipe
; /* Dummy named pipe which lets the child check for a dropped connection */
113 static gchar
*dummy_control_id
;
115 static const char *sync_pipe_signame(int);
119 static gboolean
sync_pipe_input_cb(gint source
, gpointer user_data
);
120 static int sync_pipe_wait_for_child(int fork_child
, gchar
**msgp
);
121 static void pipe_convert_header(const guchar
*header
, int header_len
, char *indicator
, int *block_len
);
122 static ssize_t
pipe_read_block(int pipe_fd
, char *indicator
, int len
, char *msg
,
125 static void (*fetch_dumpcap_pid
)(int) = NULL
;
129 capture_session_init(capture_session
*cap_session
, void *cf
)
131 cap_session
->cf
= cf
;
132 cap_session
->fork_child
= -1; /* invalid process handle */
134 cap_session
->signal_pipe_write_fd
= -1;
136 cap_session
->state
= CAPTURE_STOPPED
;
138 cap_session
->owner
= getuid();
139 cap_session
->group
= getgid();
141 cap_session
->session_started
= FALSE
;
144 /* Append an arg (realloc) to an argc/argv array */
145 /* (add a string pointer to a NULL-terminated array of string pointers) */
147 sync_pipe_add_arg(char **args
, int *argc
, const char *arg
)
149 /* Grow the array; "*argc" currently contains the number of string
150 pointers, *not* counting the NULL pointer at the end, so we have
151 to add 2 in order to get the new size of the array, including the
152 new pointer and the terminating NULL pointer. */
153 args
= (char **)g_realloc( (gpointer
) args
, (*argc
+ 2) * sizeof (char *));
155 /* Stuff the pointer into the penultimate element of the array, which
156 is the one at the index specified by "*argc". */
157 args
[*argc
] = g_strdup(arg
);
158 /* Now bump the count. */
161 /* We overwrite the NULL pointer; put it back right after the
171 /* Quote the argument element if necessary, so that it will get
172 * reconstructed correctly in the C runtime startup code. Note that
173 * the unquoting algorithm in the C runtime is really weird, and
174 * rather different than what Unix shells do. See stdargv.c in the C
175 * runtime sources (in the Platform SDK, in src/crt).
177 * Stolen from GLib's protect_argv(), an internal routine that quotes
178 * string in an argument list so that they arguments will be handled
179 * correctly in the command-line string passed to CreateProcess()
180 * if that string is constructed by gluing those strings together.
183 protect_arg (const gchar
*argv
)
186 const gchar
*p
= argv
;
189 gboolean need_dblquotes
= FALSE
;
192 if (*p
== ' ' || *p
== '\t')
193 need_dblquotes
= TRUE
;
196 else if (*p
== '\\') {
199 while (*pp
&& *pp
== '\\')
208 q
= new_arg
= g_malloc (len
+ need_dblquotes
*2 + 1);
217 else if (*p
== '\\') {
220 while (*pp
&& *pp
== '\\')
237 * Generate a string for a Win32 error.
239 #define ERRBUF_SIZE 1024
241 win32strerror(DWORD error
)
243 static char errbuf
[ERRBUF_SIZE
+1];
247 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
,
248 NULL
, error
, 0, errbuf
, ERRBUF_SIZE
, NULL
);
251 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
252 * message. Get rid of it.
254 errlen
= strlen(errbuf
);
256 errbuf
[errlen
- 1] = '\0';
257 errbuf
[errlen
- 2] = '\0';
259 p
= strchr(errbuf
, '\0');
260 g_snprintf(p
, (gulong
)(sizeof errbuf
- (p
-errbuf
)), " (%lu)", error
);
265 * Generate a string for a Win32 exception code.
268 win32strexception(DWORD exception
)
270 static char errbuf
[ERRBUF_SIZE
+1];
271 static const struct exception_msg
{
275 { EXCEPTION_ACCESS_VIOLATION
, "Access violation" },
276 { EXCEPTION_ARRAY_BOUNDS_EXCEEDED
, "Array bounds exceeded" },
277 { EXCEPTION_BREAKPOINT
, "Breakpoint" },
278 { EXCEPTION_DATATYPE_MISALIGNMENT
, "Data type misalignment" },
279 { EXCEPTION_FLT_DENORMAL_OPERAND
, "Denormal floating-point operand" },
280 { EXCEPTION_FLT_DIVIDE_BY_ZERO
, "Floating-point divide by zero" },
281 { EXCEPTION_FLT_INEXACT_RESULT
, "Floating-point inexact result" },
282 { EXCEPTION_FLT_INVALID_OPERATION
, "Invalid floating-point operation" },
283 { EXCEPTION_FLT_OVERFLOW
, "Floating-point overflow" },
284 { EXCEPTION_FLT_STACK_CHECK
, "Floating-point stack check" },
285 { EXCEPTION_FLT_UNDERFLOW
, "Floating-point underflow" },
286 { EXCEPTION_GUARD_PAGE
, "Guard page violation" },
287 { EXCEPTION_ILLEGAL_INSTRUCTION
, "Illegal instruction" },
288 { EXCEPTION_IN_PAGE_ERROR
, "Page-in error" },
289 { EXCEPTION_INT_DIVIDE_BY_ZERO
, "Integer divide by zero" },
290 { EXCEPTION_INT_OVERFLOW
, "Integer overflow" },
291 { EXCEPTION_INVALID_DISPOSITION
, "Invalid disposition" },
292 { EXCEPTION_INVALID_HANDLE
, "Invalid handle" },
293 { EXCEPTION_NONCONTINUABLE_EXCEPTION
, "Non-continuable exception" },
294 { EXCEPTION_PRIV_INSTRUCTION
, "Privileged instruction" },
295 { EXCEPTION_SINGLE_STEP
, "Single-step complete" },
296 { EXCEPTION_STACK_OVERFLOW
, "Stack overflow" },
299 #define N_EXCEPTIONS (sizeof exceptions / sizeof exceptions[0])
302 for (i
= 0; i
< N_EXCEPTIONS
; i
++) {
303 if (exceptions
[i
].code
== exception
)
304 return exceptions
[i
].msg
;
306 g_snprintf(errbuf
, (gulong
)sizeof errbuf
, "Exception 0x%08x", exception
);
311 /* Initialize an argument list and add dumpcap to it. */
313 init_pipe_args(int *argc
) {
315 const char *progfile_dir
;
318 progfile_dir
= get_progfile_dir();
319 if (progfile_dir
== NULL
) {
323 /* Allocate the string pointer array with enough space for the
324 terminating NULL pointer. */
326 argv
= (char **)g_malloc(sizeof (char *));
329 /* take Wireshark's absolute program path and replace "Wireshark" with "dumpcap" */
330 exename
= g_strdup_printf("%s" G_DIR_SEPARATOR_S
"dumpcap", progfile_dir
);
332 /* Make that the first argument in the argument list (argv[0]). */
333 argv
= sync_pipe_add_arg(argv
, argc
, exename
);
335 /* sync_pipe_add_arg strdupes exename, so we should free our copy */
341 #define ARGV_NUMBER_LEN 24
342 /* a new capture run: start a new dumpcap task and hand over parameters through command line */
344 sync_pipe_start(capture_options
*capture_opts
, capture_session
*cap_session
, void (*update_cb
)(void))
346 char ssnap
[ARGV_NUMBER_LEN
];
347 char scount
[ARGV_NUMBER_LEN
];
348 char sfilesize
[ARGV_NUMBER_LEN
];
349 char sfile_duration
[ARGV_NUMBER_LEN
];
350 char sring_num_files
[ARGV_NUMBER_LEN
];
351 char sautostop_files
[ARGV_NUMBER_LEN
];
352 char sautostop_filesize
[ARGV_NUMBER_LEN
];
353 char sautostop_duration
[ARGV_NUMBER_LEN
];
354 #ifdef HAVE_PCAP_REMOTE
357 #ifdef HAVE_PCAP_SETSAMPLING
358 char ssampling
[ARGV_NUMBER_LEN
];
361 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
362 char buffer_size
[ARGV_NUMBER_LEN
];
366 HANDLE sync_pipe_read
; /* pipe used to send messages from child to parent */
367 HANDLE sync_pipe_write
; /* pipe used to send messages from child to parent */
368 HANDLE signal_pipe
; /* named pipe used to send messages from parent to child (currently only stop) */
369 GString
*args
= g_string_sized_new(200);
371 SECURITY_ATTRIBUTES sa
;
373 PROCESS_INFORMATION pi
;
374 char control_id
[ARGV_NUMBER_LEN
];
375 gchar
*signal_pipe_name
;
378 int sync_pipe
[2]; /* pipe used to send messages from child to parent */
379 enum PIPES
{ PIPE_READ
, PIPE_WRITE
}; /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
381 int sync_pipe_read_fd
;
386 interface_options interface_opts
;
388 if (capture_opts
->ifaces
->len
> 1)
389 capture_opts
->use_pcapng
= TRUE
;
390 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "sync_pipe_start");
391 capture_opts_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, capture_opts
);
393 cap_session
->fork_child
= -1;
395 argv
= init_pipe_args(&argc
);
397 /* We don't know where to find dumpcap. */
398 report_failure("We don't know where to find dumpcap.");
402 if (capture_opts
->ifaces
->len
> 1)
403 argv
= sync_pipe_add_arg(argv
, &argc
, "-t");
405 if (capture_opts
->use_pcapng
)
406 argv
= sync_pipe_add_arg(argv
, &argc
, "-n");
408 argv
= sync_pipe_add_arg(argv
, &argc
, "-P");
410 if (capture_opts
->capture_comment
) {
411 argv
= sync_pipe_add_arg(argv
, &argc
, "--capture-comment");
412 argv
= sync_pipe_add_arg(argv
, &argc
, capture_opts
->capture_comment
);
415 if (capture_opts
->multi_files_on
) {
416 if (capture_opts
->has_autostop_filesize
) {
417 argv
= sync_pipe_add_arg(argv
, &argc
, "-b");
418 g_snprintf(sfilesize
, ARGV_NUMBER_LEN
, "filesize:%u",capture_opts
->autostop_filesize
);
419 argv
= sync_pipe_add_arg(argv
, &argc
, sfilesize
);
422 if (capture_opts
->has_file_duration
) {
423 argv
= sync_pipe_add_arg(argv
, &argc
, "-b");
424 g_snprintf(sfile_duration
, ARGV_NUMBER_LEN
, "duration:%d",capture_opts
->file_duration
);
425 argv
= sync_pipe_add_arg(argv
, &argc
, sfile_duration
);
428 if (capture_opts
->has_ring_num_files
) {
429 argv
= sync_pipe_add_arg(argv
, &argc
, "-b");
430 g_snprintf(sring_num_files
, ARGV_NUMBER_LEN
, "files:%d",capture_opts
->ring_num_files
);
431 argv
= sync_pipe_add_arg(argv
, &argc
, sring_num_files
);
434 if (capture_opts
->has_autostop_files
) {
435 argv
= sync_pipe_add_arg(argv
, &argc
, "-a");
436 g_snprintf(sautostop_files
, ARGV_NUMBER_LEN
, "files:%d",capture_opts
->autostop_files
);
437 argv
= sync_pipe_add_arg(argv
, &argc
, sautostop_files
);
440 if (capture_opts
->has_autostop_filesize
) {
441 argv
= sync_pipe_add_arg(argv
, &argc
, "-a");
442 g_snprintf(sautostop_filesize
, ARGV_NUMBER_LEN
, "filesize:%u",capture_opts
->autostop_filesize
);
443 argv
= sync_pipe_add_arg(argv
, &argc
, sautostop_filesize
);
447 if (capture_opts
->has_autostop_packets
) {
448 argv
= sync_pipe_add_arg(argv
, &argc
, "-c");
449 g_snprintf(scount
, ARGV_NUMBER_LEN
, "%d",capture_opts
->autostop_packets
);
450 argv
= sync_pipe_add_arg(argv
, &argc
, scount
);
453 if (capture_opts
->has_autostop_duration
) {
454 argv
= sync_pipe_add_arg(argv
, &argc
, "-a");
455 g_snprintf(sautostop_duration
, ARGV_NUMBER_LEN
, "duration:%d",capture_opts
->autostop_duration
);
456 argv
= sync_pipe_add_arg(argv
, &argc
, sautostop_duration
);
459 if (capture_opts
->group_read_access
) {
460 argv
= sync_pipe_add_arg(argv
, &argc
, "-g");
463 for (j
= 0; j
< capture_opts
->ifaces
->len
; j
++) {
464 interface_opts
= g_array_index(capture_opts
->ifaces
, interface_options
, j
);
466 argv
= sync_pipe_add_arg(argv
, &argc
, "-i");
467 argv
= sync_pipe_add_arg(argv
, &argc
, interface_opts
.name
);
469 if (interface_opts
.cfilter
!= NULL
&& strlen(interface_opts
.cfilter
) != 0) {
470 argv
= sync_pipe_add_arg(argv
, &argc
, "-f");
471 argv
= sync_pipe_add_arg(argv
, &argc
, interface_opts
.cfilter
);
473 if (interface_opts
.snaplen
!= WTAP_MAX_PACKET_SIZE
) {
474 argv
= sync_pipe_add_arg(argv
, &argc
, "-s");
475 g_snprintf(ssnap
, ARGV_NUMBER_LEN
, "%d", interface_opts
.snaplen
);
476 argv
= sync_pipe_add_arg(argv
, &argc
, ssnap
);
479 if (interface_opts
.linktype
!= -1) {
480 argv
= sync_pipe_add_arg(argv
, &argc
, "-y");
481 argv
= sync_pipe_add_arg(argv
, &argc
, linktype_val_to_name(interface_opts
.linktype
));
484 if (!interface_opts
.promisc_mode
) {
485 argv
= sync_pipe_add_arg(argv
, &argc
, "-p");
488 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
489 if (interface_opts
.buffer_size
!= DEFAULT_CAPTURE_BUFFER_SIZE
) {
490 argv
= sync_pipe_add_arg(argv
, &argc
, "-B");
491 g_snprintf(buffer_size
, ARGV_NUMBER_LEN
, "%d", interface_opts
.buffer_size
);
492 argv
= sync_pipe_add_arg(argv
, &argc
, buffer_size
);
496 #ifdef HAVE_PCAP_CREATE
497 if (interface_opts
.monitor_mode
) {
498 argv
= sync_pipe_add_arg(argv
, &argc
, "-I");
502 #ifdef HAVE_PCAP_REMOTE
503 if (interface_opts
.datatx_udp
)
504 argv
= sync_pipe_add_arg(argv
, &argc
, "-u");
506 if (!interface_opts
.nocap_rpcap
)
507 argv
= sync_pipe_add_arg(argv
, &argc
, "-r");
509 if (interface_opts
.auth_type
== CAPTURE_AUTH_PWD
) {
510 argv
= sync_pipe_add_arg(argv
, &argc
, "-A");
511 g_snprintf(sauth
, sizeof(sauth
), "%s:%s",
512 interface_opts
.auth_username
,
513 interface_opts
.auth_password
);
514 argv
= sync_pipe_add_arg(argv
, &argc
, sauth
);
518 #ifdef HAVE_PCAP_SETSAMPLING
519 if (interface_opts
.sampling_method
!= CAPTURE_SAMP_NONE
) {
520 argv
= sync_pipe_add_arg(argv
, &argc
, "-m");
521 g_snprintf(ssampling
, ARGV_NUMBER_LEN
, "%s:%d",
522 interface_opts
.sampling_method
== CAPTURE_SAMP_BY_COUNT
? "count" :
523 interface_opts
.sampling_method
== CAPTURE_SAMP_BY_TIMER
? "timer" :
525 interface_opts
.sampling_param
);
526 argv
= sync_pipe_add_arg(argv
, &argc
, ssampling
);
531 /* dumpcap should be running in capture child mode (hidden feature) */
533 argv
= sync_pipe_add_arg(argv
, &argc
, "-Z");
535 g_snprintf(control_id
, ARGV_NUMBER_LEN
, "%d", GetCurrentProcessId());
536 argv
= sync_pipe_add_arg(argv
, &argc
, control_id
);
538 argv
= sync_pipe_add_arg(argv
, &argc
, SIGNAL_PIPE_CTRL_ID_NONE
);
542 if (capture_opts
->save_file
) {
543 argv
= sync_pipe_add_arg(argv
, &argc
, "-w");
544 argv
= sync_pipe_add_arg(argv
, &argc
, capture_opts
->save_file
);
546 for (i
= 0; i
< argc
; i
++) {
547 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "argv[%d]: %s", i
, argv
[i
]);
551 /* init SECURITY_ATTRIBUTES */
552 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
553 sa
.bInheritHandle
= TRUE
;
554 sa
.lpSecurityDescriptor
= NULL
;
556 /* Create a pipe for the child process */
557 /* (increase this value if you have trouble while fast capture file switches) */
558 if (! CreatePipe(&sync_pipe_read
, &sync_pipe_write
, &sa
, 5120)) {
559 /* Couldn't create the pipe between parent and child. */
560 report_failure("Couldn't create sync pipe: %s",
561 win32strerror(GetLastError()));
562 for (i
= 0; i
< argc
; i
++) {
563 g_free( (gpointer
) argv
[i
]);
565 g_free( (gpointer
) argv
);
569 /* Create the signal pipe */
570 signal_pipe_name
= g_strdup_printf(SIGNAL_PIPE_FORMAT
, control_id
);
571 signal_pipe
= CreateNamedPipe(utf_8to16(signal_pipe_name
),
572 PIPE_ACCESS_OUTBOUND
, PIPE_TYPE_BYTE
, 1, 65535, 65535, 0, NULL
);
573 g_free(signal_pipe_name
);
575 if (signal_pipe
== INVALID_HANDLE_VALUE
) {
576 /* Couldn't create the signal pipe between parent and child. */
577 report_failure("Couldn't create signal pipe: %s",
578 win32strerror(GetLastError()));
579 for (i
= 0; i
< argc
; i
++) {
580 g_free( (gpointer
) argv
[i
]);
582 g_free( (gpointer
) argv
);
586 /* init STARTUPINFO */
587 memset(&si
, 0, sizeof(si
));
590 si
.dwFlags
= STARTF_USESHOWWINDOW
;
591 si
.wShowWindow
= SW_SHOW
;
593 si
.dwFlags
= STARTF_USESTDHANDLES
|STARTF_USESHOWWINDOW
;
594 si
.wShowWindow
= SW_HIDE
; /* this hides the console window */
595 si
.hStdInput
= GetStdHandle(STD_INPUT_HANDLE
);
596 si
.hStdOutput
= GetStdHandle(STD_OUTPUT_HANDLE
);
597 si
.hStdError
= sync_pipe_write
;
598 /*si.hStdError = (HANDLE) _get_osfhandle(2);*/
601 /* convert args array into a single string */
602 /* XXX - could change sync_pipe_add_arg() instead */
603 /* there is a drawback here: the length is internally limited to 1024 bytes */
604 for(i
=0; argv
[i
] != 0; i
++) {
605 if(i
!= 0) g_string_append_c(args
, ' '); /* don't prepend a space before the path!!! */
606 quoted_arg
= protect_arg(argv
[i
]);
607 g_string_append(args
, quoted_arg
);
612 if(!CreateProcess(NULL
, utf_8to16(args
->str
), NULL
, NULL
, TRUE
,
613 CREATE_NEW_CONSOLE
, NULL
, NULL
, &si
, &pi
)) {
614 report_failure("Couldn't run %s in child process: %s",
615 args
->str
, win32strerror(GetLastError()));
616 CloseHandle(sync_pipe_read
);
617 CloseHandle(sync_pipe_write
);
618 for (i
= 0; i
< argc
; i
++) {
619 g_free( (gpointer
) argv
[i
]);
621 g_free( (gpointer
) argv
);
624 cap_session
->fork_child
= (int) pi
.hProcess
;
625 g_string_free(args
, TRUE
);
627 /* associate the operating system filehandle to a C run-time file handle */
628 /* (good file handle infos at: http://www.flounder.com/handles.htm) */
629 sync_pipe_read_fd
= _open_osfhandle( (long) sync_pipe_read
, _O_BINARY
);
631 /* associate the operating system filehandle to a C run-time file handle */
632 cap_session
->signal_pipe_write_fd
= _open_osfhandle( (long) signal_pipe
, _O_BINARY
);
635 if (pipe(sync_pipe
) < 0) {
636 /* Couldn't create the pipe between parent and child. */
637 report_failure("Couldn't create sync pipe: %s", g_strerror(errno
));
638 for (i
= 0; i
< argc
; i
++) {
639 g_free( (gpointer
) argv
[i
]);
645 if ((cap_session
->fork_child
= fork()) == 0) {
647 * Child process - run dumpcap with the right arguments to make
648 * it just capture with the specified capture parameters
650 dup2(sync_pipe
[PIPE_WRITE
], 2);
651 ws_close(sync_pipe
[PIPE_READ
]);
652 execv(argv
[0], argv
);
653 g_snprintf(errmsg
, sizeof errmsg
, "Couldn't run %s in child process: %s",
654 argv
[0], g_strerror(errno
));
655 sync_pipe_errmsg_to_parent(2, errmsg
, "");
657 /* Exit with "_exit()", so that we don't close the connection
658 to the X server (and cause stuff buffered up by our parent but
659 not yet sent to be sent, as that stuff should only be sent by
660 our parent). We've sent an error message to the parent, so
661 we exit with an exit status of 1 (any exit status other than
662 0 or 1 will cause an additional message to report that exit
663 status, over and above the error message we sent to the parent). */
667 if (fetch_dumpcap_pid
&& cap_session
->fork_child
> 0)
668 fetch_dumpcap_pid(cap_session
->fork_child
);
670 sync_pipe_read_fd
= sync_pipe
[PIPE_READ
];
673 for (i
= 0; i
< argc
; i
++) {
674 g_free( (gpointer
) argv
[i
]);
677 /* Parent process - read messages from the child process over the
679 g_free( (gpointer
) argv
); /* free up arg array */
681 /* Close the write side of the pipe, so that only the child has it
682 open, and thus it completely closes, and thus returns to us
683 an EOF indication, if the child closes it (either deliberately
684 or by exiting abnormally). */
686 CloseHandle(sync_pipe_write
);
688 ws_close(sync_pipe
[PIPE_WRITE
]);
691 if (cap_session
->fork_child
== -1) {
692 /* We couldn't even create the child process. */
693 report_failure("Couldn't create child process: %s", g_strerror(errno
));
694 ws_close(sync_pipe_read_fd
);
696 ws_close(cap_session
->signal_pipe_write_fd
);
701 cap_session
->fork_child_status
= 0;
702 cap_session
->capture_opts
= capture_opts
;
704 /* we might wait for a moment till child is ready, so update screen now */
705 if (update_cb
) update_cb();
707 /* We were able to set up to read the capture file;
708 arrange that our callback be called whenever it's possible
709 to read from the sync pipe, so that it's called when
710 the child process wants to tell us something. */
712 /* we have a running capture, now wait for the real capture filename */
713 pipe_input_set_handler(sync_pipe_read_fd
, (gpointer
) cap_session
,
714 &cap_session
->fork_child
, sync_pipe_input_cb
);
720 * Open two pipes to dumpcap with the supplied arguments, one for its
721 * standard output and one for its standard error.
723 * On success, *msg is unchanged and 0 is returned; data_read_fd,
724 * messsage_read_fd, and fork_child point to the standard output pipe's
725 * file descriptor, the standard error pipe's file descriptor, and
726 * the child's PID/handle, respectively.
728 * On failure, *msg points to an error message for the failure, and -1 is
729 * returned, in which case *msg must be freed with g_free().
731 /* XXX - This duplicates a lot of code in sync_pipe_start() */
732 /* XXX - assumes PIPE_BUF_SIZE > SP_MAX_MSG_LEN */
733 #define PIPE_BUF_SIZE 5120
735 sync_pipe_open_command(char** argv
, int *data_read_fd
,
736 int *message_read_fd
, int *fork_child
, gchar
**msg
, void(*update_cb
)(void))
738 enum PIPES
{ PIPE_READ
, PIPE_WRITE
}; /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
740 HANDLE sync_pipe
[2]; /* pipe used to send messages from child to parent */
741 HANDLE data_pipe
[2]; /* pipe used to send data from child to parent */
742 GString
*args
= g_string_sized_new(200);
744 SECURITY_ATTRIBUTES sa
;
746 PROCESS_INFORMATION pi
;
749 int sync_pipe
[2]; /* pipe used to send messages from child to parent */
750 int data_pipe
[2]; /* pipe used to send data from child to parent */
755 *message_read_fd
= -1;
756 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "sync_pipe_open_command");
759 /* We can't return anything */
761 g_string_free(args
, TRUE
);
767 /* init SECURITY_ATTRIBUTES */
768 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
769 sa
.bInheritHandle
= TRUE
;
770 sa
.lpSecurityDescriptor
= NULL
;
772 /* Create a pipe for the child process to send us messages */
773 /* (increase this value if you have trouble while fast capture file switches) */
774 if (! CreatePipe(&sync_pipe
[PIPE_READ
], &sync_pipe
[PIPE_WRITE
], &sa
, 5120)) {
775 /* Couldn't create the message pipe between parent and child. */
776 *msg
= g_strdup_printf("Couldn't create sync pipe: %s",
777 win32strerror(GetLastError()));
778 for (i
= 0; argv
[i
] != NULL
; i
++) {
779 g_free( (gpointer
) argv
[i
]);
781 g_free( (gpointer
) argv
);
785 /* Create a pipe for the child process to send us data */
786 /* (increase this value if you have trouble while fast capture file switches) */
787 if (! CreatePipe(&data_pipe
[PIPE_READ
], &data_pipe
[PIPE_WRITE
], &sa
, 5120)) {
788 /* Couldn't create the message pipe between parent and child. */
789 *msg
= g_strdup_printf("Couldn't create data pipe: %s",
790 win32strerror(GetLastError()));
791 CloseHandle(sync_pipe
[PIPE_READ
]);
792 CloseHandle(sync_pipe
[PIPE_WRITE
]);
793 for (i
= 0; argv
[i
] != NULL
; i
++) {
794 g_free( (gpointer
) argv
[i
]);
796 g_free( (gpointer
) argv
);
800 /* init STARTUPINFO */
801 memset(&si
, 0, sizeof(si
));
804 si
.dwFlags
= STARTF_USESHOWWINDOW
;
805 si
.wShowWindow
= SW_SHOW
;
807 si
.dwFlags
= STARTF_USESTDHANDLES
|STARTF_USESHOWWINDOW
;
808 si
.wShowWindow
= SW_HIDE
; /* this hides the console window */
810 si
.hStdOutput
= data_pipe
[PIPE_WRITE
];
811 si
.hStdError
= sync_pipe
[PIPE_WRITE
];
814 /* convert args array into a single string */
815 /* XXX - could change sync_pipe_add_arg() instead */
816 /* there is a drawback here: the length is internally limited to 1024 bytes */
817 for(i
=0; argv
[i
] != 0; i
++) {
818 if(i
!= 0) g_string_append_c(args
, ' '); /* don't prepend a space before the path!!! */
819 quoted_arg
= protect_arg(argv
[i
]);
820 g_string_append(args
, quoted_arg
);
825 if(!CreateProcess(NULL
, utf_8to16(args
->str
), NULL
, NULL
, TRUE
,
826 CREATE_NEW_CONSOLE
, NULL
, NULL
, &si
, &pi
)) {
827 *msg
= g_strdup_printf("Couldn't run %s in child process: %s",
828 args
->str
, win32strerror(GetLastError()));
829 CloseHandle(data_pipe
[PIPE_READ
]);
830 CloseHandle(data_pipe
[PIPE_WRITE
]);
831 CloseHandle(sync_pipe
[PIPE_READ
]);
832 CloseHandle(sync_pipe
[PIPE_WRITE
]);
833 for (i
= 0; argv
[i
] != NULL
; i
++) {
834 g_free( (gpointer
) argv
[i
]);
836 g_free( (gpointer
) argv
);
839 *fork_child
= (int) pi
.hProcess
;
840 g_string_free(args
, TRUE
);
842 /* associate the operating system filehandles to C run-time file handles */
843 /* (good file handle infos at: http://www.flounder.com/handles.htm) */
844 *data_read_fd
= _open_osfhandle( (long) data_pipe
[PIPE_READ
], _O_BINARY
);
845 *message_read_fd
= _open_osfhandle( (long) sync_pipe
[PIPE_READ
], _O_BINARY
);
847 /* Create a pipe for the child process to send us messages */
848 if (pipe(sync_pipe
) < 0) {
849 /* Couldn't create the message pipe between parent and child. */
850 *msg
= g_strdup_printf("Couldn't create sync pipe: %s", g_strerror(errno
));
851 for (i
= 0; argv
[i
] != NULL
; i
++) {
852 g_free( (gpointer
) argv
[i
]);
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", g_strerror(errno
));
862 ws_close(sync_pipe
[PIPE_READ
]);
863 ws_close(sync_pipe
[PIPE_WRITE
]);
864 for (i
= 0; argv
[i
] != NULL
; i
++) {
865 g_free( (gpointer
) argv
[i
]);
871 if ((*fork_child
= fork()) == 0) {
873 * Child process - run dumpcap with the right arguments to make
874 * it just capture with the specified capture parameters
876 dup2(data_pipe
[PIPE_WRITE
], 1);
877 ws_close(data_pipe
[PIPE_READ
]);
878 ws_close(data_pipe
[PIPE_WRITE
]);
879 dup2(sync_pipe
[PIPE_WRITE
], 2);
880 ws_close(sync_pipe
[PIPE_READ
]);
881 ws_close(sync_pipe
[PIPE_WRITE
]);
882 execv(argv
[0], argv
);
883 g_snprintf(errmsg
, sizeof errmsg
, "Couldn't run %s in child process: %s",
884 argv
[0], g_strerror(errno
));
885 sync_pipe_errmsg_to_parent(2, errmsg
, "");
887 /* Exit with "_exit()", so that we don't close the connection
888 to the X server (and cause stuff buffered up by our parent but
889 not yet sent to be sent, as that stuff should only be sent by
890 our parent). We've sent an error message to the parent, so
891 we exit with an exit status of 1 (any exit status other than
892 0 or 1 will cause an additional message to report that exit
893 status, over and above the error message we sent to the parent). */
897 if (fetch_dumpcap_pid
&& *fork_child
> 0)
898 fetch_dumpcap_pid(*fork_child
);
900 *data_read_fd
= data_pipe
[PIPE_READ
];
901 *message_read_fd
= sync_pipe
[PIPE_READ
];
904 for (i
= 0; argv
[i
] != NULL
; i
++) {
905 g_free( (gpointer
) argv
[i
]);
908 /* Parent process - read messages from the child process over the
910 g_free( (gpointer
) argv
); /* free up arg array */
912 /* Close the write sides of the pipes, so that only the child has them
913 open, and thus they completely close, and thus return to us
914 an EOF indication, if the child closes them (either deliberately
915 or by exiting abnormally). */
917 CloseHandle(data_pipe
[PIPE_WRITE
]);
918 CloseHandle(sync_pipe
[PIPE_WRITE
]);
920 ws_close(data_pipe
[PIPE_WRITE
]);
921 ws_close(sync_pipe
[PIPE_WRITE
]);
924 if (*fork_child
== -1) {
925 /* We couldn't even create the child process. */
926 *msg
= g_strdup_printf("Couldn't create child process: %s", g_strerror(errno
));
927 ws_close(*data_read_fd
);
928 ws_close(*message_read_fd
);
932 /* we might wait for a moment till child is ready, so update screen now */
933 if (update_cb
) update_cb();
938 * Close the pipes we're using to read from dumpcap, and wait for it
939 * to exit. On success, *msgp is unchanged, and the exit status of
940 * dumpcap is returned. On failure (which includes "dumpcap exited
941 * due to being killed by a signal or an exception"), *msgp points
942 * to an error message for the failure, and -1 is returned. In the
943 * latter case, *msgp must be freed with g_free().
946 sync_pipe_close_command(int *data_read_fd
, int *message_read_fd
,
947 int *fork_child
, gchar
**msgp
)
949 ws_close(*data_read_fd
);
950 if (message_read_fd
!= NULL
)
951 ws_close(*message_read_fd
);
954 /* XXX - Should we signal the child somehow? */
955 sync_pipe_kill(*fork_child
);
958 return sync_pipe_wait_for_child(*fork_child
, msgp
);
962 * Run dumpcap with the supplied arguments.
964 * On success, *data points to a buffer containing the dumpcap output,
965 * *primary_msg and *secondary_message are NULL, and 0 is returned; *data
966 * must be freed with g_free().
968 * On failure, *data is NULL, *primary_msg points to an error message,
969 * *secondary_msg either points to an additional error message or is
970 * NULL, and -1 is returned; *primary_msg, and *secondary_msg if not NULL,
971 * must be freed with g_free().
973 /* XXX - This duplicates a lot of code in sync_pipe_start() */
974 /* XXX - assumes PIPE_BUF_SIZE > SP_MAX_MSG_LEN */
975 #define PIPE_BUF_SIZE 5120
977 sync_pipe_run_command_actual(char** argv
, gchar
**data
, gchar
**primary_msg
,
978 gchar
**secondary_msg
, void(*update_cb
)(void))
981 int data_pipe_read_fd
, sync_pipe_read_fd
, fork_child
, ret
;
983 gchar buffer
[PIPE_BUF_SIZE
+1];
987 char *primary_msg_text
;
988 int secondary_msg_len
;
989 char *secondary_msg_text
;
991 GString
*data_buf
= NULL
;
994 ret
= sync_pipe_open_command(argv
, &data_pipe_read_fd
, &sync_pipe_read_fd
,
995 &fork_child
, &msg
, update_cb
);
998 *secondary_msg
= NULL
;
1004 * We were able to set up to read dumpcap's output. Do so.
1006 * First, wait for an SP_ERROR_MSG message or SP_SUCCESS message.
1008 nread
= pipe_read_block(sync_pipe_read_fd
, &indicator
, SP_MAX_MSG_LEN
,
1009 buffer
, primary_msg
);
1011 /* We got a read error from the sync pipe, or we got no data at
1012 all from the sync pipe, so we're not going to be getting any
1013 data or error message from the child process. Pick up its
1014 exit status, and complain.
1016 We don't have to worry about killing the child, if the sync pipe
1017 returned an error. Usually this error is caused as the child killed
1018 itself while going down. Even in the rare cases that this isn't the
1019 case, the child will get an error when writing to the broken pipe
1020 the next time, cleaning itself up then. */
1021 ret
= sync_pipe_wait_for_child(fork_child
, &wait_msg
);
1023 /* We got an EOF from the sync pipe. That means that it exited
1024 before giving us any data to read. If ret is -1, we report
1025 that as a bad exit (e.g., exiting due to a signal); otherwise,
1026 we report it as a premature exit. */
1028 *primary_msg
= wait_msg
;
1030 *primary_msg
= g_strdup("Child dumpcap closed sync pipe prematurely");
1032 /* We got an error from the sync pipe. If ret is -1, report
1033 both the sync pipe I/O error and the wait error. */
1035 combined_msg
= g_strdup_printf("%s\n\n%s", *primary_msg
, wait_msg
);
1036 g_free(*primary_msg
);
1038 *primary_msg
= combined_msg
;
1041 *secondary_msg
= NULL
;
1046 /* we got a valid message block from the child, process it */
1051 * Error from dumpcap; there will be a primary message and a
1052 * secondary message.
1055 /* convert primary message */
1056 pipe_convert_header((guchar
*)buffer
, 4, &indicator
, &primary_msg_len
);
1057 primary_msg_text
= buffer
+4;
1058 /* convert secondary message */
1059 pipe_convert_header((guchar
*)primary_msg_text
+ primary_msg_len
, 4, &indicator
,
1060 &secondary_msg_len
);
1061 secondary_msg_text
= primary_msg_text
+ primary_msg_len
+ 4;
1062 /* the capture child will close the sync_pipe, nothing to do */
1065 * Pick up the child status.
1067 ret
= sync_pipe_close_command(&data_pipe_read_fd
, &sync_pipe_read_fd
,
1071 * Child process failed unexpectedly, or wait failed; msg is the
1075 *secondary_msg
= NULL
;
1078 * Child process failed, but returned the expected exit status.
1079 * Return the messages it gave us, and indicate failure.
1081 *primary_msg
= g_strdup(primary_msg_text
);
1082 *secondary_msg
= g_strdup(secondary_msg_text
);
1089 /* read the output from the command */
1090 data_buf
= g_string_new("");
1091 while ((count
= ws_read(data_pipe_read_fd
, buffer
, PIPE_BUF_SIZE
)) > 0) {
1092 buffer
[count
] = '\0';
1093 g_string_append(data_buf
, buffer
);
1097 * Pick up the child status.
1099 ret
= sync_pipe_close_command(&data_pipe_read_fd
, &sync_pipe_read_fd
,
1103 * Child process failed unexpectedly, or wait failed; msg is the
1107 *secondary_msg
= NULL
;
1108 g_string_free(data_buf
, TRUE
);
1112 * Child process succeeded.
1114 *primary_msg
= NULL
;
1115 *secondary_msg
= NULL
;
1116 *data
= data_buf
->str
;
1117 g_string_free(data_buf
, FALSE
);
1123 * Pick up the child status.
1125 ret
= sync_pipe_close_command(&data_pipe_read_fd
, &sync_pipe_read_fd
,
1129 * Child process failed unexpectedly, or wait failed; msg is the
1133 *secondary_msg
= NULL
;
1136 * Child process returned an unknown status.
1138 *primary_msg
= g_strdup_printf("dumpcap process gave an unexpected message type: 0x%02x",
1140 *secondary_msg
= NULL
;
1149 /* centralised logging and timing for sync_pipe_run_command_actual(),
1150 * redirects to sync_pipe_run_command_actual()
1153 sync_pipe_run_command(char** argv
, gchar
**data
, gchar
**primary_msg
,
1154 gchar
**secondary_msg
, void (*update_cb
)(void))
1157 GTimeVal start_time
;
1160 int logging_enabled
;
1162 /* check if logging is actually enabled, otherwise don't expend the CPU generating logging */
1163 logging_enabled
=( (G_LOG_LEVEL_DEBUG
| G_LOG_LEVEL_INFO
) & G_LOG_LEVEL_MASK
& prefs
.console_log_level
);
1164 if(logging_enabled
){
1165 g_get_current_time(&start_time
);
1166 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_INFO
, "sync_pipe_run_command() starts");
1167 for(i
=0; argv
[i
] != 0; i
++) {
1168 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, " argv[%d]: %s", i
, argv
[i
]);
1171 /* do the actual sync pipe run command */
1172 ret
=sync_pipe_run_command_actual(argv
, data
, primary_msg
, secondary_msg
, update_cb
);
1174 if(logging_enabled
){
1175 g_get_current_time(&end_time
);
1176 elapsed
= (float) ((end_time
.tv_sec
- start_time
.tv_sec
) +
1177 ((end_time
.tv_usec
- start_time
.tv_usec
) / 1e6
));
1179 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_INFO
, "sync_pipe_run_command() ends, taking %.3fs, result=%d", elapsed
, ret
);
1187 sync_interface_set_80211_chan(const gchar
*iface
, const char *freq
, const gchar
*type
,
1188 gchar
**data
, gchar
**primary_msg
,
1189 gchar
**secondary_msg
, void (*update_cb
)(void))
1195 argv
= init_pipe_args(&argc
);
1198 *primary_msg
= g_strdup("We don't know where to find dumpcap.");
1199 *secondary_msg
= NULL
;
1204 argv
= sync_pipe_add_arg(argv
, &argc
, "-i");
1205 argv
= sync_pipe_add_arg(argv
, &argc
, iface
);
1208 opt
= g_strdup_printf("%s,%s", freq
, type
);
1210 opt
= g_strdup_printf("%s", freq
);
1213 *primary_msg
= g_strdup("Out of mem.");
1214 *secondary_msg
= NULL
;
1219 argv
= sync_pipe_add_arg(argv
, &argc
, "-k");
1220 argv
= sync_pipe_add_arg(argv
, &argc
, opt
);
1223 /* Run dumpcap in capture child mode */
1224 argv
= sync_pipe_add_arg(argv
, &argc
, "-Z");
1225 argv
= sync_pipe_add_arg(argv
, &argc
, SIGNAL_PIPE_CTRL_ID_NONE
);
1228 ret
= sync_pipe_run_command(argv
, data
, primary_msg
, secondary_msg
, update_cb
);
1234 * Get the list of interfaces using dumpcap.
1236 * On success, *data points to a buffer containing the dumpcap output,
1237 * *primary_msg and *secondary_msg are NULL, and 0 is returned. *data
1238 * must be freed with g_free().
1240 * On failure, *data is NULL, *primary_msg points to an error message,
1241 * *secondary_msg either points to an additional error message or is
1242 * NULL, and -1 is returned; *primary_msg, and *secondary_msg if not NULL,
1243 * must be freed with g_free().
1246 sync_interface_list_open(gchar
**data
, gchar
**primary_msg
,
1247 gchar
**secondary_msg
, void (*update_cb
)(void))
1252 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "sync_interface_list_open");
1254 argv
= init_pipe_args(&argc
);
1257 *primary_msg
= g_strdup("We don't know where to find dumpcap..");
1258 *secondary_msg
= NULL
;
1263 /* Ask for the interface list */
1264 argv
= sync_pipe_add_arg(argv
, &argc
, "-D");
1267 /* Run dumpcap in capture child mode */
1268 argv
= sync_pipe_add_arg(argv
, &argc
, "-Z");
1269 argv
= sync_pipe_add_arg(argv
, &argc
, SIGNAL_PIPE_CTRL_ID_NONE
);
1271 return sync_pipe_run_command(argv
, data
, primary_msg
, secondary_msg
, update_cb
);
1275 * Get the capabilities of an interface using dumpcap.
1277 * On success, *data points to a buffer containing the dumpcap output,
1278 * *primary_msg and *secondary_msg are NULL, and 0 is returned. *data
1279 * must be freed with g_free().
1281 * On failure, *data is NULL, *primary_msg points to an error message,
1282 * *secondary_msg either points to an additional error message or is
1283 * NULL, and -1 is returned; *primary_msg, and *secondary_msg if not NULL,
1284 * must be freed with g_free().
1287 sync_if_capabilities_open(const gchar
*ifname
, gboolean monitor_mode
,
1288 gchar
**data
, gchar
**primary_msg
,
1289 gchar
**secondary_msg
, void (*update_cb
)(void))
1294 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "sync_if_capabilities_open");
1296 argv
= init_pipe_args(&argc
);
1299 *primary_msg
= g_strdup("We don't know where to find dumpcap.");
1300 *secondary_msg
= NULL
;
1305 /* Ask for the interface capabilities */
1306 argv
= sync_pipe_add_arg(argv
, &argc
, "-i");
1307 argv
= sync_pipe_add_arg(argv
, &argc
, ifname
);
1308 argv
= sync_pipe_add_arg(argv
, &argc
, "-L");
1310 argv
= sync_pipe_add_arg(argv
, &argc
, "-I");
1313 /* Run dumpcap in capture child mode */
1314 argv
= sync_pipe_add_arg(argv
, &argc
, "-Z");
1315 argv
= sync_pipe_add_arg(argv
, &argc
, SIGNAL_PIPE_CTRL_ID_NONE
);
1317 return sync_pipe_run_command(argv
, data
, primary_msg
, secondary_msg
, update_cb
);
1321 * Start getting interface statistics using dumpcap. On success, read_fd
1322 * contains the file descriptor for the pipe's stdout, *msg is unchanged,
1323 * and zero is returned. On failure, *msg will point to an error message
1324 * that must be g_free()d, and -1 will be returned.
1327 sync_interface_stats_open(int *data_read_fd
, int *fork_child
, gchar
**msg
, void (*update_cb
)(void))
1331 int message_read_fd
, ret
;
1333 gchar buffer
[PIPE_BUF_SIZE
+1];
1336 int primary_msg_len
;
1337 char *primary_msg_text
;
1338 int secondary_msg_len
;
1339 /*char *secondary_msg_text;*/
1342 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "sync_interface_stats_open");
1344 argv
= init_pipe_args(&argc
);
1347 *msg
= g_strdup("We don't know where to find dumpcap.");
1351 /* Ask for the interface statistics */
1352 argv
= sync_pipe_add_arg(argv
, &argc
, "-S");
1355 argv
= sync_pipe_add_arg(argv
, &argc
, "-Z");
1357 create_dummy_signal_pipe();
1358 argv
= sync_pipe_add_arg(argv
, &argc
, dummy_control_id
);
1360 argv
= sync_pipe_add_arg(argv
, &argc
, SIGNAL_PIPE_CTRL_ID_NONE
);
1363 ret
= sync_pipe_open_command(argv
, data_read_fd
, &message_read_fd
,
1364 fork_child
, msg
, update_cb
);
1369 * We were able to set up to read dumpcap's output. Do so.
1371 * First, wait for an SP_ERROR_MSG message or SP_SUCCESS message.
1373 nread
= pipe_read_block(message_read_fd
, &indicator
, SP_MAX_MSG_LEN
,
1376 /* We got a read error from the sync pipe, or we got no data at
1377 all from the sync pipe, so we're not going to be getting any
1378 data or error message from the child process. Pick up its
1379 exit status, and complain.
1381 We don't have to worry about killing the child, if the sync pipe
1382 returned an error. Usually this error is caused as the child killed
1383 itself while going down. Even in the rare cases that this isn't the
1384 case, the child will get an error when writing to the broken pipe
1385 the next time, cleaning itself up then. */
1386 ret
= sync_pipe_wait_for_child(*fork_child
, &wait_msg
);
1388 /* We got an EOF from the sync pipe. That means that it exited
1389 before giving us any data to read. If ret is -1, we report
1390 that as a bad exit (e.g., exiting due to a signal); otherwise,
1391 we report it as a premature exit. */
1395 *msg
= g_strdup("Child dumpcap closed sync pipe prematurely");
1397 /* We got an error from the sync pipe. If ret is -1, report
1398 both the sync pipe I/O error and the wait error. */
1400 combined_msg
= g_strdup_printf("%s\n\n%s", *msg
, wait_msg
);
1403 *msg
= combined_msg
;
1410 /* we got a valid message block from the child, process it */
1415 * Error from dumpcap; there will be a primary message and a
1416 * secondary message.
1419 /* convert primary message */
1420 pipe_convert_header((guchar
*)buffer
, 4, &indicator
, &primary_msg_len
);
1421 primary_msg_text
= buffer
+4;
1422 /* convert secondary message */
1423 pipe_convert_header((guchar
*)primary_msg_text
+ primary_msg_len
, 4, &indicator
,
1424 &secondary_msg_len
);
1425 /*secondary_msg_text = primary_msg_text + primary_msg_len + 4;*/
1426 /* the capture child will close the sync_pipe, nothing to do */
1429 * Pick up the child status.
1431 ret
= sync_pipe_close_command(data_read_fd
, &message_read_fd
,
1435 * Child process failed unexpectedly, or wait failed; msg is the
1440 * Child process failed, but returned the expected exit status.
1441 * Return the messages it gave us, and indicate failure.
1443 *msg
= g_strdup(primary_msg_text
);
1449 /* Close the message pipe. */
1450 ws_close(message_read_fd
);
1455 * Pick up the child status.
1457 ret
= sync_pipe_close_command(data_read_fd
, &message_read_fd
,
1461 * Child process failed unexpectedly, or wait failed; msg is the
1466 * Child process returned an unknown status.
1468 *msg
= g_strdup_printf("dumpcap process gave an unexpected message type: 0x%02x",
1477 /* Close down the stats process */
1479 sync_interface_stats_close(int *read_fd
, int *fork_child
, gchar
**msg
)
1483 * Don't bother waiting for the child. sync_pipe_close_command
1484 * does this for us on Windows.
1486 sync_pipe_kill(*fork_child
);
1488 return sync_pipe_close_command(read_fd
, NULL
, fork_child
, msg
);
1491 /* read a number of bytes from a pipe */
1492 /* (blocks until enough bytes read or an error occurs) */
1494 pipe_read_bytes(int pipe_fd
, char *bytes
, int required
, char **msg
)
1501 newly
= read(pipe_fd
, &bytes
[offset
], required
);
1504 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
,
1505 "read from pipe %d: EOF (capture closed?)", pipe_fd
);
1512 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
,
1513 "read from pipe %d: error(%u): %s", pipe_fd
, error
,
1515 *msg
= g_strdup_printf("Error reading from sync pipe: %s",
1520 required
-= (int)newly
;
1528 static gboolean
pipe_data_available(int pipe_fd
) {
1529 #ifdef _WIN32 /* PeekNamedPipe */
1530 HANDLE hPipe
= (HANDLE
) _get_osfhandle(pipe_fd
);
1533 if (hPipe
== INVALID_HANDLE_VALUE
)
1536 if (! PeekNamedPipe(hPipe
, NULL
, 0, NULL
, &bytes_avail
, NULL
))
1539 if (bytes_avail
> 0)
1544 struct timeval timeout
;
1547 FD_SET(pipe_fd
, &rfds
);
1549 timeout
.tv_usec
= 0;
1551 if (select(pipe_fd
+1, &rfds
, NULL
, NULL
, &timeout
) > 0)
1558 /* Read a line from a pipe, similar to fgets */
1560 sync_pipe_gets_nonblock(int pipe_fd
, char *bytes
, int max
) {
1564 while(offset
< max
- 1) {
1566 if (! pipe_data_available(pipe_fd
))
1568 newly
= read(pipe_fd
, &bytes
[offset
], 1);
1570 /* EOF - not necessarily an error */
1572 } else if (newly
== -1) {
1574 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
,
1575 "read from pipe %d: error(%u): %s", pipe_fd
, errno
, g_strerror(errno
));
1577 } else if (bytes
[offset
] == '\n') {
1583 bytes
[offset
] = '\0';
1589 /* convert header values (indicator and 3-byte length) */
1591 pipe_convert_header(const guchar
*header
, int header_len
, char *indicator
, int *block_len
) {
1593 g_assert(header_len
== 4);
1595 /* convert header values */
1596 *indicator
= header
[0];
1597 *block_len
= (header
[1]&0xFF)<<16 | (header
[2]&0xFF)<<8 | (header
[3]&0xFF);
1600 /* read a message from the sending pipe in the standard format
1601 (1-byte message indicator, 3-byte message length (excluding length
1602 and indicator field), and the rest is the message) */
1604 pipe_read_block(int pipe_fd
, char *indicator
, int len
, char *msg
,
1611 /* read header (indicator and 3-byte length) */
1612 newly
= pipe_read_bytes(pipe_fd
, header
, 4, err_msg
);
1616 * Immediate EOF; if the capture child exits normally, this
1617 * is an "I'm done" indication, so don't report it as an
1620 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
,
1621 "read %d got an EOF", pipe_fd
);
1624 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
,
1625 "read %d failed to read header: %lu", pipe_fd
, (long)newly
);
1628 * Short read, but not an immediate EOF.
1630 *err_msg
= g_strdup_printf("Premature EOF reading from sync pipe: got only %ld bytes",
1636 /* convert header values */
1637 pipe_convert_header((guchar
*)header
, 4, indicator
, &required
);
1639 /* only indicator with no value? */
1641 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
,
1642 "read %d indicator: %c empty value", pipe_fd
, *indicator
);
1646 /* does the data fit into the given buffer? */
1647 if(required
> len
) {
1648 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
,
1649 "read %d length error, required %d > len %d, header: 0x%02x 0x%02x 0x%02x 0x%02x",
1650 pipe_fd
, required
, len
,
1651 header
[0], header
[1], header
[2], header
[3]);
1653 /* we have a problem here, try to read some more bytes from the pipe to debug where the problem really is */
1654 memcpy(msg
, header
, sizeof(header
));
1655 newly
= read(pipe_fd
, &msg
[sizeof(header
)], len
-sizeof(header
));
1656 if (newly
< 0) { /* error */
1657 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
,
1658 "read from pipe %d: error(%u): %s", pipe_fd
, errno
, g_strerror(errno
));
1660 *err_msg
= g_strdup_printf("Unknown message from dumpcap, try to show it as a string: %s",
1666 /* read the actual block data */
1667 newly
= pipe_read_bytes(pipe_fd
, msg
, required
, err_msg
);
1668 if(newly
!= required
) {
1670 *err_msg
= g_strdup_printf("Unknown message from dumpcap, try to show it as a string: %s",
1676 /* XXX If message is "2part", the msg probably won't be sent to debug log correctly */
1677 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
,
1678 "read %d ok indicator: %c len: %u msg: %s", pipe_fd
, *indicator
,
1685 /* There's stuff to read from the sync pipe, meaning the child has sent
1686 us a message, or the sync pipe has closed, meaning the child has
1687 closed it (perhaps because it exited). */
1689 sync_pipe_input_cb(gint source
, gpointer user_data
)
1691 capture_session
*cap_session
= (capture_session
*)user_data
;
1693 char buffer
[SP_MAX_MSG_LEN
+1];
1699 char *secondary_msg
;
1700 char *wait_msg
, *combined_msg
;
1703 nread
= pipe_read_block(source
, &indicator
, SP_MAX_MSG_LEN
, buffer
,
1706 /* We got a read error, or a bad message, or an EOF, from the sync pipe.
1708 If we got a read error or a bad message, nread is -1 and
1709 primary_msg is set to point to an error message. We don't
1710 have to worry about killing the child; usually this error
1711 is caused as the child killed itself while going down.
1712 Even in the rare cases that this isn't the case, the child
1713 will get an error when writing to the broken pipe the next time,
1714 cleaning itself up then.
1716 If we got an EOF, nread is 0 and primary_msg isn't set. This
1717 is an indication that the capture is finished. */
1718 ret
= sync_pipe_wait_for_child(cap_session
->fork_child
, &wait_msg
);
1720 /* We got an EOF from the sync pipe. That means that the capture
1721 child exited, and not in the middle of a message; we treat
1722 that as an indication that it's done, and only report an
1723 error if ret is -1, in which case wait_msg is the error
1726 primary_msg
= wait_msg
;
1728 /* We got an error from the sync pipe. If ret is -1, report
1729 both the sync pipe I/O error and the wait error. */
1731 combined_msg
= g_strdup_printf("%s\n\n%s", primary_msg
, wait_msg
);
1732 g_free(primary_msg
);
1734 primary_msg
= combined_msg
;
1738 /* No more child process. */
1739 cap_session
->fork_child
= -1;
1740 cap_session
->fork_child_status
= ret
;
1743 ws_close(cap_session
->signal_pipe_write_fd
);
1745 capture_input_closed(cap_session
, primary_msg
);
1746 g_free(primary_msg
);
1750 /* we got a valid message block from the child, process it */
1753 if(!capture_input_new_file(cap_session
, buffer
)) {
1754 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "sync_pipe_input_cb: file failed, closing capture");
1756 /* We weren't able to open the new capture file; user has been
1757 alerted. Close the sync pipe. */
1760 /* The child has sent us a filename which we couldn't open.
1762 This could mean that the child is creating and deleting files
1763 (ring buffer mode) faster than we can handle it.
1765 That should only be the case for very fast file switches;
1766 We can't do much more than telling the child to stop.
1767 (This is the "emergency brake" if the user e.g. wants to
1768 switch files every second).
1770 This can also happen if the user specified "-", meaning
1771 "standard output", as the capture file. */
1772 sync_pipe_stop(cap_session
);
1773 capture_input_closed(cap_session
, NULL
);
1777 case SP_PACKET_COUNT
:
1778 npackets
= atoi(buffer
);
1779 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "sync_pipe_input_cb: new packets %u", npackets
);
1780 capture_input_new_packets(cap_session
, npackets
);
1783 /* convert primary message */
1784 pipe_convert_header((guchar
*)buffer
, 4, &indicator
, &primary_len
);
1785 primary_msg
= buffer
+4;
1786 /* convert secondary message */
1787 pipe_convert_header((guchar
*)primary_msg
+ primary_len
, 4, &indicator
, &secondary_len
);
1788 secondary_msg
= primary_msg
+ primary_len
+ 4;
1789 /* message output */
1790 capture_input_error_message(cap_session
, primary_msg
, secondary_msg
);
1791 /* the capture child will close the sync_pipe, nothing to do for now */
1792 /* (an error message doesn't mean we have to stop capturing) */
1794 case SP_BAD_FILTER
: {
1798 ch
= strtok(buffer
, ":");
1799 indx
= (int)strtol(ch
, NULL
, 10);
1800 ch
= strtok(NULL
, ":");
1801 capture_input_cfilter_error_message(cap_session
, indx
, ch
);
1802 /* the capture child will close the sync_pipe, nothing to do for now */
1806 capture_input_drops(cap_session
, (guint32
)strtoul(buffer
, NULL
, 10));
1809 g_assert_not_reached();
1818 * dumpcap is exiting; wait for it to exit. On success, *msgp is
1819 * unchanged, and the exit status of dumpcap is returned. On
1820 * failure (which includes "dumpcap exited due to being killed by
1821 * a signal or an exception"), *msgp points to an error message
1822 * for the failure, and -1 is returned. In the latter case, *msgp
1823 * must be freed with g_free().
1826 sync_pipe_wait_for_child(int fork_child
, gchar
**msgp
)
1828 int fork_child_status
;
1830 GTimeVal start_time
;
1835 * GLIB_CHECK_VERSION(2,28,0) adds g_get_real_time which could minimize or
1838 g_get_current_time(&start_time
);
1840 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "sync_pipe_wait_for_child: wait till child closed");
1841 g_assert(fork_child
!= -1);
1843 *msgp
= NULL
; /* assume no error */
1845 if (_cwait(&fork_child_status
, fork_child
, _WAIT_CHILD
) == -1) {
1846 *msgp
= g_strdup_printf("Error from cwait(): %s", g_strerror(errno
));
1850 * The child exited; return its exit status. Do not treat this as
1853 ret
= fork_child_status
;
1854 if ((fork_child_status
& 0xC0000000) == ERROR_SEVERITY_ERROR
) {
1855 /* Probably an exception code */
1856 *msgp
= g_strdup_printf("Child dumpcap process died: %s",
1857 win32strexception(fork_child_status
));
1862 if (waitpid(fork_child
, &fork_child_status
, 0) != -1) {
1863 if (WIFEXITED(fork_child_status
)) {
1865 * The child exited; return its exit status. Do not treat this as
1868 ret
= WEXITSTATUS(fork_child_status
);
1869 } else if (WIFSTOPPED(fork_child_status
)) {
1870 /* It stopped, rather than exiting. "Should not happen." */
1871 *msgp
= g_strdup_printf("Child dumpcap process stopped: %s",
1872 sync_pipe_signame(WSTOPSIG(fork_child_status
)));
1874 } else if (WIFSIGNALED(fork_child_status
)) {
1875 /* It died with a signal. */
1876 *msgp
= g_strdup_printf("Child dumpcap process died: %s%s",
1877 sync_pipe_signame(WTERMSIG(fork_child_status
)),
1878 WCOREDUMP(fork_child_status
) ? " - core dumped" : "");
1881 /* What? It had to either have exited, or stopped, or died with
1882 a signal; what happened here? */
1883 *msgp
= g_strdup_printf("Bad status from waitpid(): %#o",
1887 } else if (errno
!= ECHILD
) {
1888 *msgp
= g_strdup_printf("Error from waitpid(): %s", g_strerror(errno
));
1891 /* errno == ECHILD ; echld might have already reaped the child */
1892 ret
= fetch_dumpcap_pid
? 0 : -1;
1896 g_get_current_time(&end_time
);
1897 elapsed
= (float) ((end_time
.tv_sec
- start_time
.tv_sec
) +
1898 ((end_time
.tv_usec
- start_time
.tv_usec
) / 1e6
));
1899 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "sync_pipe_wait_for_child: capture child closed after %.3fs", elapsed
);
1905 /* convert signal to corresponding name */
1907 sync_pipe_signame(int sig
)
1910 static char sigmsg_buf
[6+1+3+1];
1919 sigmsg
= "Interrupted";
1927 sigmsg
= "Illegal instruction";
1931 sigmsg
= "Trace trap";
1939 sigmsg
= "Arithmetic exception";
1947 sigmsg
= "Bus error";
1951 sigmsg
= "Segmentation violation";
1954 /* http://metalab.unc.edu/pub/Linux/docs/HOWTO/GCC-HOWTO
1955 Linux is POSIX compliant. These are not POSIX-defined signals ---
1956 ISO/IEC 9945-1:1990 (IEEE Std 1003.1-1990), paragraph B.3.3.1.1 sez:
1958 ``The signals SIGBUS, SIGEMT, SIGIOT, SIGTRAP, and SIGSYS
1959 were omitted from POSIX.1 because their behavior is
1960 implementation dependent and could not be adequately catego-
1961 rized. Conforming implementations may deliver these sig-
1962 nals, but must document the circumstances under which they
1963 are delivered and note any restrictions concerning their
1966 So we only check for SIGSYS on those systems that happen to
1967 implement them (a system can be POSIX-compliant and implement
1968 them, it's just that POSIX doesn't *require* a POSIX-compliant
1969 system to implement them).
1974 sigmsg
= "Bad system call";
1979 sigmsg
= "Broken pipe";
1983 sigmsg
= "Alarm clock";
1987 sigmsg
= "Terminated";
1991 /* Returning a static buffer is ok in the context we use it here */
1992 g_snprintf(sigmsg_buf
, sizeof sigmsg_buf
, "Signal %d", sig
);
1993 sigmsg
= sigmsg_buf
;
2003 static void create_dummy_signal_pipe() {
2004 gchar
*dummy_signal_pipe_name
;
2006 if (dummy_signal_pipe
!= NULL
) return;
2008 if (!dummy_control_id
) {
2009 dummy_control_id
= g_strdup_printf("%d.dummy", GetCurrentProcessId());
2012 /* Create the signal pipe */
2013 dummy_signal_pipe_name
= g_strdup_printf(SIGNAL_PIPE_FORMAT
, dummy_control_id
);
2014 dummy_signal_pipe
= CreateNamedPipe(utf_8to16(dummy_signal_pipe_name
),
2015 PIPE_ACCESS_OUTBOUND
, PIPE_TYPE_BYTE
, 1, 65535, 65535, 0, NULL
);
2016 g_free(dummy_signal_pipe_name
);
2019 /* tell the child through the signal pipe that we want to quit the capture */
2021 signal_pipe_capquit_to_child(capture_session
*cap_session
)
2023 const char quit_msg
[] = "QUIT";
2026 g_log(LOG_DOMAIN_CAPTURE
, G_LOG_LEVEL_DEBUG
, "signal_pipe_capquit_to_child");
2028 /* it doesn't matter *what* we send here, the first byte will stop the capture */
2029 /* simply sending a "QUIT" string */
2030 /*pipe_write_block(cap_session->signal_pipe_write_fd, SP_QUIT, quit_msg);*/
2031 ret
= write(cap_session
->signal_pipe_write_fd
, quit_msg
, sizeof quit_msg
);
2033 g_log(LOG_DOMAIN_CAPTURE_CHILD
, G_LOG_LEVEL_WARNING
,
2034 "signal_pipe_capquit_to_child: %d header: error %s", cap_session
->signal_pipe_write_fd
, g_strerror(errno
));
2040 /* user wants to stop the capture run */
2042 sync_pipe_stop(capture_session
*cap_session
)
2047 gboolean terminate
= TRUE
;
2050 if (cap_session
->fork_child
!= -1) {
2052 /* send the SIGINT signal to close the capture child gracefully. */
2053 int sts
= kill(cap_session
->fork_child
, SIGINT
);
2055 g_log(LOG_DOMAIN_CAPTURE_CHILD
, G_LOG_LEVEL_WARNING
,
2056 "Sending SIGINT to child failed: %s\n", g_strerror(errno
));
2059 #define STOP_SLEEP_TIME 500 /* ms */
2060 #define STOP_CHECK_TIME 50
2061 /* First, use the special signal pipe to try to close the capture child
2064 signal_pipe_capquit_to_child(cap_session
);
2066 /* Next, wait for the process to exit on its own */
2067 for (count
= 0; count
< STOP_SLEEP_TIME
/ STOP_CHECK_TIME
; count
++) {
2068 if (GetExitCodeProcess((HANDLE
) cap_session
->fork_child
, &childstatus
) &&
2069 childstatus
!= STILL_ACTIVE
) {
2073 Sleep(STOP_CHECK_TIME
);
2076 /* Force the issue. */
2078 g_log(LOG_DOMAIN_CAPTURE_CHILD
, G_LOG_LEVEL_WARNING
,
2079 "sync_pipe_stop: forcing child to exit");
2080 sync_pipe_kill(cap_session
->fork_child
);
2087 /* Wireshark has to exit, force the capture child to close */
2089 sync_pipe_kill(int fork_child
)
2091 if (fork_child
!= -1) {
2093 int sts
= kill(fork_child
, SIGTERM
); /* SIGTERM so it can clean up if necessary */
2095 g_log(LOG_DOMAIN_CAPTURE_CHILD
, G_LOG_LEVEL_WARNING
,
2096 "Sending SIGTERM to child failed: %s\n", g_strerror(errno
));
2099 /* Remark: This is not the preferred method of closing a process!
2100 * the clean way would be getting the process id of the child process,
2101 * then getting window handle hWnd of that process (using EnumChildWindows),
2102 * and then do a SendMessage(hWnd, WM_CLOSE, 0, 0)
2104 * Unfortunately, I don't know how to get the process id from the
2105 * handle. OpenProcess will get an handle (not a window handle)
2106 * from the process ID; it will not get a window handle from the
2107 * process ID. (How could it? A process can have more than one
2108 * window. For that matter, a process might have *no* windows,
2109 * as a process running dumpcap, the normal child process program,
2112 * Hint: GenerateConsoleCtrlEvent() will only work if both processes are
2113 * running in the same console; that's not necessarily the case for
2114 * us, as we might not be running in a console.
2115 * And this also will require to have the process id.
2117 TerminateProcess((HANDLE
) (fork_child
), 0);
2122 void capture_sync_set_fetch_dumpcap_pid_cb(void(*cb
)(int pid
)) {
2123 fetch_dumpcap_pid
= cb
;
2126 #endif /* HAVE_LIBPCAP */