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