446cf2
commit b2e93de0ffedcfe2cfba100d47a4d4f6f85cea0b
446cf2
Author: DJ Delorie <dj@redhat.com>
446cf2
Date:   Tue Dec 4 00:03:12 2018 -0500
446cf2
446cf2
    test-container: add "su" command to run test as root, add unshare hints
446cf2
    
446cf2
    * support/test-container.c (check_for_unshare_hints): New.
446cf2
    (main): Call it if unshare fails.  Add support for "su" scriptlet
446cf2
    command.
446cf2
446cf2
diff --git a/support/test-container.c b/support/test-container.c
446cf2
index fe0ebbd07df83da7..1d1aebeaf3412573 100644
446cf2
--- a/support/test-container.c
446cf2
+++ b/support/test-container.c
446cf2
@@ -88,15 +88,22 @@ int verbose = 0;
446cf2
    * mytest.root/ is rsync'd into container
446cf2
    * mytest.root/preclean.req causes fresh rsync (with delete) before
446cf2
      test if present
446cf2
-   * mytest.root/mytset.script has a list of "commands" to run:
446cf2
+   * mytest.root/mytest.script has a list of "commands" to run:
446cf2
        syntax:
446cf2
          # comment
446cf2
+         su
446cf2
          mv FILE FILE
446cf2
 	 cp FILE FILE
446cf2
 	 rm FILE
446cf2
 	 FILE must start with $B/, $S/, $I/, $L/, or /
446cf2
 	  (expands to build dir, source dir, install dir, library dir
446cf2
 	   (in container), or container's root)
446cf2
+       details:
446cf2
+         - '#': A comment.
446cf2
+         - 'su': Enables running test as root in the container.
446cf2
+         - 'mv': A minimal move files command.
446cf2
+         - 'cp': A minimal copy files command.
446cf2
+         - 'rm': A minimal remove files command.
446cf2
    * mytest.root/postclean.req causes fresh rsync (with delete) after
446cf2
      test if present
446cf2
 
446cf2
@@ -349,6 +356,7 @@ recursive_remove (char *path)
446cf2
 
446cf2
   switch (child) {
446cf2
   case -1:
446cf2
+    perror("fork");
446cf2
     FAIL_EXIT1 ("Unable to fork");
446cf2
   case 0:
446cf2
     /* Child.  */
446cf2
@@ -610,6 +618,47 @@ rsync (char *src, char *dest, int and_delete)
446cf2
 }
446cf2
 
446cf2
 
446cf2
+
446cf2
+/* See if we can detect what the user needs to do to get unshare
446cf2
+   support working for us.  */
446cf2
+void
446cf2
+check_for_unshare_hints (void)
446cf2
+{
446cf2
+  FILE *f;
446cf2
+  int i;
446cf2
+
446cf2
+  /* Default Debian Linux disables user namespaces, but allows a way
446cf2
+     to enable them.  */
446cf2
+  f = fopen ("/proc/sys/kernel/unprivileged_userns_clone", "r");
446cf2
+  if (f != NULL)
446cf2
+    {
446cf2
+      i = 99; /* Sentinel.  */
446cf2
+      fscanf (f, "%d", &i);
446cf2
+      if (i == 0)
446cf2
+	{
446cf2
+	  printf ("To enable test-container, please run this as root:\n");
446cf2
+	  printf ("  echo 1 > /proc/sys/kernel/unprivileged_userns_clone\n");
446cf2
+	}
446cf2
+      fclose (f);
446cf2
+      return;
446cf2
+    }
446cf2
+
446cf2
+  /* ALT Linux has an alternate way of doing the same.  */
446cf2
+  f = fopen ("/proc/sys/kernel/userns_restrict", "r");
446cf2
+  if (f != NULL)
446cf2
+    {
446cf2
+      i = 99; /* Sentinel.  */
446cf2
+      fscanf (f, "%d", &i);
446cf2
+      if (i == 1)
446cf2
+	{
446cf2
+	  printf ("To enable test-container, please run this as root:\n");
446cf2
+	  printf ("  echo 0 > /proc/sys/kernel/userns_restrict\n");
446cf2
+	}
446cf2
+      fclose (f);
446cf2
+      return;
446cf2
+    }
446cf2
+}
446cf2
+
446cf2
 int
446cf2
 main (int argc, char **argv)
446cf2
 {
446cf2
@@ -628,6 +677,8 @@ main (int argc, char **argv)
446cf2
 
446cf2
   uid_t original_uid;
446cf2
   gid_t original_gid;
446cf2
+  /* If set, the test runs as root instead of the user running the testsuite.  */
446cf2
+  int be_su = 0;
446cf2
   int UMAP;
446cf2
   int GMAP;
446cf2
   /* Used for "%lld %lld 1" so need not be large.  */
446cf2
@@ -857,6 +908,10 @@ main (int argc, char **argv)
446cf2
 	      {
446cf2
 		maybe_xunlink (the_words[1]);
446cf2
 	      }
446cf2
+	    else if (nt == 1 && strcmp (the_words[0], "su") == 0)
446cf2
+	      {
446cf2
+		be_su = 1;
446cf2
+	      }
446cf2
 	    else if (nt > 0 && the_words[0][0] != '#')
446cf2
 	      {
446cf2
 		printf ("\033[31minvalid [%s]\033[0m\n", the_words[0]);
446cf2
@@ -910,7 +965,12 @@ main (int argc, char **argv)
446cf2
       /* Older kernels may not support all the options, or security
446cf2
 	 policy may block this call.  */
446cf2
       if (errno == EINVAL || errno == EPERM)
446cf2
-	FAIL_UNSUPPORTED ("unable to unshare user/fs: %s", strerror (errno));
446cf2
+	{
446cf2
+	  int saved_errno = errno;
446cf2
+	  if (errno == EPERM)
446cf2
+	    check_for_unshare_hints ();
446cf2
+	  FAIL_UNSUPPORTED ("unable to unshare user/fs: %s", strerror (saved_errno));
446cf2
+	}
446cf2
       else
446cf2
 	FAIL_EXIT1 ("unable to unshare user/fs: %s", strerror (errno));
446cf2
     }
446cf2
@@ -981,7 +1041,7 @@ main (int argc, char **argv)
446cf2
     FAIL_EXIT1 ("can't write to /proc/self/uid_map\n");
446cf2
 
446cf2
   sprintf (tmp, "%lld %lld 1\n",
446cf2
-	   (long long) original_uid, (long long) original_uid);
446cf2
+	   (long long) (be_su ? 0 : original_uid), (long long) original_uid);
446cf2
   write (UMAP, tmp, strlen (tmp));
446cf2
   xclose (UMAP);
446cf2
 
446cf2
@@ -1002,7 +1062,7 @@ main (int argc, char **argv)
446cf2
     FAIL_EXIT1 ("can't write to /proc/self/gid_map\n");
446cf2
 
446cf2
   sprintf (tmp, "%lld %lld 1\n",
446cf2
-	   (long long) original_gid, (long long) original_gid);
446cf2
+	   (long long) (be_su ? 0 : original_gid), (long long) original_gid);
446cf2
   write (GMAP, tmp, strlen (tmp));
446cf2
   xclose (GMAP);
446cf2