[BACK]Return to uucplock.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tip

Annotation of src/usr.bin/tip/uucplock.c, Revision 1.10

1.10    ! millert     1: /*     $OpenBSD: uucplock.c,v 1.9 2002/06/12 06:07:16 mpech Exp $      */
1.3       millert     2: /*     $NetBSD: uucplock.c,v 1.7 1997/02/11 09:24:08 mrg Exp $ */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.10    ! millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: #if 0
                     35: static char sccsid[] = "@(#)uucplock.c 8.1 (Berkeley) 6/6/93";
                     36: #endif
1.10    ! millert    37: static const char rcsid[] = "$OpenBSD: uucplock.c,v 1.9 2002/06/12 06:07:16 mpech Exp $";
1.1       deraadt    38: #endif /* not lint */
                     39:
1.5       deraadt    40: #include <sys/types.h>
                     41: #include <sys/file.h>
                     42: #include <sys/dir.h>
1.1       deraadt    43: #include <stdio.h>
                     44: #include <string.h>
1.5       deraadt    45: #include <signal.h>
1.1       deraadt    46: #include <unistd.h>
1.5       deraadt    47: #include <stdlib.h>
1.1       deraadt    48: #include <errno.h>
                     49: #include "pathnames.h"
                     50:
                     51: /*
                     52:  * uucp style locking routines
                     53:  * return: 0 - success
                     54:  *       -1 - failure
                     55:  */
                     56:
1.5       deraadt    57: int
1.1       deraadt    58: uu_lock(ttyname)
                     59:        char *ttyname;
                     60: {
1.8       deraadt    61:        int fd, len;
1.1       deraadt    62:        char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN];
                     63:        char text_pid[81];
1.8       deraadt    64:        pid_t pid;
1.1       deraadt    65:
1.4       deraadt    66:        (void)snprintf(tbuf, sizeof tbuf, _PATH_LOCKDIRNAME, ttyname);
1.1       deraadt    67:        fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
                     68:        if (fd < 0) {
                     69:                /*
                     70:                 * file is already locked
                     71:                 * check to see if the process holding the lock still exists
                     72:                 */
                     73:                fd = open(tbuf, O_RDWR, 0);
                     74:                if (fd < 0) {
                     75:                        perror(tbuf);
                     76:                        fprintf(stderr, "Can't open lock file.\n");
                     77:                        return(-1);
                     78:                }
                     79:                len = read(fd, text_pid, sizeof(text_pid)-1);
                     80:                if(len<=0) {
                     81:                        perror(tbuf);
                     82:                        (void)close(fd);
                     83:                        fprintf(stderr, "Can't read lock file.\n");
                     84:                        return(-1);
                     85:                }
                     86:                text_pid[len] = 0;
                     87:                pid = atol(text_pid);
                     88:
                     89:                if (kill(pid, 0) == 0 || errno != ESRCH) {
                     90:                        (void)close(fd);        /* process is still running */
                     91:                        return(-1);
                     92:                }
                     93:                /*
                     94:                 * The process that locked the file isn't running, so
                     95:                 * we'll lock it ourselves
                     96:                 */
1.9       mpech      97:                fprintf(stderr, "Stale lock on %s PID=%ld... overriding.\n",
                     98:                        ttyname, (long)pid);
1.6       millert    99:                if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
1.1       deraadt   100:                        perror(tbuf);
                    101:                        (void)close(fd);
                    102:                        fprintf(stderr, "Can't seek lock file.\n");
                    103:                        return(-1);
                    104:                }
                    105:                /* fall out and finish the locking process */
                    106:        }
                    107:        pid = getpid();
1.8       deraadt   108:        (void)snprintf(text_pid, sizeof text_pid, "%10ld\n", (long)pid);
1.1       deraadt   109:        len = strlen(text_pid);
                    110:        if (write(fd, text_pid, len) != len) {
                    111:                (void)close(fd);
                    112:                (void)unlink(tbuf);
                    113:                perror("lock write");
                    114:                return(-1);
                    115:        }
                    116:        (void)close(fd);
                    117:        return(0);
                    118: }
                    119:
1.5       deraadt   120: int
1.1       deraadt   121: uu_unlock(ttyname)
                    122:        char *ttyname;
                    123: {
                    124:        char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN];
                    125:
1.4       deraadt   126:        (void)snprintf(tbuf, sizeof tbuf, _PATH_LOCKDIRNAME, ttyname);
1.1       deraadt   127:        return(unlink(tbuf));
                    128: }