]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipnetns.c
Use one func to print timestamp from nlmsg
[mirror_iproute2.git] / ip / ipnetns.c
CommitLineData
0dc34c77
EB
1#define _ATFILE_SOURCE
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <sys/wait.h>
5#include <sys/inotify.h>
6#include <sys/mount.h>
7#include <sys/param.h>
8#include <sys/syscall.h>
9#include <stdio.h>
10#include <string.h>
11#include <sched.h>
12#include <fcntl.h>
13#include <dirent.h>
14#include <errno.h>
15#include <unistd.h>
9a7b3d91 16#include <ctype.h>
0dc34c77
EB
17
18#include "utils.h"
19#include "ip_common.h"
eb67e449 20#include "namespace.h"
0dc34c77 21
8e2d47dc 22static int usage(void)
0dc34c77
EB
23{
24 fprintf(stderr, "Usage: ip netns list\n");
25 fprintf(stderr, " ip netns add NAME\n");
26 fprintf(stderr, " ip netns delete NAME\n");
0948adc0 27 fprintf(stderr, " ip netns identify [PID]\n");
9a7b3d91 28 fprintf(stderr, " ip netns pids NAME\n");
0dc34c77
EB
29 fprintf(stderr, " ip netns exec NAME cmd ...\n");
30 fprintf(stderr, " ip netns monitor\n");
a05f6511 31 exit(-1);
0dc34c77
EB
32}
33
34int get_netns_fd(const char *name)
35{
36 char pathbuf[MAXPATHLEN];
37 const char *path, *ptr;
38
39 path = name;
40 ptr = strchr(name, '/');
41 if (!ptr) {
42 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
43 NETNS_RUN_DIR, name );
44 path = pathbuf;
45 }
46 return open(path, O_RDONLY);
47}
48
49static int netns_list(int argc, char **argv)
50{
51 struct dirent *entry;
52 DIR *dir;
53
54 dir = opendir(NETNS_RUN_DIR);
55 if (!dir)
a05f6511 56 return 0;
0dc34c77
EB
57
58 while ((entry = readdir(dir)) != NULL) {
59 if (strcmp(entry->d_name, ".") == 0)
60 continue;
61 if (strcmp(entry->d_name, "..") == 0)
62 continue;
63 printf("%s\n", entry->d_name);
64 }
65 closedir(dir);
a05f6511 66 return 0;
0dc34c77
EB
67}
68
0dc34c77
EB
69static int netns_exec(int argc, char **argv)
70{
71 /* Setup the proper environment for apps that are not netns
72 * aware, and execute a program in that environment.
73 */
eb67e449 74 const char *cmd;
0dc34c77
EB
75
76 if (argc < 1) {
77 fprintf(stderr, "No netns name specified\n");
a05f6511 78 return -1;
0dc34c77
EB
79 }
80 if (argc < 2) {
14645ec2 81 fprintf(stderr, "No command specified\n");
a05f6511 82 return -1;
0dc34c77 83 }
0dc34c77 84 cmd = argv[1];
a05f6511 85
eb67e449 86 if (netns_switch(argv[0]))
a05f6511 87 return -1;
0dc34c77 88
95592b47
J
89 fflush(stdout);
90
a3aa47a5 91 if (batch_mode) {
95592b47
J
92 int status;
93 pid_t pid;
94
95 pid = fork();
96 if (pid < 0) {
97 perror("fork");
a05f6511 98 exit(1);
95592b47
J
99 }
100
101 if (pid != 0) {
102 /* Parent */
103 if (waitpid(pid, &status, 0) < 0) {
104 perror("waitpid");
a05f6511 105 exit(1);
95592b47
J
106 }
107
3c61c01a
ND
108 if (WIFEXITED(status)) {
109 /* ip must return the status of the child,
110 * but do_cmd() will add a minus to this,
111 * so let's add another one here to cancel it.
112 */
113 return -WEXITSTATUS(status);
114 }
95592b47 115
3c61c01a 116 exit(1);
95592b47
J
117 }
118 }
119
0dc34c77 120 if (execvp(cmd, argv + 1) < 0)
14645ec2 121 fprintf(stderr, "exec of \"%s\" failed: %s\n",
0dc34c77 122 cmd, strerror(errno));
a05f6511 123 _exit(1);
0dc34c77
EB
124}
125
9a7b3d91
EB
126static int is_pid(const char *str)
127{
128 int ch;
129 for (; (ch = *str); str++) {
130 if (!isdigit(ch))
131 return 0;
132 }
133 return 1;
134}
135
136static int netns_pids(int argc, char **argv)
137{
138 const char *name;
139 char net_path[MAXPATHLEN];
140 int netns;
141 struct stat netst;
142 DIR *dir;
143 struct dirent *entry;
144
145 if (argc < 1) {
146 fprintf(stderr, "No netns name specified\n");
a05f6511 147 return -1;
9a7b3d91
EB
148 }
149 if (argc > 1) {
150 fprintf(stderr, "extra arguments specified\n");
a05f6511 151 return -1;
9a7b3d91
EB
152 }
153
154 name = argv[0];
155 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
156 netns = open(net_path, O_RDONLY);
157 if (netns < 0) {
158 fprintf(stderr, "Cannot open network namespace: %s\n",
159 strerror(errno));
a05f6511 160 return -1;
9a7b3d91
EB
161 }
162 if (fstat(netns, &netst) < 0) {
163 fprintf(stderr, "Stat of netns failed: %s\n",
164 strerror(errno));
a05f6511 165 return -1;
9a7b3d91
EB
166 }
167 dir = opendir("/proc/");
168 if (!dir) {
169 fprintf(stderr, "Open of /proc failed: %s\n",
170 strerror(errno));
a05f6511 171 return -1;
9a7b3d91
EB
172 }
173 while((entry = readdir(dir))) {
174 char pid_net_path[MAXPATHLEN];
175 struct stat st;
176 if (!is_pid(entry->d_name))
177 continue;
178 snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%s/ns/net",
179 entry->d_name);
180 if (stat(pid_net_path, &st) != 0)
181 continue;
182 if ((st.st_dev == netst.st_dev) &&
183 (st.st_ino == netst.st_ino)) {
184 printf("%s\n", entry->d_name);
185 }
186 }
187 closedir(dir);
a05f6511 188 return 0;
0612519e 189
9a7b3d91
EB
190}
191
192static int netns_identify(int argc, char **argv)
193{
194 const char *pidstr;
195 char net_path[MAXPATHLEN];
196 int netns;
197 struct stat netst;
198 DIR *dir;
199 struct dirent *entry;
200
201 if (argc < 1) {
0948adc0 202 pidstr = "self";
203 } else if (argc > 1) {
9a7b3d91 204 fprintf(stderr, "extra arguments specified\n");
a05f6511 205 return -1;
0948adc0 206 } else {
207 pidstr = argv[0];
208 if (!is_pid(pidstr)) {
209 fprintf(stderr, "Specified string '%s' is not a pid\n",
210 pidstr);
211 return -1;
212 }
9a7b3d91
EB
213 }
214
215 snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr);
216 netns = open(net_path, O_RDONLY);
217 if (netns < 0) {
218 fprintf(stderr, "Cannot open network namespace: %s\n",
219 strerror(errno));
a05f6511 220 return -1;
9a7b3d91
EB
221 }
222 if (fstat(netns, &netst) < 0) {
223 fprintf(stderr, "Stat of netns failed: %s\n",
224 strerror(errno));
a05f6511 225 return -1;
9a7b3d91
EB
226 }
227 dir = opendir(NETNS_RUN_DIR);
228 if (!dir) {
229 /* Succeed treat a missing directory as an empty directory */
230 if (errno == ENOENT)
a05f6511 231 return 0;
9a7b3d91
EB
232
233 fprintf(stderr, "Failed to open directory %s:%s\n",
234 NETNS_RUN_DIR, strerror(errno));
a05f6511 235 return -1;
9a7b3d91
EB
236 }
237
238 while((entry = readdir(dir))) {
239 char name_path[MAXPATHLEN];
240 struct stat st;
241
242 if (strcmp(entry->d_name, ".") == 0)
243 continue;
244 if (strcmp(entry->d_name, "..") == 0)
245 continue;
246
247 snprintf(name_path, sizeof(name_path), "%s/%s", NETNS_RUN_DIR,
248 entry->d_name);
249
250 if (stat(name_path, &st) != 0)
251 continue;
252
253 if ((st.st_dev == netst.st_dev) &&
254 (st.st_ino == netst.st_ino)) {
255 printf("%s\n", entry->d_name);
256 }
257 }
258 closedir(dir);
a05f6511 259 return 0;
0612519e 260
9a7b3d91
EB
261}
262
0dc34c77
EB
263static int netns_delete(int argc, char **argv)
264{
265 const char *name;
266 char netns_path[MAXPATHLEN];
267
268 if (argc < 1) {
269 fprintf(stderr, "No netns name specified\n");
a05f6511 270 return -1;
0dc34c77
EB
271 }
272
273 name = argv[0];
274 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
275 umount2(netns_path, MNT_DETACH);
276 if (unlink(netns_path) < 0) {
14645ec2 277 fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n",
0dc34c77 278 netns_path, strerror(errno));
a05f6511 279 return -1;
0dc34c77 280 }
a05f6511 281 return 0;
0dc34c77
EB
282}
283
c1cbb18a 284static int create_netns_dir(void)
285{
286 /* Create the base netns directory if it doesn't exist */
287 if (mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) {
288 if (errno != EEXIST) {
289 fprintf(stderr, "mkdir %s failed: %s\n",
290 NETNS_RUN_DIR, strerror(errno));
291 return -1;
292 }
293 }
294
295 return 0;
296}
297
0dc34c77
EB
298static int netns_add(int argc, char **argv)
299{
300 /* This function creates a new network namespace and
301 * a new mount namespace and bind them into a well known
302 * location in the filesystem based on the name provided.
303 *
304 * The mount namespace is created so that any necessary
305 * userspace tweaks like remounting /sys, or bind mounting
306 * a new /etc/resolv.conf can be shared between uers.
307 */
308 char netns_path[MAXPATHLEN];
309 const char *name;
223f4d8e 310 int fd;
58a3e827 311 int made_netns_run_dir_mount = 0;
0dc34c77
EB
312
313 if (argc < 1) {
314 fprintf(stderr, "No netns name specified\n");
a05f6511 315 return -1;
0dc34c77
EB
316 }
317 name = argv[0];
318
319 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
320
c1cbb18a 321 if (create_netns_dir())
322 return -1;
0dc34c77 323
d259f030 324 /* Make it possible for network namespace mounts to propagate between
58a3e827
EB
325 * mount namespaces. This makes it likely that a unmounting a network
326 * namespace file in one namespace will unmount the network namespace
327 * file in all namespaces allowing the network namespace to be freed
328 * sooner.
329 */
330 while (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) {
331 /* Fail unless we need to make the mount point */
332 if (errno != EINVAL || made_netns_run_dir_mount) {
333 fprintf(stderr, "mount --make-shared %s failed: %s\n",
334 NETNS_RUN_DIR, strerror(errno));
a05f6511 335 return -1;
58a3e827
EB
336 }
337
338 /* Upgrade NETNS_RUN_DIR to a mount point */
339 if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND, NULL)) {
340 fprintf(stderr, "mount --bind %s %s failed: %s\n",
341 NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno));
a05f6511 342 return -1;
58a3e827
EB
343 }
344 made_netns_run_dir_mount = 1;
345 }
346
0dc34c77 347 /* Create the filesystem state */
223f4d8e
EB
348 fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0);
349 if (fd < 0) {
55713c8c 350 fprintf(stderr, "Cannot create namespace file \"%s\": %s\n",
0dc34c77 351 netns_path, strerror(errno));
a05f6511 352 return -1;
0dc34c77 353 }
223f4d8e 354 close(fd);
0dc34c77 355 if (unshare(CLONE_NEWNET) < 0) {
14645ec2
KR
356 fprintf(stderr, "Failed to create a new network namespace \"%s\": %s\n",
357 name, strerror(errno));
0dc34c77
EB
358 goto out_delete;
359 }
360
361 /* Bind the netns last so I can watch for it */
362 if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) {
363 fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n",
364 netns_path, strerror(errno));
365 goto out_delete;
366 }
a05f6511 367 return 0;
0dc34c77
EB
368out_delete:
369 netns_delete(argc, argv);
a05f6511 370 return -1;
0dc34c77
EB
371}
372
373
374static int netns_monitor(int argc, char **argv)
375{
376 char buf[4096];
377 struct inotify_event *event;
378 int fd;
379 fd = inotify_init();
380 if (fd < 0) {
381 fprintf(stderr, "inotify_init failed: %s\n",
382 strerror(errno));
a05f6511 383 return -1;
0dc34c77 384 }
c1cbb18a 385
386 if (create_netns_dir())
387 return -1;
388
0dc34c77
EB
389 if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) {
390 fprintf(stderr, "inotify_add_watch failed: %s\n",
391 strerror(errno));
a05f6511 392 return -1;
0dc34c77
EB
393 }
394 for(;;) {
395 ssize_t len = read(fd, buf, sizeof(buf));
396 if (len < 0) {
397 fprintf(stderr, "read failed: %s\n",
398 strerror(errno));
a05f6511 399 return -1;
0dc34c77
EB
400 }
401 for (event = (struct inotify_event *)buf;
402 (char *)event < &buf[len];
403 event = (struct inotify_event *)((char *)event + sizeof(*event) + event->len)) {
404 if (event->mask & IN_CREATE)
405 printf("add %s\n", event->name);
406 if (event->mask & IN_DELETE)
407 printf("delete %s\n", event->name);
408 }
409 }
a05f6511 410 return 0;
0dc34c77
EB
411}
412
413int do_netns(int argc, char **argv)
414{
415 if (argc < 1)
416 return netns_list(0, NULL);
417
418 if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) ||
419 (matches(*argv, "lst") == 0))
420 return netns_list(argc-1, argv+1);
421
422 if (matches(*argv, "help") == 0)
8e2d47dc 423 return usage();
0dc34c77
EB
424
425 if (matches(*argv, "add") == 0)
426 return netns_add(argc-1, argv+1);
427
428 if (matches(*argv, "delete") == 0)
429 return netns_delete(argc-1, argv+1);
430
9a7b3d91
EB
431 if (matches(*argv, "identify") == 0)
432 return netns_identify(argc-1, argv+1);
433
434 if (matches(*argv, "pids") == 0)
435 return netns_pids(argc-1, argv+1);
436
0dc34c77
EB
437 if (matches(*argv, "exec") == 0)
438 return netns_exec(argc-1, argv+1);
439
440 if (matches(*argv, "monitor") == 0)
441 return netns_monitor(argc-1, argv+1);
442
443 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv);
a05f6511 444 exit(-1);
0dc34c77 445}