timeout.c 1.4 KB

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