diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2016-04-02 18:06:24 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-04-02 18:06:24 +0200 |
commit | 8220399173cf8d25e37059cadac96ac30f94e82a (patch) | |
tree | ffe5ae4a783c8ddeceda15f57eae74ee69feebea /util-linux | |
parent | c87e81f9440278dd46a3eddd1e0f4773afd46a95 (diff) | |
download | busybox-w32-8220399173cf8d25e37059cadac96ac30f94e82a.tar.gz busybox-w32-8220399173cf8d25e37059cadac96ac30f94e82a.tar.bz2 busybox-w32-8220399173cf8d25e37059cadac96ac30f94e82a.zip |
nsenter,unshare: share common code; fix a bug of not closing all fds
function old new delta
xvfork_parent_waits_and_exits - 64 +64
exec_prog_or_SHELL - 39 +39
unshare_main 873 810 -63
nsenter_main 663 596 -67
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 0/2 up/down: 106/-130) Total: -27 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'util-linux')
-rw-r--r-- | util-linux/nsenter.c | 21 | ||||
-rw-r--r-- | util-linux/unshare.c | 19 |
2 files changed, 8 insertions, 32 deletions
diff --git a/util-linux/nsenter.c b/util-linux/nsenter.c index 9c1dabaa8..0dad595cd 100644 --- a/util-linux/nsenter.c +++ b/util-linux/nsenter.c | |||
@@ -230,7 +230,7 @@ int nsenter_main(int argc UNUSED_PARAM, char **argv) | |||
230 | ns->ns_nsfile8 + 3 /* skip over "ns/" */ | 230 | ns->ns_nsfile8 + 3 /* skip over "ns/" */ |
231 | ); | 231 | ); |
232 | } | 232 | } |
233 | /*close(ns_ctx->fd);*/ | 233 | close(ns_ctx->fd); /* should close fds, to not confuse exec'ed PROG */ |
234 | /*ns_ctx->fd = -1;*/ | 234 | /*ns_ctx->fd = -1;*/ |
235 | } | 235 | } |
236 | 236 | ||
@@ -244,13 +244,13 @@ int nsenter_main(int argc UNUSED_PARAM, char **argv) | |||
244 | } | 244 | } |
245 | xfchdir(root_fd); | 245 | xfchdir(root_fd); |
246 | xchroot("."); | 246 | xchroot("."); |
247 | /*close(root_fd);*/ | 247 | close(root_fd); |
248 | /*root_fd = -1;*/ | 248 | /*root_fd = -1;*/ |
249 | } | 249 | } |
250 | 250 | ||
251 | if (wd_fd >= 0) { | 251 | if (wd_fd >= 0) { |
252 | xfchdir(wd_fd); | 252 | xfchdir(wd_fd); |
253 | /*close(wd_fd);*/ | 253 | close(wd_fd); |
254 | /*wd_fd = -1;*/ | 254 | /*wd_fd = -1;*/ |
255 | } | 255 | } |
256 | 256 | ||
@@ -259,14 +259,7 @@ int nsenter_main(int argc UNUSED_PARAM, char **argv) | |||
259 | * explicitly requested by the user not to. | 259 | * explicitly requested by the user not to. |
260 | */ | 260 | */ |
261 | if (!(opts & OPT_nofork) && (opts & OPT_pid)) { | 261 | if (!(opts & OPT_nofork) && (opts & OPT_pid)) { |
262 | pid_t pid = xvfork(); | 262 | xvfork_parent_waits_and_exits(); |
263 | if (pid > 0) { | ||
264 | /* Parent */ | ||
265 | int exit_status = wait_for_exitstatus(pid); | ||
266 | if (WIFSIGNALED(exit_status)) | ||
267 | kill_myself_with_sig(WTERMSIG(exit_status)); | ||
268 | return WEXITSTATUS(exit_status); | ||
269 | } | ||
270 | /* Child continues */ | 263 | /* Child continues */ |
271 | } | 264 | } |
272 | 265 | ||
@@ -278,9 +271,5 @@ int nsenter_main(int argc UNUSED_PARAM, char **argv) | |||
278 | if (opts & OPT_setuid) | 271 | if (opts & OPT_setuid) |
279 | xsetuid(uid); | 272 | xsetuid(uid); |
280 | 273 | ||
281 | if (*argv) { | 274 | exec_prog_or_SHELL(argv); |
282 | BB_EXECVP_or_die(argv); | ||
283 | } | ||
284 | |||
285 | run_shell(getenv("SHELL"), /*login:*/ 1, NULL, NULL); | ||
286 | } | 275 | } |
diff --git a/util-linux/unshare.c b/util-linux/unshare.c index 2a5bea5a6..95a7cb647 100644 --- a/util-linux/unshare.c +++ b/util-linux/unshare.c | |||
@@ -281,7 +281,7 @@ int unshare_main(int argc UNUSED_PARAM, char **argv) | |||
281 | 281 | ||
282 | if (fdp.wr >= 0) { | 282 | if (fdp.wr >= 0) { |
283 | close(fdp.wr); /* Release child */ | 283 | close(fdp.wr); /* Release child */ |
284 | /*close(fdp.rd);*/ | 284 | close(fdp.rd); /* should close fd, to not confuse exec'ed PROG */ |
285 | } | 285 | } |
286 | 286 | ||
287 | if (need_mount) { | 287 | if (need_mount) { |
@@ -307,14 +307,7 @@ int unshare_main(int argc UNUSED_PARAM, char **argv) | |||
307 | * that'll become PID 1 in this new namespace. | 307 | * that'll become PID 1 in this new namespace. |
308 | */ | 308 | */ |
309 | if (opts & OPT_fork) { | 309 | if (opts & OPT_fork) { |
310 | pid_t pid = xfork(); | 310 | xvfork_parent_waits_and_exits(); |
311 | if (pid > 0) { | ||
312 | /* Parent */ | ||
313 | int exit_status = wait_for_exitstatus(pid); | ||
314 | if (WIFSIGNALED(exit_status)) | ||
315 | kill_myself_with_sig(WTERMSIG(exit_status)); | ||
316 | return WEXITSTATUS(exit_status); | ||
317 | } | ||
318 | /* Child continues */ | 311 | /* Child continues */ |
319 | } | 312 | } |
320 | 313 | ||
@@ -354,11 +347,5 @@ int unshare_main(int argc UNUSED_PARAM, char **argv) | |||
354 | mount_or_die("proc", proc_mnt_target, "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV); | 347 | mount_or_die("proc", proc_mnt_target, "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV); |
355 | } | 348 | } |
356 | 349 | ||
357 | if (argv[0]) { | 350 | exec_prog_or_SHELL(argv); |
358 | BB_EXECVP_or_die(argv); | ||
359 | } | ||
360 | /* unshare from util-linux 2.27.1, despite not documenting it, | ||
361 | * runs a login shell (argv0="-sh") if no PROG is given | ||
362 | */ | ||
363 | run_shell(getenv("SHELL"), /*login:*/ 1, NULL, NULL); | ||
364 | } | 351 | } |