diff options
author | Stephen Hemminger <stephen@networkplumber.org> | 2013-07-12 08:43:23 -0700 |
---|---|---|
committer | Stephen Hemminger <stephen@networkplumber.org> | 2013-07-12 08:43:23 -0700 |
commit | a05f6511f5432257a5efdd8b369735228c167e94 (patch) | |
tree | ccce33f490706fe5f878878f317197a7bbf3aaf4 | |
parent | 1284fd3a81ee1e0c1b858292f030b0e53063bda0 (diff) |
netns: follow return value conventions of the rest of the code
The netns code was using EXIT_SUCCESS/EXIT_FAILURE but the rest of the ip
code used -1 explictly, so change to follow convention. Also, certain types
of errors like fork failure should abort a batch operation, rather than just
returning an error.
-rw-r--r-- | ip/ipnetns.c | 98 |
1 files changed, 48 insertions, 50 deletions
diff --git a/ip/ipnetns.c b/ip/ipnetns.c index d534fb69..5b911f76 100644 --- a/ip/ipnetns.c +++ b/ip/ipnetns.c @@ -65,7 +65,7 @@ static int usage(void) fprintf(stderr, " ip netns pids NAME\n"); fprintf(stderr, " ip netns exec NAME cmd ...\n"); fprintf(stderr, " ip netns monitor\n"); - return EXIT_FAILURE; + exit(-1); } int get_netns_fd(const char *name) @@ -90,7 +90,7 @@ static int netns_list(int argc, char **argv) dir = opendir(NETNS_RUN_DIR); if (!dir) - return EXIT_SUCCESS; + return 0; while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0) @@ -100,7 +100,7 @@ static int netns_list(int argc, char **argv) printf("%s\n", entry->d_name); } closedir(dir); - return EXIT_SUCCESS; + return 0; } static void bind_etc(const char *name) @@ -142,12 +142,13 @@ static int netns_exec(int argc, char **argv) if (argc < 1) { fprintf(stderr, "No netns name specified\n"); - return EXIT_FAILURE; + return -1; } if (argc < 2) { fprintf(stderr, "No command specified\n"); - return EXIT_FAILURE; + return -1; } + name = argv[0]; cmd = argv[1]; snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name); @@ -155,32 +156,33 @@ static int netns_exec(int argc, char **argv) if (netns < 0) { fprintf(stderr, "Cannot open network namespace \"%s\": %s\n", name, strerror(errno)); - return EXIT_FAILURE; + return -1; } + if (setns(netns, CLONE_NEWNET) < 0) { fprintf(stderr, "seting the network namespace \"%s\" failed: %s\n", name, strerror(errno)); - return EXIT_FAILURE; + return -1; } if (unshare(CLONE_NEWNS) < 0) { fprintf(stderr, "unshare failed: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } /* Don't let any mounts propogate back to the parent */ if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) { fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } /* Mount a version of /sys that describes the network namespace */ if (umount2("/sys", MNT_DETACH) < 0) { fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } if (mount(name, "/sys", "sysfs", 0, NULL) < 0) { fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno)); - return EXIT_FAILURE; + return -1; } /* Setup bind mounts for config files in /etc */ @@ -195,32 +197,28 @@ static int netns_exec(int argc, char **argv) pid = fork(); if (pid < 0) { perror("fork"); - return EXIT_FAILURE; + exit(1); } if (pid != 0) { /* Parent */ if (waitpid(pid, &status, 0) < 0) { perror("waitpid"); - return EXIT_FAILURE; + exit(1); } - if (WIFEXITED(status)) { - /* ip must returns the status of the child, - * but do_cmd() will add a minus to this, - * so let's add another one here to cancel it. - */ - return -WEXITSTATUS(status); - } + /* If child failed, propogate status */ + if (WIFEXITED(status)) + exit(WEXITSTATUS(status)); - return EXIT_FAILURE; + return 0; } } if (execvp(cmd, argv + 1) < 0) fprintf(stderr, "exec of \"%s\" failed: %s\n", cmd, strerror(errno)); - _exit(EXIT_FAILURE); + _exit(1); } static int is_pid(const char *str) @@ -244,11 +242,11 @@ static int netns_pids(int argc, char **argv) if (argc < 1) { fprintf(stderr, "No netns name specified\n"); - return EXIT_FAILURE; + return -1; } if (argc > 1) { fprintf(stderr, "extra arguments specified\n"); - return EXIT_FAILURE; + return -1; } name = argv[0]; @@ -257,18 +255,18 @@ static int netns_pids(int argc, char **argv) if (netns < 0) { fprintf(stderr, "Cannot open network namespace: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } if (fstat(netns, &netst) < 0) { fprintf(stderr, "Stat of netns failed: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } dir = opendir("/proc/"); if (!dir) { fprintf(stderr, "Open of /proc failed: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } while((entry = readdir(dir))) { char pid_net_path[MAXPATHLEN]; @@ -285,7 +283,7 @@ static int netns_pids(int argc, char **argv) } } closedir(dir); - return EXIT_SUCCESS; + return 0; } @@ -300,18 +298,18 @@ static int netns_identify(int argc, char **argv) if (argc < 1) { fprintf(stderr, "No pid specified\n"); - return EXIT_FAILURE; + return -1; } if (argc > 1) { fprintf(stderr, "extra arguments specified\n"); - return EXIT_FAILURE; + return -1; } pidstr = argv[0]; if (!is_pid(pidstr)) { fprintf(stderr, "Specified string '%s' is not a pid\n", pidstr); - return EXIT_FAILURE; + return -1; } snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr); @@ -319,22 +317,22 @@ static int netns_identify(int argc, char **argv) if (netns < 0) { fprintf(stderr, "Cannot open network namespace: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } if (fstat(netns, &netst) < 0) { fprintf(stderr, "Stat of netns failed: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } dir = opendir(NETNS_RUN_DIR); if (!dir) { /* Succeed treat a missing directory as an empty directory */ if (errno == ENOENT) - return EXIT_SUCCESS; + return 0; fprintf(stderr, "Failed to open directory %s:%s\n", NETNS_RUN_DIR, strerror(errno)); - return EXIT_FAILURE; + return -1; } while((entry = readdir(dir))) { @@ -358,7 +356,7 @@ static int netns_identify(int argc, char **argv) } } closedir(dir); - return EXIT_SUCCESS; + return 0; } @@ -369,7 +367,7 @@ static int netns_delete(int argc, char **argv) if (argc < 1) { fprintf(stderr, "No netns name specified\n"); - return EXIT_FAILURE; + return -1; } name = argv[0]; @@ -378,9 +376,9 @@ static int netns_delete(int argc, char **argv) if (unlink(netns_path) < 0) { fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n", netns_path, strerror(errno)); - return EXIT_FAILURE; + return -1; } - return EXIT_SUCCESS; + return 0; } static int netns_add(int argc, char **argv) @@ -400,7 +398,7 @@ static int netns_add(int argc, char **argv) if (argc < 1) { fprintf(stderr, "No netns name specified\n"); - return EXIT_FAILURE; + return -1; } name = argv[0]; @@ -420,14 +418,14 @@ static int netns_add(int argc, char **argv) if (errno != EINVAL || made_netns_run_dir_mount) { fprintf(stderr, "mount --make-shared %s failed: %s\n", NETNS_RUN_DIR, strerror(errno)); - return EXIT_FAILURE; + return -1; } /* Upgrade NETNS_RUN_DIR to a mount point */ if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND, NULL)) { fprintf(stderr, "mount --bind %s %s failed: %s\n", NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno)); - return EXIT_FAILURE; + return -1; } made_netns_run_dir_mount = 1; } @@ -437,7 +435,7 @@ static int netns_add(int argc, char **argv) if (fd < 0) { fprintf(stderr, "Cannot not create namespace file \"%s\": %s\n", netns_path, strerror(errno)); - return EXIT_FAILURE; + return -1; } close(fd); if (unshare(CLONE_NEWNET) < 0) { @@ -452,10 +450,10 @@ static int netns_add(int argc, char **argv) netns_path, strerror(errno)); goto out_delete; } - return EXIT_SUCCESS; + return 0; out_delete: netns_delete(argc, argv); - return EXIT_FAILURE; + return -1; } @@ -468,19 +466,19 @@ static int netns_monitor(int argc, char **argv) if (fd < 0) { fprintf(stderr, "inotify_init failed: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) { fprintf(stderr, "inotify_add_watch failed: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } for(;;) { ssize_t len = read(fd, buf, sizeof(buf)); if (len < 0) { fprintf(stderr, "read failed: %s\n", strerror(errno)); - return EXIT_FAILURE; + return -1; } for (event = (struct inotify_event *)buf; (char *)event < &buf[len]; @@ -491,7 +489,7 @@ static int netns_monitor(int argc, char **argv) printf("delete %s\n", event->name); } } - return EXIT_SUCCESS; + return 0; } int do_netns(int argc, char **argv) @@ -525,5 +523,5 @@ int do_netns(int argc, char **argv) return netns_monitor(argc-1, argv+1); fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv); - return EXIT_FAILURE; + exit(-1); } |