1 /******************************************************************************/
2 /* */
3 /* Copyright (c) Ulrich Drepper <drepper@redhat.com> */
4 /* Copyright (c) International Business Machines Corp., 2009 */
5 /* */
6 /* This program is free software; you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation; either version 2 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* This program is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
14 /* the GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with this program; if not, write to the Free Software */
18 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
19 /* */
20 /******************************************************************************/
21 /******************************************************************************/
22 /* */
23 /* File: timerfd02.c */
24 /* */
25 /* Description: This Program tests the new system call introduced in 2.6.27. */
26 /* Ulrich´s comment as in: */
27 /* http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=11fcb6c14676023d0bd437841f5dcd670e7990a0 */
28 /* says: */
29 /* The timerfd_create syscall already has a flags parameter. It just is */
30 /* unused so far. This patch changes this by introducing the TFD_CLOEXEC */
31 /* flag to set the close-on-exec flag for the returned file descriptor. A new */
32 /* name TFD_CLOEXEC is introduced which in this implementation must have the */
33 /* same value as O_CLOEXEC. */
34 /* The following test must be adjusted for architectures other than x86 and */
35 /* x86-64 and in case the syscall numbers changed. */
36 /* */
37 /* Usage: <for command-line> */
38 /* timerfd02 [-c n] [-e][-i n] [-I x] [-p x] [-t] */
39 /* where, -c n : Run n copies concurrently. */
40 /* -e : Turn on errno logging. */
41 /* -i n : Execute test n times. */
42 /* -I x : Execute test for x seconds. */
43 /* -P x : Pause for x seconds between iterations. */
44 /* -t : Turn on syscall timing. */
45 /* */
46 /* Total Tests: 1 */
47 /* */
48 /* Test Name: timerfd02 */
49 /* */
50 /* Author: Ulrich Drepper <drepper@redhat.com> */
51 /* */
52 /* History: Created - Jan 08 2009 - Ulrich Drepper <drepper@redhat.com> */
53 /* Ported to LTP */
54 /* - Jan 08 2009 - Subrata <subrata@linux.vnet.ibm.com> */
55 /******************************************************************************/
56
57 #include <stdio.h>
58 #include <time.h>
59 #include <unistd.h>
60 #include <sys/syscall.h>
61 #include <errno.h>
62
63 #include "test.h"
64 #include "lapi/fcntl.h"
65 #include "lapi/syscalls.h"
66
67 #define TFD_CLOEXEC O_CLOEXEC
68
69 char *TCID = "timerfd02";
70 int testno;
71 int TST_TOTAL = 1;
72
73 /* Extern Global Functions */
74 /******************************************************************************/
75 /* */
76 /* Function: cleanup */
77 /* */
78 /* Description: Performs all one time clean up for this test on successful */
79 /* completion, premature exit or failure. Closes all temporary */
80 /* files, removes all temporary directories exits the test with */
81 /* appropriate return code by calling tst_exit() function. */
82 /* */
83 /* Input: None. */
84 /* */
85 /* Output: None. */
86 /* */
87 /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
88 /* On success - Exits calling tst_exit(). With '0' return code. */
89 /* */
90 /******************************************************************************/
cleanup(void)91 void cleanup(void)
92 {
93
94 tst_rmdir();
95
96 }
97
98 /* Local Functions */
99 /******************************************************************************/
100 /* */
101 /* Function: setup */
102 /* */
103 /* Description: Performs all one time setup for this test. This function is */
104 /* typically used to capture signals, create temporary dirs */
105 /* and temporary files that may be used in the course of this */
106 /* test. */
107 /* */
108 /* Input: None. */
109 /* */
110 /* Output: None. */
111 /* */
112 /* Return: On failure - Exits by calling cleanup(). */
113 /* On success - returns 0. */
114 /* */
115 /******************************************************************************/
setup(void)116 void setup(void)
117 {
118 /* Capture signals if any */
119 /* Create temporary directories */
120 TEST_PAUSE;
121 tst_tmpdir();
122 }
123
main(int argc,char * argv[])124 int main(int argc, char *argv[])
125 {
126 int fd, coe;
127 int lc;
128
129 tst_parse_opts(argc, argv, NULL, NULL);
130 setup();
131
132 for (lc = 0; TEST_LOOPING(lc); ++lc) {
133 tst_count = 0;
134 for (testno = 0; testno < TST_TOTAL; ++testno) {
135 fd = tst_syscall(__NR_timerfd_create,
136 CLOCK_REALTIME, 0);
137 if (fd == -1) {
138 tst_brkm(TFAIL, cleanup,
139 "timerfd_create(0) failed");
140 }
141 coe = fcntl(fd, F_GETFD);
142 if (coe == -1) {
143 tst_brkm(TBROK, cleanup, "fcntl failed");
144 }
145 if (coe & FD_CLOEXEC) {
146 tst_brkm(TFAIL,
147 cleanup,
148 "timerfd_create(0) set close-on-exec flag");
149 }
150 close(fd);
151
152 fd = tst_syscall(__NR_timerfd_create, CLOCK_REALTIME,
153 TFD_CLOEXEC);
154 if (fd == -1) {
155 tst_brkm(TFAIL,
156 cleanup,
157 "timerfd_create(TFD_CLOEXEC) failed");
158 }
159 coe = fcntl(fd, F_GETFD);
160 if (coe == -1) {
161 tst_brkm(TBROK, cleanup, "fcntl failed");
162 }
163 if ((coe & FD_CLOEXEC) == 0) {
164 tst_brkm(TFAIL,
165 cleanup,
166 "timerfd_create(TFD_CLOEXEC) set close-on-exec flag");
167 }
168 close(fd);
169 tst_resm(TPASS, "timerfd_create(TFD_CLOEXEC) Passed");
170 cleanup();
171 }
172 }
173 tst_exit();
174 }
175