Debian Codegraph @

libinetutils/ttymsg.c

raw
1 /*
2 Copyright (C) 1996-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1989, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <sys/types.h>
51 #include <sys/uio.h>
52 #include <signal.h>
53 #include <fcntl.h>
54 #include <errno.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdlib.h>
59 #include <sys/wait.h>
60
61 #define MAX_ERRBUF 1024
62
63 static int fork2 (void);
64 static char *normalize_path (char *path, const char *delim);
65
66 /*
67 * Display the contents of a uio structure on a terminal. Used by wall(1),
68 * syslogd(8), and talkd(8). Forks and finishes in child if write would block,
69 * waiting up to tmout seconds. Returns pointer to error string on unexpected
70 * error; string is not newline-terminated. Various "normal" errors are
71 * ignored (exclusive-use, lack of permission, etc.).
72 */
73 char *
74 inetutils_ttymsg (struct iovec *iov, int iovcnt, char *line, int tmout)
75 {
76 static char errbuf[MAX_ERRBUF];
77 char *device;
78 int cnt, fd, left, wret;
79 struct iovec localiov[6];
80 int forked = 0;
81
82 if (iovcnt > (int) (sizeof (localiov) / sizeof (localiov[0])))
83 return (char *) ("too many iov's (change code in wall/ttymsg.c)");
84
85 device = malloc (sizeof PATH_TTY_PFX - 1 + strlen (line) + 1);
86 if (!device)
87 {
88 snprintf (errbuf, sizeof errbuf,
89 "Not enough memory for tty device name");
90 return errbuf;
91 }
92
93 strcpy (device, PATH_TTY_PFX);
94 strcat (device, line);
95 normalize_path (device, "/");
96 if (strncmp (device, PATH_TTY_PFX, strlen (PATH_TTY_PFX)))
97 {
98 /* An attempt to break security... */
99 snprintf (errbuf, sizeof (errbuf), "bad line name: %s", line);
100 return (errbuf);
101 }
102
103 /*
104 * open will fail on slip lines or exclusive-use lines
105 * if not running as root; not an error.
106 */
107 fd = open (device, O_WRONLY | O_NONBLOCK, 0);
108 if (fd < 0)
109 {
110 if (errno == EBUSY || errno == EACCES)
111 return (NULL);
112 #ifdef READUTMP_USE_SYSTEMD
113 /*
114 * GNU/Linux systems without utmp file but with utmp emulation
115 * provided via Gnulib's read_utmp() function regularly return
116 * non-existing TTYs for some active user sessions. This is a
117 * limitation of the utmp emulation. Thus ignore this error on
118 * such systems, but not others.
119 */
120 if (errno == ENOENT)
121 return (NULL);
122 #endif
123 snprintf (errbuf, sizeof (errbuf), "%s: %s", device, strerror (errno));
124 free (device);
125 return errbuf;
126 }
127
128 for (cnt = left = 0; cnt < iovcnt; ++cnt)
129 left += iov[cnt].iov_len;
130
131 for (;;)
132 {
133 wret = writev (fd, iov, iovcnt);
134 if (wret >= left)
135 break;
136 if (wret >= 0)
137 {
138 left -= wret;
139 if (iov != localiov)
140 {
141 memcpy (localiov, iov, iovcnt * sizeof (struct iovec));
142 iov = localiov;
143 }
144 for (cnt = 0; wret >= (int) iov->iov_len; ++cnt)
145 {
146 wret -= iov->iov_len;
147 ++iov;
148 --iovcnt;
149 }
150 if (wret)
151 {
152 iov->iov_base = (char *) iov->iov_base + wret;
153 iov->iov_len -= wret;
154 }
155 continue;
156 }
157 if (errno == EWOULDBLOCK)
158 {
159 int cpid, off = 0;
160
161 if (forked)
162 {
163 close (fd);
164 _exit (EXIT_FAILURE);
165 }
166 cpid = fork2 ();
167 if (cpid < 0)
168 {
169 snprintf (errbuf, sizeof (errbuf),
170 "fork: %s", strerror (errno));
171 close (fd);
172 free (device);
173 return (errbuf);
174 }
175 if (cpid) /* Parent. */
176 {
177 close (fd);
178 free (device);
179 return (NULL);
180 }
181 forked++;
182 /* wait at most tmout seconds */
183 signal (SIGALRM, SIG_DFL);
184 signal (SIGTERM, SIG_DFL); /* XXX */
185 #ifdef HAVE_SIGACTION
186 {
187 sigset_t empty;
188 sigemptyset (&empty);
189 sigprocmask (SIG_SETMASK, &empty, 0);
190 }
191 #else
192 sigsetmask (0);
193 #endif
194 alarm ((unsigned int) tmout);
195 fcntl (fd, O_NONBLOCK, &off);
196 continue;
197 }
198 /*
199 * We get ENODEV on a slip line if we're running as root,
200 * and EIO if the line just went away.
201 */
202 if (errno == ENODEV || errno == EIO)
203 break;
204 close (fd);
205 if (forked)
206 _exit (EXIT_FAILURE);
207 snprintf (errbuf, sizeof (errbuf), "%s: %s", device, strerror (errno));
208 free (device);
209 return (errbuf);
210 }
211
212 free (device);
213 close (fd);
214 if (forked)
215 _exit (EXIT_SUCCESS);
216 return (NULL);
217 }
218
219
220 /* This was part of the Unix-Faq, maintain by Andrew Gierth.
221 fork2() -- like fork, but the new process is immediately orphaned
222 (won't leave a zombie when it exits)
223 Returns 1 to the parent, not any meaningful pid.
224 The parent cannot wait() for the new process (it's unrelated).
225
226 This version assumes that you *haven't* caught or ignored SIGCHLD.
227 If you have, then you should just be using fork() instead anyway. */
228
229 static int
230 fork2 (void)
231 {
232 pid_t pid;
233 int status;
234
235 if (!(pid = fork ()))
236 {
237 switch (fork ())
238 {
239 case 0: /* Child. */
240 return 0;
241 case -1:
242 _exit (errno); /* Assumes all errnos are <256 */
243 default: /* Parent. */
244 _exit (EXIT_SUCCESS);
245 }
246 }
247
248 if (pid < 0 || waitpid (pid, &status, 0) < 0)
249 return -1;
250
251 if (WIFEXITED (status))
252 if (WEXITSTATUS (status) == 0)
253 return 1;
254 else
255 errno = WEXITSTATUS (status);
256 else
257 errno = EINTR; /* well, sort of :-) */
258
259 return -1;
260 }
261
262 char *
263 normalize_path (char *path, const char *delim)
264 {
265 int len;
266 char *p;
267
268 if (!path)
269 return path;
270
271 len = strlen (path);
272
273 /* Empty string is returned as is */
274 if (len == 0)
275 return path;
276
277 /* delete trailing delimiter if any */
278 if (len && path[len - 1] == delim[0])
279 path[len - 1] = 0;
280
281 /* Eliminate any /../ */
282 for (p = strchr (path, '.'); p; p = strchr (p, '.'))
283 {
284 if (p > path && p[-1] == delim[0])
285 {
286 if (p[1] == '.' && (p[2] == 0 || p[2] == delim[0]))
287 /* found */
288 {
289 char *q, *s;
290
291 /* Find previous delimiter */
292 for (q = p - 2; *q != delim[0] && q >= path; q--)
293 ;
294
295 if (q < path)
296 break;
297 /* Copy stuff */
298 s = p + 2;
299 p = q;
300 while ((*q++ = *s++))
301 ;
302 continue;
303 }
304 }
305
306 p++;
307 }
308
309 if (path[0] == 0)
310 {
311 path[0] = delim[0];
312 path[1] = 0;
313 }
314
315 return path;
316 }