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