finish off with MPI before spawning
[oweals/gnunet.git] / src / testbed / gnunet_testbed_mpi_spawn.c
1 #include "platform.h"
2 #include "gnunet_util_lib.h"
3 #include <mpi.h>
4
5 /**
6  * Generic logging shorthand
7  */
8 #define LOG(kind,...)                           \
9   fprintf (stderr, __VA_ARGS__)
10
11 /**
12  * Debug logging shorthand
13  */
14 #define LOG_DEBUG(...)                          \
15   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
16
17 /**
18  * Global result
19  */
20 static int ret;
21
22
23 /**
24  * Execution start point
25  */
26 int
27 main (int argc, char *argv[])
28 {
29   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
30     GNUNET_GETOPT_OPTION_END
31   };
32   struct GNUNET_OS_Process *proc;
33   char **argv2;
34   unsigned long code;
35   enum GNUNET_OS_ProcessStatusType proc_status;
36   int rank;
37   int chstat;
38   unsigned int host;
39   unsigned int cnt;
40   
41   ret = -1;
42   if (argc < 2)
43   {
44     printf ("Need arguments: gnunet-testbed-mpi-spawn <cmd> <cmd_args>");
45     return 1;
46   }
47   if (MPI_SUCCESS != MPI_Init (&argc, &argv))
48   {
49     GNUNET_break (0);
50     return 2;
51   }
52   if (MPI_SUCCESS != MPI_Comm_rank (MPI_COMM_WORLD, &rank))
53   {
54     GNUNET_break (0);
55     ret = 3;
56     (void) MPI_Finalize ();
57     goto end;
58   }
59   if (0 != rank)
60   {
61     ret = 0;
62     (void) MPI_Finalize ();
63     goto end;
64   }
65   (void) MPI_Finalize ();
66   PRINTF ("Spawning process\n");
67   argv2 = GNUNET_malloc (sizeof (char *) * argc);
68   for (cnt = 1; cnt < argc; cnt++)
69     argv2[cnt - 1] = argv[cnt];
70   proc =
71       GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
72                                    NULL, argv2[0], argv2);
73   if (NULL == proc)
74   {
75     LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot exec\n");
76     ret = 5;
77     goto end;
78   }
79   do
80   {
81     (void) sleep (1);
82     chstat = GNUNET_OS_process_status (proc, &proc_status, &code);
83   }
84   while (GNUNET_NO == chstat);
85   if (GNUNET_OK != chstat)
86   { 
87     ret = 6;
88     goto end;
89   }
90   if (0 != code)
91   {
92     LOG (GNUNET_ERROR_TYPE_WARNING, "Child terminated abnormally\n");
93     ret = 50 + (int) code;
94     goto end;
95   }
96   ret = 0;
97   
98  end:  
99   if (0 != ret)
100     LOG (GNUNET_ERROR_TYPE_ERROR, "Something went wrong. Error: %d\n", ret);
101   return ret;
102 }