1568b59b9SBrian Somers /* 2568b59b9SBrian Somers * Copyright (c) 1988, 1993 3568b59b9SBrian Somers * The Regents of the University of California. All rights reserved. 4568b59b9SBrian Somers * 5568b59b9SBrian Somers * Redistribution and use in source and binary forms, with or without 6568b59b9SBrian Somers * modification, are permitted provided that the following conditions 7568b59b9SBrian Somers * are met: 8568b59b9SBrian Somers * 1. Redistributions of source code must retain the above copyright 9568b59b9SBrian Somers * notice, this list of conditions and the following disclaimer. 10568b59b9SBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 11568b59b9SBrian Somers * notice, this list of conditions and the following disclaimer in the 12568b59b9SBrian Somers * documentation and/or other materials provided with the distribution. 13568b59b9SBrian Somers * 3. All advertising materials mentioning features or use of this software 14568b59b9SBrian Somers * must display the following acknowledgement: 15568b59b9SBrian Somers * This product includes software developed by the University of 16568b59b9SBrian Somers * California, Berkeley and its contributors. 17568b59b9SBrian Somers * 4. Neither the name of the University nor the names of its contributors 18568b59b9SBrian Somers * may be used to endorse or promote products derived from this software 19568b59b9SBrian Somers * without specific prior written permission. 20568b59b9SBrian Somers * 21568b59b9SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22568b59b9SBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23568b59b9SBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24568b59b9SBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25568b59b9SBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26568b59b9SBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27568b59b9SBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28568b59b9SBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29568b59b9SBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30568b59b9SBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31568b59b9SBrian Somers * SUCH DAMAGE. 32568b59b9SBrian Somers */ 33568b59b9SBrian Somers 34568b59b9SBrian Somers #ifndef lint 35568b59b9SBrian Somers static const char sccsid[] = "@(#)uucplock.c 8.1 (Berkeley) 6/6/93"; 36568b59b9SBrian Somers #endif /* not lint */ 37568b59b9SBrian Somers 38568b59b9SBrian Somers #include <sys/types.h> 39568b59b9SBrian Somers #include <sys/file.h> 40568b59b9SBrian Somers #include <dirent.h> 41568b59b9SBrian Somers #include <errno.h> 42568b59b9SBrian Somers #include <unistd.h> 43568b59b9SBrian Somers #include <signal.h> 44568b59b9SBrian Somers #include <stdio.h> 45568b59b9SBrian Somers #include <stdlib.h> 46568b59b9SBrian Somers #include <paths.h> 47687d0cdeSBrian Somers #include <string.h> 48687d0cdeSBrian Somers #include "libutil.h" 49568b59b9SBrian Somers 50568b59b9SBrian Somers #define LOCKFMT "LCK..%s" 51568b59b9SBrian Somers 52568b59b9SBrian Somers /* Forward declarations */ 53568b59b9SBrian Somers static int put_pid (int fd, pid_t pid); 54687d0cdeSBrian Somers static pid_t get_pid (int fd,int *err); 55568b59b9SBrian Somers 56568b59b9SBrian Somers /* 57568b59b9SBrian Somers * uucp style locking routines 58568b59b9SBrian Somers * return: 0 - success 59568b59b9SBrian Somers * -1 - failure 60568b59b9SBrian Somers */ 61568b59b9SBrian Somers 62568b59b9SBrian Somers int uu_lock (char *ttyname) 63568b59b9SBrian Somers { 64568b59b9SBrian Somers int fd; 65568b59b9SBrian Somers pid_t pid; 66568b59b9SBrian Somers char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 67687d0cdeSBrian Somers int err; 68568b59b9SBrian Somers 6928754192SAndrey A. Chernov (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname); 70568b59b9SBrian Somers fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660); 71568b59b9SBrian Somers if (fd < 0) { 72568b59b9SBrian Somers /* 73568b59b9SBrian Somers * file is already locked 74568b59b9SBrian Somers * check to see if the process holding the lock still exists 75568b59b9SBrian Somers */ 76568b59b9SBrian Somers fd = open(tbuf, O_RDWR, 0); 77687d0cdeSBrian Somers if (fd < 0) 78687d0cdeSBrian Somers return UU_LOCK_OPEN_ERR; 79687d0cdeSBrian Somers 80687d0cdeSBrian Somers if ((pid = get_pid (fd, &err)) == -1) { 81568b59b9SBrian Somers (void)close(fd); 82687d0cdeSBrian Somers errno = err; 83687d0cdeSBrian Somers return UU_LOCK_READ_ERR; 84568b59b9SBrian Somers } 85568b59b9SBrian Somers 86568b59b9SBrian Somers if (kill(pid, 0) == 0 || errno != ESRCH) { 87568b59b9SBrian Somers (void)close(fd); /* process is still running */ 88687d0cdeSBrian Somers return UU_LOCK_INUSE; 89568b59b9SBrian Somers } 90568b59b9SBrian Somers /* 91568b59b9SBrian Somers * The process that locked the file isn't running, so 92568b59b9SBrian Somers * we'll lock it ourselves 93568b59b9SBrian Somers */ 94568b59b9SBrian Somers if (lseek(fd, (off_t) 0, L_SET) < 0) { 95687d0cdeSBrian Somers err = errno; 96568b59b9SBrian Somers (void)close(fd); 97687d0cdeSBrian Somers errno = err; 98687d0cdeSBrian Somers return UU_LOCK_SEEK_ERR; 99568b59b9SBrian Somers } 100568b59b9SBrian Somers /* fall out and finish the locking process */ 101568b59b9SBrian Somers } 102568b59b9SBrian Somers pid = getpid(); 103568b59b9SBrian Somers if (!put_pid (fd, pid)) { 104687d0cdeSBrian Somers err = errno; 105568b59b9SBrian Somers (void)close(fd); 106568b59b9SBrian Somers (void)unlink(tbuf); 107687d0cdeSBrian Somers errno = err; 108687d0cdeSBrian Somers return UU_LOCK_WRITE_ERR; 109568b59b9SBrian Somers } 110568b59b9SBrian Somers (void)close(fd); 111687d0cdeSBrian Somers return UU_LOCK_OK; 112568b59b9SBrian Somers } 113568b59b9SBrian Somers 114568b59b9SBrian Somers int uu_unlock (char *ttyname) 115568b59b9SBrian Somers { 116568b59b9SBrian Somers char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 117568b59b9SBrian Somers 11828754192SAndrey A. Chernov (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname); 119687d0cdeSBrian Somers return unlink(tbuf); 120687d0cdeSBrian Somers } 121687d0cdeSBrian Somers 122687d0cdeSBrian Somers char *uu_lockerr (int uu_lockresult) 123687d0cdeSBrian Somers { 124687d0cdeSBrian Somers static char errbuf[512]; 12581d9597cSAndrey A. Chernov char *fmt; 126687d0cdeSBrian Somers 127687d0cdeSBrian Somers switch (uu_lockresult) { 128687d0cdeSBrian Somers case UU_LOCK_INUSE: 12928754192SAndrey A. Chernov return "device in use"; 130687d0cdeSBrian Somers case UU_LOCK_OK: 13128754192SAndrey A. Chernov return ""; 132687d0cdeSBrian Somers case UU_LOCK_OPEN_ERR: 13381d9597cSAndrey A. Chernov fmt = "open error: %s"; 134687d0cdeSBrian Somers break; 135687d0cdeSBrian Somers case UU_LOCK_READ_ERR: 13681d9597cSAndrey A. Chernov fmt = "read error: %s"; 137687d0cdeSBrian Somers break; 138687d0cdeSBrian Somers case UU_LOCK_SEEK_ERR: 13981d9597cSAndrey A. Chernov fmt = "seek error: %s"; 140687d0cdeSBrian Somers break; 141687d0cdeSBrian Somers case UU_LOCK_WRITE_ERR: 14281d9597cSAndrey A. Chernov fmt = "write error: %s"; 143687d0cdeSBrian Somers break; 144687d0cdeSBrian Somers default: 14581d9597cSAndrey A. Chernov fmt = "undefined error: %s"; 146687d0cdeSBrian Somers break; 147687d0cdeSBrian Somers } 148687d0cdeSBrian Somers 14981d9597cSAndrey A. Chernov (void)snprintf(errbuf, sizeof(errbuf), fmt, strerror(errno)); 150687d0cdeSBrian Somers return errbuf; 151568b59b9SBrian Somers } 152568b59b9SBrian Somers 153568b59b9SBrian Somers static int put_pid (int fd, pid_t pid) 154568b59b9SBrian Somers { 155568b59b9SBrian Somers char buf[32]; 156568b59b9SBrian Somers int len; 157568b59b9SBrian Somers 158568b59b9SBrian Somers len = sprintf (buf, "%10d\n", pid); 159568b59b9SBrian Somers return write (fd, buf, len) == len; 160568b59b9SBrian Somers } 161568b59b9SBrian Somers 162687d0cdeSBrian Somers static pid_t get_pid (int fd,int *err) 163568b59b9SBrian Somers { 164568b59b9SBrian Somers int bytes_read; 165568b59b9SBrian Somers char buf[32]; 166568b59b9SBrian Somers pid_t pid; 167568b59b9SBrian Somers 168568b59b9SBrian Somers bytes_read = read (fd, buf, sizeof (buf) - 1); 169568b59b9SBrian Somers if (bytes_read > 0) { 170568b59b9SBrian Somers buf[bytes_read] = '\0'; 171568b59b9SBrian Somers pid = strtol (buf, (char **) NULL, 10); 172687d0cdeSBrian Somers } else { 173568b59b9SBrian Somers pid = -1; 174687d0cdeSBrian Somers *err = bytes_read ? errno : EINVAL; 175687d0cdeSBrian Somers } 176568b59b9SBrian Somers return pid; 177568b59b9SBrian Somers } 178568b59b9SBrian Somers 179568b59b9SBrian Somers /* end of uucplock.c */ 180