]>
Commit | Line | Data |
---|---|---|
a74aeac6 PR |
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> | |
1414572e DWF |
39 | #include <paths.h> |
40 | ||
41 | #ifndef _PATH_DEV | |
42 | # define _PATH_DEV "/dev/" | |
43 | #endif | |
44 | #ifndef HOST_NAME_MAX | |
45 | # define HOST_NAME_MAX 255 | |
46 | #endif | |
a74aeac6 PR |
47 | |
48 | static sigjmp_buf jbuf; | |
49 | ||
50 | /* | |
51 | * Alarm handler | |
52 | */ | |
53 | /*ARGSUSED*/ | |
9af2d863 DWF |
54 | # ifdef __GNUC__ |
55 | static void handler(int arg __attribute__((unused))) | |
56 | # else | |
a74aeac6 | 57 | static void handler(int arg) |
9af2d863 | 58 | # endif |
a74aeac6 PR |
59 | { |
60 | siglongjmp(jbuf, 1); | |
61 | } | |
62 | ||
63 | ||
64 | /* | |
65 | * Print a text, escape all characters not in Latin-1. | |
66 | */ | |
901fc4d2 | 67 | static void feputs(const char *line, FILE *fp) |
a74aeac6 PR |
68 | { |
69 | unsigned char *p; | |
70 | ||
71 | for (p = (unsigned char *)line; *p; p++) { | |
72 | if (strchr("\t\r\n", *p) || | |
73 | (*p >= 32 && *p <= 127) || (*p >= 160)) { | |
74 | fputc(*p, fp); | |
75 | } else { | |
76 | fprintf(fp, "^%c", (*p & 0x1f) + 'A' - 1); | |
77 | } | |
78 | } | |
79 | fflush(fp); | |
80 | } | |
81 | ||
82 | ||
83 | static void getuidtty(char **userp, char **ttyp) | |
84 | { | |
85 | struct passwd *pwd; | |
86 | uid_t uid; | |
87 | char *tty; | |
88 | static char uidbuf[32]; | |
89 | static char ttynm[UT_LINESIZE + 4]; | |
90 | static int init = 0; | |
91 | ||
92 | if (!init) { | |
93 | ||
94 | uid = getuid(); | |
95 | if ((pwd = getpwuid(uid)) != NULL) { | |
96 | uidbuf[0] = 0; | |
97 | strncat(uidbuf, pwd->pw_name, sizeof(uidbuf) - 1); | |
98 | } else { | |
99 | sprintf(uidbuf, uid ? "uid %d" : "root", (int)uid); | |
100 | } | |
101 | ||
102 | if ((tty = ttyname(0)) != NULL) { | |
1414572e DWF |
103 | const size_t plen = strlen(_PATH_DEV); |
104 | if (strncmp(tty, _PATH_DEV, plen) == 0) { | |
105 | tty += plen; | |
106 | if (tty[0] == '/') | |
107 | tty++; | |
108 | } | |
109 | snprintf(ttynm, sizeof(ttynm), "(%.*s) ", | |
110 | UT_LINESIZE, tty); | |
a74aeac6 PR |
111 | } else |
112 | ttynm[0] = 0; | |
113 | init++; | |
114 | } | |
115 | ||
116 | *userp = uidbuf; | |
117 | *ttyp = ttynm; | |
118 | } | |
119 | ||
120 | /* | |
121 | * Check whether given filename looks like tty device. | |
122 | */ | |
123 | static int file_isatty(const char *fname) | |
124 | { | |
125 | struct stat st; | |
126 | int major; | |
127 | ||
128 | if (stat(fname, &st) < 0) | |
129 | return 0; | |
130 | ||
131 | if (st.st_nlink != 1 || !S_ISCHR(st.st_mode)) | |
132 | return 0; | |
133 | ||
134 | /* | |
135 | * It would be an impossible task to list all major/minors | |
136 | * of tty devices here, so we just exclude the obvious | |
137 | * majors of which just opening has side-effects: | |
138 | * printers and tapes. | |
139 | */ | |
140 | major = major(st.st_dev); | |
141 | if (major == 1 || major == 2 || major == 6 || major == 9 || | |
142 | major == 12 || major == 16 || major == 21 || major == 27 || | |
143 | major == 37 || major == 96 || major == 97 || major == 206 || | |
144 | major == 230) return 0; | |
145 | ||
146 | return 1; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Wall function. | |
151 | */ | |
901fc4d2 | 152 | void wall(const char *text, int remote) |
a74aeac6 PR |
153 | { |
154 | FILE *tp; | |
155 | struct sigaction sa; | |
156 | struct utmp *utmp; | |
157 | time_t t; | |
1414572e | 158 | char term[UT_LINESIZE+ strlen(_PATH_DEV) + 1]; |
a74aeac6 | 159 | char line[81]; |
1414572e | 160 | char hostname[HOST_NAME_MAX+1]; |
a74aeac6 PR |
161 | char *date, *p; |
162 | char *user, *tty; | |
163 | int fd, flags; | |
164 | ||
165 | /* | |
166 | * Make sure tp and fd aren't in a register. Some versions | |
167 | * of gcc clobber those after longjmp (or so I understand). | |
168 | */ | |
169 | (void) &tp; | |
170 | (void) &fd; | |
171 | ||
172 | getuidtty(&user, &tty); | |
173 | ||
174 | /* Get and report current hostname, to make it easier to find | |
175 | out which machine is being shut down. */ | |
176 | if (0 != gethostname(hostname, sizeof(hostname))) { | |
177 | strncpy(hostname, "[unknown]", sizeof(hostname)-1); | |
178 | } | |
179 | /* If hostname is truncated, it is unspecified if the string | |
180 | is null terminated or not. Make sure we know it is null | |
181 | terminated. */ | |
182 | hostname[sizeof(hostname)-1] = 0; | |
183 | ||
184 | /* Get the time */ | |
185 | time(&t); | |
186 | date = ctime(&t); | |
187 | for(p = date; *p && *p != '\n'; p++) | |
188 | ; | |
189 | *p = 0; | |
190 | ||
191 | if (remote) { | |
192 | snprintf(line, sizeof(line), | |
193 | "\007\r\nRemote broadcast message (%s):\r\n\r\n", | |
194 | date); | |
195 | } else { | |
196 | snprintf(line, sizeof(line), | |
197 | "\007\r\nBroadcast message from %s@%s %s(%s):\r\n\r\n", | |
198 | user, hostname, tty, date); | |
199 | } | |
200 | ||
201 | /* | |
202 | * Fork to avoid us hanging in a write() | |
203 | */ | |
204 | if (fork() != 0) | |
205 | return; | |
206 | ||
207 | memset(&sa, 0, sizeof(sa)); | |
208 | sa.sa_handler = handler; | |
209 | sa.sa_flags = 0; | |
210 | sigemptyset(&sa.sa_mask); | |
211 | sigaction(SIGALRM, &sa, NULL); | |
212 | ||
213 | setutent(); | |
214 | ||
215 | while ((utmp = getutent()) != NULL) { | |
216 | if(utmp->ut_type != USER_PROCESS || | |
217 | utmp->ut_user[0] == 0) continue; | |
1414572e | 218 | if (strncmp(utmp->ut_line, _PATH_DEV, strlen(_PATH_DEV)) == 0) { |
a74aeac6 PR |
219 | term[0] = 0; |
220 | strncat(term, utmp->ut_line, sizeof(term)-1); | |
221 | } else | |
1414572e | 222 | snprintf(term, sizeof(term), _PATH_DEV "%.*s", |
a74aeac6 PR |
223 | UT_LINESIZE, utmp->ut_line); |
224 | if (strstr(term, "/../")) continue; | |
225 | ||
226 | fd = -1; | |
227 | tp = NULL; | |
228 | ||
229 | /* | |
230 | * Open it non-delay | |
231 | */ | |
232 | if (sigsetjmp(jbuf, 1) == 0) { | |
233 | alarm(2); | |
234 | flags = O_WRONLY|O_NDELAY|O_NOCTTY; | |
235 | if (file_isatty(term) && | |
236 | (fd = open(term, flags)) >= 0) { | |
237 | if (isatty(fd) && | |
238 | (tp = fdopen(fd, "w")) != NULL) { | |
239 | fputs(line, tp); | |
240 | feputs(text, tp); | |
241 | fflush(tp); | |
242 | } | |
243 | } | |
244 | } | |
245 | alarm(0); | |
246 | if (fd >= 0) close(fd); | |
247 | if (tp != NULL) fclose(tp); | |
248 | } | |
249 | endutent(); | |
250 | ||
251 | exit(0); | |
252 | } | |
253 |