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