grabber.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /** @file kexec.c
  2. */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <faux/list.h>
  11. #include <faux/buf.h>
  12. #include <faux/eloop.h>
  13. typedef struct {
  14. int fd_in;
  15. int fd_out;
  16. faux_buf_t *buf;
  17. } grabber_stream_t;
  18. typedef enum {
  19. IN,
  20. OUT
  21. } direction_t;
  22. grabber_stream_t *grabber_stream_new(int fd_in, int fd_out)
  23. {
  24. grabber_stream_t *stream = NULL;
  25. stream = malloc(sizeof(*stream));
  26. assert(stream);
  27. stream->fd_in = fd_in;
  28. stream->fd_out = fd_out;
  29. stream->buf = faux_buf_new(FAUX_BUF_UNLIMITED);
  30. return stream;
  31. }
  32. void grabber_stream_free(grabber_stream_t *stream)
  33. {
  34. if (!stream)
  35. return;
  36. faux_buf_free(stream->buf);
  37. faux_free(stream);
  38. }
  39. static bool_t grabber_stop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  40. void *associated_data, void *user_data)
  41. {
  42. // Happy compiler
  43. eloop = eloop;
  44. type = type;
  45. associated_data = associated_data;
  46. user_data = user_data;
  47. return BOOL_FALSE; // Stop Event Loop
  48. }
  49. static bool_t grabber_fd_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  50. void *associated_data, void *user_data)
  51. {
  52. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  53. faux_list_t *stream_list = (faux_list_t *)user_data;
  54. grabber_stream_t *stream = NULL;
  55. grabber_stream_t *cur_stream = NULL;
  56. direction_t direction = IN;
  57. faux_list_node_t *iter = NULL;
  58. type = type; // Happy compiler
  59. iter = faux_list_head(stream_list);
  60. while((cur_stream = (grabber_stream_t *)faux_list_each(&iter))) {
  61. if (info->fd == cur_stream->fd_in) {
  62. direction = IN;
  63. stream = cur_stream;
  64. break;
  65. }
  66. if (info->fd == cur_stream->fd_out) {
  67. direction = OUT;
  68. stream = cur_stream;
  69. break;
  70. }
  71. }
  72. if (!stream)
  73. return BOOL_FALSE; // Problems
  74. // Read data
  75. if (info->revents & POLLIN) {
  76. ssize_t r = 0;
  77. size_t total = 0;
  78. do {
  79. ssize_t len = 0;
  80. void *data = NULL;
  81. size_t readed = 0;
  82. len = faux_buf_dwrite_lock_easy(stream->buf, &data);
  83. if (len <= 0)
  84. break;
  85. r = read(stream->fd_in, data, len);
  86. readed = r < 0 ? 0 : r;
  87. faux_buf_dwrite_unlock_easy(stream->buf, readed);
  88. total += readed;
  89. } while (r > 0);
  90. if (total > 0)
  91. faux_eloop_include_fd_event(eloop, stream->fd_out, POLLOUT);
  92. }
  93. // Write data
  94. if (info->revents & POLLOUT) {
  95. ssize_t r = 0;
  96. ssize_t sent = 0;
  97. ssize_t len = 0;
  98. do {
  99. void *data = NULL;
  100. len = faux_buf_dread_lock_easy(stream->buf, &data);
  101. if (len <= 0)
  102. break;
  103. r = write(stream->fd_out, data, len);
  104. sent = r < 0 ? 0 : r;
  105. faux_buf_dread_unlock_easy(stream->buf, sent);
  106. } while (sent == len);
  107. if (faux_buf_len(stream->buf) == 0)
  108. faux_eloop_exclude_fd_event(eloop, stream->fd_out, POLLOUT);
  109. }
  110. // EOF
  111. if (info->revents & POLLHUP) {
  112. faux_eloop_del_fd(eloop, info->fd);
  113. if (IN == direction)
  114. stream->fd_in = -1;
  115. else
  116. stream->fd_out = -1;
  117. }
  118. iter = faux_list_head(stream_list);
  119. while((cur_stream = (grabber_stream_t *)faux_list_each(&iter))) {
  120. if (cur_stream->fd_in != -1)
  121. return BOOL_TRUE;
  122. }
  123. return BOOL_FALSE;
  124. }
  125. void grabber(int fds[][2])
  126. {
  127. faux_eloop_t *eloop = NULL;
  128. int i = 0;
  129. faux_list_t *stream_list = NULL;
  130. stream_list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  131. NULL, NULL, (void (*)(void *))grabber_stream_free);
  132. eloop = faux_eloop_new(NULL);
  133. faux_eloop_add_signal(eloop, SIGINT, grabber_stop_ev, NULL);
  134. faux_eloop_add_signal(eloop, SIGTERM, grabber_stop_ev, NULL);
  135. faux_eloop_add_signal(eloop, SIGQUIT, grabber_stop_ev, NULL);
  136. faux_eloop_add_signal(eloop, SIGPIPE, grabber_stop_ev, NULL);
  137. i = 0;
  138. while (fds[i][0] != -1) {
  139. grabber_stream_t *stream = grabber_stream_new(fds[i][0], fds[i][1]);
  140. int fflags = 0;
  141. faux_list_add(stream_list, stream);
  142. fflags = fcntl(stream->fd_in, F_GETFL);
  143. fcntl(stream->fd_in, F_SETFL, fflags | O_NONBLOCK);
  144. fflags = fcntl(stream->fd_out, F_GETFL);
  145. fcntl(stream->fd_out, F_SETFL, fflags | O_NONBLOCK);
  146. faux_eloop_add_fd(eloop, stream->fd_in, POLLIN, grabber_fd_ev, stream_list);
  147. faux_eloop_add_fd(eloop, stream->fd_out, 0, grabber_fd_ev, stream_list);
  148. i++;
  149. }
  150. faux_eloop_loop(eloop);
  151. faux_eloop_free(eloop);
  152. faux_list_free(stream_list);
  153. }