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