waitinterface.c (1402B)
1 #include <unistd.h> 2 #include <time.h> 3 #include <sys/stat.h> 4 #include <sys/socket.h> 5 #include <string.h> 6 #include <netinet/in.h> 7 #include <net/if.h> 8 #include <sys/ioctl.h> 9 10 #include <libowfat/fmt.h> 11 #include <libowfat/errmsg.h> 12 13 int main(int argc,char* argv[],char* envp[]) { 14 unsigned int i; 15 struct timespec req,rem; 16 struct stat ss; 17 char* sys; 18 int found; 19 errmsg_iam("waitinterface"); 20 if (argc<2) 21 die(0,"usage: waitinterface wlan0 /usr/sbin/dhcpcd wlan0\n\twaits for wlan0 to show up and then executes the rest of the command line"); 22 sys=fmt_strm_alloca("/sys/class/net/",argv[1]); 23 req.tv_sec=0; req.tv_nsec=100000000; 24 for (i=0; i<100; ++i) { 25 if ((found=lstat(sys,&ss))==0) break; 26 nanosleep(&req,&rem); 27 } 28 if (found==-1) 29 die(1,"interface not showing up"); 30 if (strstr(argv[0],"up")) { 31 /* they want the interface to be up, too */ 32 int s=socket(PF_INET6,SOCK_DGRAM,IPPROTO_IP); 33 struct ifreq ifr; 34 if (s==-1) s=socket(PF_INET,SOCK_DGRAM,IPPROTO_IP); 35 if (s==-1) diesys(1,"socket"); 36 strncpy(ifr.ifr_name,argv[1],sizeof(ifr.ifr_name)); 37 for (i=0; i<100; ++i) { 38 if (ioctl(s,SIOCGIFFLAGS,&ifr)==0 && (ifr.ifr_flags&IFF_UP)) break; 39 nanosleep(&req,&rem); 40 } 41 close(s); 42 if (!(ifr.ifr_flags&IFF_UP)) 43 die(1,"interface not up"); 44 } 45 if (argv[2]) { 46 execve(argv[2],argv+2,envp); 47 diesys(1,"execve"); 48 } 49 return 0; 50 }