test_os_start_process.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_os_start_process.c
  18. * @brief testcase for os start process code
  19. *
  20. * This testcase simply calls the os start process code
  21. * giving a file descriptor to write stdout to. If the
  22. * correct data "HELLO" is read then all is well.
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "disk.h"
  27. static const char *test_phrase = "HELLO WORLD";
  28. static int ok;
  29. static struct GNUNET_OS_Process *proc;
  30. /**
  31. * Pipe to write to started processes stdin (on write end)
  32. */
  33. static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
  34. /**
  35. * Pipe to read from started processes stdout (on read end)
  36. */
  37. static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
  38. static struct GNUNET_SCHEDULER_Task *die_task;
  39. struct read_context
  40. {
  41. char buf[16];
  42. int buf_offset;
  43. const struct GNUNET_DISK_FileHandle *stdout_read_handle;
  44. };
  45. static struct read_context rc;
  46. static void
  47. end_task (void *cls)
  48. {
  49. if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
  50. {
  51. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  52. }
  53. GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
  54. GNUNET_OS_process_destroy (proc);
  55. proc = NULL;
  56. GNUNET_DISK_pipe_close (hello_pipe_stdout);
  57. GNUNET_DISK_pipe_close (hello_pipe_stdin);
  58. }
  59. static void
  60. read_call (void *cls)
  61. {
  62. int bytes;
  63. bytes = GNUNET_DISK_file_read (rc.stdout_read_handle,
  64. &rc.buf[rc.buf_offset],
  65. sizeof(rc.buf) - rc.buf_offset);
  66. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  67. "bytes is %d\n",
  68. bytes);
  69. if (bytes < 1)
  70. {
  71. GNUNET_break (0);
  72. ok = 1;
  73. GNUNET_SCHEDULER_cancel (die_task);
  74. (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
  75. return;
  76. }
  77. ok = strncmp (rc.buf, test_phrase, strlen (test_phrase));
  78. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  79. "read %s\n",
  80. &rc.buf[rc.buf_offset]);
  81. rc.buf_offset += bytes;
  82. if (0 == ok)
  83. {
  84. GNUNET_SCHEDULER_cancel (die_task);
  85. (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
  86. return;
  87. }
  88. GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
  89. rc.stdout_read_handle,
  90. &read_call,
  91. NULL);
  92. }
  93. static void
  94. run_task (void *cls)
  95. {
  96. char *fn;
  97. const struct GNUNET_DISK_FileHandle *stdout_read_handle;
  98. const struct GNUNET_DISK_FileHandle *wh;
  99. GNUNET_asprintf (&fn, "cat");
  100. hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES,
  101. GNUNET_NO);
  102. hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
  103. GNUNET_YES);
  104. if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
  105. {
  106. GNUNET_break (0);
  107. ok = 1;
  108. GNUNET_free (fn);
  109. return;
  110. }
  111. proc =
  112. GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR,
  113. hello_pipe_stdin, hello_pipe_stdout, NULL,
  114. fn,
  115. "test_gnunet_echo_hello", "-", NULL);
  116. GNUNET_free (fn);
  117. /* Close the write end of the read pipe */
  118. GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
  119. /* Close the read end of the write pipe */
  120. GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
  121. wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
  122. /* Write the test_phrase to the cat process */
  123. if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
  124. strlen (test_phrase) + 1)
  125. {
  126. GNUNET_break (0);
  127. ok = 1;
  128. return;
  129. }
  130. /* Close the write end to end the cycle! */
  131. GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
  132. stdout_read_handle =
  133. GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
  134. die_task =
  135. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  136. (GNUNET_TIME_UNIT_MINUTES, 1),
  137. &end_task,
  138. NULL);
  139. memset (&rc, 0, sizeof(rc));
  140. rc.stdout_read_handle = stdout_read_handle;
  141. GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
  142. stdout_read_handle,
  143. &read_call,
  144. NULL);
  145. }
  146. /**
  147. * Main method, starts scheduler with task1,
  148. * checks that "ok" is correct at the end.
  149. */
  150. static int
  151. check_run ()
  152. {
  153. ok = 1;
  154. GNUNET_SCHEDULER_run (&run_task, &ok);
  155. return ok;
  156. }
  157. /**
  158. * Test killing via pipe.
  159. */
  160. static int
  161. check_kill ()
  162. {
  163. char *fn;
  164. hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES,
  165. GNUNET_NO);
  166. hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
  167. GNUNET_YES);
  168. if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
  169. {
  170. return 1;
  171. }
  172. fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
  173. proc =
  174. GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
  175. hello_pipe_stdin,
  176. hello_pipe_stdout,
  177. NULL,
  178. fn,
  179. "gnunet-service-resolver", "-",
  180. NULL);
  181. if (NULL == proc)
  182. {
  183. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  184. "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
  185. return 77;
  186. }
  187. sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
  188. GNUNET_free (fn);
  189. if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
  190. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  191. GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
  192. GNUNET_OS_process_destroy (proc);
  193. proc = NULL;
  194. GNUNET_DISK_pipe_close (hello_pipe_stdout);
  195. GNUNET_DISK_pipe_close (hello_pipe_stdin);
  196. return 0;
  197. }
  198. /**
  199. * Test killing via pipe.
  200. */
  201. static int
  202. check_instant_kill ()
  203. {
  204. char *fn;
  205. hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES,
  206. GNUNET_NO);
  207. hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
  208. GNUNET_YES);
  209. if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
  210. {
  211. return 1;
  212. }
  213. fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
  214. proc =
  215. GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
  216. hello_pipe_stdin, hello_pipe_stdout, NULL,
  217. fn,
  218. "gnunet-service-resolver", "-", NULL);
  219. if (NULL == proc)
  220. {
  221. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  222. "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
  223. return 77;
  224. }
  225. if (0 != GNUNET_OS_process_kill (proc,
  226. GNUNET_TERM_SIG))
  227. {
  228. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  229. }
  230. GNUNET_free (fn);
  231. GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
  232. GNUNET_OS_process_destroy (proc);
  233. proc = NULL;
  234. GNUNET_DISK_pipe_close (hello_pipe_stdout);
  235. GNUNET_DISK_pipe_close (hello_pipe_stdin);
  236. return 0;
  237. }
  238. int
  239. main (int argc, char *argv[])
  240. {
  241. int ret;
  242. GNUNET_log_setup ("test-os-start-process",
  243. "WARNING",
  244. NULL);
  245. ret = 0;
  246. ret |= check_run ();
  247. ret |= check_kill ();
  248. ret |= check_instant_kill ();
  249. return ret;
  250. }
  251. /* end of test_os_start_process.c */