xref: /netbsd-src/lib/libutil/ttymsg.c (revision c5eb4ab6019d3068d6831a742ba274377a333a48)
1*c5eb4ab6Slukem /*	$NetBSD: ttymsg.c,v 1.23 2009/01/18 12:13:04 lukem Exp $	*/
2a8bfd3d1Sjtc 
361f28255Scgd /*
4a8bfd3d1Sjtc  * Copyright (c) 1989, 1993
5a8bfd3d1Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32f5646a08Schristos #include <sys/cdefs.h>
33f5646a08Schristos #if defined(LIBC_SCCS) && !defined(lint)
34a8bfd3d1Sjtc #if 0
35a8bfd3d1Sjtc static char sccsid[] = "@(#)ttymsg.c	8.2 (Berkeley) 11/16/93";
36f5646a08Schristos #else
37*c5eb4ab6Slukem __RCSID("$NetBSD: ttymsg.c,v 1.23 2009/01/18 12:13:04 lukem Exp $");
38a8bfd3d1Sjtc #endif
39f5646a08Schristos #endif /* LIBC_SCCS and not lint */
4061f28255Scgd 
4161f28255Scgd #include <sys/types.h>
4261f28255Scgd #include <sys/uio.h>
43b48252f3Slukem 
44b48252f3Slukem #include <assert.h>
4561f28255Scgd #include <dirent.h>
4661f28255Scgd #include <errno.h>
47b48252f3Slukem #include <fcntl.h>
4861f28255Scgd #include <paths.h>
49b48252f3Slukem #include <signal.h>
5061f28255Scgd #include <stdio.h>
5161f28255Scgd #include <stdlib.h>
52b48252f3Slukem #include <string.h>
53b48252f3Slukem #include <unistd.h>
54f5646a08Schristos #include <util.h>
5561f28255Scgd 
5661f28255Scgd /*
57a8bfd3d1Sjtc  * Display the contents of a uio structure on a terminal.  Used by wall(1),
58a8bfd3d1Sjtc  * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
59a8bfd3d1Sjtc  * waiting up to tmout seconds.  Returns pointer to error string on unexpected
60a8bfd3d1Sjtc  * error; string is not newline-terminated.  Various "normal" errors are
61a8bfd3d1Sjtc  * ignored (exclusive-use, lack of permission, etc.).
6261f28255Scgd  */
6361f28255Scgd char *
ttymsg(struct iovec * iov,int iovcnt,const char * line,int tmout)64fce98185Sad ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
6561f28255Scgd {
6661f28255Scgd 	static char errbuf[1024];
672abe3770Sitojun 	char device[MAXNAMLEN];
68d07f9e79Schristos 	const char *ptr;
69232f61faSelad 	int fd, ret;
70d07f9e79Schristos 	struct iovec localiov[32];
714a47153cSchristos 	sigset_t nset;
7261f28255Scgd 	int forked = 0;
73232f61faSelad 	size_t cnt, left, wret;
7461f28255Scgd 
75b48252f3Slukem 	_DIAGASSERT(iov != NULL);
76b48252f3Slukem 	_DIAGASSERT(iovcnt >= 0);
77b48252f3Slukem 	_DIAGASSERT(line != NULL);
78b48252f3Slukem 
79*c5eb4ab6Slukem 	if (iovcnt < 0) {
80*c5eb4ab6Slukem 		(void)snprintf(errbuf, sizeof(errbuf),
81*c5eb4ab6Slukem 		    "%s: negative iovcnt", __func__);
82*c5eb4ab6Slukem 		return errbuf;
83*c5eb4ab6Slukem 	}
84*c5eb4ab6Slukem 
85*c5eb4ab6Slukem 	if ((size_t)iovcnt >= sizeof(localiov) / sizeof(localiov[0])) {
86d07f9e79Schristos 		(void)snprintf(errbuf, sizeof(errbuf),
87d07f9e79Schristos 		    "%s: too many iov's (%d) max is %zu", __func__,
88d07f9e79Schristos 		    iovcnt, sizeof(localiov) / sizeof(localiov[0]));
89d07f9e79Schristos 		return errbuf;
902abe3770Sitojun 	}
91d07f9e79Schristos 
92232f61faSelad 	ptr = strncmp(line, "pts/", (size_t)4) == 0 ? line + 4 : line;
93d07f9e79Schristos 	if (strcspn(ptr, "./") != strlen(ptr)) {
942abe3770Sitojun 		/* A slash or dot is an attempt to break security... */
95d07f9e79Schristos 		(void)snprintf(errbuf, sizeof(errbuf),
96d07f9e79Schristos 		    "%s: '/' or '.' in \"%s\"", __func__, line);
97d07f9e79Schristos 		return errbuf;
98d07f9e79Schristos 	}
99232f61faSelad 	ret = snprintf(device, sizeof(device), "%s%s", _PATH_DEV, line);
100232f61faSelad 	if (ret == -1 || ret >= (int)sizeof(device)) {
101d07f9e79Schristos 		(void) snprintf(errbuf, sizeof(errbuf),
102d07f9e79Schristos 		    "%s: line `%s' too long", __func__, line);
103d07f9e79Schristos 		return errbuf;
104a8bfd3d1Sjtc 	}
105232f61faSelad 	cnt = (size_t)ret;
106a8bfd3d1Sjtc 
10761f28255Scgd 	/*
10861f28255Scgd 	 * open will fail on slip lines or exclusive-use lines
10961f28255Scgd 	 * if not running as root; not an error.
11061f28255Scgd 	 */
11161f28255Scgd 	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
11261f28255Scgd 		if (errno == EBUSY || errno == EACCES)
113d07f9e79Schristos 			return NULL;
11461f28255Scgd 		(void)snprintf(errbuf, sizeof(errbuf),
115d07f9e79Schristos 		    "%s: Cannot open `%s' (%s)",
116d07f9e79Schristos 		    __func__, device, strerror(errno));
117d07f9e79Schristos 		return errbuf;
11861f28255Scgd 	}
1192abe3770Sitojun 	if (!isatty(fd)) {
1202abe3770Sitojun 		(void)snprintf(errbuf, sizeof(errbuf),
121d07f9e79Schristos 		    "%s: line `%s' is not a tty device", __func__, device);
1222abe3770Sitojun 		(void)close(fd);
123d07f9e79Schristos 		return errbuf;
1242abe3770Sitojun 	}
12561f28255Scgd 
126*c5eb4ab6Slukem 	for (cnt = left = 0; cnt < (size_t)iovcnt; ++cnt)
12761f28255Scgd 		left += iov[cnt].iov_len;
12861f28255Scgd 
12961f28255Scgd 	for (;;) {
13061f28255Scgd 		wret = writev(fd, iov, iovcnt);
13161f28255Scgd 		if (wret >= left)
13261f28255Scgd 			break;
133ca7489bfSchristos 		if (wret > 0) {
13461f28255Scgd 			left -= wret;
13561f28255Scgd 			if (iov != localiov) {
136d07f9e79Schristos 				(void)memcpy(localiov, iov,
13761f28255Scgd 				    iovcnt * sizeof(struct iovec));
13861f28255Scgd 				iov = localiov;
13961f28255Scgd 			}
14061f28255Scgd 			for (cnt = 0; wret >= iov->iov_len; ++cnt) {
14161f28255Scgd 				wret -= iov->iov_len;
14261f28255Scgd 				++iov;
14361f28255Scgd 				--iovcnt;
14461f28255Scgd 			}
14561f28255Scgd 			if (wret) {
1461550a564Skleink 				iov->iov_base =
1471550a564Skleink 				    (char *)iov->iov_base + wret;
14861f28255Scgd 				iov->iov_len -= wret;
14961f28255Scgd 			}
15061f28255Scgd 			continue;
151ca7489bfSchristos 		} else if (wret == 0) {
152ca7489bfSchristos 			(void)snprintf(errbuf, sizeof(errbuf),
153232f61faSelad 			    "%s: failed writing %zu bytes to `%s'", __func__,
154ca7489bfSchristos 			    left, device);
155ca7489bfSchristos 			(void) close(fd);
156ca7489bfSchristos 			if (forked)
157ca7489bfSchristos 				_exit(1);
158ca7489bfSchristos 			return errbuf;
15961f28255Scgd 		}
16061f28255Scgd 		if (errno == EWOULDBLOCK) {
161f343a3e8Swiz 			pid_t cpid;
16261f28255Scgd 
16361f28255Scgd 			if (forked) {
16461f28255Scgd 				(void)close(fd);
16561f28255Scgd 				_exit(1);
16661f28255Scgd 			}
16761f28255Scgd 			cpid = fork();
16861f28255Scgd 			if (cpid < 0) {
16961f28255Scgd 				(void)snprintf(errbuf, sizeof(errbuf),
170d07f9e79Schristos 				    "%s: Cannot fork (%s)", __func__,
171d07f9e79Schristos 				    strerror(errno));
17261f28255Scgd 				(void)close(fd);
173d07f9e79Schristos 				return errbuf;
17461f28255Scgd 			}
17561f28255Scgd 			if (cpid) {	/* parent */
17661f28255Scgd 				(void)close(fd);
177d07f9e79Schristos 				return NULL;
17861f28255Scgd 			}
17961f28255Scgd 			forked++;
180a8bfd3d1Sjtc 			/* wait at most tmout seconds */
18161f28255Scgd 			(void)signal(SIGALRM, SIG_DFL);
18261f28255Scgd 			(void)signal(SIGTERM, SIG_DFL); /* XXX */
1834a47153cSchristos 			sigfillset(&nset);
1844a47153cSchristos 			(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
185a8bfd3d1Sjtc 			(void)alarm((u_int)tmout);
1869b087b0bSlukem 			(void)fcntl(fd, F_SETFL, 0);	/* clear O_NONBLOCK */
18761f28255Scgd 			continue;
18861f28255Scgd 		}
18961f28255Scgd 		/*
19061f28255Scgd 		 * We get ENODEV on a slip line if we're running as root,
19161f28255Scgd 		 * and EIO if the line just went away.
19261f28255Scgd 		 */
19361f28255Scgd 		if (errno == ENODEV || errno == EIO)
19461f28255Scgd 			break;
19561f28255Scgd 		(void) close(fd);
19661f28255Scgd 		if (forked)
19761f28255Scgd 			_exit(1);
19861f28255Scgd 		(void)snprintf(errbuf, sizeof(errbuf),
199d07f9e79Schristos 		    "%s: Write to line `%s' failed (%s)", __func__,
200d07f9e79Schristos 		    device, strerror(errno));
201d07f9e79Schristos 		return errbuf;
20261f28255Scgd 	}
20361f28255Scgd 
20461f28255Scgd 	(void) close(fd);
20561f28255Scgd 	if (forked)
20661f28255Scgd 		_exit(0);
207d07f9e79Schristos 	return NULL;
20861f28255Scgd }
209