ncd-request.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @file ncd-request.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 <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <base/BLog.h>
  34. #include <base/DebugObject.h>
  35. #include <system/BNetwork.h>
  36. #include <system/BReactor.h>
  37. #include <ncd/NCDValueParser.h>
  38. #include <ncd/NCDValueGenerator.h>
  39. #include <ncd/NCDRequest.h>
  40. static void request_handler_finished (void *user, int is_error);
  41. static void request_handler_reply (void *user, NCDValue reply_data);
  42. static int write_all (int fd, const uint8_t *data, size_t len);
  43. BReactor reactor;
  44. NCDRequest request;
  45. int main (int argc, char *argv[])
  46. {
  47. int res = 1;
  48. if (argc != 3) {
  49. fprintf(stderr, "Usage: %s <socket_path> <request_payload>\n", (argc > 0 ? argv[0] : ""));
  50. goto fail0;
  51. }
  52. char *socket_path = argv[1];
  53. char *request_payload_string = argv[2];
  54. BLog_InitStderr();
  55. BTime_Init();
  56. if (!BNetwork_GlobalInit()) {
  57. BLog(BLOG_ERROR, "BNetwork_Init failed");
  58. goto fail1;
  59. }
  60. if (!BReactor_Init(&reactor)) {
  61. BLog(BLOG_ERROR, "BReactor_Init failed");
  62. goto fail1;
  63. }
  64. NCDValue request_payload;
  65. if (!NCDValueParser_Parse(request_payload_string, strlen(request_payload_string), &request_payload)) {
  66. BLog(BLOG_ERROR, "BReactor_Init failed");
  67. goto fail2;
  68. }
  69. if (!NCDRequest_Init(&request, socket_path, &request_payload, &reactor, NULL, request_handler_finished, request_handler_reply)) {
  70. BLog(BLOG_ERROR, "NCDRequest_Init failed");
  71. NCDValue_Free(&request_payload);
  72. goto fail2;
  73. }
  74. NCDValue_Free(&request_payload);
  75. res = BReactor_Exec(&reactor);
  76. NCDRequest_Free(&request);
  77. fail2:
  78. BReactor_Free(&reactor);
  79. fail1:
  80. BLog_Free();
  81. fail0:
  82. DebugObjectGlobal_Finish();
  83. return res;
  84. }
  85. static void request_handler_finished (void *user, int is_error)
  86. {
  87. if (is_error) {
  88. BLog(BLOG_ERROR, "error");
  89. BReactor_Quit(&reactor, 1);
  90. return;
  91. }
  92. BReactor_Quit(&reactor, 0);
  93. }
  94. static void request_handler_reply (void *user, NCDValue reply_data)
  95. {
  96. char *str = NCDValueGenerator_Generate(&reply_data);
  97. if (!str) {
  98. BLog(BLOG_ERROR, "NCDValueGenerator_Generate failed");
  99. goto fail0;
  100. }
  101. if (!write_all(1, str, strlen(str))) {
  102. goto fail1;
  103. }
  104. if (!write_all(1, "\n", 1)) {
  105. goto fail1;
  106. }
  107. NCDRequest_Next(&request);
  108. free(str);
  109. NCDValue_Free(&reply_data);
  110. return;
  111. fail1:
  112. free(str);
  113. fail0:
  114. NCDValue_Free(&reply_data);
  115. BReactor_Quit(&reactor, 1);
  116. }
  117. static int write_all (int fd, const uint8_t *data, size_t len)
  118. {
  119. while (len > 0) {
  120. ssize_t res = write(fd, data, len);
  121. if (res <= 0) {
  122. BLog(BLOG_ERROR, "write failed");
  123. return 0;
  124. }
  125. data += res;
  126. len -= res;
  127. }
  128. return 1;
  129. }