ttymsg.c revision 286269
1241675Suqs/*
2241675Suqs * Copyright (c) 1989, 1993
3241675Suqs *	The Regents of the University of California.  All rights reserved.
4241675Suqs *
5241675Suqs * Redistribution and use in source and binary forms, with or without
6241675Suqs * modification, are permitted provided that the following conditions
7241675Suqs * are met:
8241675Suqs * 1. Redistributions of source code must retain the above copyright
9241675Suqs *    notice, this list of conditions and the following disclaimer.
10241675Suqs * 2. Redistributions in binary form must reproduce the above copyright
11241675Suqs *    notice, this list of conditions and the following disclaimer in the
12241675Suqs *    documentation and/or other materials provided with the distribution.
13241675Suqs * 4. Neither the name of the University nor the names of its contributors
14241675Suqs *    may be used to endorse or promote products derived from this software
15241675Suqs *    without specific prior written permission.
16241675Suqs *
17241675Suqs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18241675Suqs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19241675Suqs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20241675Suqs * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21241675Suqs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22241675Suqs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23241675Suqs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24241675Suqs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25241675Suqs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26241675Suqs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27241675Suqs * SUCH DAMAGE.
28241675Suqs */
29241675Suqs
30241675Suqs#include <sys/cdefs.h>
31241675Suqs
32241675Suqs__FBSDID("$FreeBSD: stable/10/usr.bin/wall/ttymsg.c 286269 2015-08-04 03:06:23Z pfg $");
33241675Suqs
34241675Suqs#ifndef lint
35241675Suqsstatic const char sccsid[] = "@(#)ttymsg.c	8.2 (Berkeley) 11/16/93";
36241675Suqs#endif
37241675Suqs
38241675Suqs#include <sys/types.h>
39241675Suqs#include <sys/uio.h>
40241675Suqs#include <dirent.h>
41241675Suqs#include <errno.h>
42241675Suqs#include <fcntl.h>
43241675Suqs#include <paths.h>
44241675Suqs#include <signal.h>
45241675Suqs#include <stdio.h>
46241675Suqs#include <string.h>
47241675Suqs#include <stdlib.h>
48241675Suqs#include <unistd.h>
49241675Suqs
50241675Suqs#include "ttymsg.h"
51241675Suqs
52241675Suqs/*
53241675Suqs * Display the contents of a uio structure on a terminal.  Used by wall(1),
54241675Suqs * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
55241675Suqs * waiting up to tmout seconds.  Returns pointer to error string on unexpected
56241675Suqs * error; string is not newline-terminated.  Various "normal" errors are
57241675Suqs * ignored (exclusive-use, lack of permission, etc.).
58241675Suqs */
59241675Suqsconst char *
60241675Suqsttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
61241675Suqs{
62241675Suqs	struct iovec localiov[7];
63241675Suqs	ssize_t left, wret;
64241675Suqs	int cnt, fd;
65241675Suqs	char device[MAXNAMLEN] = _PATH_DEV;
66241675Suqs	static char errbuf[1024];
67241675Suqs	char *p;
68241675Suqs	int forked;
69241675Suqs
70241675Suqs	forked = 0;
71241675Suqs	if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0])))
72241675Suqs		return ("too many iov's (change code in wall/ttymsg.c)");
73241675Suqs
74241675Suqs	strlcat(device, line, sizeof(device));
75241675Suqs	p = device + sizeof(_PATH_DEV) - 1;
76241675Suqs	if (strncmp(p, "pts/", 4) == 0)
77241675Suqs		p += 4;
78241675Suqs	if (strchr(p, '/') != NULL) {
79241675Suqs		/* A slash is an attempt to break security... */
80241675Suqs		(void) snprintf(errbuf, sizeof(errbuf),
81241675Suqs		    "Too many '/' in \"%s\"", device);
82241675Suqs		return (errbuf);
83241675Suqs	}
84241675Suqs
85241675Suqs	/*
86241675Suqs	 * open will fail on slip lines or exclusive-use lines
87241675Suqs	 * if not running as root; not an error.
88241675Suqs	 */
89241675Suqs	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
90241675Suqs		if (errno == EBUSY || errno == EACCES)
91241675Suqs			return (NULL);
92241675Suqs		(void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
93241675Suqs		    strerror(errno));
94241675Suqs		return (errbuf);
95	}
96
97	for (cnt = 0, left = 0; cnt < iovcnt; ++cnt)
98		left += iov[cnt].iov_len;
99
100	for (;;) {
101		wret = writev(fd, iov, iovcnt);
102		if (wret >= left)
103			break;
104		if (wret >= 0) {
105			left -= wret;
106			if (iov != localiov) {
107				bcopy(iov, localiov,
108				    iovcnt * sizeof(struct iovec));
109				iov = localiov;
110			}
111			for (cnt = 0; (size_t)wret >= iov->iov_len; ++cnt) {
112				wret -= iov->iov_len;
113				++iov;
114				--iovcnt;
115			}
116			if (wret) {
117				iov->iov_base = (char *)iov->iov_base + wret;
118				iov->iov_len -= wret;
119			}
120			continue;
121		}
122		if (errno == EWOULDBLOCK) {
123			int cpid;
124
125			if (forked) {
126				(void) close(fd);
127				_exit(1);
128			}
129			cpid = fork();
130			if (cpid < 0) {
131				(void) snprintf(errbuf, sizeof(errbuf),
132				    "fork: %s", strerror(errno));
133				(void) close(fd);
134				return (errbuf);
135			}
136			if (cpid) {	/* parent */
137				(void) close(fd);
138				return (NULL);
139			}
140			forked++;
141			/* wait at most tmout seconds */
142			(void) signal(SIGALRM, SIG_DFL);
143			(void) signal(SIGTERM, SIG_DFL); /* XXX */
144			(void) sigsetmask(0);
145			(void) alarm((u_int)tmout);
146			(void) fcntl(fd, F_SETFL, 0);	/* clear O_NONBLOCK */
147			continue;
148		}
149		/*
150		 * We get ENODEV on a slip line if we're running as root,
151		 * and EIO if the line just went away.
152		 */
153		if (errno == ENODEV || errno == EIO)
154			break;
155		(void) close(fd);
156		if (forked)
157			_exit(1);
158		(void) snprintf(errbuf, sizeof(errbuf),
159		    "%s: %s", device, strerror(errno));
160		return (errbuf);
161	}
162
163	(void) close(fd);
164	if (forked)
165		_exit(0);
166	return (NULL);
167}
168