3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
35 showHelp(int fd, char const *cmd, int res)
37 WRITE_MSG(fd, "Usage: ");
40 " [--] <lockfile> <syncpipe> [<timeout>]\n\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"
53 " tmp=$(mktemp /tmp/lock.XXXXXX)\n"
54 " rm -f $tmp # safe since mknod(2) does not follow symlinks\n"
56 " lockfile $lock $tmp &\n"
57 " cat <$tmp/sync >/dev/null\n"
58 " ... <actions> ...\n"
59 " kill -HUP $! # (implicated by shell-exit)\n"
61 "Please report bugs to " PACKAGE_BUGREPORT "\n");
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);
76 static volatile int timeout_flag = 0;
79 alarmFunc(int UNUSED sig)
82 signal(SIGALRM, alarmFunc);
86 quitFunc(int UNUSED sig)
91 int main(int argc, char *argv[])
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;
104 WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
109 if (argc==idx+3) end_time += atoi(argv[idx+2]);
110 else end_time += 300;
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) {
127 if (flock(fd,LOCK_EX)==-1) {
128 if (errno==EINTR) continue;
129 perror("lockfile: flock()");
135 signal(SIGALRM, SIG_IGN);
136 while (getppid()!=1) sleep(1);