ovs-ofctl: New "ofctl/set-output-file" unixctl command.
authorBen Pfaff <blp@nicira.com>
Thu, 26 Jan 2012 23:45:34 +0000 (15:45 -0800)
committerBen Pfaff <blp@nicira.com>
Thu, 9 Feb 2012 21:31:32 +0000 (13:31 -0800)
This will be useful in unit tests, to allow switching output to a new file
during "ovs-ofctl monitor" runtime.

Signed-off-by: Ben Pfaff <blp@nicira.com>
utilities/ovs-ofctl.8.in
utilities/ovs-ofctl.c

index fc9b699d0ac03cfa8b4e0c4fa1229c6fee0d4c7a..51144f2b05fc3c4686588afb74097c204185a528 100644 (file)
@@ -1207,6 +1207,11 @@ process.  The supported commands are listed below.
 Causes \fBovs\-ofctl\fR to gracefully terminate.  This command applies
 only when executing the \fBmonitor\fR or \fBsnoop\fR commands.
 .
+.IP "\fBofctl/set\-output\-file \fIfile\fR"
+Causes all subsequent output to go to \fIfile\fR instead of stderr.
+This command applies only when executing the \fBmonitor\fR or
+\fBsnoop\fR commands.
+.
 .IP "\fBofctl/send \fIofmsg\fR..."
 Sends each \fIofmsg\fR, specified as a sequence of hex digits that
 express an OpenFlow message, on the OpenFlow connection.  This command
index f0b5275714fb9569693c6e0a89d3b3fc51356de6..a790605751b9765982b6e5b2110af28efc62cd6e 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/fcntl.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 
@@ -902,6 +903,24 @@ ofctl_send(struct unixctl_conn *conn, int argc,
     ds_destroy(&reply);
 }
 
+static void
+ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
+                      const char *argv[], void *aux OVS_UNUSED)
+{
+    int fd;
+
+    fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
+    if (fd < 0) {
+        unixctl_command_reply(conn, 501, strerror(errno));
+        return;
+    }
+
+    fflush(stderr);
+    dup2(fd, STDERR_FILENO);
+    close(fd);
+    unixctl_command_reply(conn, 200, "");
+}
+
 static void
 monitor_vconn(struct vconn *vconn)
 {
@@ -918,6 +937,8 @@ monitor_vconn(struct vconn *vconn)
     unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
     unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
                              ofctl_send, vconn);
+    unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
+                             ofctl_set_output_file, NULL);
     daemonize_complete();
 
     for (;;) {