xref: /freebsd/lib/libutil/uucplock.c (revision 52f72944b8f5abb2386eae924357dee8aea17d5b)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #ifndef lint
36 static const char sccsid[] = "@(#)uucplock.c	8.1 (Berkeley) 6/6/93";
37 #endif /* not lint */
38 
39 #include <sys/types.h>
40 #include <sys/file.h>
41 #include <dirent.h>
42 #include <errno.h>
43 #include <paths.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include "libutil.h"
50 
51 #define MAXTRIES 5
52 
53 #define LOCKTMP "LCKTMP..%d"
54 #define LOCKFMT "LCK..%s"
55 
56 #define GORET(level, val) { err = errno; uuerr = (val); \
57 			    goto __CONCAT(ret, level); }
58 
59 /* Forward declarations */
60 static int put_pid (int fd, pid_t pid);
61 static pid_t get_pid (int fd,int *err);
62 
63 /*
64  * uucp style locking routines
65  */
66 
67 int
68 uu_lock(const char *tty_name)
69 {
70 	int fd, tmpfd, i;
71 	pid_t pid, pid_old;
72 	char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN],
73 	     lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
74 	int err, uuerr;
75 
76 	pid = getpid();
77 	(void)snprintf(lcktmpname, sizeof(lcktmpname), _PATH_UUCPLOCK LOCKTMP,
78 			pid);
79 	(void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT,
80 			tty_name);
81 	if ((tmpfd = open(lcktmpname, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
82 	    0664)) < 0)
83 		GORET(0, UU_LOCK_CREAT_ERR);
84 
85 	for (i = 0; i < MAXTRIES; i++) {
86 		if (link (lcktmpname, lckname) < 0) {
87 			if (errno != EEXIST)
88 				GORET(1, UU_LOCK_LINK_ERR);
89 			/*
90 			 * file is already locked
91 			 * check to see if the process holding the lock
92 			 * still exists
93 			 */
94 			if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) < 0)
95 				GORET(1, UU_LOCK_OPEN_ERR);
96 
97 			if ((pid_old = get_pid (fd, &err)) == -1)
98 				GORET(2, UU_LOCK_READ_ERR);
99 
100 			close(fd);
101 
102 			if (kill(pid_old, 0) == 0 || errno != ESRCH)
103 				GORET(1, UU_LOCK_INUSE);
104 			/*
105 			 * The process that locked the file isn't running, so
106 			 * we'll lock it ourselves
107 			 */
108 			(void)unlink(lckname);
109 		} else {
110 			if (!put_pid (tmpfd, pid))
111 				GORET(3, UU_LOCK_WRITE_ERR);
112 			break;
113 		}
114 	}
115 	GORET(1, (i >= MAXTRIES) ? UU_LOCK_TRY_ERR : UU_LOCK_OK);
116 
117 ret3:
118 	(void)unlink(lckname);
119 	goto ret1;
120 ret2:
121 	(void)close(fd);
122 ret1:
123 	(void)close(tmpfd);
124 	(void)unlink(lcktmpname);
125 ret0:
126 	errno = err;
127 	return uuerr;
128 }
129 
130 int
131 uu_lock_txfr(const char *tty_name, pid_t pid)
132 {
133 	int fd, err;
134 	char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
135 
136 	snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, tty_name);
137 
138 	if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) < 0)
139 		return UU_LOCK_OWNER_ERR;
140 	if (get_pid(fd, &err) != getpid())
141 		err = UU_LOCK_OWNER_ERR;
142 	else {
143         	lseek(fd, (off_t)0, SEEK_SET);
144 		err = put_pid(fd, pid) ? 0 : UU_LOCK_WRITE_ERR;
145 	}
146 	close(fd);
147 
148 	return err;
149 }
150 
151 int
152 uu_unlock(const char *tty_name)
153 {
154 	char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
155 
156 	(void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, tty_name);
157 	return unlink(tbuf);
158 }
159 
160 const char *
161 uu_lockerr(int uu_lockresult)
162 {
163 	static char errbuf[128];
164 	const char *fmt;
165 
166 	switch (uu_lockresult) {
167 		case UU_LOCK_INUSE:
168 			return "device in use";
169 		case UU_LOCK_OK:
170 			return "";
171 		case UU_LOCK_OPEN_ERR:
172 			fmt = "open error: %s";
173 			break;
174 		case UU_LOCK_READ_ERR:
175 			fmt = "read error: %s";
176 			break;
177 		case UU_LOCK_CREAT_ERR:
178 			fmt = "creat error: %s";
179 			break;
180 		case UU_LOCK_WRITE_ERR:
181 			fmt = "write error: %s";
182 			break;
183 		case UU_LOCK_LINK_ERR:
184 			fmt = "link error: %s";
185 			break;
186 		case UU_LOCK_TRY_ERR:
187 			fmt = "too many tries: %s";
188 			break;
189 		case UU_LOCK_OWNER_ERR:
190 			fmt = "not locking process: %s";
191 			break;
192 		default:
193 			fmt = "undefined error: %s";
194 			break;
195 	}
196 
197 	(void)snprintf(errbuf, sizeof(errbuf), fmt, strerror(errno));
198 	return errbuf;
199 }
200 
201 static int
202 put_pid(int fd, pid_t pid)
203 {
204 	char buf[32];
205 	int len;
206 
207 	len = sprintf (buf, "%10d\n", (int)pid);
208 	return write (fd, buf, (size_t)len) == len;
209 }
210 
211 static pid_t
212 get_pid(int fd, int *err)
213 {
214 	int bytes_read;
215 	char buf[32];
216 	pid_t pid;
217 
218 	bytes_read = read (fd, buf, sizeof (buf) - 1);
219 	if (bytes_read > 0) {
220 		buf[bytes_read] = '\0';
221 		pid = (pid_t)strtol (buf, (char **) NULL, 10);
222 	} else {
223 		pid = -1;
224 		*err = bytes_read ? errno : EINVAL;
225 	}
226 	return pid;
227 }
228 
229 /* end of uucplock.c */
230