Bernhard Reutner-Fischer | e039e68 | 2009-10-26 23:29:03 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * wall - write a message to all logged-in users |
| 4 | * Copyright (c) 2009 Bernhard Reutner-Fischer |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | e039e68 | 2009-10-26 23:29:03 +0100 | [diff] [blame] | 7 | */ |
Denys Vlasenko | cd256e1 | 2013-10-06 15:14:25 +0200 | [diff] [blame] | 8 | //config:config WALL |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "wall (2.6 kb)" |
Denys Vlasenko | cd256e1 | 2013-10-06 15:14:25 +0200 | [diff] [blame] | 10 | //config: default y |
| 11 | //config: depends on FEATURE_UTMP |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: Write a message to all users that are logged in. |
Denys Vlasenko | cd256e1 | 2013-10-06 15:14:25 +0200 | [diff] [blame] | 14 | |
| 15 | /* Needs to be run by root or be suid root - needs to write to /dev/TTY: */ |
| 16 | //applet:IF_WALL(APPLET(wall, BB_DIR_USR_BIN, BB_SUID_REQUIRE)) |
| 17 | |
| 18 | //kbuild:lib-$(CONFIG_WALL) += wall.o |
| 19 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 20 | //usage:#define wall_trivial_usage |
| 21 | //usage: "[FILE]" |
| 22 | //usage:#define wall_full_usage "\n\n" |
| 23 | //usage: "Write content of FILE or stdin to all logged-in users" |
| 24 | //usage: |
| 25 | //usage:#define wall_sample_usage |
| 26 | //usage: "echo foo | wall\n" |
| 27 | //usage: "wall ./mymessage" |
| 28 | |
Bernhard Reutner-Fischer | e039e68 | 2009-10-26 23:29:03 +0100 | [diff] [blame] | 29 | #include "libbb.h" |
Bernhard Reutner-Fischer | e039e68 | 2009-10-26 23:29:03 +0100 | [diff] [blame] | 30 | |
| 31 | int wall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 32 | int wall_main(int argc UNUSED_PARAM, char **argv) |
| 33 | { |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 34 | struct utmpx *ut; |
Bernhard Reutner-Fischer | e039e68 | 2009-10-26 23:29:03 +0100 | [diff] [blame] | 35 | char *msg; |
Denys Vlasenko | cd256e1 | 2013-10-06 15:14:25 +0200 | [diff] [blame] | 36 | int fd; |
Bernhard Reutner-Fischer | e039e68 | 2009-10-26 23:29:03 +0100 | [diff] [blame] | 37 | |
Denys Vlasenko | cd256e1 | 2013-10-06 15:14:25 +0200 | [diff] [blame] | 38 | fd = STDIN_FILENO; |
| 39 | if (argv[1]) { |
| 40 | /* The applet is setuid. |
| 41 | * Access to the file must be under user's uid/gid. |
| 42 | */ |
Ryan Mallon | 1d30b3f | 2013-10-08 14:53:29 +0200 | [diff] [blame] | 43 | fd = xopen_as_uid_gid(argv[1], O_RDONLY, getuid(), getgid()); |
Denys Vlasenko | cd256e1 | 2013-10-06 15:14:25 +0200 | [diff] [blame] | 44 | } |
Bernhard Reutner-Fischer | e039e68 | 2009-10-26 23:29:03 +0100 | [diff] [blame] | 45 | msg = xmalloc_read(fd, NULL); |
| 46 | if (ENABLE_FEATURE_CLEAN_UP && argv[1]) |
| 47 | close(fd); |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 48 | setutxent(); |
| 49 | while ((ut = getutxent()) != NULL) { |
Bernhard Reutner-Fischer | e039e68 | 2009-10-26 23:29:03 +0100 | [diff] [blame] | 50 | char *line; |
| 51 | if (ut->ut_type != USER_PROCESS) |
| 52 | continue; |
| 53 | line = concat_path_file("/dev", ut->ut_line); |
| 54 | xopen_xwrite_close(line, msg); |
| 55 | free(line); |
| 56 | } |
| 57 | if (ENABLE_FEATURE_CLEAN_UP) { |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 58 | endutxent(); |
Bernhard Reutner-Fischer | e039e68 | 2009-10-26 23:29:03 +0100 | [diff] [blame] | 59 | free(msg); |
| 60 | } |
| 61 | return EXIT_SUCCESS; |
| 62 | } |