]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/nsenter.c
nsenter: Properly spell significant in a comment.
[thirdparty/util-linux.git] / sys-utils / nsenter.c
CommitLineData
f8aa8e94
EB
1/*
2 * nsenter(1) - command-line interface for setns(2)
3 *
4 * Copyright (C) 2012-2013 Eric Biederman <ebiederm@xmission.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <sys/types.h>
21#include <sys/wait.h>
22#include <dirent.h>
23#include <errno.h>
24#include <getopt.h>
25#include <sched.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
29
30#include "nls.h"
31#include "c.h"
32#include "closestream.h"
33
34#ifndef CLONE_NEWSNS
35# define CLONE_NEWNS 0x00020000
36#endif
37#ifndef CLONE_NEWUTS
38# define CLONE_NEWUTS 0x04000000
39#endif
40#ifndef CLONE_NEWIPC
41# define CLONE_NEWIPC 0x08000000
42#endif
43#ifndef CLONE_NEWNET
44# define CLONE_NEWNET 0x40000000
45#endif
46#ifndef CLONE_NEWUSER
47# define CLONE_NEWUSER 0x10000000
48#endif
49#ifndef CLONE_NEWPID
50# define CLONE_NEWPID 0x20000000
51#endif
52
53#ifndef HAVE_SETNS
54# include <sys/syscall.h>
55static int setns(int fd, int nstype)
56{
57 return syscall(SYS_setns, fd, nstype);
58}
59#endif /* HAVE_SETNS */
60
61static struct namespace_file{
62 int nstype;
63 char *name;
64 int fd;
65} namespace_files[] = {
ebbc87cd 66 /* Careful the order is significant in this array.
f8aa8e94
EB
67 *
68 * The user namespace comes first, so that it is entered
69 * first. This gives an unprivileged user the potential to
70 * enter the other namespaces.
71 */
72 { .nstype = CLONE_NEWUSER, .name = "ns/user", .fd = -1 },
73 { .nstype = CLONE_NEWIPC, .name = "ns/ipc", .fd = -1 },
74 { .nstype = CLONE_NEWUTS, .name = "ns/uts", .fd = -1 },
75 { .nstype = CLONE_NEWNET, .name = "ns/net", .fd = -1 },
76 { .nstype = CLONE_NEWPID, .name = "ns/pid", .fd = -1 },
77 { .nstype = CLONE_NEWNS, .name = "ns/mnt", .fd = -1 },
78 {}
79};
80
81static void usage(int status)
82{
83 FILE *out = status == EXIT_SUCCESS ? stdout : stderr;
84
85 fputs(USAGE_HEADER, out);
86 fprintf(out, _(" %s [options] <program> [args...]\n"),
87 program_invocation_short_name);
88
89 fputs(USAGE_OPTIONS, out);
90 fputs(_(" -t, --target <pid> target process to get namespaces from\n"
91 " -m, --mount [<file>] enter mount namespace\n"
92 " -u, --uts [<file>] enter UTS namespace (hostname etc)\n"
93 " -i, --ipc [<file>] enter System V IPC namespace\n"
94 " -n, --net [<file>] enter network namespace\n"
95 " -p, --pid [<file>] enter pid namespace\n"
96 " -U, --user [<file>] enter user namespace\n"
97 " -e, --exec don't fork before exec'ing <program>\n"
98 " -r, --root [<dir>] set the root directory\n"
99 " -w, --wd [<dir>] set the working directory\n"), out);
100 fputs(USAGE_SEPARATOR, out);
101 fputs(USAGE_HELP, out);
102 fputs(USAGE_VERSION, out);
103 fprintf(out, USAGE_MAN_TAIL("nsenter(1)"));
104
105 exit(status);
106}
107
108static pid_t namespace_target_pid = 0;
109static int root_fd = -1;
110static int wd_fd = -1;
111
112static void open_target_fd(int *fd, const char *type, char *path)
113{
114 char pathbuf[PATH_MAX];
115
116 if (!path && namespace_target_pid) {
117 snprintf(pathbuf, sizeof(pathbuf), "/proc/%u/%s",
118 namespace_target_pid, type);
119 path = pathbuf;
120 }
121 if (!path)
122 err(EXIT_FAILURE, _("No filename and no target pid supplied for %s"),
123 type);
124
125 if (*fd >= 0)
126 close(*fd);
127
128 *fd = open(path, O_RDONLY);
129 if (*fd < 0)
130 err(EXIT_FAILURE, _("open of '%s' failed"), path);
131}
132
133static void open_namespace_fd(int nstype, char *path)
134{
135 struct namespace_file *nsfile;
136
137 for (nsfile = namespace_files; nsfile->nstype; nsfile++) {
138 if (nstype != nsfile->nstype)
139 continue;
140
141 open_target_fd(&nsfile->fd, nsfile->name, path);
142 return;
143 }
144 /* This should never happen */
145 err(EXIT_FAILURE, "Unrecognized namespace type");
146}
147
c9515f86
EB
148static void continue_as_child(void)
149{
150 pid_t child = fork();
151 int status;
152 pid_t ret;
153
154 if (child < 0)
155 err(EXIT_FAILURE, _("fork failed"));
156
157 /* Only the child returns */
158 if (child == 0)
159 return;
160
161 for (;;) {
162 ret = waitpid(child, &status, WUNTRACED);
163 if ((ret == child) && (WIFSTOPPED(status))) {
164 /* The child suspended so suspend us as well */
165 kill(getpid(), SIGSTOP);
166 kill(child, SIGCONT);
167 } else {
168 break;
169 }
170 }
171 /* Return the child's exit code if possible */
172 if (WIFEXITED(status)) {
173 exit(WEXITSTATUS(status));
174 }
175 else if (WIFSIGNALED(status)) {
176 kill(getpid(), WTERMSIG(status));
177 }
178 exit(EXIT_FAILURE);
179}
180
f8aa8e94
EB
181int main(int argc, char *argv[])
182{
183 static const struct option longopts[] = {
184 { "help", no_argument, NULL, 'h' },
185 { "version", no_argument, NULL, 'V'},
186 { "target", required_argument, NULL, 't' },
187 { "mount", optional_argument, NULL, 'm' },
188 { "uts", optional_argument, NULL, 'u' },
189 { "ipc", optional_argument, NULL, 'i' },
190 { "net", optional_argument, NULL, 'n' },
191 { "pid", optional_argument, NULL, 'p' },
192 { "user", optional_argument, NULL, 'U' },
193 { "exec", no_argument, NULL, 'e' },
194 { "root", optional_argument, NULL, 'r' },
195 { "wd", optional_argument, NULL, 'w' },
196 { NULL, 0, NULL, 0 }
197 };
198
199 struct namespace_file *nsfile;
200 int do_fork = 0;
201 char *end;
202 int c;
203
204 setlocale(LC_MESSAGES, "");
205 bindtextdomain(PACKAGE, LOCALEDIR);
206 textdomain(PACKAGE);
207 atexit(close_stdout);
208
209 while((c = getopt_long(argc, argv, "hVt:m::u::i::n::p::U::er::w::", longopts, NULL)) != -1) {
210 switch(c) {
211 case 'h':
212 usage(EXIT_SUCCESS);
213 case 'V':
214 printf(UTIL_LINUX_VERSION);
215 return EXIT_SUCCESS;
216 case 't':
217 errno = 0;
218 namespace_target_pid = strtoul(optarg, &end, 10);
219 if (!*optarg || (*optarg && *end) || errno != 0) {
220 err(EXIT_FAILURE,
221 _("Pid '%s' is not a valid number"),
222 optarg);
223 }
224 break;
225 case 'm':
226 open_namespace_fd(CLONE_NEWNS, optarg);
227 break;
228 case 'u':
229 open_namespace_fd(CLONE_NEWUTS, optarg);
230 break;
231 case 'i':
232 open_namespace_fd(CLONE_NEWIPC, optarg);
233 break;
234 case 'n':
235 open_namespace_fd(CLONE_NEWNET, optarg);
236 break;
237 case 'p':
238 do_fork = 1;
239 open_namespace_fd(CLONE_NEWPID, optarg);
240 break;
241 case 'U':
242 open_namespace_fd(CLONE_NEWUSER, optarg);
243 break;
244 case 'e':
245 do_fork = 0;
246 break;
247 case 'r':
248 open_target_fd(&root_fd, "root", optarg);
249 break;
250 case 'w':
251 open_target_fd(&wd_fd, "cwd", optarg);
252 break;
253 default:
254 usage(EXIT_FAILURE);
255 }
256 }
257
258 if(optind >= argc)
259 usage(EXIT_FAILURE);
260
261 /*
262 * Now that we know which namespaces we want to enter, enter them.
263 */
264 for (nsfile = namespace_files; nsfile->nstype; nsfile++) {
265 if (nsfile->fd < 0)
266 continue;
267 if (setns(nsfile->fd, nsfile->nstype))
268 err(EXIT_FAILURE, _("setns of '%s' failed"),
269 nsfile->name);
270 close(nsfile->fd);
271 nsfile->fd = -1;
272 }
273
274 /* Remember the current working directory if I'm not changing it */
275 if (root_fd >= 0 && wd_fd < 0) {
276 wd_fd = open(".", O_RDONLY);
277 if (wd_fd < 0)
278 err(EXIT_FAILURE, _("open of . failed"));
279 }
280
281 /* Change the root directory */
282 if (root_fd >= 0) {
283 if (fchdir(root_fd) < 0)
284 err(EXIT_FAILURE, _("fchdir to root_fd failed"));
285
286 if (chroot(".") < 0)
287 err(EXIT_FAILURE, _("chroot failed"));
288
289 close(root_fd);
290 root_fd = -1;
291 }
292
293 /* Change the working directory */
294 if (wd_fd >= 0) {
295 if (fchdir(wd_fd) < 0)
296 err(EXIT_FAILURE, _("fchdir to wd_fd failed"));
297
298 close(wd_fd);
299 wd_fd = -1;
300 }
301
c9515f86
EB
302 if (do_fork)
303 continue_as_child();
f8aa8e94
EB
304
305 execvp(argv[optind], argv + optind);
306
307 err(EXIT_FAILURE, _("exec %s failed"), argv[optind]);
308}