1 /* $NetBSD: ttymsg.c,v 1.23 2009/01/18 12:13:04 lukem Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)ttymsg.c 8.2 (Berkeley) 11/16/93";
37 __RCSID("$NetBSD: ttymsg.c,v 1.23 2009/01/18 12:13:04 lukem Exp $");
39 #endif /* LIBC_SCCS and not lint */
41 #include <sys/types.h>
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.).
64 ttymsg(struct iovec
*iov
, int iovcnt
, const char *line
, int tmout
)
66 static char errbuf
[1024];
67 char device
[MAXNAMLEN
];
70 struct iovec localiov
[32];
73 size_t cnt
, left
, wret
;
75 _DIAGASSERT(iov
!= NULL
);
76 _DIAGASSERT(iovcnt
>= 0);
77 _DIAGASSERT(line
!= NULL
);
80 (void)snprintf(errbuf
, sizeof(errbuf
),
81 "%s: negative iovcnt", __func__
);
85 if ((size_t)iovcnt
>= sizeof(localiov
) / sizeof(localiov
[0])) {
86 (void)snprintf(errbuf
, sizeof(errbuf
),
87 "%s: too many iov's (%d) max is %zu", __func__
,
88 iovcnt
, sizeof(localiov
) / sizeof(localiov
[0]));
92 ptr
= strncmp(line
, "pts/", (size_t)4) == 0 ? line
+ 4 : line
;
93 if (strcspn(ptr
, "./") != strlen(ptr
)) {
94 /* A slash or dot is an attempt to break security... */
95 (void)snprintf(errbuf
, sizeof(errbuf
),
96 "%s: '/' or '.' in \"%s\"", __func__
, line
);
99 ret
= snprintf(device
, sizeof(device
), "%s%s", _PATH_DEV
, line
);
100 if (ret
== -1 || ret
>= (int)sizeof(device
)) {
101 (void) snprintf(errbuf
, sizeof(errbuf
),
102 "%s: line `%s' too long", __func__
, line
);
108 * open will fail on slip lines or exclusive-use lines
109 * if not running as root; not an error.
111 if ((fd
= open(device
, O_WRONLY
|O_NONBLOCK
, 0)) < 0) {
112 if (errno
== EBUSY
|| errno
== EACCES
)
114 (void)snprintf(errbuf
, sizeof(errbuf
),
115 "%s: Cannot open `%s' (%s)",
116 __func__
, device
, strerror(errno
));
120 (void)snprintf(errbuf
, sizeof(errbuf
),
121 "%s: line `%s' is not a tty device", __func__
, device
);
126 for (cnt
= left
= 0; cnt
< (size_t)iovcnt
; ++cnt
)
127 left
+= iov
[cnt
].iov_len
;
130 wret
= writev(fd
, iov
, iovcnt
);
135 if (iov
!= localiov
) {
136 (void)memcpy(localiov
, iov
,
137 iovcnt
* sizeof(struct iovec
));
140 for (cnt
= 0; wret
>= iov
->iov_len
; ++cnt
) {
141 wret
-= iov
->iov_len
;
147 (char *)iov
->iov_base
+ wret
;
148 iov
->iov_len
-= wret
;
151 } else if (wret
== 0) {
152 (void)snprintf(errbuf
, sizeof(errbuf
),
153 "%s: failed writing %zu bytes to `%s'", __func__
,
160 if (errno
== EWOULDBLOCK
) {
169 (void)snprintf(errbuf
, sizeof(errbuf
),
170 "%s: Cannot fork (%s)", __func__
,
175 if (cpid
) { /* parent */
180 /* wait at most tmout seconds */
181 (void)signal(SIGALRM
, SIG_DFL
);
182 (void)signal(SIGTERM
, SIG_DFL
); /* XXX */
184 (void)sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
185 (void)alarm((u_int
)tmout
);
186 (void)fcntl(fd
, F_SETFL
, 0); /* clear O_NONBLOCK */
190 * We get ENODEV on a slip line if we're running as root,
191 * and EIO if the line just went away.
193 if (errno
== ENODEV
|| errno
== EIO
)
198 (void)snprintf(errbuf
, sizeof(errbuf
),
199 "%s: Write to line `%s' failed (%s)", __func__
,
200 device
, strerror(errno
));