ttymsg.c revision 200420
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35
36__FBSDID("$FreeBSD: head/usr.bin/wall/ttymsg.c 200420 2009-12-11 23:35:38Z delphij $");
37
38#ifndef lint
39static const char sccsid[] = "@(#)ttymsg.c	8.2 (Berkeley) 11/16/93";
40#endif
41
42#include <sys/types.h>
43#include <sys/uio.h>
44#include <dirent.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <paths.h>
48#include <signal.h>
49#include <stdio.h>
50#include <string.h>
51#include <unistd.h>
52
53#include "ttymsg.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 */
62const char *
63ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
64{
65	struct iovec localiov[7];
66	ssize_t left, wret;
67	int cnt, fd;
68	static char device[MAXNAMLEN] = _PATH_DEV;
69	static char errbuf[1024];
70	char *p;
71	int forked;
72
73	forked = 0;
74	if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0])))
75		return ("too many iov's (change code in wall/ttymsg.c)");
76
77	p = device + sizeof(_PATH_DEV) - 1;
78	strlcpy(p, line, sizeof(device));
79	if (strncmp(p, "pts/", 4) == 0)
80		p += 4;
81	if (strchr(p, '/') != NULL) {
82		/* A slash is an attempt to break security... */
83		(void) snprintf(errbuf, sizeof(errbuf),
84		    "Too many '/' in \"%s\"", device);
85		return (errbuf);
86	}
87
88	/*
89	 * open will fail on slip lines or exclusive-use lines
90	 * if not running as root; not an error.
91	 */
92	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
93		if (errno == EBUSY || errno == EACCES)
94			return (NULL);
95		(void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
96		    strerror(errno));
97		return (errbuf);
98	}
99
100	for (cnt = 0, left = 0; cnt < iovcnt; ++cnt)
101		left += iov[cnt].iov_len;
102
103	for (;;) {
104		wret = writev(fd, iov, iovcnt);
105		if (wret >= left)
106			break;
107		if (wret >= 0) {
108			left -= wret;
109			if (iov != localiov) {
110				bcopy(iov, localiov,
111				    iovcnt * sizeof(struct iovec));
112				iov = localiov;
113			}
114			for (cnt = 0; (size_t)wret >= iov->iov_len; ++cnt) {
115				wret -= iov->iov_len;
116				++iov;
117				--iovcnt;
118			}
119			if (wret) {
120				iov->iov_base = (char *)iov->iov_base + wret;
121				iov->iov_len -= wret;
122			}
123			continue;
124		}
125		if (errno == EWOULDBLOCK) {
126			int cpid;
127
128			if (forked) {
129				(void) close(fd);
130				_exit(1);
131			}
132			cpid = fork();
133			if (cpid < 0) {
134				(void) snprintf(errbuf, sizeof(errbuf),
135				    "fork: %s", strerror(errno));
136				(void) close(fd);
137				return (errbuf);
138			}
139			if (cpid) {	/* parent */
140				(void) close(fd);
141				return (NULL);
142			}
143			forked++;
144			/* wait at most tmout seconds */
145			(void) signal(SIGALRM, SIG_DFL);
146			(void) signal(SIGTERM, SIG_DFL); /* XXX */
147			(void) sigsetmask(0);
148			(void) alarm((u_int)tmout);
149			(void) fcntl(fd, F_SETFL, 0);	/* clear O_NONBLOCK */
150			continue;
151		}
152		/*
153		 * We get ENODEV on a slip line if we're running as root,
154		 * and EIO if the line just went away.
155		 */
156		if (errno == ENODEV || errno == EIO)
157			break;
158		(void) close(fd);
159		if (forked)
160			_exit(1);
161		(void) snprintf(errbuf, sizeof(errbuf),
162		    "%s: %s", device, strerror(errno));
163		return (errbuf);
164	}
165
166	(void) close(fd);
167	if (forked)
168		_exit(0);
169	return (NULL);
170}
171