NCDUdevManager.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. int mode = (o->no_udev ? NCDUDEVMONITOR_MODE_MONITOR_KERNEL : NCDUDEVMONITOR_MODE_MONITOR_UDEV);
  178. // init monitor
  179. if (!NCDUdevMonitor_Init(&o->monitor, o->reactor, o->manager, mode, o,
  180. (NCDUdevMonitor_handler_event)monitor_handler_event,
  181. (NCDUdevMonitor_handler_error)monitor_handler_error
  182. )) {
  183. BLog(BLOG_ERROR, "NCDUdevMonitor_Init failed");
  184. // set restart timer
  185. BReactor_SetTimer(o->reactor, &o->restart_timer);
  186. return;
  187. }
  188. // set have monitor
  189. o->have_monitor = 1;
  190. // set not have info monitor
  191. o->have_info_monitor = 0;
  192. }
  193. static void reset_monitor (NCDUdevManager *o)
  194. {
  195. ASSERT(o->have_monitor)
  196. ASSERT(!o->have_info_monitor)
  197. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  198. // free monitor
  199. NCDUdevMonitor_Free(&o->monitor);
  200. // set have no monitor
  201. o->have_monitor = 0;
  202. // set restart timer
  203. BReactor_SetTimer(o->reactor, &o->restart_timer);
  204. }
  205. static void timer_handler (NCDUdevManager *o)
  206. {
  207. DebugObject_Access(&o->d_obj);
  208. ASSERT(!o->have_monitor)
  209. // try again
  210. try_monitor(o);
  211. }
  212. static void monitor_handler_event (NCDUdevManager *o)
  213. {
  214. DebugObject_Access(&o->d_obj);
  215. ASSERT(o->have_monitor)
  216. ASSERT(!o->have_info_monitor)
  217. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  218. if (NCDUdevMonitor_IsReadyEvent(&o->monitor)) {
  219. BLog(BLOG_INFO, "monitor ready");
  220. // init info monitor
  221. if (!NCDUdevMonitor_Init(&o->info_monitor, o->reactor, o->manager, NCDUDEVMONITOR_MODE_INFO, o,
  222. (NCDUdevMonitor_handler_event)info_monitor_handler_event,
  223. (NCDUdevMonitor_handler_error)info_monitor_handler_error
  224. )) {
  225. BLog(BLOG_ERROR, "NCDUdevMonitor_Init failed");
  226. reset_monitor(o);
  227. return;
  228. }
  229. // set have info monitor
  230. o->have_info_monitor = 1;
  231. // start cache cleanup
  232. NCDUdevCache_StartClean(&o->cache);
  233. // hold processing monitor events until info monitor is done
  234. return;
  235. }
  236. // accept event
  237. NCDUdevMonitor_Done(&o->monitor);
  238. // process event
  239. process_event(o, &o->monitor);
  240. }
  241. static void monitor_handler_error (NCDUdevManager *o, int is_error)
  242. {
  243. DebugObject_Access(&o->d_obj);
  244. ASSERT(o->have_monitor)
  245. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  246. BLog(BLOG_ERROR, "monitor error");
  247. if (o->have_info_monitor) {
  248. // free info monitor
  249. NCDUdevMonitor_Free(&o->info_monitor);
  250. // set have no info monitor
  251. o->have_info_monitor = 0;
  252. }
  253. // reset monitor
  254. reset_monitor(o);
  255. }
  256. static void info_monitor_handler_event (NCDUdevManager *o)
  257. {
  258. DebugObject_Access(&o->d_obj);
  259. ASSERT(o->have_monitor)
  260. ASSERT(o->have_info_monitor)
  261. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  262. // accept event
  263. NCDUdevMonitor_Done(&o->info_monitor);
  264. // process event
  265. process_event(o, &o->info_monitor);
  266. }
  267. static void info_monitor_handler_error (NCDUdevManager *o, int is_error)
  268. {
  269. DebugObject_Access(&o->d_obj);
  270. ASSERT(o->have_monitor)
  271. ASSERT(o->have_info_monitor)
  272. ASSERT(!BTimer_IsRunning(&o->restart_timer))
  273. if (is_error) {
  274. BLog(BLOG_ERROR, "info monitor error");
  275. } else {
  276. BLog(BLOG_INFO, "info monitor finished");
  277. }
  278. // free info monitor
  279. NCDUdevMonitor_Free(&o->info_monitor);
  280. // set have no info monitor
  281. o->have_info_monitor = 0;
  282. if (is_error) {
  283. // reset monitor
  284. reset_monitor(o);
  285. } else {
  286. // continue processing monitor events
  287. NCDUdevMonitor_Done(&o->monitor);
  288. // finish cache cleanup
  289. NCDUdevCache_FinishClean(&o->cache);
  290. // collect cleaned devices
  291. BStringMap map;
  292. while (NCDUdevCache_GetCleanedDevice(&o->cache, &map)) {
  293. // get devpath
  294. const char *devpath = BStringMap_Get(&map, "DEVPATH");
  295. ASSERT(devpath)
  296. // queue mapless event to clients
  297. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->clients_list);
  298. while (list_node) {
  299. NCDUdevClient *client = UPPER_OBJECT(list_node, NCDUdevClient, clients_list_node);
  300. queue_mapless_event(o, devpath, client);
  301. list_node = LinkedList1Node_Next(list_node);
  302. }
  303. BStringMap_Free(&map);
  304. }
  305. }
  306. }
  307. static void next_job_handler (NCDUdevClient *o)
  308. {
  309. DebugObject_Access(&o->d_obj);
  310. ASSERT(!LinkedList1_IsEmpty(&o->events_list))
  311. ASSERT(o->running)
  312. // get event
  313. struct NCDUdevClient_event *e = UPPER_OBJECT(LinkedList1_GetFirst(&o->events_list), struct NCDUdevClient_event, events_list_node);
  314. // grab map from event
  315. int have_map = e->have_map;
  316. BStringMap map = e->map;
  317. // grab devpath from event
  318. char *devpath = e->devpath;
  319. // remove from events list
  320. LinkedList1_Remove(&o->events_list, &e->events_list_node);
  321. // free structure
  322. free(e);
  323. // schedule next event if needed
  324. if (!LinkedList1_IsEmpty(&o->events_list)) {
  325. BPending_Set(&o->next_job);
  326. }
  327. // give map to handler
  328. o->handler(o->user, devpath, have_map, map);
  329. return;
  330. }
  331. void NCDUdevManager_Init (NCDUdevManager *o, int no_udev, BReactor *reactor, BProcessManager *manager)
  332. {
  333. ASSERT(no_udev == 0 || no_udev == 1)
  334. // init arguments
  335. o->no_udev = no_udev;
  336. o->reactor = reactor;
  337. o->manager = manager;
  338. // init clients list
  339. LinkedList1_Init(&o->clients_list);
  340. // init cache
  341. NCDUdevCache_Init(&o->cache);
  342. // init restart timer
  343. BTimer_Init(&o->restart_timer, RESTART_TIMER_TIME, (BTimer_handler)timer_handler, o);
  344. // set have no monitor
  345. o->have_monitor = 0;
  346. DebugObject_Init(&o->d_obj);
  347. }
  348. void NCDUdevManager_Free (NCDUdevManager *o)
  349. {
  350. DebugObject_Free(&o->d_obj);
  351. ASSERT(LinkedList1_IsEmpty(&o->clients_list))
  352. if (o->have_monitor) {
  353. // free info monitor
  354. if (o->have_info_monitor) {
  355. NCDUdevMonitor_Free(&o->info_monitor);
  356. }
  357. // free monitor
  358. NCDUdevMonitor_Free(&o->monitor);
  359. }
  360. // free restart timer
  361. BReactor_RemoveTimer(o->reactor, &o->restart_timer);
  362. // free cache
  363. NCDUdevCache_Free(&o->cache);
  364. }
  365. const BStringMap * NCDUdevManager_Query (NCDUdevManager *o, const char *devpath)
  366. {
  367. DebugObject_Access(&o->d_obj);
  368. return NCDUdevCache_Query(&o->cache, devpath);
  369. }
  370. void NCDUdevClient_Init (NCDUdevClient *o, NCDUdevManager *m, void *user,
  371. NCDUdevClient_handler handler)
  372. {
  373. DebugObject_Access(&m->d_obj);
  374. // init arguments
  375. o->m = m;
  376. o->user = user;
  377. o->handler = handler;
  378. // insert to manager's list
  379. LinkedList1_Append(&m->clients_list, &o->clients_list_node);
  380. // init events list
  381. LinkedList1_Init(&o->events_list);
  382. // init next job
  383. BPending_Init(&o->next_job, BReactor_PendingGroup(m->reactor), (BPending_handler)next_job_handler, o);
  384. // set running
  385. o->running = 1;
  386. // queue all devices from cache
  387. const char *devpath = NCDUdevCache_First(&m->cache);
  388. while (devpath) {
  389. queue_mapless_event(m, devpath, o);
  390. devpath = NCDUdevCache_Next(&m->cache, devpath);
  391. }
  392. // if this is the first client, init monitor
  393. if (!m->have_monitor && !BTimer_IsRunning(&m->restart_timer)) {
  394. try_monitor(m);
  395. }
  396. DebugObject_Init(&o->d_obj);
  397. }
  398. void NCDUdevClient_Free (NCDUdevClient *o)
  399. {
  400. DebugObject_Free(&o->d_obj);
  401. NCDUdevManager *m = o->m;
  402. // free events
  403. LinkedList1Node *list_node;
  404. while (list_node = LinkedList1_GetFirst(&o->events_list)) {
  405. struct NCDUdevClient_event *e = UPPER_OBJECT(list_node, struct NCDUdevClient_event, events_list_node);
  406. free_event(o, e);
  407. }
  408. // free next job
  409. BPending_Free(&o->next_job);
  410. // remove from manager's list
  411. LinkedList1_Remove(&m->clients_list, &o->clients_list_node);
  412. }
  413. void NCDUdevClient_Pause (NCDUdevClient *o)
  414. {
  415. DebugObject_Access(&o->d_obj);
  416. ASSERT(o->running)
  417. // set not running
  418. o->running = 0;
  419. // unset next job to avoid reporting queued events
  420. BPending_Unset(&o->next_job);
  421. }
  422. void NCDUdevClient_Continue (NCDUdevClient *o)
  423. {
  424. DebugObject_Access(&o->d_obj);
  425. ASSERT(!o->running)
  426. // set running
  427. o->running = 1;
  428. // set next job if we have events queued
  429. if (!LinkedList1_IsEmpty(&o->events_list)) {
  430. BPending_Set(&o->next_job);
  431. }
  432. }