2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 __SCCSID("@(#)uucplock.c 8.1 (Berkeley) 6/6/93");
36 #include <sys/types.h>
50 #define LOCKTMP "LCKTMP..%d"
51 #define LOCKFMT "LCK..%s"
53 #define GORET(level, val) { err = errno; uuerr = (val); \
54 goto __CONCAT(ret, level); }
56 /* Forward declarations */
57 static int put_pid (int fd, pid_t pid);
58 static pid_t get_pid (int fd,int *err);
61 * uucp style locking routines
65 uu_lock(const char *tty_name)
69 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN],
70 lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
74 (void)snprintf(lcktmpname, sizeof(lcktmpname), _PATH_UUCPLOCK LOCKTMP,
76 (void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT,
78 if ((tmpfd = open(lcktmpname, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
80 GORET(0, UU_LOCK_CREAT_ERR);
82 for (i = 0; i < MAXTRIES; i++) {
83 if (link (lcktmpname, lckname) < 0) {
85 GORET(1, UU_LOCK_LINK_ERR);
87 * file is already locked
88 * check to see if the process holding the lock
91 if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) < 0)
92 GORET(1, UU_LOCK_OPEN_ERR);
94 if ((pid_old = get_pid (fd, &err)) == -1)
95 GORET(2, UU_LOCK_READ_ERR);
99 if (kill(pid_old, 0) == 0 || errno != ESRCH)
100 GORET(1, UU_LOCK_INUSE);
102 * The process that locked the file isn't running, so
103 * we'll lock it ourselves
105 (void)unlink(lckname);
107 if (!put_pid (tmpfd, pid))
108 GORET(3, UU_LOCK_WRITE_ERR);
112 GORET(1, (i >= MAXTRIES) ? UU_LOCK_TRY_ERR : UU_LOCK_OK);
115 (void)unlink(lckname);
121 (void)unlink(lcktmpname);
128 uu_lock_txfr(const char *tty_name, pid_t pid)
131 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
133 snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, tty_name);
135 if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) < 0)
136 return UU_LOCK_OWNER_ERR;
137 if (get_pid(fd, &err) != getpid())
138 err = UU_LOCK_OWNER_ERR;
140 lseek(fd, (off_t)0, SEEK_SET);
141 err = put_pid(fd, pid) ? 0 : UU_LOCK_WRITE_ERR;
149 uu_unlock(const char *tty_name)
151 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
153 (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, tty_name);
158 uu_lockerr(int uu_lockresult)
160 static char errbuf[128];
163 switch (uu_lockresult) {
165 return "device in use";
168 case UU_LOCK_OPEN_ERR:
169 fmt = "open error: %s";
171 case UU_LOCK_READ_ERR:
172 fmt = "read error: %s";
174 case UU_LOCK_CREAT_ERR:
175 fmt = "creat error: %s";
177 case UU_LOCK_WRITE_ERR:
178 fmt = "write error: %s";
180 case UU_LOCK_LINK_ERR:
181 fmt = "link error: %s";
183 case UU_LOCK_TRY_ERR:
184 fmt = "too many tries: %s";
186 case UU_LOCK_OWNER_ERR:
187 fmt = "not locking process: %s";
190 fmt = "undefined error: %s";
194 (void)snprintf(errbuf, sizeof(errbuf), fmt, strerror(errno));
199 put_pid(int fd, pid_t pid)
204 len = sprintf (buf, "%10d\n", (int)pid);
205 return write (fd, buf, (size_t)len) == len;
209 get_pid(int fd, int *err)
215 bytes_read = read (fd, buf, sizeof (buf) - 1);
216 if (bytes_read > 0) {
217 buf[bytes_read] = '\0';
218 pid = (pid_t)strtol (buf, (char **) NULL, 10);
221 *err = bytes_read ? errno : EINVAL;
226 /* end of uucplock.c */