]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
move monitor-fifo and monitor-sock to /run
authorDwight Engen <dwight.engen@oracle.com>
Wed, 11 Sep 2013 15:44:19 +0000 (11:44 -0400)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Wed, 11 Sep 2013 18:59:06 +0000 (13:59 -0500)
Moving these files should allow $lxcpath to be a read-only fs.

Signed-off-by: Dwight Engen <dwight.engen@oracle.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/lxc/lxc_monitord.c
src/lxc/lxclock.c
src/lxc/monitor.c
src/lxc/monitor.h
src/lxc/utils.c
src/lxc/utils.h

index c5e156e107e53f6d0c8efe80cd801d08097c062d..fda6cf4b52743fdeaef9ee1a7a8f994498c2f206 100644 (file)
@@ -76,11 +76,9 @@ static int lxc_monitord_fifo_create(struct lxc_monitor *mon)
        char fifo_path[PATH_MAX];
        int ret;
 
-       ret = snprintf(fifo_path, sizeof(fifo_path), "%s/monitor-fifo", mon->lxcpath);
-       if (ret < 0 || ret >= sizeof(fifo_path)) {
-               ERROR("lxcpath too long to monitor fifo");
-               return -1;
-       }
+       ret = lxc_monitor_fifo_name(mon->lxcpath, fifo_path, sizeof(fifo_path), 1);
+       if (ret < 0)
+               return ret;
 
        ret = mknod(fifo_path, S_IFIFO|S_IRUSR|S_IWUSR, 0);
        if (ret < 0) {
@@ -102,11 +100,10 @@ static int lxc_monitord_fifo_delete(struct lxc_monitor *mon)
        char fifo_path[PATH_MAX];
        int ret;
 
-       ret = snprintf(fifo_path, sizeof(fifo_path), "%s/monitor-fifo", mon->lxcpath);
-       if (ret < 0 || ret >= sizeof(fifo_path)) {
-               ERROR("lxcpath too long to monitor fifo");
-               return -1;
-       }
+       ret = lxc_monitor_fifo_name(mon->lxcpath, fifo_path, sizeof(fifo_path), 0);
+       if (ret < 0)
+               return ret;
+
        unlink(fifo_path);
        return 0;
 }
index 79ebf84a76d79a5a357c4c16808430bf03d60ecf..1d6a86c61297bfa97215415ea8dbddcb6b537876 100644 (file)
@@ -56,10 +56,7 @@ static char *lxclock_name(const char *p, const char *n)
 
        /* length of "/lock/lxc/" + $lxcpath + "/" + $lxcname + '\0' */
        len = strlen("/lock/lxc/") + strlen(n) + strlen(p) + 2;
-       rundir = getenv("XDG_RUNTIME_DIR");
-       if (geteuid() == 0 || rundir == NULL)
-               rundir = "/run";
-
+       rundir = get_rundir();
        len += strlen(rundir);
 
        if ((dest = malloc(len)) == NULL)
index 412d38f4b5afb300fceecc4f8bcfc513ef8f191a..bdcc581c69e335a2011cf501a40bed1a635b6983 100644 (file)
@@ -40,6 +40,7 @@
 #include "af_unix.h"
 
 #include <lxc/log.h>
+#include <lxc/lxclock.h>
 #include <lxc/state.h>
 #include <lxc/monitor.h>
 #include <lxc/utils.h>
 lxc_log_define(lxc_monitor, lxc);
 
 /* routines used by monitor publishers (containers) */
+int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path_sz,
+                         int do_mkdirp)
+{
+       int ret;
+       const char *rundir;
+
+       rundir = get_rundir();
+       if (do_mkdirp) {
+               ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath);
+               if (ret < 0 || ret >= fifo_path_sz) {
+                       ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
+                       return -1;
+               }
+               process_lock();
+               ret = mkdir_p(fifo_path, 0755);
+               process_unlock();
+               if (ret < 0) {
+                       ERROR("unable to create monitor fifo dir %s", fifo_path);
+                       return ret;
+               }
+       }
+       ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s/monitor-fifo", rundir, lxcpath);
+       if (ret < 0 || ret >= fifo_path_sz) {
+               ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
+               return -1;
+       }
+       return 0;
+}
+
 static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath)
 {
        int fd,ret;
        char fifo_path[PATH_MAX];
 
        BUILD_BUG_ON(sizeof(*msg) > PIPE_BUF); /* write not guaranteed atomic */
-       ret = snprintf(fifo_path, sizeof(fifo_path), "%s/monitor-fifo", lxcpath);
-       if (ret < 0 || ret >= sizeof(fifo_path)) {
-               ERROR("lxcpath too long to open monitor fifo");
+
+       ret = lxc_monitor_fifo_name(lxcpath, fifo_path, sizeof(fifo_path), 0);
+       if (ret < 0)
                return;
-       }
 
        fd = open(fifo_path, O_WRONLY);
        if (fd < 0) {
@@ -98,6 +127,7 @@ int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
        size_t len;
        int ret;
        char *sockname = &addr->sun_path[0]; // 1 for abstract
+       const char *rundir;
 
        /* addr.sun_path is only 108 bytes.
         * should we take a hash of lxcpath? a subset of it? ftok()? we need
@@ -106,9 +136,23 @@ int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
        memset(addr, 0, sizeof(*addr));
        addr->sun_family = AF_UNIX;
        len = sizeof(addr->sun_path) - 1;
-       ret = snprintf(sockname, len, "%s/monitor-sock", lxcpath);
+       rundir = get_rundir();
+       ret = snprintf(sockname, len, "%s/lxc/%s", rundir, lxcpath);
+       if (ret < 0 || ret >= len) {
+               ERROR("rundir/lxcpath (%s/%s) too long for monitor unix socket", rundir, lxcpath);
+               return -1;
+       }
+       process_lock();
+       ret = mkdir_p(sockname, 0755);
+       process_unlock();
+       if (ret < 0) {
+               ERROR("unable to create monitor sock %s", sockname);
+               return ret;
+       }
+
+       ret = snprintf(sockname, len, "%s/lxc/%s/monitor-sock", rundir, lxcpath);
        if (ret < 0 || ret >= len) {
-               ERROR("lxcpath too long for unix socket");
+               ERROR("rundir/lxcpath (%s/%s) too long for monitor unix socket", rundir, lxcpath);
                return -1;
        }
        return 0;
index 2a61091a5bde6536255b38cdc625d7124867fbdb..809391993aba7a5443e26fb396f3415965474e08 100644 (file)
@@ -41,6 +41,8 @@ struct lxc_msg {
 
 extern int lxc_monitor_open(const char *lxcpath);
 extern int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr);
+extern int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path,
+                                size_t fifo_path_sz, int do_mkdirp);
 extern void lxc_monitor_send_state(const char *name, lxc_state_t state,
                            const char *lxcpath);
 extern int lxc_monitord_spawn(const char *lxcpath);
index ba0604aa310ccdbe2a6b96e169541eb253ca66b6..2e665858f9639ebed064c49eb40a02f2db60ea31 100644 (file)
@@ -318,6 +318,16 @@ const char *default_lxc_path(void)
        return lxc_global_config_value("lxcpath");
 }
 
+const char *get_rundir()
+{
+       const char *rundir;
+
+       rundir = getenv("XDG_RUNTIME_DIR");
+       if (geteuid() == 0 || rundir == NULL)
+               rundir = "/run";
+       return rundir;
+}
+
 int wait_for_pid(pid_t pid)
 {
        int status, ret;
index 1a7b5518a7ebab323739c7a8ede681027de75a28..9776d188ce16e9a78499b93f5ba910edbac3a4e2 100644 (file)
@@ -37,6 +37,8 @@ extern int lxc_rmdir_onedev(char *path);
 extern int lxc_setup_fs(void);
 extern int get_u16(unsigned short *val, const char *arg, int base);
 extern int mkdir_p(const char *dir, mode_t mode);
+extern const char *get_rundir(void);
+
 /*
  * Return a buffer containing the default container path.
  * Caller must NOT free this buffer, since it may be static.