xref: /netbsd-src/lib/libutil/ttymsg.c (revision 037708cbd4616ccd0d7d0381ebd3964d6696c188)
1 /*	$NetBSD: ttymsg.c,v 1.4 1997/02/11 08:42:03 mrg 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)ttymsg.c	8.2 (Berkeley) 11/16/93";
39 #endif
40 static char rcsid[] = "$NetBSD: ttymsg.c,v 1.4 1997/02/11 08:42:03 mrg Exp $";
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 #include <sys/uio.h>
45 #include <signal.h>
46 #include <fcntl.h>
47 #include <dirent.h>
48 #include <errno.h>
49 #include <paths.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <stdlib.h>
54 
55 /*
56  * Display the contents of a uio structure on a terminal.  Used by wall(1),
57  * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
58  * waiting up to tmout seconds.  Returns pointer to error string on unexpected
59  * error; string is not newline-terminated.  Various "normal" errors are
60  * ignored (exclusive-use, lack of permission, etc.).
61  */
62 char *
63 ttymsg(iov, iovcnt, line, tmout)
64 	struct iovec *iov;
65 	int iovcnt;
66 	char *line;
67 	int tmout;
68 {
69 	static char device[MAXNAMLEN] = _PATH_DEV;
70 	static char errbuf[1024];
71 	register int cnt, fd, left, wret;
72 	struct iovec localiov[6];
73 	int forked = 0;
74 
75 	if (iovcnt > sizeof(localiov) / sizeof(localiov[0]))
76 		return ("too many iov's (change code in wall/ttymsg.c)");
77 
78 	(void)strncpy(device + sizeof(_PATH_DEV) - 1, line,
79 	    sizeof(device) - sizeof(_PATH_DEV));
80 	if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
81 		/* A slash is an attempt to break security... */
82 		(void) snprintf(errbuf, sizeof(errbuf), "'/' in \"%s\"",
83 		    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),
95 		    "%s: %s", device, strerror(errno));
96 		return (errbuf);
97 	}
98 
99 	for (cnt = 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; wret >= iov->iov_len; ++cnt) {
114 				wret -= iov->iov_len;
115 				++iov;
116 				--iovcnt;
117 			}
118 			if (wret) {
119 				iov->iov_base += wret;
120 				iov->iov_len -= wret;
121 			}
122 			continue;
123 		}
124 		if (errno == EWOULDBLOCK) {
125 			int cpid, off = 0;
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, O_NONBLOCK, &off);
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