[lxc-devel] [lxcfs/master] tree-wide: post-restructuring cleanup
brauner on Github
lxc-bot at linuxcontainers.org
Wed Feb 26 11:58:38 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 365 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200226/307b0b4b/attachment.bin>
-------------- next part --------------
From df447840d75729bc1a582f29e934ed9a29fdd4af Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Wed, 26 Feb 2020 10:29:53 +0100
Subject: [PATCH] tree-wide: post-restructuring cleanup
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
Makefile.am | 4 +-
bindings.c | 149 +++++++++++--------------------------------------
cgroup_fuse.c | 21 -------
lxcfs.c | 11 ++--
macro.h | 2 +-
proc_cpuview.c | 60 ++++++++++++--------
proc_cpuview.h | 12 +++-
proc_fuse.c | 16 +++---
proc_loadavg.c | 2 +-
proc_loadavg.h | 1 +
utils.c | 41 ++++++++++++++
utils.h | 4 ++
12 files changed, 142 insertions(+), 181 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 7ebc01a..41f3249 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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 \
diff --git a/bindings.c b/bindings.c
index f8970b0..1c737f0 100644
--- a/bindings.c
+++ b/bindings.c
@@ -55,17 +55,17 @@
/* 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 @@ pid_t lookup_initpid_in_store(pid_t qpid)
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];
@@ -785,7 +694,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)
diff --git a/cgroup_fuse.c b/cgroup_fuse.c
index e33a802..9f9325f 100644
--- a/cgroup_fuse.c
+++ b/cgroup_fuse.c
@@ -1160,27 +1160,6 @@ int cg_open(const char *path, struct fuse_file_info *fi)
#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 05cd380..ce5a24b 100644
--- 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 5db599c..dd66f24 100644
--- 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 */
diff --git a/proc_cpuview.c b/proc_cpuview.c
index f63b9a4..f159439 100644
--- a/proc_cpuview.c
+++ b/proc_cpuview.c
@@ -46,15 +46,9 @@
#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 @@ static struct cg_proc_stat *new_proc_stat_node(struct cpuacct_usage *usage, int
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 @@ bool init_cpuview()
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]);
- }
}
diff --git a/proc_cpuview.h b/proc_cpuview.h
index 91948a3..1e4fda5 100644
--- a/proc_cpuview.h
+++ b/proc_cpuview.h
@@ -21,7 +21,12 @@
#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 */
diff --git a/proc_fuse.c b/proc_fuse.c
index 37e9a1a..623a415 100644
--- a/proc_fuse.c
+++ b/proc_fuse.c
@@ -44,8 +44,10 @@
#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)
{
diff --git a/proc_loadavg.c b/proc_loadavg.c
index b0f6910..3947f31 100644
--- a/proc_loadavg.c
+++ b/proc_loadavg.c
@@ -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;
diff --git a/proc_loadavg.h b/proc_loadavg.h
index a4b0f13..5319620 100644
--- a/proc_loadavg.h
+++ b/proc_loadavg.h
@@ -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 595ab6c..f1c16a1 100644
--- 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 3cba952..6c874b4 100644
--- a/utils.h
+++ b/utils.h
@@ -14,10 +14,12 @@
#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 */
More information about the lxc-devel
mailing list