main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <vcc/vcc.h>
  31. #include <vcc/vccd.h>
  32. #include <vcc/version.h>
  33. /* well, it's not a really daemon because we didn't do things about
  34. * sessions or pgrps, and didn't close stdout stderr stdin etc.
  35. *
  36. * but that only makes things more difficult and hard to understand
  37. * that's why we just fork() and exit() */
  38. /* this is the shortest name i think. it stores the fd of the server
  39. * socket */
  40. int fd;
  41. int next_fd;
  42. #define POLLFDS 1024
  43. struct pollfd *fds;
  44. static int usage(void) {
  45. fprintf(stderr, "Usage: vccd (no argument)\n");
  46. return 1;
  47. }
  48. /*static */int becomes_daemon(void) {
  49. int pid;
  50. if ((pid = fork()))
  51. /* then prompt was there again */
  52. exit(0);
  53. umask(0);
  54. chdir("/");
  55. close(0);
  56. if (unlikely(open("/var/log/vccd.log", O_WRONLY | O_CREAT, 0644) < 0)) {
  57. perror("open(/var/run/vccd.log)");
  58. exit(1);
  59. }
  60. close(1);
  61. close(2);
  62. (void) dup(0);
  63. (void) dup(0);
  64. if (unlikely(fd = open("/var/run/vccd.pid", O_WRONLY | O_CREAT, 0644)) < 0) {
  65. perror("open(/var/run/vccd.pid)");
  66. exit(1);
  67. }
  68. dprintf(fd, "%d\n", getpid());
  69. close(fd);
  70. /* fd 3 is now /var/run/vccd.pid */
  71. vcc_log(VCCD_VERSION " started. \n");
  72. return 0;
  73. }
  74. static int init_socket(void) {
  75. struct sockaddr_in addr;
  76. int i;
  77. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  78. perror("socket()");
  79. exit(1);
  80. }
  81. memset(&addr, 0, sizeof(struct sockaddr_in));
  82. addr.sin_family = AF_INET;
  83. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  84. addr.sin_port = htons(VCC_PORT);
  85. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  86. perror("bind()");
  87. exit(1);
  88. }
  89. if (unlikely(listen(fd, SOMAXCONN) < 0)) {
  90. perror("listen()");
  91. exit(1);
  92. }
  93. if (!(fds = malloc(sizeof(struct pollfd) * POLLFDS))) {
  94. vcc_log("*** malloc() failed\n");
  95. exit(1);
  96. }
  97. for (i = 0; i < POLLFDS; i++)
  98. fds[i].fd = -1;
  99. fds[0].fd = fd;
  100. fds[0].events = POLLIN;
  101. next_fd = 1;
  102. return fd;
  103. }
  104. int main(int argc, const char **argv) {
  105. __uint32_t size;
  106. int usr_fd, nfds_total, nfds_ready, i;
  107. void *buf;
  108. struct sockaddr_in usr_addr;
  109. if (unlikely(argc != 1))
  110. return usage();
  111. // becomes_daemon();
  112. init_socket();
  113. nfds_total = 1;
  114. for (;;) {
  115. nfds_ready = poll(fds, nfds_total, -1);
  116. if (unlikely(nfds_ready == -1)) {
  117. perror("poll()");
  118. break;
  119. }
  120. if (fds[0].revents & POLLIN) {
  121. size = sizeof(struct sockaddr_in);
  122. if (unlikely(usr_fd = accept(fd, (struct sockaddr *) &usr_addr, &size)) < 0) {
  123. perror("accept()");
  124. continue;
  125. }
  126. vcc_new_connection(usr_fd, &usr_addr.sin_addr);
  127. if (next_fd >= POLLFDS) {
  128. for (next_fd = 1; fds[next_fd].fd == -1 && next_fd < POLLFDS; next_fd++)
  129. ;
  130. if (next_fd == POLLFDS)
  131. vcc_log("*** connection queue full. \n");
  132. }
  133. fds[next_fd].fd = usr_fd;
  134. fds[next_fd].events = POLLIN;
  135. next_fd++;
  136. nfds_total++;
  137. }
  138. for (i = 1; i < nfds_total; i++) {
  139. if (fds[i].fd == -1)
  140. continue;
  141. if (fds[i].revents & POLLIN) {
  142. if (!(buf = malloc(REQ_SIZE))) {
  143. vcc_log("*** malloc() failed\n");
  144. break;
  145. }
  146. size = read(fds[i].fd, buf, REQ_SIZE);
  147. if (!size) {
  148. vcc_close_connection(fds[i].fd);
  149. fds[i].fd = -1;
  150. free(buf);
  151. continue;
  152. }
  153. vcc_new_msg(fds[i].fd, buf);
  154. free(buf);
  155. fds[i].events = POLLOUT;
  156. }
  157. if (fds[i].revents & POLLOUT) {
  158. vcc_write_packages(fds[i].fd);
  159. fds[i].events = POLLIN;
  160. }
  161. fds[i].revents = 0;
  162. }
  163. }
  164. return 0;
  165. }