net_backend_rfkill.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @file net_backend_rfkill.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Rfkill monitoring module.
  25. *
  26. * Synopsis: net.backend.rfkill(string type, string name)
  27. * Arguments:
  28. * type - method of determining the index of the rfkill device. "index" for
  29. * rfkill device index, "wlan" for wireless device. Be aware that, for
  30. * the wireless device method, the index is resloved at initialization,
  31. * and no attempt is made to refresh it if the device goes away. In other
  32. * words, you should probably put a "net.backend.waitdevice" statement
  33. * in front of the rfkill statement.
  34. * name - rfkill index or wireless device name
  35. */
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <inttypes.h>
  39. #include <stdio.h>
  40. #include <sys/types.h>
  41. #include <dirent.h>
  42. #include <misc/string_begins_with.h>
  43. #include <ncd/NCDModule.h>
  44. #include <ncd/NCDRfkillMonitor.h>
  45. #include <generated/blog_channel_ncd_net_backend_rfkill.h>
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. struct instance {
  48. NCDModuleInst *i;
  49. uint32_t index;
  50. NCDRfkillMonitor monitor;
  51. int up;
  52. };
  53. static int find_wlan_rfill (const char *ifname, uint32_t *out_index)
  54. {
  55. char ieee_path[100];
  56. snprintf(ieee_path, sizeof(ieee_path), "/sys/class/net/%s/../../ieee80211", ifname);
  57. int res = 0;
  58. DIR *d = opendir(ieee_path);
  59. if (!d) {
  60. goto fail0;
  61. }
  62. struct dirent *e;
  63. while (e = readdir(d)) {
  64. if (!string_begins_with(e->d_name, "phy")) {
  65. continue;
  66. }
  67. char phy_path[150];
  68. snprintf(phy_path, sizeof(phy_path), "%s/%s", ieee_path, e->d_name);
  69. DIR *d2 = opendir(phy_path);
  70. if (!d2) {
  71. continue;
  72. }
  73. struct dirent *e2;
  74. while (e2 = readdir(d2)) {
  75. int index_pos;
  76. if (!(index_pos = string_begins_with(e2->d_name, "rfkill"))) {
  77. continue;
  78. }
  79. uint32_t index;
  80. if (sscanf(e2->d_name + index_pos, "%"SCNu32, &index) != 1) {
  81. continue;
  82. }
  83. res = 1;
  84. *out_index = index;
  85. }
  86. closedir(d2);
  87. }
  88. closedir(d);
  89. fail0:
  90. return res;
  91. }
  92. static void monitor_handler (struct instance *o, struct rfkill_event event)
  93. {
  94. if (event.idx != o->index) {
  95. return;
  96. }
  97. int was_up = o->up;
  98. o->up = (event.op != RFKILL_OP_DEL && !event.soft && !event.hard);
  99. if (o->up && !was_up) {
  100. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  101. }
  102. else if (!o->up && was_up) {
  103. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  104. }
  105. }
  106. static void * func_new (NCDModuleInst *i)
  107. {
  108. // allocate instance
  109. struct instance *o = malloc(sizeof(*o));
  110. if (!o) {
  111. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  112. goto fail0;
  113. }
  114. // init arguments
  115. o->i = i;
  116. // check arguments
  117. NCDValue *type_arg;
  118. NCDValue *name_arg;
  119. if (!NCDValue_ListRead(i->args, 2, &type_arg, &name_arg)) {
  120. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  121. goto fail1;
  122. }
  123. if (NCDValue_Type(type_arg) != NCDVALUE_STRING || NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  124. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  125. goto fail1;
  126. }
  127. char *type = NCDValue_StringValue(type_arg);
  128. char *name = NCDValue_StringValue(name_arg);
  129. if (!strcmp(type, "index")) {
  130. if (sscanf(name, "%"SCNu32, &o->index) != 1) {
  131. ModuleLog(o->i, BLOG_ERROR, "wrong index argument");
  132. goto fail1;
  133. }
  134. }
  135. else if (!strcmp(type, "wlan")) {
  136. if (!find_wlan_rfill(name, &o->index)) {
  137. ModuleLog(o->i, BLOG_ERROR, "failed to find rfkill for wlan interface");
  138. goto fail1;
  139. }
  140. }
  141. else {
  142. ModuleLog(o->i, BLOG_ERROR, "unknown type argument");
  143. goto fail1;
  144. }
  145. // init monitor
  146. if (!NCDRfkillMonitor_Init(&o->monitor, o->i->reactor, (NCDRfkillMonitor_handler)monitor_handler, o)) {
  147. ModuleLog(o->i, BLOG_ERROR, "monitor failed");
  148. goto fail1;
  149. }
  150. // set not up
  151. o->up = 0;
  152. return o;
  153. fail1:
  154. free(o);
  155. fail0:
  156. return NULL;
  157. }
  158. static void func_free (void *vo)
  159. {
  160. struct instance *o = vo;
  161. // free monitor
  162. NCDRfkillMonitor_Free(&o->monitor);
  163. // free instance
  164. free(o);
  165. }
  166. static const struct NCDModule modules[] = {
  167. {
  168. .type = "net.backend.rfkill",
  169. .func_new = func_new,
  170. .func_free = func_free
  171. }, {
  172. .type = NULL
  173. }
  174. };
  175. const struct NCDModuleGroup ncdmodule_net_backend_rfkill = {
  176. .modules = modules
  177. };