]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/time/tst-timerfd.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / time / tst-timerfd.c
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
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <error.h>
15 #include <signal.h>
16 #include <stdint.h>
17 #include <inttypes.h>
18 #include <time.h>
19 #include <sys/timerfd.h>
20 #include <sys/fcntl.h>
21
22 static int
23 do_test(void)
24 {
25         int fd, ret, result = 0;
26         struct itimerspec s;
27         uint64_t val;
28         time_t start, now;
29
30         fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
31         if (fd < 0) {
32                 perror("timerfd() failed");
33                 result = 1;
34         }
35         s.it_value.tv_sec = 1;
36         s.it_value.tv_nsec = 0;
37         s.it_interval.tv_sec = 0;
38         s.it_interval.tv_nsec = 0;
39         timerfd_settime(fd, 0, &s, NULL);
40         start = time(NULL);
41
42         /* this should return immediately with EAGAIN due to TFD_NONBLOCK */
43         ret = read(fd, &val, sizeof(val));
44         if (ret != -1 || errno != EAGAIN) {
45                 error(0, 0, "first read() returned %d", ret);
46                 result = 1;
47         }
48
49         /* let the timer expire, then check it again */
50         do {
51                 now = time(NULL);
52         } while (now - start < 2);
53
54         ret = read(fd, &val, sizeof(val));
55         if (ret != sizeof(val)) {
56                 error(0, 0, "second read() returned %d", ret);
57                 result = 1;
58         }
59
60         /* we are expecting a single expiration, since it_interval is 0 */
61         if (val != 1) {
62                 error(0, 0, "wrong number of expirations: %" PRIx64, val);
63                 result = 1;
64         }
65
66         return result;
67 }
68
69 #define TIMEOUT 5
70 #define TEST_FUNCTION do_test ()
71 #include "../test-skeleton.c"