annotate toys/other/nsenter.c @ 1676:cbb1aca81eca draft

Make toy_exec() check if argc is in optargs and deal with it there so we don't need a separate xexec_optargs().
author Rob Landley <rob@landley.net>
date Sat, 07 Feb 2015 16:17:44 -0600
parents 435f91d71898
children 83c14a9cd0fe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1675
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* nsenter.c - Enter existing namespaces
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2014 andy Lutomirski <luto@amacapital.net>
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * No standard
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * unshare.c - run command in new context
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
8 *
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
9 * Copyright 2011 Rob Landley <rob@landley.net>
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
10 *
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
11 * No Standard
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
12 *
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
13
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
14 // Note: flags go in same order (right to left) for shared subset
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
15 USE_NSENTER(NEWTOY(nsenter, "<1F(no-fork)t#<1(target)i:(ipc);m:(mount);n:(net);p:(pid);u:(uts);U:(user);", TOYFLAG_USR|TOYFLAG_BIN))
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
16 USE_UNSHARE(NEWTOY(unshare, "<1^imnpuU", TOYFLAG_USR|TOYFLAG_BIN))
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
17
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
18 config UNSHARE
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
19 bool "unshare"
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
20 default y
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
21 depends on TOYBOX_CONTAINER
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
22 help
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
23 usage: unshare [-imnpuU] COMMAND...
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
24
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
25 Create new namespace(s) for this process and its children, so some
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
26 attribute is not shared with the parent process. This is part of
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
27 Linux Containers. Each process can have its own:
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
28
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
29 -i SysV IPC (message queues, semaphores, shared memory)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
30 -m Mount/unmount tree
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
31 -n Network address, sockets, routing, iptables
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
32 -p Process IDs and init
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
33 -u Host and domain names
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
34 -U UIDs, GIDs, capabilities
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
35
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
36 config NSENTER
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
37 bool "nsenter"
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
38 default n
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
39 help
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
40 usage: nsenter [-t pid] [-F] [-i] [-m] [-n] [-p] [-u] [-U] COMMAND...
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
41
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
42 Run COMMAND in a different set of namespaces.
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
43
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
44 -t PID to take namespaces from (--target)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
45 -F don't fork, even if -p is used (--no-fork)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
46
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
47 The namespaces to switch are:
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
48
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
49 -i SysV IPC: message queues, semaphores, shared memory (--ipc)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
50 -m Mount/unmount tree (--mnt)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
51 -n Network address, sockets, routing, iptables (--net)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
52 -p Process IDs and init, will fork unless -F is used (--pid)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
53 -u Host and domain names (--uts)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
54 -U UIDs, GIDs, capabilities (--user)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
55
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
56 If -t isn't specified, each namespace argument must provide a path
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
57 to a namespace file, ala "-i=/proc/$PID/ns/ipc"
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
58 */
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
59
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
60 #define FOR_nsenter
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
61 #include "toys.h"
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
62 #include <linux/sched.h>
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
63 int unshare(int flags);
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
64 int setns(int fd, int nstype);
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
65
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
66 GLOBALS(
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
67 char *nsnames[6];
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
68 long targetpid;
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
69 )
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
70
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
71 void unshare_main(void)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
72 {
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
73 unsigned flags[]={CLONE_NEWUSER, CLONE_NEWUTS, CLONE_NEWPID, CLONE_NEWNET,
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
74 CLONE_NEWNS, CLONE_NEWIPC}, f = 0;
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
75 int i, fd;
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
76
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
77 // Create new namespace(s)?
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
78 if (CFG_UNSHARE && toys.which->name[0]) {
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
79 for (i = 0; i<ARRAY_LEN(flags); i++)
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
80 if (toys.optflags & (1<<i)) f |= flags[i];
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
81
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
82 if (unshare(f)) perror_exit(0);
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
83
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
84 // Bind to existing namespace(s)?
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
85 } else if (CFG_NSENTER) {
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
86 char *nsnames = "user\0uts\0pid\0net\0mnt\0ipc";
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
87
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
88 for (i = 0; i<ARRAY_LEN(flags); i++) {
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
89 char *filename = TT.nsnames[i];
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
90
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
91 if (toys.optflags & (1<<i)) {
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
92 if (!filename || !*filename) {
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
93 if (!(toys.optflags & FLAG_t)) error_exit("need -t or =filename");
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
94 sprintf(toybuf, "/proc/%ld/ns/%s", TT.targetpid, nsnames);
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
95 filename = toybuf;
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
96 }
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
97
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
98 if (setns(fd = xopen(filename, O_RDONLY), flags[i]))
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
99 perror_exit("setns");
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
100 close(fd);
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
101 }
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
102 nsnames += strlen(nsnames)+1;
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
103 }
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
104
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
105 if ((toys.optflags & FLAG_p) && !(toys.optflags & FLAG_F)) {
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
106 pid_t pid = xfork();
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
107
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
108 if (pid) {
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
109 while (waitpid(pid, 0, 0) == -1 && errno == EINTR);
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
110 return;
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
111 }
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
112 }
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
113 }
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
114
1676
cbb1aca81eca Make toy_exec() check if argc is in optargs and deal with it there so we don't need a separate xexec_optargs().
Rob Landley <rob@landley.net>
parents: 1675
diff changeset
115 xexec(toys.optargs);
1675
435f91d71898 Merge unshare and nsenter (promoting and cleaning up nsenter).
Rob Landley <rob@landley.net>
parents:
diff changeset
116 }