1 /* $OpenBSD: ttymsg.c,v 1.3 1996/10/25 06:06:30 downsj 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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37
38 #include "config.h"
39
40 #include <sys/types.h>
41 #include <sys/uio.h>
42 #ifdef HAVE_DIRENT_H
43 # include <dirent.h>
44 #elif defined(HAVE_SYS_HNDIR_H)
45 # include <sys/ndir.h>
46 #elif defined(HAVE_SYS_HDIR_H)
47 # include <sys/dir.h>
48 #elif defined(HAVE_NDIR_H)
49 # include <ndir.h>
50 #else
51 # define MAXNAMLEN 128
52 # warning Using MAXNAMLEN of 128
53 #endif
54 #ifndef MAXNAMLEN
55 #include <limits.h>
56 #define MAXNAMLEN NAME_MAX
57 #endif
58 #include <errno.h>
59 #include <fcntl.h>
60 #ifdef HAVE_PATHS_H
61 # include <paths.h>
62 #endif
63 #include <signal.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include <strings.h>
68
69 #ifndef _PATH_DEV
70 #define _PATH_DEV "/dev/"
71 /* #warning Using "/dev/" for _PATH_DEV */
72 #endif
73
74 #ifndef sigsetmask
75 int sigsetmask(int);
76 #endif
77
78 /*
79 * Display the contents of a uio structure on a terminal. Used by wall(1),
80 * syslogd(8), and talkd(8). Forks and finishes in child if write would block,
81 * waiting up to tmout seconds. Returns pointer to error string on unexpected
82 * error; string is not newline-terminated. Various "normal" errors are
83 * ignored (exclusive-use, lack of permission, etc.).
84 */
85 char *
ttymsg(struct iovec * iov,int iovcnt,char * line,int tmout)86 ttymsg(struct iovec *iov, int iovcnt, char *line, int tmout)
87 {
88 static char device[MAXNAMLEN] = { _PATH_DEV };
89 static char errbuf[1024];
90 register int cnt, fd, left, wret;
91 struct iovec localiov[6];
92 int forked = 0;
93
94
95 if (iovcnt > sizeof(localiov) / sizeof(localiov[0]))
96 return ("too many iov's (change code in wall/ttymsg.c)");
97
98 /*
99 * Ignore lines that start with "ftp" or "uucp".
100 */
101 if ((strncmp(line, "ftp", 3) == 0)
102 || (strncmp(line, "uucp", 4) == 0))
103 return (NULL);
104
105 (void) strcpy(device + sizeof(_PATH_DEV) - 1, line);
106
107 #ifndef HAVE_LINUX
108 if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
109 /* A slash is an attempt to break security... */
110 (void) snprintf(errbuf, sizeof(errbuf), "'/' in \"%s\"",
111 device);
112 return (errbuf);
113 }
114 #endif
115
116 /*
117 * open will fail on slip lines or exclusive-use lines
118 * if not running as root; not an error.
119 */
120 if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
121 if (errno == EBUSY || errno == EACCES)
122 return (NULL);
123 (void) snprintf(errbuf, sizeof(errbuf),
124 "%s: %s", device, strerror(errno));
125 return (errbuf);
126 }
127
128 for (cnt = left = 0; cnt < iovcnt; ++cnt)
129 left += iov[cnt].iov_len;
130
131 for (;;) {
132 wret = writev(fd, iov, iovcnt);
133 if (wret >= left)
134 break;
135 if (wret >= 0) {
136 left -= wret;
137 if (iov != localiov) {
138 bcopy(iov, localiov,
139 iovcnt * sizeof(struct iovec));
140 iov = localiov;
141 }
142 for (cnt = 0; wret >= iov->iov_len; ++cnt) {
143 wret -= iov->iov_len;
144 ++iov;
145 --iovcnt;
146 }
147 /* we assume writev() writes whole chunks. posix? */
148 continue;
149 }
150 if (errno == EWOULDBLOCK) {
151 int cpid, off = 0;
152
153 if (forked) {
154 (void) close(fd);
155 _exit(1);
156 }
157 cpid = fork();
158 if (cpid < 0) {
159 (void) snprintf(errbuf, sizeof(errbuf),
160 "fork: %s", strerror(errno));
161 (void) close(fd);
162 return (errbuf);
163 }
164 if (cpid) { /* parent */
165 (void) close(fd);
166 return (NULL);
167 }
168 forked++;
169 /* wait at most tmout seconds */
170 (void) signal(SIGALRM, SIG_DFL);
171 (void) signal(SIGTERM, SIG_DFL); /* XXX */
172 sigsetmask(0);
173 (void) alarm((u_int)tmout);
174 (void) fcntl(fd, O_NONBLOCK, &off);
175 continue;
176 }
177 /*
178 * We get ENODEV on a slip line if we're running as root,
179 * and EIO if the line just went away.
180 */
181 if (errno == ENODEV || errno == EIO)
182 break;
183 (void) close(fd);
184 if (forked)
185 _exit(1);
186 (void) snprintf(errbuf, sizeof(errbuf),
187 "%s: %s", device, strerror(errno));
188 return (errbuf);
189 }
190
191 (void) close(fd);
192 if (forked)
193 _exit(0);
194 return (NULL);
195 }
196