ovs-ofctl: Fix minor memory leaks.
[sliver-openvswitch.git] / utilities / ovs-ofctl.c
index eb6d5a8..7df0a90 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -208,7 +208,10 @@ usage(void)
            "  mod-flows SWITCH FLOW       modify actions of matching FLOWs\n"
            "  del-flows SWITCH [FLOW]     delete matching FLOWs\n"
            "  replace-flows SWITCH FILE   replace flows with those in FILE\n"
-           "  monitor SWITCH [MISSLEN]    print packets received from SWITCH\n"
+           "  diff-flows SOURCE1 SOURCE2  compare flows from two sources\n"
+           "  monitor SWITCH [MISSLEN] [invalid_ttl]\n"
+           "                              print packets received from SWITCH\n"
+           "  snoop SWITCH                snoop on SWITCH and its controller\n"
            "\nFor OpenFlow switches and controllers:\n"
            "  probe TARGET                probe whether TARGET is up\n"
            "  ping TARGET [N]             latency of N-byte echos\n"
@@ -444,6 +447,8 @@ fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
 
     config = reply->data;
     *config_ = *config;
+
+    ofpbuf_delete(reply);
 }
 
 static void
@@ -828,30 +833,9 @@ monitor_vconn(struct vconn *vconn)
 {
     struct unixctl_server *server;
     bool exiting = false;
-    int error, fd;
-
-    if (preferred_packet_in_format >= 0) {
-        set_packet_in_format(vconn, preferred_packet_in_format);
-    } else {
-        struct ofpbuf *spif, *reply;
-
-        spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
-        run(vconn_transact_noreply(vconn, spif, &reply),
-            "talking to %s", vconn_get_name(vconn));
-        if (reply) {
-            char *s = ofp_to_string(reply->data, reply->size, 2);
-            VLOG_DBG("%s: failed to set packet in format to nxm, controller"
-                     " replied: %s. Falling back to the switch default.",
-                     vconn_get_name(vconn), s);
-            free(s);
-            ofpbuf_delete(reply);
-        }
-    }
-
-    /* Daemonization will close stderr but we really want to keep it, so make a
-     * copy. */
-    fd = dup(STDERR_FILENO);
+    int error;
 
+    daemon_save_fd(STDERR_FILENO);
     daemonize_start();
     error = unixctl_server_create(NULL, &server);
     if (error) {
@@ -860,9 +844,6 @@ monitor_vconn(struct vconn *vconn)
     unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
     daemonize_complete();
 
-    /* Now get stderr back. */
-    dup2(fd, STDERR_FILENO);
-
     for (;;) {
         struct ofpbuf *b;
         int retval;
@@ -890,6 +871,8 @@ monitor_vconn(struct vconn *vconn)
         unixctl_server_wait(server);
         poll_block();
     }
+    vconn_close(vconn);
+    unixctl_server_destroy(server);
 }
 
 static void
@@ -910,6 +893,24 @@ do_monitor(int argc, char *argv[])
             monitor_set_invalid_ttl_to_controller(vconn);
         }
     }
+    if (preferred_packet_in_format >= 0) {
+        set_packet_in_format(vconn, preferred_packet_in_format);
+    } else {
+        struct ofpbuf *spif, *reply;
+
+        spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
+        run(vconn_transact_noreply(vconn, spif, &reply),
+            "talking to %s", vconn_get_name(vconn));
+        if (reply) {
+            char *s = ofp_to_string(reply->data, reply->size, 2);
+            VLOG_DBG("%s: failed to set packet in format to nxm, controller"
+                     " replied: %s. Falling back to the switch default.",
+                     vconn_get_name(vconn), s);
+            free(s);
+            ofpbuf_delete(reply);
+        }
+    }
+
     monitor_vconn(vconn);
 }