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