diff options
author | Alyssa Ross <hi@alyssa.is> | 2022-02-20 12:51:20 +0000 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2022-02-21 14:42:24 +0000 |
commit | 2adcaf59ea3902f0116f1703dfbfe084623edcba (patch) | |
tree | 06d3b807e85f51754635a79230648b4a1fd3244d /host | |
parent | 83fe8d9760bc36680c3294fa5f8c0ee7f45da7af (diff) | |
download | spectrum-2adcaf59ea3902f0116f1703dfbfe084623edcba.tar spectrum-2adcaf59ea3902f0116f1703dfbfe084623edcba.tar.gz spectrum-2adcaf59ea3902f0116f1703dfbfe084623edcba.tar.bz2 spectrum-2adcaf59ea3902f0116f1703dfbfe084623edcba.tar.lz spectrum-2adcaf59ea3902f0116f1703dfbfe084623edcba.tar.xz spectrum-2adcaf59ea3902f0116f1703dfbfe084623edcba.tar.zst spectrum-2adcaf59ea3902f0116f1703dfbfe084623edcba.zip |
host/start-vm: tap_open: return name of tap
This is important when using %d to dynamically choose a tap name in the kernel. It's not needed for the start-vm program, but supporting dynamic names makes writing tests easier.
Diffstat (limited to 'host')
-rw-r--r-- | host/start-vm/net-util.c | 15 | ||||
-rw-r--r-- | host/start-vm/net-util.h | 4 | ||||
-rw-r--r-- | host/start-vm/tests/tap_open.c | 11 |
3 files changed, 21 insertions, 9 deletions
diff --git a/host/start-vm/net-util.c b/host/start-vm/net-util.c index cc6b950..f4c454f 100644 --- a/host/start-vm/net-util.c +++ b/host/start-vm/net-util.c @@ -1,10 +1,11 @@ // SPDX-License-Identifier: EUPL-1.2 // SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is> +#include "net-util.h" + #include <err.h> #include <errno.h> #include <fcntl.h> -#include <net/if.h> #include <string.h> #include <unistd.h> @@ -126,7 +127,7 @@ int bridge_delete(const char *name) return r; } -int tap_open(const char *name, int flags) +int tap_open(char name[static IFNAMSIZ], int flags) { struct ifreq ifr; int fd; @@ -141,9 +142,11 @@ int tap_open(const char *name, int flags) if ((fd = open("/dev/net/tun", O_RDWR)) == -1) return -1; - if (!ioctl(fd, TUNSETIFF, &ifr)) - return fd; + if (ioctl(fd, TUNSETIFF, &ifr) == -1) { + close(fd); + return -1; + } - close(fd); - return -1; + strncpy(name, ifr.ifr_name, IFNAMSIZ); + return fd; } diff --git a/host/start-vm/net-util.h b/host/start-vm/net-util.h index 85d7833..e1899eb 100644 --- a/host/start-vm/net-util.h +++ b/host/start-vm/net-util.h @@ -1,6 +1,8 @@ // SPDX-License-Identifier: EUPL-1.2 // SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is> +#include <net/if.h> + int if_up(const char *name); int if_rename(const char *name, const char *newname); int if_down(const char *name); @@ -10,4 +12,4 @@ int bridge_add_if(const char *brname, const char *ifname); int bridge_remove_if(const char *brname, const char *ifname); int bridge_delete(const char *name); -int tap_open(const char *name, int flags); +int tap_open(char name[static IFNAMSIZ], int flags); diff --git a/host/start-vm/tests/tap_open.c b/host/start-vm/tests/tap_open.c index ba9b9ec..19bc145 100644 --- a/host/start-vm/tests/tap_open.c +++ b/host/start-vm/tests/tap_open.c @@ -6,10 +6,16 @@ #include <assert.h> #include <errno.h> #include <sched.h> +#include <string.h> + +#include <sys/ioctl.h> + +#include <linux/if_tun.h> int main(void) { - char name[] = "tap%d"; + char name[IFNAMSIZ] = "tap%d"; + struct ifreq ifr; int fd; unshare(CLONE_NEWUSER|CLONE_NEWNET); @@ -17,5 +23,6 @@ int main(void) fd = tap_open(name, 0); if (fd == -1 && errno == EPERM) return 77; - assert(fd != -1); + assert(!ioctl(fd, TUNGETIFF, &ifr)); + assert(!strcmp(name, ifr.ifr_name)); } |