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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31
32__FBSDID("$FreeBSD: stable/11/usr.bin/wall/ttymsg.c 335059 2018-06-13 13:41:23Z ed $");
33
34#ifndef lint
35static const char sccsid[] = "@(#)ttymsg.c	8.2 (Berkeley) 11/16/93";
36#endif
37
38#include <sys/types.h>
39#include <sys/uio.h>
40#include <dirent.h>
41#include <errno.h>
42#include <fcntl.h>
43#include <paths.h>
44#include <signal.h>
45#include <stdio.h>
46#include <string.h>
47#include <stdlib.h>
48#include <unistd.h>
49
50#include "ttymsg.h"
51
52/*
53 * Display the contents of a uio structure on a terminal.  Used by wall(1),
54 * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
55 * waiting up to tmout seconds.  Returns pointer to error string on unexpected
56 * error; string is not newline-terminated.  Various "normal" errors are
57 * ignored (exclusive-use, lack of permission, etc.).
58 */
59const char *
60ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
61{
62	struct iovec localiov[TTYMSG_IOV_MAX];
63	ssize_t left, wret;
64	int cnt, fd;
65	char device[MAXNAMLEN] = _PATH_DEV;
66	static char errbuf[1024];
67	char *p;
68	int forked;
69
70	forked = 0;
71	if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0])))
72		return ("too many iov's (change code in wall/ttymsg.c)");
73
74	strlcat(device, line, sizeof(device));
75	p = device + sizeof(_PATH_DEV) - 1;
76	if (strncmp(p, "pts/", 4) == 0)
77		p += 4;
78	if (strchr(p, '/') != NULL) {
79		/* A slash is an attempt to break security... */
80		(void) snprintf(errbuf, sizeof(errbuf),
81		    "Too many '/' in \"%s\"", device);
82		return (errbuf);
83	}
84
85	/*
86	 * open will fail on slip lines or exclusive-use lines
87	 * if not running as root; not an error.
88	 */
89	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
90		if (errno == EBUSY || errno == EACCES)
91			return (NULL);
92		(void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
93		    strerror(errno));
94		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