aboutgitcodebugslistschat
path: root/util.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-11-03 13:22:58 +1100
committerStefano Brivio <sbrivio@redhat.com>2023-11-07 09:53:18 +0100
commit4f0b9f91e40ac004f955e73860ee1a0eee9cab95 (patch)
treef32e85aee9fb11100a1e2800319b76252d0cd079 /util.c
parent17d40d1cb50e53b25eee522a165bba5f04a93a31 (diff)
downloadpasst-4f0b9f91e40ac004f955e73860ee1a0eee9cab95.tar
passt-4f0b9f91e40ac004f955e73860ee1a0eee9cab95.tar.gz
passt-4f0b9f91e40ac004f955e73860ee1a0eee9cab95.tar.bz2
passt-4f0b9f91e40ac004f955e73860ee1a0eee9cab95.tar.lz
passt-4f0b9f91e40ac004f955e73860ee1a0eee9cab95.tar.xz
passt-4f0b9f91e40ac004f955e73860ee1a0eee9cab95.tar.zst
passt-4f0b9f91e40ac004f955e73860ee1a0eee9cab95.zip
util: Add open_in_ns() helper
Most of our helpers which need to enter the pasta network namespace are quite specialised. Add one which is rather general - it just open()s a given file in the namespace context and returns the fd back to the main namespace. This will have some future uses. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'util.c')
-rw-r--r--util.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/util.c b/util.c
index a8f3b35..c38ab7e 100644
--- a/util.c
+++ b/util.c
@@ -365,6 +365,59 @@ bool ns_is_init(void)
}
/**
+ * struct open_in_ns_args - Parameters for do_open_in_ns()
+ * @c: Execution context
+ * @fd: Filled in with return value from open()
+ * @err: Filled in with errno if open() failed
+ * @path: Path to open
+ * @flags: open() flags
+ */
+struct open_in_ns_args {
+ const struct ctx *c;
+ int fd;
+ int err;
+ const char *path;
+ int flags;
+};
+
+/**
+ * do_open_in_ns() - Enter namespace and open a file
+ * @arg: See struct open_in_ns_args
+ *
+ * Must be called via NS_CALL()
+ */
+static int do_open_in_ns(void *arg)
+{
+ struct open_in_ns_args *a = (struct open_in_ns_args *)arg;
+
+ ns_enter(a->c);
+
+ a->fd = open(a->path, a->flags);
+ a->err = errno;
+
+ return 0;
+}
+
+/**
+ * open_in_ns() - open() within the pasta namespace
+ * @c: Execution context
+ * @path: Path to open
+ * @flags: open() flags
+ *
+ * Return: fd of open()ed file or -1 on error, errno is set to indicate error
+ */
+int open_in_ns(const struct ctx *c, const char *path, int flags)
+{
+ struct open_in_ns_args arg = {
+ .c = c, .path = path, .flags = flags,
+ };
+
+ NS_CALL(do_open_in_ns, &arg);
+ errno = arg.err;
+ return arg.fd;
+}
+
+/**
* pid_file() - Write PID to file, if requested to do so, and close it
* @fd: Open PID file descriptor, closed on exit, -1 to skip writing it
* @pid: PID value to write