aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/gnunet_testbed_mpi_spawn.c
blob: 2ff1972a6037179710687fa9acc1df700bb53c26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_resolver_service.h"
#include "gnunet_testbed_service.h"
#include <mpi.h>

/**
 * Generic logging shorthand
 */
#define LOG(kind,...)                                           \
  GNUNET_log_from (kind, "gnunet-mpi-test", __VA_ARGS__)

/**
 * Debug logging shorthand
 */
#define LOG_DEBUG(...)                          \
  LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)

/**
 * Timeout for resolving IPs
 */
#define RESOLVE_TIMEOUT                         \
  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)

/**
 * Global result
 */
static int ret;

/**
 * The host list
 */
static struct GNUNET_TESTBED_Host **hosts;

/**
 * Number of hosts in the host list
 */
static unsigned int nhosts;

/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param config configuration
 */
static void
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *config)
{
  struct GNUNET_OS_Process *proc;
  unsigned long code;
  enum GNUNET_OS_ProcessStatusType proc_status;
  int rank;
  unsigned int host;
  
  if (MPI_SUCCESS != MPI_Comm_rank (MPI_COMM_WORLD, &rank))
  {
    GNUNET_break (0);
    return;
  }
  if (0 != rank)
  {
    ret = GNUNET_OK;
    return;
  }
  PRINTF ("Spawning process\n");
  proc =
      GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
                                   NULL, args[0], args);
  if (NULL == proc)
  {
    printf ("Cannot exec\n");
    return;
  }
  do
  {
    (void) sleep (1);
    ret = GNUNET_OS_process_status (proc, &proc_status, &code);
  }
  while (GNUNET_NO == ret);
  GNUNET_assert (GNUNET_NO != ret);
  if (GNUNET_OK == ret)
  {
    if (0 != code)
    {
      LOG (GNUNET_ERROR_TYPE_WARNING, "Child terminated abnormally\n");
      ret = GNUNET_SYSERR;
      GNUNET_break (0);
      return;
    }
  }
  else
  {
    ret = GNUNET_SYSERR;
    GNUNET_break (0);
    return;
  }
  if (0 == (nhosts = GNUNET_TESTBED_hosts_load_from_loadleveler (config, &hosts)))
  {
    GNUNET_break (0);
    ret = GNUNET_SYSERR;
    return;
  }
  for (host = 0; host < nhosts; host++)
    GNUNET_TESTBED_host_destroy (hosts[host]);
  GNUNET_free (hosts);
  hosts = NULL;
  ret = GNUNET_OK;
}


/**
 * Execution start point
 */
int
main (int argc, char *argv[])
{
  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };
  unsigned int host;
  int rres;
  
  ret = GNUNET_SYSERR;
  if (argc < 2)
  {
    printf ("Need arguments: gnunet-testbed-mpi-spawn <cmd> <cmd_args>");
    return 1;
  }
  if (MPI_SUCCESS != MPI_Init (&argc, &argv))
  {
    GNUNET_break (0);
    return 1;
  }
  rres =
      GNUNET_PROGRAM_run (argc, argv,
                          "gnunet-testbed-mpi-spawn <cmd> <cmd_args>",
                          _("Spawns cmd after starting my the MPI run-time"),
                          options, &run, NULL);
  (void) MPI_Finalize ();
  if ((GNUNET_OK == rres) && (GNUNET_OK == ret))
    return 0;
  printf ("Something went wrong\n");
  return 1;
}