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.
33 #include <sys/param.h>
36 showHelp(char const *cmd)
38 WRITE_MSG(1, "Usage: ");
41 " [--] <lockfile> <syncpipe> [<timeout>]\n\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 lockf() 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"
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"
59 " ... <actions> ...\n"
60 " kill -HUP $! # (implicated by shell-exit)\n"
62 "Please report bugs to " PACKAGE_BUGREPORT "\n");
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);
78 alarmFunc(int UNUSED sig)
80 signal(SIGALRM, alarmFunc);
84 quitFunc(int UNUSED sig)
89 int main(int argc, char *argv[])
94 pid_t const ppid = getppid();
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;
103 WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
108 if (argc==idx+3) end_time += atoi(argv[idx+2]);
109 else end_time += 300;
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_WRONLY|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)));
126 if (lockf(fd,F_LOCK,0)==-1) {
127 if (errno==EINTR) continue;
128 perror("lockfile: lockf()");
131 signal(SIGALRM, SIG_IGN);
133 WRITE_MSG(sync_fd, "#!/bin/true\n");
135 while (getppid()==ppid) sleep(10);
141 WRITE_MSG(sync_fd, "#!/bin/false\n");