Path: blob/master/tools/testing/selftests/ftrace/poll.c
26285 views
// SPDX-License-Identifier: GPL-2.01/*2* Simple poll on a file.3*4* Copyright (c) 2024 Google LLC.5*/67#include <errno.h>8#include <fcntl.h>9#include <poll.h>10#include <stdio.h>11#include <stdlib.h>12#include <string.h>13#include <unistd.h>1415#define BUFSIZE 40961617/*18* Usage:19* poll [-I|-P] [-t timeout] FILE20*/21int main(int argc, char *argv[])22{23struct pollfd pfd = {.events = POLLIN};24char buf[BUFSIZE];25int timeout = -1;26int ret, opt;2728while ((opt = getopt(argc, argv, "IPt:")) != -1) {29switch (opt) {30case 'I':31pfd.events = POLLIN;32break;33case 'P':34pfd.events = POLLPRI;35break;36case 't':37timeout = atoi(optarg);38break;39default:40fprintf(stderr, "Usage: %s [-I|-P] [-t timeout] FILE\n",41argv[0]);42return -1;43}44}45if (optind >= argc) {46fprintf(stderr, "Error: Polling file is not specified\n");47return -1;48}4950pfd.fd = open(argv[optind], O_RDONLY);51if (pfd.fd < 0) {52fprintf(stderr, "failed to open %s", argv[optind]);53perror("open");54return -1;55}5657/* Reset poll by read if POLLIN is specified. */58if (pfd.events & POLLIN)59do {} while (read(pfd.fd, buf, BUFSIZE) == BUFSIZE);6061ret = poll(&pfd, 1, timeout);62if (ret < 0 && errno != EINTR) {63perror("poll");64return -1;65}66close(pfd.fd);6768/* If timeout happned (ret == 0), exit code is 1 */69if (ret == 0)70return 1;7172return 0;73}747576