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>
39 # include <utempter.h>
46 static void sig_handler(int signal) {
49 static void setup_signals(void) {
50 struct sigaction action;
52 memset(&action, 0, sizeof(action));
53 sigemptyset(&action.sa_mask);
54 action.sa_handler = sig_handler;
56 /* Handle all important signals which might be sent to us so we break out
57 * of the read()-loop below and can perform our cleanup. */
58 sigaction(SIGHUP, &action, NULL);
59 sigaction(SIGINT, &action, NULL);
60 sigaction(SIGQUIT, &action, NULL);
61 sigaction(SIGUSR1, &action, NULL);
62 sigaction(SIGUSR2, &action, NULL);
64 /* Collect zombies automatically without having to call waitpid(2). */
65 signal(SIGCHLD, SIG_IGN);
68 static int open_tty(void) {
72 ptm = posix_openpt(O_RDWR);
76 if (grantpt(ptm) != 0) {
80 /* Prevent write access for other users so they can't use wall to send
81 * messages to this program. */
86 if (chmod(name, S_IRUSR | S_IWUSR) != 0) {
90 if (unlockpt(ptm) != 0) {
98 static const char *skip_prefix(const char *string, const char *prefix) {
99 size_t length = strlen(prefix);
101 if (!strncmp(string, prefix, length)) {
102 return string + length;
107 static int set_utmpx(short type, int ptm) {
110 const char *tty, *user, *id, *line;
113 user = getpwuid(getuid())->pw_name;
114 gettimeofday(&now, NULL);
121 id = skip_prefix(tty, "/dev/pts/");
122 line = skip_prefix(tty, "/dev/");
124 /* Create utmp entry for the given terminal. */
125 memset(&entry, 0, sizeof(entry));
127 snprintf(entry.ut_user, sizeof(entry.ut_user), "%s", user);
128 snprintf(entry.ut_id, sizeof(entry.ut_id), "%s", id);
129 snprintf(entry.ut_line, sizeof(entry.ut_line), "%s", line);
131 entry.ut_pid = getpid();
132 entry.ut_type = type;
133 entry.ut_tv.tv_sec = now.tv_sec;
134 entry.ut_tv.tv_usec = now.tv_usec;
136 /* Write the entry to the utmp file. */
138 if (!pututxline(&entry)) {
146 static int login(int ptm) {
147 #if defined(USE_UTEMPTER)
148 return utempter_add_record(ptm, NULL);
149 #elif defined(USE_UTMPX)
150 return set_utmpx(USER_PROCESS, ptm);
152 # error "neither USE_UTEMPTER nor USE_UTMPX defined"
155 static int logout(int ptm) {
156 #if defined(USE_UTEMPTER)
157 return utempter_remove_record(ptm);
158 #elif defined(USE_UTMPX)
159 return set_utmpx(DEAD_PROCESS, ptm);
161 # error "neither USE_UTEMPTER nor USE_UTMPX defined"
165 static int wait_for_write(int fd, int timeout) {
176 result = select(fd + 1, &rfds, NULL, NULL, &tv);
186 /* Got more data to read. */
190 static void pass_buffer_to_program(const char *buffer, size_t length, char **argv) {
199 if (pipe(fds) != 0) {
204 fh = fdopen(fds[1] /* write side */, "w");
216 } else if (pid == 0) {
219 close(fds[1]); /* write side */
221 /* Pass read side as stdin to the program. */
222 if (dup2(fds[0], STDIN_FILENO) < 0) {
228 execvp(argv[0], argv);
234 if (fwrite(buffer, 1, length, fh) != length) {
236 /* continue to perform cleanup */
240 close(fds[0]); /* read side */
243 static void handle_wall(int fd, char **argv) {
247 assert(SSIZE_MAX <= SIZE_MAX);
248 while ((r = read(fd, buffer, sizeof(buffer))) > 0) {
252 /* To prevent partial messages (sometimes it takes multiple reads to
253 * get the complete message) wait for a short time to get the rest of
255 space = sizeof(buffer) - (size_t)r;
256 while (space > 0 && wait_for_write(fd, 1 /* second */)) {
257 r2 = read(fd, buffer + r, space);
265 pass_buffer_to_program(buffer, (size_t)r, argv);
270 int main(int argc, char **argv) {
275 fprintf(stderr, "usage: %s <cmd args..>\n", argv[0]);
291 printf("%s\n", name);
294 /* We need to open the slave or reading from the master yields EOF after
295 * the first wall write to it. */
296 pts = open(name, O_RDWR);
302 /* Cleanup on signals. Necessary before login(). */
310 /* Main loop. Handle all wall messages sent to our PTY. */
311 handle_wall(ptm, argv);