main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* vccd/main.c
  2. *
  3. * This file is part of vccd.
  4. *
  5. * vccd is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vccd is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vccd. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <poll.h>
  30. #include <signal.h>
  31. #include <vcc/vcc.h>
  32. #include <vcc/vccd.h>
  33. #include <vcc/version.h>
  34. /* well, it's not a really daemon because we didn't do things about
  35. * sessions or pgrps, and didn't close stdout stderr stdin etc.
  36. *
  37. * but that only makes things more difficult and hard to understand
  38. * that's why we just fork() and exit() */
  39. /* this is the shortest name i think. it stores the fd of the server
  40. * socket */
  41. int fd;
  42. int next_fd;
  43. #define POLLFDS 1024
  44. struct pollfd *fds;
  45. int users_init(void);
  46. static int usage(void) {
  47. fprintf(stderr, "Usage: vccd (no argument)\n");
  48. return 1;
  49. }
  50. int becomes_daemon(void) {
  51. int pid;
  52. if ((pid = fork()))
  53. /* then prompt was there again */
  54. exit(0);
  55. umask(0);
  56. chdir("/");
  57. close(0);
  58. if (unlikely(open("/var/log/vccd.log", O_WRONLY | O_CREAT | O_APPEND | O_SYNC, 0644) < 0)) {
  59. perror("open(/var/run/vccd.log)");
  60. exit(1);
  61. }
  62. close(1);
  63. close(2);
  64. (void) dup(0);
  65. (void) dup(0);
  66. setsid();
  67. if (unlikely((fd = open("/var/run/vccd.pid", O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)) {
  68. perror("open(/var/run/vccd.pid)");
  69. exit(1);
  70. }
  71. dprintf(fd, "%d\n", getpid());
  72. close(fd);
  73. /* fd 3 is now /var/run/vccd.pid */
  74. vcc_log(VCCD_VERSION " started. \n");
  75. return 0;
  76. }
  77. static int init_socket(void) {
  78. struct sockaddr_in addr;
  79. int i;
  80. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  81. perror("socket()");
  82. exit(1);
  83. }
  84. memset(&addr, 0, sizeof(struct sockaddr_in));
  85. addr.sin_family = AF_INET;
  86. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  87. addr.sin_port = htons(VCC_PORT);
  88. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  89. perror("bind()");
  90. exit(1);
  91. }
  92. if (unlikely(listen(fd, SOMAXCONN) < 0)) {
  93. perror("listen()");
  94. exit(1);
  95. }
  96. if (!(fds = amalloc(sizeof(struct pollfd) * POLLFDS))) {
  97. vcc_log("*** malloc() failed\n");
  98. exit(1);
  99. }
  100. memset(fds, 0, sizeof(struct pollfd) * POLLFDS);
  101. for (i = 0; i < POLLFDS; i++)
  102. fds[i].fd = -1;
  103. fds[0].fd = fd;
  104. fds[0].events = POLLIN;
  105. next_fd = 1;
  106. return fd;
  107. }
  108. void sigpipe_handler(int nr) {
  109. (void) nr;
  110. vcc_log("SIGPIPE caused. \n");
  111. }
  112. static int setup_signals(void) {
  113. struct sigaction act;
  114. memset(&act, 0, sizeof(struct sigaction));
  115. act.sa_handler = sigpipe_handler;
  116. sigaction(SIGPIPE, &act, NULL);
  117. return 0;
  118. }
  119. /* a bad programme can joke us by a wrong size member in the struct
  120. * vcc_relay_header. he makes it large, but never writes after. then
  121. * we'll wait in the loop.
  122. *
  123. * we can use a 'sending' member in the struct connection and that was
  124. * what we tried to do. but againly, do that later. :-( */
  125. int read_until(int fd, void *p, int until) {
  126. int did_read = 0;
  127. int n;
  128. while ((did_read += n = read(fd, p, until)) < until) {
  129. if (!n)
  130. return 0;
  131. p += n;
  132. }
  133. return until;
  134. }
  135. int main(int argc, const char **argv) {
  136. __uint32_t size;
  137. int usr_fd, nfds_total, nfds_ready, i;
  138. void *buf;
  139. struct sockaddr_in usr_addr;
  140. (void) argv;
  141. if (unlikely(argc != 1))
  142. return usage();
  143. // becomes_daemon();
  144. init_socket();
  145. users_init();
  146. setup_signals();
  147. nfds_total = 1;
  148. if (unlikely(!(buf = amalloc(REQ_SIZE)))) {
  149. vcc_log("*** malloc() failed\n");
  150. return 1;
  151. }
  152. for (;;) {
  153. nfds_ready = poll(fds, nfds_total, -1);
  154. if (unlikely(nfds_ready == -1)) {
  155. perror("poll()");
  156. break;
  157. }
  158. if (fds[0].revents & POLLIN) {
  159. size = sizeof(struct sockaddr_in);
  160. if (unlikely(usr_fd = accept(fd, (struct sockaddr *) &usr_addr, &size)) < 0) {
  161. perror("accept()");
  162. continue;
  163. }
  164. vcc_new_connection(usr_fd, &usr_addr.sin_addr);
  165. if (next_fd >= POLLFDS) {
  166. for (next_fd = 1; fds[next_fd].fd == -1 && next_fd < POLLFDS; next_fd++)
  167. ;
  168. if (next_fd == POLLFDS)
  169. vcc_log("*** connection queue full. \n");
  170. }
  171. fds[next_fd].fd = usr_fd;
  172. fds[next_fd].events = POLLIN;
  173. next_fd++;
  174. nfds_total++;
  175. }
  176. for (i = 1; i < nfds_total; i++) {
  177. if (fds[i].fd == -1)
  178. continue;
  179. if (fds[i].revents & POLLIN) {
  180. size = read_until(fds[i].fd, buf, sizeof(struct vcc_relay_header));
  181. if (!size) {
  182. vcc_close_connection(fds[i].fd);
  183. close(fds[i].fd);
  184. fds[i].fd = -1;
  185. continue;
  186. }
  187. if (ntohl(((struct vcc_relay_header *) buf)->magic) == VCC_MAGIC) {
  188. read_until(fds[i].fd, buf + sizeof(struct vcc_relay_header),
  189. REQ_SIZE - sizeof(struct vcc_relay_header));
  190. vcc_new_msg(fds[i].fd, buf);
  191. }
  192. else
  193. relay_new_msg(fds[i].fd, buf);
  194. fds[i].events = POLLOUT;
  195. }
  196. if (fds[i].revents & POLLOUT) {
  197. vcc_write_packages(fds[i].fd);
  198. fds[i].events = POLLIN;
  199. }
  200. fds[i].revents = 0;
  201. }
  202. }
  203. afree(buf, REQ_SIZE);
  204. afree(fds, POLLFDS * sizeof(struct pollfd));
  205. return 0;
  206. }