Skip to content

Commit

Permalink
[LibOS] Fix setting of O_NONBLOCK flag at the write end of pipe
Browse files Browse the repository at this point in the history
Previously in Gramine we didn't save the `O_NONBLOCK` flag for the write
end of the pipe. We only set the `O_NONBLOCK` flag for the PAL handle
but this wasn't propagated to the LibOS (shim) handle when creating
pipe. This commit fixes the corner case. Also, `pipe_nonblocking`
regression test is updated to test this corner case.

Signed-off-by: Vijay Dhanraj <vijay.dhanraj@intel.com>
  • Loading branch information
vijaydhanraj authored and dimakuv committed Sep 14, 2021
1 parent 74420be commit e50c01b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion LibOS/shim/src/sys/shim_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ static int create_pipes(struct shim_handle* srv, struct shim_handle* cli, int fl
srv->pal_handle = hdl1;

if (flags & O_NONBLOCK) {
/* `cli` - `hdl2` - has this flag already set by the call to `DkStreamOpen`. */
ret = set_handle_nonblocking(cli, /*on=*/true);
if (ret < 0)
goto out;

ret = set_handle_nonblocking(srv, /*on=*/true);
if (ret < 0) {
/* Restore original handle, if any. */
Expand Down
36 changes: 36 additions & 0 deletions LibOS/shim/test/regression/pipe_nonblocking.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,42 @@ int main(void) {
err(1, "pipe2");
}

/* Verify both ends of the pipe provide same flags. */
int flags_wr = fcntl(p[1], F_GETFL);
if (flags_wr < 0)
err(1, "fcntl(<write end of pipe>, F_GETFL)");

int flags_rd = fcntl(p[0], F_GETFL);
if (flags_rd < 0)
err(1, "fcntl(<read end of pipe>, F_GETFL)");

/* Ensure O_NONBLOCK flag is properly set on both ends of pipe. */
if (!(flags_wr & O_NONBLOCK) || !(flags_rd & O_NONBLOCK))
errx(1, "Expected O_NONBLOCK flag to be set on both ends of pipe but got flags_wr=0x%x, "
"flags_rd=0x%x", flags_wr, flags_rd);

flags_rd = flags_rd & ~O_ACCMODE;
flags_wr = flags_wr & ~O_ACCMODE;

if (flags_wr != flags_rd)
errx(1, "`F_GETFL` flags mismatch: flags_wr=0x%x and flags_rd=0x%x", flags_wr, flags_rd);

flags_wr = fcntl(p[1], F_GETFD);
if (flags_wr < 0)
err(1, "fcntl(<write end of pipe>, F_GETFD)");

flags_rd = fcntl(p[0], F_GETFD);
if (flags_rd < 0)
err(1, "fcntl(<read end of pipe>, F_GETFD)");

/* Ensure O_CLOEXEC flag is properly set on both ends of pipe. */
if (!(flags_wr & FD_CLOEXEC) || !(flags_rd & FD_CLOEXEC))
errx(1, "Expected O_CLOEXEC flag to be set on both ends of pipe but got flags_wr=0x%x, "
"flags_rd=0x%x", flags_wr, flags_rd);

if (flags_wr != flags_rd)
errx(1, "`F_GETFD` flags mismatch: flags_wr=0x%x and flags_rd=0x%x", flags_wr, flags_rd);

ssize_t ret = write(p[1], "a", 1);
if (ret < 0) {
err(1, "write");
Expand Down

0 comments on commit e50c01b

Please sign in to comment.