NCDUdevManager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /**
  2. * @file NCDUdevManager.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. #include <stdlib.h>
  23. #include <string.h>
  24. #include <misc/offset.h>
  25. #include <system/BLog.h>
  26. #include <udevmonitor/NCDUdevManager.h>
  27. #include <generated/blog_channel_NCDUdevManager.h>
  28. #define RESTART_TIMER_TIME 5000
  29. static int event_to_map (NCDUdevMonitor *monitor, BStringMap *out_map);
  30. static void free_event (NCDUdevClient *o, struct NCDUdevClient_event *e);
  31. static void queue_event (NCDUdevManager *o, NCDUdevMonitor *monitor, NCDUdevClient *client);
  32. static void queue_mapless_event (NCDUdevManager *o, const char *devpath, NCDUdevClient *client);
  33. static void process_event (NCDUdevManager *o, NCDUdevMonitor *monitor);
  34. static void try_monitor (NCDUdevManager *o);
  35. static void reset_monitor (NCDUdevManager *o);
  36. static void timer_handler (NCDUdevManager *o);
  37. static void monitor_handler_event (NCDUdevManager *o);
  38. static void monitor_handler_error (NCDUdevManager *o, int is_error);
  39. static void info_monitor_handler_event (NCDUdevManager *o);
  40. static void info_monitor_handler_error (NCDUdevManager *o, int is_error);
  41. static void next_job_handler (NCDUdevClient *o);
  42. static int event_to_map (NCDUdevMonitor *monitor, BStringMap *out_map)
  43. {
  44. NCDUdevMonitor_AssertReady(monitor);
  45. // init map
  46. BStringMap_Init(out_map);
  47. // insert properties to map
  48. int num_properties = NCDUdevMonitor_GetNumProperties(monitor);
  49. for (int i = 0; i < num_properties; i++) {
  50. const char *name;
  51. const char *value;
  52. NCDUdevMonitor_GetProperty(monitor, i, &name, &value);
  53. if (!BStringMap_Set(out_map, name, value)) {
  54. BLog(BLOG_ERROR, "BStringMap_Set failed");
  55. goto fail1;
  56. }
  57. }
  58. return 1;
  59. fail1:
  60. BStringMap_Free(out_map);
  61. return 0;
  62. }
  63. static void free_event (NCDUdevClient *o, struct NCDUdevClient_event *e)
  64. {
  65. // remove from events list
  66. LinkedList1_Remove(&o->events_list, &e->events_list_node);
  67. // free map
  68. if (e->have_map) {
  69. BStringMap_Free(&e->map);
  70. }
  71. // free devpath
  72. free(e->devpath);
  73. // free structure
  74. free(e);
  75. }
  76. static void queue_event (NCDUdevManager *o, NCDUdevMonitor *monitor, NCDUdevClient *client)
  77. {
  78. NCDUdevMonitor_AssertReady(monitor);
  79. // alloc event
  80. struct NCDUdevClient_event *e = malloc(sizeof(*e));
  81. if (!e) {
  82. BLog(BLOG_ERROR, "malloc failed");
  83. goto fail0;
  84. }
  85. // build map
  86. if (!event_to_map(monitor, &e->map)) {
  87. goto fail1;
  88. }
  89. // set have map
  90. e->have_map = 1;
  91. // get devpath
  92. const char *devpath = BStringMap_Get(&e->map, "DEVPATH");
  93. if (!devpath) {
  94. BLog(BLOG_ERROR, "DEVPATH missing");
  95. goto fail2;
  96. }
  97. // copy devpath
  98. if (!(e->devpath = strdup(devpath))) {
  99. BLog(BLOG_ERROR, "strdup failed");
  100. goto fail2;
  101. }
  102. // insert to client's events list
  103. LinkedList1_Append(&client->events_list, &e->events_list_node);
  104. // if client is running, set next job
  105. if (client->running) {
  106. BPending_Set(&client->next_job);
  107. }
  108. return;
  109. fail2:
  110. BStringMap_Free(&e->map);
  111. fail1:
  112. free(e);
  113. fail0:
  114. return;
  115. }
  116. static void queue_mapless_event (NCDUdevManager *o, const char *devpath, NCDUdevClient *client)
  117. {
  118. // alloc event
  119. struct NCDUdevClient_event *e = malloc(sizeof(*e));
  120. if (!e) {
  121. BLog(BLOG_ERROR, "malloc failed");
  122. goto fail0;
  123. }
  124. // set have no map
  125. e->have_map = 0;
  126. // copy devpath
  127. if (!(e->devpath = strdup(devpath))) {
  128. BLog(BLOG_ERROR, "strdup failed");
  129. goto fail1;
  130. }
  131. // insert to client's events list
  132. LinkedList1_Append(&client->events_list, &e->events_list_node);
  133. // if client is running, set next job
  134. if (client->running) {
  135. BPending_Set(&client->next_job);
  136. }
  137. return;
  138. fail1:
  139. free(e);
  140. fail0:
  141. return;
  142. }
  143. static void process_event (NCDUdevManager *o, NCDUdevMonitor *monitor)
  144. {
  145. NCDUdevMonitor_AssertReady(monitor);
  146. // build map from event
  147. BStringMap map;
  148. if (!event_to_map(monitor, &map)) {
  149. BLog(BLOG_ERROR, "failed to build map");
  150. return;
  151. }
  152. // pass event to cache
  153. if (!NCDUdevCache_Event(&o->cache, map)) {
  154. BLog(BLOG_ERROR, "failed to cache");
  155. BStringMap_Free(&map);
  156. return;
  157. }
  158. // queue event to clients
  159. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->clients_list);
  160. while (list_node) {
  161. NCDUdevClient *client = UPPER_OBJECT(list_node, NCDUdevClient, clients_list_node);
  162. queue_event(o, monitor, client);
  163. list_node = LinkedList1Node_Next(list_node);
  164. }
  165. }
  166. static void try_monitor (NCDUdevManager *o)
  167. {
  168. ASSERT(!o->have_monitor)
  169. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  170. // init monitor
  171. if (!NCDUdevMonitor_Init(&o->monitor, o->reactor, o->manager, 0, o,
  172. (NCDUdevMonitor_handler_event)monitor_handler_event,
  173. (NCDUdevMonitor_handler_error)monitor_handler_error
  174. )) {
  175. BLog(BLOG_ERROR, "NCDUdevMonitor_Init failed");
  176. // set restart timer
  177. BReactor_SetTimer(o->reactor, &o->restart_timer);
  178. return;
  179. }
  180. // set have monitor
  181. o->have_monitor = 1;
  182. // set not have info monitor
  183. o->have_info_monitor = 0;
  184. }
  185. static void reset_monitor (NCDUdevManager *o)
  186. {
  187. ASSERT(o->have_monitor)
  188. ASSERT(!o->have_info_monitor)
  189. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  190. // free monitor
  191. NCDUdevMonitor_Free(&o->monitor);
  192. // set have no monitor
  193. o->have_monitor = 0;
  194. // set restart timer
  195. BReactor_SetTimer(o->reactor, &o->restart_timer);
  196. }
  197. static void timer_handler (NCDUdevManager *o)
  198. {
  199. DebugObject_Access(&o->d_obj);
  200. ASSERT(!o->have_monitor)
  201. // try again
  202. try_monitor(o);
  203. }
  204. static void monitor_handler_event (NCDUdevManager *o)
  205. {
  206. DebugObject_Access(&o->d_obj);
  207. ASSERT(o->have_monitor)
  208. ASSERT(!o->have_info_monitor)
  209. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  210. if (NCDUdevMonitor_IsReadyEvent(&o->monitor)) {
  211. BLog(BLOG_INFO, "monitor ready");
  212. // init info monitor
  213. if (!NCDUdevMonitor_Init(&o->info_monitor, o->reactor, o->manager, 1, o,
  214. (NCDUdevMonitor_handler_event)info_monitor_handler_event,
  215. (NCDUdevMonitor_handler_error)info_monitor_handler_error
  216. )) {
  217. BLog(BLOG_ERROR, "NCDUdevMonitor_Init failed");
  218. reset_monitor(o);
  219. return;
  220. }
  221. // set have info monitor
  222. o->have_info_monitor = 1;
  223. // start cache cleanup
  224. NCDUdevCache_StartClean(&o->cache);
  225. // hold processing monitor events until info monitor is done
  226. return;
  227. }
  228. // accept event
  229. NCDUdevMonitor_Done(&o->monitor);
  230. // process event
  231. process_event(o, &o->monitor);
  232. }
  233. static void monitor_handler_error (NCDUdevManager *o, int is_error)
  234. {
  235. DebugObject_Access(&o->d_obj);
  236. ASSERT(o->have_monitor)
  237. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  238. BLog(BLOG_ERROR, "monitor error");
  239. if (o->have_info_monitor) {
  240. // free info monitor
  241. NCDUdevMonitor_Free(&o->info_monitor);
  242. // set have no info monitor
  243. o->have_info_monitor = 0;
  244. }
  245. // reset monitor
  246. reset_monitor(o);
  247. }
  248. static void info_monitor_handler_event (NCDUdevManager *o)
  249. {
  250. DebugObject_Access(&o->d_obj);
  251. ASSERT(o->have_monitor)
  252. ASSERT(o->have_info_monitor)
  253. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  254. // accept event
  255. NCDUdevMonitor_Done(&o->info_monitor);
  256. // process event
  257. process_event(o, &o->info_monitor);
  258. }
  259. static void info_monitor_handler_error (NCDUdevManager *o, int is_error)
  260. {
  261. DebugObject_Access(&o->d_obj);
  262. ASSERT(o->have_monitor)
  263. ASSERT(o->have_info_monitor)
  264. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  265. if (is_error) {
  266. BLog(BLOG_ERROR, "info monitor error");
  267. } else {
  268. BLog(BLOG_INFO, "info monitor finished");
  269. }
  270. // free info monitor
  271. NCDUdevMonitor_Free(&o->info_monitor);
  272. // set have no info monitor
  273. o->have_info_monitor = 0;
  274. if (is_error) {
  275. // reset monitor
  276. reset_monitor(o);
  277. } else {
  278. // continue processing monitor events
  279. NCDUdevMonitor_Done(&o->monitor);
  280. // finish cache cleanup
  281. NCDUdevCache_FinishClean(&o->cache);
  282. // collect cleaned devices
  283. BStringMap map;
  284. while (NCDUdevCache_GetCleanedDevice(&o->cache, &map)) {
  285. // get devpath
  286. const char *devpath = BStringMap_Get(&map, "DEVPATH");
  287. ASSERT(devpath)
  288. // queue mapless event to clients
  289. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->clients_list);
  290. while (list_node) {
  291. NCDUdevClient *client = UPPER_OBJECT(list_node, NCDUdevClient, clients_list_node);
  292. queue_mapless_event(o, devpath, client);
  293. list_node = LinkedList1Node_Next(list_node);
  294. }
  295. BStringMap_Free(&map);
  296. }
  297. }
  298. }
  299. static void next_job_handler (NCDUdevClient *o)
  300. {
  301. DebugObject_Access(&o->d_obj);
  302. ASSERT(!LinkedList1_IsEmpty(&o->events_list))
  303. ASSERT(o->running)
  304. // get event
  305. struct NCDUdevClient_event *e = UPPER_OBJECT(LinkedList1_GetFirst(&o->events_list), struct NCDUdevClient_event, events_list_node);
  306. // grab map from event
  307. int have_map = e->have_map;
  308. BStringMap map = e->map;
  309. // grab devpath from event
  310. char *devpath = e->devpath;
  311. // remove from events list
  312. LinkedList1_Remove(&o->events_list, &e->events_list_node);
  313. // free structure
  314. free(e);
  315. // schedule next event if needed
  316. if (!LinkedList1_IsEmpty(&o->events_list)) {
  317. BPending_Set(&o->next_job);
  318. }
  319. // give map to handler
  320. o->handler(o->user, devpath, have_map, map);
  321. return;
  322. }
  323. void NCDUdevManager_Init (NCDUdevManager *o, BReactor *reactor, BProcessManager *manager)
  324. {
  325. // init arguments
  326. o->reactor = reactor;
  327. o->manager = manager;
  328. // init clients list
  329. LinkedList1_Init(&o->clients_list);
  330. // init cache
  331. NCDUdevCache_Init(&o->cache);
  332. // init restart timer
  333. BTimer_Init(&o->restart_timer, RESTART_TIMER_TIME, (BTimer_handler)timer_handler, o);
  334. // set have no monitor
  335. o->have_monitor = 0;
  336. DebugObject_Init(&o->d_obj);
  337. }
  338. void NCDUdevManager_Free (NCDUdevManager *o)
  339. {
  340. DebugObject_Free(&o->d_obj);
  341. ASSERT(LinkedList1_IsEmpty(&o->clients_list))
  342. if (o->have_monitor) {
  343. // free info monitor
  344. if (o->have_info_monitor) {
  345. NCDUdevMonitor_Free(&o->info_monitor);
  346. }
  347. // free monitor
  348. NCDUdevMonitor_Free(&o->monitor);
  349. }
  350. // free restart timer
  351. BReactor_RemoveTimer(o->reactor, &o->restart_timer);
  352. // free cache
  353. NCDUdevCache_Free(&o->cache);
  354. }
  355. const BStringMap * NCDUdevManager_Query (NCDUdevManager *o, const char *devpath)
  356. {
  357. DebugObject_Access(&o->d_obj);
  358. return NCDUdevCache_Query(&o->cache, devpath);
  359. }
  360. void NCDUdevClient_Init (NCDUdevClient *o, NCDUdevManager *m, void *user,
  361. NCDUdevClient_handler handler)
  362. {
  363. DebugObject_Access(&m->d_obj);
  364. // init arguments
  365. o->m = m;
  366. o->user = user;
  367. o->handler = handler;
  368. // insert to manager's list
  369. LinkedList1_Append(&m->clients_list, &o->clients_list_node);
  370. // init events list
  371. LinkedList1_Init(&o->events_list);
  372. // init next job
  373. BPending_Init(&o->next_job, BReactor_PendingGroup(m->reactor), (BPending_handler)next_job_handler, o);
  374. // set running
  375. o->running = 1;
  376. // queue all devices from cache
  377. const char *devpath = NCDUdevCache_First(&m->cache);
  378. while (devpath) {
  379. queue_mapless_event(m, devpath, o);
  380. devpath = NCDUdevCache_Next(&m->cache, devpath);
  381. }
  382. // if this is the first client, init monitor
  383. if (!m->have_monitor && !BTimer_IsRunning(&m->restart_timer)) {
  384. try_monitor(m);
  385. }
  386. DebugObject_Init(&o->d_obj);
  387. }
  388. void NCDUdevClient_Free (NCDUdevClient *o)
  389. {
  390. DebugObject_Free(&o->d_obj);
  391. NCDUdevManager *m = o->m;
  392. // free events
  393. LinkedList1Node *list_node;
  394. while (list_node = LinkedList1_GetFirst(&o->events_list)) {
  395. struct NCDUdevClient_event *e = UPPER_OBJECT(list_node, struct NCDUdevClient_event, events_list_node);
  396. free_event(o, e);
  397. }
  398. // free next job
  399. BPending_Free(&o->next_job);
  400. // remove from manager's list
  401. LinkedList1_Remove(&m->clients_list, &o->clients_list_node);
  402. }
  403. void NCDUdevClient_Pause (NCDUdevClient *o)
  404. {
  405. DebugObject_Access(&o->d_obj);
  406. ASSERT(o->running)
  407. // set not running
  408. o->running = 0;
  409. // unset next job to avoid reporting queued events
  410. BPending_Unset(&o->next_job);
  411. }
  412. void NCDUdevClient_Continue (NCDUdevClient *o)
  413. {
  414. DebugObject_Access(&o->d_obj);
  415. ASSERT(!o->running)
  416. // set running
  417. o->running = 1;
  418. // set next job if we have events queued
  419. if (!LinkedList1_IsEmpty(&o->events_list)) {
  420. BPending_Set(&o->next_job);
  421. }
  422. }