The following pull request was submitted through Github. It can be accessed and reviewed at: https://github.com/lxc/lxcfs/pull/324
This e-mail was sent by the LXC bot, direct replies will not reach the author unless they happen to be subscribed to this list. === Description (from pull-request) === - Improve naming for {con,de}structor. - Fail when we can't setup cgroups. Signed-off-by: Christian Brauner <christian.brau...@ubuntu.com>
From 2243c5a9cb061f3409f350d6f358cbd41ab81d1f Mon Sep 17 00:00:00 2001 From: Christian Brauner <christian.brau...@ubuntu.com> Date: Mon, 24 Feb 2020 16:06:25 +0100 Subject: [PATCH] bindings: rework {con,de}structor - Improve naming for {con,de}structor. - Fail when we can't setup cgroups. Signed-off-by: Christian Brauner <christian.brau...@ubuntu.com> --- bindings.c | 48 ++++++++++++++++-------------------------------- macro.h | 6 ++++++ 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/bindings.c b/bindings.c index 9561b8c..4e2848c 100644 --- a/bindings.c +++ b/bindings.c @@ -6109,61 +6109,45 @@ static bool cgfs_setup_controllers(void) return true; } -static void __attribute__((constructor)) collect_and_mount_subsystems(void) +static void __attribute__((constructor)) lxcfs_init(void) { + __do_close_prot_errno int init_ns = -EBADF; char *cret; char cwd[MAXPATHLEN]; - int init_ns = -1; cgroup_ops = cgroup_init(); if (!cgroup_ops) - return; + log_exit("Failed to initialize cgroup support"); /* Preserve initial namespace. */ init_ns = preserve_mnt_ns(getpid()); - if (init_ns < 0) { - lxcfs_error("%s\n", "Failed to preserve initial mount namespace."); - goto out; - } + if (init_ns < 0) + log_exit("Failed to preserve initial mount namespace"); cret = getcwd(cwd, MAXPATHLEN); - if (!cret) - lxcfs_debug("Could not retrieve current working directory: %s.\n", strerror(errno)); + log_exit("%s - Could not retrieve current working directory", strerror(errno)); /* This function calls unshare(CLONE_NEWNS) our initial mount namespace * to privately mount lxcfs cgroups. */ - if (!cgfs_setup_controllers()) { - lxcfs_error("%s\n", "Failed to setup private cgroup mounts for lxcfs."); - goto out; - } + if (!cgfs_setup_controllers()) + log_exit("Failed to setup private cgroup mounts for lxcfs"); - if (setns(init_ns, 0) < 0) { - lxcfs_error("Failed to switch back to initial mount namespace: %s.\n", strerror(errno)); - goto out; - } + if (setns(init_ns, 0) < 0) + log_exit("%s - Failed to switch back to initial mount namespace", strerror(errno)); if (!cret || chdir(cwd) < 0) - lxcfs_debug("Could not change back to original working directory: %s.\n", strerror(errno)); + log_exit("%s - Could not change back to original working directory", strerror(errno)); - if (!init_cpuview()) { - lxcfs_error("%s\n", "failed to init CPU view"); - goto out; - } + if (!init_cpuview()) + log_exit("Failed to init CPU view"); print_subsystems(); - -out: - if (init_ns >= 0) - close(init_ns); } -static void __attribute__((destructor)) free_subsystems(void) +static void __attribute__((destructor)) lxcfs_exit(void) { lxcfs_debug("%s\n", "Running destructor for liblxcfs."); - - cgroup_exit(cgroup_ops); free_cpuview(); - - if (cgroup_mount_ns_fd >= 0) - close(cgroup_mount_ns_fd); + close_prot_errno_disarm(cgroup_mount_ns_fd); + cgroup_exit(cgroup_ops); } diff --git a/macro.h b/macro.h index 4ec3876..a149475 100644 --- a/macro.h +++ b/macro.h @@ -75,4 +75,10 @@ (__iterator = __it); \ __iterator = __it = strtok_r(NULL, __separators, &__p)) +#define log_exit(format, ...) \ + ({ \ + fprintf(stderr, format, ##__VA_ARGS__); \ + exit(EXIT_FAILURE); \ + }) + #endif /* __LXCFS_MACRO_H */
_______________________________________________ lxc-devel mailing list lxc-devel@lists.linuxcontainers.org http://lists.linuxcontainers.org/listinfo/lxc-devel