-#endif
-
-
-
-static gboolean
-sync_pipe_do_capture(gboolean is_tempfile) {
- guint byte_count;
- int i;
- guchar c;
- char *msg;
- int err;
- char ssnap[24];
- char scount[24]; /* need a constant for len of numbers */
- char sautostop_filesize[24]; /* need a constant for len of numbers */
- char sautostop_duration[24]; /* need a constant for len of numbers */
- char save_file_fd[24];
-#ifndef _WIN32
- char errmsg[1024+1];
-#endif
- int error;
- int argc;
- char **argv;
-#ifdef _WIN32
- char sync_pipe_fd[24];
- char *fontstring;
- char *filterstring;
-#endif
-
- /* Allocate the string pointer array with enough space for the
- terminating NULL pointer. */
- argc = 0;
- argv = g_malloc(sizeof (char *));
- *argv = NULL;
-
- /* Now add those arguments used on all platforms. */
- argv = sync_pipe_add_arg(argv, &argc, CHILD_NAME);
-
- argv = sync_pipe_add_arg(argv, &argc, "-i");
- argv = sync_pipe_add_arg(argv, &argc, cfile.iface);
-
- argv = sync_pipe_add_arg(argv, &argc, "-w");
- argv = sync_pipe_add_arg(argv, &argc, cfile.save_file);
-
- argv = sync_pipe_add_arg(argv, &argc, "-W");
- sprintf(save_file_fd,"%d",cfile.save_file_fd); /* in lieu of itoa */
- argv = sync_pipe_add_arg(argv, &argc, save_file_fd);
-
- if (capture_opts.has_autostop_packets) {
- argv = sync_pipe_add_arg(argv, &argc, "-c");
- sprintf(scount,"%d",capture_opts.autostop_packets);
- argv = sync_pipe_add_arg(argv, &argc, scount);
- }
-
- if (capture_opts.has_snaplen) {
- argv = sync_pipe_add_arg(argv, &argc, "-s");
- sprintf(ssnap,"%d",capture_opts.snaplen);
- argv = sync_pipe_add_arg(argv, &argc, ssnap);
- }
-
- if (capture_opts.linktype != -1) {
- argv = sync_pipe_add_arg(argv, &argc, "-y");
- sprintf(ssnap,"%d",capture_opts.linktype);
- argv = sync_pipe_add_arg(argv, &argc, ssnap);
- }
-
- if (capture_opts.has_autostop_filesize) {
- argv = sync_pipe_add_arg(argv, &argc, "-a");
- sprintf(sautostop_filesize,"filesize:%d",capture_opts.autostop_filesize);
- argv = sync_pipe_add_arg(argv, &argc, sautostop_filesize);
- }
-
- if (capture_opts.has_autostop_duration) {
- argv = sync_pipe_add_arg(argv, &argc, "-a");
- sprintf(sautostop_duration,"duration:%d",capture_opts.autostop_duration);
- argv = sync_pipe_add_arg(argv, &argc, sautostop_duration);
- }
-
- if (!capture_opts.promisc_mode)
- argv = sync_pipe_add_arg(argv, &argc, "-p");
-
-#ifdef _WIN32
- /* Create a pipe for the child process */
-
- if(_pipe(sync_pipe, 512, O_BINARY) < 0) {
- /* Couldn't create the pipe between parent and child. */
- error = errno;
- unlink(cfile.save_file);
- g_free(cfile.save_file);
- cfile.save_file = NULL;
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Couldn't create sync pipe: %s",
- strerror(error));
- return FALSE;
- }
-
- /* Convert font name to a quote-encapsulated string and pass to child */
- argv = sync_pipe_add_arg(argv, &argc, "-m");
- fontstring = sync_pipe_quote_encapsulate(prefs.PREFS_GUI_FONT_NAME);
- argv = sync_pipe_add_arg(argv, &argc, fontstring);
-
- /* Convert pipe write handle to a string and pass to child */
- argv = sync_pipe_add_arg(argv, &argc, "-Z");
- itoa(sync_pipe[WRITE], sync_pipe_fd, 10);
- argv = sync_pipe_add_arg(argv, &argc, sync_pipe_fd);
-
- /* Convert filter string to a quote delimited string and pass to child */
- filterstring = NULL;
- if (cfile.cfilter != NULL && strlen(cfile.cfilter) != 0) {
- argv = sync_pipe_add_arg(argv, &argc, "-f");
- filterstring = sync_pipe_quote_encapsulate(cfile.cfilter);
- argv = sync_pipe_add_arg(argv, &argc, filterstring);
- }
-
- /* Spawn process */
- fork_child = spawnvp(_P_NOWAIT, ethereal_path, argv);
- g_free(fontstring);
- if (filterstring) {
- g_free(filterstring);
- }
-#else
- if (pipe(sync_pipe) < 0) {
- /* Couldn't create the pipe between parent and child. */
- error = errno;
- unlink(cfile.save_file);
- g_free(cfile.save_file);
- cfile.save_file = NULL;
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Couldn't create sync pipe: %s",
- strerror(error));
- return FALSE;
- }
-
- argv = sync_pipe_add_arg(argv, &argc, "-m");
- argv = sync_pipe_add_arg(argv, &argc, prefs.PREFS_GUI_FONT_NAME);
-
- if (cfile.cfilter != NULL && strlen(cfile.cfilter) != 0) {
- argv = sync_pipe_add_arg(argv, &argc, "-f");
- argv = sync_pipe_add_arg(argv, &argc, cfile.cfilter);
- }
-
- if ((fork_child = fork()) == 0) {
- /*
- * Child process - run Ethereal with the right arguments to make
- * it just pop up the live capture dialog box and capture with
- * the specified capture parameters, writing to the specified file.
- *
- * args: -i interface specification
- * -w file to write
- * -W file descriptor to write
- * -c count to capture
- * -s snaplen
- * -m / -b fonts
- * -f "filter expression"
- */
- close(1);
- dup(sync_pipe[WRITE]);
- close(sync_pipe[READ]);
- execvp(ethereal_path, argv);
- snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
- ethereal_path, strerror(errno));
- sync_pipe_errmsg_to_parent(errmsg);
-
- /* 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);
- }
-#endif
-
- /* Parent process - read messages from the child process over the
- sync pipe. */
- g_free(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). */
- close(sync_pipe[WRITE]);
-
- /* Close the save file FD, as we won't be using it - we'll be opening
- it and reading the save file through Wiretap. */
- close(cfile.save_file_fd);
-
- if (fork_child == -1) {
- /* We couldn't even create the child process. */
- error = errno;
- close(sync_pipe[READ]);
- unlink(cfile.save_file);
- g_free(cfile.save_file);
- cfile.save_file = NULL;
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
- "Couldn't create child process: %s", strerror(error));
- return FALSE;
- }
-
- /* Read a byte count from "sync_pipe[READ]", terminated with a
- colon; if the count is 0, the child process created the
- capture file and we should start reading from it, otherwise
- the capture couldn't start and the count is a count of bytes
- of error message, and we should display the message. */
- byte_count = 0;
- for (;;) {
- i = read(sync_pipe[READ], &c, 1);
- if (i == 0) {
- /* EOF - the child process died.
- Close the read side of the sync pipe, remove the capture file,
- and report the failure. */
- close(sync_pipe[READ]);
- unlink(cfile.save_file);
- g_free(cfile.save_file);
- cfile.save_file = NULL;
- sync_pipe_wait_for_child(TRUE);
- return FALSE;
- }
- if (c == SP_CAPSTART || c == SP_ERROR_MSG)
- break;
- if (!isdigit(c)) {
- /* Child process handed us crap.
- Close the read side of the sync pipe, remove the capture file,
- and report the failure. */
- close(sync_pipe[READ]);
- unlink(cfile.save_file);
- g_free(cfile.save_file);
- cfile.save_file = NULL;
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
- "Capture child process sent us a bad message");
- return FALSE;
- }
- byte_count = byte_count*10 + c - '0';
- }
- if (c != SP_CAPSTART) {
- /* Failure - the child process sent us a message indicating
- what the problem was. */
- if (byte_count == 0) {
- /* Zero-length message? */
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
- "Capture child process failed, but its error message was empty.");
- } else {
- msg = g_malloc(byte_count + 1);
- if (msg == NULL) {
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
- "Capture child process failed, but its error message was too big.");
- } else {
- i = read(sync_pipe[READ], msg, byte_count);
- msg[byte_count] = '\0';
- if (i < 0) {
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
- "Capture child process failed: Error %s reading its error message.",
- strerror(errno));
- } else if (i == 0) {
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
- "Capture child process failed: EOF reading its error message.");
- sync_pipe_wait_for_child(FALSE);
- } else
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, msg);
- g_free(msg);
- }
-
- /* Close the sync pipe. */
- close(sync_pipe[READ]);
-
- /* Get rid of the save file - the capture never started. */
- unlink(cfile.save_file);
- g_free(cfile.save_file);
- cfile.save_file = NULL;
- }
- return FALSE;
- }