sys_watch_usb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. * @file sys_watch_usb.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. * USB device watcher.
  25. *
  26. * Synopsis: sys.watch_usb()
  27. * Description: reports USB device events. Transitions up when an event is detected, and
  28. * goes down waiting for the next event when ->nextevent() is called.
  29. * On startup, "added" events are reported for existing USB devices.
  30. *
  31. * Variables:
  32. * string event_type - what happened with the USB device: "added" or "removed"
  33. * string devname - device node path, e.g. /dev/bus/usb/XXX/YYY
  34. * string vendor_id - vendor ID, e.g. 046d
  35. * string model_id - model ID, e.g. c03e
  36. *
  37. * Synopsis: sys.watch_usb::nextevent()
  38. * Description: makes the watch_usb module transition down in order to report the next event.
  39. */
  40. #include <inttypes.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <stdio.h>
  44. #include <misc/debug.h>
  45. #include <misc/offset.h>
  46. #include <misc/parse_number.h>
  47. #include <structure/LinkedList1.h>
  48. #include <udevmonitor/NCDUdevManager.h>
  49. #include <ncd/NCDModule.h>
  50. #include <ncd/modules/event_template.h>
  51. #include <generated/blog_channel_ncd_sys_watch_usb.h>
  52. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  53. struct device {
  54. char *devname;
  55. char *devpath;
  56. uint16_t vendor_id;
  57. uint16_t model_id;
  58. BStringMap removed_map;
  59. LinkedList1Node devices_list_node;
  60. };
  61. struct instance {
  62. NCDModuleInst *i;
  63. NCDUdevClient client;
  64. LinkedList1 devices_list;
  65. event_template templ;
  66. };
  67. struct nextevent_instance {
  68. NCDModuleInst *i;
  69. };
  70. static void templ_func_free (struct instance *o);
  71. static struct device * find_device_by_devname (struct instance *o, const char *devname)
  72. {
  73. for (LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list); list_node; list_node = LinkedList1Node_Next(list_node)) {
  74. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  75. if (!strcmp(device->devname, devname)) {
  76. return device;
  77. }
  78. }
  79. return NULL;
  80. }
  81. static struct device * find_device_by_devpath (struct instance *o, const char *devpath)
  82. {
  83. for (LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list); list_node; list_node = LinkedList1Node_Next(list_node)) {
  84. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  85. if (!strcmp(device->devpath, devpath)) {
  86. return device;
  87. }
  88. }
  89. return NULL;
  90. }
  91. static void free_device (struct instance *o, struct device *device, int have_removed_map)
  92. {
  93. // remove from devices list
  94. LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
  95. // free removed map
  96. if (have_removed_map) {
  97. BStringMap_Free(&device->removed_map);
  98. }
  99. // free devpath
  100. free(device->devpath);
  101. // free devname
  102. free(device->devname);
  103. // free structure
  104. free(device);
  105. }
  106. static int make_event_map (struct instance *o, int added, const char *devname, uint16_t vendor_id, uint16_t model_id, BStringMap *out_map)
  107. {
  108. // init map
  109. BStringMap map;
  110. BStringMap_Init(&map);
  111. // set type
  112. if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
  113. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  114. goto fail1;
  115. }
  116. // set devname
  117. if (!BStringMap_Set(&map, "devname", devname)) {
  118. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  119. goto fail1;
  120. }
  121. // set vendor ID
  122. char vendor_id_str[5];
  123. sprintf(vendor_id_str, "%04"PRIx16, vendor_id);
  124. if (!BStringMap_Set(&map, "vendor_id", vendor_id_str)) {
  125. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  126. goto fail1;
  127. }
  128. // set model ID
  129. char model_id_str[5];
  130. sprintf(model_id_str, "%04"PRIx16, model_id);
  131. if (!BStringMap_Set(&map, "model_id", model_id_str)) {
  132. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  133. goto fail1;
  134. }
  135. *out_map = map;
  136. return 1;
  137. fail1:
  138. BStringMap_Free(&map);
  139. return 0;
  140. }
  141. static void queue_event (struct instance *o, BStringMap map)
  142. {
  143. // pass event to template
  144. int was_empty;
  145. event_template_queue(&o->templ, map, &was_empty);
  146. // if event queue was empty, stop receiving udev events
  147. if (was_empty) {
  148. NCDUdevClient_Pause(&o->client);
  149. }
  150. }
  151. static void add_device (struct instance *o, const char *devname, const char *devpath, uint16_t vendor_id, uint16_t model_id)
  152. {
  153. ASSERT(!find_device_by_devname(o, devname))
  154. ASSERT(!find_device_by_devpath(o, devpath))
  155. // allocate structure
  156. struct device *device = malloc(sizeof(*device));
  157. if (!device) {
  158. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  159. goto fail0;
  160. }
  161. // init devname
  162. if (!(device->devname = strdup(devname))) {
  163. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  164. goto fail1;
  165. }
  166. // init devpath
  167. if (!(device->devpath = strdup(devpath))) {
  168. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  169. goto fail2;
  170. }
  171. // set vendor and model ID
  172. device->vendor_id = vendor_id;
  173. device->model_id = model_id;
  174. // init removed map
  175. if (!make_event_map(o, 0, devname, vendor_id, model_id, &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, devname, vendor_id, model_id, &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->devname);
  196. fail1:
  197. free(device);
  198. fail0:
  199. ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", devname);
  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 client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  218. {
  219. // lookup existing device with this devpath
  220. struct device *ex_device = find_device_by_devpath(o, devpath);
  221. // lookup cache entry
  222. const BStringMap *cache_map = NCDUdevManager_Query(o->i->umanager, devpath);
  223. if (!cache_map) {
  224. if (ex_device) {
  225. remove_device(o, ex_device);
  226. }
  227. goto out;
  228. }
  229. const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
  230. const char *devname = BStringMap_Get(cache_map, "DEVNAME");
  231. const char *devtype = BStringMap_Get(cache_map, "DEVTYPE");
  232. const char *vendor_id_str = BStringMap_Get(cache_map, "ID_VENDOR_ID");
  233. const char *model_id_str = BStringMap_Get(cache_map, "ID_MODEL_ID");
  234. uintmax_t vendor_id;
  235. uintmax_t model_id;
  236. if (!(subsystem && !strcmp(subsystem, "usb") &&
  237. devname &&
  238. devtype && !strcmp(devtype, "usb_device") &&
  239. vendor_id_str && parse_unsigned_hex_integer(vendor_id_str, &vendor_id) &&
  240. model_id_str && parse_unsigned_hex_integer(model_id_str, &model_id)
  241. )) {
  242. if (ex_device) {
  243. remove_device(o, ex_device);
  244. }
  245. goto out;
  246. }
  247. if (ex_device && (
  248. strcmp(ex_device->devname, devname) ||
  249. ex_device->vendor_id != vendor_id || ex_device->model_id != model_id
  250. )) {
  251. remove_device(o, ex_device);
  252. ex_device = NULL;
  253. }
  254. if (!ex_device) {
  255. struct device *ex_devname_device = find_device_by_devname(o, devname);
  256. if (ex_devname_device) {
  257. remove_device(o, ex_devname_device);
  258. }
  259. add_device(o, devname, devpath, vendor_id, model_id);
  260. }
  261. out:
  262. free(devpath);
  263. if (have_map) {
  264. BStringMap_Free(&map);
  265. }
  266. }
  267. static void func_new (NCDModuleInst *i)
  268. {
  269. // allocate instance
  270. struct instance *o = malloc(sizeof(*o));
  271. if (!o) {
  272. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  273. goto fail0;
  274. }
  275. NCDModuleInst_Backend_SetUser(i, o);
  276. // init arguments
  277. o->i = i;
  278. // check arguments
  279. if (!NCDValue_ListRead(i->args, 0)) {
  280. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  281. goto fail1;
  282. }
  283. // init client
  284. NCDUdevClient_Init(&o->client, o->i->umanager, o, (NCDUdevClient_handler)client_handler);
  285. // init devices list
  286. LinkedList1_Init(&o->devices_list);
  287. event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
  288. return;
  289. fail1:
  290. free(o);
  291. fail0:
  292. NCDModuleInst_Backend_SetError(i);
  293. NCDModuleInst_Backend_Dead(i);
  294. }
  295. static void templ_func_free (struct instance *o)
  296. {
  297. NCDModuleInst *i = o->i;
  298. // free devices
  299. while (!LinkedList1_IsEmpty(&o->devices_list)) {
  300. struct device *device = UPPER_OBJECT(LinkedList1_GetFirst(&o->devices_list), struct device, devices_list_node);
  301. free_device(o, device, 1);
  302. }
  303. // free client
  304. NCDUdevClient_Free(&o->client);
  305. // free instance
  306. free(o);
  307. NCDModuleInst_Backend_Dead(i);
  308. }
  309. static void func_die (void *vo)
  310. {
  311. struct instance *o = vo;
  312. event_template_die(&o->templ);
  313. }
  314. static int func_getvar (void *vo, const char *name, NCDValue *out)
  315. {
  316. struct instance *o = vo;
  317. return event_template_getvar(&o->templ, name, out);
  318. }
  319. static void nextevent_func_new (NCDModuleInst *i)
  320. {
  321. // allocate instance
  322. struct nextevent_instance *o = malloc(sizeof(*o));
  323. if (!o) {
  324. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  325. goto fail0;
  326. }
  327. NCDModuleInst_Backend_SetUser(i, o);
  328. // init arguments
  329. o->i = i;
  330. // check arguments
  331. if (!NCDValue_ListRead(o->i->args, 0)) {
  332. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  333. goto fail1;
  334. }
  335. // get method object
  336. struct instance *mo = i->method_object->inst_user;
  337. event_template_assert_enabled(&mo->templ);
  338. // signal up.
  339. // Do it before finishing the event so our process does not advance any further if
  340. // we would be killed the event provider going down.
  341. NCDModuleInst_Backend_Up(o->i);
  342. // wait for next event
  343. next_event(mo);
  344. return;
  345. fail1:
  346. free(o);
  347. fail0:
  348. NCDModuleInst_Backend_SetError(i);
  349. NCDModuleInst_Backend_Dead(i);
  350. }
  351. static void nextevent_func_die (void *vo)
  352. {
  353. struct nextevent_instance *o = vo;
  354. NCDModuleInst *i = o->i;
  355. // free instance
  356. free(o);
  357. NCDModuleInst_Backend_Dead(i);
  358. }
  359. static const struct NCDModule modules[] = {
  360. {
  361. .type = "sys.watch_usb",
  362. .func_new = func_new,
  363. .func_die = func_die,
  364. .func_getvar = func_getvar
  365. }, {
  366. .type = "sys.watch_usb::nextevent",
  367. .func_new = nextevent_func_new,
  368. .func_die = nextevent_func_die
  369. }, {
  370. .type = NULL
  371. }
  372. };
  373. const struct NCDModuleGroup ncdmodule_sys_watch_usb = {
  374. .modules = modules
  375. };