gnunet_testbed_mpi_spawn.c 7.5 KB

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