Various common utility sources
[nethome.git] / src / flock.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <signal.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <limits.h>
11 #include <getopt.h>
12 #include <sys/file.h>
13
14 #define failf(name) do { fprintf(stderr,"%s: ",pname); perror(name"()"); exit(EXIT_FAILURE); } while (0)
15 const char version[]="This is FLock, version 1.0\n";
16
17 int verbose;
18
19 char *pname;
20
21 static __NORETURN void usage(void)
22 {
23         fprintf(stderr,"\
24 %s\
25 This command acts as an interface to flock(2):\n\
26 \n\
27 Usage: flock [{-s|--shared}|{-x|--exclusive}] [-n|--nonblock]\n\
28              [-v|--verbose] [-h|--help] [-V|--version]\n\
29              <lockname> <command>\n\
30 \n\
31   -s, --shared\tCreate shared lock (LOCK_SH) - DEFAULT\n\
32   -x, --exclusive\tCreate exclusive lock (LOCK_EX)\n\
33   -n, --nonblock\tFail immediately if req. lock is busy\n\
34   -v, --verbose\tDisplay progress during locking\n\
35   -h, --help\tPrint a summary of the options\n\
36   -V, --version\tPrint the version number\n\
37 ",version);
38         exit(EXIT_FAILURE);
39 }
40
41 const struct option longopts[]={
42 {"shared"   ,0,0,'s'},
43 {"exclusive",0,0,'x'},
44 {"nonblock" ,0,0,'n'},
45 {"verbose"  ,0,0,'v'},
46 {"help"     ,0,0,'h'},
47 {"version"  ,0,0,'V'}};
48
49 int main(int argc,char **argv)
50 {
51 char *fn,*cmd;
52 int locktype=LOCK_SH,nonblock=0,fd,optc;
53
54         pname=*argv;
55         while ((optc=getopt_long(argc,argv,"sxnvhV",longopts,NULL))!=EOF) switch (optc) {
56                 case 's':
57                         locktype=LOCK_SH;
58                         break;
59                 case 'x':
60                         locktype=LOCK_EX;
61                         break;
62                 case 'n':
63                         nonblock=LOCK_NB;
64                         break;
65                 case 'v':
66                         verbose=1;
67                         break;
68                 case 'V':
69                         fprintf(stderr,version);
70                         exit(EXIT_FAILURE);
71                 default: /* also 'h' */
72                         usage();
73                         break;
74                 }
75         if (optind+2!=argc) {
76                 fprintf(stderr,"%s: Incorrect number of parameters (command needs quoting?)!\n",pname);
77                 exit(EXIT_FAILURE);
78                 }
79         fn=argv[optind];
80         cmd=argv[optind+1];
81         if (verbose) { fprintf(stderr,"%s: Opening \"%s\"...",pname,fn); fflush(stderr); }
82         if ((fd=open(fn,O_RDONLY|(nonblock?O_NONBLOCK:0)))==-1) failf("open");
83         if (verbose) { fprintf(stderr,"done\n%s: Locking \"%s\"...",pname,fn); fflush(stderr); }
84         if (flock(fd,locktype|nonblock)) failf("flock");
85         if (verbose) { fprintf(stderr,"done\n%s: Running \"%s\"...",pname,cmd); fflush(stderr); }
86         if (system(cmd)==-1) failf("system");
87         if (verbose) { fprintf(stderr,"done\n%s: Finishing - removing lock.\n",pname); }
88         return(EXIT_SUCCESS);
89 }