NCDUdevMonitor.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @file NCDUdevMonitor.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 <stddef.h>
  30. #include <base/BLog.h>
  31. #include <udevmonitor/NCDUdevMonitor.h>
  32. #include <generated/blog_channel_NCDUdevMonitor.h>
  33. #define STDBUF_EXEC "/usr/bin/stdbuf"
  34. #define UDEVADM_EXEC "/sbin/udevadm"
  35. #define PARSER_BUF_SIZE 16384
  36. #define PARSER_MAX_PROPERTIES 256
  37. static void report_error (NCDUdevMonitor *o)
  38. {
  39. ASSERT(!o->process_running)
  40. ASSERT(!o->input_running)
  41. DEBUGERROR(&o->d_err, o->handler_error(o->user, (o->process_was_error || o->input_was_error)));
  42. }
  43. static void process_handler_terminated (NCDUdevMonitor *o, int normally, uint8_t normally_exit_status)
  44. {
  45. DebugObject_Access(&o->d_obj);
  46. ASSERT(o->process_running)
  47. BLog(BLOG_INFO, "process terminated");
  48. // set process not running (so we don't try to kill it)
  49. o->process_running = 0;
  50. // remember process error
  51. o->process_was_error = !(normally && normally_exit_status == 0);
  52. if (!o->input_running) {
  53. report_error(o);
  54. return;
  55. }
  56. }
  57. static void process_handler_closed (NCDUdevMonitor *o, int is_error)
  58. {
  59. DebugObject_Access(&o->d_obj);
  60. ASSERT(o->input_running)
  61. if (is_error) {
  62. BLog(BLOG_ERROR, "pipe error");
  63. } else {
  64. BLog(BLOG_INFO, "pipe closed");
  65. }
  66. // disconnect connector
  67. StreamRecvConnector_DisconnectInput(&o->connector);
  68. // set input not running
  69. o->input_running = 0;
  70. // remember input error
  71. o->input_was_error = is_error;
  72. if (!o->process_running) {
  73. report_error(o);
  74. return;
  75. }
  76. }
  77. static void parser_handler (NCDUdevMonitor *o)
  78. {
  79. DebugObject_Access(&o->d_obj);
  80. o->handler_event(o->user);
  81. return;
  82. }
  83. int NCDUdevMonitor_Init (NCDUdevMonitor *o, BReactor *reactor, BProcessManager *manager, int mode, void *user,
  84. NCDUdevMonitor_handler_event handler_event,
  85. NCDUdevMonitor_handler_error handler_error)
  86. {
  87. ASSERT(mode == NCDUDEVMONITOR_MODE_MONITOR_UDEV || mode == NCDUDEVMONITOR_MODE_INFO || mode == NCDUDEVMONITOR_MODE_MONITOR_KERNEL)
  88. // init arguments
  89. o->user = user;
  90. o->handler_event = handler_event;
  91. o->handler_error = handler_error;
  92. // construct arguments
  93. const char *argv_monitor_udev[] = {STDBUF_EXEC, "-o", "L", UDEVADM_EXEC, "monitor", "--udev", "--environment", NULL};
  94. const char *argv_monitor_kernel[] = {STDBUF_EXEC, "-o", "L", UDEVADM_EXEC, "monitor", "--kernel", "--environment", NULL};
  95. const char *argv_info[] = {STDBUF_EXEC, "-o", "L", UDEVADM_EXEC, "info", "--query", "all", "--export-db", NULL};
  96. // choose arguments based on mode
  97. const char **argv;
  98. switch (mode) {
  99. case NCDUDEVMONITOR_MODE_MONITOR_UDEV: argv = argv_monitor_udev; break;
  100. case NCDUDEVMONITOR_MODE_INFO: argv = argv_info; break;
  101. case NCDUDEVMONITOR_MODE_MONITOR_KERNEL: argv = argv_monitor_kernel; break;
  102. default: ASSERT(0);
  103. }
  104. // init process
  105. if (!BInputProcess_Init(&o->process, reactor, manager, o,
  106. (BInputProcess_handler_terminated)process_handler_terminated,
  107. (BInputProcess_handler_closed)process_handler_closed
  108. )) {
  109. BLog(BLOG_ERROR, "BInputProcess_Init failed");
  110. goto fail0;
  111. }
  112. // init connector
  113. StreamRecvConnector_Init(&o->connector, BReactor_PendingGroup(reactor));
  114. StreamRecvConnector_ConnectInput(&o->connector, BInputProcess_GetInput(&o->process));
  115. // init parser
  116. if (!NCDUdevMonitorParser_Init(&o->parser, StreamRecvConnector_GetOutput(&o->connector), PARSER_BUF_SIZE, PARSER_MAX_PROPERTIES,
  117. (mode == NCDUDEVMONITOR_MODE_INFO), BReactor_PendingGroup(reactor), o,
  118. (NCDUdevMonitorParser_handler)parser_handler
  119. )) {
  120. BLog(BLOG_ERROR, "NCDUdevMonitorParser_Init failed");
  121. goto fail1;
  122. }
  123. // start process
  124. if (!BInputProcess_Start(&o->process, STDBUF_EXEC, (char **)argv, NULL)) {
  125. BLog(BLOG_ERROR, "BInputProcess_Start failed");
  126. goto fail2;
  127. }
  128. // set process running, input running
  129. o->process_running = 1;
  130. o->input_running = 1;
  131. DebugError_Init(&o->d_err, BReactor_PendingGroup(reactor));
  132. DebugObject_Init(&o->d_obj);
  133. return 1;
  134. fail2:
  135. NCDUdevMonitorParser_Free(&o->parser);
  136. fail1:
  137. StreamRecvConnector_Free(&o->connector);
  138. BInputProcess_Free(&o->process);
  139. fail0:
  140. return 0;
  141. }
  142. void NCDUdevMonitor_Free (NCDUdevMonitor *o)
  143. {
  144. DebugObject_Free(&o->d_obj);
  145. DebugError_Free(&o->d_err);
  146. // free parser
  147. NCDUdevMonitorParser_Free(&o->parser);
  148. // free connector
  149. StreamRecvConnector_Free(&o->connector);
  150. // kill process it it's running
  151. if (o->process_running) {
  152. BInputProcess_Kill(&o->process);
  153. }
  154. // free process
  155. BInputProcess_Free(&o->process);
  156. }
  157. void NCDUdevMonitor_Done (NCDUdevMonitor *o)
  158. {
  159. DebugObject_Access(&o->d_obj);
  160. DebugError_AssertNoError(&o->d_err);
  161. NCDUdevMonitorParser_AssertReady(&o->parser);
  162. NCDUdevMonitorParser_Done(&o->parser);
  163. }
  164. int NCDUdevMonitor_IsReadyEvent (NCDUdevMonitor *o)
  165. {
  166. DebugObject_Access(&o->d_obj);
  167. DebugError_AssertNoError(&o->d_err);
  168. NCDUdevMonitorParser_AssertReady(&o->parser);
  169. return NCDUdevMonitorParser_IsReadyEvent(&o->parser);
  170. }
  171. void NCDUdevMonitor_AssertReady (NCDUdevMonitor *o)
  172. {
  173. DebugObject_Access(&o->d_obj);
  174. DebugError_AssertNoError(&o->d_err);
  175. NCDUdevMonitorParser_AssertReady(&o->parser);
  176. }
  177. int NCDUdevMonitor_GetNumProperties (NCDUdevMonitor *o)
  178. {
  179. DebugObject_Access(&o->d_obj);
  180. DebugError_AssertNoError(&o->d_err);
  181. NCDUdevMonitorParser_AssertReady(&o->parser);
  182. return NCDUdevMonitorParser_GetNumProperties(&o->parser);
  183. }
  184. void NCDUdevMonitor_GetProperty (NCDUdevMonitor *o, int index, const char **name, const char **value)
  185. {
  186. DebugObject_Access(&o->d_obj);
  187. DebugError_AssertNoError(&o->d_err);
  188. NCDUdevMonitorParser_AssertReady(&o->parser);
  189. NCDUdevMonitorParser_GetProperty(&o->parser, index, name, value);
  190. }