mesg.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mesg implementation for busybox
  4. *
  5. * Copyright (c) 2002 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config MESG
  10. //config: bool "mesg (1.4 kb)"
  11. //config: default y
  12. //config: help
  13. //config: Mesg controls access to your terminal by others. It is typically
  14. //config: used to allow or disallow other users to write to your terminal
  15. //config:
  16. //config:config FEATURE_MESG_ENABLE_ONLY_GROUP
  17. //config: bool "Enable writing to tty only by group, not by everybody"
  18. //config: default y
  19. //config: depends on MESG
  20. //config: help
  21. //config: Usually, ttys are owned by group "tty", and "write" tool is
  22. //config: setgid to this group. This way, "mesg y" only needs to enable
  23. //config: "write by owning group" bit in tty mode.
  24. //config:
  25. //config: If you set this option to N, "mesg y" will enable writing
  26. //config: by anybody at all. This is not recommended.
  27. //applet:IF_MESG(APPLET_NOFORK(mesg, mesg, BB_DIR_USR_BIN, BB_SUID_DROP, mesg))
  28. //kbuild:lib-$(CONFIG_MESG) += mesg.o
  29. //usage:#define mesg_trivial_usage
  30. //usage: "[y|n]"
  31. //usage:#define mesg_full_usage "\n\n"
  32. //usage: "Control write access to your terminal\n"
  33. //usage: " y Allow write access to your terminal\n"
  34. //usage: " n Disallow write access to your terminal"
  35. #include "libbb.h"
  36. #if ENABLE_FEATURE_MESG_ENABLE_ONLY_GROUP
  37. #define S_IWGRP_OR_S_IWOTH S_IWGRP
  38. #else
  39. #define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH)
  40. #endif
  41. int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  42. int mesg_main(int argc UNUSED_PARAM, char **argv)
  43. {
  44. struct stat sb;
  45. mode_t m;
  46. char c = 0;
  47. argv++;
  48. if (argv[0]
  49. && (argv[1] || ((c = argv[0][0]) != 'y' && c != 'n'))
  50. ) {
  51. bb_show_usage();
  52. }
  53. /* We are a NOFORK applet.
  54. * (Not that it's very useful, but code is trivially NOFORK-safe).
  55. * Play nice. Do not leak anything.
  56. */
  57. if (!isatty(STDIN_FILENO))
  58. bb_simple_error_msg_and_die("not a tty");
  59. xfstat(STDIN_FILENO, &sb, "stdin");
  60. if (c == 0) {
  61. puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
  62. return EXIT_SUCCESS;
  63. }
  64. m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
  65. : sb.st_mode & ~(S_IWGRP|S_IWOTH);
  66. if (fchmod(STDIN_FILENO, m) != 0)
  67. bb_perror_nomsg_and_die();
  68. return EXIT_SUCCESS;
  69. }