annotate toys/other/rfkill.c @ 1727:c0ef9b7976f0 draft

Use xsignal() instead of signal().
author Rob Landley <rob@landley.net>
date Tue, 10 Mar 2015 11:07:28 -0500
parents 3ac823675413
children 57f2a26fa92c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
1 /* rfkill.c - Enable/disable wireless devices.
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
2 *
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
3 * Copyright 2014 Ranjan Kumar <ranjankumar.bth@gmail.com>
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com>
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
5 *
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
6 * No Standard
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
7
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
8 USE_RFKILL(NEWTOY(rfkill, "<1>2", TOYFLAG_SBIN))
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
9
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
10 config RFKILL
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
11 bool "rfkill"
1346
ef63b63230ee Promote rfkill.
Rob Landley <rob@landley.net>
parents: 1344
diff changeset
12 default y
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
13 help
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
14 Usage: rfkill COMMAND [DEVICE]
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
15
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
16 Enable/disable wireless devices.
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
17
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
18 Commands:
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
19 list [DEVICE] List current state
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
20 block DEVICE Disable device
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
21 unblock DEVICE Enable device
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
22
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
23 DEVICE is an index number, or one of:
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
24 all, wlan(wifi), bluetooth, uwb(ultrawideband), wimax, wwan, gps, fm.
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
25 */
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
26
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
27 #define FOR_rfkill
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
28 #include "toys.h"
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
29 #include <linux/rfkill.h>
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
30
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
31 void rfkill_main(void)
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
32 {
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
33 struct rfkill_event rfevent;
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
34 int fd, tvar, idx = -1, tid = RFKILL_TYPE_ALL;
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
35 char **optargs = toys.optargs;
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
36
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
37 // Parse command line options
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
38 for (tvar = 0; tvar < 3; tvar++)
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
39 if (!strcmp((char *[]){"list", "block", "unblock"}[tvar], *optargs)) break;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
40 if (tvar == 3) error_exit("unknown cmd '%s'", *optargs);
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
41 if (tvar) {
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
42 int i;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
43 struct arglist {
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
44 char *name;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
45 int idx;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
46 } rftypes[] = {{"all", RFKILL_TYPE_ALL}, {"wifi", RFKILL_TYPE_WLAN},
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
47 {"wlan", RFKILL_TYPE_WLAN}, {"bluetooth", RFKILL_TYPE_BLUETOOTH},
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
48 {"uwb", RFKILL_TYPE_UWB}, {"ultrawideband", RFKILL_TYPE_UWB},
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
49 {"wimax", RFKILL_TYPE_WIMAX}, {"wwan", RFKILL_TYPE_WWAN},
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
50 {"gps", RFKILL_TYPE_GPS}, {"fm", 7}}; // RFKILL_TYPE_FM = 7
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
51
1718
3ac823675413 Fix several printf_format warnings.
Rob Landley <rob@landley.net>
parents: 1346
diff changeset
52 if (!*++optargs) error_exit("'%s' needs IDENTIFIER", optargs[-1]);
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
53 for (i = 0; i < ARRAY_LEN(rftypes); i++)
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
54 if (!strcmp(rftypes[i].name, *optargs)) break;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
55 if (i == ARRAY_LEN(rftypes)) idx = atolx_range(*optargs, 0, INT_MAX);
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
56 else tid = rftypes[i].idx;
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
57 }
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
58
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
59 // Perform requested action
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
60 fd = xopen("/dev/rfkill", (tvar ? O_RDWR : O_RDONLY)|O_NONBLOCK);
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
61 if (tvar) {
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
62 // block/unblock
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
63 memset(&rfevent, 0, sizeof(rfevent));
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
64 rfevent.soft = tvar == 1;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
65 if (idx >= 0) {
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
66 rfevent.idx = idx;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
67 rfevent.op = RFKILL_OP_CHANGE;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
68 } else {
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
69 rfevent.type = tid;
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
70 rfevent.op = RFKILL_OP_CHANGE_ALL;
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
71 }
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
72 xwrite(fd, &rfevent, sizeof(rfevent));
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
73 } else {
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
74 // show list.
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
75 while (sizeof(rfevent) == readall(fd, &rfevent, sizeof(rfevent))) {
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
76 char *line, *name = 0, *type = 0;
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
77
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
78 // filter list items
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
79 if ((tid > 0 && tid != rfevent.type) || (idx != -1 && idx != rfevent.idx))
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
80 continue;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
81
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
82 sprintf(toybuf, "/sys/class/rfkill/rfkill%u/uevent", rfevent.idx);
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
83 tvar = xopen(toybuf, O_RDONLY);
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
84 while ((line = get_line(tvar))) {
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
85 char *s = line;
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
86
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
87 if (strstart(&s, "RFKILL_NAME=")) name = xstrdup(s);
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
88 else if (strstart(&s, "RFKILL_TYPE=")) type = xstrdup(s);
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
89
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
90 free(line);
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
91 }
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
92 xclose(tvar);
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
93
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
94 xprintf("%u: %s: %s\n", rfevent.idx, name, type);
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
95 xprintf("\tSoft blocked: %s\n", rfevent.soft ? "yes" : "no");
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
96 xprintf("\tHard blocked: %s\n", rfevent.hard ? "yes" : "no");
1344
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
97 free(name);
788c6c097fa2 Cleanup pass on rfkill.
Rob Landley <rob@landley.net>
parents: 1307
diff changeset
98 free(type);
1307
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
99 }
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
100 }
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
101 xclose(fd);
8868e482963e rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
102 }