2004-10-30 11:44:06 +00:00
|
|
|
/* capture_sync.c
|
2006-05-28 20:28:20 +00:00
|
|
|
* Synchronisation between Wireshark capture parent and child instances
|
2004-10-30 11:44:06 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2004-10-30 11:44:06 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBPCAP
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <stdio.h>
|
2004-10-30 23:26:47 +00:00
|
|
|
#include <ctype.h>
|
2004-10-30 11:44:06 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2004-10-30 23:26:47 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2007-08-02 21:45:27 +00:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2004-10-30 23:26:47 +00:00
|
|
|
#include <signal.h>
|
|
|
|
|
2006-02-17 02:18:48 +00:00
|
|
|
#ifdef _WIN32
|
2006-09-23 00:38:56 +00:00
|
|
|
#include "epan/unicode-utils.h"
|
2006-02-17 02:18:48 +00:00
|
|
|
#endif
|
|
|
|
|
2004-10-30 23:26:47 +00:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
# include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
|
2006-02-17 02:18:48 +00:00
|
|
|
#include "capture-pcap-util.h"
|
|
|
|
|
2004-10-30 23:26:47 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
/*
|
|
|
|
* Define various POSIX macros (and, in the case of WCOREDUMP, non-POSIX
|
|
|
|
* macros) on UNIX systems that don't have them.
|
|
|
|
*/
|
|
|
|
#ifndef WIFEXITED
|
|
|
|
# define WIFEXITED(status) (((status) & 0177) == 0)
|
|
|
|
#endif
|
|
|
|
#ifndef WIFSTOPPED
|
|
|
|
# define WIFSTOPPED(status) (((status) & 0177) == 0177)
|
|
|
|
#endif
|
|
|
|
#ifndef WIFSIGNALED
|
|
|
|
# define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
|
|
|
|
#endif
|
|
|
|
#ifndef WEXITSTATUS
|
|
|
|
# define WEXITSTATUS(status) ((status) >> 8)
|
|
|
|
#endif
|
|
|
|
#ifndef WTERMSIG
|
|
|
|
# define WTERMSIG(status) ((status) & 0177)
|
|
|
|
#endif
|
|
|
|
#ifndef WCOREDUMP
|
|
|
|
# define WCOREDUMP(status) ((status) & 0200)
|
|
|
|
#endif
|
|
|
|
#ifndef WSTOPSIG
|
|
|
|
# define WSTOPSIG(status) ((status) >> 8)
|
|
|
|
#endif
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2004-10-30 11:44:06 +00:00
|
|
|
#include <epan/packet.h>
|
|
|
|
#include <epan/prefs.h>
|
|
|
|
|
|
|
|
#include "globals.h"
|
2005-02-04 08:52:38 +00:00
|
|
|
#include "file.h"
|
2005-12-15 00:48:59 +00:00
|
|
|
#include <epan/filesystem.h>
|
2004-10-30 11:44:06 +00:00
|
|
|
|
|
|
|
#include "capture.h"
|
|
|
|
#include "capture_sync.h"
|
|
|
|
#include "simple_dialog.h"
|
|
|
|
|
2006-04-03 01:56:53 +00:00
|
|
|
#include "sync_pipe.h"
|
|
|
|
|
2004-10-30 11:44:06 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "capture-wpcap.h"
|
|
|
|
#endif
|
|
|
|
#include "ui_util.h"
|
2005-11-06 22:43:25 +00:00
|
|
|
#include "file_util.h"
|
2005-05-26 17:50:27 +00:00
|
|
|
#include "log.h"
|
2004-10-30 11:44:06 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <process.h> /* For spawning child process */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-02-17 22:39:32 +00:00
|
|
|
|
2004-10-30 11:44:06 +00:00
|
|
|
#ifndef _WIN32
|
2005-08-06 14:03:14 +00:00
|
|
|
static const char *sync_pipe_signame(int);
|
2004-10-30 11:44:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean sync_pipe_input_cb(gint source, gpointer user_data);
|
2005-05-26 19:06:01 +00:00
|
|
|
static void sync_pipe_wait_for_child(capture_options *capture_opts);
|
2004-10-30 11:44:06 +00:00
|
|
|
|
|
|
|
|
2005-04-10 16:43:22 +00:00
|
|
|
|
2006-03-24 02:23:48 +00:00
|
|
|
/* Append an arg (realloc) to an argc/argv array */
|
|
|
|
/* (add a string pointer to a NULL-terminated array of string pointers) */
|
2005-08-06 14:03:14 +00:00
|
|
|
static const char **
|
|
|
|
sync_pipe_add_arg(const char **args, int *argc, const char *arg)
|
2004-10-30 11:44:06 +00:00
|
|
|
{
|
|
|
|
/* Grow the array; "*argc" currently contains the number of string
|
|
|
|
pointers, *not* counting the NULL pointer at the end, so we have
|
|
|
|
to add 2 in order to get the new size of the array, including the
|
|
|
|
new pointer and the terminating NULL pointer. */
|
2005-08-20 22:16:12 +00:00
|
|
|
args = g_realloc( (gpointer) args, (*argc + 2) * sizeof (char *));
|
2004-10-30 11:44:06 +00:00
|
|
|
|
|
|
|
/* Stuff the pointer into the penultimate element of the array, which
|
|
|
|
is the one at the index specified by "*argc". */
|
|
|
|
args[*argc] = arg;
|
|
|
|
|
|
|
|
/* Now bump the count. */
|
|
|
|
(*argc)++;
|
|
|
|
|
|
|
|
/* We overwrite the NULL pointer; put it back right after the
|
|
|
|
element we added. */
|
|
|
|
args[*argc] = NULL;
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-17 09:45:52 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* Quote the argument element if necessary, so that it will get
|
|
|
|
* reconstructed correctly in the C runtime startup code. Note that
|
|
|
|
* the unquoting algorithm in the C runtime is really weird, and
|
|
|
|
* rather different than what Unix shells do. See stdargv.c in the C
|
|
|
|
* runtime sources (in the Platform SDK, in src/crt).
|
|
|
|
*
|
|
|
|
* Stolen from GLib's protect_argv(), an internal routine that quotes
|
|
|
|
* string in an argument list so that they arguments will be handled
|
|
|
|
* correctly in the command-line string passed to CreateProcess()
|
|
|
|
* if that string is constructed by gluing those strings together.
|
|
|
|
*/
|
|
|
|
static gchar *
|
2006-02-17 22:39:32 +00:00
|
|
|
protect_arg (const gchar *argv)
|
2006-02-17 09:45:52 +00:00
|
|
|
{
|
|
|
|
gchar *new_arg;
|
2006-02-17 22:39:32 +00:00
|
|
|
const gchar *p = argv;
|
2006-02-17 09:45:52 +00:00
|
|
|
gchar *q;
|
|
|
|
gint len = 0;
|
|
|
|
gboolean need_dblquotes = FALSE;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
if (*p == ' ' || *p == '\t')
|
|
|
|
need_dblquotes = TRUE;
|
|
|
|
else if (*p == '"')
|
|
|
|
len++;
|
|
|
|
else if (*p == '\\') {
|
2006-02-17 22:39:32 +00:00
|
|
|
const gchar *pp = p;
|
2006-02-17 09:45:52 +00:00
|
|
|
|
|
|
|
while (*pp && *pp == '\\')
|
|
|
|
pp++;
|
|
|
|
if (*pp == '"')
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
len++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
q = new_arg = g_malloc (len + need_dblquotes*2 + 1);
|
|
|
|
p = argv;
|
|
|
|
|
|
|
|
if (need_dblquotes)
|
|
|
|
*q++ = '"';
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '"')
|
|
|
|
*q++ = '\\';
|
|
|
|
else if (*p == '\\') {
|
2006-02-17 22:39:32 +00:00
|
|
|
const gchar *pp = p;
|
2006-02-17 09:45:52 +00:00
|
|
|
|
|
|
|
while (*pp && *pp == '\\')
|
|
|
|
pp++;
|
|
|
|
if (*pp == '"')
|
|
|
|
*q++ = '\\';
|
|
|
|
}
|
|
|
|
*q++ = *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_dblquotes)
|
|
|
|
*q++ = '"';
|
|
|
|
*q++ = '\0';
|
|
|
|
|
|
|
|
return new_arg;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-07-20 21:43:07 +00:00
|
|
|
/* Initialize an argument list and add dumpcap to it. */
|
|
|
|
static const char **
|
|
|
|
init_pipe_args(int *argc) {
|
|
|
|
const char **argv;
|
|
|
|
const char *progfile_dir;
|
|
|
|
char *exename;
|
2006-02-17 09:45:52 +00:00
|
|
|
|
2007-07-20 21:43:07 +00:00
|
|
|
progfile_dir = get_progfile_dir();
|
|
|
|
if (progfile_dir == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-02-17 09:45:52 +00:00
|
|
|
|
2007-07-20 21:43:07 +00:00
|
|
|
/* Allocate the string pointer array with enough space for the
|
|
|
|
terminating NULL pointer. */
|
|
|
|
*argc = 0;
|
|
|
|
argv = g_malloc(sizeof (char *));
|
|
|
|
*argv = NULL;
|
|
|
|
|
|
|
|
/* take Wireshark's absolute program path and replace "Wireshark" with "dumpcap" */
|
|
|
|
exename = g_strdup_printf("%s" G_DIR_SEPARATOR_S "dumpcap", progfile_dir);
|
|
|
|
|
|
|
|
/* Make that the first argument in the argument list (argv[0]). */
|
|
|
|
argv = sync_pipe_add_arg(argv, argc, exename);
|
|
|
|
|
|
|
|
return argv;
|
|
|
|
}
|
2005-04-10 14:29:12 +00:00
|
|
|
|
2007-07-20 21:43:07 +00:00
|
|
|
#define ARGV_NUMBER_LEN 24
|
2006-03-24 02:23:48 +00:00
|
|
|
/* a new capture run: start a new dumpcap task and hand over parameters through command line */
|
2004-10-30 11:44:06 +00:00
|
|
|
gboolean
|
2005-04-30 15:39:40 +00:00
|
|
|
sync_pipe_start(capture_options *capture_opts) {
|
2005-04-10 14:29:12 +00:00
|
|
|
char ssnap[ARGV_NUMBER_LEN];
|
2007-11-27 19:57:35 +00:00
|
|
|
char sdlt[ARGV_NUMBER_LEN];
|
2005-04-10 14:29:12 +00:00
|
|
|
char scount[ARGV_NUMBER_LEN];
|
|
|
|
char sfilesize[ARGV_NUMBER_LEN];
|
|
|
|
char sfile_duration[ARGV_NUMBER_LEN];
|
|
|
|
char sring_num_files[ARGV_NUMBER_LEN];
|
|
|
|
char sautostop_files[ARGV_NUMBER_LEN];
|
|
|
|
char sautostop_filesize[ARGV_NUMBER_LEN];
|
|
|
|
char sautostop_duration[ARGV_NUMBER_LEN];
|
2004-10-30 11:44:06 +00:00
|
|
|
#ifdef _WIN32
|
2005-05-30 04:46:25 +00:00
|
|
|
char buffer_size[ARGV_NUMBER_LEN];
|
2006-02-11 23:25:11 +00:00
|
|
|
HANDLE sync_pipe_read; /* pipe used to send messages from child to parent */
|
|
|
|
HANDLE sync_pipe_write; /* pipe used to send messages from child to parent */
|
2007-10-26 16:32:28 +00:00
|
|
|
HANDLE signal_pipe; /* named pipe used to send messages from parent to child (currently only stop) */
|
2006-02-11 23:25:11 +00:00
|
|
|
GString *args = g_string_sized_new(200);
|
2006-02-17 09:45:52 +00:00
|
|
|
gchar *quoted_arg;
|
2006-03-09 14:39:44 +00:00
|
|
|
SECURITY_ATTRIBUTES sa;
|
2006-02-11 23:25:11 +00:00
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
int i;
|
2007-10-26 16:32:28 +00:00
|
|
|
char control_id[ARGV_NUMBER_LEN];
|
|
|
|
gchar *signal_pipe_name;
|
2005-04-10 14:29:12 +00:00
|
|
|
#else
|
|
|
|
char errmsg[1024+1];
|
2006-02-11 23:25:11 +00:00
|
|
|
int sync_pipe[2]; /* pipe used to send messages from child to parent */
|
|
|
|
enum PIPES { PIPE_READ, PIPE_WRITE }; /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
|
2004-10-30 11:44:06 +00:00
|
|
|
#endif
|
2006-02-11 23:25:11 +00:00
|
|
|
int sync_pipe_read_fd;
|
2005-04-10 14:29:12 +00:00
|
|
|
int argc;
|
2005-08-06 14:03:14 +00:00
|
|
|
const char **argv;
|
2004-10-30 11:44:06 +00:00
|
|
|
|
2005-03-28 00:19:02 +00:00
|
|
|
|
2005-05-26 17:50:27 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_start");
|
|
|
|
capture_opts_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, capture_opts);
|
2005-03-28 00:19:02 +00:00
|
|
|
|
2005-02-04 01:29:29 +00:00
|
|
|
capture_opts->fork_child = -1;
|
2004-10-30 11:44:06 +00:00
|
|
|
|
2007-07-20 21:43:07 +00:00
|
|
|
argv = init_pipe_args(&argc);
|
|
|
|
if (!argv) {
|
|
|
|
/* We don't know where to find dumpcap. */
|
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "We don't know where to find dumpcap.");
|
|
|
|
return FALSE;
|
2006-05-01 06:51:14 +00:00
|
|
|
}
|
|
|
|
|
2004-10-30 11:44:06 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-i");
|
2005-02-06 21:20:35 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, capture_opts->iface);
|
2004-10-30 11:44:06 +00:00
|
|
|
|
2004-12-29 09:09:35 +00:00
|
|
|
if (capture_opts->has_snaplen) {
|
2004-10-30 11:44:06 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-s");
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(ssnap, ARGV_NUMBER_LEN, "%d",capture_opts->snaplen);
|
2004-10-30 11:44:06 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, ssnap);
|
|
|
|
}
|
|
|
|
|
2004-12-29 09:09:35 +00:00
|
|
|
if (capture_opts->linktype != -1) {
|
2004-10-30 11:44:06 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-y");
|
|
|
|
#ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
|
2007-11-27 19:57:35 +00:00
|
|
|
g_snprintf(sdlt, ARGV_NUMBER_LEN, "%s",linktype_val_to_name(capture_opts->linktype));
|
2004-10-30 11:44:06 +00:00
|
|
|
#else
|
2006-03-24 02:23:48 +00:00
|
|
|
/* we can't get the type name, just treat it as a number */
|
2007-11-27 19:57:35 +00:00
|
|
|
g_snprintf(sdlt, ARGV_NUMBER_LEN, "%d",capture_opts->linktype);
|
2004-10-30 11:44:06 +00:00
|
|
|
#endif
|
2007-11-27 19:57:35 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, sdlt);
|
2004-10-30 11:44:06 +00:00
|
|
|
}
|
|
|
|
|
2005-03-28 00:19:02 +00:00
|
|
|
if(capture_opts->multi_files_on) {
|
|
|
|
if (capture_opts->has_autostop_filesize) {
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-b");
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(sfilesize, ARGV_NUMBER_LEN, "filesize:%d",capture_opts->autostop_filesize);
|
2005-03-28 00:19:02 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, sfilesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capture_opts->has_file_duration) {
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-b");
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(sfile_duration, ARGV_NUMBER_LEN, "duration:%d",capture_opts->file_duration);
|
2005-03-28 00:19:02 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, sfile_duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capture_opts->has_ring_num_files) {
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-b");
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(sring_num_files, ARGV_NUMBER_LEN, "files:%d",capture_opts->ring_num_files);
|
2005-03-28 00:19:02 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, sring_num_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capture_opts->has_autostop_files) {
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-a");
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(sautostop_files, ARGV_NUMBER_LEN, "files:%d",capture_opts->autostop_files);
|
2005-03-28 00:19:02 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, sautostop_files);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (capture_opts->has_autostop_filesize) {
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-a");
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(sautostop_filesize, ARGV_NUMBER_LEN, "filesize:%d",capture_opts->autostop_filesize);
|
2005-03-28 00:19:02 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, sautostop_filesize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capture_opts->has_autostop_packets) {
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-c");
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(scount, ARGV_NUMBER_LEN, "%d",capture_opts->autostop_packets);
|
2005-03-28 00:19:02 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, scount);
|
2004-10-30 11:44:06 +00:00
|
|
|
}
|
|
|
|
|
2004-12-29 09:09:35 +00:00
|
|
|
if (capture_opts->has_autostop_duration) {
|
2004-10-30 11:44:06 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-a");
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(sautostop_duration, ARGV_NUMBER_LEN, "duration:%d",capture_opts->autostop_duration);
|
2004-10-30 11:44:06 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, sautostop_duration);
|
|
|
|
}
|
|
|
|
|
2004-12-29 09:09:35 +00:00
|
|
|
if (!capture_opts->promisc_mode)
|
2004-10-30 11:44:06 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-p");
|
|
|
|
|
2006-02-11 23:25:11 +00:00
|
|
|
/* dumpcap should be running in capture child mode (hidden feature) */
|
|
|
|
#ifndef DEBUG_CHILD
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-Z");
|
2007-10-26 16:32:28 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
g_snprintf(control_id, ARGV_NUMBER_LEN, "%d", GetCurrentProcessId());
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, control_id);
|
|
|
|
#else
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
|
|
|
|
#endif
|
2006-02-11 23:25:11 +00:00
|
|
|
#endif
|
2004-10-30 11:44:06 +00:00
|
|
|
|
2006-02-11 23:25:11 +00:00
|
|
|
#ifdef _WIN32
|
2005-05-30 04:46:25 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-B");
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(buffer_size, ARGV_NUMBER_LEN, "%d",capture_opts->buffer_size);
|
2005-05-30 04:46:25 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, buffer_size);
|
2006-02-17 11:53:11 +00:00
|
|
|
#endif
|
2005-05-30 04:46:25 +00:00
|
|
|
|
2006-02-17 22:39:32 +00:00
|
|
|
if (capture_opts->cfilter != NULL && strlen(capture_opts->cfilter) != 0) {
|
2004-10-30 11:44:06 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-f");
|
2006-02-17 11:53:11 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, capture_opts->cfilter);
|
2004-10-30 11:44:06 +00:00
|
|
|
}
|
|
|
|
|
2005-05-25 17:46:04 +00:00
|
|
|
if(capture_opts->save_file) {
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-w");
|
2006-02-17 11:53:11 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, capture_opts->save_file);
|
2005-05-25 17:46:04 +00:00
|
|
|
}
|
|
|
|
|
2006-02-17 11:53:11 +00:00
|
|
|
#ifdef _WIN32
|
2006-02-11 23:25:11 +00:00
|
|
|
/* init SECURITY_ATTRIBUTES */
|
2006-03-09 14:39:44 +00:00
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
2006-02-10 01:27:23 +00:00
|
|
|
|
2006-02-11 23:25:11 +00:00
|
|
|
/* Create a pipe for the child process */
|
2007-10-25 20:37:12 +00:00
|
|
|
/* (increase this value if you have trouble while fast capture file switches) */
|
2006-02-11 23:25:11 +00:00
|
|
|
if (! CreatePipe(&sync_pipe_read, &sync_pipe_write, &sa, 5120)) {
|
|
|
|
/* Couldn't create the pipe between parent and child. */
|
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Couldn't create sync pipe: %s",
|
|
|
|
strerror(errno));
|
2007-07-21 00:31:55 +00:00
|
|
|
g_free( (gpointer) argv[0]);
|
2006-02-11 23:25:11 +00:00
|
|
|
g_free( (gpointer) argv);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-02-10 01:27:23 +00:00
|
|
|
|
2007-10-26 16:32:28 +00:00
|
|
|
/* Create the signal pipe */
|
|
|
|
signal_pipe_name = g_strdup_printf(SIGNAL_PIPE_FORMAT, control_id);
|
|
|
|
signal_pipe = CreateNamedPipe(utf_8to16(signal_pipe_name),
|
|
|
|
PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE, 1, 65535, 65535, 0, NULL);
|
|
|
|
g_free(signal_pipe_name);
|
|
|
|
|
|
|
|
if (signal_pipe == INVALID_HANDLE_VALUE) {
|
2006-02-11 23:25:11 +00:00
|
|
|
/* Couldn't create the signal pipe between parent and child. */
|
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Couldn't create signal pipe: %s",
|
|
|
|
strerror(errno));
|
2007-07-21 00:31:55 +00:00
|
|
|
g_free( (gpointer) argv[0]);
|
2006-02-11 23:25:11 +00:00
|
|
|
g_free( (gpointer) argv);
|
|
|
|
return FALSE;
|
2006-02-10 01:27:23 +00:00
|
|
|
}
|
2006-02-11 23:25:11 +00:00
|
|
|
|
|
|
|
/* init STARTUPINFO */
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
#ifdef DEBUG_CHILD
|
|
|
|
si.dwFlags = STARTF_USESHOWWINDOW;
|
|
|
|
si.wShowWindow = SW_SHOW;
|
|
|
|
#else
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
|
|
|
|
si.wShowWindow = SW_HIDE; /* this hides the console window */
|
2007-10-26 16:32:28 +00:00
|
|
|
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
2007-10-10 01:31:36 +00:00
|
|
|
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
2006-02-11 23:25:11 +00:00
|
|
|
si.hStdError = sync_pipe_write;
|
2006-02-17 02:18:48 +00:00
|
|
|
/*si.hStdError = (HANDLE) _get_osfhandle(2);*/
|
2006-02-10 01:27:23 +00:00
|
|
|
#endif
|
2006-02-11 23:25:11 +00:00
|
|
|
|
|
|
|
/* convert args array into a single string */
|
|
|
|
/* XXX - could change sync_pipe_add_arg() instead */
|
|
|
|
/* there is a drawback here: the length is internally limited to 1024 bytes */
|
|
|
|
for(i=0; argv[i] != 0; i++) {
|
2006-02-17 22:39:32 +00:00
|
|
|
if(i != 0) g_string_append_c(args, ' '); /* don't prepend a space before the path!!! */
|
2006-02-17 09:45:52 +00:00
|
|
|
quoted_arg = protect_arg(argv[i]);
|
|
|
|
g_string_append(args, quoted_arg);
|
|
|
|
g_free(quoted_arg);
|
2005-12-15 00:48:59 +00:00
|
|
|
}
|
2006-02-11 23:25:11 +00:00
|
|
|
|
|
|
|
/* call dumpcap */
|
2006-03-09 14:39:44 +00:00
|
|
|
if(!CreateProcess(NULL, utf_8to16(args->str), NULL, NULL, TRUE,
|
2006-02-12 00:59:34 +00:00
|
|
|
CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
|
2006-04-03 01:56:53 +00:00
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
|
|
|
|
"Couldn't run %s in child process: error %u",
|
|
|
|
args->str, GetLastError());
|
2006-04-03 01:58:43 +00:00
|
|
|
CloseHandle(sync_pipe_read);
|
|
|
|
CloseHandle(sync_pipe_write);
|
2007-07-21 00:31:55 +00:00
|
|
|
g_free( (gpointer) argv[0]);
|
2006-04-03 01:58:43 +00:00
|
|
|
g_free( (gpointer) argv);
|
2006-04-03 01:56:53 +00:00
|
|
|
return FALSE;
|
2006-02-11 23:25:11 +00:00
|
|
|
}
|
2006-04-03 01:56:53 +00:00
|
|
|
capture_opts->fork_child = (int) pi.hProcess;
|
2006-02-11 23:25:11 +00:00
|
|
|
g_string_free(args, TRUE);
|
|
|
|
|
|
|
|
/* associate the operating system filehandle to a C run-time file handle */
|
2006-02-12 12:04:17 +00:00
|
|
|
/* (good file handle infos at: http://www.flounder.com/handles.htm) */
|
2006-02-11 23:25:11 +00:00
|
|
|
sync_pipe_read_fd = _open_osfhandle( (long) sync_pipe_read, _O_BINARY);
|
|
|
|
|
|
|
|
/* associate the operating system filehandle to a C run-time file handle */
|
2007-10-26 16:32:28 +00:00
|
|
|
capture_opts->signal_pipe_write_fd = _open_osfhandle( (long) signal_pipe, _O_BINARY);
|
2006-02-11 23:25:11 +00:00
|
|
|
|
|
|
|
#else /* _WIN32 */
|
2004-10-30 11:44:06 +00:00
|
|
|
if (pipe(sync_pipe) < 0) {
|
|
|
|
/* Couldn't create the pipe between parent and child. */
|
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Couldn't create sync pipe: %s",
|
2005-03-28 14:39:31 +00:00
|
|
|
strerror(errno));
|
2007-07-21 00:31:55 +00:00
|
|
|
g_free( (gpointer) argv[0]);
|
2005-04-10 16:43:22 +00:00
|
|
|
g_free(argv);
|
2004-10-30 11:44:06 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-02-04 04:31:59 +00:00
|
|
|
if ((capture_opts->fork_child = fork()) == 0) {
|
2004-10-30 11:44:06 +00:00
|
|
|
/*
|
2006-08-02 19:58:36 +00:00
|
|
|
* Child process - run dumpcap with the right arguments to make
|
|
|
|
* it just capture with the specified capture parameters
|
2004-10-30 11:44:06 +00:00
|
|
|
*/
|
2007-10-10 01:31:36 +00:00
|
|
|
eth_close(2);
|
2004-10-30 11:44:06 +00:00
|
|
|
dup(sync_pipe[PIPE_WRITE]);
|
2005-11-06 22:43:25 +00:00
|
|
|
eth_close(sync_pipe[PIPE_READ]);
|
2007-07-20 21:43:07 +00:00
|
|
|
execv(argv[0], (gpointer)argv);
|
2006-04-03 01:56:53 +00:00
|
|
|
g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
|
2007-07-20 21:43:07 +00:00
|
|
|
argv[0], strerror(errno));
|
2007-09-29 01:14:16 +00:00
|
|
|
sync_pipe_errmsg_to_parent(1, errmsg, "");
|
2004-10-30 11:44:06 +00:00
|
|
|
|
|
|
|
/* Exit with "_exit()", so that we don't close the connection
|
|
|
|
to the X server (and cause stuff buffered up by our parent but
|
|
|
|
not yet sent to be sent, as that stuff should only be sent by
|
|
|
|
our parent). */
|
|
|
|
_exit(2);
|
|
|
|
}
|
2006-02-11 23:25:11 +00:00
|
|
|
|
|
|
|
sync_pipe_read_fd = sync_pipe[PIPE_READ];
|
2004-10-30 11:44:06 +00:00
|
|
|
#endif
|
|
|
|
|
2007-07-21 00:31:55 +00:00
|
|
|
g_free( (gpointer) argv[0]); /* exename */
|
2006-02-11 23:25:11 +00:00
|
|
|
|
2004-10-30 11:44:06 +00:00
|
|
|
/* Parent process - read messages from the child process over the
|
|
|
|
sync pipe. */
|
2005-08-20 22:16:12 +00:00
|
|
|
g_free( (gpointer) argv); /* free up arg array */
|
2004-10-30 11:44:06 +00:00
|
|
|
|
|
|
|
/* Close the write side of the pipe, so that only the child has it
|
|
|
|
open, and thus it completely closes, and thus returns to us
|
|
|
|
an EOF indication, if the child closes it (either deliberately
|
|
|
|
or by exiting abnormally). */
|
2006-02-11 23:25:11 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
CloseHandle(sync_pipe_write);
|
|
|
|
#else
|
2005-11-06 22:43:25 +00:00
|
|
|
eth_close(sync_pipe[PIPE_WRITE]);
|
2006-02-11 23:25:11 +00:00
|
|
|
#endif
|
2004-10-30 11:44:06 +00:00
|
|
|
|
2005-02-04 01:29:29 +00:00
|
|
|
if (capture_opts->fork_child == -1) {
|
2004-10-30 11:44:06 +00:00
|
|
|
/* We couldn't even create the child process. */
|
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
|
2005-03-28 14:39:31 +00:00
|
|
|
"Couldn't create child process: %s", strerror(errno));
|
2006-02-11 23:25:11 +00:00
|
|
|
eth_close(sync_pipe_read_fd);
|
2005-04-10 16:43:22 +00:00
|
|
|
#ifdef _WIN32
|
2006-02-11 23:25:11 +00:00
|
|
|
eth_close(capture_opts->signal_pipe_write_fd);
|
2005-04-10 16:43:22 +00:00
|
|
|
#endif
|
2004-10-30 11:44:06 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-04-10 16:43:22 +00:00
|
|
|
/* we might wait for a moment till child is ready, so update screen now */
|
|
|
|
main_window_update();
|
|
|
|
|
2005-03-28 14:39:31 +00:00
|
|
|
/* We were able to set up to read the capture file;
|
|
|
|
arrange that our callback be called whenever it's possible
|
|
|
|
to read from the sync pipe, so that it's called when
|
|
|
|
the child process wants to tell us something. */
|
2004-10-30 11:44:06 +00:00
|
|
|
|
2005-03-28 14:39:31 +00:00
|
|
|
/* we have a running capture, now wait for the real capture filename */
|
2006-03-09 14:39:44 +00:00
|
|
|
pipe_input_set_handler(sync_pipe_read_fd, (gpointer) capture_opts,
|
2005-03-28 14:39:31 +00:00
|
|
|
&capture_opts->fork_child, sync_pipe_input_cb);
|
2005-02-28 22:46:49 +00:00
|
|
|
|
2005-03-28 14:39:31 +00:00
|
|
|
return TRUE;
|
2004-10-30 11:44:06 +00:00
|
|
|
}
|
|
|
|
|
2007-06-11 03:58:58 +00:00
|
|
|
/*
|
2007-08-02 21:45:27 +00:00
|
|
|
* Open dumpcap with the supplied arguments. On success, msg points to
|
|
|
|
* a buffer containing the dumpcap output and returns 0. read_fd and
|
|
|
|
* fork_child point to the pipe's file descriptor and child PID/handle,
|
|
|
|
* respectively. On failure, msg points to the error message returned by
|
|
|
|
* dumpcap, and returns dumpcap's exit value. In either case, msg must be
|
|
|
|
* freed with g_free().
|
2007-06-11 03:58:58 +00:00
|
|
|
*/
|
2007-07-20 21:43:07 +00:00
|
|
|
/* XXX - This duplicates a lot of code in sync_pipe_start() */
|
2007-06-11 03:58:58 +00:00
|
|
|
#define PIPE_BUF_SIZE 5120
|
2007-07-20 21:43:07 +00:00
|
|
|
static int
|
2007-08-02 21:45:27 +00:00
|
|
|
sync_pipe_open_command(const char** argv, int *read_fd, int *fork_child, gchar **msg) {
|
2007-06-11 03:58:58 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE sync_pipe_read; /* pipe used to send messages from child to parent */
|
|
|
|
HANDLE sync_pipe_write; /* pipe used to send messages from parent to child */
|
|
|
|
GString *args = g_string_sized_new(200);
|
|
|
|
gchar *quoted_arg;
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
int i;
|
|
|
|
#else
|
|
|
|
int sync_pipe[2]; /* pipe used to send messages from child to parent */
|
|
|
|
enum PIPES { PIPE_READ, PIPE_WRITE }; /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
|
|
|
|
#endif
|
|
|
|
|
2007-08-02 21:45:27 +00:00
|
|
|
*fork_child = -1;
|
|
|
|
*read_fd = -1;
|
2007-07-20 21:43:07 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_run_command");
|
2007-06-11 03:58:58 +00:00
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
/* We can't return anything */
|
|
|
|
#ifdef _WIN32
|
|
|
|
g_string_free(args, TRUE);
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
/* init SECURITY_ATTRIBUTES */
|
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
|
|
/* Create a pipe for the child process */
|
|
|
|
/* (inrease this value if you have trouble while fast capture file switches) */
|
|
|
|
if (! CreatePipe(&sync_pipe_read, &sync_pipe_write, &sa, 5120)) {
|
|
|
|
/* Couldn't create the pipe between parent and child. */
|
|
|
|
*msg = g_strdup_printf("Couldn't create sync pipe: %s", strerror(errno));
|
2007-07-21 00:31:55 +00:00
|
|
|
g_free( (gpointer) argv[0]);
|
2007-06-11 03:58:58 +00:00
|
|
|
g_free( (gpointer) argv);
|
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init STARTUPINFO */
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
#ifdef DEBUG_CHILD
|
|
|
|
si.dwFlags = STARTF_USESHOWWINDOW;
|
|
|
|
si.wShowWindow = SW_SHOW;
|
|
|
|
#else
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
|
|
|
|
si.wShowWindow = SW_HIDE; /* this hides the console window */
|
|
|
|
si.hStdInput = NULL;
|
|
|
|
si.hStdOutput = sync_pipe_write;
|
|
|
|
si.hStdError = sync_pipe_write;
|
|
|
|
/*si.hStdError = (HANDLE) _get_osfhandle(2);*/
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* convert args array into a single string */
|
|
|
|
/* XXX - could change sync_pipe_add_arg() instead */
|
|
|
|
/* there is a drawback here: the length is internally limited to 1024 bytes */
|
|
|
|
for(i=0; argv[i] != 0; i++) {
|
|
|
|
if(i != 0) g_string_append_c(args, ' '); /* don't prepend a space before the path!!! */
|
|
|
|
quoted_arg = protect_arg(argv[i]);
|
|
|
|
g_string_append(args, quoted_arg);
|
|
|
|
g_free(quoted_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call dumpcap */
|
|
|
|
if(!CreateProcess(NULL, utf_8to16(args->str), NULL, NULL, TRUE,
|
|
|
|
CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
|
|
|
|
*msg = g_strdup_printf("Couldn't run %s in child process: error %u",
|
|
|
|
args->str, GetLastError());
|
|
|
|
CloseHandle(sync_pipe_read);
|
|
|
|
CloseHandle(sync_pipe_write);
|
2007-07-21 00:31:55 +00:00
|
|
|
g_free( (gpointer) argv[0]);
|
2007-06-11 03:58:58 +00:00
|
|
|
g_free( (gpointer) argv);
|
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
2007-08-02 21:45:27 +00:00
|
|
|
*fork_child = (int) pi.hProcess;
|
2007-06-11 03:58:58 +00:00
|
|
|
g_string_free(args, TRUE);
|
|
|
|
|
|
|
|
/* associate the operating system filehandle to a C run-time file handle */
|
|
|
|
/* (good file handle infos at: http://www.flounder.com/handles.htm) */
|
2007-08-02 21:45:27 +00:00
|
|
|
*read_fd = _open_osfhandle( (long) sync_pipe_read, _O_BINARY);
|
2007-06-11 03:58:58 +00:00
|
|
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
if (pipe(sync_pipe) < 0) {
|
|
|
|
/* Couldn't create the pipe between parent and child. */
|
|
|
|
*msg = g_strdup_printf("Couldn't create sync pipe: %s", strerror(errno));
|
2007-07-21 00:31:55 +00:00
|
|
|
g_free( (gpointer) argv[0]);
|
2007-06-11 03:58:58 +00:00
|
|
|
g_free(argv);
|
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
|
2007-08-02 21:45:27 +00:00
|
|
|
if ((*fork_child = fork()) == 0) {
|
2007-06-11 03:58:58 +00:00
|
|
|
/*
|
|
|
|
* Child process - run dumpcap with the right arguments to make
|
|
|
|
* it just capture with the specified capture parameters
|
|
|
|
*/
|
|
|
|
eth_close(1);
|
|
|
|
dup(sync_pipe[PIPE_WRITE]);
|
|
|
|
eth_close(sync_pipe[PIPE_READ]);
|
2007-07-20 21:43:07 +00:00
|
|
|
execv(argv[0], (gpointer)argv);
|
2007-06-11 03:58:58 +00:00
|
|
|
*msg = g_strdup_printf("Couldn't run %s in child process: %s",
|
2007-07-20 21:43:07 +00:00
|
|
|
argv[0], strerror(errno));
|
2007-06-11 03:58:58 +00:00
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
|
2007-08-02 21:45:27 +00:00
|
|
|
*read_fd = sync_pipe[PIPE_READ];
|
2007-06-11 03:58:58 +00:00
|
|
|
#endif
|
|
|
|
|
2007-07-21 00:31:55 +00:00
|
|
|
g_free( (gpointer) argv[0]); /* exename */
|
2007-06-11 03:58:58 +00:00
|
|
|
|
|
|
|
/* Parent process - read messages from the child process over the
|
|
|
|
sync pipe. */
|
|
|
|
g_free( (gpointer) argv); /* free up arg array */
|
|
|
|
|
|
|
|
/* Close the write side of the pipe, so that only the child has it
|
|
|
|
open, and thus it completely closes, and thus returns to us
|
|
|
|
an EOF indication, if the child closes it (either deliberately
|
|
|
|
or by exiting abnormally). */
|
|
|
|
#ifdef _WIN32
|
|
|
|
CloseHandle(sync_pipe_write);
|
|
|
|
#else
|
|
|
|
eth_close(sync_pipe[PIPE_WRITE]);
|
|
|
|
#endif
|
|
|
|
|
2007-08-02 21:45:27 +00:00
|
|
|
if (*fork_child == -1) {
|
2007-06-11 03:58:58 +00:00
|
|
|
/* We couldn't even create the child process. */
|
|
|
|
*msg = g_strdup_printf("Couldn't create child process: %s", strerror(errno));
|
2007-08-02 21:45:27 +00:00
|
|
|
eth_close(*read_fd);
|
2007-06-11 03:58:58 +00:00
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we might wait for a moment till child is ready, so update screen now */
|
|
|
|
main_window_update();
|
2007-08-02 21:45:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-06-11 03:58:58 +00:00
|
|
|
|
2007-08-02 21:45:27 +00:00
|
|
|
static int
|
|
|
|
#ifdef _WIN32
|
|
|
|
sync_pipe_close_command(int *read_fd, int *fork_child, gchar **msg) {
|
|
|
|
#else
|
|
|
|
sync_pipe_close_command(int *read_fd, gchar **msg) {
|
|
|
|
#endif
|
|
|
|
int fork_child_status;
|
2007-06-11 03:58:58 +00:00
|
|
|
|
2007-08-02 21:45:27 +00:00
|
|
|
eth_close(*read_fd);
|
2007-06-11 03:58:58 +00:00
|
|
|
|
2007-11-20 20:25:07 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_close_command: wait till child closed");
|
2007-06-11 03:58:58 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2007-08-02 21:45:27 +00:00
|
|
|
/* XXX - Should we signal the child somehow? */
|
|
|
|
sync_pipe_kill(*fork_child);
|
|
|
|
if (_cwait(&fork_child_status, *fork_child, _WAIT_CHILD) == -1) {
|
2007-06-11 03:58:58 +00:00
|
|
|
*msg = g_strdup_printf("Child capture process stopped unexpectedly "
|
|
|
|
"(errno:%u)", errno);
|
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (wait(&fork_child_status) != -1) {
|
|
|
|
if (WIFEXITED(fork_child_status)) {
|
|
|
|
/* The child exited. */
|
|
|
|
fork_child_status = WEXITSTATUS(fork_child_status);
|
|
|
|
} else {
|
|
|
|
if (WIFSTOPPED(fork_child_status)) {
|
|
|
|
/* It stopped, rather than exiting. "Should not happen." */
|
|
|
|
*msg = g_strdup_printf("Child capture process stopped: %s",
|
|
|
|
sync_pipe_signame(WSTOPSIG(fork_child_status)));
|
|
|
|
} else if (WIFSIGNALED(fork_child_status)) {
|
|
|
|
/* It died with a signal. */
|
|
|
|
*msg = g_strdup_printf("Child capture process died: %s%s",
|
|
|
|
sync_pipe_signame(WTERMSIG(fork_child_status)),
|
|
|
|
WCOREDUMP(fork_child_status) ? " - core dumped" : "");
|
|
|
|
} else {
|
|
|
|
/* What? It had to either have exited, or stopped, or died with
|
|
|
|
a signal; what happened here? */
|
|
|
|
*msg = g_strdup_printf("Child capture process died: wait status %#o",
|
|
|
|
fork_child_status);
|
|
|
|
}
|
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*msg = g_strdup_printf("Child capture process stopped unexpectedly "
|
|
|
|
"(errno:%u)", errno);
|
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
#endif
|
2007-08-02 21:45:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run dumpcap with the supplied arguments. On success, msg points to
|
|
|
|
* a buffer containing the dumpcap output and returns 0. On failure, msg
|
|
|
|
* points to the error message returned by dumpcap, and returns dumpcap's
|
|
|
|
* exit value. In either case, msg must be freed with g_free().
|
|
|
|
*/
|
|
|
|
/* XXX - This duplicates a lot of code in sync_pipe_start() */
|
|
|
|
#define PIPE_BUF_SIZE 5120
|
|
|
|
static int
|
|
|
|
sync_pipe_run_command(const char** argv, gchar **msg) {
|
|
|
|
int sync_pipe_read_fd, fork_child, ret;
|
|
|
|
gchar buf[PIPE_BUF_SIZE+1];
|
|
|
|
GString *msg_buf = NULL;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
ret = sync_pipe_open_command(argv, &sync_pipe_read_fd, &fork_child, msg);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* We were able to set up to read dumpcap's output. Do so and
|
|
|
|
return its exit value. */
|
|
|
|
msg_buf = g_string_new("");
|
|
|
|
while ((count = eth_read(sync_pipe_read_fd, buf, PIPE_BUF_SIZE)) > 0) {
|
|
|
|
buf[count] = '\0';
|
|
|
|
g_string_append(msg_buf, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
ret = sync_pipe_close_command(&sync_pipe_read_fd, &fork_child, msg);
|
|
|
|
#else
|
|
|
|
ret = sync_pipe_close_command(&sync_pipe_read_fd, msg);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
g_string_free(msg_buf, TRUE);
|
|
|
|
return ret;
|
|
|
|
}
|
2007-06-11 03:58:58 +00:00
|
|
|
|
|
|
|
*msg = msg_buf->str;
|
|
|
|
g_string_free(msg_buf, FALSE);
|
2007-08-02 21:45:27 +00:00
|
|
|
return 0;
|
2007-06-11 03:58:58 +00:00
|
|
|
}
|
2006-03-24 02:23:48 +00:00
|
|
|
|
2007-07-20 21:43:07 +00:00
|
|
|
/*
|
|
|
|
* Get an interface list using dumpcap. On success, msg points to
|
|
|
|
* a buffer containing the dumpcap output and returns 0. On failure, msg
|
|
|
|
* points to the error message returned by dumpcap, and returns dumpcap's
|
|
|
|
* exit value. In either case, msg must be freed with g_free().
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sync_interface_list_open(gchar **msg) {
|
|
|
|
int argc;
|
|
|
|
const char **argv;
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
/* We can't return anything */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_list_open");
|
|
|
|
|
|
|
|
argv = init_pipe_args(&argc);
|
|
|
|
|
|
|
|
if (!argv) {
|
|
|
|
*msg = g_strdup_printf("We don't know where to find dumpcap.");
|
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ask for the interface list */
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-D");
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-M");
|
|
|
|
|
|
|
|
/* dumpcap should be running in capture child mode (hidden feature) */
|
|
|
|
#ifndef DEBUG_CHILD
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-Z");
|
2007-10-26 16:32:28 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
|
2007-07-20 21:43:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return sync_pipe_run_command(argv, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get an linktype list using dumpcap. On success, msg points to
|
|
|
|
* a buffer containing the dumpcap output and returns 0. On failure, msg
|
|
|
|
* points to the error message returned by dumpcap, and returns dumpcap's
|
|
|
|
* exit value. In either case, msg must be freed with g_free().
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sync_linktype_list_open(gchar *ifname, gchar **msg) {
|
|
|
|
int argc;
|
|
|
|
const char **argv;
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
/* We can't return anything */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_linktype_list_open");
|
|
|
|
|
|
|
|
argv = init_pipe_args(&argc);
|
|
|
|
|
|
|
|
if (!argv) {
|
|
|
|
*msg = g_strdup_printf("We don't know where to find dumpcap.");
|
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ask for the linktype list */
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-i");
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, ifname);
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-L");
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-M");
|
|
|
|
|
|
|
|
/* dumpcap should be running in capture child mode (hidden feature) */
|
|
|
|
#ifndef DEBUG_CHILD
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-Z");
|
2007-10-26 16:32:28 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
|
2007-07-20 21:43:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return sync_pipe_run_command(argv, msg);
|
|
|
|
}
|
|
|
|
|
2007-08-02 21:45:27 +00:00
|
|
|
/*
|
|
|
|
* Start getting interface statistics using dumpcap. On success, read_fd
|
|
|
|
* contains the file descriptor for the pipe's stdout, msg is unchanged,
|
|
|
|
* and zero is returned. On failure, msg will point to an error message
|
|
|
|
* that must be g_free()d and a nonzero error value will be returned.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sync_interface_stats_open(int *read_fd, int *fork_child, gchar **msg) {
|
|
|
|
int argc;
|
|
|
|
const char **argv;
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
/* We can't return anything */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_linktype_list_open");
|
|
|
|
|
|
|
|
argv = init_pipe_args(&argc);
|
|
|
|
|
|
|
|
if (!argv) {
|
|
|
|
*msg = g_strdup_printf("We don't know where to find dumpcap.");
|
|
|
|
return CANT_RUN_DUMPCAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ask for the linktype list */
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-S");
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-M");
|
|
|
|
|
|
|
|
/* dumpcap should be running in capture child mode (hidden feature) */
|
|
|
|
#ifndef DEBUG_CHILD
|
|
|
|
argv = sync_pipe_add_arg(argv, &argc, "-Z");
|
2007-10-26 16:32:28 +00:00
|
|
|
argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
|
2007-08-02 21:45:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return sync_pipe_open_command(argv, read_fd, fork_child, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close down the stats process */
|
|
|
|
int
|
2007-09-29 01:14:16 +00:00
|
|
|
sync_interface_stats_close(int *read_fd, int *fork_child
|
|
|
|
#ifndef _WIN32
|
|
|
|
_U_
|
|
|
|
#endif
|
|
|
|
, gchar **msg) {
|
2007-08-02 21:45:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return sync_pipe_close_command(read_fd, fork_child, msg);
|
|
|
|
#else
|
|
|
|
return sync_pipe_close_command(read_fd, msg);
|
|
|
|
#endif
|
|
|
|
}
|
2006-03-24 02:23:48 +00:00
|
|
|
|
|
|
|
/* read a number of bytes from a pipe */
|
|
|
|
/* (blocks until enough bytes read or an error occurs) */
|
|
|
|
static int
|
|
|
|
pipe_read_bytes(int pipe, char *bytes, int required) {
|
|
|
|
int newly;
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
while(required) {
|
|
|
|
newly = read(pipe, &bytes[offset], required);
|
|
|
|
if (newly == 0) {
|
|
|
|
/* EOF */
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
|
|
|
|
"read from pipe %d: EOF (capture closed?)", pipe);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
if (newly < 0) {
|
|
|
|
/* error */
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
|
|
|
|
"read from pipe %d: error(%u): %s", pipe, errno, strerror(errno));
|
|
|
|
return newly;
|
|
|
|
}
|
|
|
|
|
|
|
|
required -= newly;
|
|
|
|
offset += newly;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2007-08-02 21:45:27 +00:00
|
|
|
static gboolean pipe_data_available(int pipe) {
|
|
|
|
#ifdef _WIN32 /* PeekNamedPipe */
|
|
|
|
HANDLE hPipe = (HANDLE) _get_osfhandle(pipe);
|
|
|
|
DWORD bytes_avail;
|
|
|
|
|
|
|
|
if (hPipe == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (! PeekNamedPipe(hPipe, NULL, 0, NULL, &bytes_avail, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (bytes_avail > 0)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
#else /* select */
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(pipe, &rfds);
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
if (select(pipe+1, &rfds, NULL, NULL, &timeout) > 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read a line from a pipe, similar to fgets */
|
|
|
|
int
|
|
|
|
sync_pipe_gets_nonblock(int pipe, char *bytes, int max) {
|
|
|
|
int newly;
|
|
|
|
int offset = -1;
|
|
|
|
|
|
|
|
while(offset < max - 1) {
|
|
|
|
offset++;
|
|
|
|
if (! pipe_data_available(pipe))
|
|
|
|
break;
|
|
|
|
newly = read(pipe, &bytes[offset], 1);
|
|
|
|
if (newly == 0) {
|
|
|
|
/* EOF - not necessarily an error */
|
|
|
|
break;
|
|
|
|
} else if (newly < 0) {
|
|
|
|
/* error */
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
|
|
|
|
"read from pipe %d: error(%u): %s", pipe, errno, strerror(errno));
|
|
|
|
return newly;
|
|
|
|
} else if (bytes[offset] == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset >= 0)
|
|
|
|
bytes[offset] = '\0';
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-24 02:23:48 +00:00
|
|
|
/* convert header values (indicator and 4-byte length) */
|
|
|
|
static void
|
2007-06-11 03:58:58 +00:00
|
|
|
pipe_convert_header(const guchar *header, int header_len, char *indicator, int *block_len) {
|
2006-03-24 02:23:48 +00:00
|
|
|
|
|
|
|
g_assert(header_len == 4);
|
|
|
|
|
|
|
|
/* convert header values */
|
|
|
|
*indicator = header[0];
|
|
|
|
*block_len = header[1]<<16 | header[2]<<8 | header[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read a message from the sending pipe in the standard format
|
|
|
|
(1-byte message indicator, 3-byte message length (excluding length
|
|
|
|
and indicator field), and the rest is the message) */
|
|
|
|
static int
|
|
|
|
pipe_read_block(int pipe, char *indicator, int len, char *msg) {
|
|
|
|
int required;
|
|
|
|
int newly;
|
|
|
|
guchar header[4];
|
|
|
|
|
|
|
|
|
|
|
|
/* read header (indicator and 3-byte length) */
|
2007-03-28 21:55:11 +00:00
|
|
|
newly = pipe_read_bytes(pipe, header, 4);
|
2006-03-24 02:23:48 +00:00
|
|
|
if(newly != 4) {
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
|
|
|
|
"read %d failed to read header: %u", pipe, newly);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert header values */
|
|
|
|
pipe_convert_header(header, 4, indicator, &required);
|
|
|
|
|
|
|
|
/* only indicator with no value? */
|
|
|
|
if(required == 0) {
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
|
|
|
|
"read %d indicator: %c empty value", pipe, *indicator);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* does the data fit into the given buffer? */
|
|
|
|
if(required > len) {
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
|
|
|
|
"read %d length error, required %d > len %d, indicator: %u",
|
|
|
|
pipe, required, len, *indicator);
|
|
|
|
|
|
|
|
/* we have a problem here, try to read some more bytes from the pipe to debug where the problem really is */
|
|
|
|
memcpy(msg, header, sizeof(header));
|
|
|
|
newly = read(pipe, &msg[sizeof(header)], len-sizeof(header));
|
|
|
|
g_warning("Unknown message from dumpcap, try to show it as a string: %s", msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
len = required;
|
|
|
|
|
|
|
|
/* read the actual block data */
|
|
|
|
newly = pipe_read_bytes(pipe, msg, required);
|
|
|
|
if(newly != required) {
|
|
|
|
g_warning("Unknown message from dumpcap, try to show it as a string: %s", msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
|
|
|
|
"read %d ok indicator: %c len: %u msg: %s", pipe, *indicator,
|
|
|
|
len, msg);
|
|
|
|
return newly + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-30 11:44:06 +00:00
|
|
|
/* There's stuff to read from the sync pipe, meaning the child has sent
|
|
|
|
us a message, or the sync pipe has closed, meaning the child has
|
|
|
|
closed it (perhaps because it exited). */
|
2006-03-09 14:39:44 +00:00
|
|
|
static gboolean
|
2004-10-30 11:44:06 +00:00
|
|
|
sync_pipe_input_cb(gint source, gpointer user_data)
|
|
|
|
{
|
2005-02-04 01:29:29 +00:00
|
|
|
capture_options *capture_opts = (capture_options *)user_data;
|
2005-11-18 00:34:11 +00:00
|
|
|
char buffer[SP_MAX_MSG_LEN+1];
|
2005-04-12 00:54:52 +00:00
|
|
|
int nread;
|
|
|
|
char indicator;
|
2006-03-13 00:30:51 +00:00
|
|
|
int primary_len;
|
|
|
|
char * primary_msg;
|
|
|
|
int secondary_len;
|
|
|
|
char * secondary_msg;
|
2004-10-30 11:44:06 +00:00
|
|
|
|
|
|
|
|
2005-11-18 00:34:11 +00:00
|
|
|
nread = pipe_read_block(source, &indicator, SP_MAX_MSG_LEN, buffer);
|
2005-04-12 00:54:52 +00:00
|
|
|
if(nread <= 0) {
|
2006-02-12 20:41:28 +00:00
|
|
|
if (nread == 0)
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
|
|
|
|
"sync_pipe_input_cb: child has closed sync_pipe");
|
|
|
|
else
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
|
|
|
|
"sync_pipe_input_cb: error reading from sync pipe");
|
2005-05-26 17:50:27 +00:00
|
|
|
|
2004-10-30 11:44:06 +00:00
|
|
|
/* The child has closed the sync pipe, meaning it's not going to be
|
|
|
|
capturing any more packets. Pick up its exit status, and
|
2006-02-12 20:41:28 +00:00
|
|
|
complain if it did anything other than exit with status 0.
|
|
|
|
|
2007-06-11 03:58:58 +00:00
|
|
|
We don't have to worry about killing the child, if the sync pipe
|
|
|
|
returned an error. Usually this error is caused as the child killed itself
|
|
|
|
while going down. Even in the rare cases that this isn't the case,
|
|
|
|
the child will get an error when writing to the broken pipe the next time,
|
2006-03-24 02:23:48 +00:00
|
|
|
cleaning itself up then. */
|
2005-05-26 19:06:01 +00:00
|
|
|
sync_pipe_wait_for_child(capture_opts);
|
2005-05-26 17:50:27 +00:00
|
|
|
|
2005-04-10 16:43:22 +00:00
|
|
|
#ifdef _WIN32
|
2006-02-11 23:25:11 +00:00
|
|
|
eth_close(capture_opts->signal_pipe_write_fd);
|
2005-04-10 16:43:22 +00:00
|
|
|
#endif
|
2005-03-28 14:39:31 +00:00
|
|
|
capture_input_closed(capture_opts);
|
2004-10-30 11:44:06 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-03-24 02:23:48 +00:00
|
|
|
/* we got a valid message block from the child, process it */
|
2005-04-12 00:54:52 +00:00
|
|
|
switch(indicator) {
|
2005-04-12 21:44:55 +00:00
|
|
|
case SP_FILE:
|
2006-03-05 03:14:16 +00:00
|
|
|
if(!capture_input_new_file(capture_opts, buffer)) {
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: file failed, closing capture");
|
|
|
|
|
|
|
|
/* We weren't able to open the new capture file; user has been
|
|
|
|
alerted. Close the sync pipe. */
|
|
|
|
eth_close(source);
|
|
|
|
|
|
|
|
/* the child has send us a filename which we couldn't open.
|
|
|
|
this probably means, the child is creating files faster than we can handle it.
|
|
|
|
this should only be the case for very fast file switches
|
|
|
|
we can't do much more than telling the child to stop
|
|
|
|
(this is the "emergency brake" if user e.g. wants to switch files every second) */
|
|
|
|
sync_pipe_stop(capture_opts);
|
|
|
|
}
|
|
|
|
break;
|
2005-04-12 00:54:52 +00:00
|
|
|
case SP_PACKET_COUNT:
|
|
|
|
nread = atoi(buffer);
|
2005-05-26 17:50:27 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: new packets %u", nread);
|
2005-04-12 00:54:52 +00:00
|
|
|
capture_input_new_packets(capture_opts, nread);
|
|
|
|
break;
|
2005-04-12 21:44:55 +00:00
|
|
|
case SP_ERROR_MSG:
|
2006-03-13 00:30:51 +00:00
|
|
|
/* convert primary message */
|
2007-03-28 21:55:11 +00:00
|
|
|
pipe_convert_header(buffer, 4, &indicator, &primary_len);
|
2006-03-13 00:30:51 +00:00
|
|
|
primary_msg = buffer+4;
|
|
|
|
/* convert secondary message */
|
2007-03-28 21:55:11 +00:00
|
|
|
pipe_convert_header(primary_msg + primary_len, 4, &indicator, &secondary_len);
|
2006-03-13 00:30:51 +00:00
|
|
|
secondary_msg = primary_msg + primary_len + 4;
|
|
|
|
/* message output */
|
|
|
|
capture_input_error_message(capture_opts, primary_msg, secondary_msg);
|
2005-05-26 17:50:27 +00:00
|
|
|
/* the capture child will close the sync_pipe, nothing to do for now */
|
2006-03-13 00:30:51 +00:00
|
|
|
/* (an error message doesn't mean we have to stop capturing) */
|
2005-04-12 21:44:55 +00:00
|
|
|
break;
|
2006-03-05 03:14:16 +00:00
|
|
|
case SP_BAD_FILTER:
|
|
|
|
capture_input_cfilter_error_message(capture_opts, buffer);
|
|
|
|
/* the capture child will close the sync_pipe, nothing to do for now */
|
|
|
|
break;
|
2005-04-12 00:54:52 +00:00
|
|
|
case SP_DROPS:
|
2005-12-03 18:15:02 +00:00
|
|
|
capture_input_drops(capture_opts, atoi(buffer));
|
2005-04-12 00:54:52 +00:00
|
|
|
break;
|
|
|
|
default:
|
2006-03-05 03:14:16 +00:00
|
|
|
g_assert_not_reached();
|
2004-10-30 11:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-12-29 09:09:35 +00:00
|
|
|
|
2005-04-12 00:54:52 +00:00
|
|
|
|
2004-12-29 09:09:35 +00:00
|
|
|
/* the child process is going down, wait until it's completely terminated */
|
2004-10-30 11:44:06 +00:00
|
|
|
static void
|
2005-05-26 19:06:01 +00:00
|
|
|
sync_pipe_wait_for_child(capture_options *capture_opts)
|
2004-10-30 11:44:06 +00:00
|
|
|
{
|
|
|
|
int wstatus;
|
|
|
|
|
2005-02-23 22:04:31 +00:00
|
|
|
|
2005-05-26 17:50:27 +00:00
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: wait till child closed");
|
2005-02-23 22:04:31 +00:00
|
|
|
g_assert(capture_opts->fork_child != -1);
|
|
|
|
|
2004-10-30 11:44:06 +00:00
|
|
|
#ifdef _WIN32
|
2005-02-23 22:04:31 +00:00
|
|
|
if (_cwait(&wstatus, capture_opts->fork_child, _WAIT_CHILD) == -1) {
|
2004-10-30 11:44:06 +00:00
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
|
2006-03-24 02:23:48 +00:00
|
|
|
"Child capture process stopped unexpectedly (errno:%u)", errno);
|
2004-10-30 11:44:06 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (wait(&wstatus) != -1) {
|
|
|
|
if (WIFEXITED(wstatus)) {
|
2005-05-26 19:06:01 +00:00
|
|
|
/* The child exited; display its exit status, if it seems uncommon (0=ok, 1=error) */
|
|
|
|
/* the child will inform us about errors through the sync_pipe, which will popup */
|
|
|
|
/* an error message, so don't popup another one */
|
|
|
|
|
2006-03-24 02:23:48 +00:00
|
|
|
/* If there are situations where the child won't send us such an error message, */
|
2005-05-26 19:06:01 +00:00
|
|
|
/* this should be fixed in the child and not here! */
|
|
|
|
if (WEXITSTATUS(wstatus) != 0 && WEXITSTATUS(wstatus) != 1) {
|
2004-10-30 11:44:06 +00:00
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
|
|
|
|
"Child capture process exited: exit status %d",
|
|
|
|
WEXITSTATUS(wstatus));
|
|
|
|
}
|
|
|
|
} else if (WIFSTOPPED(wstatus)) {
|
|
|
|
/* It stopped, rather than exiting. "Should not happen." */
|
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
|
|
|
|
"Child capture process stopped: %s",
|
|
|
|
sync_pipe_signame(WSTOPSIG(wstatus)));
|
|
|
|
} else if (WIFSIGNALED(wstatus)) {
|
|
|
|
/* It died with a signal. */
|
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
|
|
|
|
"Child capture process died: %s%s",
|
|
|
|
sync_pipe_signame(WTERMSIG(wstatus)),
|
|
|
|
WCOREDUMP(wstatus) ? " - core dumped" : "");
|
|
|
|
} else {
|
|
|
|
/* What? It had to either have exited, or stopped, or died with
|
|
|
|
a signal; what happened here? */
|
|
|
|
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
|
|
|
|
"Child capture process died: wait status %#o", wstatus);
|
|
|
|
}
|
|
|
|
}
|
2006-03-24 02:23:48 +00:00
|
|
|
#endif
|
2004-10-30 11:44:06 +00:00
|
|
|
|
|
|
|
/* No more child process. */
|
2005-02-23 22:04:31 +00:00
|
|
|
capture_opts->fork_child = -1;
|
2005-05-26 17:50:27 +00:00
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: capture child closed");
|
2004-10-30 11:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2005-04-11 18:57:19 +00:00
|
|
|
/* convert signal to corresponding name */
|
2005-08-06 14:03:14 +00:00
|
|
|
static const char *
|
2004-10-30 11:44:06 +00:00
|
|
|
sync_pipe_signame(int sig)
|
|
|
|
{
|
2005-08-06 14:03:14 +00:00
|
|
|
const char *sigmsg;
|
2004-10-30 11:44:06 +00:00
|
|
|
static char sigmsg_buf[6+1+3+1];
|
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
|
|
|
|
case SIGHUP:
|
|
|
|
sigmsg = "Hangup";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGINT:
|
|
|
|
sigmsg = "Interrupted";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGQUIT:
|
|
|
|
sigmsg = "Quit";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGILL:
|
|
|
|
sigmsg = "Illegal instruction";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGTRAP:
|
|
|
|
sigmsg = "Trace trap";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGABRT:
|
|
|
|
sigmsg = "Abort";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGFPE:
|
|
|
|
sigmsg = "Arithmetic exception";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGKILL:
|
|
|
|
sigmsg = "Killed";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGBUS:
|
|
|
|
sigmsg = "Bus error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGSEGV:
|
|
|
|
sigmsg = "Segmentation violation";
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* http://metalab.unc.edu/pub/Linux/docs/HOWTO/GCC-HOWTO
|
|
|
|
Linux is POSIX compliant. These are not POSIX-defined signals ---
|
|
|
|
ISO/IEC 9945-1:1990 (IEEE Std 1003.1-1990), paragraph B.3.3.1.1 sez:
|
|
|
|
|
|
|
|
``The signals SIGBUS, SIGEMT, SIGIOT, SIGTRAP, and SIGSYS
|
|
|
|
were omitted from POSIX.1 because their behavior is
|
|
|
|
implementation dependent and could not be adequately catego-
|
|
|
|
rized. Conforming implementations may deliver these sig-
|
|
|
|
nals, but must document the circumstances under which they
|
|
|
|
are delivered and note any restrictions concerning their
|
|
|
|
delivery.''
|
|
|
|
|
|
|
|
So we only check for SIGSYS on those systems that happen to
|
|
|
|
implement them (a system can be POSIX-compliant and implement
|
|
|
|
them, it's just that POSIX doesn't *require* a POSIX-compliant
|
|
|
|
system to implement them).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef SIGSYS
|
|
|
|
case SIGSYS:
|
|
|
|
sigmsg = "Bad system call";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case SIGPIPE:
|
|
|
|
sigmsg = "Broken pipe";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGALRM:
|
|
|
|
sigmsg = "Alarm clock";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGTERM:
|
|
|
|
sigmsg = "Terminated";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-03-24 02:23:48 +00:00
|
|
|
/* Returning a static buffer is ok in the context we use it here */
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(sigmsg_buf, sizeof sigmsg_buf, "Signal %d", sig);
|
2004-10-30 11:44:06 +00:00
|
|
|
sigmsg = sigmsg_buf;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return sigmsg;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-03-24 02:23:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* tell the child through the signal pipe that we want to quit the capture */
|
|
|
|
static void
|
|
|
|
signal_pipe_capquit_to_child(capture_options *capture_opts)
|
|
|
|
{
|
|
|
|
const char quit_msg[] = "QUIT";
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "signal_pipe_capquit_to_child");
|
|
|
|
|
|
|
|
/* it doesn't matter *what* we send here, the first byte will stop the capture */
|
|
|
|
/* simply sending a "QUIT" string */
|
|
|
|
/*pipe_write_block(capture_opts->signal_pipe_write_fd, SP_QUIT, quit_msg);*/
|
|
|
|
ret = write(capture_opts->signal_pipe_write_fd, quit_msg, sizeof quit_msg);
|
|
|
|
if(ret == -1) {
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
|
|
|
|
"signal_pipe_capquit_to_child: %d header: error %s", capture_opts->signal_pipe_write_fd, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-04-11 18:57:19 +00:00
|
|
|
/* user wants to stop the capture run */
|
2004-10-30 11:44:06 +00:00
|
|
|
void
|
2005-02-04 01:29:29 +00:00
|
|
|
sync_pipe_stop(capture_options *capture_opts)
|
2004-10-30 11:44:06 +00:00
|
|
|
{
|
2007-11-25 21:33:34 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
int count;
|
|
|
|
DWORD childstatus;
|
|
|
|
gboolean terminate = TRUE;
|
|
|
|
#endif
|
|
|
|
|
2006-03-24 02:23:48 +00:00
|
|
|
if (capture_opts->fork_child != -1) {
|
2004-10-30 11:44:06 +00:00
|
|
|
#ifndef _WIN32
|
2005-04-11 18:57:19 +00:00
|
|
|
/* send the SIGUSR1 signal to close the capture child gracefully. */
|
2005-04-10 16:43:22 +00:00
|
|
|
kill(capture_opts->fork_child, SIGUSR1);
|
2004-10-30 11:44:06 +00:00
|
|
|
#else
|
2007-11-25 21:33:34 +00:00
|
|
|
#define STOP_SLEEP_TIME 500 /* ms */
|
|
|
|
#define STOP_CHECK_TIME 50
|
|
|
|
/* First, use the special signal pipe to try to close the capture child
|
|
|
|
* gracefully.
|
|
|
|
*/
|
2005-04-12 21:44:55 +00:00
|
|
|
signal_pipe_capquit_to_child(capture_opts);
|
2007-11-25 21:33:34 +00:00
|
|
|
|
|
|
|
/* Next, wait for the process to exit on its own */
|
|
|
|
for (count = 0; count < STOP_SLEEP_TIME / STOP_CHECK_TIME; count++) {
|
|
|
|
if (GetExitCodeProcess((HANDLE) capture_opts->fork_child, &childstatus) &&
|
|
|
|
childstatus != STILL_ACTIVE) {
|
|
|
|
terminate = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Sleep(STOP_CHECK_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Force the issue. */
|
|
|
|
if (terminate) {
|
|
|
|
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
|
|
|
|
"sync_pipe_stop: forcing child to exit");
|
|
|
|
sync_pipe_kill(capture_opts->fork_child);
|
|
|
|
}
|
2004-10-30 11:44:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-28 20:28:20 +00:00
|
|
|
/* Wireshark has to exit, force the capture child to close */
|
2004-10-30 11:44:06 +00:00
|
|
|
void
|
2007-06-11 03:58:58 +00:00
|
|
|
sync_pipe_kill(int fork_child)
|
2004-10-30 11:44:06 +00:00
|
|
|
{
|
2007-06-11 03:58:58 +00:00
|
|
|
if (fork_child != -1) {
|
2004-10-30 11:44:06 +00:00
|
|
|
#ifndef _WIN32
|
2007-06-11 03:58:58 +00:00
|
|
|
kill(fork_child, SIGTERM); /* SIGTERM so it can clean up if necessary */
|
2004-10-30 11:44:06 +00:00
|
|
|
#else
|
2006-03-24 02:23:48 +00:00
|
|
|
/* Remark: This is not the preferred method of closing a process!
|
2004-10-30 11:44:06 +00:00
|
|
|
* the clean way would be getting the process id of the child process,
|
|
|
|
* then getting window handle hWnd of that process (using EnumChildWindows),
|
2006-03-09 14:39:44 +00:00
|
|
|
* and then do a SendMessage(hWnd, WM_CLOSE, 0, 0)
|
2004-10-30 11:44:06 +00:00
|
|
|
*
|
2005-02-07 03:13:51 +00:00
|
|
|
* Unfortunately, I don't know how to get the process id from the
|
|
|
|
* handle. OpenProcess will get an handle (not a window handle)
|
|
|
|
* from the process ID; it will not get a window handle from the
|
|
|
|
* process ID. (How could it? A process can have more than one
|
2006-03-24 05:05:14 +00:00
|
|
|
* window. For that matter, a process might have *no* windows,
|
|
|
|
* as a process running dumpcap, the normal child process program,
|
|
|
|
* probably does.)
|
2004-10-30 11:44:06 +00:00
|
|
|
*
|
2006-03-09 14:39:44 +00:00
|
|
|
* Hint: GenerateConsoleCtrlEvent() will only work if both processes are
|
2005-02-07 03:13:51 +00:00
|
|
|
* running in the same console; that's not necessarily the case for
|
|
|
|
* us, as we might not be running in a console.
|
|
|
|
* And this also will require to have the process id.
|
2004-10-30 11:44:06 +00:00
|
|
|
*/
|
2007-06-11 03:58:58 +00:00
|
|
|
TerminateProcess((HANDLE) (fork_child), 0);
|
2004-10-30 11:44:06 +00:00
|
|
|
#endif
|
2005-02-07 03:13:51 +00:00
|
|
|
}
|
2004-10-30 11:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LIBPCAP */
|