1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * 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 #ifndef lint 35 /*static char sccsid[] = "from: @(#)ttymsg.c 5.8 (Berkeley) 7/1/91";*/ 36 static char rcsid[] = "$Id: ttymsg.c,v 1.2 1993/08/01 18:03:09 mycroft Exp $"; 37 #endif /* not lint */ 38 39 #include <sys/types.h> 40 #include <sys/uio.h> 41 #include <signal.h> 42 #include <fcntl.h> 43 #include <dirent.h> 44 #include <errno.h> 45 #include <paths.h> 46 #include <unistd.h> 47 #include <stdio.h> 48 #include <string.h> 49 #include <stdlib.h> 50 51 /* 52 * Display the contents of a uio structure on a terminal. Used by wall(1) 53 * and syslogd(8). Forks and finishes in child if write would block, waiting 54 * at most five minutes. Returns pointer to error string on unexpected error; 55 * string is not newline-terminated. Various "normal" errors are ignored 56 * (exclusive-use, lack of permission, etc.). 57 */ 58 char * 59 ttymsg(iov, iovcnt, line) 60 struct iovec *iov; 61 int iovcnt; 62 char *line; 63 { 64 static char device[MAXNAMLEN] = _PATH_DEV; 65 static char errbuf[1024]; 66 register int cnt, fd, left, wret; 67 struct iovec localiov[6]; 68 int forked = 0; 69 70 if (iovcnt > 6) 71 return ("too many iov's (change code in wall/ttymsg.c)"); 72 /* 73 * open will fail on slip lines or exclusive-use lines 74 * if not running as root; not an error. 75 */ 76 (void) strcpy(device + sizeof(_PATH_DEV) - 1, line); 77 if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) { 78 if (errno == EBUSY || errno == EACCES) 79 return (NULL); 80 (void) snprintf(errbuf, sizeof(errbuf), 81 "%s: %s", device, strerror(errno)); 82 return (errbuf); 83 } 84 85 for (cnt = left = 0; cnt < iovcnt; ++cnt) 86 left += iov[cnt].iov_len; 87 88 for (;;) { 89 wret = writev(fd, iov, iovcnt); 90 if (wret >= left) 91 break; 92 if (wret >= 0) { 93 left -= wret; 94 if (iov != localiov) { 95 bcopy(iov, localiov, 96 iovcnt * sizeof(struct iovec)); 97 iov = localiov; 98 } 99 for (cnt = 0; wret >= iov->iov_len; ++cnt) { 100 wret -= iov->iov_len; 101 ++iov; 102 --iovcnt; 103 } 104 if (wret) { 105 iov->iov_base += wret; 106 iov->iov_len -= wret; 107 } 108 continue; 109 } 110 if (errno == EWOULDBLOCK) { 111 int cpid, off = 0; 112 113 if (forked) { 114 (void) close(fd); 115 _exit(1); 116 } 117 cpid = fork(); 118 if (cpid < 0) { 119 (void) snprintf(errbuf, sizeof(errbuf), 120 "fork: %s", strerror(errno)); 121 (void) close(fd); 122 return (errbuf); 123 } 124 if (cpid) { /* parent */ 125 (void) close(fd); 126 return (NULL); 127 } 128 forked++; 129 /* wait at most 5 minutes */ 130 (void) signal(SIGALRM, SIG_DFL); 131 (void) signal(SIGTERM, SIG_DFL); /* XXX */ 132 (void) sigsetmask(0); 133 (void) alarm((u_int)(60 * 5)); 134 (void) fcntl(fd, O_NONBLOCK, &off); 135 continue; 136 } 137 /* 138 * We get ENODEV on a slip line if we're running as root, 139 * and EIO if the line just went away. 140 */ 141 if (errno == ENODEV || errno == EIO) 142 break; 143 (void) close(fd); 144 if (forked) 145 _exit(1); 146 (void) snprintf(errbuf, sizeof(errbuf), 147 "%s: %s", device, strerror(errno)); 148 return (errbuf); 149 } 150 151 (void) close(fd); 152 if (forked) 153 _exit(0); 154 return (NULL); 155 } 156