2009-05-28 11:39:20 +02:00
|
|
|
/*
|
2017-06-24 20:03:38 +01:00
|
|
|
* /dev/rfkill userspace tool
|
|
|
|
*
|
|
|
|
* Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
|
|
|
|
* Copyright 2009 Marcel Holtmann <marcel@holtmann.org>
|
|
|
|
* Copyright 2009 Tim Gardner <tim.gardner@canonical.com>
|
2017-08-31 10:36:51 +02:00
|
|
|
* Copyright 2017 Sami Kerola <kerolasa@iki.fi>
|
2017-06-24 20:03:38 +01:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2009-05-28 11:39:20 +02:00
|
|
|
*/
|
|
|
|
|
2009-07-23 10:53:47 -06:00
|
|
|
#include <ctype.h>
|
2017-06-24 20:42:58 +01:00
|
|
|
#include <getopt.h>
|
2017-06-25 09:42:06 +01:00
|
|
|
#include <libsmartcols.h>
|
2017-06-24 20:42:58 +01:00
|
|
|
#include <linux/rfkill.h>
|
2009-06-04 20:28:39 +02:00
|
|
|
#include <sys/poll.h>
|
2017-06-25 12:12:40 +01:00
|
|
|
#include <sys/syslog.h>
|
2009-12-03 14:28:03 +01:00
|
|
|
#include <sys/time.h>
|
2009-05-28 11:39:20 +02:00
|
|
|
|
2017-06-24 20:19:26 +01:00
|
|
|
#include "c.h"
|
|
|
|
#include "closestream.h"
|
|
|
|
#include "nls.h"
|
2017-06-25 09:42:06 +01:00
|
|
|
#include "optutils.h"
|
2017-06-24 20:42:58 +01:00
|
|
|
#include "pathnames.h"
|
|
|
|
#include "strutils.h"
|
2017-06-25 11:33:57 +01:00
|
|
|
#include "timeutils.h"
|
2017-06-24 20:42:58 +01:00
|
|
|
#include "widechar.h"
|
2017-06-25 09:42:06 +01:00
|
|
|
#include "xalloc.h"
|
2017-06-24 20:42:58 +01:00
|
|
|
|
2017-08-31 20:40:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NFC supported by kernel since v3.10 (year 2013); FM and another types are from
|
|
|
|
* year 2009 (2.6.33) or older.
|
|
|
|
*/
|
|
|
|
#ifndef RFKILL_TYPE_NFC
|
|
|
|
# define RFKILL_TYPE_NFC RFKILL_TYPE_FM + 1
|
|
|
|
#endif
|
|
|
|
|
2017-06-24 20:42:58 +01:00
|
|
|
struct rfkill_type_str {
|
|
|
|
enum rfkill_type type;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct rfkill_type_str rfkill_type_strings[] = {
|
|
|
|
{ .type = RFKILL_TYPE_ALL, .name = "all" },
|
|
|
|
{ .type = RFKILL_TYPE_WLAN, .name = "wifi" },
|
|
|
|
{ .type = RFKILL_TYPE_WLAN, .name = "wlan" }, /* alias */
|
|
|
|
{ .type = RFKILL_TYPE_BLUETOOTH, .name = "bluetooth" },
|
|
|
|
{ .type = RFKILL_TYPE_UWB, .name = "uwb" },
|
|
|
|
{ .type = RFKILL_TYPE_UWB, .name = "ultrawideband" }, /* alias */
|
|
|
|
{ .type = RFKILL_TYPE_WIMAX, .name = "wimax" },
|
|
|
|
{ .type = RFKILL_TYPE_WWAN, .name = "wwan" },
|
|
|
|
{ .type = RFKILL_TYPE_GPS, .name = "gps" },
|
|
|
|
{ .type = RFKILL_TYPE_FM, .name = "fm" },
|
|
|
|
{ .type = RFKILL_TYPE_NFC, .name = "nfc" },
|
|
|
|
{ .type = NUM_RFKILL_TYPES, .name = NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rfkill_id {
|
|
|
|
union {
|
|
|
|
enum rfkill_type type;
|
|
|
|
uint32_t index;
|
|
|
|
};
|
|
|
|
enum {
|
|
|
|
RFKILL_IS_INVALID,
|
|
|
|
RFKILL_IS_TYPE,
|
|
|
|
RFKILL_IS_INDEX,
|
2017-06-25 01:04:41 +01:00
|
|
|
RFKILL_IS_ALL
|
2017-06-24 20:42:58 +01:00
|
|
|
} result;
|
|
|
|
};
|
2009-05-28 11:39:20 +02:00
|
|
|
|
2017-10-03 13:44:19 +02:00
|
|
|
/* supported actions */
|
|
|
|
enum {
|
|
|
|
ACT_LIST,
|
|
|
|
ACT_HELP,
|
|
|
|
ACT_EVENT,
|
|
|
|
ACT_BLOCK,
|
|
|
|
ACT_UNBLOCK
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *rfkill_actions[] = {
|
|
|
|
[ACT_LIST] = "list",
|
|
|
|
[ACT_HELP] = "help",
|
|
|
|
[ACT_EVENT] = "event",
|
|
|
|
[ACT_BLOCK] = "block",
|
|
|
|
[ACT_UNBLOCK] = "unblock"
|
|
|
|
};
|
|
|
|
|
2017-06-25 09:42:06 +01:00
|
|
|
/* column IDs */
|
|
|
|
enum {
|
|
|
|
COL_DEVICE,
|
|
|
|
COL_ID,
|
|
|
|
COL_TYPE,
|
|
|
|
COL_SOFT,
|
|
|
|
COL_HARD
|
|
|
|
};
|
|
|
|
|
|
|
|
/* column names */
|
|
|
|
struct colinfo {
|
|
|
|
const char *name; /* header */
|
|
|
|
double whint; /* width hint (N < 1 is in percent of termwidth) */
|
|
|
|
int flags; /* SCOLS_FL_* */
|
|
|
|
const char *help;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* columns descriptions */
|
|
|
|
static const struct colinfo infos[] = {
|
|
|
|
[COL_DEVICE] = {"DEVICE", 0, 0, N_("kernel device name")},
|
2017-10-03 13:01:56 +02:00
|
|
|
[COL_ID] = {"ID", 2, SCOLS_FL_RIGHT, N_("device identifier value")},
|
2017-06-25 09:42:06 +01:00
|
|
|
[COL_TYPE] = {"TYPE", 0, 0, N_("device type name that can be used as identifier")},
|
2017-10-03 13:01:56 +02:00
|
|
|
[COL_SOFT] = {"SOFT", 0, SCOLS_FL_RIGHT, N_("status of software block")},
|
|
|
|
[COL_HARD] = {"HARD", 0, SCOLS_FL_RIGHT, N_("status of hardware block")}
|
2017-06-25 09:42:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static int columns[ARRAY_SIZE(infos) * 2];
|
|
|
|
static size_t ncolumns;
|
|
|
|
|
|
|
|
struct control {
|
2017-09-11 20:55:57 +01:00
|
|
|
struct libscols_table *tb;
|
2017-06-25 09:42:06 +01:00
|
|
|
unsigned int
|
|
|
|
json:1,
|
|
|
|
no_headings:1,
|
|
|
|
raw:1;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int column_name_to_id(const char *name, size_t namesz)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
assert(name);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(infos); i++) {
|
|
|
|
const char *cn = infos[i].name;
|
|
|
|
|
|
|
|
if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
warnx(_("unknown column: %s"), name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_column_id(size_t num)
|
|
|
|
{
|
|
|
|
assert(num < ncolumns);
|
|
|
|
assert(columns[num] < (int)ARRAY_SIZE(infos));
|
|
|
|
return columns[num];
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct colinfo *get_column_info(int num)
|
|
|
|
{
|
|
|
|
return &infos[get_column_id(num)];
|
|
|
|
}
|
|
|
|
|
2017-10-03 13:44:19 +02:00
|
|
|
static int string_to_action(const char *str)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(rfkill_actions); i++)
|
|
|
|
if (strcmp(str, rfkill_actions[i]) == 0)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-06-24 20:42:58 +01:00
|
|
|
static int rfkill_event(void)
|
2009-06-04 20:28:39 +02:00
|
|
|
{
|
2009-07-05 14:33:25 +02:00
|
|
|
struct rfkill_event event;
|
2009-12-03 14:28:03 +01:00
|
|
|
struct timeval tv;
|
2017-06-25 11:33:57 +01:00
|
|
|
char date_buf[ISO_8601_BUFSIZ];
|
2009-06-04 20:28:39 +02:00
|
|
|
struct pollfd p;
|
|
|
|
ssize_t len;
|
2017-08-31 11:16:07 +02:00
|
|
|
int fd, n;
|
2009-06-04 20:28:39 +02:00
|
|
|
|
2017-06-24 20:42:58 +01:00
|
|
|
fd = open(_PATH_DEV_RFKILL, O_RDONLY);
|
2009-06-04 20:28:39 +02:00
|
|
|
if (fd < 0) {
|
2017-06-24 20:42:58 +01:00
|
|
|
warn(_("cannot open %s"), _PATH_DEV_RFKILL);
|
2017-08-31 11:16:07 +02:00
|
|
|
return -errno;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
p.fd = fd;
|
|
|
|
p.events = POLLIN | POLLHUP;
|
|
|
|
|
2017-08-31 11:16:07 +02:00
|
|
|
/* interrupted by signal only */
|
2009-06-04 20:28:39 +02:00
|
|
|
while (1) {
|
|
|
|
n = poll(&p, 1, -1);
|
|
|
|
if (n < 0) {
|
2017-06-24 20:42:58 +01:00
|
|
|
warn(_("failed to poll %s"), _PATH_DEV_RFKILL);
|
2017-08-31 11:16:07 +02:00
|
|
|
goto failed;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-07-05 14:33:25 +02:00
|
|
|
len = read(fd, &event, sizeof(event));
|
2009-06-04 20:28:39 +02:00
|
|
|
if (len < 0) {
|
2017-06-24 20:42:58 +01:00
|
|
|
warn(_("cannot read %s"), _PATH_DEV_RFKILL);
|
2017-08-31 11:16:07 +02:00
|
|
|
goto failed;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 09:15:12 +01:00
|
|
|
if (len < RFKILL_EVENT_SIZE_V1) {
|
|
|
|
warnx(_("wrong size of rfkill event: %zu < %d"), len, RFKILL_EVENT_SIZE_V1);
|
2009-06-04 20:28:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
2009-12-03 14:28:03 +01:00
|
|
|
gettimeofday(&tv, NULL);
|
2017-06-25 11:33:57 +01:00
|
|
|
strtimeval_iso(&tv,
|
|
|
|
ISO_8601_DATE |
|
|
|
|
ISO_8601_TIME |
|
|
|
|
ISO_8601_COMMAUSEC |
|
|
|
|
ISO_8601_TIMEZONE |
|
|
|
|
ISO_8601_SPACE, date_buf, sizeof(date_buf));
|
|
|
|
printf("%s: idx %u type %u op %u soft %u hard %u\n",
|
|
|
|
date_buf,
|
|
|
|
event.idx, event.type, event.op, event.soft, event.hard);
|
2009-10-29 13:57:27 +01:00
|
|
|
fflush(stdout);
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-31 11:16:07 +02:00
|
|
|
failed:
|
2009-06-04 20:28:39 +02:00
|
|
|
close(fd);
|
2017-08-31 11:16:07 +02:00
|
|
|
return -1;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-06-25 09:42:06 +01:00
|
|
|
static const char *get_name_or_type(uint32_t idx, int type)
|
2009-06-04 20:28:39 +02:00
|
|
|
{
|
2017-06-24 20:42:58 +01:00
|
|
|
static char name[128] = { 0 };
|
2009-06-04 20:28:39 +02:00
|
|
|
char *pos, filename[64];
|
|
|
|
int fd;
|
|
|
|
|
2017-06-25 09:42:06 +01:00
|
|
|
if (type)
|
|
|
|
pos = "type";
|
|
|
|
else
|
|
|
|
pos = "name";
|
2009-06-04 20:28:39 +02:00
|
|
|
snprintf(filename, sizeof(filename) - 1,
|
2017-06-25 09:42:06 +01:00
|
|
|
_PATH_SYS_RFKILL "/rfkill%u/%s", idx, pos);
|
2009-06-04 20:28:39 +02:00
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
2017-06-24 20:42:58 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
warn(_("cannot open %s"), filename);
|
2009-06-04 20:28:39 +02:00
|
|
|
return NULL;
|
2017-06-24 20:42:58 +01:00
|
|
|
}
|
2009-06-04 20:28:39 +02:00
|
|
|
|
2017-06-25 01:35:28 +01:00
|
|
|
if (read(fd, name, sizeof(name) - 1) < 0) {
|
|
|
|
warn(_("cannot read %s"), filename);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-06-04 20:28:39 +02:00
|
|
|
|
|
|
|
pos = strchr(name, '\n');
|
|
|
|
if (pos)
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2010-01-22 23:35:29 +00:00
|
|
|
static struct rfkill_id rfkill_id_to_type(const char *s)
|
|
|
|
{
|
|
|
|
const struct rfkill_type_str *p;
|
|
|
|
struct rfkill_id ret;
|
|
|
|
|
|
|
|
if (islower(*s)) {
|
|
|
|
for (p = rfkill_type_strings; p->name != NULL; p++) {
|
2017-06-24 20:42:58 +01:00
|
|
|
if (!strcmp(s, p->name)) {
|
2010-01-22 23:35:29 +00:00
|
|
|
ret.type = p->type;
|
2017-06-25 01:04:41 +01:00
|
|
|
if (!strcmp(s, "all"))
|
|
|
|
ret.result = RFKILL_IS_ALL;
|
|
|
|
else
|
|
|
|
ret.result = RFKILL_IS_TYPE;
|
2010-01-22 23:35:29 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isdigit(*s)) {
|
|
|
|
/* assume a numeric character implies an index. */
|
2017-07-04 21:43:56 +01:00
|
|
|
char filename[64];
|
|
|
|
|
2017-06-24 20:42:58 +01:00
|
|
|
ret.index = strtou32_or_err(s, _("invalid identifier"));
|
2017-07-04 21:43:56 +01:00
|
|
|
snprintf(filename, sizeof(filename) - 1,
|
|
|
|
_PATH_SYS_RFKILL "/rfkill%" PRIu32 "/name", ret.index);
|
|
|
|
if (access(filename, F_OK) == 0)
|
|
|
|
ret.result = RFKILL_IS_INDEX;
|
|
|
|
else
|
|
|
|
ret.result = RFKILL_IS_INVALID;
|
2010-01-22 23:35:29 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.result = RFKILL_IS_INVALID;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-25 09:42:06 +01:00
|
|
|
static void fill_table_row(struct libscols_table *tb, struct rfkill_event *event)
|
|
|
|
{
|
|
|
|
static struct libscols_line *ln;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
assert(tb);
|
|
|
|
|
|
|
|
ln = scols_table_new_line(tb, NULL);
|
|
|
|
if (!ln) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
errx(EXIT_FAILURE, _("failed to allocate output line"));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < (size_t)ncolumns; i++) {
|
|
|
|
char *str = NULL;
|
|
|
|
switch (get_column_id(i)) {
|
|
|
|
case COL_DEVICE:
|
|
|
|
str = xstrdup(get_name_or_type(event->idx, 0));
|
|
|
|
break;
|
|
|
|
case COL_ID:
|
|
|
|
xasprintf(&str, "%" PRIu32, event->idx);
|
|
|
|
break;
|
|
|
|
case COL_TYPE:
|
|
|
|
str = xstrdup(get_name_or_type(event->idx, 1));
|
|
|
|
break;
|
|
|
|
case COL_SOFT:
|
|
|
|
str = xstrdup(event->soft ? _("blocked") : _("unblocked"));
|
|
|
|
break;
|
|
|
|
case COL_HARD:
|
|
|
|
str = xstrdup(event->hard ? _("blocked") : _("unblocked"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (str && scols_line_refer_data(ln, i, str))
|
|
|
|
errx(EXIT_FAILURE, _("failed to add output data"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 20:55:57 +01:00
|
|
|
static void rfkill_list_init(struct control *ctrl)
|
2009-06-04 20:28:39 +02:00
|
|
|
{
|
2017-09-15 12:28:44 +02:00
|
|
|
size_t i;
|
|
|
|
|
2017-06-25 09:42:06 +01:00
|
|
|
scols_init_debug(0);
|
2017-09-15 12:28:44 +02:00
|
|
|
|
2017-09-11 20:55:57 +01:00
|
|
|
ctrl->tb = scols_new_table();
|
|
|
|
if (!ctrl->tb)
|
2017-06-25 09:42:06 +01:00
|
|
|
err(EXIT_FAILURE, _("failed to allocate output table"));
|
|
|
|
|
2017-09-11 20:55:57 +01:00
|
|
|
scols_table_enable_json(ctrl->tb, ctrl->json);
|
|
|
|
scols_table_enable_noheadings(ctrl->tb, ctrl->no_headings);
|
|
|
|
scols_table_enable_raw(ctrl->tb, ctrl->raw);
|
2017-06-25 09:42:06 +01:00
|
|
|
|
2017-09-15 12:28:44 +02:00
|
|
|
for (i = 0; i < (size_t) ncolumns; i++) {
|
|
|
|
const struct colinfo *col = get_column_info(i);
|
2017-06-25 09:42:06 +01:00
|
|
|
|
2017-09-15 12:28:44 +02:00
|
|
|
if (!scols_table_new_column(ctrl->tb, col->name, col->whint, col->flags))
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate output column"));
|
2017-06-25 09:42:06 +01:00
|
|
|
}
|
2017-09-11 20:55:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int rfkill_list_fill(struct control const *ctrl, const char *param)
|
|
|
|
{
|
|
|
|
struct rfkill_id id = { .result = RFKILL_IS_ALL };
|
|
|
|
struct rfkill_event event;
|
|
|
|
ssize_t len;
|
|
|
|
int fd;
|
2009-06-04 20:28:39 +02:00
|
|
|
|
2010-01-22 23:35:30 +00:00
|
|
|
if (param) {
|
|
|
|
id = rfkill_id_to_type(param);
|
|
|
|
if (id.result == RFKILL_IS_INVALID) {
|
2017-06-24 20:42:58 +01:00
|
|
|
warnx(_("invalid identifier: %s"), param);
|
2017-08-31 11:16:07 +02:00
|
|
|
return -EINVAL;
|
2010-01-22 23:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 20:42:58 +01:00
|
|
|
fd = open(_PATH_DEV_RFKILL, O_RDONLY);
|
2009-06-04 20:28:39 +02:00
|
|
|
if (fd < 0) {
|
2017-06-24 20:42:58 +01:00
|
|
|
warn(_("cannot open %s"), _PATH_DEV_RFKILL);
|
2017-08-31 11:16:07 +02:00
|
|
|
return -errno;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
|
2017-06-24 20:42:58 +01:00
|
|
|
warn(_("cannot set non-blocking %s"), _PATH_DEV_RFKILL);
|
2009-06-04 20:28:39 +02:00
|
|
|
close(fd);
|
2017-08-31 11:16:07 +02:00
|
|
|
return -errno;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2009-07-05 14:33:25 +02:00
|
|
|
len = read(fd, &event, sizeof(event));
|
2009-06-04 20:28:39 +02:00
|
|
|
if (len < 0) {
|
2017-08-31 11:16:07 +02:00
|
|
|
if (errno != EAGAIN)
|
|
|
|
warn(_("cannot read %s"), _PATH_DEV_RFKILL);
|
2009-06-04 20:28:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-07-01 09:15:12 +01:00
|
|
|
if (len < RFKILL_EVENT_SIZE_V1) {
|
|
|
|
warnx(_("wrong size of rfkill event: %zu < %d"), len, RFKILL_EVENT_SIZE_V1);
|
2009-06-04 20:28:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-07-05 14:33:25 +02:00
|
|
|
if (event.op != RFKILL_OP_ADD)
|
2009-06-04 20:28:39 +02:00
|
|
|
continue;
|
|
|
|
|
2010-01-22 23:35:30 +00:00
|
|
|
/* filter out unwanted results */
|
2017-06-24 20:42:58 +01:00
|
|
|
switch (id.result) {
|
2010-01-22 23:35:30 +00:00
|
|
|
case RFKILL_IS_TYPE:
|
|
|
|
if (event.type != id.type)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case RFKILL_IS_INDEX:
|
|
|
|
if (event.idx != id.index)
|
|
|
|
continue;
|
|
|
|
break;
|
2017-06-25 01:04:41 +01:00
|
|
|
case RFKILL_IS_ALL:
|
2017-06-24 20:42:58 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
2010-01-22 23:35:30 +00:00
|
|
|
}
|
2017-09-11 20:55:57 +01:00
|
|
|
fill_table_row(ctrl->tb, &event);
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
close(fd);
|
2010-01-22 23:35:28 +00:00
|
|
|
return 0;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-09-11 20:55:57 +01:00
|
|
|
static void rfkill_list_output(struct control const *ctrl)
|
|
|
|
{
|
|
|
|
scols_print_table(ctrl->tb);
|
|
|
|
scols_unref_table(ctrl->tb);
|
|
|
|
}
|
|
|
|
|
2017-06-24 20:42:58 +01:00
|
|
|
static int rfkill_block(uint8_t block, const char *param)
|
2009-06-04 20:28:39 +02:00
|
|
|
{
|
2010-01-22 23:35:29 +00:00
|
|
|
struct rfkill_id id;
|
2017-06-25 01:04:41 +01:00
|
|
|
struct rfkill_event event = {
|
|
|
|
.op = RFKILL_OP_CHANGE_ALL,
|
|
|
|
.soft = block,
|
|
|
|
0
|
|
|
|
};
|
2009-06-04 20:28:39 +02:00
|
|
|
ssize_t len;
|
|
|
|
int fd;
|
2017-06-25 12:12:40 +01:00
|
|
|
char *message = NULL;
|
2009-06-04 20:28:39 +02:00
|
|
|
|
2010-01-22 23:35:29 +00:00
|
|
|
id = rfkill_id_to_type(param);
|
2009-06-04 20:28:39 +02:00
|
|
|
|
2010-01-22 23:35:29 +00:00
|
|
|
switch (id.result) {
|
2017-06-25 01:04:41 +01:00
|
|
|
case RFKILL_IS_INVALID:
|
|
|
|
warnx(_("invalid identifier: %s"), param);
|
2017-08-31 11:16:07 +02:00
|
|
|
return -1;
|
2010-01-22 23:35:29 +00:00
|
|
|
case RFKILL_IS_TYPE:
|
|
|
|
event.type = id.type;
|
2017-06-25 12:12:40 +01:00
|
|
|
xasprintf(&message, "type %s", param);
|
2010-01-22 23:35:29 +00:00
|
|
|
break;
|
|
|
|
case RFKILL_IS_INDEX:
|
|
|
|
event.op = RFKILL_OP_CHANGE;
|
|
|
|
event.idx = id.index;
|
2017-06-25 12:12:40 +01:00
|
|
|
xasprintf(&message, "id %d", id.index);
|
2010-01-22 23:35:29 +00:00
|
|
|
break;
|
2017-06-25 01:04:41 +01:00
|
|
|
case RFKILL_IS_ALL:
|
2017-06-25 12:12:40 +01:00
|
|
|
message = xstrdup("all");
|
2017-06-24 20:42:58 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
2009-07-23 19:11:21 +02:00
|
|
|
}
|
2017-06-25 01:04:41 +01:00
|
|
|
|
|
|
|
fd = open(_PATH_DEV_RFKILL, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
warn(_("cannot open %s"), _PATH_DEV_RFKILL);
|
2017-06-25 12:12:40 +01:00
|
|
|
free(message);
|
2017-08-31 11:16:07 +02:00
|
|
|
return -errno;
|
2017-06-25 01:04:41 +01:00
|
|
|
}
|
2009-06-04 20:28:39 +02:00
|
|
|
|
|
|
|
len = write(fd, &event, sizeof(event));
|
|
|
|
if (len < 0)
|
2017-06-24 20:42:58 +01:00
|
|
|
warn(_("write failed: %s"), _PATH_DEV_RFKILL);
|
2017-08-31 11:16:07 +02:00
|
|
|
else {
|
|
|
|
openlog("rfkill", 0, LOG_USER);
|
|
|
|
syslog(LOG_NOTICE, "%s set for %s", block ? "block" : "unblock", message);
|
|
|
|
closelog();
|
|
|
|
}
|
2017-06-25 12:12:40 +01:00
|
|
|
free(message);
|
2017-09-11 20:55:58 +01:00
|
|
|
return close(fd);
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-06-24 20:42:58 +01:00
|
|
|
static void __attribute__((__noreturn__)) usage(void)
|
2009-06-04 20:28:39 +02:00
|
|
|
{
|
2017-06-25 09:42:06 +01:00
|
|
|
size_t i;
|
2010-01-25 22:33:30 +00:00
|
|
|
|
2017-06-24 20:42:58 +01:00
|
|
|
fputs(USAGE_HEADER, stdout);
|
2017-09-11 20:55:57 +01:00
|
|
|
fprintf(stdout, _(" %s [options] command [identifier ...]\n"), program_invocation_short_name);
|
2017-06-24 20:42:58 +01:00
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, stdout);
|
|
|
|
fputs(_("Tool for enabling and disabling wireless devices.\n"), stdout);
|
|
|
|
|
2017-06-25 09:42:06 +01:00
|
|
|
fputs(USAGE_OPTIONS, stdout);
|
|
|
|
fputs(_(" -J, --json use JSON output format\n"), stdout);
|
|
|
|
fputs(_(" -n, --noheadings don't print headings\n"), stdout);
|
|
|
|
fputs(_(" -o, --output <list> define which output columns to use\n"), stdout);
|
|
|
|
fputs(_(" -r, --raw use the raw output format\n"), stdout);
|
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, stdout);
|
2017-08-31 10:31:21 +02:00
|
|
|
printf(USAGE_HELP_OPTIONS(24));
|
2017-06-25 09:42:06 +01:00
|
|
|
|
2017-08-31 10:31:21 +02:00
|
|
|
fputs(USAGE_COLUMNS, stdout);
|
2017-06-25 09:42:06 +01:00
|
|
|
for (i = 0; i < ARRAY_SIZE(infos); i++)
|
|
|
|
fprintf(stdout, " %-6s %s\n", infos[i].name, _(infos[i].help));
|
|
|
|
|
2017-08-31 10:31:21 +02:00
|
|
|
fputs(USAGE_COMMANDS, stdout);
|
|
|
|
|
2017-06-24 20:42:58 +01:00
|
|
|
/*
|
|
|
|
* TRANSLATORS: command names should not be translated, explaining
|
|
|
|
* them as additional field after identifer is fine, for example
|
|
|
|
*
|
|
|
|
* list [identifier] (lista [tarkenne])
|
|
|
|
*/
|
|
|
|
fputs(_(" help\n"), stdout);
|
|
|
|
fputs(_(" event\n"), stdout);
|
|
|
|
fputs(_(" list [identifier]\n"), stdout);
|
|
|
|
fputs(_(" block identifier\n"), stdout);
|
|
|
|
fputs(_(" unblock identifier\n"), stdout);
|
|
|
|
|
|
|
|
fprintf(stdout, USAGE_MAN_TAIL("rfkill(8)"));
|
|
|
|
exit(EXIT_SUCCESS);
|
2009-05-28 11:39:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-06-25 09:42:06 +01:00
|
|
|
struct control ctrl = { 0 };
|
2017-10-03 13:44:19 +02:00
|
|
|
int c, act = ACT_LIST;
|
2017-06-25 09:42:06 +01:00
|
|
|
char *outarg = NULL;
|
2017-06-24 20:42:58 +01:00
|
|
|
static const struct option longopts[] = {
|
2017-06-25 09:42:06 +01:00
|
|
|
{ "json", no_argument, NULL, 'J' },
|
|
|
|
{ "noheadings", no_argument, NULL, 'n' },
|
|
|
|
{ "output", required_argument, NULL, 'o' },
|
|
|
|
{ "raw", no_argument, NULL, 'r' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2017-06-24 20:42:58 +01:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2017-06-25 09:42:06 +01:00
|
|
|
static const ul_excl_t excl[] = {
|
|
|
|
{'J', 'r'},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
|
2017-09-11 20:55:57 +01:00
|
|
|
int ret = 0;
|
2017-06-24 20:42:58 +01:00
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
atexit(close_stdout);
|
|
|
|
|
2017-06-25 09:42:06 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "Jno:rVh", longopts, NULL)) != -1) {
|
|
|
|
err_exclusive_options(c, longopts, excl, excl_st);
|
2017-06-24 20:42:58 +01:00
|
|
|
switch (c) {
|
2017-06-25 09:42:06 +01:00
|
|
|
case 'J':
|
|
|
|
ctrl.json = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
ctrl.no_headings = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
outarg = optarg;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
ctrl.raw = 1;
|
|
|
|
break;
|
2017-06-24 20:42:58 +01:00
|
|
|
case 'V':
|
|
|
|
printf(UTIL_LINUX_VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
default:
|
|
|
|
errtryhelp(EXIT_FAILURE);
|
|
|
|
}
|
2017-06-25 09:42:06 +01:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2017-10-03 13:44:19 +02:00
|
|
|
if (argc > 0) {
|
|
|
|
act = string_to_action(*argv);
|
|
|
|
if (act < 0)
|
|
|
|
errtryhelp(EXIT_FAILURE);
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (act) {
|
|
|
|
case ACT_LIST:
|
2017-06-25 09:42:06 +01:00
|
|
|
columns[ncolumns++] = COL_ID;
|
|
|
|
columns[ncolumns++] = COL_TYPE;
|
2017-10-03 13:01:56 +02:00
|
|
|
columns[ncolumns++] = COL_DEVICE;
|
2017-06-25 09:42:06 +01:00
|
|
|
columns[ncolumns++] = COL_SOFT;
|
|
|
|
columns[ncolumns++] = COL_HARD;
|
|
|
|
|
|
|
|
if (outarg
|
|
|
|
&& string_add_to_idarray(outarg, columns,
|
|
|
|
ARRAY_SIZE(columns), &ncolumns,
|
|
|
|
column_name_to_id) < 0)
|
|
|
|
return EXIT_FAILURE;
|
2017-10-03 13:44:19 +02:00
|
|
|
|
2017-09-11 20:55:57 +01:00
|
|
|
rfkill_list_init(&ctrl);
|
2017-10-03 13:44:19 +02:00
|
|
|
if (!argc)
|
|
|
|
ret |= rfkill_list_fill(&ctrl, NULL); /* ALL */
|
|
|
|
else while (argc) {
|
2017-09-11 20:55:57 +01:00
|
|
|
ret |= rfkill_list_fill(&ctrl, *argv);
|
2017-10-03 13:44:19 +02:00
|
|
|
argc--;
|
|
|
|
argv++;
|
2017-06-25 09:42:06 +01:00
|
|
|
}
|
2017-09-11 20:55:57 +01:00
|
|
|
rfkill_list_output(&ctrl);
|
2017-10-03 13:44:19 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACT_EVENT:
|
2017-06-24 20:42:58 +01:00
|
|
|
ret = rfkill_event();
|
2017-10-03 13:44:19 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACT_HELP:
|
2017-06-25 09:42:06 +01:00
|
|
|
usage();
|
2017-10-03 13:44:19 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACT_BLOCK:
|
|
|
|
while (argc) {
|
2017-09-11 20:55:57 +01:00
|
|
|
ret |= rfkill_block(1, *argv);
|
2017-10-03 13:44:19 +02:00
|
|
|
argc--;
|
2017-09-11 20:55:57 +01:00
|
|
|
argv++;
|
2017-10-03 13:44:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACT_UNBLOCK:
|
|
|
|
while (argc) {
|
2017-09-11 20:55:57 +01:00
|
|
|
ret |= rfkill_block(0, *argv);
|
2017-10-03 13:44:19 +02:00
|
|
|
argv++;
|
|
|
|
argc--;
|
2017-09-11 20:55:57 +01:00
|
|
|
}
|
2017-10-03 13:44:19 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-06-04 20:28:39 +02:00
|
|
|
|
2017-08-31 11:16:07 +02:00
|
|
|
return ret ? EXIT_FAILURE : EXIT_SUCCESS;
|
2009-05-28 11:39:20 +02:00
|
|
|
}
|