NCDUdevManager.c 15 KB

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