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;
59 * how many IP addresses are currently assigned to us
61 static unsigned int num_addrs;
66 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
69 * Task to kill the child
71 static GNUNET_SCHEDULER_TaskIdentifier terminate_task_id;
74 * Task to kill the child
76 static GNUNET_SCHEDULER_TaskIdentifier child_death_task_id;
82 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
85 if (0 != child_exit_code)
87 LOG (GNUNET_ERROR_TYPE_WARNING, "Child exited with error code: %lu\n",
89 ret = 128 + (int) child_exit_code;
95 if ((NULL != fn) && (0 != unlink (fn)))
97 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
104 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 static int hard_kill;
108 GNUNET_assert (NULL != child);
110 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
111 &terminate_task, NULL);
118 LOG (GNUNET_ERROR_TYPE_WARNING,
119 "%d more interrupts needed to send SIGKILL to the child\n",
124 GNUNET_break (0 == GNUNET_OS_process_kill (child, SIGKILL));
129 GNUNET_break (0 == GNUNET_OS_process_kill (child, SIGTERM));
130 LOG (GNUNET_ERROR_TYPE_INFO, _("Waiting for child to exit.\n"));
135 * Task triggered whenever we receive a SIGCHLD (child
138 * @param cls closure, NULL if we need to self-restart
142 child_death_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 const struct GNUNET_DISK_FileHandle *pr;
147 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
148 child_death_task_id = GNUNET_SCHEDULER_NO_TASK;
149 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
151 child_death_task_id =
152 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
153 pr, &child_death_task, NULL);
156 /* consume the signal */
157 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
158 LOG_DEBUG ("Child died\n");
159 GNUNET_SCHEDULER_cancel (terminate_task_id);
160 terminate_task_id = GNUNET_SCHEDULER_NO_TASK;
161 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_status (child, &child_status,
163 GNUNET_OS_process_destroy (child);
165 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
170 destroy_hosts(struct GNUNET_TESTBED_Host **hosts, unsigned int nhosts)
174 GNUNET_assert (NULL != hosts);
175 for (host = 0; host < nhosts; host++)
176 if (NULL != hosts[host])
177 GNUNET_TESTBED_host_destroy (hosts[host]);
184 * The main scheduler run task
187 * @param tc scheduler task context
190 run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 struct GNUNET_TESTBED_Host **hosts;
193 const struct GNUNET_CONFIGURATION_Handle *null_cfg;
205 null_cfg = GNUNET_CONFIGURATION_create ();
206 nhosts = GNUNET_TESTBED_hosts_load_from_loadleveler (null_cfg, &hosts);
213 hostname_len = GNUNET_OS_get_hostname_max_length ();
214 hostname = GNUNET_malloc (hostname_len);
215 if (0 != gethostname (hostname, hostname_len))
217 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot get hostname. Exiting\n");
218 GNUNET_free (hostname);
219 destroy_hosts (hosts, nhosts);
223 if (NULL == strstr (GNUNET_TESTBED_host_get_hostname (hosts[0]), hostname))
225 LOG_DEBUG ("Exiting as we are not the lowest host\n");
226 GNUNET_free (hostname);
230 GNUNET_free (hostname);
231 destroy_hosts (hosts, nhosts);
232 tmpdir = getenv ("TMPDIR");
234 tmpdir = getenv ("TMP");
236 tmpdir = getenv ("TEMP");
239 (void) GNUNET_asprintf (&fn, "%s/gnunet-testbed-spawn.lock", tmpdir);
240 /* Open the unique file; we can create it then we can spawn the child process
242 fh = open (fn, O_CREAT | O_EXCL | O_CLOEXEC,
243 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
248 LOG_DEBUG ("Lock file already created by other process. Exiting\n");
252 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
256 /* Spawn the new process here */
257 LOG (GNUNET_ERROR_TYPE_INFO, _("Spawning process `%s'\n"), argv2[0]);
258 child = GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
265 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
270 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
271 &terminate_task, NULL);
272 child_death_task_id =
273 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
274 GNUNET_DISK_pipe_handle (sigpipe,
275 GNUNET_DISK_PIPE_END_READ),
276 &child_death_task, NULL);
281 * Signal handler called for SIGCHLD.
284 sighandler_child_death ()
287 int old_errno = errno; /* back-up errno */
290 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
291 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
293 errno = old_errno; /* restore errno */
298 * Execution start point
301 main (int argc, char *argv[])
303 struct GNUNET_SIGNAL_Context *shc_chld;
309 printf ("Need arguments: gnunet-testbed-mpi-spawn <cmd> <cmd_args>");
312 if (GNUNET_OK != GNUNET_log_setup ("gnunet-testbed-spawn", NULL, NULL))
317 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
318 GNUNET_NO, GNUNET_NO)))
325 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
326 if (NULL == shc_chld)
328 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot install a signal handler\n");
331 argv2 = GNUNET_malloc (sizeof (char *) * argc);
332 for (cnt = 1; cnt < argc; cnt++)
333 argv2[cnt - 1] = argv[cnt];
334 GNUNET_SCHEDULER_run (run, NULL);
336 GNUNET_SIGNAL_handler_uninstall (shc_chld);
338 GNUNET_DISK_pipe_close (sigpipe);
339 GNUNET_free_non_null (fn);
340 if (GNUNET_OK != ret)