1 /* vi: set sw=4 ts=4: */
3 * mesg implementation for busybox
5 * Copyright (c) 2002 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "mesg (1.4 kb)"
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
16 //config:config FEATURE_MESG_ENABLE_ONLY_GROUP
17 //config: bool "Enable writing to tty only by group, not by everybody"
19 //config: depends on MESG
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.
25 //config: If you set this option to N, "mesg y" will enable writing
26 //config: by anybody at all. This is not recommended.
28 //applet:IF_MESG(APPLET_NOFORK(mesg, mesg, BB_DIR_USR_BIN, BB_SUID_DROP, mesg))
30 //kbuild:lib-$(CONFIG_MESG) += mesg.o
32 //usage:#define mesg_trivial_usage
34 //usage:#define mesg_full_usage "\n\n"
35 //usage: "Control write access to your terminal\n"
36 //usage: " y Allow write access to your terminal\n"
37 //usage: " n Disallow write access to your terminal"
41 #if ENABLE_FEATURE_MESG_ENABLE_ONLY_GROUP
42 #define S_IWGRP_OR_S_IWOTH S_IWGRP
44 #define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH)
47 int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
48 int mesg_main(int argc UNUSED_PARAM, char **argv)
57 && (argv[1] || ((c = argv[0][0]) != 'y' && c != 'n'))
62 /* We are a NOFORK applet.
63 * (Not that it's very useful, but code is trivially NOFORK-safe).
64 * Play nice. Do not leak anything.
67 if (!isatty(STDIN_FILENO))
68 bb_simple_error_msg_and_die("not a tty");
70 xfstat(STDIN_FILENO, &sb, "stdin");
72 puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
75 m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
76 : sb.st_mode & ~(S_IWGRP|S_IWOTH);
77 if (fchmod(STDIN_FILENO, m) != 0)
78 bb_perror_nomsg_and_die();