POLLHUP polling

From JookWiki
Revision as of 08:04, 27 October 2022 by Jookia (talk | contribs) (→‎Test code: Add test program)

This page is a work in progress

POSIX

https://pubs.opengroup.org/onlinepubs/9699919799/functions/select.html

- only lets you wait for read or write to be unblocked or exceptional

https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html

- give fds and in events such as POLLIN or POLLOUT

- get return events such as POLLIN or POLLOUT or POLLERR or POLLHUP

"The array's members are pollfd structures within which fd specifies an open file descriptor and events and revents are bitmasks constructed by OR'ing a combination of the following event flags:"

"POLLHUP - A device has been disconnected, or a pipe or FIFO has been closed by the last process that had it open for writing. ... This flag is only valid in the revents bitmask; it shall be ignored in the events member."

"In each pollfd structure, poll() shall clear the revents member, except that where the application requested a report on a condition by setting one of the bits of events listed above, poll() shall set the corresponding bit in revents if the requested condition is true. In addition, poll() shall set the POLLHUP, POLLERR, and POLLNVAL flag in revents if the condition is true, even if the application did not set the corresponding bit in events."

"If none of the defined events have occurred on any selected file descriptor, poll() shall wait at least timeout milliseconds for an event to occur on any of the selected file descriptors. If the value of timeout is 0, poll() shall return immediately. If the value of timeout is -1, poll() shall block until a requested event occurs or until the call is interrupted."

"The poll() function shall support regular files, terminal and pseudo-terminal devices, FIFOs, pipes, sockets and STREAMS-based files."

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/poll.h.html

"The <poll.h> header shall define the following symbolic constants, zero or more of which may be OR'ed together to form the events or revents members in the pollfd structure:"

https://groups.google.com/g/comp.unix.programmer/c/bNNadBIEpTo/m/G5gs1mqNhbIJ

- test suite and standards require registration or something

Test code

With that in mind, I wrote this quick program that polls for POLLHUP and nothing else:

/* compile and run with gcc test.c -oTEST && ./TEST */

#include <stdio.h>
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>

int main(void) {
  /* create a socket pair, one end for parent, one end for child */
  int sockets[2];
  int rc;
  rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
  /* fork in to to a parent in child */
  int proc = fork();
  if(proc == -1) { printf("fork err\n"); return 1; }
  if(proc != 0) {
    /* parent: poll for POLLHUP */
    rc = close(sockets[1]); /* close child end to avoid holding open */
    if(rc == -1) { printf("close err 1 \n"); return 1; }
    struct pollfd fd;
    fd.fd = sockets[0]; /* watch parent end */
    fd.events = 0;
    fd.revents = 0;
    rc = poll(&fd, 1, 10000);
    if(rc < 1) { printf("poll err\n"); return 1; }
    else if(!(fd.revents & POLLHUP)) { printf("no POLLHUP?\n"); return 1; }
    printf("got POLLHUP, all good\n");
    return 0;
  } else {
    /* child: close child socket end and quit */
    rc = close(sockets[1]); /* close child end */
    if(rc == -1) { printf("close err 3\n"); return 1; }
    return 0;
  }
}

It's a bit nasty but it gets the job done.

Linux

https://man7.org/linux/man-pages/man2/poll.2.html

"The field fd contains a file descriptor for an open file."

"The field events is an input parameter, a bit mask specifying the events the application is interested in for the file descriptor fd.  This field may be specified as zero, in which case the only events that can be returned in revents are POLLHUP, POLLERR, and POLLNVAL (see below)."

"POLLHUP - Hang up (only returned in revents; ignored in events). Note that when reading from a channel such as a pipe or a stream socket, this event merely indicates that the peer closed its end of the channel."

"If none of the events requested (and no error) has occurred for any of the file descriptors, then poll() blocks until one of the events occurs."

POLLRDHUP ??

Unix

- sysv

- sunos

BSDs

- netbsd

- freebsd

- openbsd

- illumos

- macos

Windows

wsapoll

Emulation

cygwin

minix

my dosbox code :(