[sandbox PATCH 08/10] Init-util : Common directory functions for init-common and init-qemu

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

 



Init-util provides common functions for creating directories for both
Init-common and Init-qemu. Error handling needs to be done in calling
function.
---
 libvirt-sandbox/Makefile.am                 |  4 +-
 libvirt-sandbox/libvirt-sandbox-init-qemu.c | 57 +++++++++-------------------
 libvirt-sandbox/libvirt-sandbox-init-util.c | 58 +++++++++++++++++++++++++++++
 libvirt-sandbox/libvirt-sandbox-init-util.h | 41 ++++++++++++++++++++
 4 files changed, 119 insertions(+), 41 deletions(-)
 create mode 100644 libvirt-sandbox/libvirt-sandbox-init-util.c
 create mode 100644 libvirt-sandbox/libvirt-sandbox-init-util.h

diff --git a/libvirt-sandbox/Makefile.am b/libvirt-sandbox/Makefile.am
index 8237c50..a7f1232 100644
--- a/libvirt-sandbox/Makefile.am
+++ b/libvirt-sandbox/Makefile.am
@@ -166,6 +166,7 @@ libvirt_sandbox_1_0_la_LDFLAGS = \
 			-version-info $(LIBVIRT_SANDBOX_VERSION_INFO)
 
 libvirt_sandbox_init_common_SOURCES = libvirt-sandbox-init-common.c \
+			libvirt-sandbox-init-util.c \ 
 			$(SANDBOX_GENERATED_RPC_FILES) \
 			$(SANDBOX_RPC_FILES) \
 			$(NULL)
@@ -216,7 +217,8 @@ libvirt_sandbox_init_lxc_LDADD = \
 			libvirt-sandbox-1.0.la \
 			$(NULL)
 
-libvirt_sandbox_init_qemu_SOURCES = libvirt-sandbox-init-qemu.c
+libvirt_sandbox_init_qemu_SOURCES = libvirt-sandbox-init-qemu.c \
+			libvirt-sandbox-init-util.c
 libvirt_sandbox_init_qemu_CFLAGS = \
 			-DLIBEXECDIR="\"$(libexecdir)\"" \
 			-DSANDBOXCONFIGDIR="\"$(sandboxconfigdir)\"" \
diff --git a/libvirt-sandbox/libvirt-sandbox-init-qemu.c b/libvirt-sandbox/libvirt-sandbox-init-qemu.c
index 33cebed..aa1a092 100644
--- a/libvirt-sandbox/libvirt-sandbox-init-qemu.c
+++ b/libvirt-sandbox/libvirt-sandbox-init-qemu.c
@@ -43,6 +43,8 @@
 #include <sys/reboot.h>
 #include <termios.h>
 
+#include "libvirt-sandbox-init-util.h"
+
 #define ATTR_UNUSED __attribute__((__unused__))
 
 static void print_uptime (void);
@@ -75,43 +77,13 @@ static void sig_child(int signum ATTR_UNUSED)
 }
 
 static void
-mount_mkdir(const char *dir, int mode);
-
-static void
-mount_mkparent(const char *dir, int mode)
-{
-    char *tmp = strrchr(dir, '/');
-    if (tmp && tmp != dir) {
-        char *parent = strndup(dir, tmp - dir);
-        mount_mkdir(parent, mode);
-        free(parent);
-    }
-}
-
-static void
-mount_mkdir(const char *dir, int mode)
-{
-    mount_mkparent(dir, 0755);
-
-    if (debug)
-        fprintf(stderr, "libvirt-sandbox-init-qemu: %s: %s (mode=%o)\n", __func__, dir, mode);
-
-    if (mkdir(dir, mode) < 0) {
-        if (errno != EEXIST) {
-            fprintf(stderr, "libvirt-sandbox-init-qemu: %s: cannot make directory %s: %s\n",
-                    __func__, dir, strerror(errno));
-            exit_poweroff();
-        }
-    }
-}
-
-static void
 mount_mkfile(const char *file, int mode)
 {
     int fd;
 
-    mount_mkparent(file, 0755);
-
+    if (gvir_sandbox_init_util_mkparent(file, 0755, debug) < 0)
+	exit_poweroff();
+   
     if (debug)
         fprintf(stderr, "libvirt-sandbox-init-qemu: %s: %s (mode=%o)\n",
                 __func__, file, mode);
@@ -132,8 +104,9 @@ mount_other_opts(const char *dst, const char *type, const char *opts, int mode)
     if (debug)
         fprintf(stderr, "libvirt-sandbox-init-qemu: %s: %s (type=%s opts=%s)\n", __func__, dst, type, opts);
 
-    mount_mkdir(dst, mode);
-
+    if (gvir_sandbox_init_util_mkdir(dst, mode,debug) < 0)
+	exit_poweroff();
+    
     if (mount("none", dst, type, 0, opts) < 0) {
         fprintf(stderr, "libvirt-sandbox-init-qemu: %s: cannot mount %s on %s (%s:%s): %s\n",
                 __func__, type, dst, type, opts, strerror(errno));
@@ -160,8 +133,9 @@ mount_9pfs(const char *src, const char *dst, int mode, int readonly)
     if (debug)
         fprintf(stderr, "libvirt-sandbox-init-qemu: %s: %s -> %s (%d)\n", __func__, src, dst, readonly);
 
-    mount_mkdir(dst, mode);
-
+    if (gvir_sandbox_init_util_mkdir(dst, mode, debug) < 0)
+        exit_poweroff();
+    
     if (readonly)
         flags |= MS_RDONLY;
 
@@ -286,15 +260,18 @@ main(int argc ATTR_UNUSED, char **argv ATTR_UNUSED)
                         __func__, source, strerror(errno));
                 exit_poweroff();
             }
-            if (S_ISDIR(st.st_mode))
-                mount_mkdir(target, 755);
+            if (S_ISDIR(st.st_mode)){
+                if (gvir_sandbox_init_util_mkdir(target, 755, debug) < 0)
+                   exit_poweroff();
+	    }
             else
                 mount_mkfile(target, 644);
         } else {
             if (strcmp(type, "tmpfs") == 0)
                 flags |= MS_NOSUID | MS_NODEV;
 
-            mount_mkdir(target, 0755);
+            if (gvir_sandbox_init_util_mkdir(target, 0755, debug) < 0)
+                exit_poweroff();
         }
 
         if (mount(source, target, type, flags, opts) < 0) {
diff --git a/libvirt-sandbox/libvirt-sandbox-init-util.c b/libvirt-sandbox/libvirt-sandbox-init-util.c
new file mode 100644
index 0000000..2660652
--- /dev/null
+++ b/libvirt-sandbox/libvirt-sandbox-init-util.c
@@ -0,0 +1,58 @@
+/*
+ * libvirt-sandbox-init-util.c: libvirt sandbox init util functions
+ *
+ * Copyright (C) 2015 Universitat Politècnica de Catalunya.
+ *
+ * This library 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; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Eren Yagdiran <erenyagdiran@xxxxxxxxx>
+ */
+
+#include <config.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "libvirt-sandbox-init-util.h"
+
+int gvir_sandbox_init_util_mkdir(const char *dir, int mode, int debug)
+{
+    gvir_sandbox_init_util_mkparent(dir, 0755,debug);
+    
+    if (debug > 0)
+        fprintf(stderr, "libvirt-sandbox-init-util: %s: %s (mode=%o)\n", __func__, dir, mode);
+    
+    if (mkdir(dir, mode) < 0) {
+        if (errno != EEXIST) {
+            fprintf(stderr, "libvirt-sandbox-init-util: %s: cannot make directory %s: %s\n",__func__, dir, strerror(errno));
+            return -1;
+        }
+    }
+    return 0;
+}
+
+int gvir_sandbox_init_util_mkparent(const char *dir, int mode,int debug)
+{
+    char *tmp = strrchr(dir, '/');
+    if (tmp && tmp != dir) {
+        char *parent = strndup(dir, tmp - dir);
+        gvir_sandbox_init_util_mkdir(parent, mode,debug);
+        free(parent);
+    }
+    return 0;
+}
+
diff --git a/libvirt-sandbox/libvirt-sandbox-init-util.h b/libvirt-sandbox/libvirt-sandbox-init-util.h
new file mode 100644
index 0000000..01cccf1
--- /dev/null
+++ b/libvirt-sandbox/libvirt-sandbox-init-util.h
@@ -0,0 +1,41 @@
+/*
+ * libvirt-sandbox-init-util.h: libvirt sandbox init util header
+ *
+ * Copyright (C) 2015 Universitat Politècnica de Catalunya.
+ *
+ * This library 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; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Eren Yagdiran <erenyagdiran@xxxxxxxxx>
+ */
+
+#ifndef __LIBVIRT_SANDBOX_INIT_UTIL_H__
+#define __LIBVIRT_SANDBOX_INIT_UTIL_H__
+
+int gvir_sandbox_init_util_mkdir(const char *dir, int mode, int debug);
+
+int gvir_sandbox_init_util_mkparent(const char *dir, int mode, int debug);
+
+
+#endif /* __LIBVIRT_SANDBOX_INIT_UTIL_H__ */
+
+/*
+ * Local variables:
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  indent-tabs-mode: nil
+ *  tab-width: 8
+ * End:
+ */
+
-- 
2.1.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]