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