initial checkin
[util-vserver.git] / util-vserver / src / lockfile.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 <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <time.h>
32 #include <sys/file.h>
33
34 static void
35 showHelp(int fd, char const *cmd, int res)
36 {
37   WRITE_MSG(fd, "Usage:  ");
38   WRITE_STR(fd, cmd);
39   WRITE_MSG(fd,
40             " [--] <lockfile> <syncpipe> [<timeout>]\n\n"
41             "Protocol:\n"
42             "  1.  parent (shell) creates a named <syncpipe>\n"
43             "  2.  'lockfile' will be called\n"
44             "  3a. 'lockfile' waits until somebody opens the <syncpipe> for reading\n"
45             "  3b. parent (shell) opens the pipe for reading and blocks\n"
46             "  4.  'lockfile' calls flock() on the <lockfile>\n"
47             "  5.  'lockfile' closes the <syncpipe>\n"
48             "  6.  parent (shell) unlocks since <syncpipe> is closed\n"
49             "  7.  'lockfile' goes into infinite loop\n"
50             "  8.  parent sends SIGHUP (or other signal) to 'lockfile\n"
51             "\n"
52             "Sample code:\n"
53             "  tmp=$(mktemp /tmp/lock.XXXXXX)\n"
54             "  rm -f $tmp    # safe since mknod(2) does not follow symlinks\n"
55             "  mkfifo $tmp\n"
56             "  lockfile $lock $tmp &\n"
57             "  cat <$tmp/sync >/dev/null\n"
58             "  ... <actions> ...\n"
59             "  kill -HUP $!  # (implicated by shell-exit)\n"
60             "\n"
61             "Please report bugs to " PACKAGE_BUGREPORT "\n");
62   exit(res);
63 }
64
65 static void
66 showVersion()
67 {
68   WRITE_MSG(1,
69             "lockfile " VERSION " -- locks a file"
70             "This program is part of " PACKAGE_STRING "\n\n"
71             "Copyright (C) 2004 Enrico Scholz\n"
72             VERSION_COPYRIGHT_DISCLAIMER);
73   exit(0);
74 }
75
76 static volatile int             timeout_flag = 0;
77
78 static void
79 alarmFunc(int UNUSED sig)
80 {
81   timeout_flag = 1;
82   signal(SIGALRM, alarmFunc);
83 }
84
85 static void
86 quitFunc(int UNUSED sig)
87 {
88   _exit(0);
89 }
90
91 int main(int argc, char *argv[])
92 {
93   int                   fd, sync_fd;
94   int                   idx = 1;
95   time_t                end_time;
96
97   if (argc>=2) {
98     if (strcmp(argv[1], "--help")   ==0) showHelp(1, argv[0], 0);
99     if (strcmp(argv[1], "--version")==0) showVersion();
100     if (strcmp(argv[1], "--")       ==0) ++idx;
101   }
102
103   if (argc<idx+2) {
104     WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
105     return EXIT_FAILURE;
106   }
107
108   end_time = time(0);
109   if (argc==idx+3) end_time += atoi(argv[idx+2]);
110   else             end_time += 300;
111
112   
113                    
114   if ((fd=open(argv[idx], O_CREAT|O_RDONLY|O_NOFOLLOW|O_NONBLOCK, 0644))==-1)
115     perror("lockfile: open(<lockfile>)");
116   else if ((sync_fd=open(argv[idx+1], O_WRONLY))==-1)
117     perror("lockfile: open(<syncpipe>)");
118   else if (unlink(argv[idx+1])==-1)
119     perror("lockfile: unlink(<syncpipe>)");
120   else if (siginterrupt(SIGALRM, 1)==-1)
121     perror("lockfile: siginterrupt()");
122   else if (signal(SIGALRM, alarmFunc)==SIG_ERR ||
123            signal(SIGHUP,  quitFunc)==SIG_ERR)
124     perror("lockfile: signal()");
125   else while (time(0)<end_time && getppid()!=1) {
126     alarm(1);
127     if (flock(fd,LOCK_EX)==-1) {
128       if (errno==EINTR) continue;
129       perror("lockfile: flock()");
130       return EXIT_FAILURE;
131     }
132
133     close(sync_fd);
134
135     signal(SIGALRM, SIG_IGN);
136     while (getppid()!=1) sleep(1);
137            
138     return EXIT_SUCCESS;
139   }
140
141   return EXIT_FAILURE;
142 }
143