[lxc-devel] [lxcfs/master] 2016 02 03/pidfile

hallyn on Github lxc-bot at linuxcontainers.org
Wed Feb 3 21:31:44 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 412 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160203/18fb89e6/attachment.bin>
-------------- next part --------------
From 071109fd71c4cbe5b15e802be66a0dd0c26624d2 Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge.hallyn at ubuntu.com>
Date: Wed, 3 Feb 2016 13:04:04 -0800
Subject: [PATCH 1/3] drop a debugging printf

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 bindings.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/bindings.c b/bindings.c
index ef96513..30fe31e 100644
--- a/bindings.c
+++ b/bindings.c
@@ -373,7 +373,6 @@ static bool store_hierarchy(char *stridx, char *h)
 		size_t n = (num_hierarchies / ALLOC_NUM) + 1;
 		n *= ALLOC_NUM;
 		char **tmp = realloc(hierarchies, n * sizeof(char *));
-		printf("allocated %d\n", n);
 		if (!tmp) {
 			fprintf(stderr, "Out of memory\n");
 			exit(1);

From e190ee91a070999180b780dbe1b769f91d769cdf Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge.hallyn at ubuntu.com>
Date: Wed, 3 Feb 2016 13:24:11 -0800
Subject: [PATCH 2/3] Add a pidfile

and update testsuite to use pidfile

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 lxcfs.c              | 95 +++++++++++++++++++++++++++++++++++++++++++---------
 tests/main.sh        |  4 ++-
 tests/test_reload.sh |  4 ++-
 3 files changed, 86 insertions(+), 17 deletions(-)

diff --git a/lxcfs.c b/lxcfs.c
index c6bf62a..6e87c4c 100644
--- a/lxcfs.c
+++ b/lxcfs.c
@@ -661,7 +661,8 @@ static void usage(const char *me)
 {
 	fprintf(stderr, "Usage:\n");
 	fprintf(stderr, "\n");
-	fprintf(stderr, "%s mountpoint\n", me);
+	fprintf(stderr, "%s [-p pidfile] mountpoint\n", me);
+	fprintf(stderr, "  Default pidfile is %s/lxcfs.pid\n", RUNTIME_PATH);
 	fprintf(stderr, "%s -h\n", me);
 	exit(1);
 }
@@ -691,7 +692,7 @@ void swallow_arg(int *argcp, char *argv[], char *which)
 	}
 }
 
-void swallow_option(int *argcp, char *argv[], char *opt, char *v)
+bool swallow_option(int *argcp, char *argv[], char *opt, char **v)
 {
 	int i;
 
@@ -700,16 +701,16 @@ void swallow_option(int *argcp, char *argv[], char *opt, char *v)
 			continue;
 		if (strcmp(argv[i], opt) != 0)
 			continue;
-		if (strcmp(argv[i+1], v) != 0) {
-			fprintf(stderr, "Warning: unexpected fuse option %s\n", v);
-			exit(1);
-		}
+		do {
+			*v = strdup(argv[i+1]);
+		} while (!*v);
 		for (; argv[i+1]; i++) {
 			argv[i] = argv[i+2];
 		}
 		(*argcp) -= 2;
-		return;
+		return true;
 	}
+	return false;
 }
 
 static bool mkdir_p(const char *dir, mode_t mode)
@@ -833,9 +834,53 @@ static bool cgfs_setup_controllers(void)
 	return true;
 }
 
+static int set_pidfile(char *pidfile)
+{
+	int fd;
+	char buf[50];
+	struct flock fl;
+
+	fl.l_type = F_WRLCK;
+	fl.l_whence = SEEK_SET;
+	fl.l_start = 0;
+	fl.l_len = 0;
+
+	fd = open(pidfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
+	if (fd == -1) {
+		fprintf(stderr, "Could not open pidfile %s: %m", pidfile);
+		return -1;
+	}
+
+	if (fcntl(fd, F_SETLK, &fl) == -1) {
+		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");
+	}
+
+	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());
+	if (write(fd, buf, strlen(buf)) != strlen(buf)) {
+		fprintf(stderr, "Error writing to PID file '%s': %m", pidfile);
+		close(fd);
+		return -1;
+	}
+
+	return fd;
+}
+
 int main(int argc, char *argv[])
 {
-	int ret = -1;
+	int ret = -1, pidfd;
+	char *pidfile = NULL, *v = NULL;
+	size_t pidfile_len;
 	/*
 	 * what we pass to fuse_main is:
 	 * argv[0] -s -f -o allow_other,directio argv[1] NULL
@@ -843,12 +888,6 @@ int main(int argc, char *argv[])
 	int nargs = 5, cnt = 0;
 	char *newargv[6];
 
-	dlopen_handle = dlopen("liblxcfs.so", RTLD_LAZY);
-	if (!dlopen_handle) {
-		fprintf(stderr, "Failed to open liblxcfs\n");
-		exit(1);
-	}
-	signal(SIGUSR1, reload_handler);
 #ifdef FORTRAVIS
 	/* for travis which runs on 12.04 */
 	if (glib_check_version (2, 36, 0) != NULL)
@@ -858,7 +897,16 @@ int main(int argc, char *argv[])
 	/* accomodate older init scripts */
 	swallow_arg(&argc, argv, "-s");
 	swallow_arg(&argc, argv, "-f");
-	swallow_option(&argc, argv, "-o", "allow_other");
+	if (swallow_option(&argc, argv, "-o", &v)) {
+		if (strcmp(v, "allow_other") != 0) {
+			fprintf(stderr, "Warning: unexpected fuse option %s\n", v);
+			exit(1);
+		}
+		free(v);
+		v = NULL;
+	}
+	if (swallow_option(&argc, argv, "-p", &v))
+		pidfile = v;
 
 	if (argc == 2  && strcmp(argv[1], "--version") == 0) {
 		fprintf(stderr, "%s\n", VERSION);
@@ -867,6 +915,13 @@ int main(int argc, char *argv[])
 	if (argc != 2 || is_help(argv[1]))
 		usage(argv[0]);
 
+	dlopen_handle = dlopen("liblxcfs.so", RTLD_LAZY);
+	if (!dlopen_handle) {
+		fprintf(stderr, "Failed to open liblxcfs\n");
+		exit(1);
+	}
+	signal(SIGUSR1, reload_handler);
+
 	newargv[cnt++] = argv[0];
 	newargv[cnt++] = "-f";
 	newargv[cnt++] = "-o";
@@ -877,9 +932,19 @@ int main(int argc, char *argv[])
 	if (!cgfs_setup_controllers())
 		goto out;
 
+	if (!pidfile) {
+		pidfile_len = strlen(RUNTIME_PATH) + strlen("/lxcfs.pid") + 1;
+		pidfile = alloca(pidfile_len);
+		snprintf(pidfile, pidfile_len, "%s/lxcfs.pid", RUNTIME_PATH);
+	}
+	if ((pidfd = set_pidfile(pidfile)) < 0)
+		goto out;
+
 	ret = fuse_main(nargs, newargv, &lxcfs_ops, NULL);
 
 	dlclose(dlopen_handle);
+	unlink(pidfile);
+	close(pidfd);
 
 out:
 	return ret;
diff --git a/tests/main.sh b/tests/main.sh
index 95c2f3c..01a900b 100755
--- a/tests/main.sh
+++ b/tests/main.sh
@@ -6,6 +6,7 @@ set -ex
 
 # Run lxcfs testsuite
 export LXCFSDIR=$(mktemp -d)
+pidfile=$(mktemp)
 
 cmdline=$(realpath $0)
 dirname=$(dirname ${cmdline})
@@ -22,6 +23,7 @@ cleanup() {
 		umount -l ${LXCFSDIR}
 		rmdir ${LXCFSDIR}
 	fi
+	rm -f ${pidfile}
 	if [ ${FAILED} -eq 1 ]; then
 		echo "FAILED at $TESTCASE"
 		exit 1
@@ -35,7 +37,7 @@ lxcfs=${topdir}/lxcfs
 
 if [ -x ${lxcfs} ]; then
 	echo "Running ${lxcfs} ${LXCFSDIR}"
-	${lxcfs} ${LXCFSDIR} &
+	${lxcfs} -p ${pidfile} ${LXCFSDIR} &
 	p=$!
 else
 	pidof lxcfs
diff --git a/tests/test_reload.sh b/tests/test_reload.sh
index e48124a..4560ba2 100755
--- a/tests/test_reload.sh
+++ b/tests/test_reload.sh
@@ -10,6 +10,7 @@ topdir=$(dirname ${dirname})
 
 testdir=`mktemp -t -d libs.XXX`
 installdir=`mktemp -t -d libs.XXX`
+pidfile=$(mktemp)
 libdir=${installdir}/usr/lib
 bindir=${installdir}/usr/bin
 lxcfspid=-1
@@ -26,6 +27,7 @@ cleanup() {
   fi
   rm -rf ${testdir} ${installdir}
   rm -f iwashere
+  rm -f ${pidfile}
   if [ ${FAILED} -eq 1 ]; then
     echo "liblxcfs.so reload test FAILED"
   else
@@ -38,7 +40,7 @@ trap cleanup EXIT SIGHUP SIGINT SIGTERM
 ( cd ${topdir}; DESTDIR=${installdir} make install )
 export LD_LIBRARY_PATH=${libdir}
 
-${bindir}/lxcfs ${testdir} &
+${bindir}/lxcfs -p ${pidfile} ${testdir} &
 lxcfspid=$!
 count=1
 while [ ! -d ${testdir}/proc ]; do

From 3affb0c9537c9dd5853a987c865c1952b48728e9 Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge.hallyn at ubuntu.com>
Date: Wed, 3 Feb 2016 13:25:19 -0800
Subject: [PATCH 3/3] drop glibc travis exceptions - we don't use glibc any
 more

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 bindings.c |  5 -----
 lxcfs.c    | 11 -----------
 2 files changed, 16 deletions(-)

diff --git a/bindings.c b/bindings.c
index 30fe31e..4bb0631 100644
--- a/bindings.c
+++ b/bindings.c
@@ -28,11 +28,6 @@
 #include <sys/epoll.h>
 #include <wait.h>
 
-#ifdef FORTRAVIS
-#define GLIB_DISABLE_DEPRECATION_WARNINGS
-#include <glib-object.h>
-#endif
-
 #include "bindings.h"
 
 #include "config.h" // for VERSION
diff --git a/lxcfs.c b/lxcfs.c
index 6e87c4c..1d1016c 100644
--- a/lxcfs.c
+++ b/lxcfs.c
@@ -28,11 +28,6 @@
 #include <sys/epoll.h>
 #include <wait.h>
 
-#ifdef FORTRAVIS
-#define GLIB_DISABLE_DEPRECATION_WARNINGS
-#include <glib-object.h>
-#endif
-
 #include "config.h" // for VERSION
 #include "bindings.h"
 
@@ -888,12 +883,6 @@ int main(int argc, char *argv[])
 	int nargs = 5, cnt = 0;
 	char *newargv[6];
 
-#ifdef FORTRAVIS
-	/* for travis which runs on 12.04 */
-	if (glib_check_version (2, 36, 0) != NULL)
-		g_type_init ();
-#endif
-
 	/* accomodate older init scripts */
 	swallow_arg(&argc, argv, "-s");
 	swallow_arg(&argc, argv, "-f");


More information about the lxc-devel mailing list