From patchwork Tue Jan 28 20:38:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 1230648 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-109006-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha1 header.s=default header.b=arSGM2C2; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LCtIeMIr; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 486dkN0z53z9sNT for ; Wed, 29 Jan 2020 07:38:51 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:in-reply-to:date:message-id :mime-version:content-type:content-transfer-encoding; q=dns; s= default; b=yJhvJpawal8M7KkSRjRywjvOjIY/TqCgSRihDdhxRwqVx/NCtkzAn ja34kawWLLtgDWRkOMCAruaePPsJ6SJ2OgAyBcz4sSHrpSRwPgVLuouneGQM3Agz 8OAR8NtotHqz0cw9RBA4NLzl0m1oWhk9X91egu4TW0z6uTa9q55f/w= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:in-reply-to:date:message-id :mime-version:content-type:content-transfer-encoding; s=default; bh=e3SbQmeWUndcrCiYReDSohp/0kI=; b=arSGM2C2Lhd8r/f+TjG+G4vDg8pv bL/7DjkeDgqayfjjz32oklX+PiPLn4gsfhAQls1Pn59tEWhQTMsnAEQ+Row9kY04 I5Vt9VCxe8XZy4LrxCqi4v4HHWTZJsW0V5qwfOleY1189IG9412RiO+lKB5aeqOx LKbL46We+oxAY7U= Received: (qmail 120012 invoked by alias); 28 Jan 2020 20:38:46 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 120001 invoked by uid 89); 28 Jan 2020 20:38:45 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=pipes, cwd X-HELO: us-smtp-delivery-1.mimecast.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580243922; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to; bh=0LbyqKVgG2m8BGJXVQ0/npBiSQvWBduYqINAw01e0Rw=; b=LCtIeMIrMUc0XXZOYm/6oAErEjmoFpMyUblsCkQORS1hqkiDpwbFvT9WcO6Hdg08xiz5KR WWQ5wI/jF87onjALBD5of8Pqf+l419NhhVZV698VCzWay7qGAsiLiaMMmD5Eet8awvwJRn rPIIJ60y3HWOiE3kvJw2zT+hbnUhDps= From: DJ Delorie To: libc-alpha@sourceware.org Subject: V3 test-container: ability to specify exec path In-Reply-To: <87r219fzrs.fsf@oldenburg2.str.redhat.com> (message from Florian Weimer on Thu, 12 Dec 2019 14:18:31 +0100) Date: Tue, 28 Jan 2020 15:38:37 -0500 Message-ID: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com * Fixed Florian's comments from 12 Dec. * Fixed new_child_name argv[0] bug * Fixed prog name in usage (was "containerize ...") From 1904ed5a5a34dafcb8a8a9810317ffc78494a0ab Mon Sep 17 00:00:00 2001 From: DJ Delorie Date: Fri, 13 Dec 2019 13:37:30 -0500 Subject: test-container: add exec, cwd exec [optional_argv_0] copies test binary to specified location and runs it from there. If the second argument is provided, that will be used for argv[0] cwd attempts to chdir(directory) before running test Note: "cwd" not "cd" as it takes effect just before the test binary runs, not when it's encountered in the script, so it can't be used as a path shortcut like "cd" would imply. cleanup: use xstrdup() instead of strdup() Reviewed-by: Carlos O'Donell diff --git a/support/test-container.c b/support/test-container.c index 4c58254558..e970eb2c18 100644 --- a/support/test-container.c +++ b/support/test-container.c @@ -95,6 +95,8 @@ int verbose = 0; mv FILE FILE cp FILE FILE rm FILE + cwd PATH + exec FILE FILE must start with $B/, $S/, $I/, $L/, or / (expands to build dir, source dir, install dir, library dir (in container), or container's root) @@ -104,6 +106,8 @@ int verbose = 0; - 'mv': A minimal move files command. - 'cp': A minimal copy files command. - 'rm': A minimal remove files command. + - 'cwd': set test working directory + - 'exec': change test binary location (may end in /) * mytest.root/postclean.req causes fresh rsync (with delete) after test if present @@ -147,7 +151,7 @@ maybe_xmkdir (const char *path, mode_t mode) } /* Temporarily concatenate multiple strings into one. Allows up to 10 - temporary results; use strdup () if you need them to be + temporary results; use xstrdup () if you need them to be permanent. */ static char * concat (const char *str, ...) @@ -670,11 +674,13 @@ main (int argc, char **argv) char *new_objdir_path; char *new_srcdir_path; char **new_child_proc; + char *new_child_exec; char *command_root; char *command_base; char *command_basename; char *so_base; int do_postclean = 0; + char *change_cwd = NULL; int pipes[2]; char pid_buf[20]; @@ -701,7 +707,7 @@ main (int argc, char **argv) if (argc < 2) { - fprintf (stderr, "Usage: containerize \n"); + fprintf (stderr, "Usage: test-container \n"); exit (1); } @@ -746,12 +752,13 @@ main (int argc, char **argv) } } - pristine_root_path = strdup (concat (support_objdir_root, + pristine_root_path = xstrdup (concat (support_objdir_root, "/testroot.pristine", NULL)); - new_root_path = strdup (concat (support_objdir_root, + new_root_path = xstrdup (concat (support_objdir_root, "/testroot.root", NULL)); new_cwd_path = get_current_dir_name (); new_child_proc = argv + 1; + new_child_exec = argv[1]; lock_fd = open (concat (pristine_root_path, "/lock.fd", NULL), O_CREAT | O_TRUNC | O_RDWR, 0666); @@ -778,10 +785,10 @@ main (int argc, char **argv) command_root = concat (support_srcdir_root, argv[1] + strlen (support_objdir_root), ".root", NULL); - command_root = strdup (command_root); + command_root = xstrdup (command_root); /* This cuts off the ".root" we appended above. */ - command_base = strdup (command_root); + command_base = xstrdup (command_root); command_base[strlen (command_base) - 5] = 0; /* This is the basename of the test we're running. */ @@ -792,7 +799,7 @@ main (int argc, char **argv) ++command_basename; /* Shared object base directory. */ - so_base = strdup (argv[1]); + so_base = xstrdup (argv[1]); if (strrchr (so_base, '/') != NULL) strrchr (so_base, '/')[1] = 0; @@ -806,9 +813,9 @@ main (int argc, char **argv) && S_ISDIR (st.st_mode)) rsync (command_root, new_root_path, 0); - new_objdir_path = strdup (concat (new_root_path, + new_objdir_path = xstrdup (concat (new_root_path, support_objdir_root, NULL)); - new_srcdir_path = strdup (concat (new_root_path, + new_srcdir_path = xstrdup (concat (new_root_path, support_srcdir_root, NULL)); /* new_cwd_path starts with '/' so no "/" needed between the two. */ @@ -868,7 +875,10 @@ main (int argc, char **argv) the_words[i] = concat (new_root_path, support_libdir_prefix, the_words[i] + 2, NULL); - else if (the_words[i][0] == '/') + /* "exec" and "cwd" use inside-root paths. */ + else if (strcmp (the_words[0], "exec") != 0 + && strcmp (the_words[0], "cwd") != 0 + && the_words[i][0] == '/') the_words[i] = concat (new_root_path, the_words[i], NULL); } @@ -912,13 +922,49 @@ main (int argc, char **argv) { maybe_xunlink (the_words[1]); } + else if (nt >= 2 && strcmp (the_words[0], "exec") == 0) + { + /* The first argument is the desired location and name + of the test binary as we wish to exec it; we will + copy the binary there. The second (optional) + argument is the value to pass as argv[0], it + defaults to the same as the first argument. */ + char *new_exec_path = the_words[1]; + + /* If the new exec path ends with a slash, that's the + * directory, and use the old test base name. */ + if (new_exec_path [strlen(new_exec_path) - 1] == '/') + new_exec_path = concat (new_exec_path, + basename (new_child_proc[0]), + NULL); + + + /* new_child_proc is in the build tree, so has the + same path inside the chroot as outside. The new + exec path is, by definition, relative to the + chroot. */ + copy_one_file (new_child_proc[0], concat (new_root_path, + new_exec_path, + NULL)); + + new_child_exec = xstrdup (new_exec_path); + if (the_words[2]) + new_child_proc[0] = xstrdup (the_words[2]); + else + new_child_proc[0] = new_child_exec; + } + else if (nt == 2 && strcmp (the_words[0], "cwd") == 0) + { + change_cwd = xstrdup (the_words[1]); + } else if (nt == 1 && strcmp (the_words[0], "su") == 0) { be_su = 1; } else if (nt > 0 && the_words[0][0] != '#') { - printf ("\033[31minvalid [%s]\033[0m\n", the_words[0]); + fprintf (stderr, "\033[31minvalid [%s]\033[0m\n", the_words[0]); + exit (1); } } fclose (f); @@ -1089,11 +1135,17 @@ main (int argc, char **argv) write (GMAP, tmp, strlen (tmp)); xclose (GMAP); + if (change_cwd) + { + if (chdir (change_cwd) < 0) + FAIL_EXIT1 ("Can't cd to %s inside container - ", change_cwd); + } + /* Now run the child. */ - execvp (new_child_proc[0], new_child_proc); + execvp (new_child_exec, new_child_proc); /* Or don't run the child? */ - FAIL_EXIT1 ("Unable to exec %s\n", new_child_proc[0]); + FAIL_EXIT1 ("Unable to exec %s\n", new_child_exec); /* Because gcc won't know error () never returns... */ exit (EXIT_UNSUPPORTED);