robot.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* vcc/robot.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc 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. * vcc 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 vcc. 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 <ctype.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <poll.h>
  31. #include <signal.h>
  32. #include <klist.h>
  33. #include <vcc/vcc.h>
  34. #include <vcc/plugin.h>
  35. #include <vcc/version.h>
  36. #include <vcc/pretty.h>
  37. #include <vcc/interfaces.h>
  38. int becomes_daemon(void) {
  39. int pid, fd;
  40. if ((pid = fork()))
  41. /* then prompt was there again */
  42. exit(0);
  43. umask(0);
  44. chdir("/");
  45. close(0);
  46. if (unlikely(open("/var/log/vcc.log", O_WRONLY | O_CREAT, 0644) < 0)) {
  47. perror("open(/var/run/vcc.log)");
  48. exit(1);
  49. }
  50. close(1);
  51. close(2);
  52. (void) dup(0);
  53. (void) dup(0);
  54. setsid();
  55. if (unlikely((fd = open("/var/run/vcc.pid", O_WRONLY | O_CREAT, 0644))) < 0) {
  56. perror("open(/var/run/vcc.pid)");
  57. exit(1);
  58. }
  59. dprintf(fd, "%d\n", getpid());
  60. close(fd);
  61. /* fd 3 is now /var/run/vcc.pid */
  62. printf(VCC_VERSION " started. \n");
  63. return 0;
  64. }
  65. int robot(void) {
  66. struct vcc_request req;
  67. plg_insert(find_plg("robot"));
  68. // becomes_daemon();
  69. vcc_login("robot", "robot");
  70. for (;;) {
  71. memset(&req, 0, REQ_SIZE);
  72. read(fd, &req, REQ_SIZE);
  73. robot_new_msg(&req);
  74. }
  75. return 0;
  76. }