poll(2) doesn't detect POLLHUP when events is 0

Originator:shea
Number:rdar://37537852 Date Originated:February 14 2018
Status:Open Resolved:
Product:macOS + SDK Product Version:
Classification:Bug Reproducible:Yes
 
If the .events field of a struct pollfd is 0, poll(2) doesn't detect POLLHUP on that fd.

Test case demonstrating the bug:

#include <sys/types.h>
#include <unistd.h>
#include <poll.h>
#include <stdio.h>
#include <sys/wait.h>

int monitor_process(int fd, short events) {
  struct pollfd spec = { .fd = fd, .events = events };
  int res = poll(&spec, 1, 5 /* seconds */ * 1000 /* milliseconds/second */);
  switch (res) {
  case -1:
    perror("waiting for events");
    return 1;
  case 0:
    fprintf(stderr, "poll timed out\n");
    return 1;
  default:
    return 0;
  }
}

int go(short events) {
  int pipe_fds[2];
  if (pipe(pipe_fds) == -1) {
    perror("making a pipe");
    return 1;
  }

  pid_t child = fork();
  switch (child) {
  case -1:
    perror("forking");
    return 1;
  case 0:
    close(pipe_fds[1]);
    _exit(monitor_process(pipe_fds[0], events));
  }
  close(pipe_fds[0]);
  close(pipe_fds[1]);
  int status;
  waitpid(child, &status, 0);
  if (WIFSIGNALED(status)) {
    fprintf(stderr, "child process died with signal: %d", WTERMSIG(status));
    return 1;
  }
  int ret = WEXITSTATUS(status);
  if (ret != 0)
    fprintf(stderr, "child process failed with status %d, POLLHUP %s specified\n", ret, events & POLLHUP ? "is" : "is not");
  return WEXITSTATUS(status);
}

int main(int argc, char ** argv) {
  int res0 = go(POLLHUP);
  int res1 = go(0);
  return res1 + res0;
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!