tap: Don't leak file descriptor used to bring up loopback interface

...and while at it, set the socket as non-blocking directly on open().

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
Stefano Brivio 2021-09-27 00:28:24 +02:00
parent f1c1d40f90
commit f004de4a9d

19
tap.c
View file

@ -848,20 +848,25 @@ static int tap_sock_init_tun_ns(void *target_pid)
if (ns_enter(*(int *)target_pid)) if (ns_enter(*(int *)target_pid))
goto fail; goto fail;
if ((fd = open("/dev/net/tun", O_RDWR)) < 0) if ((fd = open("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
goto fail; goto fail;
fcntl(fd, F_SETFL, O_NONBLOCK);
tun_ns_fd = fd; tun_ns_fd = fd;
if (ioctl(socket(AF_INET, SOCK_DGRAM, 0), SIOCSIFFLAGS, if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
&((struct ifreq) { .ifr_name = "lo", perror("socket for ioctl");
.ifr_flags = IFF_UP }))) {
perror("SIOCSIFFLAGS ioctl for \"lo\"");
goto fail; goto fail;
} }
if (ioctl(fd, SIOCSIFFLAGS, &((struct ifreq){ .ifr_name = "lo",
.ifr_flags = IFF_UP }))) {
perror("SIOCSIFFLAGS ioctl for \"lo\"");
close(fd);
goto fail;
}
close(fd);
return 0; return 0;
fail: fail: