[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 26 of 29 RFC] xldeviced: new daemon to execute hotplug scripts



# HG changeset patch
# User Roger Pau Monne <roger.pau@xxxxxxxxxxxxx>
# Date 1328179909 -3600
# Node ID e904f98a4445fee7489490b5ccc00b7bb62d796d
# Parent  94532199f647fc03816fc5ae50ab53c8c5b80cd8
xldeviced: new daemon to execute hotplug scripts

This patch introduces xldeviced, a new daemon that monitors
/hotplug/<domid> and is in charge of attaching the passed devices to
the desired DomU. Makes use of the previously introduced libxl hotplug
functions.

Signed-off-by: Roger Pau Monne <roger.pau@xxxxxxxxxxxxx>

diff -r 94532199f647 -r e904f98a4445 tools/libxl/Makefile
--- a/tools/libxl/Makefile      Thu Feb 02 11:48:06 2012 +0100
+++ b/tools/libxl/Makefile      Thu Feb 02 11:51:49 2012 +0100
@@ -13,7 +13,7 @@ XLUMINOR = 0
 
 CFLAGS += -Werror -Wno-format-zero-length -Wmissing-declarations \
        -Wno-declaration-after-statement -Wformat-nonliteral
-CFLAGS += -I. -fPIC
+CFLAGS += -I. -fPIC -g
 
 ifeq ($(CONFIG_Linux),y)
 LIBUUID_LIBS += -luuid
@@ -60,12 +60,16 @@ LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_
        libxlu_disk_l.o libxlu_disk.o
 $(LIBXLU_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 
-CLIENTS = xl testidl
+CLIENTS = xl xldeviced testidl
 
 XL_OBJS = xl.o xl_cmdimpl.o xl_cmdtable.o
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxenlight)
 
+XLDEVICED_OBJS = xldeviced.o
+$(XLDEVICED_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
+$(XLDEVICED_OBJS): CFLAGS += $(CFLAGS_libxenlight)
+
 testidl.o: CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenlight)
 testidl.c: libxl_types.idl gentest.py libxl.h $(AUTOINCS)
        $(PYTHON) gentest.py libxl_types.idl testidl.c.new
@@ -140,8 +144,12 @@ libxlutil.a: $(LIBXLU_OBJS)
 xl: $(XL_OBJS) libxlutil.so libxenlight.so
        $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) libxlutil.so $(LDLIBS_libxenlight) 
$(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
 
+xldeviced: $(XLDEVICED_OBJS) libxlutil.so libxenlight.so
+       $(CC) $(LDFLAGS) -o $@ $(XLDEVICED_OBJS) libxlutil.so 
$(LDLIBS_libxenlight) $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
+
 testidl: testidl.o libxlutil.so libxenlight.so
        $(CC) $(LDFLAGS) -o $@ testidl.o libxlutil.so $(LDLIBS_libxenlight) 
$(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
+    
 
 .PHONY: install
 install: all
@@ -151,6 +159,7 @@ install: all
        $(INSTALL_DIR) $(DESTDIR)$(BASH_COMPLETION_DIR)
        $(INSTALL_DIR) $(DESTDIR)$(XEN_RUN_DIR)
        $(INSTALL_PROG) xl $(DESTDIR)$(SBINDIR)
+       $(INSTALL_PROG) xldeviced $(DESTDIR)$(SBINDIR)
        $(INSTALL_PROG) libxenlight.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)
        ln -sf libxenlight.so.$(MAJOR).$(MINOR) 
$(DESTDIR)$(LIBDIR)/libxenlight.so.$(MAJOR)
        ln -sf libxenlight.so.$(MAJOR) $(DESTDIR)$(LIBDIR)/libxenlight.so
diff -r 94532199f647 -r e904f98a4445 tools/libxl/xldeviced.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/libxl/xldeviced.c   Thu Feb 02 11:51:49 2012 +0100
@@ -0,0 +1,187 @@
+/*
+ * Copyright (C) 2012
+ * Author Roger Pau Monnà <roger.pau@xxxxxxxxxxxxx>
+ *
+ * This program 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; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program 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.
+ */
+
+#include "libxl_osdeps.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <assert.h>
+
+#include <xenctrl.h>
+#include <xs.h>
+
+#include "libxl.h"
+#include "libxl_utils.h"
+#include "libxlutil.h"
+#include "xldeviced.h"
+
+xentoollog_logger_stdiostream *logger;
+libxl_ctx *ctx;
+
+static xentoollog_level minmsglevel = XTL_PROGRESS;
+struct xs_handle *xs;
+
+libxl_device_disk_hotplug_actions disk_handler = {
+    .connect = libxl_device_disk_add,
+    .disconnect = libxl_device_disk_remove,
+    .force_disconnect = libxl_device_disk_destroy,
+};
+
+libxl_device_nic_hotplug_actions nic_handler = {
+    .connect = libxl_device_nic_add,
+    .disconnect = libxl_device_nic_remove,
+    .force_disconnect = libxl_device_nic_destroy,
+};
+
+static int create_pidfile(char *file)
+{
+    pid_t pid;
+    int rc, fd;
+    char *s;
+
+    fd = open(file, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
+    if (fd < 0) {
+        LOG("unable to open pidfile %s", file);
+        return -1;
+    }
+    pid = getpid();
+    rc = asprintf(&s, "%d\n", pid);
+    if (rc < 0)
+        return -1;
+    libxl_write_exactly(NULL, fd, s, rc, NULL, NULL);
+
+    close(fd);
+    return 0;
+}
+
+int main(int argc, char **argv)
+{
+    int opt = 0;
+    int daemonize = 1;
+    int status, rc = 0, ret = 0;
+    char *logfilename = "xldeviced";
+    char *pidfile = PIDFILE;
+    int logfile;
+
+    while ((opt = getopt(argc, argv, "+vfp:")) >= 0) {
+        switch (opt) {
+        case 'v':
+            if (minmsglevel > 0) minmsglevel--;
+            break;
+        case 'f':
+            daemonize = 0;
+            break;
+        case 'p':
+            pidfile = optarg;
+            break;
+        default:
+            fprintf(stderr, "unknown global option\n");
+            exit(2);
+        }
+    }
+
+    logger = xtl_createlogger_stdiostream(stderr, minmsglevel,  0);
+    if (!logger) exit(1);
+
+    if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0, (xentoollog_logger*)logger)) {
+        fprintf(stderr, "cannot init xldeviced context\n");
+        exit(1);
+    }
+
+    if (daemonize) {
+        char *fullname;
+        pid_t child1, got_child;
+        int nullfd;
+
+        child1 = libxl_fork(ctx);
+        if (child1) {
+
+            for (;;) {
+                got_child = waitpid(child1, &status, 0);
+                if (got_child == child1) break;
+                assert(got_child == -1);
+                if (errno != EINTR) {
+                    perror("failed to wait for daemonizing child");
+                    ret = ERROR_FAIL;
+                    goto out;
+                }
+            }
+            if (status) {
+                libxl_report_child_exitstatus(ctx, XTL_ERROR,
+                           "daemonizing child", child1, status);
+                ret = ERROR_FAIL;
+                goto out;
+            }
+            ret = 0;
+            goto out;
+        }
+
+        rc = libxl_ctx_postfork(ctx);
+        if (rc) {
+            LOG("failed to reinitialise context after fork");
+            exit(-1);
+        }
+
+        rc = libxl_create_logfile(ctx, logfilename, &fullname);
+        if (rc) {
+            LOG("failed to open logfile %s: %s",fullname,strerror(errno));
+            exit(-1);
+        }
+
+        CHK_ERRNO(( logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND,
+                                   0644) )<0);
+        free(fullname);
+
+        CHK_ERRNO(( nullfd = open("/dev/null", O_RDONLY) )<0);
+        dup2(nullfd, 0);
+        dup2(logfile, 1);
+        dup2(logfile, 2);
+
+        CHK_ERRNO(daemon(0, 1) < 0);
+    }
+    /* Write pid */
+    if (create_pidfile(pidfile) < 0) {
+        LOG("unable to write pid to %s", pidfile);
+        goto out_clean;
+    }
+
+    /* Watch /hotplug/<my_dom_id> for changes */
+    ret = libxl_setup_hotplug_listener(ctx);
+    if (ret < 0) {
+        LOG("Failed to setup xenstore watches");
+        goto out_clean;
+    }
+
+    ret = libxl_hotplug_dispatch(ctx, &disk_handler, &nic_handler);
+
+out_clean:
+    unlink(pidfile);
+out:
+    libxl_ctx_free(ctx);
+    xtl_logger_destroy((xentoollog_logger*)logger);
+    return ret;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff -r 94532199f647 -r e904f98a4445 tools/libxl/xldeviced.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/libxl/xldeviced.h   Thu Feb 02 11:51:49 2012 +0100
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2012
+ * Author Roger Pau Monnà <roger.pau@xxxxxxxxxxxxx>
+ *
+ * This program 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; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program 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.
+ */
+
+#ifndef XLDEVICED_H
+#define XLDEVICED_H
+
+#include "xentoollog.h"
+
+extern libxl_ctx *ctx;
+extern xentoollog_logger_stdiostream *logger;
+int logfile = 2;
+
+#define PIDFILE "/var/run/xldeviced.pid"
+
+#define LOG(_f, _a...)   dolog(__FILE__, __LINE__, __func__, _f "\n", ##_a)
+
+static void dolog(const char *file, int line, const char *func, char *fmt, ...)
+     __attribute__((format(printf,4,5)));
+
+static void dolog(const char *file, int line, const char *func, char *fmt, ...)
+{
+    va_list ap;
+    char *s;
+    int rc;
+
+    va_start(ap, fmt);
+    rc = vasprintf(&s, fmt, ap);
+    va_end(ap);
+    if (rc >= 0)
+        libxl_write_exactly(NULL, logfile, s, rc, NULL, NULL);
+}
+
+#define CHK_ERRNO( call ) ({                                            \
+        int chk_errno = (call);                                         \
+        if (chk_errno < 0) {                                            \
+            fprintf(stderr,"xl: fatal error: %s:%d: %s: %s\n",          \
+                    __FILE__,__LINE__, strerror(chk_errno), #call);     \
+            exit(-ERROR_FAIL);                                          \
+        }                                                               \
+    })
+
+#endif /* XLDEVICED_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.