446cf2
commit 4f79b3e2fb3eba003240ec38a0e68702b9a60b86
446cf2
Author: DJ Delorie <dj@redhat.com>
446cf2
Date:   Mon Feb 3 14:49:25 2020 -0500
446cf2
446cf2
    test-container: add exec, cwd
446cf2
    
446cf2
    exec <path_to_test_binary> [optional_argv_0]
446cf2
    
446cf2
      copies test binary to specified location and runs it from
446cf2
      there.  If the second argument is provided, that will
446cf2
      be used for argv[0]
446cf2
    
446cf2
    cwd <directory>
446cf2
    
446cf2
      attempts to chdir(directory) before running test
446cf2
    
446cf2
    Note: "cwd" not "cd" as it takes effect just before the
446cf2
    test binary runs, not when it's encountered in the script,
446cf2
    so it can't be used as a path shortcut like "cd" would imply.
446cf2
    
446cf2
    cleanup: use xstrdup() instead of strdup()
446cf2
    
446cf2
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
446cf2
446cf2
diff --git a/support/test-container.c b/support/test-container.c
446cf2
index 6503cea90309b9b0..9488ec7b4a824380 100644
446cf2
--- a/support/test-container.c
446cf2
+++ b/support/test-container.c
446cf2
@@ -95,6 +95,8 @@ int verbose = 0;
446cf2
          mv FILE FILE
446cf2
 	 cp FILE FILE
446cf2
 	 rm FILE
446cf2
+	 cwd PATH
446cf2
+	 exec 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
@@ -104,6 +106,8 @@ int verbose = 0;
446cf2
          - 'mv': A minimal move files command.
446cf2
          - 'cp': A minimal copy files command.
446cf2
          - 'rm': A minimal remove files command.
446cf2
+	 - 'cwd': set test working directory
446cf2
+	 - 'exec': change test binary location (may end in /)
446cf2
    * mytest.root/postclean.req causes fresh rsync (with delete) after
446cf2
      test if present
446cf2
 
446cf2
@@ -147,7 +151,7 @@ maybe_xmkdir (const char *path, mode_t mode)
446cf2
 }
446cf2
 
446cf2
 /* Temporarily concatenate multiple strings into one.  Allows up to 10
446cf2
-   temporary results; use strdup () if you need them to be
446cf2
+   temporary results; use xstrdup () if you need them to be
446cf2
    permanent.  */
446cf2
 static char *
446cf2
 concat (const char *str, ...)
446cf2
@@ -670,11 +674,13 @@ main (int argc, char **argv)
446cf2
   char *new_objdir_path;
446cf2
   char *new_srcdir_path;
446cf2
   char **new_child_proc;
446cf2
+  char *new_child_exec;
446cf2
   char *command_root;
446cf2
   char *command_base;
446cf2
   char *command_basename;
446cf2
   char *so_base;
446cf2
   int do_postclean = 0;
446cf2
+  char *change_cwd = NULL;
446cf2
 
446cf2
   int pipes[2];
446cf2
   char pid_buf[20];
446cf2
@@ -701,7 +707,7 @@ main (int argc, char **argv)
446cf2
 
446cf2
   if (argc < 2)
446cf2
     {
446cf2
-      fprintf (stderr, "Usage: containerize <program to run> <args...>\n");
446cf2
+      fprintf (stderr, "Usage: test-container <program to run> <args...>\n");
446cf2
       exit (1);
446cf2
     }
446cf2
 
446cf2
@@ -746,12 +752,13 @@ main (int argc, char **argv)
446cf2
 	}
446cf2
     }
446cf2
 
446cf2
-  pristine_root_path = strdup (concat (support_objdir_root,
446cf2
+  pristine_root_path = xstrdup (concat (support_objdir_root,
446cf2
 				       "/testroot.pristine", NULL));
446cf2
-  new_root_path = strdup (concat (support_objdir_root,
446cf2
+  new_root_path = xstrdup (concat (support_objdir_root,
446cf2
 				  "/testroot.root", NULL));
446cf2
   new_cwd_path = get_current_dir_name ();
446cf2
   new_child_proc = argv + 1;
446cf2
+  new_child_exec = argv[1];
446cf2
 
446cf2
   lock_fd = open (concat (pristine_root_path, "/lock.fd", NULL),
446cf2
 		 O_CREAT | O_TRUNC | O_RDWR, 0666);
446cf2
@@ -778,10 +785,10 @@ main (int argc, char **argv)
446cf2
     command_root = concat (support_srcdir_root,
446cf2
 			   argv[1] + strlen (support_objdir_root),
446cf2
 			   ".root", NULL);
446cf2
-  command_root = strdup (command_root);
446cf2
+  command_root = xstrdup (command_root);
446cf2
 
446cf2
   /* This cuts off the ".root" we appended above.  */
446cf2
-  command_base = strdup (command_root);
446cf2
+  command_base = xstrdup (command_root);
446cf2
   command_base[strlen (command_base) - 5] = 0;
446cf2
 
446cf2
   /* This is the basename of the test we're running.  */
446cf2
@@ -792,7 +799,7 @@ main (int argc, char **argv)
446cf2
     ++command_basename;
446cf2
 
446cf2
   /* Shared object base directory.  */
446cf2
-  so_base = strdup (argv[1]);
446cf2
+  so_base = xstrdup (argv[1]);
446cf2
   if (strrchr (so_base, '/') != NULL)
446cf2
     strrchr (so_base, '/')[1] = 0;
446cf2
 
446cf2
@@ -806,9 +813,9 @@ main (int argc, char **argv)
446cf2
       && S_ISDIR (st.st_mode))
446cf2
     rsync (command_root, new_root_path, 0);
446cf2
 
446cf2
-  new_objdir_path = strdup (concat (new_root_path,
446cf2
+  new_objdir_path = xstrdup (concat (new_root_path,
446cf2
 				    support_objdir_root, NULL));
446cf2
-  new_srcdir_path = strdup (concat (new_root_path,
446cf2
+  new_srcdir_path = xstrdup (concat (new_root_path,
446cf2
 				    support_srcdir_root, NULL));
446cf2
 
446cf2
   /* new_cwd_path starts with '/' so no "/" needed between the two.  */
446cf2
@@ -868,7 +875,10 @@ main (int argc, char **argv)
446cf2
 		  the_words[i] = concat (new_root_path,
446cf2
 					 support_libdir_prefix,
446cf2
 					 the_words[i] + 2, NULL);
446cf2
-		else if (the_words[i][0] == '/')
446cf2
+		/* "exec" and "cwd" use inside-root paths.  */
446cf2
+		else if (strcmp (the_words[0], "exec") != 0
446cf2
+			 && strcmp (the_words[0], "cwd") != 0
446cf2
+			 && the_words[i][0] == '/')
446cf2
 		  the_words[i] = concat (new_root_path,
446cf2
 					 the_words[i], NULL);
446cf2
 	      }
446cf2
@@ -912,13 +922,49 @@ main (int argc, char **argv)
446cf2
 	      {
446cf2
 		maybe_xunlink (the_words[1]);
446cf2
 	      }
446cf2
+	    else if (nt >= 2 && strcmp (the_words[0], "exec") == 0)
446cf2
+	      {
446cf2
+		/* The first argument is the desired location and name
446cf2
+		   of the test binary as we wish to exec it; we will
446cf2
+		   copy the binary there.  The second (optional)
446cf2
+		   argument is the value to pass as argv[0], it
446cf2
+		   defaults to the same as the first argument.  */
446cf2
+		char *new_exec_path = the_words[1];
446cf2
+
446cf2
+		/* If the new exec path ends with a slash, that's the
446cf2
+		 * directory, and use the old test base name.  */
446cf2
+		if (new_exec_path [strlen(new_exec_path) - 1] == '/')
446cf2
+		    new_exec_path = concat (new_exec_path,
446cf2
+					    basename (new_child_proc[0]),
446cf2
+					    NULL);
446cf2
+
446cf2
+
446cf2
+		/* new_child_proc is in the build tree, so has the
446cf2
+		   same path inside the chroot as outside.  The new
446cf2
+		   exec path is, by definition, relative to the
446cf2
+		   chroot.  */
446cf2
+		copy_one_file (new_child_proc[0],  concat (new_root_path,
446cf2
+							   new_exec_path,
446cf2
+							   NULL));
446cf2
+
446cf2
+		new_child_exec =  xstrdup (new_exec_path);
446cf2
+		if (the_words[2])
446cf2
+		  new_child_proc[0] = xstrdup (the_words[2]);
446cf2
+		else
446cf2
+		  new_child_proc[0] = new_child_exec;
446cf2
+	      }
446cf2
+	    else if (nt == 2 && strcmp (the_words[0], "cwd") == 0)
446cf2
+	      {
446cf2
+		change_cwd = xstrdup (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
+		fprintf (stderr, "\033[31minvalid [%s]\033[0m\n", the_words[0]);
446cf2
+		exit (1);
446cf2
 	      }
446cf2
 	  }
446cf2
 	fclose (f);
446cf2
@@ -1089,11 +1135,17 @@ main (int argc, char **argv)
446cf2
   write (GMAP, tmp, strlen (tmp));
446cf2
   xclose (GMAP);
446cf2
 
446cf2
+  if (change_cwd)
446cf2
+    {
446cf2
+      if (chdir (change_cwd) < 0)
446cf2
+	FAIL_EXIT1 ("Can't cd to %s inside container - ", change_cwd);
446cf2
+    }
446cf2
+
446cf2
   /* Now run the child.  */
446cf2
-  execvp (new_child_proc[0], new_child_proc);
446cf2
+  execvp (new_child_exec, new_child_proc);
446cf2
 
446cf2
   /* Or don't run the child?  */
446cf2
-  FAIL_EXIT1 ("Unable to exec %s\n", new_child_proc[0]);
446cf2
+  FAIL_EXIT1 ("Unable to exec %s\n", new_child_exec);
446cf2
 
446cf2
   /* Because gcc won't know error () never returns...  */
446cf2
   exit (EXIT_UNSUPPORTED);