Gentoo Archives: gentoo-commits

From: Mike Frysinger <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/sandbox:master commit in: tests/, src/
Date: Fri, 29 Oct 2021 05:37:50
Message-Id: 1635479400.9a026d957ffc18ab4f4f7d069f4373ddf190eca9.vapier@gentoo
1 commit: 9a026d957ffc18ab4f4f7d069f4373ddf190eca9
2 Author: Mike Frysinger <vapier <AT> gentoo <DOT> org>
3 AuthorDate: Fri Oct 29 03:50:00 2021 +0000
4 Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org>
5 CommitDate: Fri Oct 29 03:50:00 2021 +0000
6 URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=9a026d95
7
8 sandbox: change interface to make it easier to pass thru
9
10 The sandbox command line is passed to a shell for execution. This can
11 be a bit awkward to quote right if you weren't expecting it, and even
12 if you were. Change the default behavior to be more like `env` where
13 the arguments, as they are, get passed through and run. If people want
14 the old shell behavior, they can use the -c option akin to `bash -c`.
15
16 Bug: https://bugs.gentoo.org/265907
17 Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org>
18
19 src/options.c | 8 +++++++-
20 src/sandbox.c | 46 +++++++++++++++++++++++++++-------------------
21 src/sandbox.h | 1 +
22 tests/git-bisector.sh | 15 +++++++++++++--
23 tests/local.at | 2 +-
24 5 files changed, 49 insertions(+), 23 deletions(-)
25
26 diff --git a/src/options.c b/src/options.c
27 index 03cffda..64cd750 100644
28 --- a/src/options.c
29 +++ b/src/options.c
30 @@ -20,6 +20,7 @@ int opt_use_ns_sysv = -1;
31 int opt_use_ns_time = -1;
32 int opt_use_ns_user = -1;
33 int opt_use_ns_uts = -1;
34 +bool opt_use_bash = false;
35
36 static const struct {
37 const char *name;
38 @@ -76,7 +77,7 @@ static void show_version(void)
39 exit(0);
40 }
41
42 -#define PARSE_FLAGS "+hV"
43 +#define PARSE_FLAGS "+chV"
44 #define a_argument required_argument
45 static struct option const long_opts[] = {
46 {"ns-on", no_argument, &opt_use_namespaces, true},
47 @@ -99,6 +100,7 @@ static struct option const long_opts[] = {
48 {"ns-user-off", no_argument, &opt_use_ns_user, false},
49 {"ns-uts-on", no_argument, &opt_use_ns_uts, true},
50 {"ns-uts-off", no_argument, &opt_use_ns_uts, false},
51 + {"bash", no_argument, NULL, 'c'},
52 {"help", no_argument, NULL, 'h'},
53 {"version", no_argument, NULL, 'V'},
54 {"run-configure", no_argument, NULL, 0x800},
55 @@ -125,6 +127,7 @@ static const char * const opts_help[] = {
56 "Disable the use of user namespaces",
57 "Enable the use of UTS (hostname/uname) namespaces",
58 "Disable the use of UTS (hostname/uname) namespaces",
59 + "Run command through bash shell",
60 "Print this help and exit",
61 "Print version and exit",
62 "Run local sandbox configure in same way and exit (developer only)",
63 @@ -201,6 +204,9 @@ void parseargs(int argc, char *argv[])
64
65 while ((i = getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
66 switch (i) {
67 + case 'c':
68 + opt_use_bash = true;
69 + break;
70 case 'V':
71 show_version();
72 case 'h':
73
74 diff --git a/src/sandbox.c b/src/sandbox.c
75 index 7e8a769..7d6b03f 100644
76 --- a/src/sandbox.c
77 +++ b/src/sandbox.c
78 @@ -175,7 +175,9 @@ static int spawn_shell(char *argv_bash[], char **env, int debug)
79
80 /* Child's process */
81 if (0 == child_pid) {
82 - int ret = execve(argv_bash[0], argv_bash, env);
83 + /* Would be nice if execvpe were in POSIX. */
84 + environ = env;
85 + int ret = execvp(argv_bash[0], argv_bash);
86 sb_pwarn("failed to exec child");
87 _exit(ret);
88 } else if (child_pid < 0) {
89 @@ -258,25 +260,31 @@ int main(int argc, char **argv)
90 goto oom_error;
91
92 /* Setup bash argv */
93 - str_list_add_item_copy(argv_bash, "/bin/bash", oom_error);
94 - str_list_add_item_copy(argv_bash, "-rcfile", oom_error);
95 - str_list_add_item_copy(argv_bash, sandbox_info.sandbox_rc, oom_error);
96 - if (argc >= 2) {
97 - int i;
98 - size_t cmdlen;
99 - char *cmd = NULL;
100 -
101 - str_list_add_item_copy(argv_bash, run_str, oom_error);
102 - str_list_add_item_copy(argv_bash, argv[1], oom_error);
103 - cmdlen = strlen(argv_bash[4]);
104 - for (i = 2; i < argc; i++) {
105 - size_t arglen = strlen(argv[i]);
106 - argv_bash[4] = xrealloc(argv_bash[4], cmdlen + arglen + 2);
107 - argv_bash[4][cmdlen] = ' ';
108 - memcpy(argv_bash[4] + cmdlen + 1, argv[i], arglen);
109 - cmdlen += arglen + 1;
110 - argv_bash[4][cmdlen] = '\0';
111 + if (opt_use_bash || argc == 1) {
112 + str_list_add_item_copy(argv_bash, "/bin/bash", oom_error);
113 + str_list_add_item_copy(argv_bash, "-rcfile", oom_error);
114 + str_list_add_item_copy(argv_bash, sandbox_info.sandbox_rc, oom_error);
115 + if (argc >= 2) {
116 + int i;
117 + size_t cmdlen;
118 + char *cmd = NULL;
119 +
120 + str_list_add_item_copy(argv_bash, run_str, oom_error);
121 + str_list_add_item_copy(argv_bash, argv[1], oom_error);
122 + cmdlen = strlen(argv_bash[4]);
123 + for (i = 2; i < argc; i++) {
124 + size_t arglen = strlen(argv[i]);
125 + argv_bash[4] = xrealloc(argv_bash[4], cmdlen + arglen + 2);
126 + argv_bash[4][cmdlen] = ' ';
127 + memcpy(argv_bash[4] + cmdlen + 1, argv[i], arglen);
128 + cmdlen += arglen + 1;
129 + argv_bash[4][cmdlen] = '\0';
130 + }
131 }
132 + } else {
133 + int i;
134 + for (i = 1; i < argc; ++i)
135 + str_list_add_item_copy(argv_bash, argv[i], oom_error);
136 }
137
138 #ifdef HAVE_PRCTL
139
140 diff --git a/src/sandbox.h b/src/sandbox.h
141 index 7e5b575..cdc1b9e 100644
142 --- a/src/sandbox.h
143 +++ b/src/sandbox.h
144 @@ -52,5 +52,6 @@ extern int opt_use_ns_sysv;
145 extern int opt_use_ns_time;
146 extern int opt_use_ns_user;
147 extern int opt_use_ns_uts;
148 +extern bool opt_use_bash;
149
150 #endif
151
152 diff --git a/tests/git-bisector.sh b/tests/git-bisector.sh
153 index c45db6e..b64dff6 100755
154 --- a/tests/git-bisector.sh
155 +++ b/tests/git-bisector.sh
156 @@ -21,10 +21,21 @@ make="make -s -j"
157 cat << EOF > git-run.sh
158 #!/bin/sh
159 ./autogen.sh
160 -./configure -q -C $(sandbox -V | tail -n1)
161 +# Newer versions of sandbox can run configure for us.
162 +# Should drop old support around Jan 2023.
163 +if sandbox --help | grep -q -e--run-configure ; then
164 + sandbox --run-configure -q -C
165 +else
166 + ./configure -q -C $(sandbox -V | tail -n1)
167 +fi
168 ${make} clean
169 ${make}
170 -./src/sandbox.sh . ./data/sandbox.bashrc \; . ./git-run-sandbox.sh
171 +opt=
172 +# Older versions of sandbox implied -c all the time.
173 +if ./src/sandbox.sh --help | grep -q -e--bash ; then
174 + opt="-c"
175 +fi
176 +./src/sandbox.sh ${opt} . ./data/sandbox.bashrc \; . ./git-run-sandbox.sh
177 EOF
178 chmod a+rx git-run.sh
179
180
181 diff --git a/tests/local.at b/tests/local.at
182 index 95db774..028961d 100644
183 --- a/tests/local.at
184 +++ b/tests/local.at
185 @@ -6,7 +6,7 @@ dnl due to the default PM test env having that predict.
186 m4_defun([SB_RUN],[\
187 env \
188 SANDBOX_LOG="$PWD/sandbox.log" \
189 - sandbox.sh \
190 + sandbox.sh -c \
191 addpredict / \; \
192 addwrite "${PWD%/*}" \; \
193 set -x \; \