busybox/miscutils/rfkill.c

150 lines
3.9 KiB
C
Raw Normal View History

/* vi: set sw=4 ts=4: */
/*
* rfkill implementation for busybox
*
* Copyright (C) 2010 Malek Degachi <malek-degachi@laposte.net>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
//config:config RFKILL
//config: bool "rfkill (4.4 kb)"
//config: default n # doesn't build on Ubuntu 9.04
//config: select PLATFORM_LINUX
//config: help
//config: Enable/disable wireless devices.
//config:
//config: rfkill list : list all wireless devices
//config: rfkill list bluetooth : list all bluetooth devices
//config: rfkill list 1 : list device corresponding to the given index
//config: rfkill block|unblock wlan : block/unblock all wlan(wifi) devices
//config:
//applet:IF_RFKILL(APPLET(rfkill, BB_DIR_USR_SBIN, BB_SUID_DROP))
//kbuild:lib-$(CONFIG_RFKILL) += rfkill.o
//usage:#define rfkill_trivial_usage
//usage: "COMMAND [INDEX|TYPE]"
//usage:#define rfkill_full_usage "\n\n"
//usage: "Enable/disable wireless devices\n"
//usage: "\nCommands:"
//usage: "\n list [INDEX|TYPE] List current state"
//usage: "\n block INDEX|TYPE Disable device"
//usage: "\n unblock INDEX|TYPE Enable device"
//usage: "\n"
//usage: "\n TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
//usage: "\n wimax, wwan, gps, fm"
#include "libbb.h"
#include <linux/rfkill.h>
enum {
OPT_b = (1 << 0), /* must be = 1 */
OPT_u = (1 << 1),
OPT_l = (1 << 2),
};
int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int rfkill_main(int argc UNUSED_PARAM, char **argv)
{
struct rfkill_event event;
const char *rf_name;
int rf_fd;
int mode;
int rf_type;
int rf_idx;
unsigned rf_opt = 0;
argv++;
/* Must have one or two params */
if (!argv[0] || (argv[1] && argv[2]))
bb_show_usage();
mode = O_RDWR | O_NONBLOCK;
rf_name = argv[1];
if (strcmp(argv[0], "list") == 0) {
rf_opt |= OPT_l;
mode = O_RDONLY | O_NONBLOCK;
} else if (strcmp(argv[0], "block") == 0 && rf_name) {
rf_opt |= OPT_b;
} else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
rf_opt |= OPT_u;
} else
bb_show_usage();
rf_type = RFKILL_TYPE_ALL;
rf_idx = -1;
if (rf_name) {
static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
if (strcmp(rf_name, "wifi") == 0)
rf_name = "wlan";
if (strcmp(rf_name, "ultrawideband") == 0)
rf_name = "uwb";
rf_type = index_in_strings(rfkill_types, rf_name);
if (rf_type < 0) {
rf_idx = xatoi_positive(rf_name);
}
}
rf_fd = device_open("/dev/rfkill", mode);
if (rf_fd < 0)
libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
bb_simple_perror_msg_and_die("/dev/rfkill");
if (rf_opt & OPT_l) {
while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
parser_t *parser;
char *tokens[2];
char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
char *name, *type;
if (rf_type && rf_type != event.type && rf_idx < 0) {
continue;
}
if (rf_idx >= 0 && event.idx != rf_idx) {
continue;
}
name = NULL;
type = NULL;
sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
parser = config_open2(rf_sysfs, fopen_for_read);
while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
name = xstrdup(tokens[1]);
continue;
}
if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
type = xstrdup(tokens[1]);
continue;
}
}
config_close(parser);
printf("%u: %s: %s\n", event.idx, name, type);
printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
free(name);
free(type);
}
} else {
memset(&event, 0, sizeof(event));
if (rf_type >= 0) {
event.type = rf_type;
event.op = RFKILL_OP_CHANGE_ALL;
}
if (rf_idx >= 0) {
event.idx = rf_idx;
event.op = RFKILL_OP_CHANGE;
}
/* Note: OPT_b == 1 */
event.soft = (rf_opt & OPT_b);
xwrite(rf_fd, &event, sizeof(event));
}
return EXIT_SUCCESS;
}