在 HP-UX 中立即轮询 FIFO returns

poll on FIFO returns immediately in HP-UX

我正在尝试在各种平台上使用 FIFO 上的 poll(2),但是(与 Linux、Solaris、AIX 相比),HP-UX 的行为与其他平台有些不同。

下面的代码创建一个 FIFO(命名管道)并用 3 秒超时轮询它。由于此代码不写入 FIFO,我预计轮询结果为 0。

#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <unistd.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#if defined(CLOCK_MONOTONIC)
#define MY_CLOCK_TIME CLOCK_MONOTONIC
#elif defined(CLOCK_REALTIME)
#define MY_CLOCK_TIME CLOCK_REALTIME
#else
#error neither CLOCK_REALTIME nor CLOCK_MONOTONIC defined.
#endif

#define HANDLE_ERROR(expr, msg) \
    do                          \
    {                           \
        if ((expr) < 0)         \
        {                       \
            perror(msg);        \
            exit(EXIT_FAILURE); \
        }                       \
    } while (0);

namespace {
const char pipename[] = "pipe";

template <size_t N>
struct type_of_size
{
    typedef char type[N];
};

template <typename T, size_t Size>
typename type_of_size<Size>::type& sizeof_array_helper(T (&)[Size]);

#define SIZE_OF_ARR(pArray) sizeof(sizeof_array_helper(pArray))

int my_unlink(const char* name)
{
    int result = unlink(name);
    if (result < 0)
    {
        if (errno == ENOENT)
        {
            result = 0;
            errno = 0;
        }
    }
    return result;
}

double timediff(const struct timespec& t1, const struct timespec& t2)
{
    return static_cast<double>(static_cast<int64_t>(
               (t1.tv_sec - t2.tv_sec) * 1000000000LL + (t1.tv_nsec - t2.tv_nsec))) /
           1000000000LL;
}
} // namespace

int main()
{
    HANDLE_ERROR(my_unlink(pipename), "unlink");
    HANDLE_ERROR(mkfifo(pipename, 0600), "mkfifo");

    int result = 0;
    int poll_timeout = 3000; // 3s
    struct timespec before;
    memset(&before, 0, sizeof(struct timespec));
    struct timespec after;
    memset(&after, 0, sizeof(struct timespec));

    int fd = open(pipename, O_RDONLY | O_NONBLOCK);
    HANDLE_ERROR((fd < 0), "open");
    pollfd fds[1];
    memset(fds, 0, sizeof(pollfd) * SIZE_OF_ARR(fds));
    fds[0].fd = fd;
    fds[0].events = POLLIN;

    HANDLE_ERROR(clock_gettime(MY_CLOCK_TIME, &before), "clock_gettime");

#ifdef USE_FAKE_OPEN
    int fakeopen_fd = open(pipename, O_WRONLY | O_NONBLOCK);
    HANDLE_ERROR((fakeopen_fd < 0), "open");
#endif

    result = poll(fds, SIZE_OF_ARR(fds), poll_timeout);
    memset(&after, 0, sizeof(struct timespec));
    HANDLE_ERROR(clock_gettime(MY_CLOCK_TIME, &after), "clock_gettime");
    double interval = timediff(after, before);
    printf("call interval: %.3f, poll result: %d, errr: %s\n", interval, result, strerror(errno));

#ifdef USE_FAKE_OPEN
    close(fakeopen_fd);
#endif

    close(fd);
    return (interval > 2.0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

在 Linux、Solaris 10、AIX 7.1 上,它按预期工作(call interval: 3.000, poll result: 0, errr: Error 0),但 HP-UX(11.31) 不立即遵守超时参数和 returns (call interval: 0.000, poll result: 1, errr: Error 0).

如果我在轮询读取它之前打开 FIFO 进行写入,(定义 USE_FAKE_OPEN)即使对于 HP-UX 它也能按预期工作。

  1. 是什么导致了这种行为?

  2. HP-UX 的投票 POSIX 是否合规?

好的,有一个编程规则就是"It's Always Your Fault (Select Isn't Broken)", but HP says their poll on FIFO is broken in their documentation for HP-UX patch PHKL_41419:

poll(2) sets POLLIN (data ready for reading)event even if there is no writer at the other end when a FIFO is opened with O_RDONLY in non blocking mode(O_NONBLOCK set).

案件结案。