net_watch_interfaces.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. * @file net_watch_interfaces.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Network interface watcher.
  32. *
  33. * Synopsis: net.watch_interfaces()
  34. * Description: reports network interface events. Transitions up when an event is detected, and
  35. * goes down waiting for the next event when net.watch_interfaces::nextevent() is called.
  36. * On startup, "added" events are reported for existing interfaces.
  37. * Variables:
  38. * string event_type - what happened with the interface: "added" or "removed". This may not be
  39. * consistent across events.
  40. * string devname - interface name
  41. * string bus - bus location, for example "pci:0000:06:00.0", "usb:2-1.3:1.0", or "unknown"
  42. *
  43. * Synopsis: net.watch_interfaces::nextevent()
  44. * Description: makes the watch_interfaces module transition down in order to report the next event.
  45. */
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <regex.h>
  49. #include <misc/debug.h>
  50. #include <misc/offset.h>
  51. #include <misc/parse_number.h>
  52. #include <misc/bsize.h>
  53. #include <structure/LinkedList1.h>
  54. #include <udevmonitor/NCDUdevManager.h>
  55. #include <ncd/NCDModule.h>
  56. #include <ncd/modules/event_template.h>
  57. #include <generated/blog_channel_ncd_net_watch_interfaces.h>
  58. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  59. struct device {
  60. char *ifname;
  61. char *devpath;
  62. uintmax_t ifindex;
  63. BStringMap removed_map;
  64. LinkedList1Node devices_list_node;
  65. };
  66. struct instance {
  67. NCDModuleInst *i;
  68. NCDUdevClient client;
  69. LinkedList1 devices_list;
  70. regex_t preg;
  71. event_template templ;
  72. };
  73. struct nextevent_instance {
  74. NCDModuleInst *i;
  75. };
  76. static void templ_func_free (struct instance *o);
  77. static struct device * find_device_by_ifname (struct instance *o, const char *ifname)
  78. {
  79. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  80. while (list_node) {
  81. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  82. if (!strcmp(device->ifname, ifname)) {
  83. return device;
  84. }
  85. list_node = LinkedList1Node_Next(list_node);
  86. }
  87. return NULL;
  88. }
  89. static struct device * find_device_by_devpath (struct instance *o, const char *devpath)
  90. {
  91. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  92. while (list_node) {
  93. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  94. if (!strcmp(device->devpath, devpath)) {
  95. return device;
  96. }
  97. list_node = LinkedList1Node_Next(list_node);
  98. }
  99. return NULL;
  100. }
  101. static void free_device (struct instance *o, struct device *device, int have_removed_map)
  102. {
  103. // remove from devices list
  104. LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
  105. // free removed map
  106. if (have_removed_map) {
  107. BStringMap_Free(&device->removed_map);
  108. }
  109. // free devpath
  110. free(device->devpath);
  111. // free ifname
  112. free(device->ifname);
  113. // free structure
  114. free(device);
  115. }
  116. static int make_event_map (struct instance *o, int added, const char *ifname, const char *bus, BStringMap *out_map)
  117. {
  118. // init map
  119. BStringMap map;
  120. BStringMap_Init(&map);
  121. // set type
  122. if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
  123. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  124. goto fail1;
  125. }
  126. // set ifname
  127. if (!BStringMap_Set(&map, "devname", ifname)) {
  128. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  129. goto fail1;
  130. }
  131. // set bus
  132. if (!BStringMap_Set(&map, "bus", bus)) {
  133. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  134. goto fail1;
  135. }
  136. *out_map = map;
  137. return 1;
  138. fail1:
  139. BStringMap_Free(&map);
  140. return 0;
  141. }
  142. static void queue_event (struct instance *o, BStringMap map)
  143. {
  144. // pass event to template
  145. int was_empty;
  146. event_template_queue(&o->templ, map, &was_empty);
  147. // if event queue was empty, stop receiving udev events
  148. if (was_empty) {
  149. NCDUdevClient_Pause(&o->client);
  150. }
  151. }
  152. static void add_device (struct instance *o, const char *ifname, const char *devpath, uintmax_t ifindex, const char *bus)
  153. {
  154. ASSERT(!find_device_by_ifname(o, ifname))
  155. ASSERT(!find_device_by_devpath(o, devpath))
  156. // allocate structure
  157. struct device *device = malloc(sizeof(*device));
  158. if (!device) {
  159. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  160. goto fail0;
  161. }
  162. // init ifname
  163. if (!(device->ifname = strdup(ifname))) {
  164. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  165. goto fail1;
  166. }
  167. // init devpath
  168. if (!(device->devpath = strdup(devpath))) {
  169. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  170. goto fail2;
  171. }
  172. // set ifindex
  173. device->ifindex = ifindex;
  174. // init removed map
  175. if (!make_event_map(o, 0, ifname, bus, &device->removed_map)) {
  176. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  177. goto fail3;
  178. }
  179. // init added map
  180. BStringMap added_map;
  181. if (!make_event_map(o, 1, ifname, bus, &added_map)) {
  182. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  183. goto fail4;
  184. }
  185. // insert to devices list
  186. LinkedList1_Append(&o->devices_list, &device->devices_list_node);
  187. // queue event
  188. queue_event(o, added_map);
  189. return;
  190. fail4:
  191. BStringMap_Free(&device->removed_map);
  192. fail3:
  193. free(device->devpath);
  194. fail2:
  195. free(device->ifname);
  196. fail1:
  197. free(device);
  198. fail0:
  199. ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", ifname);
  200. }
  201. static void remove_device (struct instance *o, struct device *device)
  202. {
  203. queue_event(o, device->removed_map);
  204. free_device(o, device, 0);
  205. }
  206. static void next_event (struct instance *o)
  207. {
  208. event_template_assert_enabled(&o->templ);
  209. // order template to finish the current event
  210. int is_empty;
  211. event_template_dequeue(&o->templ, &is_empty);
  212. // if template has no events, continue udev events
  213. if (is_empty) {
  214. NCDUdevClient_Continue(&o->client);
  215. }
  216. }
  217. static void make_bus (struct instance *o, const char *devpath, const BStringMap *map, char *out_bus, size_t bus_avail)
  218. {
  219. const char *type = BStringMap_Get(map, "ID_BUS");
  220. if (!type) {
  221. goto fail;
  222. }
  223. size_t type_len = strlen(type);
  224. if (strcmp(type, "pci") && strcmp(type, "usb")) {
  225. goto fail;
  226. }
  227. regmatch_t pmatch[2];
  228. if (regexec(&o->preg, devpath, 2, pmatch, 0)) {
  229. goto fail;
  230. }
  231. const char *id = devpath + pmatch[1].rm_so;
  232. size_t id_len = pmatch[1].rm_eo - pmatch[1].rm_so;
  233. bsize_t bus_len = bsize_add(bsize_fromsize(type_len), bsize_add(bsize_fromint(1), bsize_add(bsize_fromsize(id_len), bsize_fromint(1))));
  234. if (bus_len.is_overflow || bus_len.value > bus_avail) {
  235. goto fail;
  236. }
  237. memcpy(out_bus, type, type_len);
  238. out_bus[type_len] = ':';
  239. memcpy(out_bus + type_len + 1, id, id_len);
  240. out_bus[type_len + 1 + id_len] = '\0';
  241. return;
  242. fail:
  243. snprintf(out_bus, bus_avail, "%s", "unknown");
  244. }
  245. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  246. {
  247. // lookup existing device with this devpath
  248. struct device *ex_device = find_device_by_devpath(o, devpath);
  249. // lookup cache entry
  250. const BStringMap *cache_map = NCDUdevManager_Query(o->i->umanager, devpath);
  251. if (!cache_map) {
  252. if (ex_device) {
  253. remove_device(o, ex_device);
  254. }
  255. goto out;
  256. }
  257. const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
  258. const char *interface = BStringMap_Get(cache_map, "INTERFACE");
  259. const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
  260. uintmax_t ifindex;
  261. if (!(subsystem && !strcmp(subsystem, "net") && interface && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
  262. if (ex_device) {
  263. remove_device(o, ex_device);
  264. }
  265. goto out;
  266. }
  267. if (ex_device && (strcmp(ex_device->ifname, interface) || ex_device->ifindex != ifindex)) {
  268. remove_device(o, ex_device);
  269. ex_device = NULL;
  270. }
  271. if (!ex_device) {
  272. struct device *ex_ifname_device = find_device_by_ifname(o, interface);
  273. if (ex_ifname_device) {
  274. remove_device(o, ex_ifname_device);
  275. }
  276. char bus[128];
  277. make_bus(o, devpath, cache_map, bus, sizeof(bus));
  278. add_device(o, interface, devpath, ifindex, bus);
  279. }
  280. out:
  281. free(devpath);
  282. if (have_map) {
  283. BStringMap_Free(&map);
  284. }
  285. }
  286. static void func_new (NCDModuleInst *i)
  287. {
  288. // allocate instance
  289. struct instance *o = malloc(sizeof(*o));
  290. if (!o) {
  291. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  292. goto fail0;
  293. }
  294. NCDModuleInst_Backend_SetUser(i, o);
  295. // init arguments
  296. o->i = i;
  297. // check arguments
  298. if (!NCDValue_ListRead(o->i->args, 0)) {
  299. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  300. goto fail1;
  301. }
  302. // init client
  303. NCDUdevClient_Init(&o->client, o->i->umanager, o, (NCDUdevClient_handler)client_handler);
  304. // init devices list
  305. LinkedList1_Init(&o->devices_list);
  306. // compile regex
  307. if (regcomp(&o->preg, "/([^/]+)/net/", REG_EXTENDED)) {
  308. ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
  309. goto fail2;
  310. }
  311. event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
  312. return;
  313. fail2:
  314. NCDUdevClient_Free(&o->client);
  315. fail1:
  316. free(o);
  317. fail0:
  318. NCDModuleInst_Backend_SetError(i);
  319. NCDModuleInst_Backend_Dead(i);
  320. }
  321. static void templ_func_free (struct instance *o)
  322. {
  323. NCDModuleInst *i = o->i;
  324. // free devices
  325. LinkedList1Node *list_node;
  326. while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
  327. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  328. free_device(o, device, 1);
  329. }
  330. // free regex
  331. regfree(&o->preg);
  332. // free client
  333. NCDUdevClient_Free(&o->client);
  334. // free instance
  335. free(o);
  336. NCDModuleInst_Backend_Dead(i);
  337. }
  338. static void func_die (void *vo)
  339. {
  340. struct instance *o = vo;
  341. event_template_die(&o->templ);
  342. }
  343. static int func_getvar (void *vo, const char *name, NCDValue *out)
  344. {
  345. struct instance *o = vo;
  346. return event_template_getvar(&o->templ, name, out);
  347. }
  348. static void nextevent_func_new (NCDModuleInst *i)
  349. {
  350. // allocate instance
  351. struct nextevent_instance *o = malloc(sizeof(*o));
  352. if (!o) {
  353. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  354. goto fail0;
  355. }
  356. NCDModuleInst_Backend_SetUser(i, o);
  357. // init arguments
  358. o->i = i;
  359. // check arguments
  360. if (!NCDValue_ListRead(o->i->args, 0)) {
  361. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  362. goto fail1;
  363. }
  364. // get method object
  365. struct instance *mo = i->method_object->inst_user;
  366. event_template_assert_enabled(&mo->templ);
  367. // signal up.
  368. // Do it before finishing the event so our process does not advance any further if
  369. // we would be killed the event provider going down.
  370. NCDModuleInst_Backend_Up(o->i);
  371. // wait for next event
  372. next_event(mo);
  373. return;
  374. fail1:
  375. free(o);
  376. fail0:
  377. NCDModuleInst_Backend_SetError(i);
  378. NCDModuleInst_Backend_Dead(i);
  379. }
  380. static void nextevent_func_die (void *vo)
  381. {
  382. struct nextevent_instance *o = vo;
  383. NCDModuleInst *i = o->i;
  384. // free instance
  385. free(o);
  386. NCDModuleInst_Backend_Dead(i);
  387. }
  388. static const struct NCDModule modules[] = {
  389. {
  390. .type = "net.watch_interfaces",
  391. .func_new = func_new,
  392. .func_die = func_die,
  393. .func_getvar = func_getvar
  394. }, {
  395. .type = "net.watch_interfaces::nextevent",
  396. .func_new = nextevent_func_new,
  397. .func_die = nextevent_func_die
  398. }, {
  399. .type = NULL
  400. }
  401. };
  402. const struct NCDModuleGroup ncdmodule_net_watch_interfaces = {
  403. .modules = modules
  404. };