]>
Commit | Line | Data |
---|---|---|
6dbe3af9 | 1 | /* |
726f69e2 KZ |
2 | * Copyright (c) 1989, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
6dbe3af9 KZ |
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 | * | |
726f69e2 | 33 | * Modified Sun Mar 12 10:39:22 1995, faith@cs.unc.edu for Linux |
6dbe3af9 KZ |
34 | * |
35 | */ | |
36 | ||
b50945d4 | 37 | /* 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
7eda085c KZ |
38 | * - added Native Language Support |
39 | * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> | |
40 | * - fixed strerr(errno) in gettext calls | |
41 | */ | |
42 | ||
6dbe3af9 | 43 | #include <sys/types.h> |
00c505d9 | 44 | #include <sys/param.h> |
6dbe3af9 KZ |
45 | #include <sys/uio.h> |
46 | #include <signal.h> | |
47 | #include <fcntl.h> | |
48 | #include <dirent.h> | |
49 | #include <errno.h> | |
50 | #include <paths.h> | |
51 | #include <unistd.h> | |
52 | #include <stdio.h> | |
53 | #include <string.h> | |
54 | #include <stdlib.h> | |
55 | ||
cdd2a8c3 SK |
56 | #include "nls.h" |
57 | #include "closestream.h" | |
726f69e2 | 58 | #include "pathnames.h" |
66ee8158 | 59 | #include "ttymsg.h" |
726f69e2 | 60 | |
afbdd44f SK |
61 | #define ERR_BUFLEN (MAXNAMLEN + 1024) |
62 | ||
6dbe3af9 | 63 | /* |
726f69e2 KZ |
64 | * Display the contents of a uio structure on a terminal. Used by wall(1), |
65 | * syslogd(8), and talkd(8). Forks and finishes in child if write would block, | |
66 | * waiting up to tmout seconds. Returns pointer to error string on unexpected | |
67 | * error; string is not newline-terminated. Various "normal" errors are | |
68 | * ignored (exclusive-use, lack of permission, etc.). | |
6dbe3af9 KZ |
69 | */ |
70 | char * | |
5e6bf800 | 71 | ttymsg(struct iovec *iov, size_t iovcnt, char *line, int tmout) { |
fd6b7a7f | 72 | static char device[MAXNAMLEN]; |
afbdd44f | 73 | static char errbuf[ERR_BUFLEN]; |
5e6bf800 SK |
74 | size_t cnt, left; |
75 | ssize_t wret; | |
6dbe3af9 | 76 | struct iovec localiov[6]; |
afbdd44f SK |
77 | int fd, forked = 0; |
78 | ssize_t len = 0; | |
6dbe3af9 | 79 | |
afbdd44f SK |
80 | if (iovcnt > ARRAY_SIZE(localiov)) { |
81 | snprintf(errbuf, sizeof(errbuf), _("internal error: too many iov's")); | |
82 | return errbuf; | |
83 | } | |
7eda085c KZ |
84 | |
85 | /* The old code here rejected the line argument when it contained a '/', | |
86 | saying: "A slash may be an attempt to break security...". | |
87 | However, if a user can control the line argument here | |
88 | then he can make this routine write to /dev/hda or /dev/sda | |
89 | already. So, this test was worthless, and these days it is | |
90 | also wrong since people use /dev/pts/xxx. */ | |
726f69e2 | 91 | |
afbdd44f | 92 | len = snprintf(device, sizeof(device), "%s%s", _PATH_DEV, line); |
06fa5817 | 93 | if (len < 0 || (size_t)len >= sizeof(device)) { |
afbdd44f SK |
94 | snprintf(errbuf, sizeof(errbuf), _("excessively long line arg")); |
95 | return errbuf; | |
726f69e2 KZ |
96 | } |
97 | ||
6dbe3af9 KZ |
98 | /* |
99 | * open will fail on slip lines or exclusive-use lines | |
100 | * if not running as root; not an error. | |
101 | */ | |
6dbe3af9 KZ |
102 | if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) { |
103 | if (errno == EBUSY || errno == EACCES) | |
afbdd44f SK |
104 | return NULL; |
105 | ||
106 | len = snprintf(errbuf, sizeof(errbuf), "%s: %m", device); | |
06fa5817 | 107 | if (len < 0 || (size_t)len >= sizeof(errbuf)) |
afbdd44f SK |
108 | snprintf(errbuf, sizeof(errbuf), _("open failed")); |
109 | return errbuf; | |
6dbe3af9 KZ |
110 | } |
111 | ||
112 | for (cnt = left = 0; cnt < iovcnt; ++cnt) | |
113 | left += iov[cnt].iov_len; | |
114 | ||
115 | for (;;) { | |
116 | wret = writev(fd, iov, iovcnt); | |
5e6bf800 | 117 | if (wret >= (ssize_t) left) |
6dbe3af9 KZ |
118 | break; |
119 | if (wret >= 0) { | |
120 | left -= wret; | |
121 | if (iov != localiov) { | |
c0f19ccf | 122 | memmove(localiov, iov, |
6dbe3af9 KZ |
123 | iovcnt * sizeof(struct iovec)); |
124 | iov = localiov; | |
125 | } | |
5e6bf800 | 126 | for (cnt = 0; wret >= (ssize_t) iov->iov_len; ++cnt) { |
6dbe3af9 KZ |
127 | wret -= iov->iov_len; |
128 | ++iov; | |
129 | --iovcnt; | |
130 | } | |
131 | if (wret) { | |
5e6bf800 | 132 | iov->iov_base = (char *) iov->iov_base + wret; |
6dbe3af9 KZ |
133 | iov->iov_len -= wret; |
134 | } | |
135 | continue; | |
136 | } | |
137 | if (errno == EWOULDBLOCK) { | |
a6f62883 | 138 | int cpid, flags; |
1ed485b5 | 139 | sigset_t sigmask; |
6dbe3af9 KZ |
140 | |
141 | if (forked) { | |
89dda678 | 142 | close(fd); |
5e6bf800 | 143 | _exit(EXIT_FAILURE); |
6dbe3af9 KZ |
144 | } |
145 | cpid = fork(); | |
146 | if (cpid < 0) { | |
afbdd44f | 147 | len = snprintf(errbuf, sizeof(errbuf), _("fork: %m")); |
06fa5817 | 148 | if (len < 0 || (size_t)len >= sizeof(errbuf)) |
afbdd44f | 149 | snprintf(errbuf, sizeof(errbuf), _("cannot fork")); |
89dda678 | 150 | close(fd); |
afbdd44f | 151 | return errbuf; |
6dbe3af9 KZ |
152 | } |
153 | if (cpid) { /* parent */ | |
89dda678 | 154 | close(fd); |
afbdd44f | 155 | return NULL; |
6dbe3af9 KZ |
156 | } |
157 | forked++; | |
726f69e2 | 158 | /* wait at most tmout seconds */ |
89dda678 SK |
159 | signal(SIGALRM, SIG_DFL); |
160 | signal(SIGTERM, SIG_DFL); /* XXX */ | |
1ed485b5 KZ |
161 | sigemptyset(&sigmask); |
162 | sigprocmask (SIG_SETMASK, &sigmask, NULL); | |
89dda678 | 163 | alarm((u_int)tmout); |
a6f62883 KZ |
164 | flags = fcntl(fd, F_GETFL); |
165 | fcntl(flags, F_SETFL, (long) (flags & ~O_NONBLOCK)); | |
6dbe3af9 | 166 | continue; |
726f69e2 | 167 | } |
6dbe3af9 KZ |
168 | /* |
169 | * We get ENODEV on a slip line if we're running as root, | |
170 | * and EIO if the line just went away. | |
171 | */ | |
172 | if (errno == ENODEV || errno == EIO) | |
173 | break; | |
7f587afc SK |
174 | if (close_fd(fd) != 0) |
175 | warn(_("write failed: %s"), device); | |
6dbe3af9 | 176 | if (forked) |
5e6bf800 | 177 | _exit(EXIT_FAILURE); |
afbdd44f SK |
178 | |
179 | len = snprintf(errbuf, sizeof(errbuf), "%s: %m", device); | |
06fa5817 | 180 | if (len < 0 || (size_t)len >= sizeof(errbuf)) |
afbdd44f SK |
181 | snprintf(errbuf, sizeof(errbuf), |
182 | _("%s: BAD ERROR, message is " | |
183 | "far too long"), device); | |
184 | return errbuf; | |
6dbe3af9 KZ |
185 | } |
186 | ||
6dbe3af9 | 187 | if (forked) |
5e6bf800 | 188 | _exit(EXIT_SUCCESS); |
afbdd44f | 189 | return NULL; |
6dbe3af9 | 190 | } |