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: src/usr.bin/wall/ttymsg.c,v 1.12 2006/01/27 08:52:14 ume Exp $"); 37 38#ifndef lint 39__attribute__((__used__)) 40static const char sccsid[] = "@(#)ttymsg.c 8.2 (Berkeley) 11/16/93"; 41#endif 42 43#include <sys/types.h> 44#include <sys/uio.h> 45#include <dirent.h> 46#include <errno.h> 47#include <fcntl.h> 48#include <paths.h> 49#include <signal.h> 50#include <stdio.h> 51#include <string.h> 52#include <stdlib.h> 53#include <unistd.h> 54 55#include "ttymsg.h" 56 57/* 58 * Display the contents of a uio structure on a terminal. Used by wall(1), 59 * syslogd(8), and talkd(8). Forks and finishes in child if write would block, 60 * waiting up to tmout seconds. Returns pointer to error string on unexpected 61 * error; string is not newline-terminated. Various "normal" errors are 62 * ignored (exclusive-use, lack of permission, etc.). 63 */ 64const char * 65ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout) 66{ 67 struct iovec localiov[7]; 68 ssize_t left, wret; 69 int cnt, fd; 70 static char device[MAXNAMLEN] = _PATH_DEV; 71 static char errbuf[1024]; 72 char *p; 73 int forked; 74 75 forked = 0; 76 if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0]))) 77 return ("too many iov's (change code in wall/ttymsg.c)"); 78 79 p = device + sizeof(_PATH_DEV) - 1; 80#ifdef __APPLE__ 81 /* radar:13145432 */ 82 strlcpy(p, line, sizeof(device) - (sizeof(_PATH_DEV) - 1)); 83#else 84 strlcpy(p, line, sizeof(device)); 85#endif 86 if (strncmp(p, "pts/", 4) == 0) 87 p += 4; 88 if (strchr(p, '/') != NULL) { 89 /* A slash is an attempt to break security... */ 90 (void) snprintf(errbuf, sizeof(errbuf), 91 "Too many '/' in \"%s\"", device); 92 return (errbuf); 93 } 94 95 /* 96 * open will fail on slip lines or exclusive-use lines 97 * if not running as root; not an error. 98 */ 99 if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) { 100 if (errno == EBUSY || errno == EACCES) 101 return (NULL); 102 (void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device, 103 strerror(errno)); 104 return (errbuf); 105 } 106 107 for (cnt = 0, left = 0; cnt < iovcnt; ++cnt) 108 left += iov[cnt].iov_len; 109 110 for (;;) { 111 wret = writev(fd, iov, iovcnt); 112 if (wret >= left) 113 break; 114 if (wret >= 0) { 115 left -= wret; 116 if (iov != localiov) { 117 bcopy(iov, localiov, 118 iovcnt * sizeof(struct iovec)); 119 iov = localiov; 120 } 121 for (cnt = 0; (size_t)wret >= iov->iov_len; ++cnt) { 122 wret -= iov->iov_len; 123 ++iov; 124 --iovcnt; 125 } 126 if (wret) { 127 iov->iov_base = (char *)iov->iov_base + wret; 128 iov->iov_len -= wret; 129 } 130 continue; 131 } 132 if (errno == EWOULDBLOCK) { 133 int cpid; 134 135 if (forked) { 136 (void) close(fd); 137 _exit(1); 138 } 139 cpid = fork(); 140 if (cpid < 0) { 141 (void) snprintf(errbuf, sizeof(errbuf), 142 "fork: %s", strerror(errno)); 143 (void) close(fd); 144 return (errbuf); 145 } 146 if (cpid) { /* parent */ 147 (void) close(fd); 148 return (NULL); 149 } 150 forked++; 151 /* wait at most tmout seconds */ 152 (void) signal(SIGALRM, SIG_DFL); 153 (void) signal(SIGTERM, SIG_DFL); /* XXX */ 154 (void) sigsetmask(0); 155 (void) alarm((u_int)tmout); 156 (void) fcntl(fd, F_SETFL, 0); /* clear O_NONBLOCK */ 157 continue; 158 } 159 /* 160 * We get ENODEV on a slip line if we're running as root, 161 * and EIO if the line just went away. 162 */ 163 if (errno == ENODEV || errno == EIO) 164 break; 165 (void) close(fd); 166 if (forked) 167 _exit(1); 168 (void) snprintf(errbuf, sizeof(errbuf), 169 "%s: %s", device, strerror(errno)); 170 return (errbuf); 171 } 172 173 (void) close(fd); 174 if (forked) 175 _exit(0); 176 return (NULL); 177} 178