fixed scenario loading
[strongswan.git] / src / dumm / guest.c
index cdb97c7..bbb59f4 100644 (file)
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/uio.h>
 #include <unistd.h>
 #include <stdio.h>
-#include <sys/uio.h>
-#include <sys/types.h>
 #include <fcntl.h>
 #include <signal.h>
+#include <dirent.h>
+#include <termios.h>
 
 #include <debug.h>
 #include <utils/linked_list.h>
 #include "dumm.h"
 #include "guest.h"
 #include "mconsole.h"
+#include "cowfs.h"
+
+#define PERME (S_IRWXU | S_IRWXG)
+#define PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
+
+#define MASTER_DIR "master"
+#define DIFF_DIR "diff"
+#define UNION_DIR "union"
+#define MEMORY_FILE "mem"
+#define KERNEL_FILE "linux"
+#define LOG_FILE "boot.log"
+#define NOTIFY_FILE "notify"
+#define PTYS 0
 
 typedef struct private_guest_t private_guest_t;
 
@@ -38,22 +53,34 @@ struct private_guest_t {
        guest_t public;
        /** name of the guest */
        char *name;
-       /** kernel to boot for guest */
-       char *kernel;
-       /** read only master filesystem guest uses */
-       char *master;
+       /** directory of guest */
+       int dir;
+       /** directory name of guest */
+       char *dirname;
        /** amount of memory for guest, in MB */
        int mem;
        /** pid of guest child process */
        int pid;
+       /** state of guest */
+       guest_state_t state;
        /** log file for console 0 */
        int bootlog;
+       /** FUSE cowfs instance */
+       cowfs_t *cowfs;
        /** mconsole to control running UML */
        mconsole_t *mconsole;
        /** list of interfaces attached to the guest */
        linked_list_t *ifaces;
 };
 
+ENUM(guest_state_names, GUEST_STOPPED, GUEST_STOPPING,
+       "STOPPED",
+       "STARTING",
+       "RUNNING",
+       "PAUSED",
+       "STOPPING",
+);
+
 /**
  * Implementation of guest_t.get_name.
  */
@@ -70,7 +97,7 @@ static iface_t* create_iface(private_guest_t *this, char *name)
        iterator_t *iterator;
        iface_t *iface;
        
-       if (this->pid == 0)
+       if (this->state != GUEST_RUNNING)
        {
                DBG1("guest '%s' not running, unable to add interface", this->name);
                return NULL;
@@ -79,7 +106,7 @@ static iface_t* create_iface(private_guest_t *this, char *name)
        iterator = this->ifaces->create_iterator(this->ifaces, TRUE);
        while (iterator->iterate(iterator, (void**)&iface))
        {
-               if (streq(name, iface->get_guest(iface)))
+               if (streq(name, iface->get_guestif(iface)))
                {
                        DBG1("guest '%s' already has an interface '%s'", this->name, name);
                        iterator->destroy(iterator);
@@ -88,7 +115,7 @@ static iface_t* create_iface(private_guest_t *this, char *name)
        }
        iterator->destroy(iterator);
 
-       iface = iface_create(name, this->mconsole);
+       iface = iface_create(this->name, name, this->mconsole);
        if (iface)
        {
                this->ifaces->insert_last(this->ifaces, iface);
@@ -103,6 +130,22 @@ static iterator_t* create_iface_iterator(private_guest_t *this)
 {
        return this->ifaces->create_iterator(this->ifaces, TRUE);
 }
+       
+/**
+ * Implementation of guest_t.get_state.
+ */
+static guest_state_t get_state(private_guest_t *this)
+{
+       return this->state;
+}
+
+/**
+ * Implementation of guest_t.get_pid.
+ */
+static pid_t get_pid(private_guest_t *this)
+{
+       return this->pid;
+}
 
 /**
  * write format string to a buffer, and advance buffer position
@@ -127,32 +170,64 @@ static char* write_arg(char **pos, size_t *left, char *format, ...)
 }
 
 /**
+ * Implementation of get_t.close_console.
+ */
+static char* get_console(private_guest_t *this, int console)
+{
+       if (this->state == GUEST_RUNNING)
+       {
+               return this->mconsole->get_console_pts(this->mconsole, console);
+       }
+       return NULL;
+}
+
+/**
+ * Implementation of guest_t.stop.
+ */
+static void stop(private_guest_t *this)
+{
+       if (this->state != GUEST_STOPPED)
+       {
+               this->state = GUEST_STOPPING;
+               this->ifaces->destroy_offset(this->ifaces, offsetof(iface_t, destroy));
+               this->ifaces = linked_list_create();
+               kill(this->pid, SIGINT);
+               waitpid(this->pid, NULL, 0);
+               this->state = GUEST_STOPPED;
+       }
+}
+
+/**
  * Implementation of guest_t.start.
  */
 static bool start(private_guest_t *this)
 {
-       char buf[1024];
-       char cwd[512];
+       char buf[2048];
+       char *notify;
        char *pos = buf;
        char *args[16];
        int i = 0;
        size_t left = sizeof(buf);
-
-       args[i++] = this->kernel;
+       
+       if (this->state != GUEST_STOPPED)
+       {
+               DBG1("unable to start guest in state %N", guest_state_names, this->state);
+               return FALSE;
+       }
+       this->state = GUEST_STARTING;
+       
+       notify = write_arg(&pos, &left, "%s/%s", this->dirname, NOTIFY_FILE);
+       
+       args[i++] = write_arg(&pos, &left, "%s/%s", this->dirname, KERNEL_FILE);
        args[i++] = write_arg(&pos, &left, "root=/dev/root");
        args[i++] = write_arg(&pos, &left, "rootfstype=hostfs");
-       args[i++] = write_arg(&pos, &left, "rootflags=%s/%s/%s",
-                                                 getcwd(cwd, sizeof(cwd)), MOUNT_DIR, this->name);
-       args[i++] = write_arg(&pos, &left, "uml_dir=%s/%s", RUN_DIR, this->name);
+       args[i++] = write_arg(&pos, &left, "rootflags=%s/%s", this->dirname, UNION_DIR);
+       args[i++] = write_arg(&pos, &left, "uml_dir=%s", this->dirname);
        args[i++] = write_arg(&pos, &left, "umid=%s", this->name);
        args[i++] = write_arg(&pos, &left, "mem=%dM", this->mem);
-       /*args[i++] = write_arg(&pos, &left, "con=pts");*/
-       args[i++] = write_arg(&pos, &left, "con0=null,fd:%d", this->bootlog);
-       args[i++] = write_arg(&pos, &left, "con1=fd:0,fd:1");
-       args[i++] = write_arg(&pos, &left, "con3=null,null");
-       args[i++] = write_arg(&pos, &left, "con4=null,null");
-       args[i++] = write_arg(&pos, &left, "con5=null,null");
-       args[i++] = write_arg(&pos, &left, "con6=null,null");
+       args[i++] = write_arg(&pos, &left, "mconsole=notify:%s", notify);
+       args[i++] = write_arg(&pos, &left, "con=pts");
+       args[i++] = write_arg(&pos, &left, "con0=none,fd:%d", this->bootlog);
        args[i++] = NULL;
          
        this->pid = fork();
@@ -160,174 +235,333 @@ static bool start(private_guest_t *this)
        {
                case 0: /* child,  */
                        dup2(open("/dev/null", 0), 0);
-                       dup2(open("/dev/null", 0), 1);
-                       dup2(open("/dev/null", 0), 2);
+                       dup2(this->bootlog, 1);
+                       dup2(this->bootlog, 2);
                        execvp(args[0], args);
-                       DBG1("starting UML kernel '%s' failed", args[0]);
+                       DBG1("starting UML kernel '%s' failed: %m", args[0]);
                        exit(1);
                case -1:
-                       this->pid = 0;
+                       this->state = GUEST_STOPPED;
                        return FALSE;
                default:
                        break;
        }
        /* open mconsole */
-       snprintf(buf, sizeof(buf), "%s/%s/%s/mconsole", RUN_DIR, this->name, this->name);
-       this->mconsole = mconsole_create(buf);
+       this->mconsole = mconsole_create(notify);
        if (this->mconsole == NULL)
        {
                DBG1("opening mconsole at '%s' failed, stopping guest", buf);
-               kill(this->pid, SIGINT);
-               this->pid = 0;
+               stop(this);
                return FALSE;
        }
+       
+       this->state = GUEST_RUNNING;
        return TRUE;
-}
-
+}      
+       
 /**
- * Implementation of guest_t.stop.
+ * Implementation of guest_t.load_template.
  */
-static void stop(private_guest_t *this)
+static bool load_template(private_guest_t *this, char *path)
 {
-       if (this->pid)
+       char dir[PATH_MAX];
+       size_t len;
+       
+       if (path == NULL)
        {
-               kill(this->pid, SIGINT);
-               this->pid = 0;
+               return this->cowfs->set_overlay(this->cowfs, NULL);     
        }
-}
-
-/**
- * Check if directory exists, create otherwise
- */
-static bool makedir(char *dir, char *name)
-{
-       struct stat st;
-       char buf[256];
-       size_t len;
        
-       len = snprintf(buf, sizeof(buf), "%s/%s", dir, name);
-       if (len < 0 || len >= sizeof(buf))
+       len = snprintf(dir, sizeof(dir), "%s/%s", path, this->name);
+       if (len < 0 || len >= sizeof(dir))
        {
                return FALSE;
        }
-       if (stat(buf, &st) != 0)
+       if (access(dir, F_OK) != 0)
        {
-               return mkdir(buf, S_IRWXU) == 0;
+               if (mkdir(dir, PERME) != 0)
+               {
+                       DBG1("creating overlay for guest '%s' failed: %m", this->name);
+                       return FALSE;
+               }
        }
-       return S_ISDIR(st.st_mode);
+       return this->cowfs->set_overlay(this->cowfs, dir);
 }
 
 /**
- * umount the union filesystem
+ * Implementation of guest_t.sigchild.
  */
-static bool umount_unionfs(char *name)
+static void sigchild(private_guest_t *this)
 {
-       char cmd[128];
-       size_t len;
-       
-       len = snprintf(cmd, sizeof(cmd), "fusermount -u %s/%s", MOUNT_DIR, name);
-       if (len < 0 || len >= sizeof(cmd))
-       {
-               return FALSE;
+       if (this->state != GUEST_STOPPING)
+       {       /* collect zombie if uml crashed */
+               waitpid(this->pid, NULL, WNOHANG);
        }
-       if (system(cmd) != 0)
+       DESTROY_IF(this->mconsole);
+       this->mconsole = NULL;
+       this->state = GUEST_STOPPED;
+}
+
+/**
+ * umount the union filesystem
+ */
+static bool umount_unionfs(private_guest_t *this)
+{
+       if (this->cowfs)
        {
-               DBG1("unmounting guest unionfs for %s failed", name);
-               return FALSE;
+               this->cowfs->destroy(this->cowfs);
+               this->cowfs = NULL;
+               return TRUE;
        }
-       return TRUE;
+       return FALSE;
 }
 
 /**
  * mount the union filesystem
  */
-static bool mount_unionfs(char *name, char *master)
+static bool mount_unionfs(private_guest_t *this)
 {
-       char cmd[256];
-       size_t len;
-       
-       len = snprintf(cmd, sizeof(cmd), "unionfs %s/%s:%s %s/%s",
-                                  HOST_DIR, name, master, MOUNT_DIR, name);
-       if (len < 0 || len >= sizeof(cmd))
-       {
-               return FALSE;
-       }
-       if (system(cmd) != 0)
+       char master[PATH_MAX];
+       char diff[PATH_MAX];
+       char mount[PATH_MAX];
+
+       if (this->cowfs == NULL)
        {
-               DBG1("mounting guest unionfs for %s using '%s' failed", name, cmd);
-               return FALSE;
+               snprintf(master, sizeof(master), "%s/%s", this->dirname, MASTER_DIR);
+               snprintf(diff, sizeof(diff), "%s/%s", this->dirname, DIFF_DIR);
+               snprintf(mount, sizeof(mount), "%s/%s", this->dirname, UNION_DIR);
+               
+               this->cowfs = cowfs_create(master, diff, mount);
+               if (this->cowfs)
+               {
+                       return TRUE;
+               }
        }
-       return TRUE;
+       return FALSE;
 }
 
 /**
  * open logfile for boot messages
  */
-static int open_bootlog(char *name)
+static int open_bootlog(private_guest_t *this)
 {
-       char blg[256];
-       size_t len;
        int fd;
        
-       len = snprintf(blg, sizeof(blg), "%s/%s/boot.log", RUN_DIR, name);
-       if (len < 0 || len >= sizeof(blg))
-       {
-               return 1;
-       }
-       fd = open(blg, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
+       fd = openat(this->dir, LOG_FILE, O_WRONLY | O_CREAT, PERM);
        if (fd == -1)
        {
-               DBG1("opening bootlog '%s' for %s failed, using stdout", blg, name);
+               DBG1("opening bootlog failed, using stdout");
                return 1;
        }
        return fd;
 }
 
 /**
+ * load memory configuration from file
+ */
+int loadmem(private_guest_t *this)
+{
+       FILE *file;
+       int mem = 0;
+       
+       file = fdopen(openat(this->dir, MEMORY_FILE, O_RDONLY, PERM), "r");
+       if (file)
+       {
+               if (fscanf(file, "%d", &mem) <= 0)
+               {
+                       mem = 0;
+               }
+               fclose(file);
+       }
+       return mem;
+}
+
+/**
+ * save memory configuration to file
+ */
+bool savemem(private_guest_t *this, int mem)
+{
+       FILE *file;
+       bool retval = FALSE;
+       
+       file = fdopen(openat(this->dir, MEMORY_FILE, O_RDWR | O_CREAT | O_TRUNC,
+                                                PERM), "w");
+       if (file)
+       {
+               if (fprintf(file, "%d", mem) > 0)
+               {
+                       retval = TRUE;
+               }
+               fclose(file);
+       }
+       return retval;
+}
+
+/**
  * Implementation of guest_t.destroy.
  */
 static void destroy(private_guest_t *this)
 {
        stop(this);
-       umount_unionfs(this->name);
-       this->ifaces->destroy_offset(this->ifaces, offsetof(iface_t, destroy));
-       DESTROY_IF(this->mconsole);
+       umount_unionfs(this);
+       if (this->bootlog > 1)
+       {
+               close(this->bootlog);
+       }
+       if (this->dir > 0)
+       {
+               close(this->dir);
+       }
+       free(this->dirname);
        free(this->name);
-       free(this->kernel);
-       free(this->master);
        free(this);
 }
 
 /**
- * create the guest instance, including required dirs and mounts 
+ * generic guest constructor
  */
-guest_t *guest_create(char *name, char *kernel, char *master, int mem)
+static private_guest_t *guest_create_generic(char *parent, char *name,
+                                                                                        bool create)
 {
+       char cwd[PATH_MAX];
        private_guest_t *this = malloc_thing(private_guest_t);
        
        this->public.get_name = (void*)get_name;
+       this->public.get_pid = (pid_t(*)(guest_t*))get_pid;
+       this->public.get_state = (guest_state_t(*)(guest_t*))get_state;
        this->public.create_iface = (iface_t*(*)(guest_t*,char*))create_iface;
        this->public.create_iface_iterator = (iterator_t*(*)(guest_t*))create_iface_iterator;
        this->public.start = (void*)start;
        this->public.stop = (void*)stop;
+       this->public.get_console = (char*(*)(guest_t*,int))get_console;
+       this->public.load_template = (bool(*)(guest_t*, char *path))load_template;
+       this->public.sigchild = (void(*)(guest_t*))sigchild;
        this->public.destroy = (void*)destroy;
-       
-       if (!makedir(HOST_DIR, name) || !makedir(MOUNT_DIR, name) ||
-               !makedir(RUN_DIR, name) || !mount_unionfs(name, master))
+               
+       if (*parent == '/' || getcwd(cwd, sizeof(cwd)) == NULL)
+       {
+               asprintf(&this->dirname, "%s/%s", parent, name);
+       }
+       else
+       {
+               asprintf(&this->dirname, "%s/%s/%s", cwd, parent, name);
+       }
+       if (create)
+       {
+               mkdir(this->dirname, PERME);
+       }
+       this->dir = open(this->dirname, O_DIRECTORY, PERME);
+       if (this->dir < 0)
        {
+               DBG1("opening guest directory '%s' failed: %m", this->dirname);
+               free(this->dirname);
                free(this);
                return NULL;
        }
        
-       this->name = strdup(name);
-       this->kernel = strdup(kernel);
-       this->master = strdup(master);
-       this->mem = mem;
        this->pid = 0;
-       this->bootlog = open_bootlog(name);
+       this->state = GUEST_STOPPED;
        this->mconsole = NULL;
        this->ifaces = linked_list_create();
+       this->mem = 0;
+       this->bootlog = open_bootlog(this);
+       this->name = strdup(name);
+       this->cowfs = NULL;
+       
+       return this;
+}
+
+/**
+ * create a symlink to old called new in our working dir
+ */
+static bool make_symlink(private_guest_t *this, char *old, char *new)
+{
+       char cwd[PATH_MAX];
+       char buf[PATH_MAX];
+       
+       if (*old == '/' || getcwd(cwd, sizeof(cwd)) == NULL)
+       {
+               snprintf(buf, sizeof(buf), "%s", old);
+       }
+       else
+       {
+               snprintf(buf, sizeof(buf), "%s/%s", cwd, old);
+       }
+       return symlinkat(buf, this->dir, new) == 0;
+}
+
 
+/**
+ * create the guest instance, including required dirs and mounts 
+ */
+guest_t *guest_create(char *parent, char *name, char *kernel,
+                                         char *master, int mem)
+{
+       private_guest_t *this = guest_create_generic(parent, name, TRUE);
+       
+       if (this == NULL)
+       {
+               return NULL;
+       }
+       
+       if (!make_symlink(this, master, MASTER_DIR) ||
+               !make_symlink(this, kernel, KERNEL_FILE))
+       {
+               DBG1("creating master/kernel symlink failed: %m");
+               destroy(this);
+               return NULL;
+       }
+       
+       if (mkdirat(this->dir, UNION_DIR, PERME) != 0 || 
+               mkdirat(this->dir, DIFF_DIR, PERME) != 0)
+       {
+               DBG1("unable to create directories for '%s': %m", name);
+               destroy(this);
+               return NULL;
+       }
+       
+       this->mem = mem;
+       if (!savemem(this, mem))
+       {
+               destroy(this);
+               return NULL;
+       }
+       
+       if (!mount_unionfs(this))
+       {
+               destroy(this);
+               return NULL;
+       }
+
+       return &this->public;
+}
+
+/**
+ * load an already created guest
+ */
+guest_t *guest_load(char *parent, char *name)
+{
+       private_guest_t *this = guest_create_generic(parent, name, FALSE);
+       
+       if (this == NULL)
+       {
+               return NULL;
+       }
+       
+       this->mem = loadmem(this);
+       if (this->mem == 0)
+       {
+               DBG1("unable to open memory configuration file: %m", name);
+               destroy(this);
+               return NULL;
+       }
+       
+       if (!mount_unionfs(this))
+       {
+               destroy(this);
+               return NULL;
+       }
+       
        return &this->public;
 }