1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33
34 __FBSDID("$FreeBSD$");
35
36 #ifndef lint
37 static const char sccsid[] = "@(#)ttymsg.c 8.2 (Berkeley) 11/16/93";
38 #endif
39
40 #include <sys/types.h>
41 #include <sys/uio.h>
42 #include <dirent.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <paths.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51
52 #include "ttymsg.h"
53
54 /*
55 * Display the contents of a uio structure on a terminal. Used by wall(1),
56 * syslogd(8), and talkd(8). Forks and finishes in child if write would block,
57 * waiting up to tmout seconds. Returns pointer to error string on unexpected
58 * error; string is not newline-terminated. Various "normal" errors are
59 * ignored (exclusive-use, lack of permission, etc.).
60 */
61 const char *
ttymsg(struct iovec * iov,int iovcnt,const char * line,int tmout)62 ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
63 {
64 struct iovec localiov[TTYMSG_IOV_MAX];
65 ssize_t left, wret;
66 int cnt, fd;
67 char device[MAXNAMLEN] = _PATH_DEV;
68 static char errbuf[1024];
69 char *p;
70 int forked;
71
72 forked = 0;
73 if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0])))
74 return ("too many iov's (change code in wall/ttymsg.c)");
75
76 strlcat(device, line, sizeof(device));
77 p = device + sizeof(_PATH_DEV) - 1;
78 if (strncmp(p, "pts/", 4) == 0)
79 p += 4;
80 if (strchr(p, '/') != NULL) {
81 /* A slash is an attempt to break security... */
82 (void) snprintf(errbuf, sizeof(errbuf),
83 "Too many '/' in \"%s\"", device);
84 return (errbuf);
85 }
86
87 /*
88 * open will fail on slip lines or exclusive-use lines
89 * if not running as root; not an error.
90 */
91 if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
92 if (errno == EBUSY || errno == EACCES)
93 return (NULL);
94 (void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
95 strerror(errno));
96 return (errbuf);
97 }
98
99 for (cnt = 0, left = 0; cnt < iovcnt; ++cnt)
100 left += iov[cnt].iov_len;
101
102 for (;;) {
103 wret = writev(fd, iov, iovcnt);
104 if (wret >= left)
105 break;
106 if (wret >= 0) {
107 left -= wret;
108 if (iov != localiov) {
109 bcopy(iov, localiov,
110 iovcnt * sizeof(struct iovec));
111 iov = localiov;
112 }
113 for (cnt = 0; (size_t)wret >= iov->iov_len; ++cnt) {
114 wret -= iov->iov_len;
115 ++iov;
116 --iovcnt;
117 }
118 if (wret) {
119 iov->iov_base = (char *)iov->iov_base + wret;
120 iov->iov_len -= wret;
121 }
122 continue;
123 }
124 if (errno == EWOULDBLOCK) {
125 int cpid;
126
127 if (forked) {
128 (void) close(fd);
129 _exit(1);
130 }
131 cpid = fork();
132 if (cpid < 0) {
133 (void) snprintf(errbuf, sizeof(errbuf),
134 "fork: %s", strerror(errno));
135 (void) close(fd);
136 return (errbuf);
137 }
138 if (cpid) { /* parent */
139 (void) close(fd);
140 return (NULL);
141 }
142 forked++;
143 /* wait at most tmout seconds */
144 (void) signal(SIGALRM, SIG_DFL);
145 (void) signal(SIGTERM, SIG_DFL); /* XXX */
146 (void) sigsetmask(0);
147 (void) alarm((u_int)tmout);
148 (void) fcntl(fd, F_SETFL, 0); /* clear O_NONBLOCK */
149 continue;
150 }
151 /*
152 * We get ENODEV on a slip line if we're running as root,
153 * and EIO if the line just went away.
154 */
155 if (errno == ENODEV || errno == EIO)
156 break;
157 (void) close(fd);
158 if (forked)
159 _exit(1);
160 (void) snprintf(errbuf, sizeof(errbuf),
161 "%s: %s", device, strerror(errno));
162 return (errbuf);
163 }
164
165 (void) close(fd);
166 if (forked)
167 _exit(0);
168 return (NULL);
169 }
170