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