use 'findObject -e' instead of 'findFile' to find /dev/null also
[util-vserver.git] / util-vserver / src / vshelper-sync.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "util.h"
24
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <stdio.h>
30
31 static void
32 showHelp(int fd, char const *cmd, int res)
33 {
34   WRITE_MSG(fd, "Usage:  ");
35   WRITE_STR(fd, cmd);
36   WRITE_MSG(fd,
37             " [--] <pipe> <timeout>\n\n"
38             "Please report bugs to " PACKAGE_BUGREPORT "\n");
39   exit(res);
40 }
41
42 static void
43 showVersion()
44 {
45   WRITE_MSG(1,
46             "vshelper-sync " VERSION " -- waits for data from a pipe"
47             "This program is part of " PACKAGE_STRING "\n\n"
48             "Copyright (C) 2004 Enrico Scholz\n"
49             VERSION_COPYRIGHT_DISCLAIMER);
50   exit(0);
51 }
52
53 static volatile sig_atomic_t    timeout_flag = 0;
54
55 void alarmHandler(int UNUSED sig)
56 {
57   timeout_flag = 1;
58 }
59
60 int main(int argc, char *argv[])
61 {
62   int           fd;
63   int           idx = 1;
64
65   if (argc>=2) {
66     if (strcmp(argv[1], "--help")   ==0) showHelp(1, argv[0], 0);
67     if (strcmp(argv[1], "--version")==0) showVersion();
68     if (strcmp(argv[1], "--")       ==0) ++idx;
69   }
70
71   if (argc<idx+2) {
72     WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
73     return EXIT_FAILURE;
74   }
75
76
77   signal(SIGALRM, alarmHandler);
78   alarm(atoi(argv[idx+1]));
79   
80   fd = open(argv[idx], O_RDONLY,0);
81   if (timeout_flag) return EXIT_FAILURE;
82   if (fd==-1) {
83     perror("vshelper-sync: open()");
84     return EXIT_FAILURE;
85   }
86   
87   for (;;) {
88     char        buf[512];
89     ssize_t     len = read(fd,buf,sizeof buf);
90     if (len==0) break;
91     if (timeout_flag) return EXIT_FAILURE;
92     if (len==-1) {
93       perror("vshelper-sync: read()");
94       return EXIT_FAILURE;
95     }
96   }
97
98   return EXIT_SUCCESS;
99 }