Bogus ioctl arguments fixed.
[unix-history] / usr.bin / tip / uucplock.c
CommitLineData
15637ed4
RG
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, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
34 * -------------------- ----- ----------------------
35 * CURRENT PATCH LEVEL: 1 00139
36 * -------------------- ----- ----------------------
37 *
38 * 19 Aug 93 Peter da Silva Better errors and Taylor UUCP locks
39 *
40 */
41
42#ifndef lint
43static char sccsid[] = "@(#)uucplock.c 5.5 (Berkeley) 6/1/90";
44#endif /* not lint */
45
46#include <stdio.h>
47#include <sys/types.h>
48#include <sys/file.h>
49#include <sys/dir.h>
50#include <errno.h>
51#include "pathnames.h"
52
53/*
54 * uucp style locking routines
55 * return: 0 - success
56 * -1 - failure
57 */
58
59uu_lock(ttyname)
60 char *ttyname;
61{
62 extern int errno;
63 int fd, pid;
64 char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN];
65 off_t lseek();
66 char text_pid[81]; /* PDS 93 */
67 int len; /* PDS 93 */
68
69 (void)sprintf(tbuf, _PATH_LOCKDIRNAME, ttyname);
70 fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
71 if (fd < 0) {
72 /*
73 * file is already locked
74 * check to see if the process holding the lock still exists
75 */
76 fd = open(tbuf, O_RDWR, 0);
77 if (fd < 0) {
78 perror(tbuf); /* +PDS 93 */
79 fprintf(stderr, "Can't open lock file.\n");
80 return(-1);
81 }
82 len = read(fd, text_pid, sizeof(text_pid)-1);
83 if(len<=0) {
84 perror(tbuf);
85 (void)close(fd);
86 fprintf(stderr, "Can't read lock file.\n");
87 return(-1);
88 }
89 text_pid[len] = 0;
90 pid = atol(text_pid); /* -PDS 93 */
91
92 if (kill(pid, 0) == 0 || errno != ESRCH) {
93 (void)close(fd); /* process is still running */
94 return(-1);
95 }
96 /*
97 * The process that locked the file isn't running, so
98 * we'll lock it ourselves
99 */
100 /* +PDS 93 */
101 fprintf(stderr, "Stale lock on %s PID=%d... overriding.\n",
102 ttyname, pid);
103 if (lseek(fd, 0L, L_SET) < 0) {
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();
112 sprintf(text_pid, "%10d\n", pid);
113 len = strlen(text_pid);
114 if (write(fd, text_pid, len) != len) { /* -PDS 93 */
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
124uu_unlock(ttyname)
125 char *ttyname;
126{
127 char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN];
128
129 (void)sprintf(tbuf, _PATH_LOCKDIRNAME, ttyname);
130 return(unlink(tbuf));
131}