uucplock.c revision 24531
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 * $Id$
34 *
35 */
36
37#ifndef lint
38static const char sccsid[] = "@(#)uucplock.c	8.1 (Berkeley) 6/6/93";
39#endif /* not lint */
40
41#include <sys/types.h>
42#include <sys/file.h>
43#include <dirent.h>
44#include <errno.h>
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)snprintf(tbuf, sizeof(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)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname);
122	return unlink(tbuf);
123}
124
125char *uu_lockerr (int uu_lockresult)
126{
127	static char errbuf[128];
128	char *fmt;
129
130	switch (uu_lockresult) {
131		case UU_LOCK_INUSE:
132			return "device in use";
133		case UU_LOCK_OK:
134			return "";
135		case UU_LOCK_OPEN_ERR:
136			fmt = "open error: %s";
137			break;
138		case UU_LOCK_READ_ERR:
139			fmt = "read error: %s";
140			break;
141		case UU_LOCK_SEEK_ERR:
142			fmt = "seek error: %s";
143			break;
144		case UU_LOCK_WRITE_ERR:
145			fmt = "write error: %s";
146			break;
147		default:
148			fmt = "undefined error: %s";
149			break;
150	}
151
152	(void)snprintf(errbuf, sizeof(errbuf), fmt, strerror(errno));
153	return errbuf;
154}
155
156static int put_pid (int fd, pid_t pid)
157{
158	char buf[32];
159	int len;
160
161	len = sprintf (buf, "%10d\n", pid);
162	return write (fd, buf, len) == len;
163}
164
165static pid_t get_pid (int fd,int *err)
166{
167	int bytes_read;
168	char buf[32];
169	pid_t pid;
170
171	bytes_read = read (fd, buf, sizeof (buf) - 1);
172	if (bytes_read > 0) {
173		buf[bytes_read] = '\0';
174		pid = strtol (buf, (char **) NULL, 10);
175	} else {
176		pid = -1;
177		*err = bytes_read ? errno : EINVAL;
178	}
179	return pid;
180}
181
182/* end of uucplock.c */
183