Add a new global flag "capture_child", which is TRUE if we're a child

process for a sync mode or fork mode capture.

Have that flag control whether we do things that *only* the parent or
*only* the child should do, rather than basing it solely on the setting
of "sync_mode" or "fork_mode" (or, in the case of stuff done in the
child process either in sync mode or fork mode, rather than basing it on
the setting of those flags at all).

Split "do_capture()" into a "run_capture()" routine that starts a
capture (possibly by forking off and execing a child process, if we're
supposed to do sync mode or fork mode captures), and that assumes the
file to which the capture is to write has already been opened and that
"cf.save_file_fd" is the file descriptor for that file, and a
"do_capture()" routine that creates a temporary file, getting an FD for
it, and calls "run_capture()".

Use "run_capture()", rather than "capture()", for "-k" captures, so that
it'll do the capture in a child process if "-S" or "-F" was specified
("do_capture()" won't do because "-k" captures should write to the file
specified by the "-w" flag, not some random temporary file).

For child process captures, however, just use "capture()" - the child
process shouldn't itself fork off a child if we're in sync or fork mode,
and should just write to the file whose file descriptor was specified by
the "-W" flag on the command line.

All this allows you to do "ethereal -S -w <file> -i <interface> -k" to
start a sync mode capture from the command line.

svn path=/trunk/; revision=740
This commit is contained in:
Guy Harris 1999-09-30 06:50:01 +00:00
parent 062cb007f1
commit bab015f5e5
4 changed files with 92 additions and 51 deletions

View File

@ -1,7 +1,7 @@
/* capture.c
* Routines for packet capture windows
*
* $Id: capture.c,v 1.73 1999/09/30 06:11:43 guy Exp $
* $Id: capture.c,v 1.74 1999/09/30 06:49:53 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -97,14 +97,11 @@ typedef struct _loop_data {
wtap_dumper *pdh;
} loop_data;
/* Create a temporary file and start a capture to it. */
void
do_capture(void)
{
char tmpname[128+1];
u_char c;
int i;
guint byte_count;
char *msg;
/* Choose a random name for the capture buffer */
cf.save_file_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
@ -123,7 +120,21 @@ do_capture(void)
}
cf.save_file = g_strdup(tmpname);
cf.user_saved = 0;
run_capture();
}
/* Start a capture to a file we've opened; "cf.save_file" is the
pathname of the file, and "cf.save_file_fd" is the file descriptor
we got when we opened it. */
void
run_capture(void)
{
u_char c;
int i;
guint byte_count;
char *msg;
if (sync_mode || fork_mode) { /* use fork() for capture */
int fork_child;
char ssnap[24];
@ -234,6 +245,7 @@ do_capture(void)
capture();
}
/* Do the low-level work of a capture. */
void
capture(void)
{
@ -265,11 +277,12 @@ capture(void)
pch = pcap_open_live(cf.iface, cf.snap, 1, 250, err_str);
if (pch == NULL) {
/* Well, we couldn't start the capture. */
if (!sync_mode && !fork_mode) {
/* In fork mode, we shouldn't do any UI stuff until we pop up the
capture-progress window, and, since we couldn't start the
capture, we haven't popped it up. */
/* Well, we couldn't start the capture.
If this is a child process that does the capturing in sync
mode or fork mode, it shouldn't do any UI stuff until we pop up the
capture-progress window, and, since we couldn't start the
capture, we haven't popped it up. */
if (!capture_child) {
while (gtk_events_pending()) gtk_main_iteration();
}
snprintf(errmsg, sizeof errmsg,
@ -337,14 +350,15 @@ capture(void)
goto error;
}
if (sync_mode) {
if (capture_child && sync_mode) {
/* Well, we should be able to start capturing.
Sync out the capture file, so the header makes it to the file
system, and send a "capture started successfully and capture
file created" message to our parent so that they'll open the
capture file and update its windows to indicate that we have
a live capture in progress. */
This is the child process for a sync mode capture, so sync out
the capture file, so the header makes it to the file system,
and send a "capture started successfully and capture file created"
message to our parent so that they'll open the capture file and
update its windows to indicate that we have a live capture in
progress. */
fflush(wtap_dump_file(ld.pdh));
write(1, "0;", 2);
}
@ -447,7 +461,10 @@ capture(void)
/* do sync here, too */
fflush(wtap_dump_file(ld.pdh));
if (sync_mode && ld.sync_packets) {
if (capture_child && sync_mode && ld.sync_packets) {
/* This is the child process for a sync mode capture, so send
our parent a message saying we've written out "ld.sync_packets"
packets to the capture file. */
char tmp[20];
sprintf(tmp, "%d*", ld.sync_packets);
write(1, tmp, strlen(tmp));
@ -515,8 +532,9 @@ error:
/* We couldn't even start the capture, so get rid of the capture
file. */
unlink(cf.save_file); /* silently ignore error */
if (sync_mode) {
/* Send the error message to our parent, so they can display a
if (capture_child && sync_mode) {
/* This is the child process for a sync mode capture.
Send the error message to our parent, so they can display a
dialog box containing it. */
int msglen = strlen(errmsg);
char lenbuf[10+1+1];

View File

@ -1,7 +1,7 @@
/* capture.h
* Definitions for packet capture windows
*
* $Id: capture.h,v 1.16 1999/09/30 06:11:44 guy Exp $
* $Id: capture.h,v 1.17 1999/09/30 06:49:54 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -51,7 +51,15 @@
/* Name we give to the child process when doing a "-S" or "-F" capture. */
#define CHILD_NAME "ethereal-capture"
/* Create a temporary file and start a capture to it. */
void do_capture(void);
/* Start a capture to a file we've opened; "cf.save_file" is the
pathname of the file, and "cf.save_file_fd" is the file descriptor
we got when we opened it. */
void run_capture(void);
/* Do the low-level work of a capture. */
void capture(void);
#endif /* HAVE_LIBPCAP */

View File

@ -1,7 +1,7 @@
/* globals.h
* Global defines, etc.
*
* $Id: globals.h,v 1.8 1999/09/29 22:19:13 guy Exp $
* $Id: globals.h,v 1.9 1999/09/30 06:49:54 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -89,7 +89,6 @@ extern GtkWidget *file_sel, *packet_list, *tree_view, *byte_view, *prog_bar,
*info_bar;
extern GdkFont *m_r_font, *m_b_font;
extern guint main_ctx, file_ctx;
extern gint start_capture;
extern gchar comp_info_str[256];
extern gchar *ethereal_path;
extern gchar *medium_font;
@ -107,6 +106,7 @@ extern int sync_mode; /* allow sync */
extern int sync_pipe[2]; /* used to sync father */
extern int fork_mode; /* fork a child to do the capture */
extern int quit_after_cap; /* Makes a "capture only mode". Implies -k */
extern gboolean capture_child; /* if this is the child for "-F"/"-S" */
#endif
#define PF_DIR ".ethereal"

View File

@ -1,6 +1,6 @@
/* main.c
*
* $Id: main.c,v 1.10 1999/09/30 06:11:50 guy Exp $
* $Id: main.c,v 1.11 1999/09/30 06:50:01 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -103,7 +103,6 @@ GtkWidget *file_sel, *packet_list, *tree_view, *byte_view, *prog_bar,
*info_bar;
GdkFont *m_r_font, *m_b_font;
guint main_ctx, file_ctx;
gint start_capture = 0;
gchar comp_info_str[256];
gchar *ethereal_path = NULL;
gchar *medium_font = MONO_MEDIUM_FONT;
@ -119,6 +118,7 @@ int sync_mode; /* fork a child to do the capture, and sync between them */
int sync_pipe[2]; /* used to sync father */
int fork_mode; /* fork a child to do the capture */
int quit_after_cap; /* Makes a "capture only mode". Implies -k */
gboolean capture_child; /* if this is the child for "-F"/"-S" */
#endif
/* Specifies byte offsets for object selected in tree */
@ -462,7 +462,7 @@ print_usage(void) {
int
main(int argc, char *argv[])
{
char *command_name, *s;
char *command_name, *s;
int i;
#ifndef WIN32
int opt;
@ -471,6 +471,9 @@ main(int argc, char *argv[])
char *pf_path;
int pf_open_errno = 0;
int err;
#ifdef HAVE_LIBPCAP
gboolean start_capture = FALSE;
#endif
GtkWidget *window, *main_vbox, *menubar, *u_pane, *l_pane,
*bv_table, *bv_hscroll, *bv_vscroll, *stat_hbox,
*tv_scrollw, *filter_bt, *filter_te;
@ -501,7 +504,11 @@ main(int argc, char *argv[])
proto_registrar_dump();
exit(0);
}
/* Set "capture_child" to indicate whether this is going to be a child
process for a "-S" or "-F" capture? */
capture_child = (strcmp(command_name, CHILD_NAME) == 0);
/* Let GTK get its args */
gtk_init (&argc, &argv);
@ -589,7 +596,7 @@ main(int argc, char *argv[])
break;
#ifdef HAVE_LIBPCAP
case 'k': /* Start capture immediately */
start_capture = 1;
start_capture = TRUE;
break;
#endif
case 'P': /* Packet list pane height */
@ -598,7 +605,7 @@ main(int argc, char *argv[])
#ifdef HAVE_LIBPCAP
case 'Q': /* Quit after capture (just capture to file) */
quit_after_cap = 1;
start_capture = 1; /*** -Q implies -k !! ***/
start_capture = TRUE; /*** -Q implies -k !! ***/
break;
#endif
case 'r': /* Read capture file xxx */
@ -659,7 +666,7 @@ main(int argc, char *argv[])
exit(1);
}
#ifdef HAVE_LIBPCAP
if (sync_mode || fork_mode) {
if (capture_child && (sync_mode || fork_mode)) {
if (cf.save_file_fd == -1) {
fprintf(stderr, "ethereal: \"-k\" flag was specified with \"-%c\" flag but without \"-W\" flag\n",
(sync_mode ? 'S' : 'F'));
@ -859,7 +866,7 @@ main(int argc, char *argv[])
/* Is this a "child" ethereal, which is only supposed to pop up a
capture box to let us stop the capture, and run a capture
to a file that our parent will read? */
if (strcmp(command_name, CHILD_NAME) != 0) {
if (!capture_child) {
/* No. Pop up the main window, and read in a capture file if
we were told to. */
@ -907,28 +914,36 @@ main(int argc, char *argv[])
}
#ifdef HAVE_LIBPCAP
if (start_capture) {
/* "-k" was specified; start a capture. */
if (capture_child) {
/* This is the child process for a sync mode or fork mode capture,
so just do the low-level work of a capture - don't create
a temporary file (so don't call "do_capture()"), and don't
fork off *another* child process (so don't call "run_capture()"). */
/* Try to open/create the file specified on the command line with
the "-w" flag. (We already checked in "main()" that "-w" was
specified. */
cf.save_file_fd = open(cf.save_file, O_RDWR|O_TRUNC|O_CREAT, 0600);
if (cf.save_file_fd == -1) {
/* XXX - display the error in a message box, or on the command line? */
simple_dialog(ESD_TYPE_WARN, NULL,
"The file to which the capture would be saved (\"%s\")"
"could not be opened: %s.", cf.save_file, strerror(errno));
} else {
/* XXX - "capture()" used to do this, but we now do it in
"do_capture()", before calling "capture()"; will we ever
have a capture file open here?
Yes, if "-r" was specified - but that's arguably silly, so
perhaps we should treate that as an error. */
close_cap_file(&cf, info_bar, file_ctx);
capture();
capture();
} else {
if (start_capture) {
/* "-k" was specified; start a capture. */
/* Try to open/create the file specified on the command line with
the "-w" flag. (We already checked in "main()" that "-w" was
specified. */
cf.save_file_fd = open(cf.save_file, O_RDWR|O_TRUNC|O_CREAT, 0600);
if (cf.save_file_fd == -1) {
/* XXX - display the error in a message box, or on the command line? */
simple_dialog(ESD_TYPE_WARN, NULL,
"The file to which the capture would be saved (\"%s\")"
"could not be opened: %s.", cf.save_file, strerror(errno));
} else {
/* XXX - "capture()" used to do this, but we now do it in
"do_capture()", before calling "capture()"; will we ever
have a capture file open here?
Yes, if "-r" was specified - but that's arguably silly, so
perhaps we should treate that as an error. */
close_cap_file(&cf, info_bar, file_ctx);
run_capture();
}
}
start_capture = 0;
}
#endif