2 * Copyright (C) 1997 John D. Polstra. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY JOHN D. POLSTRA AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL JOHN D. POLSTRA OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * $FreeBSD: src/usr.bin/lockf/lockf.c,v 1.8 1999/08/28 01:03:07 peter Exp $
26 * $DragonFly: src/usr.bin/lockf/lockf.c,v 1.3 2005/01/05 00:46:52 cpressey Exp $
29 #include <sys/types.h>
41 static int acquire_lock(const char *name
);
42 static void cleanup(void);
43 static void killed(int sig
);
44 static void timeout(int sig
);
45 static void usage(void);
46 static void wait_for_lock(const char *name
);
48 static const char *lockname
;
51 static volatile sig_atomic_t timed_out
;
54 * Execute an arbitrary command while holding a file lock.
57 main(int argc
, char **argv
)
67 waitsec
= -1; /* Infinite. */
68 while ((ch
= getopt(argc
, argv
, "skt:")) != -1) {
82 waitsec
= strtol(optarg
, &endptr
, 0);
83 if (*optarg
== '\0' || *endptr
!= '\0' || waitsec
< 0)
84 errx(EX_USAGE
, "invalid timeout \"%s\"", optarg
);
92 if (argc
- optind
< 2)
94 lockname
= argv
[optind
++];
98 if (waitsec
> 0) { /* Set up a timeout. */
101 act
.sa_handler
= timeout
;
102 sigemptyset(&act
.sa_mask
);
103 act
.sa_flags
= 0; /* Note that we do not set SA_RESTART. */
105 sigaction(SIGALRM
, &act
, NULL
);
109 lockfd
= acquire_lock(lockname
);
110 while (lockfd
== -1 && !timed_out
&& waitsec
!= 0) {
111 wait_for_lock(lockname
);
112 lockfd
= acquire_lock(lockname
);
118 if (lockfd
== -1) { /* We failed to acquire the lock. */
121 errx(EX_TEMPFAIL
, "%s: already locked", lockname
);
124 /* At this point, we own the lock. */
126 if (atexit(cleanup
) == -1)
127 err(EX_OSERR
, "atexit failed");
129 if ((child
= fork()) == -1)
130 err(EX_OSERR
, "cannot fork");
132 if (child
== 0) { /* The child process. */
134 execvp(argv
[0], argv
);
139 /* This is the parent process. */
141 signal(SIGINT
, SIG_IGN
);
142 signal(SIGQUIT
, SIG_IGN
);
143 signal(SIGTERM
, killed
);
145 if (waitpid(child
, &status
, 0) == -1)
146 err(EX_OSERR
, "waitpid failed");
148 return WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
152 * Try to acquire a lock on the given file, but don't wait for it. Returns
153 * an open file descriptor on success, or -1 on failure.
156 acquire_lock(const char *name
)
160 if ((fd
= open(name
, O_RDONLY
|O_CREAT
|O_EXLOCK
|O_NONBLOCK
, 0666)) == -1) {
161 if (errno
== EAGAIN
|| errno
== EINTR
)
163 err(EX_CANTCREAT
, "cannot open %s", name
);
169 * Remove the lock file.
175 flock(lockfd
, LOCK_UN
);
181 * Signal handler for SIGTERM. Cleans up the lock file, then re-raises
188 signal(sig
, SIG_DFL
);
189 if (kill(getpid(), sig
) == -1)
190 err(EX_OSERR
, "kill failed");
194 * Signal handler for SIGALRM.
197 timeout(__unused
int sig
)
206 "usage: lockf [-ks] [-t seconds] file command [arguments]\n");
211 * Wait until it might be possible to acquire a lock on the given file.
214 wait_for_lock(const char *name
)
218 if ((fd
= open(name
, O_RDONLY
|O_EXLOCK
)) == -1) {
219 if (errno
== ENOENT
|| errno
== EINTR
)
221 err(EX_CANTCREAT
, "cannot open %s", name
);