1*515e489cSderaadt /* $OpenBSD: uucplock.c,v 1.21 2019/07/03 03:24:04 deraadt Exp $ */
2c280f579Sbri /*
3c280f579Sbri * Copyright (c) 1988, 1993
4c280f579Sbri * The Regents of the University of California. All rights reserved.
5c280f579Sbri *
6c280f579Sbri * Redistribution and use in source and binary forms, with or without
7c280f579Sbri * modification, are permitted provided that the following conditions
8c280f579Sbri * are met:
9c280f579Sbri * 1. Redistributions of source code must retain the above copyright
10c280f579Sbri * notice, this list of conditions and the following disclaimer.
11c280f579Sbri * 2. Redistributions in binary form must reproduce the above copyright
12c280f579Sbri * notice, this list of conditions and the following disclaimer in the
13c280f579Sbri * documentation and/or other materials provided with the distribution.
146580fee3Smillert * 3. Neither the name of the University nor the names of its contributors
15c280f579Sbri * may be used to endorse or promote products derived from this software
16c280f579Sbri * without specific prior written permission.
17c280f579Sbri *
18c280f579Sbri * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19c280f579Sbri * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20c280f579Sbri * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21c280f579Sbri * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22c280f579Sbri * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23c280f579Sbri * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24c280f579Sbri * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25c280f579Sbri * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26c280f579Sbri * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27c280f579Sbri * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28c280f579Sbri * SUCH DAMAGE.
29c280f579Sbri *
30c280f579Sbri *
31c280f579Sbri */
32c280f579Sbri
33c280f579Sbri #include <sys/types.h>
34c280f579Sbri #include <dirent.h>
35c280f579Sbri #include <errno.h>
3601a83688Smillert #include <fcntl.h>
37c280f579Sbri #include <unistd.h>
38c280f579Sbri #include <signal.h>
39c280f579Sbri #include <stdio.h>
40c280f579Sbri #include <stdlib.h>
41c280f579Sbri #include <paths.h>
42c280f579Sbri #include <string.h>
43c280f579Sbri #include "util.h"
44c280f579Sbri
45c280f579Sbri #define MAXTRIES 5
46c280f579Sbri
4713ee8a54Sderaadt #define LOCKTMP "LCKTMP..%ld"
48c280f579Sbri #define LOCKFMT "LCK..%s"
49c280f579Sbri
50c280f579Sbri #define GORET(level, val) { err = errno; uuerr = (val); \
51c280f579Sbri goto __CONCAT(ret, level); }
52c280f579Sbri
53c280f579Sbri /* Forward declarations */
54c72b5b24Smillert static int put_pid(int fd, pid_t pid);
55c72b5b24Smillert static pid_t get_pid(int fd,int *err);
56c280f579Sbri
57c280f579Sbri /*
58c280f579Sbri * uucp style locking routines
59c280f579Sbri */
60c280f579Sbri int
uu_lock(const char * ttyname)611477552aSderaadt uu_lock(const char *ttyname)
62c280f579Sbri {
63c280f579Sbri char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN],
64c280f579Sbri lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
651477552aSderaadt int fd, tmpfd, i, err, uuerr;
661477552aSderaadt pid_t pid, pid_old;
67c280f579Sbri
68c280f579Sbri pid = getpid();
69c280f579Sbri (void)snprintf(lcktmpname, sizeof(lcktmpname), _PATH_UUCPLOCK LOCKTMP,
7013ee8a54Sderaadt (long)pid);
71c280f579Sbri (void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT,
72c280f579Sbri ttyname);
73478ad52bSguenther tmpfd = open(lcktmpname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664);
74df69c215Sderaadt if (tmpfd == -1)
75c280f579Sbri GORET(0, UU_LOCK_CREAT_ERR);
76c280f579Sbri
77c280f579Sbri for (i = 0; i < MAXTRIES; i++) {
78df69c215Sderaadt if (link(lcktmpname, lckname) == -1) {
79c280f579Sbri if (errno != EEXIST)
80c280f579Sbri GORET(1, UU_LOCK_LINK_ERR);
81c280f579Sbri /*
82c280f579Sbri * file is already locked
83c280f579Sbri * check to see if the process holding the lock
84c280f579Sbri * still exists
85c280f579Sbri */
86df69c215Sderaadt if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) == -1)
87c280f579Sbri GORET(1, UU_LOCK_OPEN_ERR);
88c280f579Sbri
89cd1720ecSbrian if ((pid_old = get_pid(fd, &err)) == -1)
90c280f579Sbri GORET(2, UU_LOCK_READ_ERR);
91c280f579Sbri
92c280f579Sbri close(fd);
93c280f579Sbri
94cd1720ecSbrian if (kill(pid_old, 0) == 0 || errno != ESRCH)
95c280f579Sbri GORET(1, UU_LOCK_INUSE);
96c280f579Sbri /*
97c280f579Sbri * The process that locked the file isn't running, so
98c280f579Sbri * we'll lock it ourselves
99c280f579Sbri */
100c280f579Sbri (void)unlink(lckname);
101c280f579Sbri } else {
102c280f579Sbri if (!put_pid(tmpfd, pid))
103c280f579Sbri GORET(3, UU_LOCK_WRITE_ERR);
104c280f579Sbri break;
105c280f579Sbri }
106c280f579Sbri }
107c280f579Sbri GORET(1, (i >= MAXTRIES) ? UU_LOCK_TRY_ERR : UU_LOCK_OK);
108c280f579Sbri
109c280f579Sbri ret3:
110c280f579Sbri (void)unlink(lckname);
111c280f579Sbri goto ret1;
112c280f579Sbri ret2:
113c280f579Sbri (void)close(fd);
114c280f579Sbri ret1:
115c280f579Sbri (void)close(tmpfd);
116c280f579Sbri (void)unlink(lcktmpname);
117c280f579Sbri ret0:
118c280f579Sbri errno = err;
119c280f579Sbri return uuerr;
120c280f579Sbri }
121c280f579Sbri
122c280f579Sbri int
uu_lock_txfr(const char * ttyname,pid_t pid)1231477552aSderaadt uu_lock_txfr(const char *ttyname, pid_t pid)
124709dd3e4Sbrian {
125709dd3e4Sbrian char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
1268b0f1b7aScloder int fd, err, ret;
127709dd3e4Sbrian
128709dd3e4Sbrian snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname);
129709dd3e4Sbrian
130df69c215Sderaadt if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) == -1)
131709dd3e4Sbrian return UU_LOCK_OWNER_ERR;
132709dd3e4Sbrian if (get_pid(fd, &err) != getpid())
1338b0f1b7aScloder ret = UU_LOCK_OWNER_ERR;
1348b0f1b7aScloder else {
135709dd3e4Sbrian lseek(fd, 0, SEEK_SET);
1368b0f1b7aScloder ret = put_pid(fd, pid) ? UU_LOCK_OK : UU_LOCK_WRITE_ERR;
1378b0f1b7aScloder }
138709dd3e4Sbrian
1398b0f1b7aScloder close(fd);
1408b0f1b7aScloder return ret;
141709dd3e4Sbrian }
142709dd3e4Sbrian
143709dd3e4Sbrian int
uu_unlock(const char * ttyname)1441477552aSderaadt uu_unlock(const char *ttyname)
145c280f579Sbri {
146c280f579Sbri char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
147c280f579Sbri
148c280f579Sbri (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname);
149c280f579Sbri return unlink(tbuf);
150c280f579Sbri }
151c280f579Sbri
152c280f579Sbri const char *
uu_lockerr(int uu_lockresult)1531477552aSderaadt uu_lockerr(int uu_lockresult)
154c280f579Sbri {
155c280f579Sbri static char errbuf[128];
156da975fa8Sguenther const char *err;
157c280f579Sbri
158c280f579Sbri switch (uu_lockresult) {
159c280f579Sbri case UU_LOCK_INUSE:
160c280f579Sbri return "device in use";
161c280f579Sbri case UU_LOCK_OK:
162c280f579Sbri return "";
163c280f579Sbri case UU_LOCK_OPEN_ERR:
164da975fa8Sguenther err = "open error";
165c280f579Sbri break;
166c280f579Sbri case UU_LOCK_READ_ERR:
167da975fa8Sguenther err = "read error";
168c280f579Sbri break;
169c280f579Sbri case UU_LOCK_CREAT_ERR:
170da975fa8Sguenther err = "creat error";
171c280f579Sbri break;
172c280f579Sbri case UU_LOCK_WRITE_ERR:
173da975fa8Sguenther err = "write error";
174c280f579Sbri break;
175c280f579Sbri case UU_LOCK_LINK_ERR:
176da975fa8Sguenther err = "link error";
177c280f579Sbri break;
178c280f579Sbri case UU_LOCK_TRY_ERR:
179da975fa8Sguenther err = "too many tries";
180c280f579Sbri break;
181709dd3e4Sbrian case UU_LOCK_OWNER_ERR:
182da975fa8Sguenther err = "not locking process";
183709dd3e4Sbrian break;
184c280f579Sbri default:
185da975fa8Sguenther err = "undefined error";
186c280f579Sbri break;
187c280f579Sbri }
188c280f579Sbri
189da975fa8Sguenther (void)snprintf(errbuf, sizeof(errbuf), "%s: %s", err, strerror(errno));
190c280f579Sbri return errbuf;
191c280f579Sbri }
192c280f579Sbri
193c280f579Sbri static int
put_pid(int fd,pid_t pid)1941477552aSderaadt put_pid(int fd, pid_t pid)
195c280f579Sbri {
196c280f579Sbri char buf[32];
197c280f579Sbri int len;
198c280f579Sbri
19913ee8a54Sderaadt len = snprintf(buf, sizeof buf, "%10ld\n", (long)pid);
200*515e489cSderaadt if (len < 0 || len >= sizeof buf)
201*515e489cSderaadt return 0;
202ef993c80Sbri
203*515e489cSderaadt if (write(fd, buf, len) != len)
204*515e489cSderaadt return 0;
205*515e489cSderaadt
206ef993c80Sbri /* We don't mind too much if ftruncate() fails - see get_pid */
207b0d2416bSderaadt ftruncate(fd, (off_t)len);
208ef993c80Sbri return 1;
209ef993c80Sbri }
210c280f579Sbri
211c280f579Sbri static pid_t
get_pid(int fd,int * err)2121477552aSderaadt get_pid(int fd, int *err)
213c280f579Sbri {
214d7e1ad0bSderaadt ssize_t bytes_read;
215c280f579Sbri char buf[32];
216c280f579Sbri pid_t pid;
217c280f579Sbri
218c280f579Sbri bytes_read = read(fd, buf, sizeof (buf) - 1);
219c280f579Sbri if (bytes_read > 0) {
220c280f579Sbri buf[bytes_read] = '\0';
221d7e1ad0bSderaadt pid = (pid_t)strtoul(buf, (char **) NULL, 10);
222c280f579Sbri } else {
223c280f579Sbri pid = -1;
224c280f579Sbri *err = bytes_read ? errno : EINVAL;
225c280f579Sbri }
226c280f579Sbri return pid;
227c280f579Sbri }
228