]> git.proxmox.com Git - mirror_lxcfs.git/commitdiff
tree-wide: post-restructuring cleanup
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 26 Feb 2020 09:29:53 +0000 (10:29 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Wed, 26 Feb 2020 12:00:38 +0000 (13:00 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
12 files changed:
Makefile.am
bindings.c
cgroup_fuse.c
lxcfs.c
macro.h
proc_cpuview.c
proc_cpuview.h
proc_fuse.c
proc_loadavg.c
proc_loadavg.h
utils.c
utils.h

index 7ebc01ac86a806362be6d2ce2a5114355c92111b..41f3249dfbaac9d6c4061ee363eb52816a9d758f 100644 (file)
@@ -13,7 +13,7 @@ AM_LDFLAGS = $(FUSE_LIBS) -pthread
 AM_CFLAGS += -DRUNTIME_PATH=\"$(RUNTIME_PATH)\"
 
 liblxcfs_la_SOURCES = bindings.c bindings.h \
-                     cgroup_fuse.c cgroup_fuse.h
+                     cgroup_fuse.c cgroup_fuse.h \
                      cgroups/cgfsng.c \
                      cgroups/cgroup.c cgroups/cgroup.h \
                      cgroups/cgroup2_devices.c cgroups/cgroup2_devices.h \
@@ -30,7 +30,7 @@ liblxcfs_la_CFLAGS = $(AM_CFLAGS)
 liblxcfs_la_LDFLAGS = $(AM_CFLAGS) -module -avoid-version -shared
 
 liblxcfstest_la_SOURCES = bindings.c bindings.h \
-                         cgroup_fuse.c cgroup_fuse.h
+                         cgroup_fuse.c cgroup_fuse.h \
                          cgroups/cgfsng.c \
                          cgroups/cgroup.c cgroups/cgroup.h \
                          cgroups/cgroup2_devices.c cgroups/cgroup2_devices.h \
index f8970b0ea1ea53a16237e45790da067fc74a1d90..7bf5d287d2edeba16f4680c40926739a295677b9 100644 (file)
 
 /* Define pivot_root() if missing from the C library */
 #ifndef HAVE_PIVOT_ROOT
-static int pivot_root(const char * new_root, const char * put_old)
+static int pivot_root(const char *new_root, const char *put_old)
 {
 #ifdef __NR_pivot_root
-return syscall(__NR_pivot_root, new_root, put_old);
+       return syscall(__NR_pivot_root, new_root, put_old);
 #else
-errno = ENOSYS;
-return -1;
+       errno = ENOSYS;
+       return -1;
 #endif
 }
 #else
-extern int pivot_root(const char * new_root, const char * put_old);
+extern int pivot_root(const char *new_root, const char *put_old);
 #endif
 
 /*
@@ -96,14 +96,14 @@ struct pidns_init_store {
 
 static struct pidns_init_store *pidns_hash_table[PIDNS_HASH_SIZE];
 static pthread_mutex_t pidns_store_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 static void lock_mutex(pthread_mutex_t *l)
 {
        int ret;
 
-       if ((ret = pthread_mutex_lock(l)) != 0) {
-               lxcfs_error("returned:%d %s\n", ret, strerror(ret));
-               exit(1);
-       }
+       ret = pthread_mutex_lock(l);
+       if (ret)
+               log_exit("%s - returned %d\n", strerror(ret), ret);
 }
 
 struct cgroup_ops *cgroup_ops;
@@ -112,10 +112,9 @@ static void unlock_mutex(pthread_mutex_t *l)
 {
        int ret;
 
-       if ((ret = pthread_mutex_unlock(l)) != 0) {
-               lxcfs_error("returned:%d %s\n", ret, strerror(ret));
-               exit(1);
-       }
+       ret = pthread_mutex_unlock(l);
+       if (ret)
+               log_exit("%s - returned %d\n", strerror(ret), ret);
 }
 
 static void store_lock(void)
@@ -267,52 +266,22 @@ static struct pidns_init_store *lookup_verify_initpid(struct stat *sb)
        return NULL;
 }
 
-struct cgfs_files {
-       char *name;
-       uint32_t uid, gid;
-       uint32_t mode;
-};
-
-static void print_subsystems(void)
+static int send_creds_clone_wrapper(void *arg)
 {
-       int i = 0;
-
-       fprintf(stderr, "mount namespace: %d\n", cgroup_ops->mntns_fd);
-       fprintf(stderr, "hierarchies:\n");
-       for (struct hierarchy **h = cgroup_ops->hierarchies; h && *h; h++, i++) {
-               __do_free char *controllers = lxc_string_join(",", (const char **)(*h)->controllers, false);
-               fprintf(stderr, " %2d: fd: %3d: %s\n", i, (*h)->fd, controllers ?: "");
-       }
-}
-
-bool cgfs_param_exist(const char *controller, const char *cgroup, const char *file)
-{
-       int ret, cfd;
-       size_t len;
-       char *fnam;
-
-       cfd = get_cgroup_fd(controller);
-       if (cfd < 0)
-               return false;
-
-       /* Make sure we pass a relative path to *at() family of functions.
-        * . + /cgroup + / + file + \0
-        */
-       len = strlen(cgroup) + strlen(file) + 3;
-       fnam = alloca(len);
-       ret = snprintf(fnam, len, "%s%s/%s", dot_or_empty(cgroup), cgroup, file);
-       if (ret < 0 || (size_t)ret >= len)
-               return false;
+       struct ucred cred;
+       char v;
+       int sock = *(int *)arg;
 
-       return (faccessat(cfd, fnam, F_OK, 0) == 0);
+       /* we are the child */
+       cred.uid = 0;
+       cred.gid = 0;
+       cred.pid = 1;
+       v = '1';
+       if (send_creds(sock, &cred, v, true) != SEND_CREDS_OK)
+               return 1;
+       return 0;
 }
 
-#define SEND_CREDS_OK 0
-#define SEND_CREDS_NOTSK 1
-#define SEND_CREDS_FAIL 2
-static int wait_for_pid(pid_t pid);
-static int send_creds_clone_wrapper(void *arg);
-
 /*
  * clone a task which switches to @task's namespace and writes '1'.
  * over a unix sock so we can read the task's reaper's pid in our
@@ -355,21 +324,6 @@ static void write_task_init_pid_exit(int sock, pid_t target)
        }
 }
 
-static int send_creds_clone_wrapper(void *arg) {
-       struct ucred cred;
-       char v;
-       int sock = *(int *)arg;
-
-       /* we are the child */
-       cred.uid = 0;
-       cred.gid = 0;
-       cred.pid = 1;
-       v = '1';
-       if (send_creds(sock, &cred, v, true) != SEND_CREDS_OK)
-               return 1;
-       return 0;
-}
-
 static pid_t get_init_pid_for_task(pid_t task)
 {
        int sock[2];
@@ -432,52 +386,6 @@ out:
        return answer;
 }
 
-static int wait_for_pid(pid_t pid)
-{
-       int status, ret;
-
-       if (pid <= 0)
-               return -1;
-
-again:
-       ret = waitpid(pid, &status, 0);
-       if (ret == -1) {
-               if (errno == EINTR)
-                       goto again;
-               return -1;
-       }
-       if (ret != pid)
-               goto again;
-       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
-               return -1;
-       return 0;
-}
-
-#define INITSCOPE "/init.scope"
-void prune_init_slice(char *cg)
-{
-       char *point;
-       size_t cg_len = strlen(cg), initscope_len = strlen(INITSCOPE);
-
-       if (cg_len < initscope_len)
-               return;
-
-       point = cg + cg_len - initscope_len;
-       if (strcmp(point, INITSCOPE) == 0) {
-               if (point == cg)
-                       *(point+1) = '\0';
-               else
-                       *point = '\0';
-       }
-}
-
-struct pid_ns_clone_args {
-       int *cpipe;
-       int sock;
-       pid_t tpid;
-       int (*wrapped) (int, pid_t); // pid_from_ns or pid_to_ns
-};
-
 /*
  * Functions needed to setup cgroups in the __constructor__.
  */
@@ -756,6 +664,7 @@ static bool cgfs_setup_controllers(void)
 static void __attribute__((constructor)) lxcfs_init(void)
 {
        __do_close_prot_errno int init_ns = -EBADF;
+       int i = 0;
        char *cret;
        char cwd[MAXPATHLEN];
 
@@ -769,6 +678,7 @@ static void __attribute__((constructor)) lxcfs_init(void)
                log_exit("Failed to preserve initial mount namespace");
 
        cret = getcwd(cwd, MAXPATHLEN);
+       if (!cret)
                log_exit("%s - Could not retrieve current working directory", strerror(errno));
 
        /* This function calls unshare(CLONE_NEWNS) our initial mount namespace
@@ -785,7 +695,13 @@ static void __attribute__((constructor)) lxcfs_init(void)
        if (!init_cpuview())
                log_exit("Failed to init CPU view");
 
-       print_subsystems();
+       fprintf(stderr, "mount namespace: %d\n", cgroup_ops->mntns_fd);
+       fprintf(stderr, "hierarchies:\n");
+
+       for (struct hierarchy **h = cgroup_ops->hierarchies; h && *h; h++, i++) {
+               __do_free char *controllers = lxc_string_join(",", (const char **)(*h)->controllers, false);
+               fprintf(stderr, " %2d: fd: %3d: %s\n", i, (*h)->fd, controllers ?: "");
+       }
 }
 
 static void __attribute__((destructor)) lxcfs_exit(void)
index e33a8027c71cc05174ab9815468eb7bd7823fe90..9f9325fa3ac87cee0594aa5b49531d9891777113 100644 (file)
@@ -1160,27 +1160,6 @@ out:
 
 #define POLLIN_SET ( EPOLLIN | EPOLLHUP | EPOLLRDHUP )
 
-static int wait_for_pid(pid_t pid)
-{
-       int status, ret;
-
-       if (pid <= 0)
-               return -1;
-
-again:
-       ret = waitpid(pid, &status, 0);
-       if (ret == -1) {
-               if (errno == EINTR)
-                       goto again;
-               return -1;
-       }
-       if (ret != pid)
-               goto again;
-       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
-               return -1;
-       return 0;
-}
-
 /*
  * pid_to_ns - reads pids from a ucred over a socket, then writes the
  * int value back over the socket.  This shifts the pid from the
diff --git a/lxcfs.c b/lxcfs.c
index 05cd380b7cd658d9cb5c9c971fb4ee516ef85a20..ce5a24be72945a89d4e6cb0bb765443b748bf6e4 100644 (file)
--- a/lxcfs.c
+++ b/lxcfs.c
@@ -39,7 +39,7 @@
 #include <linux/limits.h>
 
 #include "bindings.h"
-#include "config.h" // for VERSION
+#include "config.h"
 
 void *dlopen_handle;
 
@@ -1094,8 +1094,8 @@ int main(int argc, char *argv[])
         */
        int nargs = 5, cnt = 0;
        char *newargv[6];
-
        struct lxcfs_opts *opts;
+
        opts = malloc(sizeof(struct lxcfs_opts));
        if (opts == NULL) {
                fprintf(stderr, "Error allocating memory for options.\n");
@@ -1107,12 +1107,11 @@ int main(int argc, char *argv[])
        swallow_arg(&argc, argv, "-s");
        swallow_arg(&argc, argv, "-f");
        debug = swallow_arg(&argc, argv, "-d");
-       if (swallow_arg(&argc, argv, "-l")) {
+       if (swallow_arg(&argc, argv, "-l"))
                load_use = true;
-       }
-       if (swallow_arg(&argc, argv, "-u")) {
+       if (swallow_arg(&argc, argv, "-u"))
                opts->swap_off = true;
-       }
+
        if (swallow_option(&argc, argv, "-o", &v)) {
                /* Parse multiple values */
                for (; (token = strtok_r(v, ",", &saveptr)); v = NULL) {
diff --git a/macro.h b/macro.h
index 5db599c032395ee150faf7a8d290b81201a4b430..dd66f24b72972b30ed665e4650b42cd257b86ad6 100644 (file)
--- a/macro.h
+++ b/macro.h
@@ -92,7 +92,7 @@
 #define log_exit(format, ...)                           \
        ({                                              \
                fprintf(stderr, format, ##__VA_ARGS__); \
-               exit(EXIT_FAILURE);                     \
+               _exit(EXIT_FAILURE);                    \
        })
 
 #endif /* __LXCFS_MACRO_H */
index f63b9a4e22efa03b989f0a9dce79b3f40f75c61c..f159439967929a77889c9a949a9d42df5606bde3 100644 (file)
 #include "cgroups/cgroup.h"
 #include "cgroups/cgroup_utils.h"
 #include "memory_utils.h"
+#include "proc_loadavg.h"
 #include "utils.h"
 
-struct cpuacct_usage {
-       uint64_t user;
-       uint64_t system;
-       uint64_t idle;
-       bool online;
-};
-
 /* Data for CPU view */
 struct cg_proc_stat {
        char *cg;
@@ -138,6 +132,15 @@ static bool expand_proc_stat_node(struct cg_proc_stat *node, int cpu_count)
        return true;
 }
 
+static void free_proc_stat_node(struct cg_proc_stat *node)
+{
+       pthread_mutex_destroy(&node->lock);
+       free_disarm(node->cg);
+       free_disarm(node->usage);
+       free_disarm(node->view);
+       free_disarm(node);
+}
+
 static struct cg_proc_stat *add_proc_stat_node(struct cg_proc_stat *new_node)
 {
        int hash = calc_hash(new_node->cg) % CPUVIEW_HASH_SIZE;
@@ -233,6 +236,29 @@ err:
        return NULL;
 }
 
+static bool cgfs_param_exist(const char *controller, const char *cgroup,
+                            const char *file)
+{
+       int ret, cfd;
+       size_t len;
+       char *fnam;
+
+       cfd = get_cgroup_fd(controller);
+       if (cfd < 0)
+               return false;
+
+       /* Make sure we pass a relative path to *at() family of functions.
+        * . + /cgroup + / + file + \0
+        */
+       len = strlen(cgroup) + strlen(file) + 3;
+       fnam = alloca(len);
+       ret = snprintf(fnam, len, "%s%s/%s", dot_or_empty(cgroup), cgroup, file);
+       if (ret < 0 || (size_t)ret >= len)
+               return false;
+
+       return (faccessat(cfd, fnam, F_OK, 0) == 0);
+}
+
 static struct cg_proc_stat *prune_proc_stat_list(struct cg_proc_stat *node)
 {
        struct cg_proc_stat *first = NULL, *prev, *tmp;
@@ -454,7 +480,7 @@ static double exact_cpu_count(const char *cg)
  * Return the maximum number of visible CPUs based on CPU quotas.
  * If there is no quota set, zero is returned.
  */
-static int max_cpu_count(const char *cg)
+int max_cpu_count(const char *cg)
 {
        int rv, nprocs;
        int64_t cfs_quota, cfs_period;
@@ -1129,7 +1155,7 @@ static bool cpuview_init_head(struct cg_proc_stat_head **head)
        return true;
 }
 
-bool init_cpuview()
+bool init_cpuview(void)
 {
        int i;
 
@@ -1152,15 +1178,6 @@ err:
        return false;
 }
 
-static void free_proc_stat_node(struct cg_proc_stat *node)
-{
-       pthread_mutex_destroy(&node->lock);
-       free_disarm(node->cg);
-       free_disarm(node->usage);
-       free_disarm(node->view);
-       free_disarm(node);
-}
-
 static void cpuview_free_head(struct cg_proc_stat_head *head)
 {
        struct cg_proc_stat *node, *tmp;
@@ -1182,12 +1199,9 @@ static void cpuview_free_head(struct cg_proc_stat_head *head)
        free_disarm(head);
 }
 
-void free_cpuview()
+void free_cpuview(void)
 {
-       int i;
-
-       for (i = 0; i < CPUVIEW_HASH_SIZE; i++) {
+       for (int i = 0; i < CPUVIEW_HASH_SIZE; i++)
                if (proc_stat_history[i])
                        cpuview_free_head(proc_stat_history[i]);
-       }
 }
index 91948a38aaf7cbcf237b3f9c157fec7abd5c746a..1e4fda548800bb3f41b3d42e105b06081a714e96 100644 (file)
 #include "config.h"
 #include "macro.h"
 
-struct cpuacct_usage;
+struct cpuacct_usage {
+       uint64_t user;
+       uint64_t system;
+       uint64_t idle;
+       bool online;
+};
 
 extern int cpuview_proc_stat(const char *cg, const char *cpuset,
                             struct cpuacct_usage *cg_cpu_usage,
@@ -31,8 +36,9 @@ extern int proc_cpuinfo_read(char *buf, size_t size, off_t offset,
                             struct fuse_file_info *fi);
 extern int read_cpuacct_usage_all(char *cg, char *cpuset,
                                  struct cpuacct_usage **return_usage, int *size);
-extern bool init_cpuview();
-extern void free_cpuview();
+extern bool init_cpuview(void);
+extern void free_cpuview(void);
+extern int max_cpu_count(const char *cg);
 
 #endif /* __LXCFS_PROC_CPUVIEW_FUSE_H */
 
index 37e9a1ad75157a593ccdc8c0b68687d34c785361..623a4150331ab67c2e2074d4cd75cb7664edf697 100644 (file)
 #include "cgroup_fuse.h"
 #include "cgroups/cgroup.h"
 #include "cgroups/cgroup_utils.h"
+#include "cpuset_parse.h"
 #include "memory_utils.h"
 #include "proc_loadavg.h"
+#include "proc_cpuview.h"
 #include "utils.h"
 
 int proc_getattr(const char *path, struct stat *sb)
@@ -209,6 +211,13 @@ static unsigned long get_min_memlimit(const char *cgroup, bool swap)
        return retlimit;
 }
 
+static bool startswith(const char *line, const char *pref)
+{
+       if (strncmp(line, pref, strlen(pref)) == 0)
+               return true;
+       return false;
+}
+
 static int proc_swaps_read(char *buf, size_t size, off_t offset,
                           struct fuse_file_info *fi)
 {
@@ -997,13 +1006,6 @@ static void parse_memstat(int version,
        }
 }
 
-static bool startswith(const char *line, const char *pref)
-{
-       if (strncmp(line, pref, strlen(pref)) == 0)
-               return true;
-       return false;
-}
-
 static int proc_meminfo_read(char *buf, size_t size, off_t offset,
                             struct fuse_file_info *fi)
 {
index b0f6910f96269fa3c6ba87a9c6650c7543ee15d2..3947f3149e67de4e7494c347b51dbc69617501a5 100644 (file)
@@ -144,7 +144,7 @@ static void insert_node(struct load_node **n, int locate)
        pthread_rwlock_unlock(&load_hash[locate].rilock);
 }
 
-static int calc_hash(const char *name)
+int calc_hash(const char *name)
 {
        unsigned int hash = 0;
        unsigned int x = 0;
index a4b0f13e7b4f5b21c85d01cfd24f597adef15946..531962054ffdcbfbba996cf334fb778eaf6dd60e 100644 (file)
@@ -27,6 +27,7 @@ extern int proc_loadavg_read(char *buf, size_t size, off_t offset,
                             struct fuse_file_info *fi);
 extern pthread_t load_daemon(int load_use);
 extern int stop_load_daemon(pthread_t pid);
+extern int calc_hash(const char *name);
 
 #endif /* __LXCFS_PROC_LOADAVG_FUSE_H */
 
diff --git a/utils.c b/utils.c
index 595ab6c3b5962d1eba3d7875ab16f5128fe253a7..f1c16a1a4e9955436a1639a4f807787fbebed4e3 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -16,6 +16,7 @@
 #include <inttypes.h>
 #include <sched.h>
 #include <stdarg.h>
+#include <stdio.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -24,6 +25,7 @@
 #include <sys/epoll.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 #include "bindings.h"
@@ -353,3 +355,42 @@ int read_file_fuse(const char *path, char *buf, size_t size, struct file_info *d
                d->cached = d->size - total_len;
        return total_len;
 }
+
+#define INITSCOPE "/init.scope"
+void prune_init_slice(char *cg)
+{
+       char *point;
+       size_t cg_len = strlen(cg), initscope_len = strlen(INITSCOPE);
+
+       if (cg_len < initscope_len)
+               return;
+
+       point = cg + cg_len - initscope_len;
+       if (strcmp(point, INITSCOPE) == 0) {
+               if (point == cg)
+                       *(point+1) = '\0';
+               else
+                       *point = '\0';
+       }
+}
+
+int wait_for_pid(pid_t pid)
+{
+       int status, ret;
+
+       if (pid <= 0)
+               return -1;
+
+again:
+       ret = waitpid(pid, &status, 0);
+       if (ret == -1) {
+               if (errno == EINTR)
+                       goto again;
+               return -1;
+       }
+       if (ret != pid)
+               goto again;
+       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+               return -1;
+       return 0;
+}
diff --git a/utils.h b/utils.h
index 3cba95263d19e715e9cd8d9872ea37b7f7ced12c..6c874b457863c51627881ba474765712effd83c6 100644 (file)
--- a/utils.h
+++ b/utils.h
 #define _FILE_OFFSET_BITS 64
 
 #include <fuse.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/un.h>
+#include <sys/syscall.h>
 #include <unistd.h>
 
 #include "config.h"
@@ -41,5 +43,7 @@ extern int send_creds(int sock, struct ucred *cred, char v, bool pingfirst);
 extern bool wait_for_sock(int sock, int timeout);
 extern int read_file_fuse(const char *path, char *buf, size_t size,
                          struct file_info *d);
+extern void prune_init_slice(char *cg);
+extern int wait_for_pid(pid_t pid);
 
 #endif /* __LXCFS_UTILS_H */