X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=util-vserver%2Fsrc%2Fvshelper-sync.c;h=28d14d02d3d33faabecd9bae04044688c21589f0;hb=a917f24ef7b003dcef54a3db2644cf9cb4bc2db2;hp=cf987dee1e6471a5b4f438f341a024520e967816;hpb=a8d292acbadef48cd3f2062015a72fc0f267e96a;p=util-vserver.git diff --git a/util-vserver/src/vshelper-sync.c b/util-vserver/src/vshelper-sync.c index cf987de..28d14d0 100644 --- a/util-vserver/src/vshelper-sync.c +++ b/util-vserver/src/vshelper-sync.c @@ -27,6 +27,7 @@ #include #include #include +#include static void showHelp(int fd, char const *cmd, int res) @@ -50,17 +51,11 @@ showVersion() exit(0); } -static volatile sig_atomic_t timeout_flag = 0; - -void alarmHandler(int UNUSED sig) -{ - timeout_flag = 1; -} - int main(int argc, char *argv[]) { - int fd; - int idx = 1; + int fd; + int idx = 1; + struct timeval timeout; if (argc>=2) { if (strcmp(argv[1], "--help") ==0) showHelp(1, argv[0], 0); @@ -73,22 +68,38 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - - signal(SIGALRM, alarmHandler); - alarm(atoi(argv[idx+1])); - - fd = open(argv[idx], O_RDONLY,0); - if (timeout_flag) return EXIT_FAILURE; + fd = open(argv[idx], O_RDONLY|O_NONBLOCK, 0); if (fd==-1) { perror("vshelper-sync: open()"); return EXIT_FAILURE; } - + + timeout.tv_sec = atoi(argv[idx+1]); + for (;;) { char buf[512]; - ssize_t len = read(fd,buf,sizeof buf); + ssize_t len; + fd_set fds; + + FD_ZERO(&fds); + FD_SET(fd, &fds); + +#ifndef __linux +# error vshelper relies on the Linux select() behavior (timeout holds remaining time) +#endif + + switch (select(fd+1, &fds, 0,0, &timeout)) { + case 0 : return EXIT_FAILURE; // timeout + case -1 : + perror("vshelper: select()"); + return EXIT_FAILURE; + default : break; + } + + assert(FD_ISSET(fd, &fds)); + + len = read(fd,buf,sizeof buf); if (len==0) break; - if (timeout_flag) return EXIT_FAILURE; if (len==-1) { perror("vshelper-sync: read()"); return EXIT_FAILURE; @@ -96,4 +107,4 @@ int main(int argc, char *argv[]) } return EXIT_SUCCESS; -} +}