-fix
[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     goto finalize;
57   }
58   if (0 != rank)
59   {
60     ret = 0;
61     goto finalize;
62   }
63   PRINTF ("Spawning process\n");
64   argv2 = GNUNET_malloc (sizeof (char *) * argc);
65   for (cnt = 1; cnt < argc; cnt++)
66     argv2[cnt - 1] = argv[cnt];
67   proc =
68       GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
69                                    NULL, argv2[0], argv2);
70   if (NULL == proc)
71   {
72     LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot exec\n");
73     ret = 5;
74     goto finalize;
75   }
76   do
77   {
78     (void) sleep (1);
79     chstat = GNUNET_OS_process_status (proc, &proc_status, &code);
80   }
81   while (GNUNET_NO == chstat);
82   if (GNUNET_OK != chstat)
83   { 
84     ret = 6;
85     goto finalize;
86   }
87   if (0 != code)
88   {
89     LOG (GNUNET_ERROR_TYPE_WARNING, "Child terminated abnormally\n");
90     ret = 50 + (int) code;
91     goto finalize;
92   }
93   ret = 0;
94   
95  finalize:
96   (void) MPI_Finalize ();
97   if (0 != ret)
98     LOG (GNUNET_ERROR_TYPE_ERROR, "Something went wrong. Error: %d\n", ret);
99   return ret;
100 }