diff mbox series

[5/8] util: introduce mount/umount helper functions

Message ID 20180423123109.18590-5-christian.storm@siemens.com
State Accepted
Headers show
Series [1/8] core: warn about non-Linux parent SIGTERM tracking | expand

Commit Message

Storm, Christian April 23, 2018, 12:31 p.m. UTC
OSes other than Linux such as, e.g., FreeBSD, may use
a different API for (un-)mounting. Hence, factor out
the (un-)mounting into central helper functions that
do so OS-specific.

Signed-off-by: Thomas Zander <thomas.zander@siemens.com>
Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 core/util.c                | 55 ++++++++++++++++++++++++++++++++++++++
 corelib/lua_interface.c    |  5 ++--
 handlers/archive_handler.c |  5 ++--
 handlers/raw_handler.c     |  5 ++--
 include/util.h             |  3 +++
 5 files changed, 64 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index 29ab45f..d2a1c40 100644
--- a/core/util.c
+++ b/core/util.c
@@ -14,6 +14,9 @@ 
 #include <fcntl.h>
 #include <sys/file.h>
 #include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <sys/uio.h>
 #include <dirent.h>
 #include "swupdate.h"
 #include "util.h"
@@ -492,3 +495,55 @@  unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
 	}
 	return result;
 }
+
+int swupdate_mount(const char *device, const char *dir, const char *fstype)
+{
+#if defined(__linux__)
+	return mount(device, dir, fstype, 0, NULL);
+#elif defined(__FreeBSD__)
+	int iovlen = 8;
+	struct iovec iov[iovlen];
+	int mntflags = 0;
+	char errmsg[255];
+	memset(errmsg, 0, sizeof(errmsg));
+	iov[0].iov_base = (void*)"fstype";
+	iov[0].iov_len = strlen("fstype") + 1;
+	iov[1].iov_base = (void*)fstype;
+	iov[1].iov_len = strlen(fstype) + 1;
+	iov[2].iov_base = (void*)"fspath";
+	iov[2].iov_len = strlen("fspath") + 1;
+	iov[3].iov_base = (void*)dir;
+	iov[3].iov_len = strlen(dir) + 1;
+	iov[4].iov_base = (void*)"from";
+	iov[4].iov_len = strlen("from") + 1;
+	iov[5].iov_base = (void*)device;
+	iov[5].iov_len = strlen(device) + 1;
+	/* The underlying fs driver may require a
+	   buffer for an error message, even if we
+	   do not use it here. */
+	iov[6].iov_base = (void*)"errmsg";
+	iov[6].iov_len = strlen("errmsg") + 1;
+	iov[7].iov_base = errmsg;
+	iov[7].iov_len = strlen(errmsg) + 1;
+	return nmount(iov, iovlen, mntflags);
+#else
+	/* Not implemented for this OS, no specific errno. */
+	errno = 0;
+	return -1;
+#endif
+}
+
+int swupdate_umount(const char *dir)
+{
+#if defined(__linux__)
+	return umount(dir);
+#elif defined(__FreeBSD__)
+	int mntflags = 0;
+	return unmount(dir, mntflags);
+#else
+	/* Not implemented for this OS, no specific errno. */
+	errno = 0;
+	return -1;
+#endif
+}
+
diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 3212ea8..c4d29c6 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -10,7 +10,6 @@ 
 #include <unistd.h>
 #include <stdbool.h>
 #include <errno.h>
-#include <sys/mount.h>
 
 #include "lua.h"
 #include "lauxlib.h"
@@ -698,7 +697,7 @@  static int l_mount(lua_State *L) {
 		goto l_mount_free_exit;
 	}
 
-	if (mount(device, target, filesystem, 0, NULL) == -1) {
+	if (swupdate_mount(device, target, filesystem) == -1) {
 		TRACE("Device %s with filesystem %s cannot be mounted: %s",
 			device, filesystem, strerror(errno));
 		goto l_mount_rmdir_exit;
@@ -724,7 +723,7 @@  l_mount_exit:
 static int l_umount(lua_State *L) {
 	const char *target = luaL_checkstring(L, 1);
 
-	if (umount(target) == -1) {
+	if (swupdate_umount(target) == -1) {
 		TRACE("Unable to unmount %s: %s\n", target, strerror(errno));
 		goto l_umount_exit;
 	}
diff --git a/handlers/archive_handler.c b/handlers/archive_handler.c
index 687958f..4999f73 100644
--- a/handlers/archive_handler.c
+++ b/handlers/archive_handler.c
@@ -8,7 +8,6 @@ 
 #include <sys/types.h>
 #include <stdio.h>
 #include <sys/stat.h>
-#include <sys/mount.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdlib.h>
@@ -158,7 +157,7 @@  static int install_archive_image(struct img_type *img,
 	}
 
 	if (use_mount) {
-		ret = mount(img->device, DATADST_DIR, img->filesystem, 0, NULL);
+		ret = swupdate_mount(img->device, DATADST_DIR, img->filesystem);
 		if (ret) {
 			ERROR("Device %s with filesystem %s cannot be mounted",
 				img->device, img->filesystem);
@@ -241,7 +240,7 @@  static int install_archive_image(struct img_type *img,
 	}
 
 	if (use_mount) {
-		umount(DATADST_DIR);
+		swupdate_umount(DATADST_DIR);
 	}
 
 	return ret;
diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c
index c2c96b4..ac8e47a 100644
--- a/handlers/raw_handler.c
+++ b/handlers/raw_handler.c
@@ -7,7 +7,6 @@ 
  */
 
 #include <stdio.h>
-#include <sys/mount.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdlib.h>
@@ -55,7 +54,7 @@  static int install_raw_file(struct img_type *img,
 	}
 
 	if (use_mount) {
-		ret = mount(img->device, DATADST_DIR, img->filesystem, 0, NULL);
+		ret = swupdate_mount(img->device, DATADST_DIR, img->filesystem);
 		if (ret) {
 			ERROR("Device %s with filesystem %s cannot be mounted",
 				img->device, img->filesystem);
@@ -84,7 +83,7 @@  static int install_raw_file(struct img_type *img,
 	close(fdout);
 
 	if (use_mount) {
-		umount(DATADST_DIR);
+		swupdate_umount(DATADST_DIR);
 	}
 
 	return ret;
diff --git a/include/util.h b/include/util.h
index 2ac4139..6db0690 100644
--- a/include/util.h
+++ b/include/util.h
@@ -169,4 +169,7 @@  unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base);
 const char* get_tmpdir(void);
 const char* get_tmpdirscripts(void);
 
+int swupdate_mount(const char *device, const char *dir, const char *fstype);
+int swupdate_umount(const char *dir);
+
 #endif