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 | 51 +++++++-------------------
libvirt-sandbox/libvirt-sandbox-init-util.c | 57 +++++++++++++++++++++++++++++
libvirt-sandbox/libvirt-sandbox-init-util.h | 40 ++++++++++++++++++++
4 files changed, 114 insertions(+), 38 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..ffb6c87 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..a517c2f 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,42 +77,12 @@ 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",
@@ -132,7 +104,8 @@ 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",
@@ -160,7 +133,8 @@ 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..4bc02cc
--- /dev/null
+++ b/libvirt-sandbox/libvirt-sandbox-init-util.c
@@ -0,0 +1,57 @@
+/*
+ * 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(a)gmail.com>
+ */
+
+#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..bb3e27a
--- /dev/null
+++ b/libvirt-sandbox/libvirt-sandbox-init-util.h
@@ -0,0 +1,40 @@
+/*
+ * 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(a)gmail.com>
+ */
+
+#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