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 * Task to kill the child
61 static struct GNUNET_SCHEDULER_Task * terminate_task_id;
64 * Task to kill the child
66 static struct GNUNET_SCHEDULER_Task * child_death_task_id;
72 shutdown_task (void *cls)
74 if (0 != child_exit_code)
76 LOG (GNUNET_ERROR_TYPE_WARNING, "Child exited with error code: %lu\n",
78 ret = 128 + (int) child_exit_code;
84 if ((NULL != fn) && (0 != unlink (fn)))
86 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
93 terminate_task (void *cls)
97 GNUNET_assert (NULL != child);
99 GNUNET_SCHEDULER_add_shutdown (&terminate_task, NULL);
106 LOG (GNUNET_ERROR_TYPE_WARNING,
107 "%d more interrupts needed to send SIGKILL to the child\n",
112 GNUNET_break (0 == GNUNET_OS_process_kill (child, SIGKILL));
117 GNUNET_break (0 == GNUNET_OS_process_kill (child, GNUNET_TERM_SIG));
118 LOG (GNUNET_ERROR_TYPE_INFO, _("Waiting for child to exit.\n"));
123 * Task triggered whenever we receive a SIGCHLD (child
126 * @param cls closure, NULL if we need to self-restart
129 child_death_task (void *cls)
131 const struct GNUNET_DISK_FileHandle *pr;
133 const struct GNUNET_SCHEDULER_TaskContext *tc;
136 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
137 child_death_task_id = NULL;
138 tc = GNUNET_SCHEDULER_get_task_context ();
139 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
141 child_death_task_id =
142 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
143 pr, &child_death_task, NULL);
146 /* consume the signal */
147 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
148 LOG_DEBUG ("Child died\n");
149 GNUNET_SCHEDULER_cancel (terminate_task_id);
150 terminate_task_id = NULL;
151 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_status (child, &child_status,
153 GNUNET_OS_process_destroy (child);
155 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
160 destroy_hosts(struct GNUNET_TESTBED_Host **hosts, unsigned int nhosts)
164 GNUNET_assert (NULL != hosts);
165 for (host = 0; host < nhosts; host++)
166 if (NULL != hosts[host])
167 GNUNET_TESTBED_host_destroy (hosts[host]);
174 * The main scheduler run task
181 struct GNUNET_TESTBED_Host **hosts;
182 const struct GNUNET_CONFIGURATION_Handle *null_cfg;
188 null_cfg = GNUNET_CONFIGURATION_create ();
189 nhosts = GNUNET_TESTBED_hosts_load_from_loadleveler (null_cfg, &hosts);
196 hostname_len = GNUNET_OS_get_hostname_max_length ();
197 hostname = GNUNET_malloc (hostname_len);
198 if (0 != gethostname (hostname, hostname_len))
200 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot get hostname. Exiting\n");
201 GNUNET_free (hostname);
202 destroy_hosts (hosts, nhosts);
206 if (NULL == strstr (GNUNET_TESTBED_host_get_hostname (hosts[0]), hostname))
208 LOG_DEBUG ("Exiting as `%s' is not the lowest host\n", hostname);
209 GNUNET_free (hostname);
213 LOG_DEBUG ("Will be executing `%s' on host `%s'\n", argv2[0], hostname);
214 GNUNET_free (hostname);
215 destroy_hosts (hosts, nhosts);
216 tmpdir = getenv ("TMPDIR");
218 tmpdir = getenv ("TMP");
220 tmpdir = getenv ("TEMP");
223 (void) GNUNET_asprintf (&fn, "%s/gnunet-testbed-spawn.lock", tmpdir);
224 /* Open the unique file; we can create it then we can spawn the child process
226 fh = open (fn, O_CREAT | O_EXCL | O_CLOEXEC,
227 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
232 LOG_DEBUG ("Lock file already created by other process. Exiting\n");
236 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
240 /* Spawn the new process here */
241 LOG (GNUNET_ERROR_TYPE_INFO, _("Spawning process `%s'\n"), argv2[0]);
242 child = GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
249 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
254 GNUNET_SCHEDULER_add_shutdown (&terminate_task, NULL);
255 child_death_task_id =
256 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
257 GNUNET_DISK_pipe_handle (sigpipe,
258 GNUNET_DISK_PIPE_END_READ),
259 &child_death_task, NULL);
264 * Signal handler called for SIGCHLD.
267 sighandler_child_death ()
270 int old_errno = errno; /* back-up errno */
273 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
274 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
276 errno = old_errno; /* restore errno */
281 * Execution start point
284 main (int argc, char *argv[])
286 struct GNUNET_SIGNAL_Context *shc_chld;
292 printf ("Need arguments: gnunet-testbed-mpi-spawn <cmd> <cmd_args>");
295 if (GNUNET_OK != GNUNET_log_setup ("gnunet-testbed-spawn", NULL, NULL))
300 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
301 GNUNET_NO, GNUNET_NO)))
308 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
309 if (NULL == shc_chld)
311 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot install a signal handler\n");
314 argv2 = GNUNET_malloc (sizeof (char *) * argc);
315 for (cnt = 1; cnt < argc; cnt++)
316 argv2[cnt - 1] = argv[cnt];
317 GNUNET_SCHEDULER_run (run, NULL);
319 GNUNET_SIGNAL_handler_uninstall (shc_chld);
321 GNUNET_DISK_pipe_close (sigpipe);
322 GNUNET_free_non_null (fn);
323 if (GNUNET_OK != ret)