xref: /freebsd/usr.bin/wall/ttymsg.c (revision be9f35396449129ebe952fdd1c080f7856dee030)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/uio.h>
35 
36 #include <dirent.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <paths.h>
40 #include <signal.h>
41 #include <stdbool.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 
47 #include "ttymsg.h"
48 
49 /*
50  * Display the contents of a uio structure on a terminal.  If shout is
51  * non-zero, do so even if the terminal has messages disabled.  Used by
52  * wall(1), syslogd(8), and talkd(8).  Forks and finishes in child if
53  * write would block, waiting up to timeout seconds.  Various "normal"
54  * errors are ignored (exclusive-use, lack of permission, etc.).
55  */
56 int
57 ttymsg(struct iovec *iov, int iovcnt, const char *tty, int timeout,
58     bool shout)
59 {
60 	struct iovec localiov[TTYMSG_IOV_MAX];
61 	struct stat sb;
62 	ssize_t wret;
63 	size_t resid;
64 	int cnt, dd, fd, serrno;
65 	int forked;
66 
67 	forked = 0;
68 	if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0]))) {
69 		errno = EFBIG;
70 		return (-1);
71 	}
72 
73 	dd = open(_PATH_DEV, O_SEARCH | O_DIRECTORY);
74 	if (dd < 0)
75 		return (-1);
76 	fd = openat(dd, tty, O_WRONLY | O_NONBLOCK | O_RESOLVE_BENEATH);
77 	if (fd < 0) {
78 		serrno = errno;
79 		close(dd);
80 		/*
81 		 * open will fail on slip lines or exclusive-use lines
82 		 * if not running as root; not an error.
83 		 */
84 		if (serrno == EBUSY || serrno == EACCES)
85 			return (0);
86 		errno = serrno;
87 		return (-1);
88 	}
89 	close(dd);
90 	if (!shout) {
91 		if (fstat(fd, &sb) != 0) {
92 			serrno = errno;
93 			close(fd);
94 			errno = serrno;
95 			return (-1);
96 		}
97 		if ((sb.st_mode & S_IWGRP) == 0) {
98 			close(fd);
99 			return (0);
100 		}
101 	}
102 
103 	for (cnt = 0, resid = 0; cnt < iovcnt; ++cnt)
104 		resid += iov[cnt].iov_len;
105 
106 	do {
107 		wret = writev(fd, iov, iovcnt);
108 		if (wret >= 0) {
109 			resid -= wret;
110 			if (iov != localiov) {
111 				bcopy(iov, localiov,
112 				    iovcnt * sizeof(struct iovec));
113 				iov = localiov;
114 			}
115 			while ((size_t)wret >= iov->iov_len) {
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 				serrno = errno;
136 				(void) close(fd);
137 				errno = serrno;
138 				return (-1);
139 			}
140 			if (cpid) {	/* parent */
141 				(void) close(fd);
142 				return (0);
143 			}
144 			forked++;
145 			/* wait at most timeout seconds */
146 			(void) signal(SIGALRM, SIG_DFL);
147 			(void) signal(SIGTERM, SIG_DFL); /* XXX */
148 			(void) sigsetmask(0);
149 			(void) alarm((u_int)timeout);
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 		serrno = errno;
158 		if (serrno == ENODEV || serrno == EIO)
159 			break;
160 		(void) close(fd);
161 		if (forked)
162 			_exit(1);
163 		errno = serrno;
164 		return (-1);
165 	} while (resid > 0);
166 
167 	(void) close(fd);
168 	if (forked)
169 		_exit(0);
170 	return (0);
171 }
172