From Irene Ruengeler: Whitespace changes.
[obnox/wireshark/wip.git] / capture_sync.c
index 310c9179945237930bb01f7dc6db3304792cfe01..04614b2ebeab2d90cce3e9a39cfb4fd80c8317f6 100644 (file)
@@ -97,7 +97,9 @@
 #ifdef _WIN32
 #include "capture-wpcap.h"
 #endif
-#include "ui_util.h"
+
+#include "ui/ui_util.h"
+
 #include <wsutil/file_util.h>
 #include "log.h"
 
@@ -133,8 +135,7 @@ sync_pipe_add_arg(const char **args, int *argc, const char *arg)
 
     /* Stuff the pointer into the penultimate element of the array, which
        is the one at the index specified by "*argc". */
-    args[*argc] = arg;
-
+    args[*argc] = g_strdup(arg);
     /* Now bump the count. */
     (*argc)++;
 
@@ -438,12 +439,6 @@ sync_pipe_start(capture_options *capture_opts) {
             argv = sync_pipe_add_arg(argv, &argc, "-f");
             argv = sync_pipe_add_arg(argv, &argc, interface_opts.cfilter);
         }
-        if (interface_opts.cfilter == NULL &&
-            capture_opts->default_options.cfilter != NULL &&
-            strlen(capture_opts->default_options.cfilter) != 0) {
-            argv = sync_pipe_add_arg(argv, &argc, "-f");
-            argv = sync_pipe_add_arg(argv, &argc, capture_opts->default_options.cfilter);
-        }
         if (interface_opts.snaplen != WTAP_MAX_PACKET_SIZE) {
             argv = sync_pipe_add_arg(argv, &argc, "-s");
             g_snprintf(ssnap, ARGV_NUMBER_LEN, "%d", interface_opts.snaplen);
@@ -468,9 +463,11 @@ sync_pipe_start(capture_options *capture_opts) {
         }
 #endif
 
+#ifdef HAVE_PCAP_CREATE
         if (interface_opts.monitor_mode) {
             argv = sync_pipe_add_arg(argv, &argc, "-I");
         }
+#endif
 
 #ifdef HAVE_PCAP_REMOTE
         if (interface_opts.datatx_udp)
@@ -532,7 +529,9 @@ sync_pipe_start(capture_options *capture_opts) {
         /* Couldn't create the pipe between parent and child. */
         report_failure("Couldn't create sync pipe: %s",
                        win32strerror(GetLastError()));
-        g_free( (gpointer) argv[0]);
+        for (i = 0; i < argc; i++) {
+            g_free( (gpointer) argv[i]);
+        }
         g_free( (gpointer) argv);
         return FALSE;
     }
@@ -547,7 +546,9 @@ sync_pipe_start(capture_options *capture_opts) {
         /* Couldn't create the signal pipe between parent and child. */
         report_failure("Couldn't create signal pipe: %s",
                        win32strerror(GetLastError()));
-        g_free( (gpointer) argv[0]);
+        for (i = 0; i < argc; i++) {
+            g_free( (gpointer) argv[i]);
+        }
         g_free( (gpointer) argv);
         return FALSE;
     }
@@ -584,7 +585,9 @@ sync_pipe_start(capture_options *capture_opts) {
                        args->str, win32strerror(GetLastError()));
         CloseHandle(sync_pipe_read);
         CloseHandle(sync_pipe_write);
-        g_free( (gpointer) argv[0]);
+        for (i = 0; i < argc; i++) {
+            g_free( (gpointer) argv[i]);
+        }
         g_free( (gpointer) argv);
         return FALSE;
     }
@@ -601,8 +604,10 @@ sync_pipe_start(capture_options *capture_opts) {
 #else /* _WIN32 */
     if (pipe(sync_pipe) < 0) {
         /* Couldn't create the pipe between parent and child. */
-        report_failure("Couldn't create sync pipe: %s", strerror(errno));
-        g_free( (gpointer) argv[0]);
+        report_failure("Couldn't create sync pipe: %s", g_strerror(errno));
+        for (i = 0; i < argc; i++) {
+            g_free( (gpointer) argv[i]);
+        }
         g_free(argv);
         return FALSE;
     }
@@ -616,7 +621,7 @@ sync_pipe_start(capture_options *capture_opts) {
         ws_close(sync_pipe[PIPE_READ]);
         execv(argv[0], (gpointer)argv);
         g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
-                   argv[0], strerror(errno));
+                   argv[0], g_strerror(errno));
         sync_pipe_errmsg_to_parent(2, errmsg, "");
 
         /* Exit with "_exit()", so that we don't close the connection
@@ -632,7 +637,9 @@ sync_pipe_start(capture_options *capture_opts) {
     sync_pipe_read_fd = sync_pipe[PIPE_READ];
 #endif
 
-    g_free( (gpointer) argv[0]);  /* exename */
+    for (i = 0; i < argc; i++) {
+        g_free( (gpointer) argv[i]);
+    }
 
     /* Parent process - read messages from the child process over the
        sync pipe. */
@@ -650,7 +657,7 @@ sync_pipe_start(capture_options *capture_opts) {
 
     if (capture_opts->fork_child == -1) {
         /* We couldn't even create the child process. */
-        report_failure("Couldn't create child process: %s", strerror(errno));
+        report_failure("Couldn't create child process: %s", g_strerror(errno));
         ws_close(sync_pipe_read_fd);
 #ifdef _WIN32
         ws_close(capture_opts->signal_pipe_write_fd);
@@ -703,13 +710,12 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
     SECURITY_ATTRIBUTES sa;
     STARTUPINFO si;
     PROCESS_INFORMATION pi;
-    int i;
 #else
     char errmsg[1024+1];
     int sync_pipe[2];                       /* pipe used to send messages from child to parent */
     int data_pipe[2];                       /* pipe used to send data from child to parent */
 #endif
-
+    int i;
     *fork_child = -1;
     *data_read_fd = -1;
     *message_read_fd = -1;
@@ -735,7 +741,9 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
         /* Couldn't create the message pipe between parent and child. */
         *msg = g_strdup_printf("Couldn't create sync pipe: %s",
                                win32strerror(GetLastError()));
-        g_free( (gpointer) argv[0]);
+        for (i = 0; argv[i] != NULL; i++) {
+            g_free( (gpointer) argv[i]);
+        }
         g_free( (gpointer) argv);
         return -1;
     }
@@ -748,7 +756,9 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
                                win32strerror(GetLastError()));
         CloseHandle(sync_pipe[PIPE_READ]);
         CloseHandle(sync_pipe[PIPE_WRITE]);
-        g_free( (gpointer) argv[0]);
+        for (i = 0; argv[i] != NULL; i++) {
+            g_free( (gpointer) argv[i]);
+        }
         g_free( (gpointer) argv);
         return -1;
     }
@@ -786,7 +796,9 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
         CloseHandle(data_pipe[PIPE_WRITE]);
         CloseHandle(sync_pipe[PIPE_READ]);
         CloseHandle(sync_pipe[PIPE_WRITE]);
-        g_free( (gpointer) argv[0]);
+        for (i = 0; argv[i] != NULL; i++) {
+            g_free( (gpointer) argv[i]);
+        }
         g_free( (gpointer) argv);
         return -1;
     }
@@ -801,8 +813,10 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
     /* Create a pipe for the child process to send us messages */
     if (pipe(sync_pipe) < 0) {
         /* Couldn't create the message pipe between parent and child. */
-        *msg = g_strdup_printf("Couldn't create sync pipe: %s", strerror(errno));
-        g_free( (gpointer) argv[0]);
+        *msg = g_strdup_printf("Couldn't create sync pipe: %s", g_strerror(errno));
+        for (i = 0; argv[i] != NULL; i++) {
+            g_free( (gpointer) argv[i]);
+        }
         g_free(argv);
         return -1;
     }
@@ -810,10 +824,12 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
     /* Create a pipe for the child process to send us data */
     if (pipe(data_pipe) < 0) {
         /* Couldn't create the data pipe between parent and child. */
-        *msg = g_strdup_printf("Couldn't create data pipe: %s", strerror(errno));
+        *msg = g_strdup_printf("Couldn't create data pipe: %s", g_strerror(errno));
         ws_close(sync_pipe[PIPE_READ]);
         ws_close(sync_pipe[PIPE_WRITE]);
-        g_free( (gpointer) argv[0]);
+        for (i = 0; argv[i] != NULL; i++) {
+            g_free( (gpointer) argv[i]);
+        }
         g_free(argv);
         return -1;
     }
@@ -831,7 +847,7 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
         ws_close(sync_pipe[PIPE_WRITE]);
         execv(argv[0], (gpointer)argv);
         g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
-                   argv[0], strerror(errno));
+                   argv[0], g_strerror(errno));
         sync_pipe_errmsg_to_parent(2, errmsg, "");
 
         /* Exit with "_exit()", so that we don't close the connection
@@ -848,7 +864,9 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
     *message_read_fd = sync_pipe[PIPE_READ];
 #endif
 
-    g_free( (gpointer) argv[0]);  /* exename */
+    for (i = 0; argv[i] != NULL; i++) {
+        g_free( (gpointer) argv[i]);
+    }
 
     /* Parent process - read messages from the child process over the
        sync pipe. */
@@ -868,7 +886,7 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
 
     if (*fork_child == -1) {
         /* We couldn't even create the child process. */
-        *msg = g_strdup_printf("Couldn't create child process: %s", strerror(errno));
+        *msg = g_strdup_printf("Couldn't create child process: %s", g_strerror(errno));
         ws_close(*data_read_fd);
         ws_close(*message_read_fd);
         return -1;
@@ -880,14 +898,16 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
 }
 
 /*
- * Wait for dumpcap to finish.  On success, *msg is unchanged, and 0 is
- * returned.  On failure, *msg points to an error message for the
- * failure, and -1 is returned.  In the latter case, *msg must be
- * freed with g_free().
+ * Close the pipes we're using to read from dumpcap, and wait for it
+ * to exit.  On success, *msgp is unchanged, and the exit status of
+ * dumpcap is returned.  On failure (which includes "dumpcap exited
+ * due to being killed by a signal or an exception"), *msgp points
+ * to an error message for the failure, and -1 is returned.  In the
+ * latter case, *msgp must be freed with g_free().
  */
 static int
 sync_pipe_close_command(int *data_read_fd, int *message_read_fd,
-                        int *fork_child, gchar **msg)
+                        int *fork_child, gchar **msgp)
 {
     ws_close(*data_read_fd);
     if (message_read_fd != NULL)
@@ -898,7 +918,7 @@ sync_pipe_close_command(int *data_read_fd, int *message_read_fd,
     sync_pipe_kill(*fork_child);
 #endif
 
-    return sync_pipe_wait_for_child(*fork_child, msg);
+    return sync_pipe_wait_for_child(*fork_child, msgp);
 }
 
 /*
@@ -1358,9 +1378,9 @@ pipe_read_bytes(int pipe_fd, char *bytes, int required, char **msg)
             error = errno;
             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
                   "read from pipe %d: error(%u): %s", pipe_fd, error,
-                  strerror(error));
+                  g_strerror(error));
             *msg = g_strdup_printf("Error reading from sync pipe: %s",
-                                   strerror(error));
+                                   g_strerror(error));
             return newly;
         }
 
@@ -1419,7 +1439,7 @@ sync_pipe_gets_nonblock(int pipe_fd, char *bytes, int max) {
         } else if (newly < 0) {
             /* error */
             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
-                  "read from pipe %d: error(%u): %s", pipe_fd, errno, strerror(errno));
+                  "read from pipe %d: error(%u): %s", pipe_fd, errno, g_strerror(errno));
             return newly;
         } else if (bytes[offset] == '\n') {
             break;
@@ -1598,12 +1618,22 @@ sync_pipe_input_cb(gint source, gpointer user_data)
                alerted. Close the sync pipe. */
             ws_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) */
+            /* The child has sent us a filename which we couldn't open.
+
+               This could mean that the child is creating files faster
+              than we can handle it.  (XXX - why would that result in
+              a failure to open the file?)
+
+               That 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 the user e.g. wants to
+              switch files every second).
+
+               This can also happen if the user specified "-", meaning
+               "standard output", as the capture file. */
             sync_pipe_stop(capture_opts);
+            capture_input_closed(capture_opts, NULL);
+            return FALSE;
         }
         break;
     case SP_PACKET_COUNT:
@@ -1646,7 +1676,14 @@ sync_pipe_input_cb(gint source, gpointer user_data)
 
 
 
-/* the child process is going down, wait until it's completely terminated */
+/*
+ * dumpcap is exiting; wait for it to exit.  On success, *msgp is
+ * unchanged, and the exit status of dumpcap is returned.  On
+ * failure (which includes "dumpcap exited due to being killed by
+ * a signal or an exception"), *msgp points to an error message
+ * for the failure, and -1 is returned.  In the latter case, *msgp
+ * must be freed with g_free().
+ */
 static int
 sync_pipe_wait_for_child(int fork_child, gchar **msgp)
 {
@@ -1657,10 +1694,9 @@ sync_pipe_wait_for_child(int fork_child, gchar **msgp)
     g_assert(fork_child != -1);
 
     *msgp = NULL; /* assume no error */
-    ret = 0;
 #ifdef _WIN32
     if (_cwait(&fork_child_status, fork_child, _WAIT_CHILD) == -1) {
-        *msgp = g_strdup_printf("Error from cwait(): %s", strerror(errno));
+        *msgp = g_strdup_printf("Error from cwait(): %s", g_strerror(errno));
         ret = -1;
     } else {
         /*
@@ -1702,7 +1738,7 @@ sync_pipe_wait_for_child(int fork_child, gchar **msgp)
             ret = -1;
         }
     } else {
-        *msgp = g_strdup_printf("Error from waitpid(): %s", strerror(errno));
+        *msgp = g_strdup_printf("Error from waitpid(): %s", g_strerror(errno));
         ret = -1;
     }
 #endif
@@ -1826,7 +1862,7 @@ signal_pipe_capquit_to_child(capture_options *capture_opts)
     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));
+              "signal_pipe_capquit_to_child: %d header: error %s", capture_opts->signal_pipe_write_fd, g_strerror(errno));
     }
 }
 #endif
@@ -1848,7 +1884,7 @@ sync_pipe_stop(capture_options *capture_opts)
         int sts = kill(capture_opts->fork_child, SIGINT);
         if (sts != 0) {
             g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
-                  "Sending SIGINT to child failed: %s\n", strerror(errno));
+                  "Sending SIGINT to child failed: %s\n", g_strerror(errno));
         }
 #else
 #define STOP_SLEEP_TIME 500 /* ms */
@@ -1888,7 +1924,7 @@ sync_pipe_kill(int fork_child)
         int sts = kill(fork_child, SIGTERM);    /* SIGTERM so it can clean up if necessary */
         if (sts != 0) {
             g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
-                  "Sending SIGTERM to child failed: %s\n", strerror(errno));
+                  "Sending SIGTERM to child failed: %s\n", g_strerror(errno));
         }
 #else
         /* Remark: This is not the preferred method of closing a process!