3
0

wall.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //config:config WALL
  9. //config: bool "wall (2.9 kb)"
  10. //config: default y
  11. //config: depends on FEATURE_UTMP
  12. //config: help
  13. //config: Write a message to all users that are logged in.
  14. /* Needs to be run by root or be suid root - needs to write to /dev/TTY: */
  15. //applet:IF_WALL(APPLET(wall, BB_DIR_USR_BIN, BB_SUID_REQUIRE))
  16. //kbuild:lib-$(CONFIG_WALL) += wall.o
  17. //usage:#define wall_trivial_usage
  18. //usage: "[FILE]"
  19. //usage:#define wall_full_usage "\n\n"
  20. //usage: "Write content of FILE or stdin to all logged-in users"
  21. //usage:
  22. //usage:#define wall_sample_usage
  23. //usage: "echo foo | wall\n"
  24. //usage: "wall ./mymessage"
  25. #include "libbb.h"
  26. int wall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int wall_main(int argc UNUSED_PARAM, char **argv)
  28. {
  29. struct utmpx *ut;
  30. char *msg;
  31. int fd;
  32. fd = STDIN_FILENO;
  33. if (argv[1]) {
  34. /* The applet is setuid.
  35. * Access to the file must be under user's uid/gid.
  36. */
  37. fd = xopen_as_uid_gid(argv[1], O_RDONLY, getuid(), getgid());
  38. }
  39. msg = xmalloc_read(fd, NULL);
  40. if (ENABLE_FEATURE_CLEAN_UP && argv[1])
  41. close(fd);
  42. setutxent();
  43. while ((ut = getutxent()) != NULL) {
  44. char *line;
  45. if (ut->ut_type != USER_PROCESS)
  46. continue;
  47. line = concat_path_file("/dev", ut->ut_line);
  48. xopen_xwrite_close(line, msg);
  49. free(line);
  50. }
  51. if (ENABLE_FEATURE_CLEAN_UP) {
  52. endutxent();
  53. free(msg);
  54. }
  55. return EXIT_SUCCESS;
  56. }