From: William Tu Date: Tue, 12 Jan 2016 22:24:53 +0000 (-0800) Subject: ovs-vsctl: Add parent process name and ID. X-Git-Url: http://git.cascardo.eti.br/?a=commitdiff_plain;h=484371776e5de6ccf194573dc9e984a653a6dcfc;hp=7157b6d3cde2f019dd68f78a71181477f40280f9;p=cascardo%2Fovs.git ovs-vsctl: Add parent process name and ID. 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 [blp@ovn.org made stylistic changes] Signed-off-by: Ben Pfaff --- diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c index 36290db79..c5a28e016 100644 --- a/utilities/ovs-vsctl.c +++ b/utilities/ovs-vsctl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc. + * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -2468,6 +2468,44 @@ run_prerequisites(struct ctl_command *commands, size_t n_commands, } } +static char * +vsctl_parent_process_info(void) +{ +#ifdef __linux__ + pid_t parent_pid; + char *procfile; + struct ds s; + FILE *f; + + parent_pid = getppid(); + procfile = xasprintf("/proc/%d/cmdline", parent_pid); + + f = fopen(procfile, "r"); + if (!f) { + VLOG_WARN("%s: open failed (%s)", procfile, ovs_strerror(errno)); + free(procfile); + return NULL; + } + free(procfile); + + ds_init(&s); + for (;;) { + int c = getc(f); + if (!c || c == EOF) { + break; + } + ds_put_char(&s, c); + } + fclose(f); + + ds_put_format(&s, " (pid %d)", parent_pid); + + return ds_steal_cstr(&s); +#else + return NULL; +#endif +} + static void do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands, struct ovsdb_idl *idl) @@ -2481,13 +2519,21 @@ 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); + 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 { + ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args); + } ovs = ovsrec_open_vswitch_first(idl); if (!ovs) {