2 #include "gnunet_util_lib.h"
3 #include "gnunet_testbed_service.h"
7 * Generic logging shorthand
9 #define LOG(kind,...) \
10 GNUNET_log (kind, __VA_ARGS__)
13 * Debug logging shorthand
15 #define LOG_DEBUG(...) \
16 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
24 * The child process we spawn
26 static struct GNUNET_OS_Process *child;
29 * The arguments including the binary to spawn
34 * All our IP addresses
36 static char **our_addrs;
39 * Pipe used to communicate shutdown via signal.
41 static struct GNUNET_DISK_PipeHandle *sigpipe;
44 * Filename of the unique file
49 * Handle to the unique file
54 * The return code of the binary
56 static unsigned long child_exit_code;
59 * The process status of the child
61 static enum GNUNET_OS_ProcessStatusType child_status;
64 * how many IP addresses are currently assigned to us
66 static unsigned int num_addrs;
71 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
74 * Task to kill the child
76 static GNUNET_SCHEDULER_TaskIdentifier terminate_task_id;
79 * Task to kill the child
81 static GNUNET_SCHEDULER_TaskIdentifier child_death_task_id;
87 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
90 if (0 != child_exit_code)
92 LOG (GNUNET_ERROR_TYPE_WARNING, "Child exited with error code: %lu\n",
94 ret = 128 + (int) child_exit_code;
100 if ((NULL != fn) && (0 != unlink (fn)))
102 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
109 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 static int hard_kill;
113 GNUNET_assert (NULL != child);
115 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
116 &terminate_task, NULL);
123 LOG (GNUNET_ERROR_TYPE_WARNING,
124 "%d more interrupts needed to send SIGKILL to the child\n",
129 GNUNET_break (0 == GNUNET_OS_process_kill (child, SIGKILL));
134 GNUNET_break (0 == GNUNET_OS_process_kill (child, SIGTERM));
135 LOG (GNUNET_ERROR_TYPE_INFO, _("Waiting for child to exit.\n"));
140 * Task triggered whenever we receive a SIGCHLD (child
143 * @param cls closure, NULL if we need to self-restart
147 child_death_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 const struct GNUNET_DISK_FileHandle *pr;
152 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
153 child_death_task_id = GNUNET_SCHEDULER_NO_TASK;
154 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
156 child_death_task_id =
157 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
158 pr, &child_death_task, NULL);
161 /* consume the signal */
162 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
163 LOG_DEBUG ("Child died\n");
164 GNUNET_SCHEDULER_cancel (terminate_task_id);
165 terminate_task_id = GNUNET_SCHEDULER_NO_TASK;
166 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_status (child, &child_status,
168 GNUNET_OS_process_destroy (child);
170 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
175 destroy_hosts(struct GNUNET_TESTBED_Host **hosts, unsigned int nhosts)
179 GNUNET_assert (NULL != hosts);
180 for (host = 0; host < nhosts; host++)
181 if (NULL != hosts[host])
182 GNUNET_TESTBED_host_destroy (hosts[host]);
189 * The main scheduler run task
192 * @param tc scheduler task context
195 run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
197 struct GNUNET_TESTBED_Host **hosts;
198 const struct GNUNET_CONFIGURATION_Handle *null_cfg;
211 null_cfg = GNUNET_CONFIGURATION_create ();
212 nhosts = GNUNET_TESTBED_hosts_load_from_loadleveler (null_cfg, &hosts);
219 hostname_len = GNUNET_OS_get_hostname_max_length ();
220 hostname = GNUNET_malloc (hostname_len);
221 if (0 != gethostname (hostname, hostname_len))
223 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot get hostname. Exiting\n");
224 GNUNET_free (hostname);
225 destroy_hosts (hosts, nhosts);
229 if (NULL == strstr (GNUNET_TESTBED_host_get_hostname (hosts[0]), hostname))
231 LOG_DEBUG ("Exiting as we are not the lowest host\n");
232 GNUNET_free (hostname);
236 GNUNET_free (hostname);
237 destroy_hosts (hosts, nhosts);
238 tmpdir = getenv ("TMPDIR");
240 tmpdir = getenv ("TMP");
242 tmpdir = getenv ("TEMP");
245 (void) GNUNET_asprintf (&fn, "%s/gnunet-testbed-spawn.lock", tmpdir);
246 /* Open the unique file; we can create it then we can spawn the child process
248 fh = open (fn, O_CREAT | O_EXCL | O_CLOEXEC,
249 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
254 LOG_DEBUG ("Lock file already created by other process. Exiting\n");
258 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
262 /* Spawn the new process here */
263 LOG (GNUNET_ERROR_TYPE_INFO, _("Spawning process `%s'\n"), argv2[0]);
264 child = GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
271 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
276 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
277 &terminate_task, NULL);
278 child_death_task_id =
279 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
280 GNUNET_DISK_pipe_handle (sigpipe,
281 GNUNET_DISK_PIPE_END_READ),
282 &child_death_task, NULL);
287 * Signal handler called for SIGCHLD.
290 sighandler_child_death ()
293 int old_errno = errno; /* back-up errno */
296 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
297 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
299 errno = old_errno; /* restore errno */
304 * Execution start point
307 main (int argc, char *argv[])
309 struct GNUNET_SIGNAL_Context *shc_chld;
315 printf ("Need arguments: gnunet-testbed-mpi-spawn <cmd> <cmd_args>");
318 if (GNUNET_OK != GNUNET_log_setup ("gnunet-testbed-spawn", NULL, NULL))
323 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
324 GNUNET_NO, GNUNET_NO)))
331 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
332 if (NULL == shc_chld)
334 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot install a signal handler\n");
337 argv2 = GNUNET_malloc (sizeof (char *) * argc);
338 for (cnt = 1; cnt < argc; cnt++)
339 argv2[cnt - 1] = argv[cnt];
340 GNUNET_SCHEDULER_run (run, NULL);
342 GNUNET_SIGNAL_handler_uninstall (shc_chld);
344 GNUNET_DISK_pipe_close (sigpipe);
345 GNUNET_free_non_null (fn);
346 if (GNUNET_OK != ret)