From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 92F055A0275 for ; Thu, 5 Oct 2023 05:44:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1696477489; bh=ZS8u7CPcJExRl4HHKnYVK4KtCAeS2kdebsFwy0uiEvQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=efSMjFdQjPD9Q+sRzvpnFFkJTYXJRwmyWPxG25u4qSmc5cN+q9d420LcRWKkMWbtY 7RB1XoQ2AsadapUeMAX0vvKv7oeO++NViOzvHVeoA/iesENFXPDciFSD1L9ccP4iQD h4LwcIdmwaM1uM1XwF6IpnLZ8zgjwLnxgLWruADU= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4S1HW51Shlz4xVL; Thu, 5 Oct 2023 14:44:49 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 4/9] util: Add open_in_ns() helper Date: Thu, 5 Oct 2023 14:44:40 +1100 Message-ID: <20231005034445.2015303-5-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231005034445.2015303-1-david@gibson.dropbear.id.au> References: <20231005034445.2015303-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: QCAMLTV6ER5WTZJ6NFQPLNF37TP6NTVA X-Message-ID-Hash: QCAMLTV6ER5WTZJ6NFQPLNF37TP6NTVA X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- util.c | 39 +++++++++++++++++++++++++++++++++++++++ util.h | 1 + 2 files changed, 40 insertions(+) diff --git a/util.c b/util.c index a8f3b35..92ad375 100644 --- a/util.c +++ b/util.c @@ -364,6 +364,45 @@ bool ns_is_init(void) return ret; } +struct open_in_ns_args { + const struct ctx *c; + int fd; + int err; + const char *path; + int flags; +}; + +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 diff --git a/util.h b/util.h index 9effc54..78a8fb2 100644 --- a/util.h +++ b/util.h @@ -219,6 +219,7 @@ int bitmap_isset(const uint8_t *map, int bit); char *line_read(char *buf, size_t len, int fd); void ns_enter(const struct ctx *c); bool ns_is_init(void); +int open_in_ns(const struct ctx *c, const char *path, int flags); void write_pidfile(int fd, pid_t pid); int __daemon(int pidfile_fd, int devnull_fd); int fls(unsigned long x); -- 2.41.0