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