2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
49 * Display the contents of a uio structure on a terminal. Used by wall(1),
50 * syslogd(8), and talkd(8). Forks and finishes in child if write would block,
51 * waiting up to tmout seconds. Returns pointer to error string on unexpected
52 * error; string is not newline-terminated. Various "normal" errors are
53 * ignored (exclusive-use, lack of permission, etc.).
56 ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
58 struct iovec localiov[TTYMSG_IOV_MAX];
61 char device[MAXNAMLEN] = _PATH_DEV;
62 static char errbuf[1024];
67 if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0])))
68 return ("too many iov's (change code in wall/ttymsg.c)");
70 strlcat(device, line, sizeof(device));
71 p = device + sizeof(_PATH_DEV) - 1;
72 if (strncmp(p, "pts/", 4) == 0)
74 if (strchr(p, '/') != NULL) {
75 /* A slash is an attempt to break security... */
76 (void) snprintf(errbuf, sizeof(errbuf),
77 "Too many '/' in \"%s\"", device);
82 * open will fail on slip lines or exclusive-use lines
83 * if not running as root; not an error.
85 if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
86 if (errno == EBUSY || errno == EACCES)
88 (void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
93 for (cnt = 0, left = 0; cnt < iovcnt; ++cnt)
94 left += iov[cnt].iov_len;
97 wret = writev(fd, iov, iovcnt);
102 if (iov != localiov) {
104 iovcnt * sizeof(struct iovec));
107 for (cnt = 0; (size_t)wret >= iov->iov_len; ++cnt) {
108 wret -= iov->iov_len;
113 iov->iov_base = (char *)iov->iov_base + wret;
114 iov->iov_len -= wret;
118 if (errno == EWOULDBLOCK) {
127 (void) snprintf(errbuf, sizeof(errbuf),
128 "fork: %s", strerror(errno));
132 if (cpid) { /* parent */
137 /* wait at most tmout seconds */
138 (void) signal(SIGALRM, SIG_DFL);
139 (void) signal(SIGTERM, SIG_DFL); /* XXX */
140 (void) sigsetmask(0);
141 (void) alarm((u_int)tmout);
142 (void) fcntl(fd, F_SETFL, 0); /* clear O_NONBLOCK */
146 * We get ENODEV on a slip line if we're running as root,
147 * and EIO if the line just went away.
149 if (errno == ENODEV || errno == EIO)
154 (void) snprintf(errbuf, sizeof(errbuf),
155 "%s: %s", device, strerror(errno));