[lxc-devel] [lxcfs/master] Revert "Merge pull request #272 from brauner/2019-02-19/compiler_base…

brauner on Github lxc-bot at linuxcontainers.org
Wed Apr 24 12:14:58 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 521 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190424/840e853b/attachment-0001.bin>
-------------- next part --------------
From 2a1a5c189d20e24b7c30abaec760d844194e02ef Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Wed, 24 Apr 2019 14:14:16 +0200
Subject: [PATCH] Revert "Merge pull request #272 from
 brauner/2019-02-19/compiler_based_housekeeping"

This reverts commit ac98f00cd89278e82a4cffe9075792d2070abe69, reversing
changes made to d8addb023a0e96b7d42e253703ba909bbc81dd26.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 Makefile.am    |   2 +-
 bindings.c     | 216 ++++++++++++++++------------
 lxcfs.c        | 381 ++++++++++++++++++++-----------------------------
 memory_utils.h |  87 -----------
 4 files changed, 280 insertions(+), 406 deletions(-)
 delete mode 100644 memory_utils.h

diff --git a/Makefile.am b/Makefile.am
index 2cfeb7b..e3060ef 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -25,7 +25,7 @@ sodir=$(libdir)
 lxcfs_LTLIBRARIES = liblxcfs.la
 EXTRA_LTLIBRARIES = liblxcfstest.la
 
-lxcfs_SOURCES = lxcfs.c memory_utils.h
+lxcfs_SOURCES = lxcfs.c
 lxcfs_LDADD = -ldl
 lxcfs_CFLAGS = $(AM_CFLAGS)
 lxcfs_LDFLAGS = $(AM_LDFLAGS)
diff --git a/bindings.c b/bindings.c
index e09a986..049418c 100644
--- a/bindings.c
+++ b/bindings.c
@@ -37,8 +37,7 @@
 #include <sys/vfs.h>
 
 #include "bindings.h"
-#include "config.h"
-#include "memory_utils.h"
+#include "config.h" // for VERSION
 
 /* Maximum number for 64 bit integer is a string with 21 digits: 2^64 - 1 = 21 */
 #define LXCFS_NUMSTRLEN64 21
@@ -250,8 +249,8 @@ static struct load_node *del_node(struct load_node *n, int locate)
 		n->next->pre = n->pre;
 	}
 	g = n->next;
-	__free_move__(n->cg);
-	__free_move__(n);
+	free(n->cg);
+	free(n);
 	pthread_rwlock_unlock(&load_hash[locate].rdlock);
 	return g;
 }
@@ -275,9 +274,9 @@ static void load_free(void)
 			continue;
 		}
 		for (f = load_hash[i].next; f; ) {
-			__free_move__(f->cg);
+			free(f->cg);
 			p = f->next;
-			__free_move__(f);
+			free(f);
 			f = p;
 		}
 		pthread_mutex_unlock(&load_hash[i].lock);
@@ -325,7 +324,7 @@ static bool cpuview_init_head(struct cg_proc_stat_head **head)
 
 	if (pthread_rwlock_init(&(*head)->lock, NULL) != 0) {
 		lxcfs_error("%s\n", "Failed to initialize list lock");
-		__free_move__(*head);
+		free(*head);
 		return false;
 	}
 
@@ -349,7 +348,8 @@ static bool init_cpuview()
 err:
 	for (i = 0; i < CPUVIEW_HASH_SIZE; i++) {
 		if (proc_stat_history[i]) {
-			__free_move__(proc_stat_history[i]);
+			free(proc_stat_history[i]);
+			proc_stat_history[i] = NULL;
 		}
 	}
 
@@ -359,10 +359,10 @@ static bool init_cpuview()
 static void free_proc_stat_node(struct cg_proc_stat *node)
 {
 	pthread_mutex_destroy(&node->lock);
-	__free_move__(node->cg);
-	__free_move__(node->usage);
-	__free_move__(node->view);
-	__free_move__(node);
+	free(node->cg);
+	free(node->usage);
+	free(node->view);
+	free(node);
 }
 
 static void cpuview_free_head(struct cg_proc_stat_head *head)
@@ -383,7 +383,7 @@ static void cpuview_free_head(struct cg_proc_stat_head *head)
 	}
 
 	pthread_rwlock_destroy(&head->lock);
-	__free_move__(head);
+	free(head);
 }
 
 static void free_cpuview()
@@ -507,7 +507,7 @@ static void remove_initpid(struct pidns_init_store *e)
 	h = HASH(e->ino);
 	if (pidns_hash_table[h] == e) {
 		pidns_hash_table[h] = e->next;
-		__free_move__(e);
+		free(e);
 		return;
 	}
 
@@ -515,7 +515,7 @@ static void remove_initpid(struct pidns_init_store *e)
 	while (tmp) {
 		if (tmp->next == e) {
 			tmp->next = e->next;
-			__free_move__(e);
+			free(e);
 			return;
 		}
 		tmp = tmp->next;
@@ -527,6 +527,7 @@ static void remove_initpid(struct pidns_init_store *e)
 static void prune_initpid_store(void)
 {
 	static long int last_prune = 0;
+	struct pidns_init_store *e, *prev, *delme;
 	long int now, threshold;
 	int i;
 
@@ -544,11 +545,7 @@ static void prune_initpid_store(void)
 	threshold = now - 2 * PURGE_SECS;
 
 	for (i = 0; i < PIDNS_HASH_SIZE; i++) {
-		struct pidns_init_store *e, *prev;
-
 		for (prev = NULL, e = pidns_hash_table[i]; e; ) {
-			__do_free struct pidns_init_store *delme = NULL;
-
 			if (e->lastcheck < threshold) {
 
 				lxcfs_debug("Removing cached entry for %d.\n", e->initpid);
@@ -559,6 +556,7 @@ static void prune_initpid_store(void)
 				else
 					pidns_hash_table[i] = e->next;
 				e = e->next;
+				free(delme);
 			} else {
 				prev = e;
 				e = e->next;
@@ -672,22 +670,23 @@ static void append_line(char **contents, size_t *len, char *line, ssize_t linele
 
 static char *slurp_file(const char *from, int fd)
 {
-	__do_free char *line = NULL;
-	__do_fclose FILE *f = NULL;
+	char *line = NULL;
 	char *contents = NULL;
+	FILE *f = fdopen(fd, "r");
 	size_t len = 0, fulllen = 0;
 	ssize_t linelen;
 
-	f = fdopen(fd, "r");
 	if (!f)
 		return NULL;
 
-	while ((linelen = getline(&line, &len, f)) != -1)
+	while ((linelen = getline(&line, &len, f)) != -1) {
 		append_line(&contents, &fulllen, line, linelen);
+	}
+	fclose(f);
 
 	if (contents)
 		drop_trailing_newlines(contents);
-
+	free(line);
 	return contents;
 }
 
@@ -1162,9 +1161,8 @@ void free_key(struct cgfs_files *k)
 {
 	if (!k)
 		return;
-
-	__free_move__(k->name);
-	__free_move__(k);
+	free(k->name);
+	free(k);
 }
 
 void free_keys(struct cgfs_files **keys)
@@ -1173,11 +1171,10 @@ void free_keys(struct cgfs_files **keys)
 
 	if (!keys)
 		return;
-
-	for (i = 0; keys[i]; i++)
+	for (i = 0; keys[i]; i++) {
 		free_key(keys[i]);
-
-	__free_move__(keys);
+	}
+	free(keys);
 }
 
 bool cgfs_get_value(const char *controller, const char *cgroup, const char *file, char **value)
@@ -1650,11 +1647,11 @@ static void stripnewline(char *x)
 
 static char *get_pid_cgroup(pid_t pid, const char *contrl)
 {
-	__do_free char *line = NULL;
-	__do_fclose FILE *f = NULL;
 	int cfd;
 	char fnam[PROCLEN];
+	FILE *f;
 	char *answer = NULL;
+	char *line = NULL;
 	size_t len = 0;
 	int ret;
 	const char *h = find_mounted_controller(contrl, &cfd);
@@ -1669,36 +1666,29 @@ static char *get_pid_cgroup(pid_t pid, const char *contrl)
 
 	while (getline(&line, &len, f) != -1) {
 		char *c1, *c2;
-
 		if (!line[0])
 			continue;
-
 		c1 = strchr(line, ':');
 		if (!c1)
-			return NULL;
-
+			goto out;
 		c1++;
-
 		c2 = strchr(c1, ':');
 		if (!c2)
-			return NULL;
-
+			goto out;
 		*c2 = '\0';
-
 		if (strcmp(c1, h) != 0)
 			continue;
-
 		c2++;
-
 		stripnewline(c2);
-
 		do {
 			answer = strdup(c2);
 		} while (!answer);
-
 		break;
 	}
 
+out:
+	fclose(f);
+	free(line);
 	return answer;
 }
 
@@ -1768,11 +1758,10 @@ static void prune_init_slice(char *cg)
  */
 static bool caller_is_in_ancestor(pid_t pid, const char *contrl, const char *cg, char **nextcg)
 {
-	__do_free char *c2 = NULL;
 	bool answer = false;
+	char *c2 = get_pid_cgroup(pid, contrl);
 	char *linecmp;
 
-	c2 = get_pid_cgroup(pid, contrl);
 	if (!c2)
 		return false;
 	prune_init_slice(c2);
@@ -1791,13 +1780,15 @@ static bool caller_is_in_ancestor(pid_t pid, const char *contrl, const char *cg,
 	else
 		linecmp = c2 + 1;
 	if (strncmp(linecmp, cg, strlen(linecmp)) != 0) {
-		if (nextcg)
+		if (nextcg) {
 			*nextcg = get_next_cgroup_dir(linecmp, cg);
+		}
 		goto out;
 	}
 	answer = true;
 
 out:
+	free(c2);
 	return answer;
 }
 
@@ -1806,9 +1797,8 @@ static bool caller_is_in_ancestor(pid_t pid, const char *contrl, const char *cg,
  */
 static bool caller_may_see_dir(pid_t pid, const char *contrl, const char *cg)
 {
-	__do_free char *c2 = NULL;
 	bool answer = false;
-	char *task_cg;
+	char *c2, *task_cg;
 	size_t target_len, task_len;
 
 	if (strcmp(cg, "/") == 0 || strcmp(cg, "./") == 0)
@@ -1849,6 +1839,7 @@ static bool caller_may_see_dir(pid_t pid, const char *contrl, const char *cg)
 	}
 
 out:
+	free(c2);
 	return answer;
 }
 
@@ -1935,9 +1926,9 @@ static void get_cgdir_and_path(const char *cg, char **dir, char **last)
 
 int cg_getattr(const char *path, struct stat *sb)
 {
-	__do_free char * cgdir = NULL;
 	struct timespec now;
 	struct fuse_context *fc = fuse_get_context();
+	char * cgdir = NULL;
 	char *last = NULL, *path1, *path2;
 	struct cgfs_files *k = NULL;
 	const char *cgroup;
@@ -2039,6 +2030,7 @@ int cg_getattr(const char *path, struct stat *sb)
 	}
 
 out:
+	free(cgdir);
 	return ret;
 }
 
@@ -2096,10 +2088,10 @@ int cg_opendir(const char *path, struct fuse_file_info *fi)
 int cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
 		struct fuse_file_info *fi)
 {
-	__do_free char *nextcg = NULL;
 	struct file_info *d = (struct file_info *)fi->fh;
 	struct cgfs_files **list = NULL;
 	int i, ret;
+	char *nextcg = NULL;
 	struct fuse_context *fc = fuse_get_context();
 	char **clist = NULL;
 
@@ -2134,6 +2126,7 @@ int cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset
 	if (!caller_is_in_ancestor(initpid, d->controller, d->cgroup, &nextcg)) {
 		if (nextcg) {
 			ret = filler(buf, nextcg,  NULL, 0);
+			free(nextcg);
 			if (ret != 0) {
 				ret = -EIO;
 				goto out;
@@ -2170,8 +2163,8 @@ int cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset
 	free_keys(list);
 	if (clist) {
 		for (i = 0; clist[i]; i++)
-			__free_move__(clist[i]);
-		__free_move__(clist);
+			free(clist[i]);
+		free(clist);
 	}
 	return ret;
 }
@@ -2185,11 +2178,16 @@ static void do_release_file_info(struct fuse_file_info *fi)
 
 	fi->fh = 0;
 
-	__free_move__(f->controller);
-	__free_move__(f->cgroup);
-	__free_move__(f->file);
-	__free_move__(f->buf);
-	__free_move__(f);
+	free(f->controller);
+	f->controller = NULL;
+	free(f->cgroup);
+	f->cgroup = NULL;
+	free(f->file);
+	f->file = NULL;
+	free(f->buf);
+	f->buf = NULL;
+	free(f);
+	f = NULL;
 }
 
 int cg_releasedir(const char *path, struct fuse_file_info *fi)
@@ -2200,9 +2198,8 @@ int cg_releasedir(const char *path, struct fuse_file_info *fi)
 
 int cg_open(const char *path, struct fuse_file_info *fi)
 {
-	__do_free char *cgdir = NULL;
 	const char *cgroup;
-	char *last = NULL, *path1, *path2, *controller;
+	char *last = NULL, *path1, *path2, * cgdir = NULL, *controller;
 	struct cgfs_files *k = NULL;
 	struct file_info *file_info;
 	struct fuse_context *fc = fuse_get_context();
@@ -2263,16 +2260,16 @@ int cg_open(const char *path, struct fuse_file_info *fi)
 	ret = 0;
 
 out:
+	free(cgdir);
 	return ret;
 }
 
 int cg_access(const char *path, int mode)
 {
-	__do_free char *cgdir = NULL;
 	int ret;
 	const char *cgroup;
 	char *path1, *path2, *controller;
-	char *last = NULL;
+	char *last = NULL, *cgdir = NULL;
 	struct cgfs_files *k = NULL;
 	struct fuse_context *fc = fuse_get_context();
 
@@ -2327,6 +2324,7 @@ int cg_access(const char *path, int mode)
 	ret = 0;
 
 out:
+	free(cgdir);
 	return ret;
 }
 
@@ -2601,7 +2599,7 @@ static void pid_to_ns_wrapper(int sock, pid_t tpid)
 bool do_read_pids(pid_t tpid, const char *contrl, const char *cg, const char *file, char **d)
 {
 	int sock[2] = {-1, -1};
-	__do_free char *tmpdata = NULL;
+	char *tmpdata = NULL;
 	int ret;
 	pid_t qpid, cpid = -1;
 	bool answer = false;
@@ -2620,6 +2618,7 @@ bool do_read_pids(pid_t tpid, const char *contrl, const char *cg, const char *fi
 
 	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sock) < 0) {
 		perror("socketpair");
+		free(tmpdata);
 		return false;
 	}
 
@@ -2670,6 +2669,7 @@ bool do_read_pids(pid_t tpid, const char *contrl, const char *cg, const char *fi
 	answer = true;
 
 out:
+	free(tmpdata);
 	if (cpid != -1)
 		wait_for_pid(cpid);
 	if (sock[0] != -1) {
@@ -2682,10 +2682,10 @@ bool do_read_pids(pid_t tpid, const char *contrl, const char *cg, const char *fi
 int cg_read(const char *path, char *buf, size_t size, off_t offset,
 		struct fuse_file_info *fi)
 {
-	__do_free char *data = NULL;
 	struct fuse_context *fc = fuse_get_context();
 	struct file_info *f = (struct file_info *)fi->fh;
 	struct cgfs_files *k = NULL;
+	char *data = NULL;
 	int ret, s;
 	bool r;
 
@@ -2742,6 +2742,7 @@ int cg_read(const char *path, char *buf, size_t size, off_t offset,
 	ret = s;
 
 out:
+	free(data);
 	return ret;
 }
 
@@ -3042,9 +3043,8 @@ int cg_write(const char *path, const char *buf, size_t size, off_t offset,
 
 int cg_chown(const char *path, uid_t uid, gid_t gid)
 {
-	__do_free char *cgdir = NULL;
 	struct fuse_context *fc = fuse_get_context();
-	char *last = NULL, *path1, *path2, *controller;
+	char *cgdir = NULL, *last = NULL, *path1, *path2, *controller;
 	struct cgfs_files *k = NULL;
 	const char *cgroup;
 	int ret;
@@ -3102,15 +3102,15 @@ int cg_chown(const char *path, uid_t uid, gid_t gid)
 
 out:
 	free_key(k);
+	free(cgdir);
 
 	return ret;
 }
 
 int cg_chmod(const char *path, mode_t mode)
 {
-	__do_free char *cgdir = NULL;
 	struct fuse_context *fc = fuse_get_context();
-	char *last = NULL, *path1, *path2, *controller;
+	char * cgdir = NULL, *last = NULL, *path1, *path2, *controller;
 	struct cgfs_files *k = NULL;
 	const char *cgroup;
 	int ret;
@@ -3172,14 +3172,14 @@ int cg_chmod(const char *path, mode_t mode)
 	ret = 0;
 out:
 	free_key(k);
+	free(cgdir);
 	return ret;
 }
 
 int cg_mkdir(const char *path, mode_t mode)
 {
-	__do_free char *cgdir = NULL, *next = NULL;
 	struct fuse_context *fc = fuse_get_context();
-	char *last = NULL, *path1, *controller;
+	char *last = NULL, *path1, *cgdir = NULL, *controller, *next = NULL;
 	const char *cgroup;
 	int ret;
 
@@ -3225,14 +3225,15 @@ int cg_mkdir(const char *path, mode_t mode)
 	ret = cgfs_create(controller, cgroup, fc->uid, fc->gid);
 
 out:
+	free(cgdir);
+	free(next);
 	return ret;
 }
 
 int cg_rmdir(const char *path)
 {
-	__do_free char *cgdir = NULL, *next = NULL;
 	struct fuse_context *fc = fuse_get_context();
-	char *last = NULL, *controller;
+	char *last = NULL, *cgdir = NULL, *controller, *next = NULL;
 	const char *cgroup;
 	int ret;
 
@@ -3285,6 +3286,8 @@ int cg_rmdir(const char *path)
 	ret = 0;
 
 out:
+	free(cgdir);
+	free(next);
 	return ret;
 }
 
@@ -3358,13 +3361,11 @@ static void get_blkio_io_value(char *str, unsigned major, unsigned minor, char *
 static int read_file(const char *path, char *buf, size_t size,
 		     struct file_info *d)
 {
-	__do_free char *line = NULL;
-	__do_fclose FILE *f = NULL;
-	size_t linelen = 0, total_len = 0;
+	size_t linelen = 0, total_len = 0, rv = 0;
+	char *line = NULL;
 	char *cache = d->buf;
 	size_t cache_size = d->buflen;
-
-	f = fopen(path, "r");
+	FILE *f = fopen(path, "r");
 	if (!f)
 		return 0;
 
@@ -3372,11 +3373,13 @@ static int read_file(const char *path, char *buf, size_t size,
 		ssize_t l = snprintf(cache, cache_size, "%s", line);
 		if (l < 0) {
 			perror("Error writing to cache");
-			return 0;
+			rv = 0;
+			goto err;
 		}
 		if (l >= cache_size) {
 			lxcfs_error("%s\n", "Internal error: truncated write to cache.");
-			return 0;
+			rv = 0;
+			goto err;
 		}
 		cache += l;
 		cache_size -= l;
@@ -3389,7 +3392,11 @@ static int read_file(const char *path, char *buf, size_t size,
 
 	/* read from off 0 */
 	memcpy(buf, d->buf, total_len);
-	return total_len;
+	rv = total_len;
+  err:
+	fclose(f);
+	free(line);
+	return rv;
 }
 
 /*
@@ -3398,12 +3405,14 @@ static int read_file(const char *path, char *buf, size_t size,
 
 static unsigned long get_memlimit(const char *cgroup, const char *file)
 {
-	__do_free char *memlimit_str = NULL;
+	char *memlimit_str = NULL;
 	unsigned long memlimit = -1;
 
 	if (cgfs_get_value("memory", cgroup, file, &memlimit_str))
 		memlimit = strtoul(memlimit_str, NULL, 10);
 
+	free(memlimit_str);
+
 	return memlimit;
 }
 
@@ -3427,20 +3436,21 @@ static unsigned long get_min_memlimit(const char *cgroup, const char *file)
 static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 		struct fuse_file_info *fi)
 {
-	__do_free char *cg = NULL, *line = NULL, *memusage_str = NULL,
-		       *memstat_str = NULL, *memswlimit_str = NULL,
-		       *memswusage_str = NULL;
-	__do_fclose FILE *f = NULL;
 	struct fuse_context *fc = fuse_get_context();
 	struct lxcfs_opts *opts = (struct lxcfs_opts *) fuse_get_context()->private_data;
 	struct file_info *d = (struct file_info *)fi->fh;
+	char *cg;
+	char *memusage_str = NULL, *memstat_str = NULL,
+		*memswlimit_str = NULL, *memswusage_str = NULL;
 	unsigned long memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
 		cached = 0, hosttotal = 0, active_anon = 0, inactive_anon = 0,
 		active_file = 0, inactive_file = 0, unevictable = 0, shmem = 0,
 		hostswtotal = 0;
+	char *line = NULL;
 	size_t linelen = 0, total_len = 0, rv = 0;
 	char *cache = d->buf;
 	size_t cache_size = d->buflen;
+	FILE *f = NULL;
 
 	if (offset){
 		if (offset > d->size)
@@ -3604,6 +3614,14 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 
 	rv = total_len;
 err:
+	if (f)
+		fclose(f);
+	free(line);
+	free(cg);
+	free(memusage_str);
+	free(memswlimit_str);
+	free(memswusage_str);
+	free(memstat_str);
 	return rv;
 }
 
@@ -3637,9 +3655,9 @@ static bool cpuline_in_cpuset(const char *line, const char *cpuset)
  */
 static bool read_cpu_cfs_param(const char *cg, const char *param, int64_t *value)
 {
-	__do_free char *str = NULL;
 	bool rv = false;
 	char file[11 + 6 + 1]; // cpu.cfs__us + quota/period + \0
+	char *str = NULL;
 
 	sprintf(file, "cpu.cfs_%s_us", param);
 
@@ -3652,6 +3670,8 @@ static bool read_cpu_cfs_param(const char *cg, const char *param, int64_t *value
 	rv = true;
 
 err:
+	if (str)
+		free(str);
 	return rv;
 }
 
@@ -3723,16 +3743,18 @@ static bool is_processor_line(const char *line)
 static int proc_cpuinfo_read(char *buf, size_t size, off_t offset,
 		struct fuse_file_info *fi)
 {
-	__do_free char *cg = NULL, *cpuset = NULL, *line = NULL;
-	__do_fclose FILE *f = NULL;
 	struct fuse_context *fc = fuse_get_context();
 	struct file_info *d = (struct file_info *)fi->fh;
+	char *cg;
+	char *cpuset = NULL;
+	char *line = NULL;
 	size_t linelen = 0, total_len = 0, rv = 0;
 	bool am_printing = false, firstline = true, is_s390x = false;
 	int curcpu = -1, cpu, max_cpus = 0;
 	bool use_view;
 	char *cache = d->buf;
 	size_t cache_size = d->buflen;
+	FILE *f = NULL;
 
 	if (offset){
 		if (offset > d->size)
@@ -3847,7 +3869,7 @@ static int proc_cpuinfo_read(char *buf, size_t size, off_t offset,
 	}
 
 	if (is_s390x) {
-		__do_free char *origcache = d->buf;
+		char *origcache = d->buf;
 		ssize_t l;
 		do {
 			d->buf = malloc(d->buflen);
@@ -3856,18 +3878,23 @@ static int proc_cpuinfo_read(char *buf, size_t size, off_t offset,
 		cache_size = d->buflen;
 		total_len = 0;
 		l = snprintf(cache, cache_size, "vendor_id       : IBM/S390\n");
-		if (l < 0 || l >= cache_size)
+		if (l < 0 || l >= cache_size) {
+			free(origcache);
 			goto err;
+		}
 		cache_size -= l;
 		cache += l;
 		total_len += l;
 		l = snprintf(cache, cache_size, "# processors    : %d\n", curcpu + 1);
-		if (l < 0 || l >= cache_size)
+		if (l < 0 || l >= cache_size) {
+			free(origcache);
 			goto err;
+		}
 		cache_size -= l;
 		cache += l;
 		total_len += l;
 		l = snprintf(cache, cache_size, "%s", origcache);
+		free(origcache);
 		if (l < 0 || l >= cache_size)
 			goto err;
 		total_len += l;
@@ -3881,6 +3908,11 @@ static int proc_cpuinfo_read(char *buf, size_t size, off_t offset,
 	memcpy(buf, d->buf, total_len);
 	rv = total_len;
 err:
+	if (f)
+		fclose(f);
+	free(line);
+	free(cpuset);
+	free(cg);
 	return rv;
 }
 
diff --git a/lxcfs.c b/lxcfs.c
index 1e7b5d1..2247fcb 100644
--- a/lxcfs.c
+++ b/lxcfs.c
@@ -15,24 +15,23 @@
 #include <fcntl.h>
 #include <fuse.h>
 #include <libgen.h>
-#include <linux/limits.h>
-#include <linux/sched.h>
 #include <pthread.h>
 #include <sched.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/epoll.h>
-#include <sys/mount.h>
-#include <sys/socket.h>
 #include <time.h>
 #include <unistd.h>
 #include <wait.h>
+#include <linux/sched.h>
+#include <sys/epoll.h>
+#include <sys/mount.h>
+#include <sys/socket.h>
+#include <linux/limits.h>
 
 #include "bindings.h"
-#include "config.h"
-#include "memory_utils.h"
+#include "config.h" // for VERSION
 
 void *dlopen_handle;
 
@@ -40,11 +39,11 @@ void *dlopen_handle;
 
 static int users_count;
 static pthread_mutex_t user_count_mutex = PTHREAD_MUTEX_INITIALIZER;
-
 static void lock_mutex(pthread_mutex_t *l)
 {
-	int ret = pthread_mutex_lock(l);
-	if (ret) {
+	int ret;
+
+	if ((ret = pthread_mutex_lock(l)) != 0) {
 		lxcfs_error("returned:%d %s\n", ret, strerror(ret));
 		exit(1);
 	}
@@ -52,8 +51,9 @@ static void lock_mutex(pthread_mutex_t *l)
 
 static void unlock_mutex(pthread_mutex_t *l)
 {
-	int ret = pthread_mutex_unlock(l);
-	if (ret) {
+	int ret;
+
+	if ((ret = pthread_mutex_unlock(l)) != 0) {
 		lxcfs_error("returned:%d %s\n", ret, strerror(ret));
 		exit(1);
 	}
@@ -72,41 +72,38 @@ static void users_unlock(void)
 static pthread_t loadavg_pid = 0;
 
 /* Returns zero on success */
-static int start_loadavg(void)
-{
+static int start_loadavg(void) {
 	char *error;
 	pthread_t (*load_daemon)(int);
 
-	dlerror(); /* Clear any existing error */
+	dlerror();    /* Clear any existing error */
 
-	load_daemon = (pthread_t(*)(int))dlsym(dlopen_handle, "load_daemon");
+	load_daemon = (pthread_t (*)(int)) dlsym(dlopen_handle, "load_daemon");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("load_daemon fails:%s\n", error);
 		return -1;
 	}
-
 	loadavg_pid = load_daemon(1);
-	if (!loadavg_pid)
+	if (loadavg_pid == 0)
 		return -1;
 
 	return 0;
 }
 
 /* Returns zero on success */
-static int stop_loadavg(void)
-{
+static int stop_loadavg(void) {
 	char *error;
 	int (*stop_load_daemon)(pthread_t);
 
-	stop_load_daemon = (int (*)(pthread_t))dlsym(dlopen_handle, "stop_load_daemon");
+	stop_load_daemon = (int (*)(pthread_t)) dlsym(dlopen_handle, "stop_load_daemon");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("stop_load_daemon error: %s\n", error);
 		return -1;
 	}
 
-	if (stop_load_daemon(loadavg_pid))
+	if (stop_load_daemon(loadavg_pid) != 0)
 		return -1;
 
 	return 0;
@@ -118,30 +115,30 @@ static volatile sig_atomic_t need_reload;
  * lock and when we know the user_count was 0 */
 static void do_reload(void)
 {
-	char *lxcfs_lib_path;
+	char lxcfs_lib_path[PATH_MAX];
 
 	if (loadavg_pid > 0)
 		stop_loadavg();
 
 	if (dlopen_handle) {
-		lxcfs_debug("%s\n", "Closing liblxcfs.so handle");
+		lxcfs_debug("%s\n", "Closing liblxcfs.so handle.");
 		dlclose(dlopen_handle);
 	}
 
 	/* First try loading using ld.so */
 	dlopen_handle = dlopen("liblxcfs.so", RTLD_LAZY);
 	if (dlopen_handle) {
-		lxcfs_debug("%s\n", "Successfully called dlopen() on liblxcfs.so");
+		lxcfs_debug("%s\n", "Successfully called dlopen() on liblxcfs.so.");
 		goto good;
 	}
 
 #ifdef LIBDIR
 	/* LIBDIR: autoconf will setup this MACRO. Default value is $PREFIX/lib */
-	lxcfs_lib_path = LIBDIR "/lxcfs/liblxcfs.so";
+        snprintf(lxcfs_lib_path, PATH_MAX, "%s/lxcfs/liblxcfs.so", LIBDIR);
 #else
-	lxcfs_lib_path = "/usr/local/lib/lxcfs/liblxcfs.so";
+        snprintf(lxcfs_lib_path, PATH_MAX, "/usr/local/lib/lxcfs/liblxcfs.so");
 #endif
-	dlopen_handle = dlopen(lxcfs_lib_path, RTLD_LAZY);
+        dlopen_handle = dlopen(lxcfs_lib_path, RTLD_LAZY);
 	if (!dlopen_handle) {
 		lxcfs_error("Failed to open liblxcfs.so: %s.\n", dlerror());
 		_exit(1);
@@ -153,7 +150,6 @@ static void do_reload(void)
 
 	if (need_reload)
 		lxcfs_error("%s\n", "lxcfs: reloaded");
-
 	need_reload = 0;
 }
 
@@ -181,14 +177,12 @@ static void reload_handler(int sig)
 /* Functions to run the library methods */
 static int do_cg_getattr(const char *path, struct stat *sb)
 {
-	char *error;
 	int (*cg_getattr)(const char *path, struct stat *sb);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "cg_getattr");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -198,14 +192,12 @@ static int do_cg_getattr(const char *path, struct stat *sb)
 
 static int do_proc_getattr(const char *path, struct stat *sb)
 {
-	char *error;
 	int (*proc_getattr)(const char *path, struct stat *sb);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	proc_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "proc_getattr");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -214,17 +206,16 @@ static int do_proc_getattr(const char *path, struct stat *sb)
 }
 
 static int do_cg_read(const char *path, char *buf, size_t size, off_t offset,
-		      struct fuse_file_info *fi)
+		struct fuse_file_info *fi)
 {
-	char *error;
 	int (*cg_read)(const char *path, char *buf, size_t size, off_t offset,
-		       struct fuse_file_info *fi);
+		struct fuse_file_info *fi);
+	char *error;
 
 	dlerror();    /* Clear any existing error */
-
 	cg_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_read");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -233,17 +224,16 @@ static int do_cg_read(const char *path, char *buf, size_t size, off_t offset,
 }
 
 static int do_proc_read(const char *path, char *buf, size_t size, off_t offset,
-			struct fuse_file_info *fi)
+		struct fuse_file_info *fi)
 {
-	char *error;
 	int (*proc_read)(const char *path, char *buf, size_t size, off_t offset,
-			 struct fuse_file_info *fi);
+		struct fuse_file_info *fi);
+	char *error;
 
 	dlerror();    /* Clear any existing error */
-
 	proc_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_read");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -251,18 +241,16 @@ static int do_proc_read(const char *path, char *buf, size_t size, off_t offset,
 	return proc_read(path, buf, size, offset, fi);
 }
 
-static int do_cg_write(const char *path, const char *buf, size_t size,
-		       off_t offset, struct fuse_file_info *fi)
+static int do_cg_write(const char *path, const char *buf, size_t size, off_t offset,
+	     struct fuse_file_info *fi)
 {
+	int (*cg_write)(const char *path, const char *buf, size_t size, off_t offset,
+	     struct fuse_file_info *fi);
 	char *error;
-	int (*cg_write)(const char *path, const char *buf, size_t size,
-			off_t offset, struct fuse_file_info *fi);
-
 	dlerror();    /* Clear any existing error */
-
 	cg_write = (int (*)(const char *, const char *, size_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_write");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -272,14 +260,12 @@ static int do_cg_write(const char *path, const char *buf, size_t size,
 
 static int do_cg_mkdir(const char *path, mode_t mode)
 {
-	char *error;
 	int (*cg_mkdir)(const char *path, mode_t mode);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_mkdir = (int (*)(const char *, mode_t)) dlsym(dlopen_handle, "cg_mkdir");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -289,14 +275,12 @@ static int do_cg_mkdir(const char *path, mode_t mode)
 
 static int do_cg_chown(const char *path, uid_t uid, gid_t gid)
 {
-	char *error;
 	int (*cg_chown)(const char *path, uid_t uid, gid_t gid);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_chown = (int (*)(const char *, uid_t, gid_t)) dlsym(dlopen_handle, "cg_chown");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -306,14 +290,12 @@ static int do_cg_chown(const char *path, uid_t uid, gid_t gid)
 
 static int do_cg_rmdir(const char *path)
 {
-	char *error;
 	int (*cg_rmdir)(const char *path);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_rmdir = (int (*)(const char *path)) dlsym(dlopen_handle, "cg_rmdir");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -323,14 +305,12 @@ static int do_cg_rmdir(const char *path)
 
 static int do_cg_chmod(const char *path, mode_t mode)
 {
-	char *error;
 	int (*cg_chmod)(const char *path, mode_t mode);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_chmod = (int (*)(const char *, mode_t)) dlsym(dlopen_handle, "cg_chmod");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -338,18 +318,17 @@ static int do_cg_chmod(const char *path, mode_t mode)
 	return cg_chmod(path, mode);
 }
 
-static int do_cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
-			 off_t offset, struct fuse_file_info *fi)
+static int do_cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
+		struct fuse_file_info *fi)
 {
+	int (*cg_readdir)(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
+		struct fuse_file_info *fi);
 	char *error;
-	int (*cg_readdir)(const char *path, void *buf, fuse_fill_dir_t filler,
-			  off_t offset, struct fuse_file_info *fi);
 
 	dlerror();    /* Clear any existing error */
-
 	cg_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_readdir");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -357,18 +336,17 @@ static int do_cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 	return cg_readdir(path, buf, filler, offset, fi);
 }
 
-static int do_proc_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
-			   off_t offset, struct fuse_file_info *fi)
+static int do_proc_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
+		struct fuse_file_info *fi)
 {
+	int (*proc_readdir)(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
+		struct fuse_file_info *fi);
 	char *error;
-	int (*proc_readdir)(const char *path, void *buf, fuse_fill_dir_t filler,
-			    off_t offset, struct fuse_file_info *fi);
 
 	dlerror();    /* Clear any existing error */
-
 	proc_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_readdir");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -378,14 +356,12 @@ static int do_proc_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 
 static int do_cg_open(const char *path, struct fuse_file_info *fi)
 {
-	char *error;
 	int (*cg_open)(const char *path, struct fuse_file_info *fi);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_open = (int (*)(const char *, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_open");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -395,14 +371,12 @@ static int do_cg_open(const char *path, struct fuse_file_info *fi)
 
 static int do_cg_access(const char *path, int mode)
 {
-	char *error;
 	int (*cg_access)(const char *path, int mode);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_access = (int (*)(const char *, int mode)) dlsym(dlopen_handle, "cg_access");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -412,14 +386,12 @@ static int do_cg_access(const char *path, int mode)
 
 static int do_proc_open(const char *path, struct fuse_file_info *fi)
 {
-	char *error;
 	int (*proc_open)(const char *path, struct fuse_file_info *fi);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	proc_open = (int (*)(const char *path, struct fuse_file_info *fi)) dlsym(dlopen_handle, "proc_open");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -429,14 +401,12 @@ static int do_proc_open(const char *path, struct fuse_file_info *fi)
 
 static int do_proc_access(const char *path, int mode)
 {
-	char *error;
 	int (*proc_access)(const char *path, int mode);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	proc_access = (int (*)(const char *, int mode)) dlsym(dlopen_handle, "proc_access");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -446,14 +416,12 @@ static int do_proc_access(const char *path, int mode)
 
 static int do_cg_release(const char *path, struct fuse_file_info *fi)
 {
-	char *error;
 	int (*cg_release)(const char *path, struct fuse_file_info *fi);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_release = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_release");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -463,14 +431,12 @@ static int do_cg_release(const char *path, struct fuse_file_info *fi)
 
 static int do_proc_release(const char *path, struct fuse_file_info *fi)
 {
-	char *error;
 	int (*proc_release)(const char *path, struct fuse_file_info *fi);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	proc_release = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_release");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -480,14 +446,12 @@ static int do_proc_release(const char *path, struct fuse_file_info *fi)
 
 static int do_cg_opendir(const char *path, struct fuse_file_info *fi)
 {
-	char *error;
 	int (*cg_opendir)(const char *path, struct fuse_file_info *fi);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_opendir = (int (*)(const char *path, struct fuse_file_info *fi)) dlsym(dlopen_handle, "cg_opendir");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -497,14 +461,12 @@ static int do_cg_opendir(const char *path, struct fuse_file_info *fi)
 
 static int do_cg_releasedir(const char *path, struct fuse_file_info *fi)
 {
-	char *error;
 	int (*cg_releasedir)(const char *path, struct fuse_file_info *fi);
-
+	char *error;
 	dlerror();    /* Clear any existing error */
-
 	cg_releasedir = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "cg_releasedir");
 	error = dlerror();
-	if (error) {
+	if (error != NULL) {
 		lxcfs_error("%s\n", error);
 		return -1;
 	}
@@ -526,7 +488,6 @@ static int lxcfs_getattr(const char *path, struct stat *sb)
 	if (strcmp(path, "/") == 0) {
 		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
 			return -EINVAL;
-
 		sb->st_uid = sb->st_gid = 0;
 		sb->st_atim = sb->st_mtim = sb->st_ctim = now;
 		sb->st_size = 0;
@@ -541,41 +502,36 @@ static int lxcfs_getattr(const char *path, struct stat *sb)
 		down_users();
 		return ret;
 	}
-
 	if (strncmp(path, "/proc", 5) == 0) {
 		up_users();
 		ret = do_proc_getattr(path, sb);
 		down_users();
 		return ret;
 	}
-
 	return -ENOENT;
 }
 
 static int lxcfs_opendir(const char *path, struct fuse_file_info *fi)
 {
+	int ret;
 	if (strcmp(path, "/") == 0)
 		return 0;
 
 	if (strncmp(path, "/cgroup", 7) == 0) {
-		int ret;
 		up_users();
 		ret = do_cg_opendir(path, fi);
 		down_users();
 		return ret;
 	}
-
 	if (strcmp(path, "/proc") == 0)
 		return 0;
-
 	return -ENOENT;
 }
 
-static int lxcfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
-			 off_t offset, struct fuse_file_info *fi)
+static int lxcfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
+		struct fuse_file_info *fi)
 {
 	int ret;
-
 	if (strcmp(path, "/") == 0) {
 		if (filler(buf, ".", NULL, 0) != 0 ||
 		    filler(buf, "..", NULL, 0) != 0 ||
@@ -584,21 +540,18 @@ static int lxcfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 			return -ENOMEM;
 		return 0;
 	}
-
 	if (strncmp(path, "/cgroup", 7) == 0) {
 		up_users();
 		ret = do_cg_readdir(path, buf, filler, offset, fi);
 		down_users();
 		return ret;
 	}
-
 	if (strcmp(path, "/proc") == 0) {
 		up_users();
 		ret = do_proc_readdir(path, buf, filler, offset, fi);
 		down_users();
 		return ret;
 	}
-
 	return -ENOENT;
 }
 
@@ -615,7 +568,6 @@ static int lxcfs_access(const char *path, int mode)
 		down_users();
 		return ret;
 	}
-
 	if (strncmp(path, "/proc", 5) == 0) {
 		up_users();
 		ret = do_proc_access(path, mode);
@@ -629,34 +581,28 @@ static int lxcfs_access(const char *path, int mode)
 static int lxcfs_releasedir(const char *path, struct fuse_file_info *fi)
 {
 	int ret;
-
 	if (strcmp(path, "/") == 0)
 		return 0;
-
 	if (strncmp(path, "/cgroup", 7) == 0) {
 		up_users();
 		ret = do_cg_releasedir(path, fi);
 		down_users();
 		return ret;
 	}
-
 	if (strcmp(path, "/proc") == 0)
 		return 0;
-
 	return -EINVAL;
 }
 
 static int lxcfs_open(const char *path, struct fuse_file_info *fi)
 {
 	int ret;
-
 	if (strncmp(path, "/cgroup", 7) == 0) {
 		up_users();
 		ret = do_cg_open(path, fi);
 		down_users();
 		return ret;
 	}
-
 	if (strncmp(path, "/proc", 5) == 0) {
 		up_users();
 		ret = do_proc_open(path, fi);
@@ -668,17 +614,15 @@ static int lxcfs_open(const char *path, struct fuse_file_info *fi)
 }
 
 static int lxcfs_read(const char *path, char *buf, size_t size, off_t offset,
-		      struct fuse_file_info *fi)
+		struct fuse_file_info *fi)
 {
 	int ret;
-
 	if (strncmp(path, "/cgroup", 7) == 0) {
 		up_users();
 		ret = do_cg_read(path, buf, size, offset, fi);
 		down_users();
 		return ret;
 	}
-
 	if (strncmp(path, "/proc", 5) == 0) {
 		up_users();
 		ret = do_proc_read(path, buf, size, offset, fi);
@@ -690,10 +634,9 @@ static int lxcfs_read(const char *path, char *buf, size_t size, off_t offset,
 }
 
 int lxcfs_write(const char *path, const char *buf, size_t size, off_t offset,
-		struct fuse_file_info *fi)
+	     struct fuse_file_info *fi)
 {
 	int ret;
-
 	if (strncmp(path, "/cgroup", 7) == 0) {
 		up_users();
 		ret = do_cg_write(path, buf, size, offset, fi);
@@ -712,14 +655,12 @@ static int lxcfs_flush(const char *path, struct fuse_file_info *fi)
 static int lxcfs_release(const char *path, struct fuse_file_info *fi)
 {
 	int ret;
-
 	if (strncmp(path, "/cgroup", 7) == 0) {
 		up_users();
 		ret = do_cg_release(path, fi);
 		down_users();
 		return ret;
 	}
-
 	if (strncmp(path, "/proc", 5) == 0) {
 		up_users();
 		ret = do_proc_release(path, fi);
@@ -737,9 +678,8 @@ static int lxcfs_fsync(const char *path, int datasync, struct fuse_file_info *fi
 
 int lxcfs_mkdir(const char *path, mode_t mode)
 {
+	int ret;
 	if (strncmp(path, "/cgroup", 7) == 0) {
-		int ret;
-
 		up_users();
 		ret = do_cg_mkdir(path, mode);
 		down_users();
@@ -752,7 +692,6 @@ int lxcfs_mkdir(const char *path, mode_t mode)
 int lxcfs_chown(const char *path, uid_t uid, gid_t gid)
 {
 	int ret;
-
 	if (strncmp(path, "/cgroup", 7) == 0) {
 		up_users();
 		ret = do_cg_chown(path, uid, gid);
@@ -775,29 +714,25 @@ int lxcfs_truncate(const char *path, off_t newsize)
 {
 	if (strncmp(path, "/cgroup", 7) == 0)
 		return 0;
-
 	return -EPERM;
 }
 
 int lxcfs_rmdir(const char *path)
 {
+	int ret;
 	if (strncmp(path, "/cgroup", 7) == 0) {
-		int ret;
-
 		up_users();
 		ret = do_cg_rmdir(path);
 		down_users();
 		return ret;
 	}
-
 	return -EPERM;
 }
 
 int lxcfs_chmod(const char *path, mode_t mode)
 {
+	int ret;
 	if (strncmp(path, "/cgroup", 7) == 0) {
-		int ret;
-
 		up_users();
 		ret = do_cg_chmod(path, mode);
 		down_users();
@@ -811,41 +746,46 @@ int lxcfs_chmod(const char *path, mode_t mode)
 }
 
 const struct fuse_operations lxcfs_ops = {
-    .getattr     = lxcfs_getattr,
-    .readlink    = NULL,
-    .getdir      = NULL,
-    .mknod       = NULL,
-    .mkdir       = lxcfs_mkdir,
-    .unlink      = NULL,
-    .rmdir       = lxcfs_rmdir,
-    .symlink     = NULL,
-    .rename      = NULL,
-    .link        = NULL,
-    .chmod       = lxcfs_chmod,
-    .chown       = lxcfs_chown,
-    .truncate    = lxcfs_truncate,
-    .utime       = NULL,
-    .open        = lxcfs_open,
-    .read        = lxcfs_read,
-    .release     = lxcfs_release,
-    .write       = lxcfs_write,
-    .statfs      = NULL,
-    .flush       = lxcfs_flush,
-    .fsync       = lxcfs_fsync,
-    .setxattr    = NULL,
-    .getxattr    = NULL,
-    .listxattr   = NULL,
-    .removexattr = NULL,
-    .opendir     = lxcfs_opendir,
-    .readdir     = lxcfs_readdir,
-    .releasedir  = lxcfs_releasedir,
-    .fsyncdir    = NULL,
-    .init        = NULL,
-    .destroy     = NULL,
-    .access      = lxcfs_access,
-    .create      = NULL,
-    .ftruncate   = NULL,
-    .fgetattr    = NULL,
+	.getattr = lxcfs_getattr,
+	.readlink = NULL,
+	.getdir = NULL,
+	.mknod = NULL,
+	.mkdir = lxcfs_mkdir,
+	.unlink = NULL,
+	.rmdir = lxcfs_rmdir,
+	.symlink = NULL,
+	.rename = NULL,
+	.link = NULL,
+	.chmod = lxcfs_chmod,
+	.chown = lxcfs_chown,
+	.truncate = lxcfs_truncate,
+	.utime = NULL,
+
+	.open = lxcfs_open,
+	.read = lxcfs_read,
+	.release = lxcfs_release,
+	.write = lxcfs_write,
+
+	.statfs = NULL,
+	.flush = lxcfs_flush,
+	.fsync = lxcfs_fsync,
+
+	.setxattr = NULL,
+	.getxattr = NULL,
+	.listxattr = NULL,
+	.removexattr = NULL,
+
+	.opendir = lxcfs_opendir,
+	.readdir = lxcfs_readdir,
+	.releasedir = lxcfs_releasedir,
+
+	.fsyncdir = NULL,
+	.init = NULL,
+	.destroy = NULL,
+	.access = lxcfs_access,
+	.create = NULL,
+	.ftruncate = NULL,
+	.fgetattr = NULL,
 };
 
 static void usage()
@@ -863,27 +803,27 @@ static void usage()
 
 static bool is_help(char *w)
 {
-	if (strcmp(w, "-h") == 0 || strcmp(w, "--help") == 0 ||
-	    strcmp(w, "-help") == 0 || strcmp(w, "help") == 0)
+	if (strcmp(w, "-h") == 0 ||
+			strcmp(w, "--help") == 0 ||
+			strcmp(w, "-help") == 0 ||
+			strcmp(w, "help") == 0)
 		return true;
-
 	return false;
 }
 
 bool swallow_arg(int *argcp, char *argv[], char *which)
 {
-	for (int i = 1; argv[i]; i++) {
+	int i;
+
+	for (i = 1; argv[i]; i++) {
 		if (strcmp(argv[i], which) != 0)
 			continue;
-
-		for (; argv[i]; i++)
-			argv[i] = argv[i + 1];
-
+		for (; argv[i]; i++) {
+			argv[i] = argv[i+1];
+		}
 		(*argcp)--;
-
 		return true;
 	}
-
 	return false;
 }
 
@@ -892,30 +832,25 @@ bool swallow_option(int *argcp, char *argv[], char *opt, char **v)
 	int i;
 
 	for (i = 1; argv[i]; i++) {
-		if (!argv[i + 1])
+		if (!argv[i+1])
 			continue;
-
 		if (strcmp(argv[i], opt) != 0)
 			continue;
-
 		do {
-			*v = strdup(argv[i + 1]);
+			*v = strdup(argv[i+1]);
 		} while (!*v);
-
-		for (; argv[i + 1]; i++)
-			argv[i] = argv[i + 2];
-
+		for (; argv[i+1]; i++) {
+			argv[i] = argv[i+2];
+		}
 		(*argcp) -= 2;
-
 		return true;
 	}
-
 	return false;
 }
 
 static int set_pidfile(char *pidfile)
 {
-	__do_close_prot_errno int fd = -EBADF;
+	int fd;
 	char buf[50];
 	struct flock fl;
 
@@ -931,9 +866,9 @@ static int set_pidfile(char *pidfile)
 	}
 
 	if (fcntl(fd, F_SETLK, &fl) == -1) {
-		if (errno == EAGAIN || errno == EACCES) {
-			fprintf(stderr, "PID file '%s' is already locked\n",
-				pidfile);
+		if (errno  == EAGAIN || errno == EACCES) {
+			fprintf(stderr, "PID file '%s' is already locked.\n", pidfile);
+			close(fd);
 			return -1;
 		}
 		fprintf(stderr, "Warning; unable to lock PID file, proceeding.\n");
@@ -941,16 +876,18 @@ static int set_pidfile(char *pidfile)
 
 	if (ftruncate(fd, 0) == -1) {
 		fprintf(stderr, "Error truncating PID file '%s': %m", pidfile);
+		close(fd);
 		return -1;
 	}
 
-	snprintf(buf, 50, "%ld\n", (long)getpid());
+	snprintf(buf, 50, "%ld\n", (long) getpid());
 	if (write(fd, buf, strlen(buf)) != strlen(buf)) {
 		fprintf(stderr, "Error writing to PID file '%s': %m", pidfile);
+		close(fd);
 		return -1;
 	}
 
-	return move_fd(fd);
+	return fd;
 }
 
 int main(int argc, char *argv[])
@@ -970,7 +907,7 @@ int main(int argc, char *argv[])
 
 	struct lxcfs_opts *opts;
 	opts = malloc(sizeof(struct lxcfs_opts));
-	if (!opts) {
+	if (opts == NULL) {
 		fprintf(stderr, "Error allocating memory for options.\n");
 		goto out;
 	}
@@ -980,12 +917,12 @@ 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) {
@@ -1002,7 +939,6 @@ int main(int argc, char *argv[])
 		free(v);
 		v = NULL;
 	}
-
 	if (swallow_option(&argc, argv, "-p", &v))
 		pidfile = v;
 
@@ -1010,7 +946,6 @@ int main(int argc, char *argv[])
 		fprintf(stderr, "%s\n", VERSION);
 		exit(EXIT_SUCCESS);
 	}
-
 	if (argc != 2 || is_help(argv[1]))
 		usage();
 
@@ -1038,9 +973,7 @@ int main(int argc, char *argv[])
 		pidfile = alloca(pidfile_len);
 		snprintf(pidfile, pidfile_len, "%s/lxcfs.pid", RUNTIME_PATH);
 	}
-
-	pidfd = set_pidfile(pidfile);
-	if (pidfd < 0)
+	if ((pidfd = set_pidfile(pidfile)) < 0)
 		goto out;
 
 	if (load_use && start_loadavg() != 0)
@@ -1048,19 +981,15 @@ int main(int argc, char *argv[])
 
 	if (!fuse_main(nargs, newargv, &lxcfs_ops, opts))
 		ret = EXIT_SUCCESS;
-
 	if (load_use)
 		stop_loadavg();
 
 out:
 	if (dlopen_handle)
 		dlclose(dlopen_handle);
-
 	if (pidfile)
 		unlink(pidfile);
-
 	if (pidfd > 0)
 		close(pidfd);
-
 	exit(ret);
 }
diff --git a/memory_utils.h b/memory_utils.h
deleted file mode 100644
index dce98de..0000000
--- a/memory_utils.h
+++ /dev/null
@@ -1,87 +0,0 @@
-/* liblxcapi
- *
- * Copyright © 2019 Christian Brauner <christian.brauner at ubuntu.com>.
- * Copyright © 2019 Canonical Ltd.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
-
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
-
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#ifndef __LXC_MEMORY_UTILS_H
-#define __LXC_MEMORY_UTILS_H
-
-#include <dirent.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include "macro.h"
-
-static inline void __free_move__(void *p)
-{
-	free(*(void **)p);
-	*(void **)p = NULL;
-}
-
-static inline void __fclose_move__(FILE **f)
-{
-	if (*f) {
-		fclose(*f);
-		*f = NULL;
-	}
-}
-
-static inline void __closedir_move__(DIR **d)
-{
-	if (*d) {
-		closedir(*d);
-		*d = NULL;
-	}
-}
-
-#define close_prot_errno_disarm(fd) \
-	if (fd >= 0) {              \
-		int _e_ = errno;    \
-		close(fd);          \
-		errno = _e_;        \
-		fd = -EBADF;        \
-	}
-
-static inline void __close_move__(int *fd)
-{
-	close_prot_errno_disarm(*fd);
-}
-
-#define __do_close_prot_errno __attribute__((__cleanup__(__close_move__)))
-#define __do_free __attribute__((__cleanup__(__free_move__)))
-#define __do_fclose __attribute__((__cleanup__(__fclose_move__)))
-#define __do_closedir __attribute__((__cleanup__(__closedir_move__)))
-
-#define move_ptr(ptr)                                 \
-	({                                            \
-		typeof(ptr) __internal_ptr__ = (ptr); \
-		(ptr) = NULL;                         \
-		__internal_ptr__;                     \
-	})
-
-#define move_fd(fd)                         \
-	({                                  \
-		int __internal_fd__ = (fd); \
-		(fd) = -EBADF;              \
-		__internal_fd__;            \
-	})
-
-#endif /* __LXC_MEMORY_UTILS_H */


More information about the lxc-devel mailing list