GNUnet  0.10.x
gnunet_testbed_mpi_spawn.c
Go to the documentation of this file.
1 #include "platform.h"
2 #include "gnunet_util_lib.h"
4 
5 
9 #define LOG(kind, ...) \
10  GNUNET_log(kind, __VA_ARGS__)
11 
15 #define LOG_DEBUG(...) \
16  LOG(GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
17 
21 static int ret;
22 
26 static struct GNUNET_OS_Process *child;
27 
31 static char **argv2;
32 
37 
41 static char *fn;
42 
46 static int fh;
47 
51 static unsigned long child_exit_code;
52 
57 
62 
67 
71 static void
72 shutdown_task(void *cls)
73 {
74  if (0 != child_exit_code)
75  {
76  LOG(GNUNET_ERROR_TYPE_WARNING, "Child exited with error code: %lu\n",
78  ret = 128 + (int)child_exit_code;
79  }
80  if (0 != fh)
81  {
82  close(fh);
83  }
84  if ((NULL != fn) && (0 != unlink(fn)))
85  {
88  }
89 }
90 
91 
92 static void
93 terminate_task(void *cls)
94 {
95  static int hard_kill;
96 
97  GNUNET_assert(NULL != child);
98  terminate_task_id =
100  if (0 != hard_kill)
101  {
102  switch (hard_kill)
103  {
104  case 1:
105  case 2:
107  "%d more interrupts needed to send SIGKILL to the child\n",
108  3 - hard_kill);
109  hard_kill++;
110  return;
111 
112  case 3:
113  GNUNET_break(0 == GNUNET_OS_process_kill(child, SIGKILL));
114  return;
115  }
116  }
117  hard_kill++;
119  LOG(GNUNET_ERROR_TYPE_INFO, _("Waiting for child to exit.\n"));
120 }
121 
122 
129 static void
131 {
132  const struct GNUNET_DISK_FileHandle *pr;
133  char c[16];
134  const struct GNUNET_SCHEDULER_TaskContext *tc;
135 
136 
138  child_death_task_id = NULL;
141  {
142  child_death_task_id =
144  pr, &child_death_task, NULL);
145  return;
146  }
147  /* consume the signal */
148  GNUNET_break(0 < GNUNET_DISK_file_read(pr, &c, sizeof(c)));
149  LOG_DEBUG("Child died\n");
150  GNUNET_SCHEDULER_cancel(terminate_task_id);
151  terminate_task_id = NULL;
153  &child_exit_code));
155  child = NULL;
157 }
158 
159 
160 static void
161 destroy_hosts(struct GNUNET_TESTBED_Host **hosts, unsigned int nhosts)
162 {
163  unsigned int host;
164 
165  GNUNET_assert(NULL != hosts);
166  for (host = 0; host < nhosts; host++)
167  if (NULL != hosts[host])
168  GNUNET_TESTBED_host_destroy(hosts[host]);
169  GNUNET_free(hosts);
170  hosts = NULL;
171 }
172 
173 
179 static void
180 run(void *cls)
181 {
182  struct GNUNET_TESTBED_Host **hosts;
183  const struct GNUNET_CONFIGURATION_Handle *null_cfg;
184  char *tmpdir;
185  char *hostname;
186  size_t hostname_len;
187  unsigned int nhosts;
188 
189  null_cfg = GNUNET_CONFIGURATION_create();
190  nhosts = GNUNET_TESTBED_hosts_load_from_loadleveler(null_cfg, &hosts);
191  if (0 == nhosts)
192  {
193  GNUNET_break(0);
194  ret = GNUNET_SYSERR;
195  return;
196  }
197  hostname_len = GNUNET_OS_get_hostname_max_length();
198  hostname = GNUNET_malloc(hostname_len);
199  if (0 != gethostname(hostname, hostname_len))
200  {
201  LOG(GNUNET_ERROR_TYPE_ERROR, "Cannot get hostname. Exiting\n");
202  GNUNET_free(hostname);
203  destroy_hosts(hosts, nhosts);
204  ret = GNUNET_SYSERR;
205  return;
206  }
207  if (NULL == strstr(GNUNET_TESTBED_host_get_hostname(hosts[0]), hostname))
208  {
209  LOG_DEBUG("Exiting as `%s' is not the lowest host\n", hostname);
210  GNUNET_free(hostname);
211  ret = GNUNET_OK;
212  return;
213  }
214  LOG_DEBUG("Will be executing `%s' on host `%s'\n", argv2[0], hostname);
215  GNUNET_free(hostname);
216  destroy_hosts(hosts, nhosts);
217  tmpdir = getenv("TMPDIR");
218  if (NULL == tmpdir)
219  tmpdir = getenv("TMP");
220  if (NULL == tmpdir)
221  tmpdir = getenv("TEMP");
222  if (NULL == tmpdir)
223  tmpdir = "/tmp";
224  (void)GNUNET_asprintf(&fn, "%s/gnunet-testbed-spawn.lock", tmpdir);
225  /* Open the unique file; we can create it then we can spawn the child process
226  else we exit */
227  fh = open(fn, O_CREAT | O_EXCL | O_CLOEXEC,
228  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
229  if (-1 == fh)
230  {
231  if (EEXIST == errno)
232  {
233  LOG_DEBUG("Lock file already created by other process. Exiting\n");
234  ret = GNUNET_OK;
235  return;
236  }
238  ret = GNUNET_SYSERR;
239  return;
240  }
241  /* Spawn the new process here */
242  LOG(GNUNET_ERROR_TYPE_INFO, _("Spawning process `%s'\n"), argv2[0]);
244  NULL, NULL,
245  argv2[0], argv2);
246  if (NULL == child)
247  {
248  GNUNET_break(0);
249  ret = GNUNET_SYSERR;
251  return;
252  }
253  ret = GNUNET_OK;
254  terminate_task_id =
256  child_death_task_id =
258  GNUNET_DISK_pipe_handle(sigpipe,
260  &child_death_task, NULL);
261 }
262 
263 
267 static void
269 {
270  static char c;
271  int old_errno = errno; /* back-up errno */
272 
273  GNUNET_break(1 ==
275  (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
276  &c, sizeof(c)));
277  errno = old_errno; /* restore errno */
278 }
279 
280 
284 int
285 main(int argc, char *argv[])
286 {
287  struct GNUNET_SIGNAL_Context *shc_chld;
288  unsigned int cnt;
289 
290  ret = -1;
291  if (argc < 2)
292  {
293  printf("Need arguments: gnunet-testbed-mpi-spawn <cmd> <cmd_args>");
294  return 1;
295  }
296  if (GNUNET_OK != GNUNET_log_setup("gnunet-testbed-spawn", NULL, NULL))
297  {
298  GNUNET_break(0);
299  return 1;
300  }
301  if (NULL == (sigpipe = GNUNET_DISK_pipe(GNUNET_NO, GNUNET_NO,
302  GNUNET_NO, GNUNET_NO)))
303  {
304  GNUNET_break(0);
305  ret = GNUNET_SYSERR;
306  return 1;
307  }
308  shc_chld =
310  if (NULL == shc_chld)
311  {
312  LOG(GNUNET_ERROR_TYPE_ERROR, "Cannot install a signal handler\n");
313  return 1;
314  }
315  argv2 = GNUNET_malloc(sizeof(char *) * argc);
316  for (cnt = 1; cnt < argc; cnt++)
317  argv2[cnt - 1] = argv[cnt];
318  GNUNET_SCHEDULER_run(run, NULL);
321  shc_chld = NULL;
322  GNUNET_DISK_pipe_close(sigpipe);
324  if (GNUNET_OK != ret)
325  return ret;
326  return 0;
327 }
const struct GNUNET_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
Definition: scheduler.c:737
static void terminate_task(void *cls)
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:286
#define GNUNET_SIGCHLD
Definition: platform.h:42
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:729
int GNUNET_OS_process_status(struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Retrieve the status of a process, waiting on it if dead.
Definition: os_priority.c:963
The reading socket is ready.
Context information passed to each scheduler task.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:410
void GNUNET_SCHEDULER_run(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Initialize and run scheduler.
Definition: scheduler.c:707
#define GNUNET_NO
Definition: gnunet_common.h:78
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1615
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
void GNUNET_SIGNAL_handler_uninstall(struct GNUNET_SIGNAL_Context *ctx)
Uninstall a previously installed signal hander.
Definition: signal.c:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
Opaque handle to a host running experiments managed by the testing framework.
enum GNUNET_SCHEDULER_Reason reason
Reason why the task is run now.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
const struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd n)
Get the handle to a particular pipe end.
Definition: disk.c:1744
static void destroy_hosts(struct GNUNET_TESTBED_Host **hosts, unsigned int nhosts)
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static unsigned long child_exit_code
The return code of the binary.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
Use this option to have all of the standard streams (stdin, stdout and stderror) be inherited...
#define LOG_DEBUG(...)
Debug logging shorthand.
#define GNUNET_OS_get_hostname_max_length()
Get maximum string length returned by gethostname()
static char * fn
Filename of the unique file.
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:817
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
GNUNET_OS_ProcessStatusType
Process status types.
static enum GNUNET_OS_ProcessStatusType child_status
The process status of the child.
struct GNUNET_OS_Process * GNUNET_OS_start_process_vap(int pipe_control, enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:607
int main(int argc, char *argv[])
Execution start point.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
The writing-end of a pipe.
The reading-end of a pipe.
static int fh
Handle to the unique file.
static char ** argv2
The arguments including the binary to spawn.
static struct GNUNET_SCHEDULER_Task * terminate_task_id
Task to kill the child.
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(int blocking_read, int blocking_write, int inherit_read, int inherit_write)
Creates an interprocess channel.
Definition: disk.c:1518
static struct GNUNET_DISK_PipeHandle * sigpipe
Pipe used to communicate shutdown via signal.
struct GNUNET_SIGNAL_Context * GNUNET_SIGNAL_handler_install(int signal, GNUNET_SIGNAL_Handler handler)
Install a signal handler that will be run if the given signal is received.
Definition: signal.c:50
void GNUNET_TESTBED_host_destroy(struct GNUNET_TESTBED_Host *host)
Destroy a host handle.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static int ret
Global result.
char * getenv()
int GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1706
static struct GNUNET_SCHEDULER_Task * child_death_task_id
Task to kill the child.
const char * GNUNET_TESTBED_host_get_hostname(const struct GNUNET_TESTBED_Host *host)
Obtain the host&#39;s hostname.
static char * hostname
Our hostname; we give this to all the peers we start.
static struct GNUNET_OS_Process * child
The child process we spawn.
static void shutdown_task(void *cls)
The shutdown task.
configuration data
Definition: configuration.c:83
static void run(void *cls)
The main scheduler run task.
unsigned int GNUNET_TESTBED_hosts_load_from_loadleveler(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_TESTBED_Host ***hosts)
Loads the set of host allocated by the LoadLeveler Job Scheduler.
Entry in list of pending tasks.
Definition: scheduler.c:131
#define LOG(kind,...)
Generic logging shorthand.
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
static void sighandler_child_death()
Signal handler called for SIGCHLD.
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:242
Handle used to access files (and pipes).
Handle used to manage a pipe.
Definition: disk.c:68
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:224
static void child_death_task(void *cls)
Task triggered whenever we receive a SIGCHLD (child process died).
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956