linux-stable/tools/testing/vsock/timeout.c
Thomas Gleixner b886d83c5b treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441
Based on 1 normalized pattern(s):

  this program is free software you can redistribute it and or modify
  it under the terms of the gnu general public license as published by
  the free software foundation version 2 of the license

extracted by the scancode license scanner the SPDX license identifier

  GPL-2.0-only

has been chosen to replace the boilerplate/reference in 315 file(s).

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Allison Randal <allison@lohutok.net>
Reviewed-by: Armijn Hemel <armijn@tjaldur.nl>
Cc: linux-spdx@vger.kernel.org
Link: https://lkml.kernel.org/r/20190531190115.503150771@linutronix.de
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-05 17:37:17 +02:00

60 lines
1.3 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/* Timeout API for single-threaded programs that use blocking
* syscalls (read/write/send/recv/connect/accept).
*
* Copyright (C) 2017 Red Hat, Inc.
*
* Author: Stefan Hajnoczi <stefanha@redhat.com>
*/
/* Use the following pattern:
*
* timeout_begin(TIMEOUT);
* do {
* ret = accept(...);
* timeout_check("accept");
* } while (ret < 0 && ret == EINTR);
* timeout_end();
*/
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include "timeout.h"
static volatile bool timeout;
/* SIGALRM handler function. Do not use sleep(2), alarm(2), or
* setitimer(2) while using this API - they may interfere with each
* other.
*/
void sigalrm(int signo)
{
timeout = true;
}
/* Start a timeout. Call timeout_check() to verify that the timeout hasn't
* expired. timeout_end() must be called to stop the timeout. Timeouts cannot
* be nested.
*/
void timeout_begin(unsigned int seconds)
{
alarm(seconds);
}
/* Exit with an error message if the timeout has expired */
void timeout_check(const char *operation)
{
if (timeout) {
fprintf(stderr, "%s timed out\n", operation);
exit(EXIT_FAILURE);
}
}
/* Stop a timeout */
void timeout_end(void)
{
alarm(0);
timeout = false;
}