^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Timeout API for single-threaded programs that use blocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * syscalls (read/write/send/recv/connect/accept).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2017 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Stefan Hajnoczi <stefanha@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /* Use the following pattern:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * timeout_begin(TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * ret = accept(...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * timeout_check("accept");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * } while (ret < 0 && ret == EINTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * timeout_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "timeout.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static volatile bool timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* SIGALRM handler function. Do not use sleep(2), alarm(2), or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * setitimer(2) while using this API - they may interfere with each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * other.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) void sigalrm(int signo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) timeout = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Start a timeout. Call timeout_check() to verify that the timeout hasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * expired. timeout_end() must be called to stop the timeout. Timeouts cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * be nested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) void timeout_begin(unsigned int seconds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) alarm(seconds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* Exit with an error message if the timeout has expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) void timeout_check(const char *operation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) fprintf(stderr, "%s timed out\n", operation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* Stop a timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void timeout_end(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) alarm(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) timeout = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }