main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. int infofd = 1;
  44. #define POLLFDS 1024
  45. struct pollfd *fds;
  46. int users_init(void);
  47. static int usage(void) {
  48. fprintf(stderr, "Usage: vccd (no argument)\n");
  49. return 1;
  50. }
  51. int becomes_daemon(void) {
  52. int pid;
  53. if ((pid = fork()))
  54. /* then prompt was there again */
  55. exit(0);
  56. umask(0);
  57. chdir("/");
  58. close(0);
  59. if (unlikely(open("/var/log/vccd.log", O_WRONLY | O_CREAT | O_APPEND | O_SYNC, 0644) < 0)) {
  60. perror("open(/var/run/vccd.log)");
  61. exit(1);
  62. }
  63. close(1);
  64. close(2);
  65. (void) dup(0);
  66. (void) dup(0);
  67. if (unlikely((infofd = open("/var/log/vccd-info.log", O_WRONLY | O_CREAT | O_APPEND | O_SYNC, 0644)) < 0)) {
  68. perror("open(/var/run/vccd-info.log)");
  69. exit(1);
  70. }
  71. setsid();
  72. if (unlikely((fd = open("/var/run/vccd.pid", O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)) {
  73. perror("open(/var/run/vccd.pid)");
  74. exit(1);
  75. }
  76. dprintf(fd, "%d\n", getpid());
  77. close(fd);
  78. /* fd 3 is now /var/run/vccd.pid */
  79. vcc_log(VCCD_VERSION " started. \n");
  80. return 0;
  81. }
  82. static int init_socket(void) {
  83. struct sockaddr_in addr;
  84. int i, enable;
  85. enable = 1;
  86. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  87. perror("socket()");
  88. exit(1);
  89. }
  90. memset(&addr, 0, sizeof(struct sockaddr_in));
  91. addr.sin_family = AF_INET;
  92. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  93. addr.sin_port = htons(VCC_PORT);
  94. if (unlikely(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)) {
  95. perror("setsockopt()");
  96. exit(1);
  97. }
  98. #ifdef SO_REUSEPORT
  99. if (unlikely(setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0)) {
  100. perror("setsockopt()");
  101. exit(1);
  102. }
  103. #endif
  104. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  105. perror("bind()");
  106. exit(1);
  107. }
  108. if (unlikely(listen(fd, SOMAXCONN) < 0)) {
  109. perror("listen()");
  110. exit(1);
  111. }
  112. if (!(fds = amalloc(sizeof(struct pollfd) * POLLFDS))) {
  113. vcc_log("*** malloc() failed\n");
  114. exit(1);
  115. }
  116. memset(fds, 0, sizeof(struct pollfd) * POLLFDS);
  117. for (i = 0; i < POLLFDS; i++)
  118. fds[i].fd = -1;
  119. fds[0].fd = fd;
  120. fds[0].events = POLLIN;
  121. next_fd = 1;
  122. return fd;
  123. }
  124. void sigpipe_handler(int nr) {
  125. (void) nr;
  126. vcc_log("SIGPIPE caused. \n");
  127. }
  128. static int setup_signals(void) {
  129. struct sigaction act;
  130. memset(&act, 0, sizeof(struct sigaction));
  131. act.sa_handler = sigpipe_handler;
  132. sigaction(SIGPIPE, &act, NULL);
  133. return 0;
  134. }
  135. /* a bad programme can joke us by a wrong size member in the struct
  136. * vcc_relay_header. he makes it large, but never writes after. then
  137. * we'll wait in the loop.
  138. *
  139. * we can use a 'sending' member in the struct connection and that was
  140. * what we tried to do. but againly, do that later. :-( */
  141. int read_until(int fd, void *p, int until) {
  142. int did_read = 0;
  143. int n;
  144. while ((did_read += n = read(fd, p, until)) < until) {
  145. if (!n)
  146. return 0;
  147. p += n;
  148. }
  149. return until;
  150. }
  151. int main(int argc, const char **argv) {
  152. __uint32_t size;
  153. int usr_fd, nfds_total, nfds_ready, i;
  154. void *buf;
  155. struct sockaddr_in usr_addr;
  156. (void) argv;
  157. if (unlikely(argc != 1))
  158. return usage();
  159. becomes_daemon();
  160. init_socket();
  161. users_init();
  162. setup_signals();
  163. nfds_total = 1;
  164. if (unlikely(!(buf = amalloc(REQ_SIZE)))) {
  165. vcc_log("*** malloc() failed\n");
  166. return 1;
  167. }
  168. for (;;) {
  169. nfds_ready = poll(fds, nfds_total, -1);
  170. if (unlikely(nfds_ready == -1)) {
  171. perror("poll()");
  172. break;
  173. }
  174. if (fds[0].revents & POLLIN) {
  175. size = sizeof(struct sockaddr_in);
  176. if (unlikely(usr_fd = accept(fd, (struct sockaddr *) &usr_addr, &size)) < 0) {
  177. perror("accept()");
  178. continue;
  179. }
  180. vcc_new_connection(usr_fd, &usr_addr.sin_addr);
  181. if (next_fd >= POLLFDS) {
  182. for (next_fd = 1; fds[next_fd].fd == -1 && next_fd < POLLFDS; next_fd++)
  183. ;
  184. if (next_fd == POLLFDS)
  185. vcc_log("*** connection queue full. \n");
  186. }
  187. fds[next_fd].fd = usr_fd;
  188. fds[next_fd].events = POLLIN;
  189. next_fd++;
  190. nfds_total++;
  191. }
  192. for (i = 1; i < nfds_total; i++) {
  193. if (fds[i].fd == -1)
  194. continue;
  195. if (fds[i].revents & POLLIN) {
  196. size = read_until(fds[i].fd, buf, sizeof(struct vcc_relay_header));
  197. if (!size) {
  198. vcc_close_connection(fds[i].fd);
  199. close(fds[i].fd);
  200. fds[i].fd = -1;
  201. continue;
  202. }
  203. if (ntohl(((struct vcc_relay_header *) buf)->magic) == VCC_MAGIC) {
  204. read_until(fds[i].fd, buf + sizeof(struct vcc_relay_header),
  205. REQ_SIZE - sizeof(struct vcc_relay_header));
  206. vcc_new_msg(fds[i].fd, buf);
  207. }
  208. else
  209. relay_new_msg(fds[i].fd, buf);
  210. fds[i].events = POLLOUT;
  211. }
  212. if (fds[i].revents & POLLOUT) {
  213. vcc_write_packages(fds[i].fd);
  214. fds[i].events = POLLIN;
  215. }
  216. fds[i].revents = 0;
  217. }
  218. }
  219. afree(buf, REQ_SIZE);
  220. afree(fds, POLLFDS * sizeof(struct pollfd));
  221. return 0;
  222. }