blob: 23fb8751719b085ead03fa75caf3e0690543caaf [file] [log] [blame]
Bernhard Reutner-Fischere039e682009-10-26 23:29:03 +01001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischere039e682009-10-26 23:29:03 +01007 */
Denys Vlasenkocd256e12013-10-06 15:14:25 +02008//config:config WALL
Denys Vlasenkob097a842018-12-28 03:20:17 +01009//config: bool "wall (2.6 kb)"
Denys Vlasenkocd256e12013-10-06 15:14:25 +020010//config: default y
11//config: depends on FEATURE_UTMP
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Write a message to all users that are logged in.
Denys Vlasenkocd256e12013-10-06 15:14:25 +020014
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 Orga5bc8c002011-04-11 03:29:49 +020020//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-Fischere039e682009-10-26 23:29:03 +010029#include "libbb.h"
Bernhard Reutner-Fischere039e682009-10-26 23:29:03 +010030
31int wall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
32int wall_main(int argc UNUSED_PARAM, char **argv)
33{
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020034 struct utmpx *ut;
Bernhard Reutner-Fischere039e682009-10-26 23:29:03 +010035 char *msg;
Denys Vlasenkocd256e12013-10-06 15:14:25 +020036 int fd;
Bernhard Reutner-Fischere039e682009-10-26 23:29:03 +010037
Denys Vlasenkocd256e12013-10-06 15:14:25 +020038 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 Mallon1d30b3f2013-10-08 14:53:29 +020043 fd = xopen_as_uid_gid(argv[1], O_RDONLY, getuid(), getgid());
Denys Vlasenkocd256e12013-10-06 15:14:25 +020044 }
Bernhard Reutner-Fischere039e682009-10-26 23:29:03 +010045 msg = xmalloc_read(fd, NULL);
46 if (ENABLE_FEATURE_CLEAN_UP && argv[1])
47 close(fd);
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020048 setutxent();
49 while ((ut = getutxent()) != NULL) {
Bernhard Reutner-Fischere039e682009-10-26 23:29:03 +010050 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-Fischer86a7f182015-04-02 23:03:46 +020058 endutxent();
Bernhard Reutner-Fischere039e682009-10-26 23:29:03 +010059 free(msg);
60 }
61 return EXIT_SUCCESS;
62}