small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[oweals/gnunet.git] / src / testbed / gnunet_testbed_mpi_spawn.c
1 #include "platform.h"
2 #include "gnunet_util_lib.h"
3 #include "gnunet_testbed_service.h"
4
5
6 /**
7  * Generic logging shorthand
8  */
9 #define LOG(kind,...)                           \
10   GNUNET_log (kind, __VA_ARGS__)
11
12 /**
13  * Debug logging shorthand
14  */
15 #define LOG_DEBUG(...)                          \
16   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
17
18 /**
19  * Global result
20  */
21 static int ret;
22
23 /**
24  * The child process we spawn
25  */
26 static struct GNUNET_OS_Process *child;
27
28 /**
29  * The arguments including the binary to spawn
30  */
31 static char **argv2;
32
33 /**
34  * Pipe used to communicate shutdown via signal.
35  */
36 static struct GNUNET_DISK_PipeHandle *sigpipe;
37
38 /**
39  * Filename of the unique file
40  */
41 static char *fn;
42
43 /**
44  * Handle to the unique file
45  */
46 static int fh;
47
48 /**
49  * The return code of the binary
50  */
51 static unsigned long child_exit_code;
52
53 /**
54  * The process status of the child
55  */
56 static enum GNUNET_OS_ProcessStatusType child_status;
57
58 /**
59  * The shutdown task
60  */
61 static struct GNUNET_SCHEDULER_Task * shutdown_task_id;
62
63 /**
64  * Task to kill the child
65  */
66 static struct GNUNET_SCHEDULER_Task * terminate_task_id;
67
68 /**
69  * Task to kill the child
70  */
71 static struct GNUNET_SCHEDULER_Task * child_death_task_id;
72
73 /**
74  * The shutdown task
75  */
76 static void
77 shutdown_task (void *cls)
78 {
79   shutdown_task_id = NULL;
80   if (0 != child_exit_code)
81   {
82     LOG (GNUNET_ERROR_TYPE_WARNING, "Child exited with error code: %lu\n",
83          child_exit_code);
84     ret = 128 + (int) child_exit_code;
85   }
86   if (0 != fh)
87   {
88     close (fh);
89   }
90   if ((NULL != fn) && (0 != unlink (fn)))
91   {
92     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
93     ret = GNUNET_SYSERR;
94   }
95 }
96
97
98 static void
99 terminate_task (void *cls)
100 {
101   static int hard_kill;
102
103   GNUNET_assert (NULL != child);
104   terminate_task_id =
105       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
106                                     &terminate_task, NULL);
107   if (0 != hard_kill)
108   {
109     switch (hard_kill)
110     {
111     case 1:
112     case 2:
113       LOG (GNUNET_ERROR_TYPE_WARNING,
114            "%d more interrupts needed to send SIGKILL to the child\n",
115            3 - hard_kill);
116       hard_kill++;
117       return;
118     case 3:
119       GNUNET_break (0 == GNUNET_OS_process_kill (child, SIGKILL));
120       return;
121     }
122   }
123   hard_kill++;
124   GNUNET_break (0 == GNUNET_OS_process_kill (child, GNUNET_TERM_SIG));
125   LOG (GNUNET_ERROR_TYPE_INFO, _("Waiting for child to exit.\n"));
126 }
127
128
129 /**
130  * Task triggered whenever we receive a SIGCHLD (child
131  * process died).
132  *
133  * @param cls closure, NULL if we need to self-restart
134  */
135 static void
136 child_death_task (void *cls)
137 {
138   const struct GNUNET_DISK_FileHandle *pr;
139   char c[16];
140   const struct GNUNET_SCHEDULER_TaskContext *tc;
141
142
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))
147   {
148     child_death_task_id =
149         GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
150                                         pr, &child_death_task, NULL);
151     return;
152   }
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,
159                                                         &child_exit_code));
160   GNUNET_OS_process_destroy (child);
161   child = NULL;
162   shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
163 }
164
165
166 static void
167 destroy_hosts(struct GNUNET_TESTBED_Host **hosts, unsigned int nhosts)
168 {
169   unsigned int host;
170
171   GNUNET_assert (NULL != hosts);
172   for (host = 0; host < nhosts; host++)
173     if (NULL != hosts[host])
174       GNUNET_TESTBED_host_destroy (hosts[host]);
175   GNUNET_free (hosts);
176   hosts = NULL;
177 }
178
179
180 /**
181  * The main scheduler run task
182  *
183  * @param cls NULL
184  */
185 static void
186 run (void *cls)
187 {
188   struct GNUNET_TESTBED_Host **hosts;
189   const struct GNUNET_CONFIGURATION_Handle *null_cfg;
190   char *tmpdir;
191   char *hostname;
192   size_t hostname_len;
193   unsigned int nhosts;
194
195   null_cfg = GNUNET_CONFIGURATION_create ();
196   nhosts = GNUNET_TESTBED_hosts_load_from_loadleveler (null_cfg, &hosts);
197   if (0 == nhosts)
198   {
199     GNUNET_break (0);
200     ret = GNUNET_SYSERR;
201     return;
202   }
203   hostname_len = GNUNET_OS_get_hostname_max_length ();
204   hostname = GNUNET_malloc (hostname_len);
205   if (0 != gethostname (hostname, hostname_len))
206   {
207     LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot get hostname.  Exiting\n");
208     GNUNET_free (hostname);
209     destroy_hosts (hosts, nhosts);
210     ret = GNUNET_SYSERR;
211     return;
212   }
213   if (NULL == strstr (GNUNET_TESTBED_host_get_hostname (hosts[0]), hostname))
214   {
215     LOG_DEBUG ("Exiting as `%s' is not the lowest host\n", hostname);
216     GNUNET_free (hostname);
217     ret = GNUNET_OK;
218     return;
219   }
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");
224   if (NULL == tmpdir)
225     tmpdir = getenv ("TMP");
226   if (NULL == tmpdir)
227     tmpdir = getenv ("TEMP");
228   if (NULL == tmpdir)
229     tmpdir = "/tmp";
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
232      else we exit */
233   fh = open (fn, O_CREAT | O_EXCL | O_CLOEXEC,
234              S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
235   if (-1 == fh)
236   {
237     if (EEXIST == errno)
238     {
239       LOG_DEBUG ("Lock file already created by other process.  Exiting\n");
240       ret = GNUNET_OK;
241       return;
242     }
243     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
244     ret = GNUNET_SYSERR;
245     return;
246   }
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,
250                                        NULL, NULL,
251                                        argv2[0], argv2);
252   if (NULL == child)
253   {
254     GNUNET_break (0);
255     ret = GNUNET_SYSERR;
256     shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
257     return;
258   }
259   ret = GNUNET_OK;
260   terminate_task_id =
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);
268 }
269
270
271 /**
272  * Signal handler called for SIGCHLD.
273  */
274 static void
275 sighandler_child_death ()
276 {
277   static char c;
278   int old_errno = errno;        /* back-up errno */
279
280   GNUNET_break (1 ==
281                 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
282                                         (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
283                                         &c, sizeof (c)));
284   errno = old_errno;            /* restore errno */
285 }
286
287
288 /**
289  * Execution start point
290  */
291 int
292 main (int argc, char *argv[])
293 {
294   struct GNUNET_SIGNAL_Context *shc_chld;
295   unsigned int cnt;
296
297   ret = -1;
298   if (argc < 2)
299   {
300     printf ("Need arguments: gnunet-testbed-mpi-spawn <cmd> <cmd_args>");
301     return 1;
302   }
303   if (GNUNET_OK != GNUNET_log_setup ("gnunet-testbed-spawn", NULL, NULL))
304   {
305     GNUNET_break (0);
306     return 1;
307   }
308   if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
309                                            GNUNET_NO, GNUNET_NO)))
310   {
311     GNUNET_break (0);
312     ret = GNUNET_SYSERR;
313     return 1;
314   }
315   shc_chld =
316       GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
317   if (NULL == shc_chld)
318   {
319     LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot install a signal handler\n");
320     return 1;
321   }
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);
326   GNUNET_free (argv2);
327   GNUNET_SIGNAL_handler_uninstall (shc_chld);
328   shc_chld = NULL;
329   GNUNET_DISK_pipe_close (sigpipe);
330   GNUNET_free_non_null (fn);
331   if (GNUNET_OK != ret)
332     return ret;
333   return 0;
334 }