Get more help from gcc, add -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2...
[sysvinit.git] / src / ifdown.c
index 42c0d6fd3f63334c2ef7709b4be51b2882ba3463..a2b3bc5da70cc41239b9fa7be61e06410f2695e8 100644 (file)
@@ -26,11 +26,11 @@ char *v_ifdown = "@(#)ifdown.c  1.11  02-Jun-1998  miquels@cistron.nl";
 #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 <sys/errno.h>
 
 #include <net/if.h>
 #include <netinet/in.h>
@@ -77,15 +77,33 @@ int ifdown(void)
                                continue;
                        if (strchr(ifr[i].ifr_name, ':') != NULL)
                                continue;
-                       ifr[i].ifr_flags &= ~(IFF_UP);
-                       if (ioctl(fd, SIOCSIFFLAGS, &ifr[i]) < 0) {
+
+                       /* Read interface flags */
+                       if (ioctl(fd, SIOCGIFFLAGS, &ifr[i]) < 0) {
                                fprintf(stderr, "ifdown: shutdown ");
                                perror(ifr[i].ifr_name);
+                               continue;
                        }
+                       /*
+                        * Expected in <net/if.h> according to
+                        * "UNIX Network Programming".
+                        */
+#ifdef ifr_flags
+# define IRFFLAGS      ifr_flags
+#else  /* Present on kFreeBSD */
+# define IRFFLAGS      ifr_flagshigh
+#endif
+                       if (ifr[i].IRFFLAGS & IFF_UP) {
+                               ifr[i].IRFFLAGS &= ~(IFF_UP);
+                               if (ioctl(fd, SIOCSIFFLAGS, &ifr[i]) < 0) {
+                                       fprintf(stderr, "ifdown: shutdown ");
+                                       perror(ifr[i].ifr_name);
+                               }
+                       }
+#undef IRFFLAGS
                }
        }
        close(fd);
 
        return 0;
 }
-