Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 2 | /* |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 3 | * mesg implementation for busybox |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 4 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 5 | * Copyright (c) 2002 Manuel Novoa III <mjn3@codepoet.org> |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame^] | 10 | //applet:IF_MESG(APPLET(mesg, _BB_DIR_USR_BIN, _BB_SUID_DROP)) |
| 11 | |
| 12 | //kbuild:lib-$(CONFIG_MESG) += mesg.o |
| 13 | |
| 14 | //config:config MESG |
| 15 | //config: bool "mesg" |
| 16 | //config: default y |
| 17 | //config: help |
| 18 | //config: Mesg controls access to your terminal by others. It is typically |
| 19 | //config: used to allow or disallow other users to write to your terminal |
| 20 | |
| 21 | //usage:#define mesg_trivial_usage |
| 22 | //usage: "[y|n]" |
| 23 | //usage:#define mesg_full_usage "\n\n" |
| 24 | //usage: "Control write access to your terminal\n" |
| 25 | //usage: " y Allow write access to your terminal\n" |
| 26 | //usage: " n Disallow write access to your terminal" |
| 27 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 28 | #include "libbb.h" |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef USE_TTY_GROUP |
| 31 | #define S_IWGRP_OR_S_IWOTH S_IWGRP |
| 32 | #else |
| 33 | #define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH) |
| 34 | #endif |
| 35 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 36 | int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 37 | int mesg_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 38 | { |
| 39 | struct stat sb; |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 40 | const char *tty; |
Eric Andersen | 7850014 | 2004-08-27 19:55:28 +0000 | [diff] [blame] | 41 | char c = 0; |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 42 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 43 | argv++; |
| 44 | |
| 45 | if (!argv[0] |
| 46 | || (!argv[1] && ((c = argv[0][0]) == 'y' || c == 'n')) |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 47 | ) { |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 48 | tty = xmalloc_ttyname(STDERR_FILENO); |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 49 | if (tty == NULL) { |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 50 | tty = "ttyname"; |
| 51 | } else if (stat(tty, &sb) == 0) { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 52 | mode_t m; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 53 | if (c == 0) { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 54 | puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n"); |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 55 | return EXIT_SUCCESS; |
| 56 | } |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 57 | m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH |
| 58 | : sb.st_mode & ~(S_IWGRP|S_IWOTH); |
| 59 | if (chmod(tty, m) == 0) { |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 60 | return EXIT_SUCCESS; |
| 61 | } |
| 62 | } |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 63 | bb_simple_perror_msg_and_die(tty); |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 64 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | bb_show_usage(); |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 66 | } |