Add casts to get rid of compiler warning about signed/unsigned issues.
[sysvinit.git] / src / dowall.c
1 /*
2 * dowall.c Write to all users on the system.
3 *
4 * Author: Miquel van Smoorenburg, miquels@cistron.nl
5 *
6 * Version: @(#)dowall.c 2.85-5 02-Jul-2003 miquels@cistron.nl
7 *
8 * This file is part of the sysvinit suite,
9 * Copyright (C) 1991-2003 Miquel van Smoorenburg.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/sysmacros.h>
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <utmp.h>
35 #include <pwd.h>
36 #include <fcntl.h>
37 #include <signal.h>
38 #include <setjmp.h>
39
40 static sigjmp_buf jbuf;
41
42 /*
43 * Alarm handler
44 */
45 /*ARGSUSED*/
46 static void handler(int arg)
47 {
48 siglongjmp(jbuf, 1);
49 }
50
51
52 /*
53 * Print a text, escape all characters not in Latin-1.
54 */
55 static void feputs(char *line, FILE *fp)
56 {
57 unsigned char *p;
58
59 for (p = (unsigned char *)line; *p; p++) {
60 if (strchr("\t\r\n", *p) ||
61 (*p >= 32 && *p <= 127) || (*p >= 160)) {
62 fputc(*p, fp);
63 } else {
64 fprintf(fp, "^%c", (*p & 0x1f) + 'A' - 1);
65 }
66 }
67 fflush(fp);
68 }
69
70
71 static void getuidtty(char **userp, char **ttyp)
72 {
73 struct passwd *pwd;
74 uid_t uid;
75 char *tty;
76 static char uidbuf[32];
77 static char ttynm[UT_LINESIZE + 4];
78 static int init = 0;
79
80 if (!init) {
81
82 uid = getuid();
83 if ((pwd = getpwuid(uid)) != NULL) {
84 uidbuf[0] = 0;
85 strncat(uidbuf, pwd->pw_name, sizeof(uidbuf) - 1);
86 } else {
87 sprintf(uidbuf, uid ? "uid %d" : "root", (int)uid);
88 }
89
90 if ((tty = ttyname(0)) != NULL) {
91 if (strncmp(tty, "/dev/", 5) == 0)
92 tty += 5;
93 sprintf(ttynm, "(%.28s) ", tty);
94 } else
95 ttynm[0] = 0;
96 init++;
97 }
98
99 *userp = uidbuf;
100 *ttyp = ttynm;
101 }
102
103 /*
104 * Check whether given filename looks like tty device.
105 */
106 static int file_isatty(const char *fname)
107 {
108 struct stat st;
109 int major;
110
111 if (stat(fname, &st) < 0)
112 return 0;
113
114 if (st.st_nlink != 1 || !S_ISCHR(st.st_mode))
115 return 0;
116
117 /*
118 * It would be an impossible task to list all major/minors
119 * of tty devices here, so we just exclude the obvious
120 * majors of which just opening has side-effects:
121 * printers and tapes.
122 */
123 major = major(st.st_dev);
124 if (major == 1 || major == 2 || major == 6 || major == 9 ||
125 major == 12 || major == 16 || major == 21 || major == 27 ||
126 major == 37 || major == 96 || major == 97 || major == 206 ||
127 major == 230) return 0;
128
129 return 1;
130 }
131
132 /*
133 * Wall function.
134 */
135 void wall(char *text, int fromshutdown, int remote)
136 {
137 FILE *tp;
138 struct sigaction sa;
139 struct utmp *utmp;
140 time_t t;
141 char term[UT_LINESIZE+6];
142 char line[81];
143 char hostname[256]; /* HOST_NAME_MAX+1 */
144 char *date, *p;
145 char *user, *tty;
146 int fd, flags;
147
148 /*
149 * Make sure tp and fd aren't in a register. Some versions
150 * of gcc clobber those after longjmp (or so I understand).
151 */
152 (void) &tp;
153 (void) &fd;
154
155 getuidtty(&user, &tty);
156
157 /* Get and report current hostname, to make it easier to find
158 out which machine is being shut down. */
159 if (0 != gethostname(hostname, sizeof(hostname))) {
160 strncpy(hostname, "[unknown]", sizeof(hostname)-1);
161 }
162 /* If hostname is truncated, it is unspecified if the string
163 is null terminated or not. Make sure we know it is null
164 terminated. */
165 hostname[sizeof(hostname)-1] = 0;
166
167 /* Get the time */
168 time(&t);
169 date = ctime(&t);
170 for(p = date; *p && *p != '\n'; p++)
171 ;
172 *p = 0;
173
174 if (remote) {
175 snprintf(line, sizeof(line),
176 "\007\r\nRemote broadcast message (%s):\r\n\r\n",
177 date);
178 } else {
179 snprintf(line, sizeof(line),
180 "\007\r\nBroadcast message from %s@%s %s(%s):\r\n\r\n",
181 user, hostname, tty, date);
182 }
183
184 /*
185 * Fork to avoid us hanging in a write()
186 */
187 if (fork() != 0)
188 return;
189
190 memset(&sa, 0, sizeof(sa));
191 sa.sa_handler = handler;
192 sa.sa_flags = 0;
193 sigemptyset(&sa.sa_mask);
194 sigaction(SIGALRM, &sa, NULL);
195
196 setutent();
197
198 while ((utmp = getutent()) != NULL) {
199 if(utmp->ut_type != USER_PROCESS ||
200 utmp->ut_user[0] == 0) continue;
201 if (strncmp(utmp->ut_line, "/dev/", 5) == 0) {
202 term[0] = 0;
203 strncat(term, utmp->ut_line, sizeof(term)-1);
204 } else
205 snprintf(term, sizeof(term), "/dev/%.*s",
206 UT_LINESIZE, utmp->ut_line);
207 if (strstr(term, "/../")) continue;
208
209 fd = -1;
210 tp = NULL;
211
212 /*
213 * Open it non-delay
214 */
215 if (sigsetjmp(jbuf, 1) == 0) {
216 alarm(2);
217 flags = O_WRONLY|O_NDELAY|O_NOCTTY;
218 if (file_isatty(term) &&
219 (fd = open(term, flags)) >= 0) {
220 if (isatty(fd) &&
221 (tp = fdopen(fd, "w")) != NULL) {
222 fputs(line, tp);
223 feputs(text, tp);
224 fflush(tp);
225 }
226 }
227 }
228 alarm(0);
229 if (fd >= 0) close(fd);
230 if (tp != NULL) fclose(tp);
231 }
232 endutent();
233
234 exit(0);
235 }
236