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