From c76f89f0638d5e57ba77c40f66b4bf7a4ab2b61b Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Sat, 26 Aug 2017 23:16:03 +0200 Subject: [PATCH] lxc-user-nic: add new {create,delete} subcommands Signed-off-by: Christian Brauner --- src/lxc/lxc_user_nic.c | 64 ++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/src/lxc/lxc_user_nic.c b/src/lxc/lxc_user_nic.c index c47f1f765..c0f9eea69 100644 --- a/src/lxc/lxc_user_nic.c +++ b/src/lxc/lxc_user_nic.c @@ -59,12 +59,17 @@ static void usage(char *me, bool fail) { - fprintf(stderr, "Usage: %s lxcpath name pid type bridge nicname\n", me); - fprintf(stderr, " nicname is the name to use inside the container\n"); - exit(fail ? 1 : 0); -} + fprintf(stderr, "Usage: %s create {lxcpath} {name} {pid} {type} " + "{bridge} {nicname}\n", me); + fprintf(stderr, "Usage: %s delete {lxcpath} {name} {pid} {type} " + "{bridge} {nicname}\n", me); + fprintf(stderr, "{nicname} is the name to use inside the container\n"); + + if (fail) + exit(EXIT_FAILURE); -static char *lxcpath, *lxcname; + exit(EXIT_SUCCESS); +} static int open_and_lock(char *path) { @@ -925,12 +930,38 @@ static bool may_access_netns(int pid) return may_access; } +struct user_nic_args { + char *cmd; + char *lxc_path; + char *lxc_name; + char *pid; + char *type; + char *link; + char *veth_name; +}; + int main(int argc, char *argv[]) { int fd, n, pid, ret; char *me; char *cnic = NULL, *nicname = NULL, *vethname = NULL; struct alloted_s *alloted = NULL; + struct user_nic_args args; + + if (argc < 7 || argc > 8) { + usage(argv[0], true); + exit(EXIT_FAILURE); + } + + memset(&args, 0, sizeof(struct user_nic_args)); + args.cmd = argv[1]; + args.lxc_path = argv[2]; + args.lxc_name = argv[3]; + args.pid = argv[4]; + args.type = argv[5]; + args.link = argv[6]; + if (argc >= 8) + args.veth_name = argv[7]; /* Set a sane env, because we are setuid-root. */ ret = clearenv(); @@ -951,18 +982,9 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (argc < 6) - usage(argv[0], true); - - if (argc >= 7) - vethname = argv[6]; - - lxcpath = argv[1]; - lxcname = argv[2]; - - ret = lxc_safe_int(argv[3], &pid); + ret = lxc_safe_int(args.pid, &pid); if (ret < 0) { - usernic_error("Could not read pid: %s\n", argv[1]); + usernic_error("Could not read pid: %s\n", args.pid); exit(EXIT_FAILURE); } @@ -982,10 +1004,10 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - n = get_alloted(me, argv[4], argv[5], &alloted); + n = get_alloted(me, args.type, args.link, &alloted); if (n > 0) - nicname = get_nic_if_avail(fd, alloted, pid, argv[4], - argv[5], n, &cnic); + nicname = get_nic_if_avail(fd, alloted, pid, args.type, + args.link, n, &cnic); close(fd); free_alloted(&alloted); @@ -995,7 +1017,7 @@ int main(int argc, char *argv[]) } /* Now rename the link. */ - ret = rename_in_ns(pid, cnic, &vethname); + ret = rename_in_ns(pid, cnic, &args.veth_name); if (ret < 0) { usernic_error("%s", "Failed to rename the link\n"); ret = lxc_netdev_delete_by_name(cnic); @@ -1006,7 +1028,7 @@ int main(int argc, char *argv[]) } /* Write the name of the interface pair to the stdout: eth0:veth9MT2L4 */ - fprintf(stdout, "%s:%s\n", vethname, nicname); + fprintf(stdout, "%s:%s\n", args.veth_name, nicname); free(nicname); exit(EXIT_SUCCESS); } -- 2.47.2