grabber.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 (must locates before write data code)
  75. if (info->revents & POLLIN) {
  76. ssize_t r = 0;
  77. do {
  78. ssize_t len = 0;
  79. void *data = NULL;
  80. size_t readed = 0;
  81. len = faux_buf_dwrite_lock_easy(stream->buf, &data);
  82. if (len <= 0)
  83. break;
  84. r = read(stream->fd_in, data, len);
  85. readed = r < 0 ? 0 : r;
  86. faux_buf_dwrite_unlock_easy(stream->buf, readed);
  87. } while (r > 0);
  88. }
  89. // Write data
  90. if (faux_buf_len(stream->buf) > 0) {
  91. ssize_t r = 0;
  92. ssize_t sent = 0;
  93. ssize_t len = 0;
  94. do {
  95. void *data = NULL;
  96. len = faux_buf_dread_lock_easy(stream->buf, &data);
  97. if (len <= 0)
  98. break;
  99. r = write(stream->fd_out, data, len);
  100. sent = r < 0 ? 0 : r;
  101. faux_buf_dread_unlock_easy(stream->buf, sent);
  102. } while (sent == len);
  103. }
  104. // EOF
  105. if (info->revents & POLLHUP) {
  106. faux_eloop_del_fd(eloop, info->fd);
  107. if (IN == direction)
  108. stream->fd_in = -1;
  109. else
  110. stream->fd_out = -1;
  111. }
  112. // Check if there additional data to send
  113. if (stream->fd_out >= 0) {
  114. if (faux_buf_len(stream->buf) == 0) {
  115. faux_eloop_exclude_fd_event(eloop, stream->fd_out, POLLOUT);
  116. // If correspondent IN is closed and buffer is empty
  117. // then out stream descriptor is not needed any more too
  118. if (stream->fd_in < 0)
  119. stream->fd_out = -1;
  120. } else {
  121. faux_eloop_include_fd_event(eloop, stream->fd_out, POLLOUT);
  122. }
  123. }
  124. iter = faux_list_head(stream_list);
  125. while((cur_stream = (grabber_stream_t *)faux_list_each(&iter))) {
  126. // Check is there any writers
  127. if (cur_stream->fd_in != -1)
  128. return BOOL_TRUE;
  129. // Process can have no writers but buffer can be non-empty in
  130. // same time
  131. if (cur_stream->fd_out != -1)
  132. return BOOL_TRUE;
  133. }
  134. return BOOL_FALSE;
  135. }
  136. void grabber(int fds[][2])
  137. {
  138. faux_eloop_t *eloop = NULL;
  139. int i = 0;
  140. faux_list_t *stream_list = NULL;
  141. stream_list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  142. NULL, NULL, (void (*)(void *))grabber_stream_free);
  143. eloop = faux_eloop_new(NULL);
  144. faux_eloop_add_signal(eloop, SIGINT, grabber_stop_ev, NULL);
  145. faux_eloop_add_signal(eloop, SIGTERM, grabber_stop_ev, NULL);
  146. faux_eloop_add_signal(eloop, SIGQUIT, grabber_stop_ev, NULL);
  147. faux_eloop_add_signal(eloop, SIGPIPE, grabber_stop_ev, NULL);
  148. i = 0;
  149. while (fds[i][0] != -1) {
  150. grabber_stream_t *stream = grabber_stream_new(fds[i][0], fds[i][1]);
  151. int fflags = 0;
  152. faux_list_add(stream_list, stream);
  153. fflags = fcntl(stream->fd_in, F_GETFL);
  154. fcntl(stream->fd_in, F_SETFL, fflags | O_NONBLOCK);
  155. fflags = fcntl(stream->fd_out, F_GETFL);
  156. fcntl(stream->fd_out, F_SETFL, fflags | O_NONBLOCK);
  157. faux_eloop_add_fd(eloop, stream->fd_in, POLLIN, grabber_fd_ev, stream_list);
  158. faux_eloop_add_fd(eloop, stream->fd_out, 0, grabber_fd_ev, stream_list);
  159. i++;
  160. }
  161. faux_eloop_loop(eloop);
  162. faux_eloop_free(eloop);
  163. faux_list_free(stream_list);
  164. }