main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/version.h>
  32. /* well, it's not a really daemon because we didn't do things about
  33. * sessions or pgrps, and didn't close stdout stderr stdin etc.
  34. *
  35. * but that only makes things more difficult and hard to understand
  36. * that's why we just fork() and exit() */
  37. #define vcc_log(msg, ...) fprintf(stderr, msg, ##__VA_ARGS__)
  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. char *buf;
  108. const char msg[] = {"Hello, I'm the server. \n"};
  109. struct sockaddr_in usr_addr;
  110. if (unlikely(argc != 1))
  111. return usage();
  112. // becomes_daemon();
  113. init_socket();
  114. nfds_total = 1;
  115. for (;;) {
  116. nfds_ready = poll(fds, nfds_total, -1);
  117. if (unlikely(nfds_ready == -1)) {
  118. perror("poll()");
  119. break;
  120. }
  121. if (fds[0].revents & POLLIN) {
  122. size = sizeof(struct sockaddr_in);
  123. if (unlikely(usr_fd = accept(fd, (struct sockaddr *) &usr_addr, &size)) < 0) {
  124. perror("accept()");
  125. continue;
  126. }
  127. vcc_log("New connection from %s\n", inet_ntoa(usr_addr.sin_addr));
  128. if (next_fd >= POLLFDS) {
  129. for (next_fd = 1; fds[next_fd].fd == -1 && next_fd < POLLFDS; next_fd++)
  130. ;
  131. if (next_fd == POLLFDS)
  132. vcc_log("*** connection queue full. \n");
  133. }
  134. fds[next_fd].fd = usr_fd;
  135. fds[next_fd].events = POLLIN;
  136. next_fd++;
  137. nfds_total++;
  138. }
  139. for (i = 1; i < nfds_total; i++) {
  140. if (fds[i].fd == -1)
  141. continue;
  142. if (fds[i].revents & POLLIN) {
  143. if (!(buf = malloc(REQ_SIZE))) {
  144. vcc_log("*** malloc() failed\n");
  145. break;
  146. }
  147. size = read(fds[i].fd, buf, REQ_SIZE);
  148. if (!size) {
  149. vcc_log("Closed. \n");
  150. fds[i].fd = -1;
  151. free(buf);
  152. continue;
  153. }
  154. vcc_log("message received: %s\n", buf);
  155. free(buf);
  156. fds[i].events = POLLOUT;
  157. }
  158. if (fds[i].revents & POLLOUT) {
  159. write(fds[i].fd, msg, sizeof(msg));
  160. vcc_log("message sent. \n");
  161. fds[i].events = POLLIN;
  162. }
  163. fds[i].revents = 0;
  164. }
  165. }
  166. return 0;
  167. }