GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libutil/uucplock.c Lines: 0 72 0.0 %
Date: 2017-11-07 Branches: 0 40 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: uucplock.c,v 1.19 2016/08/30 14:52:09 guenther Exp $	*/
2
/*
3
 * Copyright (c) 1988, 1993
4
 *	The Regents of the University of California.  All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 * 3. Neither the name of the University nor the names of its contributors
15
 *    may be used to endorse or promote products derived from this software
16
 *    without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 *
31
 */
32
33
#include <sys/types.h>
34
#include <dirent.h>
35
#include <errno.h>
36
#include <fcntl.h>
37
#include <unistd.h>
38
#include <signal.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <paths.h>
42
#include <string.h>
43
#include "util.h"
44
45
#define MAXTRIES 5
46
47
#define LOCKTMP "LCKTMP..%ld"
48
#define LOCKFMT "LCK..%s"
49
50
#define GORET(level, val) { err = errno; uuerr = (val); \
51
			    goto __CONCAT(ret, level); }
52
53
/* Forward declarations */
54
static int put_pid(int fd, pid_t pid);
55
static pid_t get_pid(int fd,int *err);
56
57
/*
58
 * uucp style locking routines
59
 */
60
int
61
uu_lock(const char *ttyname)
62
{
63
	char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN],
64
	     lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
65
	int fd, tmpfd, i, err, uuerr;
66
	pid_t pid, pid_old;
67
68
	pid = getpid();
69
	(void)snprintf(lcktmpname, sizeof(lcktmpname), _PATH_UUCPLOCK LOCKTMP,
70
	    (long)pid);
71
	(void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT,
72
	    ttyname);
73
	tmpfd = open(lcktmpname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664);
74
	if (tmpfd < 0)
75
		GORET(0, UU_LOCK_CREAT_ERR);
76
77
	for (i = 0; i < MAXTRIES; i++) {
78
		if (link(lcktmpname, lckname) < 0) {
79
			if (errno != EEXIST)
80
				GORET(1, UU_LOCK_LINK_ERR);
81
			/*
82
			 * file is already locked
83
			 * check to see if the process holding the lock
84
			 * still exists
85
			 */
86
			if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) < 0)
87
				GORET(1, UU_LOCK_OPEN_ERR);
88
89
			if ((pid_old = get_pid(fd, &err)) == -1)
90
				GORET(2, UU_LOCK_READ_ERR);
91
92
			close(fd);
93
94
			if (kill(pid_old, 0) == 0 || errno != ESRCH)
95
				GORET(1, UU_LOCK_INUSE);
96
			/*
97
			 * The process that locked the file isn't running, so
98
			 * we'll lock it ourselves
99
			 */
100
			(void)unlink(lckname);
101
		} else {
102
			if (!put_pid(tmpfd, pid))
103
				GORET(3, UU_LOCK_WRITE_ERR);
104
			break;
105
		}
106
	}
107
	GORET(1, (i >= MAXTRIES) ? UU_LOCK_TRY_ERR : UU_LOCK_OK);
108
109
ret3:
110
	(void)unlink(lckname);
111
	goto ret1;
112
ret2:
113
	(void)close(fd);
114
ret1:
115
	(void)close(tmpfd);
116
	(void)unlink(lcktmpname);
117
ret0:
118
	errno = err;
119
	return uuerr;
120
}
121
122
int
123
uu_lock_txfr(const char *ttyname, pid_t pid)
124
{
125
	char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
126
	int fd, err, ret;
127
128
	snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname);
129
130
	if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) < 0)
131
		return UU_LOCK_OWNER_ERR;
132
	if (get_pid(fd, &err) != getpid())
133
		ret = UU_LOCK_OWNER_ERR;
134
	else {
135
		lseek(fd, 0, SEEK_SET);
136
		ret = put_pid(fd, pid) ? UU_LOCK_OK : UU_LOCK_WRITE_ERR;
137
	}
138
139
	close(fd);
140
	return ret;
141
}
142
143
int
144
uu_unlock(const char *ttyname)
145
{
146
	char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
147
148
	(void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname);
149
	return unlink(tbuf);
150
}
151
152
const char *
153
uu_lockerr(int uu_lockresult)
154
{
155
	static char errbuf[128];
156
	const char *err;
157
158
	switch (uu_lockresult) {
159
	case UU_LOCK_INUSE:
160
		return "device in use";
161
	case UU_LOCK_OK:
162
		return "";
163
	case UU_LOCK_OPEN_ERR:
164
		err = "open error";
165
		break;
166
	case UU_LOCK_READ_ERR:
167
		err = "read error";
168
		break;
169
	case UU_LOCK_CREAT_ERR:
170
		err = "creat error";
171
		break;
172
	case UU_LOCK_WRITE_ERR:
173
		err = "write error";
174
		break;
175
	case UU_LOCK_LINK_ERR:
176
		err = "link error";
177
		break;
178
	case UU_LOCK_TRY_ERR:
179
		err = "too many tries";
180
		break;
181
	case UU_LOCK_OWNER_ERR:
182
		err = "not locking process";
183
		break;
184
	default:
185
		err = "undefined error";
186
		break;
187
	}
188
189
	(void)snprintf(errbuf, sizeof(errbuf), "%s: %s", err, strerror(errno));
190
	return errbuf;
191
}
192
193
static int
194
put_pid(int fd, pid_t pid)
195
{
196
	char buf[32];
197
	int len;
198
199
	len = snprintf(buf, sizeof buf, "%10ld\n", (long)pid);
200
201
	if (len < sizeof buf && len != -1 && write(fd, buf, (size_t)len) == len) {
202
		/* We don't mind too much if ftruncate() fails - see get_pid */
203
		ftruncate(fd, (off_t)len);
204
		return 1;
205
	}
206
	return 0;
207
}
208
209
static pid_t
210
get_pid(int fd, int *err)
211
{
212
	ssize_t bytes_read;
213
	char buf[32];
214
	pid_t pid;
215
216
	bytes_read = read(fd, buf, sizeof (buf) - 1);
217
	if (bytes_read > 0) {
218
		buf[bytes_read] = '\0';
219
		pid = (pid_t)strtoul(buf, (char **) NULL, 10);
220
	} else {
221
		pid = -1;
222
		*err = bytes_read ? errno : EINVAL;
223
	}
224
	return pid;
225
}