xref: /freebsd/lib/libutil/uucplock.c (revision 3cbb4cc200f8a0ad7ed08233425ea54524a21f1c)
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 __SCCSID("@(#)uucplock.c	8.1 (Berkeley) 6/6/93");
35 
36 #include <sys/types.h>
37 #include <sys/file.h>
38 #include <dirent.h>
39 #include <errno.h>
40 #include <paths.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include "libutil.h"
47 
48 #define MAXTRIES 5
49 
50 #define LOCKTMP "LCKTMP..%d"
51 #define LOCKFMT "LCK..%s"
52 
53 #define GORET(level, val) { err = errno; uuerr = (val); \
54 			    goto __CONCAT(ret, level); }
55 
56 /* Forward declarations */
57 static int put_pid (int fd, pid_t pid);
58 static pid_t get_pid (int fd,int *err);
59 
60 /*
61  * uucp style locking routines
62  */
63 
64 int
65 uu_lock(const char *tty_name)
66 {
67 	int fd, tmpfd, i;
68 	pid_t pid, pid_old;
69 	char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN],
70 	     lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
71 	int err, uuerr;
72 
73 	pid = getpid();
74 	(void)snprintf(lcktmpname, sizeof(lcktmpname), _PATH_UUCPLOCK LOCKTMP,
75 			pid);
76 	(void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT,
77 			tty_name);
78 	if ((tmpfd = open(lcktmpname, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
79 	    0664)) < 0)
80 		GORET(0, UU_LOCK_CREAT_ERR);
81 
82 	for (i = 0; i < MAXTRIES; i++) {
83 		if (link (lcktmpname, lckname) < 0) {
84 			if (errno != EEXIST)
85 				GORET(1, UU_LOCK_LINK_ERR);
86 			/*
87 			 * file is already locked
88 			 * check to see if the process holding the lock
89 			 * still exists
90 			 */
91 			if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) < 0)
92 				GORET(1, UU_LOCK_OPEN_ERR);
93 
94 			if ((pid_old = get_pid (fd, &err)) == -1)
95 				GORET(2, UU_LOCK_READ_ERR);
96 
97 			close(fd);
98 
99 			if (kill(pid_old, 0) == 0 || errno != ESRCH)
100 				GORET(1, UU_LOCK_INUSE);
101 			/*
102 			 * The process that locked the file isn't running, so
103 			 * we'll lock it ourselves
104 			 */
105 			(void)unlink(lckname);
106 		} else {
107 			if (!put_pid (tmpfd, pid))
108 				GORET(3, UU_LOCK_WRITE_ERR);
109 			break;
110 		}
111 	}
112 	GORET(1, (i >= MAXTRIES) ? UU_LOCK_TRY_ERR : UU_LOCK_OK);
113 
114 ret3:
115 	(void)unlink(lckname);
116 	goto ret1;
117 ret2:
118 	(void)close(fd);
119 ret1:
120 	(void)close(tmpfd);
121 	(void)unlink(lcktmpname);
122 ret0:
123 	errno = err;
124 	return uuerr;
125 }
126 
127 int
128 uu_lock_txfr(const char *tty_name, pid_t pid)
129 {
130 	int fd, err;
131 	char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
132 
133 	snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, tty_name);
134 
135 	if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) < 0)
136 		return UU_LOCK_OWNER_ERR;
137 	if (get_pid(fd, &err) != getpid())
138 		err = UU_LOCK_OWNER_ERR;
139 	else {
140         	lseek(fd, (off_t)0, SEEK_SET);
141 		err = put_pid(fd, pid) ? 0 : UU_LOCK_WRITE_ERR;
142 	}
143 	close(fd);
144 
145 	return err;
146 }
147 
148 int
149 uu_unlock(const char *tty_name)
150 {
151 	char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
152 
153 	(void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, tty_name);
154 	return unlink(tbuf);
155 }
156 
157 const char *
158 uu_lockerr(int uu_lockresult)
159 {
160 	static char errbuf[128];
161 	const char *fmt;
162 
163 	switch (uu_lockresult) {
164 		case UU_LOCK_INUSE:
165 			return "device in use";
166 		case UU_LOCK_OK:
167 			return "";
168 		case UU_LOCK_OPEN_ERR:
169 			fmt = "open error: %s";
170 			break;
171 		case UU_LOCK_READ_ERR:
172 			fmt = "read error: %s";
173 			break;
174 		case UU_LOCK_CREAT_ERR:
175 			fmt = "creat error: %s";
176 			break;
177 		case UU_LOCK_WRITE_ERR:
178 			fmt = "write error: %s";
179 			break;
180 		case UU_LOCK_LINK_ERR:
181 			fmt = "link error: %s";
182 			break;
183 		case UU_LOCK_TRY_ERR:
184 			fmt = "too many tries: %s";
185 			break;
186 		case UU_LOCK_OWNER_ERR:
187 			fmt = "not locking process: %s";
188 			break;
189 		default:
190 			fmt = "undefined error: %s";
191 			break;
192 	}
193 
194 	(void)snprintf(errbuf, sizeof(errbuf), fmt, strerror(errno));
195 	return errbuf;
196 }
197 
198 static int
199 put_pid(int fd, pid_t pid)
200 {
201 	char buf[32];
202 	int len;
203 
204 	len = sprintf (buf, "%10d\n", (int)pid);
205 	return write (fd, buf, (size_t)len) == len;
206 }
207 
208 static pid_t
209 get_pid(int fd, int *err)
210 {
211 	int bytes_read;
212 	char buf[32];
213 	pid_t pid;
214 
215 	bytes_read = read (fd, buf, sizeof (buf) - 1);
216 	if (bytes_read > 0) {
217 		buf[bytes_read] = '\0';
218 		pid = (pid_t)strtol (buf, (char **) NULL, 10);
219 	} else {
220 		pid = -1;
221 		*err = bytes_read ? errno : EINVAL;
222 	}
223 	return pid;
224 }
225 
226 /* end of uucplock.c */
227