1 /* $OpenBSD: uucplock.c,v 1.21 2019/07/03 03:24:04 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
uu_lock(const char * ttyname)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 tmpfd = open(lcktmpname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664);
74 if (tmpfd == -1)
75 GORET(0, UU_LOCK_CREAT_ERR);
76
77 for (i = 0; i < MAXTRIES; i++) {
78 if (link(lcktmpname, lckname) == -1) {
79 if (errno != EEXIST)
80 GORET(1, UU_LOCK_LINK_ERR);
81 /*
82 * file is already locked
83 * check to see if the process holding the lock
84 * still exists
85 */
86 if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) == -1)
87 GORET(1, UU_LOCK_OPEN_ERR);
88
89 if ((pid_old = get_pid(fd, &err)) == -1)
90 GORET(2, UU_LOCK_READ_ERR);
91
92 close(fd);
93
94 if (kill(pid_old, 0) == 0 || errno != ESRCH)
95 GORET(1, UU_LOCK_INUSE);
96 /*
97 * The process that locked the file isn't running, so
98 * we'll lock it ourselves
99 */
100 (void)unlink(lckname);
101 } else {
102 if (!put_pid(tmpfd, pid))
103 GORET(3, UU_LOCK_WRITE_ERR);
104 break;
105 }
106 }
107 GORET(1, (i >= MAXTRIES) ? UU_LOCK_TRY_ERR : UU_LOCK_OK);
108
109 ret3:
110 (void)unlink(lckname);
111 goto ret1;
112 ret2:
113 (void)close(fd);
114 ret1:
115 (void)close(tmpfd);
116 (void)unlink(lcktmpname);
117 ret0:
118 errno = err;
119 return uuerr;
120 }
121
122 int
uu_lock_txfr(const char * ttyname,pid_t pid)123 uu_lock_txfr(const char *ttyname, pid_t pid)
124 {
125 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
126 int fd, err, ret;
127
128 snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname);
129
130 if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) == -1)
131 return UU_LOCK_OWNER_ERR;
132 if (get_pid(fd, &err) != getpid())
133 ret = UU_LOCK_OWNER_ERR;
134 else {
135 lseek(fd, 0, SEEK_SET);
136 ret = put_pid(fd, pid) ? UU_LOCK_OK : UU_LOCK_WRITE_ERR;
137 }
138
139 close(fd);
140 return ret;
141 }
142
143 int
uu_unlock(const char * ttyname)144 uu_unlock(const char *ttyname)
145 {
146 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
147
148 (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname);
149 return unlink(tbuf);
150 }
151
152 const char *
uu_lockerr(int uu_lockresult)153 uu_lockerr(int uu_lockresult)
154 {
155 static char errbuf[128];
156 const char *err;
157
158 switch (uu_lockresult) {
159 case UU_LOCK_INUSE:
160 return "device in use";
161 case UU_LOCK_OK:
162 return "";
163 case UU_LOCK_OPEN_ERR:
164 err = "open error";
165 break;
166 case UU_LOCK_READ_ERR:
167 err = "read error";
168 break;
169 case UU_LOCK_CREAT_ERR:
170 err = "creat error";
171 break;
172 case UU_LOCK_WRITE_ERR:
173 err = "write error";
174 break;
175 case UU_LOCK_LINK_ERR:
176 err = "link error";
177 break;
178 case UU_LOCK_TRY_ERR:
179 err = "too many tries";
180 break;
181 case UU_LOCK_OWNER_ERR:
182 err = "not locking process";
183 break;
184 default:
185 err = "undefined error";
186 break;
187 }
188
189 (void)snprintf(errbuf, sizeof(errbuf), "%s: %s", err, strerror(errno));
190 return errbuf;
191 }
192
193 static int
put_pid(int fd,pid_t pid)194 put_pid(int fd, pid_t pid)
195 {
196 char buf[32];
197 int len;
198
199 len = snprintf(buf, sizeof buf, "%10ld\n", (long)pid);
200 if (len < 0 || len >= sizeof buf)
201 return 0;
202
203 if (write(fd, buf, len) != len)
204 return 0;
205
206 /* We don't mind too much if ftruncate() fails - see get_pid */
207 ftruncate(fd, (off_t)len);
208 return 1;
209 }
210
211 static pid_t
get_pid(int fd,int * err)212 get_pid(int fd, int *err)
213 {
214 ssize_t bytes_read;
215 char buf[32];
216 pid_t pid;
217
218 bytes_read = read(fd, buf, sizeof (buf) - 1);
219 if (bytes_read > 0) {
220 buf[bytes_read] = '\0';
221 pid = (pid_t)strtoul(buf, (char **) NULL, 10);
222 } else {
223 pid = -1;
224 *err = bytes_read ? errno : EINVAL;
225 }
226 return pid;
227 }
228