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