poll(2) returns 0 with -1 timeout when .events = POLLHUP and fd is readable

Originator:shea
Number:rdar://37550628 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 POLLHUP (see rdar://37537852 for why this is needed) and the associated fd is readable but has not received hangup, poll will return with 0 if timeout is -1 and no other fds are ready.

See the following test case:

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

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

int main(int argc, char ** argv) {
  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]));
  }
  close(pipe_fds[0]);
  char c = 'c';
  write(pipe_fds[1], &c, 1);
  sleep(1);
  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;
  }
  return WEXITSTATUS(status);
}

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!