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