net_backend_rfkill.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. NCDModuleInst_Backend_SetUser(i, o);
  115. // init arguments
  116. o->i = i;
  117. // check arguments
  118. NCDValue *type_arg;
  119. NCDValue *name_arg;
  120. if (!NCDValue_ListRead(i->args, 2, &type_arg, &name_arg)) {
  121. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  122. goto fail1;
  123. }
  124. if (NCDValue_Type(type_arg) != NCDVALUE_STRING || NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  125. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  126. goto fail1;
  127. }
  128. char *type = NCDValue_StringValue(type_arg);
  129. char *name = NCDValue_StringValue(name_arg);
  130. if (!strcmp(type, "index")) {
  131. if (sscanf(name, "%"SCNu32, &o->index) != 1) {
  132. ModuleLog(o->i, BLOG_ERROR, "wrong index argument");
  133. goto fail1;
  134. }
  135. }
  136. else if (!strcmp(type, "wlan")) {
  137. if (!find_wlan_rfill(name, &o->index)) {
  138. ModuleLog(o->i, BLOG_ERROR, "failed to find rfkill for wlan interface");
  139. goto fail1;
  140. }
  141. }
  142. else {
  143. ModuleLog(o->i, BLOG_ERROR, "unknown type argument");
  144. goto fail1;
  145. }
  146. // init monitor
  147. if (!NCDRfkillMonitor_Init(&o->monitor, o->i->reactor, (NCDRfkillMonitor_handler)monitor_handler, o)) {
  148. ModuleLog(o->i, BLOG_ERROR, "monitor failed");
  149. goto fail1;
  150. }
  151. // set not up
  152. o->up = 0;
  153. return;
  154. fail1:
  155. free(o);
  156. fail0:
  157. NCDModuleInst_Backend_SetError(i);
  158. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  159. }
  160. static void func_die (void *vo)
  161. {
  162. struct instance *o = vo;
  163. NCDModuleInst *i = o->i;
  164. // free monitor
  165. NCDRfkillMonitor_Free(&o->monitor);
  166. // free instance
  167. free(o);
  168. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  169. }
  170. static const struct NCDModule modules[] = {
  171. {
  172. .type = "net.backend.rfkill",
  173. .func_new = func_new,
  174. .func_die = func_die
  175. }, {
  176. .type = NULL
  177. }
  178. };
  179. const struct NCDModuleGroup ncdmodule_net_backend_rfkill = {
  180. .modules = modules
  181. };