xref: /netbsd-src/lib/libutil/ttymsg.c (revision f876c1012ec8280866d3287842a50de849d25967)
1 /*	$NetBSD: ttymsg.c,v 1.21 2005/01/08 06:43:16 christos Exp $	*/
2 
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 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)ttymsg.c	8.2 (Berkeley) 11/16/93";
36 #else
37 __RCSID("$NetBSD: ttymsg.c,v 1.21 2005/01/08 06:43:16 christos Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include <sys/types.h>
42 #include <sys/uio.h>
43 
44 #include <assert.h>
45 #include <dirent.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <paths.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <util.h>
55 
56 /*
57  * Display the contents of a uio structure on a terminal.  Used by wall(1),
58  * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
59  * waiting up to tmout seconds.  Returns pointer to error string on unexpected
60  * error; string is not newline-terminated.  Various "normal" errors are
61  * ignored (exclusive-use, lack of permission, etc.).
62  */
63 char *
64 ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
65 {
66 	static char errbuf[1024];
67 	char device[MAXNAMLEN];
68 	const char *ptr;
69 	int cnt, fd, left, wret;
70 	struct iovec localiov[32];
71 	sigset_t nset;
72 	int forked = 0;
73 
74 	_DIAGASSERT(iov != NULL);
75 	_DIAGASSERT(iovcnt >= 0);
76 	_DIAGASSERT(line != NULL);
77 
78 	if (iovcnt >= sizeof(localiov) / sizeof(localiov[0])) {
79 		(void)snprintf(errbuf, sizeof(errbuf),
80 		    "%s: too many iov's (%d) max is %zu", __func__,
81 		    iovcnt, sizeof(localiov) / sizeof(localiov[0]));
82 		return errbuf;
83 	}
84 
85 	ptr = strncmp(line, "pts/", 4) == 0 ? line + 4 : line;
86 	if (strcspn(ptr, "./") != strlen(ptr)) {
87 		/* A slash or dot is an attempt to break security... */
88 		(void)snprintf(errbuf, sizeof(errbuf),
89 		    "%s: '/' or '.' in \"%s\"", __func__, line);
90 		return errbuf;
91 	}
92 	cnt = snprintf(device, sizeof(device), "%s%s", _PATH_DEV, line);
93 	if (cnt == -1 || cnt >= sizeof(device)) {
94 		(void) snprintf(errbuf, sizeof(errbuf),
95 		    "%s: line `%s' too long", __func__, line);
96 		return errbuf;
97 	}
98 
99 	/*
100 	 * open will fail on slip lines or exclusive-use lines
101 	 * if not running as root; not an error.
102 	 */
103 	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
104 		if (errno == EBUSY || errno == EACCES)
105 			return NULL;
106 		(void)snprintf(errbuf, sizeof(errbuf),
107 		    "%s: Cannot open `%s' (%s)",
108 		    __func__, device, strerror(errno));
109 		return errbuf;
110 	}
111 	if (!isatty(fd)) {
112 		(void)snprintf(errbuf, sizeof(errbuf),
113 		    "%s: line `%s' is not a tty device", __func__, device);
114 		(void)close(fd);
115 		return errbuf;
116 	}
117 
118 	for (cnt = left = 0; cnt < iovcnt; ++cnt)
119 		left += iov[cnt].iov_len;
120 
121 	for (;;) {
122 		wret = writev(fd, iov, iovcnt);
123 		if (wret >= left)
124 			break;
125 		if (wret > 0) {
126 			left -= wret;
127 			if (iov != localiov) {
128 				(void)memcpy(localiov, iov,
129 				    iovcnt * sizeof(struct iovec));
130 				iov = localiov;
131 			}
132 			for (cnt = 0; wret >= iov->iov_len; ++cnt) {
133 				wret -= iov->iov_len;
134 				++iov;
135 				--iovcnt;
136 			}
137 			if (wret) {
138 				iov->iov_base =
139 				    (char *)iov->iov_base + wret;
140 				iov->iov_len -= wret;
141 			}
142 			continue;
143 		} else if (wret == 0) {
144 			(void)snprintf(errbuf, sizeof(errbuf),
145 			    "%s: failed writing %d bytes to `%s'", __func__,
146 			    left, device);
147 			(void) close(fd);
148 			if (forked)
149 				_exit(1);
150 			return errbuf;
151 		}
152 		if (errno == EWOULDBLOCK) {
153 			pid_t cpid;
154 
155 			if (forked) {
156 				(void)close(fd);
157 				_exit(1);
158 			}
159 			cpid = fork();
160 			if (cpid < 0) {
161 				(void)snprintf(errbuf, sizeof(errbuf),
162 				    "%s: Cannot fork (%s)", __func__,
163 				    strerror(errno));
164 				(void)close(fd);
165 				return errbuf;
166 			}
167 			if (cpid) {	/* parent */
168 				(void)close(fd);
169 				return NULL;
170 			}
171 			forked++;
172 			/* wait at most tmout seconds */
173 			(void)signal(SIGALRM, SIG_DFL);
174 			(void)signal(SIGTERM, SIG_DFL); /* XXX */
175 			sigfillset(&nset);
176 			(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
177 			(void)alarm((u_int)tmout);
178 			(void)fcntl(fd, F_SETFL, 0);	/* clear O_NONBLOCK */
179 			continue;
180 		}
181 		/*
182 		 * We get ENODEV on a slip line if we're running as root,
183 		 * and EIO if the line just went away.
184 		 */
185 		if (errno == ENODEV || errno == EIO)
186 			break;
187 		(void) close(fd);
188 		if (forked)
189 			_exit(1);
190 		(void)snprintf(errbuf, sizeof(errbuf),
191 		    "%s: Write to line `%s' failed (%s)", __func__,
192 		    device, strerror(errno));
193 		return errbuf;
194 	}
195 
196 	(void) close(fd);
197 	if (forked)
198 		_exit(0);
199 	return NULL;
200 }
201