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 * Pipe used to communicate shutdown via signal.
36 static struct GNUNET_DISK_PipeHandle *sigpipe;
39 * Filename of the unique file
44 * Handle to the unique file
49 * The return code of the binary
51 static unsigned long child_exit_code;
54 * The process status of the child
56 static enum GNUNET_OS_ProcessStatusType child_status;
61 static struct GNUNET_SCHEDULER_Task * shutdown_task_id;
64 * Task to kill the child
66 static struct GNUNET_SCHEDULER_Task * terminate_task_id;
69 * Task to kill the child
71 static struct GNUNET_SCHEDULER_Task * child_death_task_id;
77 shutdown_task (void *cls)
79 shutdown_task_id = NULL;
80 if (0 != child_exit_code)
82 LOG (GNUNET_ERROR_TYPE_WARNING, "Child exited with error code: %lu\n",
84 ret = 128 + (int) child_exit_code;
90 if ((NULL != fn) && (0 != unlink (fn)))
92 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
99 terminate_task (void *cls)
101 static int hard_kill;
103 GNUNET_assert (NULL != child);
105 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
106 &terminate_task, NULL);
113 LOG (GNUNET_ERROR_TYPE_WARNING,
114 "%d more interrupts needed to send SIGKILL to the child\n",
119 GNUNET_break (0 == GNUNET_OS_process_kill (child, SIGKILL));
124 GNUNET_break (0 == GNUNET_OS_process_kill (child, GNUNET_TERM_SIG));
125 LOG (GNUNET_ERROR_TYPE_INFO, _("Waiting for child to exit.\n"));
130 * Task triggered whenever we receive a SIGCHLD (child
133 * @param cls closure, NULL if we need to self-restart
136 child_death_task (void *cls)
138 const struct GNUNET_DISK_FileHandle *pr;
140 const struct GNUNET_SCHEDULER_TaskContext *tc;
143 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
144 child_death_task_id = NULL;
145 tc = GNUNET_SCHEDULER_get_task_context ();
146 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
148 child_death_task_id =
149 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
150 pr, &child_death_task, NULL);
153 /* consume the signal */
154 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
155 LOG_DEBUG ("Child died\n");
156 GNUNET_SCHEDULER_cancel (terminate_task_id);
157 terminate_task_id = NULL;
158 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_status (child, &child_status,
160 GNUNET_OS_process_destroy (child);
162 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
167 destroy_hosts(struct GNUNET_TESTBED_Host **hosts, unsigned int nhosts)
171 GNUNET_assert (NULL != hosts);
172 for (host = 0; host < nhosts; host++)
173 if (NULL != hosts[host])
174 GNUNET_TESTBED_host_destroy (hosts[host]);
181 * The main scheduler run task
188 struct GNUNET_TESTBED_Host **hosts;
189 const struct GNUNET_CONFIGURATION_Handle *null_cfg;
195 null_cfg = GNUNET_CONFIGURATION_create ();
196 nhosts = GNUNET_TESTBED_hosts_load_from_loadleveler (null_cfg, &hosts);
203 hostname_len = GNUNET_OS_get_hostname_max_length ();
204 hostname = GNUNET_malloc (hostname_len);
205 if (0 != gethostname (hostname, hostname_len))
207 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot get hostname. Exiting\n");
208 GNUNET_free (hostname);
209 destroy_hosts (hosts, nhosts);
213 if (NULL == strstr (GNUNET_TESTBED_host_get_hostname (hosts[0]), hostname))
215 LOG_DEBUG ("Exiting as `%s' is not the lowest host\n", hostname);
216 GNUNET_free (hostname);
220 LOG_DEBUG ("Will be executing `%s' on host `%s'\n", argv2[0], hostname);
221 GNUNET_free (hostname);
222 destroy_hosts (hosts, nhosts);
223 tmpdir = getenv ("TMPDIR");
225 tmpdir = getenv ("TMP");
227 tmpdir = getenv ("TEMP");
230 (void) GNUNET_asprintf (&fn, "%s/gnunet-testbed-spawn.lock", tmpdir);
231 /* Open the unique file; we can create it then we can spawn the child process
233 fh = open (fn, O_CREAT | O_EXCL | O_CLOEXEC,
234 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
239 LOG_DEBUG ("Lock file already created by other process. Exiting\n");
243 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
247 /* Spawn the new process here */
248 LOG (GNUNET_ERROR_TYPE_INFO, _("Spawning process `%s'\n"), argv2[0]);
249 child = GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
256 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
261 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
262 &terminate_task, NULL);
263 child_death_task_id =
264 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
265 GNUNET_DISK_pipe_handle (sigpipe,
266 GNUNET_DISK_PIPE_END_READ),
267 &child_death_task, NULL);
272 * Signal handler called for SIGCHLD.
275 sighandler_child_death ()
278 int old_errno = errno; /* back-up errno */
281 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
282 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
284 errno = old_errno; /* restore errno */
289 * Execution start point
292 main (int argc, char *argv[])
294 struct GNUNET_SIGNAL_Context *shc_chld;
300 printf ("Need arguments: gnunet-testbed-mpi-spawn <cmd> <cmd_args>");
303 if (GNUNET_OK != GNUNET_log_setup ("gnunet-testbed-spawn", NULL, NULL))
308 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
309 GNUNET_NO, GNUNET_NO)))
316 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
317 if (NULL == shc_chld)
319 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot install a signal handler\n");
322 argv2 = GNUNET_malloc (sizeof (char *) * argc);
323 for (cnt = 1; cnt < argc; cnt++)
324 argv2[cnt - 1] = argv[cnt];
325 GNUNET_SCHEDULER_run (run, NULL);
327 GNUNET_SIGNAL_handler_uninstall (shc_chld);
329 GNUNET_DISK_pipe_close (sigpipe);
330 GNUNET_free_non_null (fn);
331 if (GNUNET_OK != ret)