diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:56:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:56:35 +0000 |
commit | eba0cfa6b0bef4f2e73c8630a7efa3944df8b0f8 (patch) | |
tree | 74c37eede1f0634cc5de1c63c934edaa1630c6bc /kexec/ifdown.c | |
parent | Initial commit. (diff) | |
download | kexec-tools-7bbe6b040c9123991377749057cfc0356c289ceb.tar.xz kexec-tools-7bbe6b040c9123991377749057cfc0356c289ceb.zip |
Adding upstream version 1:2.0.27.upstream/1%2.0.27upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'kexec/ifdown.c')
-rw-r--r-- | kexec/ifdown.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/kexec/ifdown.c b/kexec/ifdown.c new file mode 100644 index 0000000..3ac19c1 --- /dev/null +++ b/kexec/ifdown.c @@ -0,0 +1,79 @@ +/* + * ifdown.c Find all network interfaces on the system and + * shut them down. + * + */ +char *v_ifdown = "@(#)ifdown.c 1.11 02-Jun-1998 miquels@cistron.nl"; + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <time.h> +#include <string.h> +#include <errno.h> + +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <sys/time.h> + +#include <net/if.h> +#include <netinet/in.h> + +/* + * First, we find all shaper devices and down them. Then we + * down all real interfaces. This is because the comment in the + * shaper driver says "if you down the shaper device before the + * attached inerface your computer will follow". + */ +int ifdown(void) +{ + struct if_nameindex *ifa, *ifp; + struct ifreq ifr; + int fd, shaper; + + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + fprintf(stderr, "ifdown: "); + perror("socket"); + goto error; + } + + if ((ifa = if_nameindex()) == NULL) { + fprintf(stderr, "ifdown: "); + perror("if_nameindex"); + goto error; + } + + for (shaper = 1; shaper >= 0; shaper--) { + for (ifp = ifa; ifp->if_index; ifp++) { + + if ((strncmp(ifp->if_name, "shaper", 6) == 0) + != shaper) continue; + if (strcmp(ifp->if_name, "lo") == 0) + continue; + if (strchr(ifp->if_name, ':') != NULL) + continue; + + strncpy(ifr.ifr_name, ifp->if_name, IFNAMSIZ-1); + ifr.ifr_name[IFNAMSIZ-1] = 0; + if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) { + fprintf(stderr, "ifdown: shutdown "); + perror(ifp->if_name); + goto error; + } + ifr.ifr_flags &= ~(IFF_UP); + if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) { + fprintf(stderr, "ifdown: shutdown "); + perror(ifp->if_name); + goto error; + } + + } + } + + close(fd); + return 0; + +error: + close(fd); + return -1; +} |