SCSI: Add basic decoding of SANITIZE opcode
[metze/wireshark/wip.git] / capture_sync.c
index 51c74bc13b4f632a76d9f98945b09622efd8a3c0..7bff5e6c482123211efcbceb45404e9cdb7cb14c 100644 (file)
@@ -118,18 +118,32 @@ static void pipe_convert_header(const guchar *header, int header_len, char *indi
 static ssize_t pipe_read_block(int pipe_fd, char *indicator, int len, char *msg,
                            char **err_msg);
 
-
+void
+capture_session_init(capture_session *cap_session, void *cf)
+{
+    cap_session->cf                              = cf;
+    cap_session->fork_child                      = -1;               /* invalid process handle */
+#ifdef _WIN32
+    cap_session->signal_pipe_write_fd            = -1;
+#endif
+    cap_session->state                           = CAPTURE_STOPPED;
+#ifndef _WIN32
+    cap_session->owner                           = getuid();
+    cap_session->group                           = getgid();
+#endif
+    cap_session->session_started                 = FALSE;
+}
 
 /* Append an arg (realloc) to an argc/argv array */
 /* (add a string pointer to a NULL-terminated array of string pointers) */
-static const char **
-sync_pipe_add_arg(const char **args, int *argc, const char *arg)
+static char **
+sync_pipe_add_arg(char **args, int *argc, const char *arg)
 {
     /* 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. */
-    args = (const char **)g_realloc( (gpointer) args, (*argc + 2) * sizeof (char *));
+    args = (char **)g_realloc( (gpointer) args, (*argc + 2) * sizeof (char *));
 
     /* Stuff the pointer into the penultimate element of the array, which
        is the one at the index specified by "*argc". */
@@ -288,9 +302,9 @@ win32strexception(DWORD exception)
 #endif
 
 /* Initialize an argument list and add dumpcap to it. */
-static const char **
+static char **
 init_pipe_args(int *argc) {
-    const char **argv;
+    char **argv;
     const char *progfile_dir;
     char *exename;
 
@@ -302,7 +316,7 @@ init_pipe_args(int *argc) {
     /* Allocate the string pointer array with enough space for the
        terminating NULL pointer. */
     *argc = 0;
-    argv = (const char **)g_malloc(sizeof (char *));
+    argv = (char **)g_malloc(sizeof (char *));
     *argv = NULL;
 
     /* take Wireshark's absolute program path and replace "Wireshark" with "dumpcap" */
@@ -320,9 +334,9 @@ init_pipe_args(int *argc) {
 #define ARGV_NUMBER_LEN 24
 /* a new capture run: start a new dumpcap task and hand over parameters through command line */
 gboolean
-sync_pipe_start(capture_options *capture_opts) {
+sync_pipe_start(capture_options *capture_opts, capture_session *cap_session)
+{
     char ssnap[ARGV_NUMBER_LEN];
-    char sdlt[ARGV_NUMBER_LEN];
     char scount[ARGV_NUMBER_LEN];
     char sfilesize[ARGV_NUMBER_LEN];
     char sfile_duration[ARGV_NUMBER_LEN];
@@ -359,7 +373,7 @@ sync_pipe_start(capture_options *capture_opts) {
 #endif
     int sync_pipe_read_fd;
     int argc;
-    const char **argv;
+    char **argv;
     int i;
     guint j;
     interface_options interface_opts;
@@ -369,7 +383,7 @@ sync_pipe_start(capture_options *capture_opts) {
     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_start");
     capture_opts_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, capture_opts);
 
-    capture_opts->fork_child = -1;
+    cap_session->fork_child = -1;
 
     argv = init_pipe_args(&argc);
     if (!argv) {
@@ -452,8 +466,7 @@ sync_pipe_start(capture_options *capture_opts) {
 
         if (interface_opts.linktype != -1) {
             argv = sync_pipe_add_arg(argv, &argc, "-y");
-            g_snprintf(sdlt, ARGV_NUMBER_LEN, "%s", linktype_val_to_name(interface_opts.linktype));
-            argv = sync_pipe_add_arg(argv, &argc, sdlt);
+            argv = sync_pipe_add_arg(argv, &argc, linktype_val_to_name(interface_opts.linktype));
         }
 
         if (!interface_opts.promisc_mode) {
@@ -596,7 +609,7 @@ sync_pipe_start(capture_options *capture_opts) {
         g_free( (gpointer) argv);
         return FALSE;
     }
-    capture_opts->fork_child = (int) pi.hProcess;
+    cap_session->fork_child = (int) pi.hProcess;
     g_string_free(args, TRUE);
 
     /* associate the operating system filehandle to a C run-time file handle */
@@ -604,7 +617,7 @@ sync_pipe_start(capture_options *capture_opts) {
     sync_pipe_read_fd = _open_osfhandle( (long) sync_pipe_read, _O_BINARY);
 
     /* associate the operating system filehandle to a C run-time file handle */
-    capture_opts->signal_pipe_write_fd = _open_osfhandle( (long) signal_pipe, _O_BINARY);
+    cap_session->signal_pipe_write_fd = _open_osfhandle( (long) signal_pipe, _O_BINARY);
 
 #else /* _WIN32 */
     if (pipe(sync_pipe) < 0) {
@@ -617,14 +630,14 @@ sync_pipe_start(capture_options *capture_opts) {
         return FALSE;
     }
 
-    if ((capture_opts->fork_child = fork()) == 0) {
+    if ((cap_session->fork_child = fork()) == 0) {
         /*
          * Child process - run dumpcap with the right arguments to make
          * it just capture with the specified capture parameters
          */
         dup2(sync_pipe[PIPE_WRITE], 2);
         ws_close(sync_pipe[PIPE_READ]);
-        execv(argv[0], (char * const*)argv);
+        execv(argv[0], argv);
         g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
                    argv[0], g_strerror(errno));
         sync_pipe_errmsg_to_parent(2, errmsg, "");
@@ -660,17 +673,18 @@ sync_pipe_start(capture_options *capture_opts) {
     ws_close(sync_pipe[PIPE_WRITE]);
 #endif
 
-    if (capture_opts->fork_child == -1) {
+    if (cap_session->fork_child == -1) {
         /* We couldn't even create the child process. */
         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);
+        ws_close(cap_session->signal_pipe_write_fd);
 #endif
         return FALSE;
     }
 
-    capture_opts->fork_child_status = 0;
+    cap_session->fork_child_status = 0;
+    cap_session->capture_opts = capture_opts;
 
     /* we might wait for a moment till child is ready, so update screen now */
     main_window_update();
@@ -681,8 +695,8 @@ sync_pipe_start(capture_options *capture_opts) {
        the child process wants to tell us something. */
 
     /* we have a running capture, now wait for the real capture filename */
-    pipe_input_set_handler(sync_pipe_read_fd, (gpointer) capture_opts,
-                           &capture_opts->fork_child, sync_pipe_input_cb);
+    pipe_input_set_handler(sync_pipe_read_fd, (gpointer) cap_session,
+                           &cap_session->fork_child, sync_pipe_input_cb);
 
     return TRUE;
 }
@@ -703,7 +717,7 @@ sync_pipe_start(capture_options *capture_opts) {
 /* XXX - assumes PIPE_BUF_SIZE > SP_MAX_MSG_LEN */
 #define PIPE_BUF_SIZE 5120
 static int
-sync_pipe_open_command(const char** argv, int *data_read_fd,
+sync_pipe_open_command(char** argv, int *data_read_fd,
                        int *message_read_fd, int *fork_child, gchar **msg)
 {
     enum PIPES { PIPE_READ, PIPE_WRITE };   /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
@@ -850,7 +864,7 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
         dup2(sync_pipe[PIPE_WRITE], 2);
         ws_close(sync_pipe[PIPE_READ]);
         ws_close(sync_pipe[PIPE_WRITE]);
-        execv(argv[0], (char * const*)argv);
+        execv(argv[0], argv);
         g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
                    argv[0], g_strerror(errno));
         sync_pipe_errmsg_to_parent(2, errmsg, "");
@@ -942,7 +956,7 @@ sync_pipe_close_command(int *data_read_fd, int *message_read_fd,
 /* XXX - assumes PIPE_BUF_SIZE > SP_MAX_MSG_LEN */
 #define PIPE_BUF_SIZE 5120
 static int
-sync_pipe_run_command(const char** argv, gchar **data, gchar **primary_msg,
+sync_pipe_run_command(char** argv, gchar **data, gchar **primary_msg,
                       gchar **secondary_msg)
 {
     gchar *msg;
@@ -1120,7 +1134,7 @@ sync_interface_set_80211_chan(const gchar *iface, const char *freq, const gchar
                               gchar **secondary_msg)
 {
     int argc, ret;
-    const char **argv;
+    char **argv;
     gchar *opt;
 
     argv = init_pipe_args(&argc);
@@ -1178,7 +1192,7 @@ sync_interface_list_open(gchar **data, gchar **primary_msg,
                          gchar **secondary_msg)
 {
     int argc;
-    const char **argv;
+    char **argv;
 
     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_list_open");
 
@@ -1220,7 +1234,7 @@ sync_if_capabilities_open(const gchar *ifname, gboolean monitor_mode,
                           gchar **secondary_msg)
 {
     int argc;
-    const char **argv;
+    char **argv;
 
     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_linktype_list_open");
 
@@ -1258,7 +1272,7 @@ int
 sync_interface_stats_open(int *data_read_fd, int *fork_child, gchar **msg)
 {
     int argc;
-    const char **argv;
+    char **argv;
     int message_read_fd, ret;
     char *wait_msg;
     gchar buffer[PIPE_BUF_SIZE+1];
@@ -1614,7 +1628,7 @@ pipe_read_block(int pipe_fd, char *indicator, int len, char *msg,
 static gboolean
 sync_pipe_input_cb(gint source, gpointer user_data)
 {
-    capture_options *capture_opts = (capture_options *)user_data;
+    capture_session *cap_session = (capture_session *)user_data;
     int  ret;
     char buffer[SP_MAX_MSG_LEN+1];
     ssize_t nread;
@@ -1641,7 +1655,7 @@ sync_pipe_input_cb(gint source, gpointer user_data)
 
            If we got an EOF, nread is 0 and primary_msg isn't set.  This
            is an indication that the capture is finished. */
-        ret = sync_pipe_wait_for_child(capture_opts->fork_child, &wait_msg);
+        ret = sync_pipe_wait_for_child(cap_session->fork_child, &wait_msg);
         if(nread == 0) {
             /* We got an EOF from the sync pipe.  That means that the capture
                child exited, and not in the middle of a message; we treat
@@ -1662,13 +1676,13 @@ sync_pipe_input_cb(gint source, gpointer user_data)
         }
 
         /* No more child process. */
-        capture_opts->fork_child = -1;
-        capture_opts->fork_child_status = ret;
+        cap_session->fork_child = -1;
+        cap_session->fork_child_status = ret;
 
 #ifdef _WIN32
-        ws_close(capture_opts->signal_pipe_write_fd);
+        ws_close(cap_session->signal_pipe_write_fd);
 #endif
-        capture_input_closed(capture_opts, primary_msg);
+        capture_input_closed(cap_session, primary_msg);
         g_free(primary_msg);
         return FALSE;
     }
@@ -1676,7 +1690,7 @@ sync_pipe_input_cb(gint source, gpointer user_data)
     /* we got a valid message block from the child, process it */
     switch(indicator) {
     case SP_FILE:
-        if(!capture_input_new_file(capture_opts, buffer)) {
+        if(!capture_input_new_file(cap_session, 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
@@ -1696,15 +1710,15 @@ sync_pipe_input_cb(gint source, gpointer user_data)
 
                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);
+            sync_pipe_stop(cap_session);
+            capture_input_closed(cap_session, NULL);
             return FALSE;
         }
         break;
     case SP_PACKET_COUNT:
         npackets = atoi(buffer);
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: new packets %u", npackets);
-        capture_input_new_packets(capture_opts, npackets);
+        capture_input_new_packets(cap_session, npackets);
         break;
     case SP_ERROR_MSG:
         /* convert primary message */
@@ -1714,7 +1728,7 @@ sync_pipe_input_cb(gint source, gpointer user_data)
         pipe_convert_header((guchar*)primary_msg + primary_len, 4, &indicator, &secondary_len);
         secondary_msg = primary_msg + primary_len + 4;
         /* message output */
-        capture_input_error_message(capture_opts, primary_msg, secondary_msg);
+        capture_input_error_message(cap_session, primary_msg, secondary_msg);
         /* the capture child will close the sync_pipe, nothing to do for now */
         /* (an error message doesn't mean we have to stop capturing) */
         break;
@@ -1725,12 +1739,12 @@ sync_pipe_input_cb(gint source, gpointer user_data)
         ch = strtok(buffer, ":");
         indx = (int)strtol(ch, NULL, 10);
         ch = strtok(NULL, ":");
-        capture_input_cfilter_error_message(capture_opts, indx, ch);
+        capture_input_cfilter_error_message(cap_session, indx, ch);
          /* the capture child will close the sync_pipe, nothing to do for now */
          break;
         }
     case SP_DROPS:
-        capture_input_drops(capture_opts, (guint32)strtoul(buffer, NULL, 10));
+        capture_input_drops(cap_session, (guint32)strtoul(buffer, NULL, 10));
         break;
     default:
         g_assert_not_reached();
@@ -1925,21 +1939,20 @@ sync_pipe_signame(int sig)
 #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)
+signal_pipe_capquit_to_child(capture_session *cap_session)
 {
     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);
+    /*pipe_write_block(cap_session->signal_pipe_write_fd, SP_QUIT, quit_msg);*/
+    ret = write(cap_session->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, g_strerror(errno));
+              "signal_pipe_capquit_to_child: %d header: error %s", cap_session->signal_pipe_write_fd, g_strerror(errno));
     }
 }
 #endif
@@ -1947,7 +1960,7 @@ signal_pipe_capquit_to_child(capture_options *capture_opts)
 
 /* user wants to stop the capture run */
 void
-sync_pipe_stop(capture_options *capture_opts)
+sync_pipe_stop(capture_session *cap_session)
 {
 #ifdef _WIN32
     int count;
@@ -1955,10 +1968,10 @@ sync_pipe_stop(capture_options *capture_opts)
     gboolean terminate = TRUE;
 #endif
 
-    if (capture_opts->fork_child != -1) {
+    if (cap_session->fork_child != -1) {
 #ifndef _WIN32
         /* send the SIGINT signal to close the capture child gracefully. */
-        int sts = kill(capture_opts->fork_child, SIGINT);
+        int sts = kill(cap_session->fork_child, SIGINT);
         if (sts != 0) {
             g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
                   "Sending SIGINT to child failed: %s\n", g_strerror(errno));
@@ -1969,11 +1982,11 @@ sync_pipe_stop(capture_options *capture_opts)
         /* First, use the special signal pipe to try to close the capture child
          * gracefully.
          */
-        signal_pipe_capquit_to_child(capture_opts);
+        signal_pipe_capquit_to_child(cap_session);
 
         /* 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) &&
+            if (GetExitCodeProcess((HANDLE) cap_session->fork_child, &childstatus) &&
                 childstatus != STILL_ACTIVE) {
                 terminate = FALSE;
                 break;
@@ -1985,7 +1998,7 @@ sync_pipe_stop(capture_options *capture_opts)
         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);
+            sync_pipe_kill(cap_session->fork_child);
         }
 #endif
     }