GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/syslogd/ttymsg.c Lines: 0 83 0.0 %
Date: 2017-11-13 Branches: 0 60 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: ttymsg.c,v 1.17 2017/10/05 16:15:24 bluhm Exp $	*/
2
/*	$NetBSD: ttymsg.c,v 1.3 1994/11/17 07:17:55 jtc Exp $	*/
3
4
/*
5
 * Copyright (c) 2014-2017 Alexander Bluhm <bluhm@genua.de>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
/*
21
 * Copyright (c) 1989, 1993
22
 *	The Regents of the University of California.  All rights reserved.
23
 *
24
 * Redistribution and use in source and binary forms, with or without
25
 * modification, are permitted provided that the following conditions
26
 * are met:
27
 * 1. Redistributions of source code must retain the above copyright
28
 *    notice, this list of conditions and the following disclaimer.
29
 * 2. Redistributions in binary form must reproduce the above copyright
30
 *    notice, this list of conditions and the following disclaimer in the
31
 *    documentation and/or other materials provided with the distribution.
32
 * 3. Neither the name of the University nor the names of its contributors
33
 *    may be used to endorse or promote products derived from this software
34
 *    without specific prior written permission.
35
 *
36
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46
 * SUCH DAMAGE.
47
 */
48
49
#include <sys/stat.h>
50
#include <sys/syslog.h>
51
52
#include <dirent.h>
53
#include <errno.h>
54
#include <event.h>
55
#include <paths.h>
56
#include <signal.h>
57
#include <stdio.h>
58
#include <stdlib.h>
59
#include <string.h>
60
#include <unistd.h>
61
62
#include "log.h"
63
#include "syslogd.h"
64
65
#ifndef nitems
66
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67
#endif
68
69
struct tty_delay {
70
	struct event	 td_event;
71
	size_t		 td_length;
72
	char		 td_line[LOG_MAXLINE];
73
};
74
int tty_delayed = 0;
75
void ttycb(int, short, void *);
76
77
/*
78
 * Display the contents of a uio structure on a terminal.
79
 * Schedules an event if write would block, waiting up to TTYMSGTIME
80
 * seconds.
81
 */
82
void
83
ttymsg(struct iovec *iov, int iovcnt, char *utline)
84
{
85
	static char device[MAXNAMLEN] = _PATH_DEV;
86
	int cnt, fd;
87
	size_t left;
88
	ssize_t wret;
89
	struct iovec localiov[6];
90
91
	if (iovcnt < 0 || (size_t)iovcnt > nitems(localiov)) {
92
		log_warnx("too many iov's (change code in syslogd/ttymsg.c)");
93
		return;
94
	}
95
96
	/*
97
	 * Ignore lines that start with "ftp" or "uucp".
98
	 */
99
	if ((strncmp(utline, "ftp", 3) == 0) ||
100
	    (strncmp(utline, "uucp", 4) == 0))
101
		return;
102
103
	(void) strlcpy(device + sizeof(_PATH_DEV) - 1, utline,
104
	    sizeof(device) - (sizeof(_PATH_DEV) - 1));
105
	if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
106
		/* A slash is an attempt to break security... */
107
		log_warnx("'/' in tty device \"%s\"", device);
108
		return;
109
	}
110
111
	/*
112
	 * open will fail on slip lines or exclusive-use lines
113
	 * if not running as root; not an error.
114
	 */
115
	if ((fd = priv_open_tty(device)) < 0) {
116
		if (errno != EBUSY && errno != EACCES)
117
			log_warn("priv_open_tty device \"%s\"", device);
118
		return;
119
	}
120
121
	left = 0;
122
	for (cnt = 0; cnt < iovcnt; ++cnt)
123
		left += iov[cnt].iov_len;
124
125
	for (;;) {
126
		wret = writev(fd, iov, iovcnt);
127
		if (wret >= 0) {
128
			if ((size_t)wret >= left)
129
				break;
130
			left -= wret;
131
			if (iov != localiov) {
132
				memmove(localiov, iov,
133
				    iovcnt * sizeof(struct iovec));
134
				iov = localiov;
135
			}
136
			while ((size_t)wret >= iov->iov_len) {
137
				wret -= iov->iov_len;
138
				++iov;
139
				--iovcnt;
140
			}
141
			if (wret) {
142
				iov->iov_base = (char *)iov->iov_base + wret;
143
				iov->iov_len -= wret;
144
			}
145
			continue;
146
		}
147
		if (errno == EWOULDBLOCK) {
148
			struct tty_delay	*td;
149
			struct timeval		 to;
150
151
			if (tty_delayed >= TTYMAXDELAY) {
152
				log_warnx("tty device \"%s\": %s",
153
				    device, "too many delayed writes");
154
				break;
155
			}
156
			log_debug("ttymsg delayed write");
157
			if (iov != localiov) {
158
				memmove(localiov, iov,
159
				    iovcnt * sizeof(struct iovec));
160
				iov = localiov;
161
			}
162
			if ((td = malloc(sizeof(*td))) == NULL) {
163
				log_warn("allocate delay tty device \"%s\"",
164
				    device);
165
				break;
166
			}
167
			td->td_length = 0;
168
			if (left > LOG_MAXLINE)
169
				left = LOG_MAXLINE;
170
			while (iovcnt && left) {
171
				if (iov->iov_len > left)
172
					iov->iov_len = left;
173
				memcpy(td->td_line + td->td_length,
174
				    iov->iov_base, iov->iov_len);
175
				td->td_length += iov->iov_len;
176
				left -= iov->iov_len;
177
				++iov;
178
				--iovcnt;
179
			}
180
			tty_delayed++;
181
			event_set(&td->td_event, fd, EV_WRITE, ttycb, td);
182
			to.tv_sec = TTYMSGTIME;
183
			to.tv_usec = 0;
184
			event_add(&td->td_event, &to);
185
			return;
186
		}
187
		/*
188
		 * We get ENODEV on a slip line if we're running as root,
189
		 * and EIO if the line just went away.
190
		 */
191
		if (errno != ENODEV && errno != EIO)
192
			log_warn("writev tty device \"%s\"", device);
193
		break;
194
	}
195
196
	(void) close(fd);
197
	return;
198
}
199
200
void
201
ttycb(int fd, short event, void *arg)
202
{
203
	struct tty_delay	*td = arg;
204
	struct timeval		 to;
205
	ssize_t			 wret;
206
207
	if (event != EV_WRITE)
208
		goto done;
209
210
	wret = write(fd, td->td_line, td->td_length);
211
	if (wret < 0 && errno != EINTR && errno != EWOULDBLOCK)
212
		goto done;
213
	if (wret > 0) {
214
		td->td_length -= wret;
215
		if (td->td_length == 0)
216
			goto done;
217
		memmove(td->td_line, td->td_line + wret, td->td_length);
218
	}
219
	to.tv_sec = TTYMSGTIME;
220
	to.tv_usec = 0;
221
	event_add(&td->td_event, &to);
222
	return;
223
224
 done:
225
	tty_delayed--;
226
	close(fd);
227
	free(td);
228
}