uucplock.c revision 24461
1/*
2 * Copyright (c) 1988, 1993
3 *	The Regents of the University of California.  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
34#ifndef lint
35static const char sccsid[] = "@(#)uucplock.c	8.1 (Berkeley) 6/6/93";
36#endif /* not lint */
37
38#include <sys/types.h>
39#include <sys/file.h>
40#include <dirent.h>
41#include <errno.h>
42#ifndef USE_PERROR
43#include <syslog.h>
44#endif
45#include <unistd.h>
46#include <signal.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <paths.h>
50#include <string.h>
51#include "libutil.h"
52
53#define LOCKFMT "LCK..%s"
54
55/* Forward declarations */
56static int put_pid (int fd, pid_t pid);
57static pid_t get_pid (int fd,int *err);
58
59/*
60 * uucp style locking routines
61 * return: 0 - success
62 * 	  -1 - failure
63 */
64
65int uu_lock (char *ttyname)
66{
67	int fd;
68	pid_t pid;
69	char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
70	int err;
71
72	(void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname);
73	fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
74	if (fd < 0) {
75		/*
76		 * file is already locked
77		 * check to see if the process holding the lock still exists
78		 */
79		fd = open(tbuf, O_RDWR, 0);
80		if (fd < 0)
81			return UU_LOCK_OPEN_ERR;
82
83		if ((pid = get_pid (fd, &err)) == -1) {
84			(void)close(fd);
85			errno = err;
86			return UU_LOCK_READ_ERR;
87		}
88
89		if (kill(pid, 0) == 0 || errno != ESRCH) {
90			(void)close(fd);	/* process is still running */
91			return UU_LOCK_INUSE;
92		}
93		/*
94		 * The process that locked the file isn't running, so
95		 * we'll lock it ourselves
96		 */
97		if (lseek(fd, (off_t) 0, L_SET) < 0) {
98			err = errno;
99			(void)close(fd);
100			errno = err;
101			return UU_LOCK_SEEK_ERR;
102		}
103		/* fall out and finish the locking process */
104	}
105	pid = getpid();
106	if (!put_pid (fd, pid)) {
107		err = errno;
108		(void)close(fd);
109		(void)unlink(tbuf);
110		errno = err;
111		return UU_LOCK_WRITE_ERR;
112	}
113	(void)close(fd);
114	return UU_LOCK_OK;
115}
116
117int uu_unlock (char *ttyname)
118{
119	char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
120
121	(void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname);
122	return unlink(tbuf);
123}
124
125char *uu_lockerr (int uu_lockresult)
126{
127	static char errbuf[512];
128	int len;
129
130	switch (uu_lockresult) {
131		case UU_LOCK_INUSE:
132			return "";
133		case UU_LOCK_OK:
134			return 0;
135		case UU_LOCK_OPEN_ERR:
136			strcpy(errbuf,"open error: ");
137			len = 12;
138			break;
139		case UU_LOCK_READ_ERR:
140			strcpy(errbuf,"read error: ");
141			len = 12;
142			break;
143		case UU_LOCK_SEEK_ERR:
144			strcpy(errbuf,"seek error: ");
145			len = 12;
146			break;
147		case UU_LOCK_WRITE_ERR:
148			strcpy(errbuf,"write error: ");
149			len = 13;
150			break;
151		default:
152			strcpy(errbuf,"Undefined error: ");
153			len = 17;
154			break;
155	}
156
157	strncpy(errbuf+len,strerror(errno),sizeof(errbuf)-len-1);
158	return errbuf;
159}
160
161static int put_pid (int fd, pid_t pid)
162{
163	char buf[32];
164	int len;
165
166	len = sprintf (buf, "%10d\n", pid);
167	return write (fd, buf, len) == len;
168}
169
170static pid_t get_pid (int fd,int *err)
171{
172	int bytes_read;
173	char buf[32];
174	pid_t pid;
175
176	bytes_read = read (fd, buf, sizeof (buf) - 1);
177	if (bytes_read > 0) {
178		buf[bytes_read] = '\0';
179		pid = strtol (buf, (char **) NULL, 10);
180	} else {
181		pid = -1;
182		*err = bytes_read ? errno : EINVAL;
183	}
184	return pid;
185}
186
187/* end of uucplock.c */
188