struct msg_fds {
int pfd[2];
int cfd[2];
};
#define CLOSEFD(fd) do { \
if (fd != -1) { \
close(fd); \
fd = -1; \
} \
} while ( 0)
static int __used
msg_open(struct msg_fds *fds)
{
if (pipe(fds->pfd) == -1)
return -1;
if (pipe(fds->cfd) == -1) {
close(fds->pfd[0]);
close(fds->pfd[1]);
return -1;
}
return 0;
}
static void __used
msg_close(struct msg_fds *fds)
{
CLOSEFD(fds->pfd[0]);
CLOSEFD(fds->pfd[1]);
CLOSEFD(fds->cfd[0]);
CLOSEFD(fds->cfd[1]);
}
static int __used
msg_write_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
{
ssize_t rv;
CLOSEFD(fds->cfd[1]);
CLOSEFD(fds->pfd[0]);
rv = write(fds->pfd[1], msg, len);
if (rv != (ssize_t)len)
return 1;
rv = read(fds->cfd[0], msg, len);
if (rv != (ssize_t)len)
return 1;
return 0;
}
static int __used
msg_write_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
{
ssize_t rv;
CLOSEFD(fds->pfd[1]);
CLOSEFD(fds->cfd[0]);
rv = write(fds->cfd[1], msg, len);
if (rv != (ssize_t)len)
return 1;
rv = read(fds->pfd[0], msg, len);
if (rv != (ssize_t)len)
return 1;
return 0;
}
static int __used
msg_read_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
{
ssize_t rv;
CLOSEFD(fds->pfd[1]);
CLOSEFD(fds->cfd[0]);
rv = read(fds->pfd[0], msg, len);
if (rv != (ssize_t)len)
return 1;
rv = write(fds->cfd[1], msg, len);
if (rv != (ssize_t)len)
return 1;
return 0;
}
static int __used
msg_read_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
{
ssize_t rv;
CLOSEFD(fds->cfd[1]);
CLOSEFD(fds->pfd[0]);
rv = read(fds->cfd[0], msg, len);
if (rv != (ssize_t)len)
return 1;
rv = write(fds->pfd[1], msg, len);
if (rv != (ssize_t)len)
return 1;
return 0;
}
#define PARENT_TO_CHILD(info, fds, msg) \
SYSCALL_REQUIRE(msg_write_child(info " to child " # fds, &fds, &msg, \
sizeof(msg)) == 0)
#define CHILD_FROM_PARENT(info, fds, msg) \
FORKEE_ASSERT(msg_read_parent(info " from parent " # fds, &fds, &msg, \
sizeof(msg)) == 0)
#define CHILD_TO_PARENT(info, fds, msg) \
FORKEE_ASSERT(msg_write_parent(info " to parent " # fds, &fds, &msg, \
sizeof(msg)) == 0)
#define PARENT_FROM_CHILD(info, fds, msg) \
SYSCALL_REQUIRE(msg_read_child(info " from parent " # fds, &fds, &msg, \
sizeof(msg)) == 0)