robot.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. extern int fd;
  38. int vcc_login(char *, char *);
  39. int robot_new_msg(struct vcc_request *);
  40. int becomes_daemon(void) {
  41. int pid, fd;
  42. if ((pid = fork()))
  43. /* then prompt was there again */
  44. exit(0);
  45. umask(0);
  46. chdir("/");
  47. close(0);
  48. if (unlikely(open("/var/log/vcc.log", O_WRONLY | O_CREAT, 0644) < 0)) {
  49. perror("open(/var/run/vcc.log)");
  50. exit(1);
  51. }
  52. close(1);
  53. close(2);
  54. (void) dup(0);
  55. (void) dup(0);
  56. setsid();
  57. if (unlikely((fd = open("/var/run/vcc.pid", O_WRONLY | O_CREAT, 0644))) < 0) {
  58. perror("open(/var/run/vcc.pid)");
  59. exit(1);
  60. }
  61. dprintf(fd, "%d\n", getpid());
  62. close(fd);
  63. /* fd 3 is now /var/run/vcc.pid */
  64. printf(VCC_VERSION " started. \n");
  65. return 0;
  66. }
  67. int robot(void) {
  68. struct vcc_request req;
  69. plg_insert(find_plg("robot"));
  70. // becomes_daemon();
  71. vcc_login("robot", "robot");
  72. for (;;) {
  73. memset(&req, 0, REQ_SIZE);
  74. read(fd, &req, REQ_SIZE);
  75. robot_new_msg(&req);
  76. }
  77. return 0;
  78. }