Add uid/gid config parameters to shared-vm database map operator
[vpp.git] / vpp / vpp-api / vpp_get_metrics.c
index e963bc6..ea4af01 100644 (file)
@@ -18,6 +18,8 @@
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
+#include <pwd.h>
+#include <grp.h>
 #include <netinet/in.h>
 #include <signal.h>
 #include <pthread.h>
@@ -106,9 +108,17 @@ main (int argc, char **argv)
   int interval = 0;
   f64 *vector_ratep, *rx_ratep, *sig_error_ratep;
   pid_t *vpp_pidp;
+  svmdb_map_args_t _ma, *ma= &_ma;
+  int uid, gid, rv;
+  struct passwd _pw, *pw;
+  struct group _grp, *grp;
+  char *s, buf[128];
 
   unformat_init_command_line (&input, argv);
 
+  uid = geteuid();
+  gid = getegid();
+
   while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
     {
       if (unformat (&input, "chroot %s", &chroot_path_u8))
@@ -117,6 +127,46 @@ main (int argc, char **argv)
        }
       else if (unformat (&input, "interval %d", &interval))
        ;
+      else if (unformat (&input, "uid %d", &uid))
+        ;
+      else if (unformat (&input, "gid %d", &gid))
+        ;
+      else if (unformat (&input, "uid %s", &s))
+        {
+          /* lookup the username */
+          pw = NULL;
+          rv = getpwnam_r(s, &_pw, buf, sizeof(buf), &pw);
+          if (rv < 0)
+            {
+              fformat (stderr, "cannot fetch username %s", s);
+              exit (1);
+            }
+          if (pw == NULL)
+            {
+              fformat (stderr, "username %s does not exist", s);
+              exit (1);
+            }
+          vec_free (s);
+          uid = pw->pw_uid;
+        }
+      else if (unformat (&input, "gid %s", &s))
+        {
+          /* lookup the group name */
+          grp = NULL;
+          rv = getgrnam_r(s, &_grp, buf, sizeof(buf), &grp);
+          if (rv != 0)
+            {
+              fformat (stderr, "cannot fetch group %s", s);
+              exit (1);
+            }
+          if (grp == NULL)
+            {
+              fformat (stderr, "group %s does not exist", s);
+              exit (1);
+            }
+          vec_free (s);
+          gid = grp->gr_gid;
+        }
       else
        {
          fformat (stderr,
@@ -127,7 +177,12 @@ main (int argc, char **argv)
 
   setup_signal_handlers ();
 
-  c = svmdb_map_chroot (chroot_path);
+  memset (ma, 0, sizeof (*ma));
+  ma->root_path = chroot_path;
+  ma->uid = uid;
+  ma->gid = gid;
+
+  c = svmdb_map (ma);
 
   vpp_pidp =
     svmdb_local_get_variable_reference (c, SVMDB_NAMESPACE_VEC, "vpp_pid");
@@ -156,8 +211,12 @@ main (int argc, char **argv)
 
   do
     {
-      /* Once vpp exits, the svm db region will be recreated... */
-      if (*vpp_pidp == 0 || kill (*vpp_pidp, 0) < 0)
+      /* 
+       * Once vpp exits, the svm db region will be recreated...
+       * Can't use kill (*vpp_pidp, 0) if running as non-root / 
+       * accessing the shared-VM database via group perms.
+       */
+      if (*vpp_pidp == 0)
        {
          fformat (stdout, "vpp not running\n");
          exit (1);