1 /* $OpenBSD: ttymsg.c,v 1.19 2021/09/03 16:28:33 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 struct tty_delay { 66 struct event td_event; 67 size_t td_length; 68 char td_line[LOG_MAXLINE]; 69 }; 70 int tty_delayed = 0; 71 void ttycb(int, short, void *); 72 73 /* 74 * Display the contents of a uio structure on a terminal. 75 * Schedules an event if write would block, waiting up to TTYMSGTIME 76 * seconds. 77 */ 78 void 79 ttymsg(char *utline, struct iovec *iov) 80 { 81 static char device[MAXNAMLEN] = _PATH_DEV; 82 struct iovec localiov[IOVCNT]; 83 int iovcnt = IOVCNT; 84 int cnt, fd; 85 size_t left; 86 ssize_t wret; 87 88 /* 89 * Ignore lines that start with "ftp" or "uucp". 90 */ 91 if ((strncmp(utline, "ftp", 3) == 0) || 92 (strncmp(utline, "uucp", 4) == 0)) 93 return; 94 95 (void) strlcpy(device + sizeof(_PATH_DEV) - 1, utline, 96 sizeof(device) - (sizeof(_PATH_DEV) - 1)); 97 if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) { 98 /* A slash is an attempt to break security... */ 99 log_warnx("'/' in tty device \"%s\"", device); 100 return; 101 } 102 103 /* 104 * open will fail on slip lines or exclusive-use lines 105 * if not running as root; not an error. 106 */ 107 if ((fd = priv_open_tty(device)) == -1) { 108 if (errno != EBUSY && errno != EACCES) 109 log_warn("priv_open_tty device \"%s\"", device); 110 return; 111 } 112 113 left = 0; 114 for (cnt = 0; cnt < iovcnt; ++cnt) 115 left += iov[cnt].iov_len; 116 117 for (;;) { 118 wret = writev(fd, iov, iovcnt); 119 if (wret >= 0) { 120 if ((size_t)wret >= left) 121 break; 122 left -= wret; 123 if (iov != localiov) { 124 memmove(localiov, iov, 125 iovcnt * sizeof(struct iovec)); 126 iov = localiov; 127 } 128 while ((size_t)wret >= iov->iov_len) { 129 wret -= iov->iov_len; 130 ++iov; 131 --iovcnt; 132 } 133 if (wret) { 134 iov->iov_base = (char *)iov->iov_base + wret; 135 iov->iov_len -= wret; 136 } 137 continue; 138 } 139 if (errno == EWOULDBLOCK) { 140 struct tty_delay *td; 141 struct timeval to; 142 143 if (tty_delayed >= TTYMAXDELAY) { 144 log_warnx("tty device \"%s\": %s", 145 device, "too many delayed writes"); 146 break; 147 } 148 log_debug("ttymsg delayed write"); 149 if (iov != localiov) { 150 memmove(localiov, iov, 151 iovcnt * sizeof(struct iovec)); 152 iov = localiov; 153 } 154 if ((td = malloc(sizeof(*td))) == NULL) { 155 log_warn("allocate delay tty device \"%s\"", 156 device); 157 break; 158 } 159 td->td_length = 0; 160 if (left > LOG_MAXLINE) 161 left = LOG_MAXLINE; 162 while (iovcnt && left) { 163 if (iov->iov_len > left) 164 iov->iov_len = left; 165 memcpy(td->td_line + td->td_length, 166 iov->iov_base, iov->iov_len); 167 td->td_length += iov->iov_len; 168 left -= iov->iov_len; 169 ++iov; 170 --iovcnt; 171 } 172 tty_delayed++; 173 event_set(&td->td_event, fd, EV_WRITE, ttycb, td); 174 to.tv_sec = TTYMSGTIME; 175 to.tv_usec = 0; 176 event_add(&td->td_event, &to); 177 return; 178 } 179 /* 180 * We get ENODEV on a slip line if we're running as root, 181 * and EIO if the line just went away. 182 */ 183 if (errno != ENODEV && errno != EIO) 184 log_warn("writev tty device \"%s\"", device); 185 break; 186 } 187 188 (void) close(fd); 189 return; 190 } 191 192 void 193 ttycb(int fd, short event, void *arg) 194 { 195 struct tty_delay *td = arg; 196 struct timeval to; 197 ssize_t wret; 198 199 if (event != EV_WRITE) 200 goto done; 201 202 wret = write(fd, td->td_line, td->td_length); 203 if (wret == -1 && errno != EINTR && errno != EWOULDBLOCK) 204 goto done; 205 if (wret > 0) { 206 td->td_length -= wret; 207 if (td->td_length == 0) 208 goto done; 209 memmove(td->td_line, td->td_line + wret, td->td_length); 210 } 211 to.tv_sec = TTYMSGTIME; 212 to.tv_usec = 0; 213 event_add(&td->td_event, &to); 214 return; 215 216 done: 217 tty_delayed--; 218 close(fd); 219 free(td); 220 } 221