NCDUdevMonitorParser.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**
  2. * @file NCDUdevMonitorParser.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/string_begins_with.h>
  25. #include <misc/balloc.h>
  26. #include <base/BLog.h>
  27. #include <udevmonitor/NCDUdevMonitorParser.h>
  28. #include <generated/blog_channel_NCDUdevMonitorParser.h>
  29. #define PROPERTY_REGEX "^([^=]+)=(.*)$"
  30. static uint8_t * find_end (uint8_t *buf, size_t len)
  31. {
  32. while (len >= 2) {
  33. if (buf[0] == '\n' && buf[1] == '\n') {
  34. return (buf + 2);
  35. }
  36. buf++;
  37. len--;
  38. }
  39. return NULL;
  40. }
  41. static int parse_property (NCDUdevMonitorParser *o, char *data)
  42. {
  43. ASSERT(o->ready_num_properties >= 0)
  44. ASSERT(o->ready_num_properties <= o->max_properties)
  45. if (o->ready_num_properties == o->max_properties) {
  46. BLog(BLOG_ERROR, "too many properties");
  47. return 0;
  48. }
  49. struct NCDUdevMonitorParser_property *prop = &o->ready_properties[o->ready_num_properties];
  50. // execute property regex
  51. regmatch_t matches[3];
  52. if (regexec(&o->property_regex, data, 3, matches, 0) != 0) {
  53. BLog(BLOG_ERROR, "failed to parse property");
  54. return 0;
  55. }
  56. // extract components
  57. prop->name = data + matches[1].rm_so;
  58. *(data + matches[1].rm_eo) = '\0';
  59. prop->value = data + matches[2].rm_so;
  60. *(data + matches[2].rm_eo) = '\0';
  61. // register property
  62. o->ready_num_properties++;
  63. return 1;
  64. }
  65. static int parse_message (NCDUdevMonitorParser *o)
  66. {
  67. ASSERT(!o->is_ready)
  68. ASSERT(o->ready_len >= 2)
  69. ASSERT(o->buf[o->ready_len - 2] == '\n')
  70. ASSERT(o->buf[o->ready_len - 1] == '\n')
  71. // zero terminate message (replacing the second newline)
  72. o->buf[o->ready_len - 1] = '\0';
  73. // start parsing
  74. char *line = (char *)o->buf;
  75. int first_line = 1;
  76. // set is not ready event
  77. o->ready_is_ready_event = 0;
  78. // init properties
  79. o->ready_num_properties = 0;
  80. do {
  81. // find end of line
  82. char *line_end = strchr(line, '\n');
  83. ASSERT(line_end)
  84. // zero terminate line
  85. *line_end = '\0';
  86. if (o->is_info_mode) {
  87. // parse prefix
  88. if (strlen(line) < 3 || line[1] != ':' || line[2] != ' ') {
  89. BLog(BLOG_ERROR, "failed to parse head");
  90. return 0;
  91. }
  92. char line_type = line[0];
  93. char *line_value = line + 3;
  94. if (first_line) {
  95. if (line_type != 'P') {
  96. BLog(BLOG_ERROR, "wrong first line type");
  97. return 0;
  98. }
  99. } else {
  100. if (line_type == 'E') {
  101. if (!parse_property(o, line_value)) {
  102. return 0;
  103. }
  104. }
  105. }
  106. } else {
  107. if (first_line) {
  108. // is this the initial informational message?
  109. if (string_begins_with(line, "monitor")) {
  110. o->ready_is_ready_event = 1;
  111. break;
  112. }
  113. // check first line
  114. if (!string_begins_with(line, "UDEV ")) {
  115. BLog(BLOG_ERROR, "failed to parse head");
  116. return 0;
  117. }
  118. } else {
  119. if (!parse_property(o, line)) {
  120. return 0;
  121. }
  122. }
  123. }
  124. first_line = 0;
  125. line = line_end + 1;
  126. } while (*line);
  127. // set ready
  128. o->is_ready = 1;
  129. return 1;
  130. }
  131. static void process_data (NCDUdevMonitorParser *o)
  132. {
  133. ASSERT(!o->is_ready)
  134. while (1) {
  135. // look for end of event
  136. uint8_t *c = find_end(o->buf, o->buf_used);
  137. if (!c) {
  138. // check for out of buffer condition
  139. if (o->buf_used == o->buf_size) {
  140. BLog(BLOG_ERROR, "out of buffer");
  141. o->buf_used = 0;
  142. }
  143. // receive more data
  144. StreamRecvInterface_Receiver_Recv(o->input, o->buf + o->buf_used, o->buf_size - o->buf_used);
  145. return;
  146. }
  147. // remember message length
  148. o->ready_len = c - o->buf;
  149. // parse message
  150. if (parse_message(o)) {
  151. break;
  152. }
  153. // shift buffer
  154. memmove(o->buf, o->buf + o->ready_len, o->buf_used - o->ready_len);
  155. o->buf_used -= o->ready_len;
  156. }
  157. // call handler
  158. o->handler(o->user);
  159. return;
  160. }
  161. static void input_handler_done (NCDUdevMonitorParser *o, int data_len)
  162. {
  163. DebugObject_Access(&o->d_obj);
  164. ASSERT(!o->is_ready)
  165. ASSERT(data_len > 0)
  166. ASSERT(data_len <= o->buf_size - o->buf_used)
  167. // increment buffer position
  168. o->buf_used += data_len;
  169. // process data
  170. process_data(o);
  171. return;
  172. }
  173. static void done_job_handler (NCDUdevMonitorParser *o)
  174. {
  175. DebugObject_Access(&o->d_obj);
  176. ASSERT(o->is_ready)
  177. // shift buffer
  178. memmove(o->buf, o->buf + o->ready_len, o->buf_used - o->ready_len);
  179. o->buf_used -= o->ready_len;
  180. // set not ready
  181. o->is_ready = 0;
  182. // process data
  183. process_data(o);
  184. return;
  185. }
  186. int NCDUdevMonitorParser_Init (NCDUdevMonitorParser *o, StreamRecvInterface *input, int buf_size, int max_properties,
  187. int is_info_mode, BPendingGroup *pg, void *user,
  188. NCDUdevMonitorParser_handler handler)
  189. {
  190. ASSERT(buf_size > 0)
  191. ASSERT(max_properties >= 0)
  192. ASSERT(is_info_mode == 0 || is_info_mode == 1)
  193. // init arguments
  194. o->input = input;
  195. o->buf_size = buf_size;
  196. o->max_properties = max_properties;
  197. o->is_info_mode = is_info_mode;
  198. o->user = user;
  199. o->handler = handler;
  200. // init input
  201. StreamRecvInterface_Receiver_Init(o->input, (StreamRecvInterface_handler_done)input_handler_done, o);
  202. // init property regex
  203. if (regcomp(&o->property_regex, PROPERTY_REGEX, REG_EXTENDED) != 0) {
  204. BLog(BLOG_ERROR, "regcomp failed");
  205. goto fail1;
  206. }
  207. // init done job
  208. BPending_Init(&o->done_job, pg, (BPending_handler)done_job_handler, o);
  209. // allocate buffer
  210. if (!(o->buf = malloc(o->buf_size))) {
  211. BLog(BLOG_ERROR, "malloc failed");
  212. goto fail2;
  213. }
  214. // set buffer position
  215. o->buf_used = 0;
  216. // set not ready
  217. o->is_ready = 0;
  218. // allocate properties
  219. if (!(o->ready_properties = BAllocArray(o->max_properties, sizeof(o->ready_properties[0])))) {
  220. BLog(BLOG_ERROR, "BAllocArray failed");
  221. goto fail3;
  222. }
  223. // start receiving
  224. StreamRecvInterface_Receiver_Recv(o->input, o->buf, o->buf_size);
  225. DebugObject_Init(&o->d_obj);
  226. return 1;
  227. fail3:
  228. free(o->buf);
  229. fail2:
  230. BPending_Free(&o->done_job);
  231. regfree(&o->property_regex);
  232. fail1:
  233. return 0;
  234. }
  235. void NCDUdevMonitorParser_Free (NCDUdevMonitorParser *o)
  236. {
  237. DebugObject_Free(&o->d_obj);
  238. // free properties
  239. BFree(o->ready_properties);
  240. // free buffer
  241. free(o->buf);
  242. // free done job
  243. BPending_Free(&o->done_job);
  244. // free property regex
  245. regfree(&o->property_regex);
  246. }
  247. void NCDUdevMonitorParser_AssertReady (NCDUdevMonitorParser *o)
  248. {
  249. DebugObject_Access(&o->d_obj);
  250. ASSERT(o->is_ready)
  251. }
  252. void NCDUdevMonitorParser_Done (NCDUdevMonitorParser *o)
  253. {
  254. DebugObject_Access(&o->d_obj);
  255. ASSERT(o->is_ready)
  256. // schedule done job
  257. BPending_Set(&o->done_job);
  258. }
  259. int NCDUdevMonitorParser_IsReadyEvent (NCDUdevMonitorParser *o)
  260. {
  261. DebugObject_Access(&o->d_obj);
  262. ASSERT(o->is_ready)
  263. return o->ready_is_ready_event;
  264. }
  265. int NCDUdevMonitorParser_GetNumProperties (NCDUdevMonitorParser *o)
  266. {
  267. DebugObject_Access(&o->d_obj);
  268. ASSERT(o->is_ready)
  269. return o->ready_num_properties;
  270. }
  271. void NCDUdevMonitorParser_GetProperty (NCDUdevMonitorParser *o, int index, const char **name, const char **value)
  272. {
  273. DebugObject_Access(&o->d_obj);
  274. ASSERT(o->is_ready)
  275. ASSERT(index >= 0)
  276. ASSERT(index < o->ready_num_properties)
  277. *name = o->ready_properties[index].name;
  278. *value = o->ready_properties[index].value;
  279. }