ttymsg.c revision 200462
172445Sassar/*
2233294Sstas * Copyright (c) 1989, 1993
3233294Sstas *	The Regents of the University of California.  All rights reserved.
4233294Sstas *
572445Sassar * Redistribution and use in source and binary forms, with or without
6233294Sstas * modification, are permitted provided that the following conditions
7233294Sstas * are met:
8233294Sstas * 1. Redistributions of source code must retain the above copyright
972445Sassar *    notice, this list of conditions and the following disclaimer.
10233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer in the
1272445Sassar *    documentation and/or other materials provided with the distribution.
13233294Sstas * 3. All advertising materials mentioning features or use of this software
14233294Sstas *    must display the following acknowledgement:
15233294Sstas *	This product includes software developed by the University of
1672445Sassar *	California, Berkeley and its contributors.
17233294Sstas * 4. Neither the name of the University nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
2072445Sassar *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
3272445Sassar */
3372445Sassar
34233294Sstas#include <sys/cdefs.h>
3572445Sassar
3672445Sassar__FBSDID("$FreeBSD: head/usr.bin/wall/ttymsg.c 200462 2009-12-13 03:14:06Z delphij $");
3772445Sassar
3872445Sassar#ifndef lint
3972445Sassarstatic const char sccsid[] = "@(#)ttymsg.c	8.2 (Berkeley) 11/16/93";
4072445Sassar#endif
4172445Sassar
42178825Sdfr#include <sys/types.h>
4372445Sassar#include <sys/uio.h>
4478527Sassar#include <dirent.h>
4578527Sassar#include <errno.h>
4678527Sassar#include <fcntl.h>
4778527Sassar#include <paths.h>
48233294Sstas#include <signal.h>
4972445Sassar#include <stdio.h>
50233294Sstas#include <string.h>
51233294Sstas#include <stdlib.h>
5272445Sassar#include <unistd.h>
53233294Sstas
54233294Sstas#include "ttymsg.h"
55233294Sstas
56233294Sstas/*
57233294Sstas * Display the contents of a uio structure on a terminal.  Used by wall(1),
5878527Sassar * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
5972445Sassar * waiting up to tmout seconds.  Returns pointer to error string on unexpected
60233294Sstas * error; string is not newline-terminated.  Various "normal" errors are
61233294Sstas * ignored (exclusive-use, lack of permission, etc.).
6278527Sassar */
6378527Sassarconst char *
64233294Sstasttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
65233294Sstas{
6672445Sassar	struct iovec localiov[7];
67233294Sstas	ssize_t left, wret;
68233294Sstas	int cnt, fd;
6972445Sassar	static char device[MAXNAMLEN] = _PATH_DEV;
70233294Sstas	static char errbuf[1024];
71233294Sstas	char *p;
7278527Sassar	int forked;
73233294Sstas
74233294Sstas	forked = 0;
75233294Sstas	if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0])))
7672445Sassar		return ("too many iov's (change code in wall/ttymsg.c)");
77
78	p = device + sizeof(_PATH_DEV) - 1;
79	strlcpy(p, line, sizeof(device));
80	if (strncmp(p, "pts/", 4) == 0)
81		p += 4;
82	if (strchr(p, '/') != NULL) {
83		/* A slash is an attempt to break security... */
84		(void) snprintf(errbuf, sizeof(errbuf),
85		    "Too many '/' in \"%s\"", device);
86		return (errbuf);
87	}
88
89	/*
90	 * open will fail on slip lines or exclusive-use lines
91	 * if not running as root; not an error.
92	 */
93	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
94		if (errno == EBUSY || errno == EACCES)
95			return (NULL);
96		(void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
97		    strerror(errno));
98		return (errbuf);
99	}
100
101	for (cnt = 0, left = 0; cnt < iovcnt; ++cnt)
102		left += iov[cnt].iov_len;
103
104	for (;;) {
105		wret = writev(fd, iov, iovcnt);
106		if (wret >= left)
107			break;
108		if (wret >= 0) {
109			left -= wret;
110			if (iov != localiov) {
111				bcopy(iov, localiov,
112				    iovcnt * sizeof(struct iovec));
113				iov = localiov;
114			}
115			for (cnt = 0; (size_t)wret >= iov->iov_len; ++cnt) {
116				wret -= iov->iov_len;
117				++iov;
118				--iovcnt;
119			}
120			if (wret) {
121				iov->iov_base = (char *)iov->iov_base + wret;
122				iov->iov_len -= wret;
123			}
124			continue;
125		}
126		if (errno == EWOULDBLOCK) {
127			int cpid;
128
129			if (forked) {
130				(void) close(fd);
131				_exit(1);
132			}
133			cpid = fork();
134			if (cpid < 0) {
135				(void) snprintf(errbuf, sizeof(errbuf),
136				    "fork: %s", strerror(errno));
137				(void) close(fd);
138				return (errbuf);
139			}
140			if (cpid) {	/* parent */
141				(void) close(fd);
142				return (NULL);
143			}
144			forked++;
145			/* wait at most tmout seconds */
146			(void) signal(SIGALRM, SIG_DFL);
147			(void) signal(SIGTERM, SIG_DFL); /* XXX */
148			(void) sigsetmask(0);
149			(void) alarm((u_int)tmout);
150			(void) fcntl(fd, F_SETFL, 0);	/* clear O_NONBLOCK */
151			continue;
152		}
153		/*
154		 * We get ENODEV on a slip line if we're running as root,
155		 * and EIO if the line just went away.
156		 */
157		if (errno == ENODEV || errno == EIO)
158			break;
159		(void) close(fd);
160		if (forked)
161			_exit(1);
162		(void) snprintf(errbuf, sizeof(errbuf),
163		    "%s: %s", device, strerror(errno));
164		return (errbuf);
165	}
166
167	(void) close(fd);
168	if (forked)
169		_exit(0);
170	return (NULL);
171}
172