diff options
| author | David Gibson <david@gibson.dropbear.id.au> | 2026-07-17 15:46:32 +1000 |
|---|---|---|
| committer | Stefano Brivio <sbrivio@redhat.com> | 2026-07-18 09:54:29 +0200 |
| commit | ab825955836cb04c1e994d17af4c50a243ea67a0 (patch) | |
| tree | 5d24115dce08701733f8202e0733adbe5104210d | |
| parent | 33e0fbc396f494e2fdd8033191b76d2c4aef43ec (diff) | |
| download | passt-ab825955836cb04c1e994d17af4c50a243ea67a0.tar passt-ab825955836cb04c1e994d17af4c50a243ea67a0.tar.gz passt-ab825955836cb04c1e994d17af4c50a243ea67a0.tar.bz2 passt-ab825955836cb04c1e994d17af4c50a243ea67a0.tar.lz passt-ab825955836cb04c1e994d17af4c50a243ea67a0.tar.xz passt-ab825955836cb04c1e994d17af4c50a243ea67a0.tar.zst passt-ab825955836cb04c1e994d17af4c50a243ea67a0.zip | |
conf: Make conf_tap_fd() operate more like conf_mode()
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>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
| -rw-r--r-- | conf.c | 25 | ||||
| -rw-r--r-- | conf.h | 2 | ||||
| -rw-r--r-- | isolation.c | 13 |
3 files changed, 24 insertions, 16 deletions
@@ -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; } @@ -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 c9dfefa..725a72b 100644 --- a/isolation.c +++ b/isolation.c @@ -63,7 +63,6 @@ #include <errno.h> #include <fcntl.h> -#include <getopt.h> #include <grp.h> #include <inttypes.h> #include <limits.h> @@ -262,17 +261,9 @@ void isolate_initial(void) */ int isolate_fds(int argc, char **argv) { - const struct option optfd[] = { { "fd", required_argument, NULL, 'F' }, - { 0 }, }; - long fd = -1; - int name, rc; + int fd, rc; - do { - name = getopt_long(argc, argv, "-:F:", optfd, NULL); - - if (name == 'F') - fd = conf_tap_fd(optarg); - } while (name != -1); + fd = conf_tap_fd(argc, argv); if (fd == -1) { rc = close_range(STDERR_FILENO + 1, ~0U, CLOSE_RANGE_UNSHARE); |
