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 static void sig_handler(int signal) {
50 static void setup_signals(void) {
51 struct sigaction action;
53 memset(&action, 0, sizeof(action));
54 sigemptyset(&action.sa_mask);
55 action.sa_handler = sig_handler;
57 /* Handle all important signals which might be sent to us so we break out
58 * of the read()-loop below and can perform our cleanup. */
59 sigaction(SIGHUP, &action, NULL);
60 sigaction(SIGINT, &action, NULL);
61 sigaction(SIGQUIT, &action, NULL);
62 sigaction(SIGUSR1, &action, NULL);
63 sigaction(SIGUSR2, &action, NULL);
66 static int open_tty(void) {
70 ptm = posix_openpt(O_RDWR);
74 if (grantpt(ptm) != 0) {
78 /* Prevent write access for other users so they can't use wall to send
79 * messages to this program. */
84 if (chmod(name, S_IRUSR | S_IWUSR) != 0) {
88 if (unlockpt(ptm) != 0) {
96 static const char *skip_prefix(const char *string, const char *prefix) {
97 size_t length = strlen(prefix);
99 if (!strncmp(string, prefix, length)) {
100 return string + length;
105 static int set_utmpx(short type, int ptm) {
108 const char *tty, *user, *id, *line;
111 user = getpwuid(getuid())->pw_name;
112 gettimeofday(&now, NULL);
119 id = skip_prefix(tty, "/dev/pts/");
120 line = skip_prefix(tty, "/dev/");
122 /* Create utmp entry for the given terminal. */
123 memset(&entry, 0, sizeof(entry));
125 snprintf(entry.ut_user, sizeof(entry.ut_user), "%s", user);
126 snprintf(entry.ut_id, sizeof(entry.ut_id), "%s", id);
127 snprintf(entry.ut_line, sizeof(entry.ut_line), "%s", line);
129 entry.ut_pid = getpid();
130 entry.ut_type = type;
131 entry.ut_tv.tv_sec = now.tv_sec;
132 entry.ut_tv.tv_usec = now.tv_usec;
134 /* Write the entry to the utmp file. */
136 if (!pututxline(&entry)) {
144 static int login(int ptm) {
145 #if defined(USE_UTEMPTER)
146 int result = utempter_add_record(ptm, NULL);
147 /* Exit value of utempter_*() is not correct on all systems, e.g.
148 * FreeBSD() always returns 0. Checking the utmpx database manually is
149 * difficult because we don't know the exact values for ut_id and ut_line,
150 * therefore we only check the return value on systems known to return a
156 #elif defined(USE_UTMPX)
157 return set_utmpx(USER_PROCESS, ptm);
159 # error "neither USE_UTEMPTER nor USE_UTMPX defined"
162 static int logout(int ptm) {
163 #if defined(USE_UTEMPTER)
164 int result = utempter_remove_record(ptm);
170 #elif defined(USE_UTMPX)
171 return set_utmpx(DEAD_PROCESS, ptm);
173 # error "neither USE_UTEMPTER nor USE_UTMPX defined"
177 static int wait_for_write(int fd, int timeout) {
188 result = select(fd + 1, &rfds, NULL, NULL, &tv);
198 /* Got more data to read. */
202 static void pass_buffer_to_program(const char *buffer, size_t length, char **argv) {
211 if (pipe(fds) != 0) {
216 fh = fdopen(fds[1] /* write side */, "w");
228 } else if (pid == 0) {
231 close(fds[1]); /* write side */
233 /* Pass read side as stdin to the program. */
234 if (dup2(fds[0], STDIN_FILENO) < 0) {
240 /* Double fork so `wall-notify` doesn't have to wait for the
241 * notification process to terminate. We can't just use
242 * signal(SIGCHLD, SIG_IGN); because utempter on at least FreeBSD
243 * doesn't work if SIGCHLD is ignored. */
248 } else if (pid == 0) {
249 execvp(argv[0], argv);
258 if (fwrite(buffer, 1, length, fh) != length) {
260 /* continue to perform cleanup */
264 close(fds[0]); /* read side */
267 if (waitpid(pid, NULL, 0) < 0) {
271 static void handle_wall(int fd, char **argv) {
275 assert(SSIZE_MAX <= SIZE_MAX);
276 while ((r = read(fd, buffer, sizeof(buffer))) > 0) {
280 /* To prevent partial messages (sometimes it takes multiple reads to
281 * get the complete message) wait for a short time to get the rest of
283 space = sizeof(buffer) - (size_t)r;
284 while (space > 0 && wait_for_write(fd, 1 /* second */)) {
285 r2 = read(fd, buffer + r, space);
293 pass_buffer_to_program(buffer, (size_t)r, argv);
298 int main(int argc, char **argv) {
303 fprintf(stderr, "usage: %s <cmd args..>\n", argv[0]);
319 printf("%s\n", name);
322 /* We need to open the slave or reading from the master yields EOF after
323 * the first wall write to it. */
324 pts = open(name, O_RDWR);
330 /* Cleanup on signals. Necessary before login(). */
338 /* Main loop. Handle all wall messages sent to our PTY. */
339 handle_wall(ptm, argv);