Home | History | Annotate | Line # | Download | only in libutil
ttymsg.c revision 1.22
      1 /*	$NetBSD: ttymsg.c,v 1.22 2005/08/27 17:16:06 elad 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.22 2005/08/27 17:16:06 elad 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 	const char *ptr;
     69 	int fd, ret;
     70 	struct iovec localiov[32];
     71 	sigset_t nset;
     72 	int forked = 0;
     73 	size_t cnt, left, wret;
     74 
     75 	_DIAGASSERT(iov != NULL);
     76 	_DIAGASSERT(iovcnt >= 0);
     77 	_DIAGASSERT(line != NULL);
     78 
     79 	if (iovcnt >= sizeof(localiov) / sizeof(localiov[0])) {
     80 		(void)snprintf(errbuf, sizeof(errbuf),
     81 		    "%s: too many iov's (%d) max is %zu", __func__,
     82 		    iovcnt, sizeof(localiov) / sizeof(localiov[0]));
     83 		return errbuf;
     84 	}
     85 
     86 	ptr = strncmp(line, "pts/", (size_t)4) == 0 ? line + 4 : line;
     87 	if (strcspn(ptr, "./") != strlen(ptr)) {
     88 		/* A slash or dot is an attempt to break security... */
     89 		(void)snprintf(errbuf, sizeof(errbuf),
     90 		    "%s: '/' or '.' in \"%s\"", __func__, line);
     91 		return errbuf;
     92 	}
     93 	ret = snprintf(device, sizeof(device), "%s%s", _PATH_DEV, line);
     94 	if (ret == -1 || ret >= (int)sizeof(device)) {
     95 		(void) snprintf(errbuf, sizeof(errbuf),
     96 		    "%s: line `%s' too long", __func__, line);
     97 		return errbuf;
     98 	}
     99 	cnt = (size_t)ret;
    100 
    101 	/*
    102 	 * open will fail on slip lines or exclusive-use lines
    103 	 * if not running as root; not an error.
    104 	 */
    105 	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
    106 		if (errno == EBUSY || errno == EACCES)
    107 			return NULL;
    108 		(void)snprintf(errbuf, sizeof(errbuf),
    109 		    "%s: Cannot open `%s' (%s)",
    110 		    __func__, device, strerror(errno));
    111 		return errbuf;
    112 	}
    113 	if (!isatty(fd)) {
    114 		(void)snprintf(errbuf, sizeof(errbuf),
    115 		    "%s: line `%s' is not a tty device", __func__, device);
    116 		(void)close(fd);
    117 		return errbuf;
    118 	}
    119 
    120 	for (cnt = left = 0; cnt < iovcnt; ++cnt)
    121 		left += iov[cnt].iov_len;
    122 
    123 	for (;;) {
    124 		wret = writev(fd, iov, iovcnt);
    125 		if (wret >= left)
    126 			break;
    127 		if (wret > 0) {
    128 			left -= wret;
    129 			if (iov != localiov) {
    130 				(void)memcpy(localiov, iov,
    131 				    iovcnt * sizeof(struct iovec));
    132 				iov = localiov;
    133 			}
    134 			for (cnt = 0; wret >= iov->iov_len; ++cnt) {
    135 				wret -= iov->iov_len;
    136 				++iov;
    137 				--iovcnt;
    138 			}
    139 			if (wret) {
    140 				iov->iov_base =
    141 				    (char *)iov->iov_base + wret;
    142 				iov->iov_len -= wret;
    143 			}
    144 			continue;
    145 		} else if (wret == 0) {
    146 			(void)snprintf(errbuf, sizeof(errbuf),
    147 			    "%s: failed writing %zu bytes to `%s'", __func__,
    148 			    left, device);
    149 			(void) close(fd);
    150 			if (forked)
    151 				_exit(1);
    152 			return errbuf;
    153 		}
    154 		if (errno == EWOULDBLOCK) {
    155 			pid_t cpid;
    156 
    157 			if (forked) {
    158 				(void)close(fd);
    159 				_exit(1);
    160 			}
    161 			cpid = fork();
    162 			if (cpid < 0) {
    163 				(void)snprintf(errbuf, sizeof(errbuf),
    164 				    "%s: Cannot fork (%s)", __func__,
    165 				    strerror(errno));
    166 				(void)close(fd);
    167 				return errbuf;
    168 			}
    169 			if (cpid) {	/* parent */
    170 				(void)close(fd);
    171 				return NULL;
    172 			}
    173 			forked++;
    174 			/* wait at most tmout seconds */
    175 			(void)signal(SIGALRM, SIG_DFL);
    176 			(void)signal(SIGTERM, SIG_DFL); /* XXX */
    177 			sigfillset(&nset);
    178 			(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
    179 			(void)alarm((u_int)tmout);
    180 			(void)fcntl(fd, F_SETFL, 0);	/* clear O_NONBLOCK */
    181 			continue;
    182 		}
    183 		/*
    184 		 * We get ENODEV on a slip line if we're running as root,
    185 		 * and EIO if the line just went away.
    186 		 */
    187 		if (errno == ENODEV || errno == EIO)
    188 			break;
    189 		(void) close(fd);
    190 		if (forked)
    191 			_exit(1);
    192 		(void)snprintf(errbuf, sizeof(errbuf),
    193 		    "%s: Write to line `%s' failed (%s)", __func__,
    194 		    device, strerror(errno));
    195 		return errbuf;
    196 	}
    197 
    198 	(void) close(fd);
    199 	if (forked)
    200 		_exit(0);
    201 	return NULL;
    202 }
    203