vcr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* vcc/plugins/vcr/vcr.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 <string.h>
  20. #include <stdlib.h>
  21. #include <sys/wait.h>
  22. #include <vcc/vcc.h>
  23. #include <vcc/pretty.h>
  24. #include <vcc/plugin.h>
  25. #include <vcc/hooks.h>
  26. #include <vcc/interfaces.h>
  27. #include <vcc/version.h>
  28. #include <readconf/readconf.h>
  29. #include <readconf/do_op_set.h>
  30. extern char **environ;
  31. static int vcr_listen_sid;
  32. static char *vcr_executable;
  33. extern char usrname[];
  34. static int vcr_init(void);
  35. struct cmdtable vcr_cmdtab[] = {
  36. { NULL, NULL, NULL }
  37. };
  38. struct plugin plg_vcr = {
  39. .state = 0,
  40. .name = "vcr",
  41. .descr = "vcc bridge. ",
  42. .cmdtab = vcr_cmdtab,
  43. .init = vcr_init
  44. };
  45. #define this_plg (&plg_vcr)
  46. static char *set_environments(struct hook_info *hi) {
  47. char *t;
  48. if (unlikely(!(t = malloc(64)))) {
  49. fprintf(stderr, "*** malloc() failed\n");
  50. return NULL;
  51. }
  52. sprintf(t, "%d", hi->sid);
  53. setenv("VCR_TYPE", hi->type == HOOK_RECV ? "recv" : "send", 1);
  54. setenv("VCR_USER", hi->usr, 1);
  55. setenv("VCR_MSG", hi->msg, 1);
  56. setenv("VCR_SID", t, 1);
  57. return t;
  58. }
  59. static int unset_environments(void) {
  60. unsetenv("VCR_TYPE");
  61. unsetenv("VCR_USER");
  62. unsetenv("VCR_MSG");
  63. unsetenv("VCR_SID");
  64. return 0;
  65. }
  66. static int send_result(int fd) {
  67. char p[MSG_SIZE];
  68. read(fd, &p, MSG_SIZE);
  69. p[MSG_SIZE - 1] = 0;
  70. send_msg(p, usrname);
  71. return 0;
  72. }
  73. static int vcr_execute(struct hook_info *hi) {
  74. int pid, status;
  75. int pipefd[2];
  76. char *t;
  77. t = set_environments(hi);
  78. if (pipe(pipefd) < 0) {
  79. perror("pipe");
  80. return 1;
  81. }
  82. switch ((pid = fork())) {
  83. case 0:
  84. close(pipefd[0]);
  85. dup2(pipefd[1], 1);
  86. execlp(vcr_executable, vcr_executable, NULL);
  87. printf("%s\n", vcr_executable);
  88. perror("execlp");
  89. abort();
  90. case -1:
  91. perror("fork");
  92. return 1;
  93. default:
  94. wait4(pid, &status, 0, NULL);
  95. printf("%d exited with status %d. \n", pid, status);
  96. send_result(pipefd[0]);
  97. unset_environments();
  98. free(t);
  99. }
  100. close(pipefd[0]);
  101. close(pipefd[1]);
  102. return status;
  103. }
  104. static char *vcr_hook(struct hook_info *hi) {
  105. if (hi->sid != vcr_listen_sid)
  106. return HOOK_IGNORE;
  107. vcr_execute(hi);
  108. return HOOK_IGNORE;
  109. }
  110. static int load_vcr_config(void) {
  111. struct readconf_uintconf *conf_sid;
  112. struct readconf_strconf *conf_exec;
  113. if (!(conf_sid = readconf_find_uintname("vcr_listen_sid")) ||
  114. !(conf_exec = readconf_find_strname("vcr_executable"))) {
  115. fprintf(stderr, "*** vcr: cannot load configure\n");
  116. return 1;
  117. }
  118. vcr_listen_sid = (int) conf_sid->conf_value;
  119. vcr_executable = conf_exec->conf_value;
  120. printf("vcr is listening on sid %d. \n", vcr_listen_sid);
  121. printf("vcr's executable is %s\n", vcr_executable);
  122. return 0;
  123. }
  124. static int vcr_init(void) {
  125. if (load_vcr_config())
  126. return 1;
  127. if (mode != MODE_ROBOT) {
  128. fprintf(stderr, "*** vcr should be loaded in the robot mode. \n");
  129. return 1;
  130. }
  131. register_hook(HOOK_SEND, vcr_hook);
  132. register_hook(HOOK_RECV, vcr_hook);
  133. join_session((current_sid = vcr_listen_sid));
  134. return 0;
  135. }