tst-timerfd.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* vi: set sw=4 ts=4 sts=4: */
  2. /*
  3. * timerfd test for uClibc
  4. * Copyright (C) 2012 by Kevin Cernekee <cernekee@gmail.com>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <error.h>
  14. #include <signal.h>
  15. #include <stdint.h>
  16. #include <inttypes.h>
  17. #include <time.h>
  18. #include <sys/timerfd.h>
  19. #include <sys/fcntl.h>
  20. static int
  21. do_test(void)
  22. {
  23. int fd, ret, result = 0;
  24. struct itimerspec s;
  25. uint64_t val;
  26. time_t start, now;
  27. fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
  28. if (fd < 0) {
  29. perror("timerfd() failed");
  30. result = 1;
  31. }
  32. s.it_value.tv_sec = 1;
  33. s.it_value.tv_nsec = 0;
  34. s.it_interval.tv_sec = 0;
  35. s.it_interval.tv_nsec = 0;
  36. timerfd_settime(fd, 0, &s, NULL);
  37. start = time(NULL);
  38. /* this should return immediately with EAGAIN due to TFD_NONBLOCK */
  39. ret = read(fd, &val, sizeof(val));
  40. if (ret != -1 || errno != EAGAIN) {
  41. error(0, 0, "first read() returned %d", ret);
  42. result = 1;
  43. }
  44. /* let the timer expire, then check it again */
  45. do {
  46. now = time(NULL);
  47. } while (now - start < 2);
  48. ret = read(fd, &val, sizeof(val));
  49. if (ret != sizeof(val)) {
  50. error(0, 0, "second read() returned %d", ret);
  51. result = 1;
  52. }
  53. /* we are expecting a single expiration, since it_interval is 0 */
  54. if (val != 1) {
  55. error(0, 0, "wrong number of expirations: %" PRIx64, val);
  56. result = 1;
  57. }
  58. return result;
  59. }
  60. #define TIMEOUT 5
  61. #define TEST_FUNCTION do_test ()
  62. #include "../test-skeleton.c"