Commit | Line | Data |
---|---|---|
051b1e55 | 1 | /* |
0a6056a4 KB |
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 the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
051b1e55 DF |
16 | */ |
17 | ||
05862919 | 18 | #ifndef lint |
435e8dff | 19 | static char sccsid[] = "@(#)uucplock.c 5.4 (Berkeley) %G%"; |
0a6056a4 | 20 | #endif /* not lint */ |
5b6474c5 | 21 | |
5b6474c5 | 22 | #include <sys/types.h> |
0a6056a4 KB |
23 | #include <sys/file.h> |
24 | #include <sys/dir.h> | |
25 | #include <errno.h> | |
435e8dff | 26 | #include "pathnames.h" |
5b6474c5 | 27 | |
0a6056a4 KB |
28 | /* |
29 | * uucp style locking routines | |
30 | * return: 0 - success | |
31 | * -1 - failure | |
5b6474c5 BJ |
32 | */ |
33 | ||
0a6056a4 KB |
34 | uu_lock(ttyname) |
35 | char *ttyname; | |
5b6474c5 | 36 | { |
0a6056a4 KB |
37 | extern int errno; |
38 | int fd, pid; | |
435e8dff | 39 | char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN]; |
0a6056a4 KB |
40 | off_t lseek(); |
41 | ||
435e8dff | 42 | (void)sprintf(tbuf, _PATH_LOCKDIRNAME, ttyname); |
0a6056a4 KB |
43 | fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660); |
44 | if (fd < 0) { | |
45 | /* | |
46 | * file is already locked | |
47 | * check to see if the process holding the lock still exists | |
48 | */ | |
49 | fd = open(tbuf, O_RDWR, 0); | |
50 | if (fd < 0) { | |
51 | perror("lock open"); | |
52 | return(-1); | |
53 | } | |
54 | if (read(fd, &pid, sizeof(pid)) != sizeof(pid)) { | |
55 | (void)close(fd); | |
56 | perror("lock read"); | |
57 | return(-1); | |
58 | } | |
5b6474c5 | 59 | |
0a6056a4 KB |
60 | if (kill(pid, 0) == 0 || errno != ESRCH) { |
61 | (void)close(fd); /* process is still running */ | |
62 | return(-1); | |
63 | } | |
64 | /* | |
65 | * The process that locked the file isn't running, so | |
66 | * we'll lock it ourselves | |
67 | */ | |
68 | if (lseek(fd, 0L, L_SET) < 0) { | |
69 | (void)close(fd); | |
70 | perror("lock lseek"); | |
71 | return(-1); | |
5b6474c5 | 72 | } |
0a6056a4 | 73 | /* fall out and finish the locking process */ |
5b6474c5 | 74 | } |
0a6056a4 KB |
75 | pid = getpid(); |
76 | if (write(fd, (char *)&pid, sizeof(pid)) != sizeof(pid)) { | |
77 | (void)close(fd); | |
78 | (void)unlink(tbuf); | |
79 | perror("lock write"); | |
80 | return(-1); | |
5b6474c5 | 81 | } |
0a6056a4 KB |
82 | (void)close(fd); |
83 | return(0); | |
5b6474c5 BJ |
84 | } |
85 | ||
0a6056a4 KB |
86 | uu_unlock(ttyname) |
87 | char *ttyname; | |
5b6474c5 | 88 | { |
435e8dff | 89 | char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN]; |
5b6474c5 | 90 | |
435e8dff | 91 | (void)sprintf(tbuf, _PATH_LOCKDIRNAME, ttyname); |
0a6056a4 | 92 | return(unlink(tbuf)); |
5b6474c5 | 93 | } |