From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 3/6] conf: Make conf_tap_fd() operate more like conf_mode()
Date: Tue, 14 Jul 2026 19:29:23 +1000 [thread overview]
Message-ID: <20260714092926.2881848-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20260714092926.2881848-1-david@gibson.dropbear.id.au>
We have two cases where we need to parse specific options early:
conf_tap_fd() and conf_mode(). conf_tap_fd() has a slightly odd interface,
requiring the caller to use getopt_long() to find the right option, then
pass it in. Alter it to work like conf_mode() instead, where all the
command line parsing logic is contained within the conf.c function.
This is slightly more lines, but has a clearer division of responsibility.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
conf.c | 25 +++++++++++++++++++++----
conf.h | 2 +-
isolation.c | 12 ++----------
3 files changed, 24 insertions(+), 15 deletions(-)
diff --git a/conf.c b/conf.c
index 41c9d557..f0bce41b 100644
--- a/conf.c
+++ b/conf.c
@@ -1153,17 +1153,34 @@ static void conf_sock_listen(const struct ctx *c)
/**
* conf_tap_fd() - Read tap fd as supplied by -F command line option
- * @arg: Argument to -F command line option
+ * @argc: Argument count
+ * @argv: Command line options
+ *
+ * Return: fd number from --fd option, or -1 if not supplied
*/
-int conf_tap_fd(const char *arg)
+int conf_tap_fd(int argc, char **argv)
{
- const char *p = arg;
+ const struct option optfd[] = { { "fd", required_argument, NULL, 'F' },
+ { 0 }, };
+ const char *fdarg = NULL, *p;
unsigned long val;
+ int name;
+
+ optind = 0;
+ do {
+ name = getopt_long(argc, argv, "-:F:", optfd, NULL);
+ if (name == 'F')
+ fdarg = optarg;
+ } while (name != -1);
+
+ if (!fdarg)
+ return -1;
+ p = fdarg;
if (!parse_unsigned(&p, 0, &val) || !parse_eoi(p) ||
val > INT_MAX ||
(val != STDIN_FILENO && val <= STDERR_FILENO))
- die("Invalid --fd: %s", arg);
+ die("Invalid --fd: %s", fdarg);
return val;
}
diff --git a/conf.h b/conf.h
index 1fa1280e..19bf9bc5 100644
--- a/conf.h
+++ b/conf.h
@@ -7,7 +7,7 @@
#define CONF_H
enum passt_modes conf_mode(int argc, char *argv[]);
-int conf_tap_fd(const char *arg);
+int conf_tap_fd(int argc, char **argv);
void conf(struct ctx *c, int argc, char **argv);
void conf_listen_handler(struct ctx *c, uint32_t events);
void conf_handler(struct ctx *c, uint32_t events);
diff --git a/isolation.c b/isolation.c
index 4bb79e34..c4d476d6 100644
--- a/isolation.c
+++ b/isolation.c
@@ -58,7 +58,6 @@
#include <errno.h>
#include <fcntl.h>
-#include <getopt.h>
#include <grp.h>
#include <inttypes.h>
#include <limits.h>
@@ -208,9 +207,7 @@ static int move_root(void)
*/
int isolate_initial(int argc, char **argv)
{
- const struct option optfd[] = { { "fd", required_argument, NULL, 'F' },
- { 0 }, };
- int name, rc, fd_tap = -1;
+ int rc, fd_tap = -1;
uint64_t keep;
/* We want to keep CAP_NET_BIND_SERVICE in the initial
@@ -251,12 +248,7 @@ int isolate_initial(int argc, char **argv)
drop_caps_ep_except(keep);
- do {
- name = getopt_long(argc, argv, "-:F:", optfd, NULL);
-
- if (name == 'F')
- fd_tap = conf_tap_fd(optarg);
- } while (name != -1);
+ fd_tap = conf_tap_fd(argc, argv);
if (fd_tap == -1) {
rc = close_range(STDERR_FILENO + 1, ~0U, CLOSE_RANGE_UNSHARE);
--
2.55.0
next prev parent reply other threads:[~2026-07-14 9:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-14 9:29 [PATCH 0/6] Fix bug 215 and some related issues with fd handling David Gibson
2026-07-14 9:29 ` [PATCH 1/6] isolation, util: Fold close_open_files() into isolate_initial() David Gibson
2026-07-15 7:12 ` Stefano Brivio
2026-07-14 9:29 ` [PATCH 2/6] isolation, conf: Set c->fd_tap from eary parse of --fd David Gibson
2026-07-15 7:12 ` Stefano Brivio
2026-07-14 9:29 ` David Gibson [this message]
2026-07-14 9:29 ` [PATCH 4/6] isolation: Move --fd descriptor to a number of our choosing David Gibson
2026-07-14 9:29 ` [PATCH 5/6] main: Ensure fds 0-2 are populated David Gibson
2026-07-14 9:29 ` [PATCH 6/6] passt: *Always* close pidfile_fd, not just when daemonizing David Gibson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260714092926.2881848-4-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).