Home | History | Annotate | Line # | Download | only in libutil
ttymsg.c revision 1.18
      1 /*	$NetBSD: ttymsg.c,v 1.18 2003/08/07 16:45:00 agc Exp $	*/
      2 
      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/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 #if 0
     35 static char sccsid[] = "@(#)ttymsg.c	8.2 (Berkeley) 11/16/93";
     36 #else
     37 __RCSID("$NetBSD: ttymsg.c,v 1.18 2003/08/07 16:45:00 agc Exp $");
     38 #endif
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/uio.h>
     43 
     44 #include <assert.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 <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 #include <util.h>
     55 
     56 /*
     57  * Display the contents of a uio structure on a terminal.  Used by wall(1),
     58  * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
     59  * waiting up to tmout seconds.  Returns pointer to error string on unexpected
     60  * error; string is not newline-terminated.  Various "normal" errors are
     61  * ignored (exclusive-use, lack of permission, etc.).
     62  */
     63 char *
     64 ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
     65 {
     66 	static char errbuf[1024];
     67 	char device[MAXNAMLEN];
     68 	int cnt, fd, left, wret;
     69 	struct iovec localiov[6];
     70 	sigset_t nset;
     71 	int forked = 0;
     72 
     73 	_DIAGASSERT(iov != NULL);
     74 	_DIAGASSERT(iovcnt >= 0);
     75 	_DIAGASSERT(line != NULL);
     76 
     77 	if (iovcnt > sizeof(localiov) / sizeof(localiov[0]))
     78 		return ("too many iov's (change code in libutil/ttymsg.c)");
     79 
     80 	if (strlcpy(device, _PATH_DEV, sizeof(device)) >= sizeof(device) ||
     81 	    strlcat(device, line, sizeof(device)) >= sizeof(device)) {
     82 		(void) snprintf(errbuf, sizeof(errbuf), "%s: path too long",
     83 		    line);
     84 		return (errbuf);
     85 	}
     86 	if (strcspn(line, "./") != strlen(line)) {
     87 		/* A slash or dot is an attempt to break security... */
     88 		(void) snprintf(errbuf, sizeof(errbuf), "'/' or '.' in \"%s\"",
     89 		    line);
     90 		return (errbuf);
     91 	}
     92 
     93 	/*
     94 	 * open will fail on slip lines or exclusive-use lines
     95 	 * if not running as root; not an error.
     96 	 */
     97 	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
     98 		if (errno == EBUSY || errno == EACCES)
     99 			return (NULL);
    100 		(void) snprintf(errbuf, sizeof(errbuf),
    101 		    "%s: %s", device, strerror(errno));
    102 		return (errbuf);
    103 	}
    104 	if (!isatty(fd)) {
    105 		(void) snprintf(errbuf, sizeof(errbuf),
    106 		    "%s: not a tty device", device);
    107 		(void) close(fd);
    108 		return (errbuf);
    109 	}
    110 
    111 	for (cnt = left = 0; cnt < iovcnt; ++cnt)
    112 		left += iov[cnt].iov_len;
    113 
    114 	for (;;) {
    115 		wret = writev(fd, iov, iovcnt);
    116 		if (wret >= left)
    117 			break;
    118 		if (wret >= 0) {
    119 			left -= wret;
    120 			if (iov != localiov) {
    121 				memcpy(localiov, iov,
    122 				    iovcnt * sizeof(struct iovec));
    123 				iov = localiov;
    124 			}
    125 			for (cnt = 0; wret >= iov->iov_len; ++cnt) {
    126 				wret -= iov->iov_len;
    127 				++iov;
    128 				--iovcnt;
    129 			}
    130 			if (wret) {
    131 				iov->iov_base =
    132 				    (char *)iov->iov_base + wret;
    133 				iov->iov_len -= wret;
    134 			}
    135 			continue;
    136 		}
    137 		if (errno == EWOULDBLOCK) {
    138 			int cpid;
    139 
    140 			if (forked) {
    141 				(void) close(fd);
    142 				_exit(1);
    143 			}
    144 			cpid = fork();
    145 			if (cpid < 0) {
    146 				(void) snprintf(errbuf, sizeof(errbuf),
    147 				    "fork: %s", strerror(errno));
    148 				(void) close(fd);
    149 				return (errbuf);
    150 			}
    151 			if (cpid) {	/* parent */
    152 				(void) close(fd);
    153 				return (NULL);
    154 			}
    155 			forked++;
    156 			/* wait at most tmout seconds */
    157 			(void) signal(SIGALRM, SIG_DFL);
    158 			(void) signal(SIGTERM, SIG_DFL); /* XXX */
    159 			sigfillset(&nset);
    160 			(void) sigprocmask(SIG_UNBLOCK, &nset, NULL);
    161 			(void) alarm((u_int)tmout);
    162 			(void) fcntl(fd, F_SETFL, 0);	/* clear O_NONBLOCK */
    163 			continue;
    164 		}
    165 		/*
    166 		 * We get ENODEV on a slip line if we're running as root,
    167 		 * and EIO if the line just went away.
    168 		 */
    169 		if (errno == ENODEV || errno == EIO)
    170 			break;
    171 		(void) close(fd);
    172 		if (forked)
    173 			_exit(1);
    174 		(void) snprintf(errbuf, sizeof(errbuf),
    175 		    "%s: %s", device, strerror(errno));
    176 		return (errbuf);
    177 	}
    178 
    179 	(void) close(fd);
    180 	if (forked)
    181 		_exit(0);
    182 	return (NULL);
    183 }
    184