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