2 * Receive wall messages and pass them to a notification program via stdin.
4 * Copyright (C) 2014 Simon Ruderich
6 * This program 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
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
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/>.
32 #include <sys/select.h>
35 #include <sys/types.h>
40 # include <utempter.h>
47 # include <X11/Xlib.h>
51 static void sig_handler(int signal) {
54 static void setup_signals(void) {
55 struct sigaction action;
57 memset(&action, 0, sizeof(action));
58 sigemptyset(&action.sa_mask);
59 action.sa_handler = sig_handler;
61 /* Handle all important signals which might be sent to us so we break out
62 * of the read()-loop below and can perform our cleanup. */
63 sigaction(SIGHUP, &action, NULL);
64 sigaction(SIGINT, &action, NULL);
65 sigaction(SIGQUIT, &action, NULL);
66 sigaction(SIGUSR1, &action, NULL);
67 sigaction(SIGUSR2, &action, NULL);
70 static int open_tty(int public) {
74 ptm = posix_openpt(O_RDWR);
78 if (grantpt(ptm) != 0) {
82 /* Prevent write access for other users so they can't use wall to send
83 * messages to this program. */
89 if (chmod(name, S_IRUSR | S_IWUSR) != 0) {
94 if (unlockpt(ptm) != 0) {
102 static const char *skip_prefix(const char *string, const char *prefix) {
103 size_t length = strlen(prefix);
105 if (!strncmp(string, prefix, length)) {
106 return string + length;
111 static int set_utmpx(short type, int ptm) {
114 const char *tty, *user, *id, *line;
117 user = getpwuid(getuid())->pw_name;
118 gettimeofday(&now, NULL);
125 id = skip_prefix(tty, "/dev/pts/");
126 line = skip_prefix(tty, "/dev/");
128 /* Create utmp entry for the given terminal. */
129 memset(&entry, 0, sizeof(entry));
131 snprintf(entry.ut_user, sizeof(entry.ut_user), "%s", user);
132 snprintf(entry.ut_id, sizeof(entry.ut_id), "%s", id);
133 snprintf(entry.ut_line, sizeof(entry.ut_line), "%s", line);
135 entry.ut_pid = getpid();
136 entry.ut_type = type;
137 entry.ut_tv.tv_sec = now.tv_sec;
138 entry.ut_tv.tv_usec = now.tv_usec;
140 /* Write the entry to the utmp file. */
142 if (!pututxline(&entry)) {
150 static int login(int ptm) {
151 #if defined(USE_UTEMPTER)
152 int result = utempter_add_record(ptm, NULL);
153 /* Exit value of utempter_*() is not correct on all systems, e.g.
154 * FreeBSD() always returns 0. Checking the utmpx database manually is
155 * difficult because we don't know the exact values for ut_id and ut_line,
156 * therefore we only check the return value on systems known to return a
162 #elif defined(USE_UTMPX)
163 return set_utmpx(USER_PROCESS, ptm);
165 # error "neither USE_UTEMPTER nor USE_UTMPX defined"
168 static int logout(int ptm) {
169 #if defined(USE_UTEMPTER)
170 int result = utempter_remove_record(ptm);
176 #elif defined(USE_UTMPX)
177 return set_utmpx(DEAD_PROCESS, ptm);
179 # error "neither USE_UTEMPTER nor USE_UTMPX defined"
183 static int wait_for_write(int fd, int timeout) {
194 result = select(fd + 1, &rfds, NULL, NULL, &tv);
204 /* Got more data to read. */
209 static void drop_privileges(void) {
210 uid_t uid, ruid, euid, suid;
211 gid_t gid, rgid, egid, sgid;
216 /* Drop all privileges. */
217 if (setresuid(uid, uid, uid) != 0) {
221 if (setresgid(gid, gid, gid) != 0) {
226 /* Verify all privileges were dropped. */
227 if (getresuid(&ruid, &euid, &suid) != 0) {
231 if (getresgid(&rgid, &egid, &sgid) != 0) {
235 if (uid == ruid && uid == euid && uid == suid
236 && gid == rgid && gid == egid && gid == sgid) {
237 /* Everything fine. */
241 fprintf(stderr, "failed to drop privileges, aborting\n");
246 static void pass_buffer_to_program(const char *buffer, size_t length, char **argv) {
252 if (pipe(fds) != 0) {
257 fh = fdopen(fds[1] /* write side */, "w");
269 } else if (pid == 0) {
276 close(fds[1]); /* write side */
278 /* Pass read side as stdin to the program. */
279 if (dup2(fds[0], STDIN_FILENO) < 0) {
285 /* Double fork so `wall-notify` doesn't have to wait for the
286 * notification process to terminate. We can't just use
287 * signal(SIGCHLD, SIG_IGN); because utempter on at least FreeBSD
288 * doesn't work if SIGCHLD is ignored. */
293 } else if (pid == 0) {
294 execvp(argv[0], argv);
303 if (fwrite(buffer, 1, length, fh) != length) {
305 /* continue to perform cleanup */
309 close(fds[0]); /* read side */
312 if (waitpid(pid, NULL, 0) < 0) {
316 static void handle_wall(int fd, char **argv) {
320 assert(SSIZE_MAX <= SIZE_MAX);
321 while ((r = read(fd, buffer, sizeof(buffer))) > 0) {
325 /* To prevent partial messages (sometimes it takes multiple reads to
326 * get the complete message) wait for a short time to get the rest of
328 space = sizeof(buffer) - (size_t)r;
329 while (space > 0 && wait_for_write(fd, 1 /* second */)) {
330 r2 = read(fd, buffer + r, space);
338 pass_buffer_to_program(buffer, (size_t)r, argv);
342 static void *x11_event_loop_thread(void *unused) {
348 pthread_detach(pthread_self());
350 display = XOpenDisplay(NULL);
352 fprintf(stderr, "failed to connect to X server\n");
356 /* Do nothing. We just want to die if the X11 session is closed. */
358 XNextEvent(display, &event);
363 static void usage(const char *argv0) {
364 fprintf(stderr, "usage: %s [-X] [-m] <cmd args..>\n", argv0);
365 fprintf(stderr, "Pass wall messages to <cmd args..>.\n");
366 fprintf(stderr, "\n");
367 fprintf(stderr, "-X quit when the current X session terminates\n");
368 fprintf(stderr, "-m allow messages from normal users (like `mesg y`)\n");
370 fprintf(stderr, "\n");
371 fprintf(stderr, "compiled without X11 support, -X disabled\n");
377 int main(int argc, char **argv) {
378 int option, enable_x11, mesg_yes;
382 /* Don't display error messages for unknown options. */
389 * Glibc violates POSIX by default and skips over non-option arguments and
390 * parses later arguments which look like options as well. But we want to
391 * pass everything except the options unmodified to execvp(). Prefixing
392 * the optstring with "+" fixes this behaver. This is not POSIX
393 * compatible, but the option should be ignored on other systems.
395 while ((option = getopt(argc, argv, "+Xmh")) != -1) {
407 fprintf(stderr, "%s: unknown option '%s'!\n\n",
408 argv[0], argv[optind - 1]);
414 /* No arguments remaining, abort. */
418 /* Arguments for notification program. */
421 ptm = open_tty(mesg_yes);
433 printf("%s\n", name);
436 /* We need to open the slave or reading from the master yields EOF after
437 * the first wall write to it. */
438 pts = open(name, O_RDWR);
444 /* Start a thread which connects to X11. This way we die if the user logs
445 * out of its X11 session. */
448 fprintf(stderr, "%s: option -X is disabled!\n\n", argv0);
453 if (pthread_create(&tid, NULL, x11_event_loop_thread, NULL) != 0) {
454 perror("pthread_create");
460 /* Cleanup on signals. Necessary before login(). */
468 /* Main loop. Handle all wall messages sent to our PTY. */
469 handle_wall(ptm, argv);