uucplock.c revision 28040
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: uucplock.c,v 1.7 1997/08/05 12:58:02 ache Exp $
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 MAXTRIES 5
54
55#define LOCKTMP "LCKTMP..%d"
56#define LOCKFMT "LCK..%s"
57
58#define GORET(level, val) { err = errno; uuerr = (val); \
59			    goto __CONCAT(ret, level); }
60
61/* Forward declarations */
62static int put_pid (int fd, pid_t pid);
63static pid_t get_pid (int fd,int *err);
64
65/*
66 * uucp style locking routines
67 */
68
69int uu_lock (const char *ttyname)
70{
71	int fd, tmpfd, i;
72	pid_t pid;
73	char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN],
74	     lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
75	int err, uuerr;
76
77	pid = getpid();
78	(void)snprintf(lcktmpname, sizeof(lcktmpname), _PATH_UUCPLOCK LOCKTMP,
79			pid);
80	(void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT,
81			ttyname);
82	if ((tmpfd = creat(lcktmpname, 0664)) < 0)
83		GORET(0, UU_LOCK_CREAT_ERR);
84
85	for (i = 0; i < MAXTRIES; i++) {
86		if (link (lcktmpname, lckname) < 0) {
87			if (errno != EEXIST)
88				GORET(1, UU_LOCK_LINK_ERR);
89			/*
90			 * file is already locked
91			 * check to see if the process holding the lock
92			 * still exists
93			 */
94			if ((fd = open(lckname, O_RDONLY)) < 0)
95				GORET(1, UU_LOCK_OPEN_ERR);
96
97			if ((pid = get_pid (fd, &err)) == -1)
98				GORET(2, UU_LOCK_READ_ERR);
99
100			close(fd);
101
102			if (kill(pid, 0) == 0 || errno != ESRCH)
103				GORET(1, UU_LOCK_INUSE);
104			/*
105			 * The process that locked the file isn't running, so
106			 * we'll lock it ourselves
107			 */
108			(void)unlink(lckname);
109		} else {
110			if (!put_pid (tmpfd, pid))
111				GORET(3, UU_LOCK_WRITE_ERR);
112			break;
113		}
114	}
115	GORET(1, (i >= MAXTRIES) ? UU_LOCK_TRY_ERR : UU_LOCK_OK);
116
117ret3:
118	(void)unlink(lckname);
119	goto ret1;
120ret2:
121	(void)close(fd);
122ret1:
123	(void)close(tmpfd);
124	(void)unlink(lcktmpname);
125ret0:
126	errno = err;
127	return uuerr;
128}
129
130int uu_unlock (const char *ttyname)
131{
132	char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
133
134	(void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname);
135	return unlink(tbuf);
136}
137
138const char *uu_lockerr (int uu_lockresult)
139{
140	static char errbuf[128];
141	char *fmt;
142
143	switch (uu_lockresult) {
144		case UU_LOCK_INUSE:
145			return "device in use";
146		case UU_LOCK_OK:
147			return "";
148		case UU_LOCK_OPEN_ERR:
149			fmt = "open error: %s";
150			break;
151		case UU_LOCK_READ_ERR:
152			fmt = "read error: %s";
153			break;
154		case UU_LOCK_CREAT_ERR:
155			fmt = "creat error: %s";
156			break;
157		case UU_LOCK_WRITE_ERR:
158			fmt = "write error: %s";
159			break;
160		case UU_LOCK_LINK_ERR:
161			fmt = "link error: %s";
162			break;
163		case UU_LOCK_TRY_ERR:
164			fmt = "too many tries: %s";
165			break;
166		default:
167			fmt = "undefined error: %s";
168			break;
169	}
170
171	(void)snprintf(errbuf, sizeof(errbuf), fmt, strerror(errno));
172	return errbuf;
173}
174
175static int put_pid (int fd, pid_t pid)
176{
177	char buf[32];
178	int len;
179
180	len = sprintf (buf, "%10d\n", pid);
181	return write (fd, buf, len) == len;
182}
183
184static pid_t get_pid (int fd, int *err)
185{
186	int bytes_read;
187	char buf[32];
188	pid_t pid;
189
190	bytes_read = read (fd, buf, sizeof (buf) - 1);
191	if (bytes_read > 0) {
192		buf[bytes_read] = '\0';
193		pid = strtol (buf, (char **) NULL, 10);
194	} else {
195		pid = -1;
196		*err = bytes_read ? errno : EINVAL;
197	}
198	return pid;
199}
200
201/* end of uucplock.c */
202