|
|
1.1 root 1: /*
2: * Copyright (c) 1988 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: static char sccsid[] = "@(#)uucplock.c 5.5 (Berkeley) 6/1/90";
22: #endif /* not lint */
23:
24: #include <sys/types.h>
25: #include <sys/file.h>
26: #include <sys/dir.h>
27: #include <errno.h>
28: #include "pathnames.h"
29:
30: /*
31: * uucp style locking routines
32: * return: 0 - success
33: * -1 - failure
34: */
35:
36: uu_lock(ttyname)
37: char *ttyname;
38: {
39: extern int errno;
40: int fd, pid;
41: char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN];
42: off_t lseek();
43:
44: (void)sprintf(tbuf, _PATH_LOCKDIRNAME, ttyname);
45: fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
46: if (fd < 0) {
47: /*
48: * file is already locked
49: * check to see if the process holding the lock still exists
50: */
51: fd = open(tbuf, O_RDWR, 0);
52: if (fd < 0) {
53: perror("lock open");
54: return(-1);
55: }
56: if (read(fd, &pid, sizeof(pid)) != sizeof(pid)) {
57: (void)close(fd);
58: perror("lock read");
59: return(-1);
60: }
61:
62: if (kill(pid, 0) == 0 || errno != ESRCH) {
63: (void)close(fd); /* process is still running */
64: return(-1);
65: }
66: /*
67: * The process that locked the file isn't running, so
68: * we'll lock it ourselves
69: */
70: if (lseek(fd, 0L, L_SET) < 0) {
71: (void)close(fd);
72: perror("lock lseek");
73: return(-1);
74: }
75: /* fall out and finish the locking process */
76: }
77: pid = getpid();
78: if (write(fd, (char *)&pid, sizeof(pid)) != sizeof(pid)) {
79: (void)close(fd);
80: (void)unlink(tbuf);
81: perror("lock write");
82: return(-1);
83: }
84: (void)close(fd);
85: return(0);
86: }
87:
88: uu_unlock(ttyname)
89: char *ttyname;
90: {
91: char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN];
92:
93: (void)sprintf(tbuf, _PATH_LOCKDIRNAME, ttyname);
94: return(unlink(tbuf));
95: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.