[ovs-dev] [PATCH v3] ovs-vsctl: add parent process name and ID

William Tu u9012063 at gmail.com
Tue Jan 12 22:24:53 UTC 2016


This patch forces appending "parent_process_name(PID)" when invoking
ovs-vsctl, in order to assist debugging. The patch is for Linux only.
For example:
    User adds br0 by "ovs-vsctl add-br0", the log shows:
    "ovs-vsctl (invoked by base(1528)): ovs-vsctl add-br br0"

Signed-off-by: William Tu <u9012063 at gmail.com>
---
 utilities/ovs-vsctl.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)

diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
index 36290db..801113a 100644
--- a/utilities/ovs-vsctl.c
+++ b/utilities/ovs-vsctl.c
@@ -2468,6 +2468,54 @@ run_prerequisites(struct ctl_command *commands, size_t n_commands,
     }
 }
 
+#ifdef __linux__
+
+static char*
+vsctl_parent_process_info(void)
+{
+    pid_t parent_pid;
+    char *procfile;
+    char *msg_ppid;
+    char *msg;
+    struct ds s;
+    FILE *f;
+    size_t total_size = 0;
+
+    parent_pid = getppid();
+    procfile = xasprintf("/proc/%d/cmdline", parent_pid);
+
+    f = fopen(procfile, "r");
+    if (!f) {
+        VLOG_ERR("can not open file: %s\n", procfile);
+        free(procfile);
+        return NULL;
+    }
+
+    ds_init(&s);
+
+    while (1) {
+        size_t size;
+        char c;
+        size = fread(&c, 1, 1, f);
+		if (c == '\0' || size < 1) {
+            break;
+        }
+        ds_put_char(&s, c);
+        total_size += size;
+    }
+    fclose(f);
+
+    msg_ppid = xasprintf("(%d)", parent_pid);
+    ds_put_cstr(&s, msg_ppid);
+
+    msg = ds_steal_cstr(&s);
+    free(procfile);
+    free(msg_ppid);
+    ds_destroy(&s);
+    return msg;
+}
+#endif
+
 static void
 do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
          struct ovsdb_idl *idl)
@@ -2481,13 +2529,26 @@ do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
     struct shash_node *node;
     int64_t next_cfg = 0;
     char *error = NULL;
+    char *ppid_info = NULL;
 
     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
     if (dry_run) {
         ovsdb_idl_txn_set_dry_run(txn);
     }
 
-    ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
+#ifdef __linux__
+    ppid_info = vsctl_parent_process_info();
+    if (ppid_info) {
+        ovsdb_idl_txn_add_comment(txn, "ovs-vsctl (invoked by %s): %s",
+                                  ppid_info, args);
+        free(ppid_info);
+    }
+    else {
+#endif
+        ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
+#ifdef __linux__
+    }
+#endif
 
     ovs = ovsrec_open_vswitch_first(idl);
     if (!ovs) {
-- 
2.5.0




More information about the dev mailing list