aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/gnunet_mpi_test.c
blob: 9229675ce0e23a0bc6e53a6b2d991df7337c13cf (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
#include "platform.h"
#include "gnunet_util_lib.h"

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

int main (int argc, char *argv[])
{
  char *msg;
  char *filename;
  pid_t pid;
  pid_t ppid;
  int rank;
  int msg_size;
  int ret;

  ret = GNUNET_SYSERR;
  rank = 0;
  pid = getpid();
  (void) GNUNET_asprintf (&filename, "%d-%d.mpiout", (int) pid, rank);
  msg_size = GNUNET_asprintf (&msg, "My pid is: %d\n", pid);
  printf ("%s", msg);
  if (msg_size == GNUNET_DISK_fn_write (filename,
                                        msg, msg_size,
                                        GNUNET_DISK_PERM_USER_READ
                                        | GNUNET_DISK_PERM_GROUP_READ
                                        | GNUNET_DISK_PERM_USER_WRITE
                                        | GNUNET_DISK_PERM_GROUP_WRITE))
    ret = GNUNET_OK;
  GNUNET_free (filename);
  GNUNET_free (msg);
  if (GNUNET_OK != ret)
    goto finalize;

  ret = GNUNET_SYSERR;
  ppid = pid;
  pid = fork ();
  if (-1 == pid)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
    goto finalize;
  }
  if (0 == pid)
  {
    /* child code */
    pid = getpid ();
    (void) GNUNET_asprintf (&filename, "%d-%d.mpiout", (int) pid, rank);
    msg_size = GNUNET_asprintf (&msg, "Child of %d\n", (int) ppid);
    printf ("%s", msg);
    if (msg_size == GNUNET_DISK_fn_write (filename,
                                        msg, msg_size,
                                        GNUNET_DISK_PERM_USER_READ
                                        | GNUNET_DISK_PERM_GROUP_READ
                                        | GNUNET_DISK_PERM_USER_WRITE
                                        | GNUNET_DISK_PERM_GROUP_WRITE))
    ret = GNUNET_OK;
    GNUNET_free (filename);
    GNUNET_free (msg);
    return (GNUNET_OK == ret) ? 0 : 1;
  }
  else {
    int status;
    int childpid;

    childpid = waitpid (pid, &status, 0);
    if (childpid != pid)
    {
      GNUNET_break (0);
      goto finalize;
    }
    if (!WIFEXITED (status))
    {
      GNUNET_break (0);
      goto finalize;
    }
    if (0 != WEXITSTATUS (status))
    {
      GNUNET_break (0);
    }
  }
  ret = GNUNET_OK;

 finalize:
  return (GNUNET_OK == ret) ? 0 : 1;
}