2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file util/test_os_start_process.c
22 * @brief testcase for os start process code
24 * This testcase simply calls the os start process code
25 * giving a file descriptor to write stdout to. If the
26 * correct data "HELLO" is read then all is well.
29 #include "gnunet_common.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
37 static const char *test_phrase = "HELLO WORLD";
41 static struct GNUNET_OS_Process *proc;
44 * Pipe to write to started processes stdin (on write end)
46 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
49 * Pipe to read from started processes stdout (on read end)
51 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
53 static GNUNET_SCHEDULER_TaskIdentifier die_task;
59 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
62 struct read_context rc;
65 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
67 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
69 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
71 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
72 GNUNET_OS_process_destroy (proc);
74 GNUNET_DISK_pipe_close (hello_pipe_stdout);
75 GNUNET_DISK_pipe_close (hello_pipe_stdin);
80 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 bytes = GNUNET_DISK_file_read (rc.stdout_read_handle, &rc.buf[rc.buf_offset], \
85 sizeof (rc.buf) - rc.buf_offset);
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "bytes is %d\n", bytes);
93 GNUNET_SCHEDULER_cancel (die_task);
94 GNUNET_SCHEDULER_add_now (&end_task, NULL);
98 ok = strncmp (rc.buf, test_phrase, strlen (test_phrase));
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "read %s\n", &rc.buf[rc.buf_offset]);
100 rc.buf_offset += bytes;
104 GNUNET_SCHEDULER_cancel (die_task);
105 GNUNET_SCHEDULER_add_now (&end_task, NULL);
109 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
110 rc.stdout_read_handle, &read_call,
117 run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
121 const struct GNUNET_DISK_FileHandle *wh;
124 GNUNET_asprintf (&fn, "cat");
126 GNUNET_asprintf (&fn, "w32cat");
129 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
130 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
132 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
141 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR,
142 hello_pipe_stdin, hello_pipe_stdout, fn,
143 "test_gnunet_echo_hello", "-", NULL);
146 /* Close the write end of the read pipe */
147 GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
148 /* Close the read end of the write pipe */
149 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
151 wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
153 /* Write the test_phrase to the cat process */
154 if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
155 strlen (test_phrase) + 1)
162 /* Close the write end to end the cycle! */
163 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
166 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
169 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
170 (GNUNET_TIME_UNIT_MINUTES, 1), &end_task,
173 memset (&rc, 0, sizeof (rc));
174 rc.stdout_read_handle = stdout_read_handle;
175 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
176 stdout_read_handle, &read_call,
182 * Main method, starts scheduler with task1,
183 * checks that "ok" is correct at the end.
189 GNUNET_SCHEDULER_run (&run_task, &ok);
195 * Test killing via pipe.
202 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
203 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
204 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
208 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
210 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, hello_pipe_stdin, hello_pipe_stdout, fn,
211 "gnunet-service-resolver", "-", NULL);
212 sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
214 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
215 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
216 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
217 GNUNET_OS_process_destroy (proc);
219 GNUNET_DISK_pipe_close (hello_pipe_stdout);
220 GNUNET_DISK_pipe_close (hello_pipe_stdin);
226 * Test killing via pipe.
229 check_instant_kill ()
233 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
234 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
235 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
239 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
241 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, hello_pipe_stdin, hello_pipe_stdout, fn,
242 "gnunet-service-resolver", "-", NULL);
243 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
245 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
248 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
249 GNUNET_OS_process_destroy (proc);
251 GNUNET_DISK_pipe_close (hello_pipe_stdout);
252 GNUNET_DISK_pipe_close (hello_pipe_stdin);
258 main (int argc, char *argv[])
262 GNUNET_log_setup ("test-os-start-process",
267 ret |= check_kill ();
268 ret |= check_instant_kill ();
272 /* end of test_os_start_process.c */