1*0c3983b2SBen Gras /* $NetBSD: ttymsg.c,v 1.23 2009/01/18 12:13:04 lukem Exp $ */
2*0c3983b2SBen Gras
3*0c3983b2SBen Gras /*
4*0c3983b2SBen Gras * Copyright (c) 1989, 1993
5*0c3983b2SBen Gras * The Regents of the University of California. All rights reserved.
6*0c3983b2SBen Gras *
7*0c3983b2SBen Gras * Redistribution and use in source and binary forms, with or without
8*0c3983b2SBen Gras * modification, are permitted provided that the following conditions
9*0c3983b2SBen Gras * are met:
10*0c3983b2SBen Gras * 1. Redistributions of source code must retain the above copyright
11*0c3983b2SBen Gras * notice, this list of conditions and the following disclaimer.
12*0c3983b2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13*0c3983b2SBen Gras * notice, this list of conditions and the following disclaimer in the
14*0c3983b2SBen Gras * documentation and/or other materials provided with the distribution.
15*0c3983b2SBen Gras * 3. Neither the name of the University nor the names of its contributors
16*0c3983b2SBen Gras * may be used to endorse or promote products derived from this software
17*0c3983b2SBen Gras * without specific prior written permission.
18*0c3983b2SBen Gras *
19*0c3983b2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*0c3983b2SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*0c3983b2SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*0c3983b2SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*0c3983b2SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*0c3983b2SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*0c3983b2SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*0c3983b2SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*0c3983b2SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*0c3983b2SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0c3983b2SBen Gras * SUCH DAMAGE.
30*0c3983b2SBen Gras */
31*0c3983b2SBen Gras
32*0c3983b2SBen Gras #include <sys/cdefs.h>
33*0c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*0c3983b2SBen Gras #if 0
35*0c3983b2SBen Gras static char sccsid[] = "@(#)ttymsg.c 8.2 (Berkeley) 11/16/93";
36*0c3983b2SBen Gras #else
37*0c3983b2SBen Gras __RCSID("$NetBSD: ttymsg.c,v 1.23 2009/01/18 12:13:04 lukem Exp $");
38*0c3983b2SBen Gras #endif
39*0c3983b2SBen Gras #endif /* LIBC_SCCS and not lint */
40*0c3983b2SBen Gras
41*0c3983b2SBen Gras #include <sys/types.h>
42*0c3983b2SBen Gras #include <sys/uio.h>
43*0c3983b2SBen Gras
44*0c3983b2SBen Gras #include <assert.h>
45*0c3983b2SBen Gras #include <dirent.h>
46*0c3983b2SBen Gras #include <errno.h>
47*0c3983b2SBen Gras #include <fcntl.h>
48*0c3983b2SBen Gras #include <paths.h>
49*0c3983b2SBen Gras #include <signal.h>
50*0c3983b2SBen Gras #include <stdio.h>
51*0c3983b2SBen Gras #include <stdlib.h>
52*0c3983b2SBen Gras #include <string.h>
53*0c3983b2SBen Gras #include <unistd.h>
54*0c3983b2SBen Gras #include <util.h>
55*0c3983b2SBen Gras
56*0c3983b2SBen Gras /*
57*0c3983b2SBen Gras * Display the contents of a uio structure on a terminal. Used by wall(1),
58*0c3983b2SBen Gras * syslogd(8), and talkd(8). Forks and finishes in child if write would block,
59*0c3983b2SBen Gras * waiting up to tmout seconds. Returns pointer to error string on unexpected
60*0c3983b2SBen Gras * error; string is not newline-terminated. Various "normal" errors are
61*0c3983b2SBen Gras * ignored (exclusive-use, lack of permission, etc.).
62*0c3983b2SBen Gras */
63*0c3983b2SBen Gras char *
ttymsg(struct iovec * iov,int iovcnt,const char * line,int tmout)64*0c3983b2SBen Gras ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
65*0c3983b2SBen Gras {
66*0c3983b2SBen Gras static char errbuf[1024];
67*0c3983b2SBen Gras char device[MAXNAMLEN];
68*0c3983b2SBen Gras const char *ptr;
69*0c3983b2SBen Gras int fd, ret;
70*0c3983b2SBen Gras struct iovec localiov[32];
71*0c3983b2SBen Gras sigset_t nset;
72*0c3983b2SBen Gras int forked = 0;
73*0c3983b2SBen Gras size_t cnt, left, wret;
74*0c3983b2SBen Gras
75*0c3983b2SBen Gras _DIAGASSERT(iov != NULL);
76*0c3983b2SBen Gras _DIAGASSERT(iovcnt >= 0);
77*0c3983b2SBen Gras _DIAGASSERT(line != NULL);
78*0c3983b2SBen Gras
79*0c3983b2SBen Gras if (iovcnt < 0) {
80*0c3983b2SBen Gras (void)snprintf(errbuf, sizeof(errbuf),
81*0c3983b2SBen Gras "%s: negative iovcnt", __func__);
82*0c3983b2SBen Gras return errbuf;
83*0c3983b2SBen Gras }
84*0c3983b2SBen Gras
85*0c3983b2SBen Gras if ((size_t)iovcnt >= sizeof(localiov) / sizeof(localiov[0])) {
86*0c3983b2SBen Gras (void)snprintf(errbuf, sizeof(errbuf),
87*0c3983b2SBen Gras "%s: too many iov's (%d) max is %zu", __func__,
88*0c3983b2SBen Gras iovcnt, sizeof(localiov) / sizeof(localiov[0]));
89*0c3983b2SBen Gras return errbuf;
90*0c3983b2SBen Gras }
91*0c3983b2SBen Gras
92*0c3983b2SBen Gras ptr = strncmp(line, "pts/", (size_t)4) == 0 ? line + 4 : line;
93*0c3983b2SBen Gras if (strcspn(ptr, "./") != strlen(ptr)) {
94*0c3983b2SBen Gras /* A slash or dot is an attempt to break security... */
95*0c3983b2SBen Gras (void)snprintf(errbuf, sizeof(errbuf),
96*0c3983b2SBen Gras "%s: '/' or '.' in \"%s\"", __func__, line);
97*0c3983b2SBen Gras return errbuf;
98*0c3983b2SBen Gras }
99*0c3983b2SBen Gras ret = snprintf(device, sizeof(device), "%s%s", _PATH_DEV, line);
100*0c3983b2SBen Gras if (ret == -1 || ret >= (int)sizeof(device)) {
101*0c3983b2SBen Gras (void) snprintf(errbuf, sizeof(errbuf),
102*0c3983b2SBen Gras "%s: line `%s' too long", __func__, line);
103*0c3983b2SBen Gras return errbuf;
104*0c3983b2SBen Gras }
105*0c3983b2SBen Gras cnt = (size_t)ret;
106*0c3983b2SBen Gras
107*0c3983b2SBen Gras /*
108*0c3983b2SBen Gras * open will fail on slip lines or exclusive-use lines
109*0c3983b2SBen Gras * if not running as root; not an error.
110*0c3983b2SBen Gras */
111*0c3983b2SBen Gras if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
112*0c3983b2SBen Gras if (errno == EBUSY || errno == EACCES)
113*0c3983b2SBen Gras return NULL;
114*0c3983b2SBen Gras (void)snprintf(errbuf, sizeof(errbuf),
115*0c3983b2SBen Gras "%s: Cannot open `%s' (%s)",
116*0c3983b2SBen Gras __func__, device, strerror(errno));
117*0c3983b2SBen Gras return errbuf;
118*0c3983b2SBen Gras }
119*0c3983b2SBen Gras if (!isatty(fd)) {
120*0c3983b2SBen Gras (void)snprintf(errbuf, sizeof(errbuf),
121*0c3983b2SBen Gras "%s: line `%s' is not a tty device", __func__, device);
122*0c3983b2SBen Gras (void)close(fd);
123*0c3983b2SBen Gras return errbuf;
124*0c3983b2SBen Gras }
125*0c3983b2SBen Gras
126*0c3983b2SBen Gras for (cnt = left = 0; cnt < (size_t)iovcnt; ++cnt)
127*0c3983b2SBen Gras left += iov[cnt].iov_len;
128*0c3983b2SBen Gras
129*0c3983b2SBen Gras for (;;) {
130*0c3983b2SBen Gras wret = writev(fd, iov, iovcnt);
131*0c3983b2SBen Gras if (wret >= left)
132*0c3983b2SBen Gras break;
133*0c3983b2SBen Gras if (wret > 0) {
134*0c3983b2SBen Gras left -= wret;
135*0c3983b2SBen Gras if (iov != localiov) {
136*0c3983b2SBen Gras (void)memcpy(localiov, iov,
137*0c3983b2SBen Gras iovcnt * sizeof(struct iovec));
138*0c3983b2SBen Gras iov = localiov;
139*0c3983b2SBen Gras }
140*0c3983b2SBen Gras for (cnt = 0; wret >= iov->iov_len; ++cnt) {
141*0c3983b2SBen Gras wret -= iov->iov_len;
142*0c3983b2SBen Gras ++iov;
143*0c3983b2SBen Gras --iovcnt;
144*0c3983b2SBen Gras }
145*0c3983b2SBen Gras if (wret) {
146*0c3983b2SBen Gras iov->iov_base =
147*0c3983b2SBen Gras (char *)iov->iov_base + wret;
148*0c3983b2SBen Gras iov->iov_len -= wret;
149*0c3983b2SBen Gras }
150*0c3983b2SBen Gras continue;
151*0c3983b2SBen Gras } else if (wret == 0) {
152*0c3983b2SBen Gras (void)snprintf(errbuf, sizeof(errbuf),
153*0c3983b2SBen Gras "%s: failed writing %zu bytes to `%s'", __func__,
154*0c3983b2SBen Gras left, device);
155*0c3983b2SBen Gras (void) close(fd);
156*0c3983b2SBen Gras if (forked)
157*0c3983b2SBen Gras _exit(1);
158*0c3983b2SBen Gras return errbuf;
159*0c3983b2SBen Gras }
160*0c3983b2SBen Gras if (errno == EWOULDBLOCK) {
161*0c3983b2SBen Gras pid_t cpid;
162*0c3983b2SBen Gras
163*0c3983b2SBen Gras if (forked) {
164*0c3983b2SBen Gras (void)close(fd);
165*0c3983b2SBen Gras _exit(1);
166*0c3983b2SBen Gras }
167*0c3983b2SBen Gras cpid = fork();
168*0c3983b2SBen Gras if (cpid < 0) {
169*0c3983b2SBen Gras (void)snprintf(errbuf, sizeof(errbuf),
170*0c3983b2SBen Gras "%s: Cannot fork (%s)", __func__,
171*0c3983b2SBen Gras strerror(errno));
172*0c3983b2SBen Gras (void)close(fd);
173*0c3983b2SBen Gras return errbuf;
174*0c3983b2SBen Gras }
175*0c3983b2SBen Gras if (cpid) { /* parent */
176*0c3983b2SBen Gras (void)close(fd);
177*0c3983b2SBen Gras return NULL;
178*0c3983b2SBen Gras }
179*0c3983b2SBen Gras forked++;
180*0c3983b2SBen Gras /* wait at most tmout seconds */
181*0c3983b2SBen Gras (void)signal(SIGALRM, SIG_DFL);
182*0c3983b2SBen Gras (void)signal(SIGTERM, SIG_DFL); /* XXX */
183*0c3983b2SBen Gras sigfillset(&nset);
184*0c3983b2SBen Gras (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
185*0c3983b2SBen Gras (void)alarm((u_int)tmout);
186*0c3983b2SBen Gras (void)fcntl(fd, F_SETFL, 0); /* clear O_NONBLOCK */
187*0c3983b2SBen Gras continue;
188*0c3983b2SBen Gras }
189*0c3983b2SBen Gras /*
190*0c3983b2SBen Gras * We get ENODEV on a slip line if we're running as root,
191*0c3983b2SBen Gras * and EIO if the line just went away.
192*0c3983b2SBen Gras */
193*0c3983b2SBen Gras if (errno == ENODEV || errno == EIO)
194*0c3983b2SBen Gras break;
195*0c3983b2SBen Gras (void) close(fd);
196*0c3983b2SBen Gras if (forked)
197*0c3983b2SBen Gras _exit(1);
198*0c3983b2SBen Gras (void)snprintf(errbuf, sizeof(errbuf),
199*0c3983b2SBen Gras "%s: Write to line `%s' failed (%s)", __func__,
200*0c3983b2SBen Gras device, strerror(errno));
201*0c3983b2SBen Gras return errbuf;
202*0c3983b2SBen Gras }
203*0c3983b2SBen Gras
204*0c3983b2SBen Gras (void) close(fd);
205*0c3983b2SBen Gras if (forked)
206*0c3983b2SBen Gras _exit(0);
207*0c3983b2SBen Gras return NULL;
208*0c3983b2SBen Gras }
209