]> git.proxmox.com Git - pve-cluster.git/blobdiff - data/src/pmxcfs.c
pmxcfs: do not wait artificially when stopping
[pve-cluster.git] / data / src / pmxcfs.c
index 69b3fb2e18ba1b0edde9b2750266dce687c08074..bb5b4ad393e8d837234eab87ecb31de0302f268b 100644 (file)
@@ -27,7 +27,6 @@
 #include <string.h>
 #include <unistd.h>
 #include <glib.h>
-#include <syslog.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mount.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+
 #include <qb/qbdefs.h>
 #include <qb/qbutil.h>
+#include <qb/qblog.h>
 
 #include "cfs-utils.h"
 #include "cfs-plug.h"
 #define DBFILENAME VARLIBDIR "/config.db"
 #define LOCKFILE VARLIBDIR "/.pmxcfs.lockfile"
 
-#define CFSDIR "/etc/pve" 
+#define CFSDIR "/etc/pve"
 
 cfs_t cfs = {
        .debug = 0,
-       .print_to_console = 1,
 };
 
 static struct fuse *fuse = NULL;
@@ -72,8 +71,7 @@ static cfs_plug_t *root_plug;
 
 static void glib_print_handler(const gchar *string)
 {
-       if (cfs.debug || cfs.print_to_console)
-               printf("%s", string);
+       printf("%s", string);
 }
 
 static void glib_log_handler(const gchar *log_domain,
@@ -110,7 +108,7 @@ static cfs_plug_t *find_plug(const char *path, char **sub)
 
        cfs_debug("find_plug end %s = %p (%s)", path, plug, subpath);
 
-       if (subpath && subpath[0]) 
+       if (subpath && subpath[0])
                *sub = g_strdup(subpath);
 
        g_free(tmppath);
@@ -131,16 +129,25 @@ static int cfs_fuse_getattr(const char *path, struct stat *stbuf)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (plug && plug->ops && plug->ops->getattr) {
                ret = plug->ops->getattr(plug, subpath ? subpath : "", stbuf);
 
                stbuf->st_gid = cfs.gid;
 
-               stbuf->st_mode &= 0777750; // no access for other users
-
-               if (path_is_private(path))
+               if (path_is_private(path)) {
                        stbuf->st_mode &= 0777700;
+               } else {
+                       if (S_ISDIR(stbuf->st_mode) || S_ISLNK(stbuf->st_mode)) {
+                               stbuf->st_mode &= 0777755; // access for other users
+                       } else {
+                               if (path_is_lxc_conf(path)) {
+                                       stbuf->st_mode &= 0777755; // access for other users
+                               } else {
+                                       stbuf->st_mode &= 0777750; // no access for other users
+                               }
+                       }
+               }
        }
 
        cfs_debug("leave cfs_fuse_getattr %s (%d)", path, ret);
@@ -164,7 +171,7 @@ static int cfs_fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -172,10 +179,45 @@ static int cfs_fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
                ret = plug->ops->readdir(plug, subpath ? subpath : "", buf, filler, 0, fi);
 ret:
        cfs_debug("leave cfs_fuse_readdir %s (%d)", path, ret);
-               
+
        if (subpath)
                g_free(subpath);
-       
+
+       return ret;
+}
+
+static int cfs_fuse_chmod(const char *path, mode_t mode)
+{
+       int ret = -EPERM;
+
+       cfs_debug("enter cfs_fuse_chmod %s", path);
+
+       mode_t allowed_mode = (S_IRUSR | S_IWUSR);
+       if (!path_is_private(path))
+               allowed_mode |= (S_IRGRP);
+
+       // allow only setting our supported modes (0600 for priv, 0640 for rest)
+       // mode has additional bits set, which we ignore; see stat(2)
+       if ((mode & ALLPERMS) == allowed_mode)
+               ret = 0;
+
+       cfs_debug("leave cfs_fuse_chmod %s (%d) mode: %o", path, ret, (int)mode);
+
+       return ret;
+}
+
+static int cfs_fuse_chown(const char *path, uid_t user, gid_t group)
+{
+       int ret = -EPERM;
+
+       cfs_debug("enter cfs_fuse_chown %s", path);
+
+       // we get -1 if no change should be made
+       if ((user == 0 || user == -1) && (group == cfs.gid || group == -1))
+               ret = 0;
+
+       cfs_debug("leave cfs_fuse_chown %s (%d) (uid: %d; gid: %d)", path, ret, user, group);
+
        return ret;
 }
 
@@ -187,7 +229,7 @@ static int cfs_fuse_mkdir(const char *path, mode_t mode)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -211,7 +253,7 @@ static int cfs_fuse_rmdir(const char *path)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -268,8 +310,8 @@ static int cfs_fuse_open(const char *path, struct fuse_file_info *fi)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
-       if (plug && plug->ops) { 
+
+       if (plug && plug->ops) {
                if ((subpath || !plug->ops->readdir) && plug->ops->open) {
                        ret = plug->ops->open(plug, subpath ? subpath : "", fi);
                }
@@ -288,14 +330,14 @@ static int cfs_fuse_read(const char *path, char *buf, size_t size, off_t offset,
 {
        (void) fi;
 
-       cfs_debug("enter cfs_fuse_read %s %lu %ld", path, size, offset);
+       cfs_debug("enter cfs_fuse_read %s %zu %jd", path, size, offset);
 
        int ret = -EACCES;
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
-       if (plug && plug->ops) { 
+
+       if (plug && plug->ops) {
                if ((subpath || !plug->ops->readdir) && plug->ops->read)
                        ret = plug->ops->read(plug, subpath ? subpath : "", buf, size, offset, fi);
        }
@@ -313,19 +355,19 @@ static int cfs_fuse_write(const char *path, const char *buf, size_t size,
 {
        (void) fi;
 
-       cfs_debug("enter cfs_fuse_write %s %lu %ld", path, size, offset);
+       cfs_debug("enter cfs_fuse_write %s %zu %jd", path, size, offset);
 
        int ret = -EACCES;
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
-       if (plug && plug->ops) { 
+
+       if (plug && plug->ops) {
                if ((subpath || !plug->ops->readdir) && plug->ops->write)
-               ret = plug->ops->write(plug, subpath ? subpath : "", 
+               ret = plug->ops->write(plug, subpath ? subpath : "",
                                       buf, size, offset, fi);
        }
-       
+
        cfs_debug("leave cfs_fuse_write %s (%d)", path, ret);
 
        if (subpath)
@@ -336,14 +378,14 @@ static int cfs_fuse_write(const char *path, const char *buf, size_t size,
 
 static int cfs_fuse_truncate(const char *path, off_t size)
 {
-       cfs_debug("enter cfs_fuse_truncate %s %ld", path, size);
+       cfs_debug("enter cfs_fuse_truncate %s %jd", path, size);
 
        int ret = -EACCES;
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
-       if (plug && plug->ops) { 
+
+       if (plug && plug->ops) {
                if ((subpath || !plug->ops->readdir) && plug->ops->truncate)
                        ret = plug->ops->truncate(plug, subpath ? subpath : "", size);
        }
@@ -364,7 +406,7 @@ static int cfs_fuse_create(const char *path, mode_t mode, struct fuse_file_info
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -388,7 +430,7 @@ static int cfs_fuse_unlink(const char *path)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -400,7 +442,7 @@ ret:
 
        if (subpath)
                g_free(subpath);
-       
+
        return ret;
 }
 
@@ -412,13 +454,13 @@ static int cfs_fuse_readlink(const char *path, char *buf, size_t max)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
        if (plug->ops && plug->ops->readlink)
                ret = plug->ops->readlink(plug, subpath ? subpath : "", buf, max);
-       
+
 ret:
        cfs_debug("leave cfs_fuse_readlink %s (%d)", path, ret);
 
@@ -436,13 +478,13 @@ static int cfs_fuse_utimens(const char *path, const struct timespec tv[2])
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
        if (plug->ops && plug->ops->utimens)
                ret = plug->ops->utimens(plug, subpath ? subpath : "", tv);
-       
+
 ret:
        cfs_debug("leave cfs_fuse_utimens %s (%d)", path, ret);
 
@@ -481,7 +523,9 @@ static struct fuse_operations fuse_ops = {
        .readlink = cfs_fuse_readlink,
        .utimens = cfs_fuse_utimens,
        .statfs = cfs_fuse_statfs,
-       .init = cfs_fuse_init
+       .init = cfs_fuse_init,
+       .chown = cfs_fuse_chown,
+       .chmod = cfs_fuse_chmod
 };
 
 static char *
@@ -559,11 +603,53 @@ create_dot_clusterlog_cb(cfs_plug_t *plug)
 static char *
 read_debug_setting_cb(cfs_plug_t *plug)
 {
-       return g_strdup_printf("%d\n", !!cfs.debug); 
+       return g_strdup_printf("%d\n", !!cfs.debug);
+}
+
+static void
+my_qb_log_filter(struct qb_log_callsite *cs)
+{
+       int32_t priority = cfs.debug ? LOG_DEBUG : LOG_INFO;
+
+       if (qb_bit_is_set(cs->tags, QB_LOG_TAG_LIBQB_MSG_BIT)) {
+               if (cs->priority <= (cfs.debug ? priority : LOG_WARNING)) {
+                       qb_bit_set(cs->targets, QB_LOG_SYSLOG);
+               } else {
+                       qb_bit_clear(cs->targets, QB_LOG_SYSLOG);
+               }
+               if (cs->priority <= priority) {
+                       qb_bit_set(cs->targets, QB_LOG_STDERR);
+               } else {
+                       qb_bit_clear(cs->targets, QB_LOG_STDERR);
+               }
+       } else {
+               if (cs->priority <= priority) {
+                       qb_bit_set(cs->targets, QB_LOG_SYSLOG);
+                       qb_bit_set(cs->targets, QB_LOG_STDERR);
+               } else {
+                       qb_bit_clear(cs->targets, QB_LOG_SYSLOG);
+                       qb_bit_clear(cs->targets, QB_LOG_STDERR);
+               }
+       }
+}
+
+static void
+update_qb_log_settings(void)
+{
+       qb_log_filter_fn_set(my_qb_log_filter);
+
+       if (cfs.debug) {
+               qb_log_format_set(QB_LOG_SYSLOG, "[%g] %p: %b (%f:%l:%n)");
+               qb_log_format_set(QB_LOG_STDERR, "[%g] %p: %b (%f:%l:%n)");
+       } else {
+               qb_log_format_set(QB_LOG_SYSLOG, "[%g] %p: %b");
+               qb_log_format_set(QB_LOG_STDERR, "[%g] %p: %b");
+       }
 }
 
-static int write_debug_setting_cb(
-       cfs_plug_t *plug, 
+static int
+write_debug_setting_cb(
+       cfs_plug_t *plug,
        const char *buf,
        size_t size)
 {
@@ -576,12 +662,14 @@ static int write_debug_setting_cb(
                if (cfs.debug) {
                        cfs_message("disable debug mode");
                        cfs.debug = 0;
+                       update_qb_log_settings();
                }
                return 2;
        } else if (strncmp(buf, "1\n", 2) == 0) {
                if (!cfs.debug) {
-                       cfs_message("enable debug mode");
                        cfs.debug = 1;
+                       update_qb_log_settings();
+                       cfs_message("enable debug mode");
                }
                return 2;
        }
@@ -589,8 +677,7 @@ static int write_debug_setting_cb(
        return res;
 }
 
-
-static void 
+static void
 create_symlinks(cfs_plug_base_t *bplug, const char *nodename)
 {
        g_return_if_fail(bplug != NULL);
@@ -605,15 +692,20 @@ create_symlinks(cfs_plug_base_t *bplug, const char *nodename)
        lnk = cfs_plug_link_new("qemu-server", lnktarget);
        g_free(lnktarget);
        cfs_plug_base_insert(bplug, (cfs_plug_t*)lnk);
-       
+
        lnktarget = g_strdup_printf("nodes/%s/openvz", nodename);
        lnk = cfs_plug_link_new("openvz", lnktarget);
        g_free(lnktarget);
        cfs_plug_base_insert(bplug, (cfs_plug_t*)lnk);
 
+       lnktarget = g_strdup_printf("nodes/%s/lxc", nodename);
+       lnk = cfs_plug_link_new("lxc", lnktarget);
+       g_free(lnktarget);
+       cfs_plug_base_insert(bplug, (cfs_plug_t*)lnk);
+
        cfs_plug_func_t *fplug = cfs_plug_func_new(".version", 0440, create_dot_version_cb, NULL);
        cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug);
-       
+
        fplug = cfs_plug_func_new(".members", 0440, create_dot_members_cb, NULL);
        cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug);
 
@@ -633,28 +725,51 @@ create_symlinks(cfs_plug_base_t *bplug, const char *nodename)
 }
 
 static char *
-lookup_node_ip(const char *nodename) 
+lookup_node_ip(const char *nodename)
 {
+       char buf[INET6_ADDRSTRLEN];
        struct addrinfo *ainfo;
        struct addrinfo ahints;
+       char *res = NULL;
        memset(&ahints, 0, sizeof(ahints));
 
        if (getaddrinfo(nodename, NULL, &ahints, &ainfo))
                return NULL;
 
        if (ainfo->ai_family == AF_INET) {
-               char buf[INET6_ADDRSTRLEN];
                struct sockaddr_in *sa = (struct sockaddr_in *)ainfo->ai_addr;
                inet_ntop(ainfo->ai_family, &sa->sin_addr, buf, sizeof(buf));
                if (strncmp(buf, "127.", 4) != 0) {
-                       return g_strdup(buf);
+                       res = g_strdup(buf);
+               }
+       } else if (ainfo->ai_family == AF_INET6) {
+               struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ainfo->ai_addr;
+               inet_ntop(ainfo->ai_family, &sa->sin6_addr, buf, sizeof(buf));
+               if (strcmp(buf, "::1") != 0) {
+                       res = g_strdup(buf);
                }
        }
 
-       // ipv6 support ?
+       freeaddrinfo(ainfo);
 
-       return NULL;
+       return res;
+}
+
+static const char*
+log_tags_stringify(uint32_t tags) {
+       if (qb_bit_is_set(tags, QB_LOG_TAG_LIBQB_MSG_BIT)) {
+               return "libqb";
+       } else {
+               GQuark quark = tags;
+               const char *domain = g_quark_to_string(quark);
+               if (domain != NULL) {
+                       return domain;
+               } else {
+                       return "main";
+               }
+       }
 }
+
 int main(int argc, char *argv[])
 {
        int ret = -1;
@@ -667,7 +782,16 @@ int main(int argc, char *argv[])
        dfsm_t *dcdb = NULL;
        dfsm_t *status_fsm = NULL;
 
-       openlog("pmxcfs", LOG_PID, LOG_DAEMON);
+       qb_log_init("pmxcfs", LOG_DAEMON, LOG_DEBUG);
+       /* remove default filter */
+       qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_REMOVE,
+                         QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
+
+       qb_log_tags_stringify_fn_set(log_tags_stringify);
+
+       qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
+
+       update_qb_log_settings();
 
        g_set_print_handler(glib_print_handler);
        g_set_printerr_handler(glib_print_handler);
@@ -678,8 +802,8 @@ int main(int argc, char *argv[])
        GOptionEntry entries[] = {
                { "debug", 'd', 0, G_OPTION_ARG_NONE, &cfs.debug, "Turn on debug messages", NULL },
                { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Do not daemonize server", NULL },
-               { "local", 'l', 0, G_OPTION_ARG_NONE, &force_local_mode, 
-                 "Force local mode (ignore cluster.conf, force quorum)", NULL },
+               { "local", 'l', 0, G_OPTION_ARG_NONE, &force_local_mode,
+                 "Force local mode (ignore corosync.conf, force quorum)", NULL },
                { NULL },
        };
 
@@ -691,48 +815,53 @@ int main(int argc, char *argv[])
        {
                cfs_critical("option parsing failed: %s", err->message);
                g_error_free (err);
+               qb_log_fini();
                exit (1);
        }
        g_option_context_free(context);
 
        if (optind < argc) {
                cfs_critical("too many arguments");
+               qb_log_fini();
                exit(-1);
        }
-               
+
+       if (cfs.debug) {
+               update_qb_log_settings();
+       }
+
        struct utsname utsname;
        if (uname(&utsname) != 0) {
                cfs_critical("Unable to read local node name");
+               qb_log_fini();
                exit (-1);
        }
-       
-       for (int i=0; i < sizeof(utsname.nodename); i++) {
-               if (utsname.nodename[i] =='.') utsname.nodename[i] = 0;
-       }
+
+       char *dot = strchr(utsname.nodename, '.');
+       if (dot)
+               *dot = 0;
 
        cfs.nodename = g_strdup(utsname.nodename);
 
-       if (!(cfs.ip = lookup_node_ip(cfs.nodename))) { 
+       if (!(cfs.ip = lookup_node_ip(cfs.nodename))) {
                cfs_critical("Unable to get local IP address");
+               qb_log_fini();
                exit(-1);
        }
 
        struct group *www_data = getgrnam("www-data");
        if (!www_data) {
                cfs_critical("Unable to get www-data group ID");
+               qb_log_fini();
                exit (-1);
        }
        cfs.gid = www_data->gr_gid;
 
-       g_thread_init(NULL);
-
-       qb_util_set_log_function(ipc_log_fn);
-
        umask(027);
 
        mkdir(VARLIBDIR, 0755);
 
-       if ((lockfd = open(LOCKFILE, O_RDWR|O_CREAT|O_APPEND)) == -1) {
+       if ((lockfd = open(LOCKFILE, O_RDWR|O_CREAT|O_APPEND, 0600)) == -1) {
                cfs_critical("unable to create lock '%s': %s", LOCKFILE, strerror (errno));
                goto err;
        }
@@ -759,7 +888,7 @@ int main(int argc, char *argv[])
                goto err;
        }
 
-       // automatically import cluster.conf from host
+       // automatically import corosync.conf from host
        if (create && !force_local_mode) {
                char *cdata = NULL;
                gsize clen = 0;
@@ -767,46 +896,48 @@ int main(int argc, char *argv[])
 
                        guint32 mtime = time(NULL);
 
-                       memdb_create(memdb, "/cluster.conf", 0, mtime);
-                       if (memdb_write(memdb, "/cluster.conf", 0, mtime, cdata, clen, 0, 1) < 0) {
-                               cfs_critical("memdb_write failed - unable to import cluster.conf");
+                       memdb_create(memdb, "/corosync.conf", 0, mtime);
+                       if (memdb_write(memdb, "/corosync.conf", 0, mtime, cdata, clen, 0, 1) < 0) {
+                               cfs_critical("memdb_write failed - unable to import corosync.conf");
                                goto err;
                        }
                }
        }
 
-       // does cluster.conf exist?
+       // does corosync.conf exist?
        gpointer conf_data = NULL;
-       int len = memdb_read(memdb, "cluster.conf", &conf_data);
+       int len = memdb_read(memdb, "corosync.conf", &conf_data);
        if (len >= 0) {
                if (force_local_mode) {
-                       cfs_message("forcing local mode (althought cluster.conf exists)");
+                       cfs_message("forcing local mode (although corosync.conf exists)");
                        cfs_set_quorate(1, TRUE);
                } else {
                        if (!(dcdb = dcdb_new(memdb)))
                                goto err;
-                       dcdb_sync_cluster_conf(memdb, 1);
+                       dcdb_sync_corosync_conf(memdb, 1);
                }
        } else {
-               cfs_debug("using local mode (cluster.conf does not exist)");
+               cfs_debug("using local mode (corosync.conf does not exist)");
                cfs_set_quorate(1, TRUE);
        }
        if (conf_data) g_free(conf_data);
 
        cfs_plug_memdb_t *config = cfs_plug_memdb_new("memdb", memdb, dcdb);
-       
+
        cfs_plug_base_t *bplug = cfs_plug_base_new("", (cfs_plug_t *)config);
 
        create_symlinks(bplug, cfs.nodename);
 
        root_plug = (cfs_plug_t *)bplug;
 
-       system("umount -f " CFSDIR " >/dev/null 2>&1");
+       umount2(CFSDIR, MNT_FORCE);
+
+       mkdir(CFSDIR, 0755);
 
        char *fa[] = { "-f", "-odefault_permissions", "-oallow_other", NULL};
 
-       struct fuse_args fuse_args = FUSE_ARGS_INIT(sizeof (fa)/sizeof(gpointer) - 1, fa); 
-       
+       struct fuse_args fuse_args = FUSE_ARGS_INIT(sizeof (fa)/sizeof(gpointer) - 1, fa);
+
        struct fuse_chan *fuse_chan = fuse_mount(CFSDIR, &fuse_args);
        if (!fuse_chan) {
                cfs_critical("fuse_mount error: %s", strerror(errno));
@@ -828,6 +959,7 @@ int main(int argc, char *argv[])
                        goto err;
                } else if (cpid) {
                        write_pidfile(cpid);
+                       qb_log_fini();
                        _exit (0);
                } else {
                        int nullfd;
@@ -843,7 +975,8 @@ int main(int argc, char *argv[])
                        }
 
                        // do not print to the console after this point
-                       cfs.print_to_console = 0;
+                       qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE);
+
                        setsid();
                }
        } else {
@@ -915,16 +1048,15 @@ int main(int argc, char *argv[])
        if (service_status)
                service_dfsm_destroy(service_status);
 
-       sleep(1); /* do not restart too fast */
  ret:
 
-       if (status_fsm) 
+       if (status_fsm)
                dfsm_destroy(status_fsm);
 
        if (dcdb)
                dfsm_destroy(dcdb);
 
-       if (memdb) 
+       if (memdb)
                memdb_close(memdb);
 
        if (wrote_pidfile)
@@ -934,6 +1066,8 @@ int main(int argc, char *argv[])
 
        cfs_status_cleanup();
 
+       qb_log_fini();
+
        exit(ret);
 
  err: