xref: /freebsd/usr.bin/lockf/lockf.c (revision 45edbdccd79e1844d08e6edf18ba37920e84179d)
1c8929a49SJohn Polstra /*
2c8929a49SJohn Polstra  * Copyright (C) 1997 John D. Polstra.  All rights reserved.
3c8929a49SJohn Polstra  *
4c8929a49SJohn Polstra  * Redistribution and use in source and binary forms, with or without
5c8929a49SJohn Polstra  * modification, are permitted provided that the following conditions
6c8929a49SJohn Polstra  * are met:
7c8929a49SJohn Polstra  * 1. Redistributions of source code must retain the above copyright
8c8929a49SJohn Polstra  *    notice, this list of conditions and the following disclaimer.
9c8929a49SJohn Polstra  * 2. Redistributions in binary form must reproduce the above copyright
10c8929a49SJohn Polstra  *    notice, this list of conditions and the following disclaimer in the
11c8929a49SJohn Polstra  *    documentation and/or other materials provided with the distribution.
12c8929a49SJohn Polstra  *
13c8929a49SJohn Polstra  * THIS SOFTWARE IS PROVIDED BY JOHN D. POLSTRA AND CONTRIBUTORS ``AS IS'' AND
14c8929a49SJohn Polstra  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15c8929a49SJohn Polstra  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16c8929a49SJohn Polstra  * ARE DISCLAIMED.  IN NO EVENT SHALL JOHN D. POLSTRA OR CONTRIBUTORS BE LIABLE
17c8929a49SJohn Polstra  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18c8929a49SJohn Polstra  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19c8929a49SJohn Polstra  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20c8929a49SJohn Polstra  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21c8929a49SJohn Polstra  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22c8929a49SJohn Polstra  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23c8929a49SJohn Polstra  * SUCH DAMAGE.
24c8929a49SJohn Polstra  */
25c8929a49SJohn Polstra 
26e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
27e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
28e026a48cSDavid E. O'Brien 
29c8929a49SJohn Polstra #include <sys/types.h>
30c8929a49SJohn Polstra #include <sys/wait.h>
31c8929a49SJohn Polstra 
32c8929a49SJohn Polstra #include <err.h>
33c8929a49SJohn Polstra #include <errno.h>
34c8929a49SJohn Polstra #include <fcntl.h>
35c8929a49SJohn Polstra #include <signal.h>
36c8929a49SJohn Polstra #include <stdio.h>
37c8929a49SJohn Polstra #include <stdlib.h>
38ae06cb4dSJohn Polstra #include <sysexits.h>
39c8929a49SJohn Polstra #include <unistd.h>
40c8929a49SJohn Polstra 
41c8929a49SJohn Polstra static void cleanup(void);
42c8929a49SJohn Polstra static void killed(int sig);
43c8929a49SJohn Polstra static void timeout(int sig);
44c8929a49SJohn Polstra static void usage(void);
454ccbe0c5SChristian S.J. Peron static int wait_for_lock(const char *name, int flags);
46c8929a49SJohn Polstra 
47c8929a49SJohn Polstra static const char *lockname;
48031469ebSChristian S.J. Peron static int lockfd = -1;
492f278eacSJohn Polstra static int keep;
50c8929a49SJohn Polstra static volatile sig_atomic_t timed_out;
51c8929a49SJohn Polstra 
52c8929a49SJohn Polstra /*
53c8929a49SJohn Polstra  * Execute an arbitrary command while holding a file lock.
54c8929a49SJohn Polstra  */
55c8929a49SJohn Polstra int
56c8929a49SJohn Polstra main(int argc, char **argv)
57c8929a49SJohn Polstra {
5845edbdccSChristian S.J. Peron 	int ch, silent, status, waitsec;
59c8929a49SJohn Polstra 	pid_t child;
60c8929a49SJohn Polstra 
6145edbdccSChristian S.J. Peron 	silent = keep = 0;
62c8929a49SJohn Polstra 	waitsec = -1;	/* Infinite. */
632f278eacSJohn Polstra 	while ((ch = getopt(argc, argv, "skt:")) != -1) {
64c8929a49SJohn Polstra 		switch (ch) {
652f278eacSJohn Polstra 		case 'k':
662f278eacSJohn Polstra 			keep = 1;
672f278eacSJohn Polstra 			break;
68c8929a49SJohn Polstra 		case 's':
69c8929a49SJohn Polstra 			silent = 1;
70c8929a49SJohn Polstra 			break;
71c8929a49SJohn Polstra 		case 't':
72c8929a49SJohn Polstra 		{
73c8929a49SJohn Polstra 			char *endptr;
74c8929a49SJohn Polstra 			waitsec = strtol(optarg, &endptr, 0);
75c8929a49SJohn Polstra 			if (*optarg == '\0' || *endptr != '\0' || waitsec < 0)
7645edbdccSChristian S.J. Peron 				errx(EX_USAGE,
7745edbdccSChristian S.J. Peron 				    "invalid timeout \"%s\"", optarg);
78c8929a49SJohn Polstra 		}
79c8929a49SJohn Polstra 			break;
80c8929a49SJohn Polstra 		default:
81c8929a49SJohn Polstra 			usage();
82c8929a49SJohn Polstra 		}
83c8929a49SJohn Polstra 	}
84c8929a49SJohn Polstra 	if (argc - optind < 2)
85c8929a49SJohn Polstra 		usage();
86c8929a49SJohn Polstra 	lockname = argv[optind++];
87c8929a49SJohn Polstra 	argc -= optind;
88c8929a49SJohn Polstra 	argv += optind;
89c8929a49SJohn Polstra 	if (waitsec > 0) {		/* Set up a timeout. */
90c8929a49SJohn Polstra 		struct sigaction act;
91c8929a49SJohn Polstra 
92c8929a49SJohn Polstra 		act.sa_handler = timeout;
93c8929a49SJohn Polstra 		sigemptyset(&act.sa_mask);
94c8929a49SJohn Polstra 		act.sa_flags = 0;	/* Note that we do not set SA_RESTART. */
95c8929a49SJohn Polstra 		sigaction(SIGALRM, &act, NULL);
96c8929a49SJohn Polstra 		alarm(waitsec);
97c8929a49SJohn Polstra 	}
984ccbe0c5SChristian S.J. Peron 	lockfd = wait_for_lock(lockname, O_NONBLOCK);
99031469ebSChristian S.J. Peron 	while (lockfd == -1 && !timed_out && waitsec != 0)
1004ccbe0c5SChristian S.J. Peron 		lockfd = wait_for_lock(lockname, 0);
101c8929a49SJohn Polstra 	if (waitsec > 0)
102c8929a49SJohn Polstra 		alarm(0);
103c8929a49SJohn Polstra 	if (lockfd == -1) {		/* We failed to acquire the lock. */
104c8929a49SJohn Polstra 		if (silent)
105ae06cb4dSJohn Polstra 			exit(EX_TEMPFAIL);
106ae06cb4dSJohn Polstra 		errx(EX_TEMPFAIL, "%s: already locked", lockname);
107c8929a49SJohn Polstra 	}
108c8929a49SJohn Polstra 	/* At this point, we own the lock. */
109c8929a49SJohn Polstra 	if (atexit(cleanup) == -1)
110ae06cb4dSJohn Polstra 		err(EX_OSERR, "atexit failed");
111c8929a49SJohn Polstra 	if ((child = fork()) == -1)
112ae06cb4dSJohn Polstra 		err(EX_OSERR, "cannot fork");
113c8929a49SJohn Polstra 	if (child == 0) {	/* The child process. */
114c8929a49SJohn Polstra 		close(lockfd);
115c8929a49SJohn Polstra 		execvp(argv[0], argv);
116208f2fd1STim J. Robbins 		warn("%s", argv[0]);
117c8929a49SJohn Polstra 		_exit(1);
118c8929a49SJohn Polstra 	}
119c8929a49SJohn Polstra 	/* This is the parent process. */
120c8929a49SJohn Polstra 	signal(SIGINT, SIG_IGN);
121c8929a49SJohn Polstra 	signal(SIGQUIT, SIG_IGN);
122c8929a49SJohn Polstra 	signal(SIGTERM, killed);
123c8929a49SJohn Polstra 	if (waitpid(child, &status, 0) == -1)
124ae06cb4dSJohn Polstra 		err(EX_OSERR, "waitpid failed");
12545edbdccSChristian S.J. Peron 	return (WIFEXITED(status) ? WEXITSTATUS(status) : 1);
126c8929a49SJohn Polstra }
127c8929a49SJohn Polstra 
128c8929a49SJohn Polstra /*
129c8929a49SJohn Polstra  * Remove the lock file.
130c8929a49SJohn Polstra  */
131c8929a49SJohn Polstra static void
132c8929a49SJohn Polstra cleanup(void)
133c8929a49SJohn Polstra {
1342f278eacSJohn Polstra     if (keep)
1352f278eacSJohn Polstra 	flock(lockfd, LOCK_UN);
1362f278eacSJohn Polstra     else
137ae06cb4dSJohn Polstra 	unlink(lockname);
138c8929a49SJohn Polstra }
139c8929a49SJohn Polstra 
140c8929a49SJohn Polstra /*
141c8929a49SJohn Polstra  * Signal handler for SIGTERM.  Cleans up the lock file, then re-raises
142c8929a49SJohn Polstra  * the signal.
143c8929a49SJohn Polstra  */
144c8929a49SJohn Polstra static void
145c8929a49SJohn Polstra killed(int sig)
146c8929a49SJohn Polstra {
147c8929a49SJohn Polstra     cleanup();
148c8929a49SJohn Polstra     signal(sig, SIG_DFL);
149c8929a49SJohn Polstra     if (kill(getpid(), sig) == -1)
150ae06cb4dSJohn Polstra 	err(EX_OSERR, "kill failed");
151c8929a49SJohn Polstra }
152c8929a49SJohn Polstra 
153c8929a49SJohn Polstra /*
154c8929a49SJohn Polstra  * Signal handler for SIGALRM.
155c8929a49SJohn Polstra  */
156c8929a49SJohn Polstra static void
1579ff5e898SDavid Malone timeout(int sig __unused)
158c8929a49SJohn Polstra {
159c8929a49SJohn Polstra     timed_out = 1;
160c8929a49SJohn Polstra }
161c8929a49SJohn Polstra 
162c8929a49SJohn Polstra static void
163c8929a49SJohn Polstra usage(void)
164c8929a49SJohn Polstra {
1653ec665d4SPhilippe Charnier     fprintf(stderr,
1662f278eacSJohn Polstra       "usage: lockf [-ks] [-t seconds] file command [arguments]\n");
1673ec665d4SPhilippe Charnier     exit(EX_USAGE);
168c8929a49SJohn Polstra }
169c8929a49SJohn Polstra 
170c8929a49SJohn Polstra /*
171c8929a49SJohn Polstra  * Wait until it might be possible to acquire a lock on the given file.
172c8929a49SJohn Polstra  */
173031469ebSChristian S.J. Peron static int
1744ccbe0c5SChristian S.J. Peron wait_for_lock(const char *name, int flags)
175c8929a49SJohn Polstra {
176c8929a49SJohn Polstra     int fd;
177c8929a49SJohn Polstra 
1784ccbe0c5SChristian S.J. Peron     if ((fd = open(name, O_CREAT|O_RDONLY|O_EXLOCK|flags, 0666)) == -1) {
17945edbdccSChristian S.J. Peron 	if (errno == EINTR || errno == EAGAIN)
180031469ebSChristian S.J. Peron 	    return (-1);
181ae06cb4dSJohn Polstra 	err(EX_CANTCREAT, "cannot open %s", name);
182c8929a49SJohn Polstra     }
183031469ebSChristian S.J. Peron     return (fd);
184c8929a49SJohn Polstra }
185