blob: d37fccfd54edfe729cec483733db1196bd943590 [file] [log] [blame]
subrata_modak89c795f2009-01-16 10:12:03 +00001/******************************************************************************/
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 */
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
subrata_modak89c795f2009-01-16 10:12:03 +000019/* */
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 <fcntl.h>
58#include <stdio.h>
59#include <time.h>
60#include <unistd.h>
61#include <sys/syscall.h>
subrata_modakc488f902009-02-23 07:17:33 +000062#include <errno.h>
subrata_modak89c795f2009-01-16 10:12:03 +000063
subrata_modak89c795f2009-01-16 10:12:03 +000064#include "test.h"
65#include "usctest.h"
Cyril Hrubis98f87472014-01-08 13:32:41 +010066#include "lapi/fcntl.h"
subrata_modak115006c2009-02-04 06:16:40 +000067#include "linux_syscall_numbers.h"
subrata_modak89c795f2009-01-16 10:12:03 +000068
subrata_modak89c795f2009-01-16 10:12:03 +000069#define TFD_CLOEXEC O_CLOEXEC
70
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020071char *TCID = "timerfd02";
subrata_modak56207ce2009-03-23 13:35:39 +000072int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020073int TST_TOTAL = 1;
subrata_modak89c795f2009-01-16 10:12:03 +000074
75/* Extern Global Functions */
76/******************************************************************************/
77/* */
78/* Function: cleanup */
79/* */
80/* Description: Performs all one time clean up for this test on successful */
81/* completion, premature exit or failure. Closes all temporary */
82/* files, removes all temporary directories exits the test with */
83/* appropriate return code by calling tst_exit() function. */
84/* */
85/* Input: None. */
86/* */
87/* Output: None. */
88/* */
89/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
90/* On success - Exits calling tst_exit(). With '0' return code. */
91/* */
92/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -040093void cleanup(void)
subrata_modak56207ce2009-03-23 13:35:39 +000094{
Garrett Cooper2c282152010-12-16 00:55:50 -080095
subrata_modak56207ce2009-03-23 13:35:39 +000096 TEST_CLEANUP;
97 tst_rmdir();
subrata_modak89c795f2009-01-16 10:12:03 +000098
subrata_modak89c795f2009-01-16 10:12:03 +000099}
100
101/* Local Functions */
102/******************************************************************************/
103/* */
104/* Function: setup */
105/* */
106/* Description: Performs all one time setup for this test. This function is */
107/* typically used to capture signals, create temporary dirs */
108/* and temporary files that may be used in the course of this */
109/* test. */
110/* */
111/* Input: None. */
112/* */
113/* Output: None. */
114/* */
115/* Return: On failure - Exits by calling cleanup(). */
116/* On success - returns 0. */
117/* */
118/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400119void setup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000120{
121 /* Capture signals if any */
122 /* Create temporary directories */
123 TEST_PAUSE;
124 tst_tmpdir();
subrata_modak89c795f2009-01-16 10:12:03 +0000125}
126
subrata_modak56207ce2009-03-23 13:35:39 +0000127int main(int argc, char *argv[])
128{
129 int fd, coe;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200130 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200131 const char *msg;
subrata_modak89c795f2009-01-16 10:12:03 +0000132
Garrett Cooper45e285d2010-11-22 12:19:25 -0800133 msg = parse_opts(argc, argv, NULL, NULL);
134 if (msg != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000135 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +0000136 }
137 if ((tst_kvercmp(2, 6, 27)) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100138 tst_brkm(TCONF,
139 NULL,
subrata_modak56207ce2009-03-23 13:35:39 +0000140 "This test can only run on kernels that are 2.6.27 and higher");
subrata_modak56207ce2009-03-23 13:35:39 +0000141 }
142 setup();
subrata_modak89c795f2009-01-16 10:12:03 +0000143
subrata_modak56207ce2009-03-23 13:35:39 +0000144 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800145 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000146 for (testno = 0; testno < TST_TOTAL; ++testno) {
Jan Stancek359980f2013-02-15 10:16:05 +0100147 fd = ltp_syscall(__NR_timerfd_create,
148 CLOCK_REALTIME, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000149 if (fd == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100150 tst_brkm(TFAIL, cleanup,
151 "timerfd_create(0) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000152 }
153 coe = fcntl(fd, F_GETFD);
154 if (coe == -1) {
155 tst_brkm(TBROK, cleanup, "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000156 }
157 if (coe & FD_CLOEXEC) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100158 tst_brkm(TFAIL,
159 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000160 "timerfd_create(0) set close-on-exec flag");
subrata_modak56207ce2009-03-23 13:35:39 +0000161 }
162 close(fd);
subrata_modak89c795f2009-01-16 10:12:03 +0000163
Jan Stancek359980f2013-02-15 10:16:05 +0100164 fd = ltp_syscall(__NR_timerfd_create, CLOCK_REALTIME,
subrata_modak56207ce2009-03-23 13:35:39 +0000165 TFD_CLOEXEC);
166 if (fd == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100167 tst_brkm(TFAIL,
168 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000169 "timerfd_create(TFD_CLOEXEC) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000170 }
171 coe = fcntl(fd, F_GETFD);
172 if (coe == -1) {
173 tst_brkm(TBROK, cleanup, "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000174 }
175 if ((coe & FD_CLOEXEC) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100176 tst_brkm(TFAIL,
177 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000178 "timerfd_create(TFD_CLOEXEC) set close-on-exec flag");
subrata_modak56207ce2009-03-23 13:35:39 +0000179 }
180 close(fd);
181 tst_resm(TPASS, "timerfd_create(TFD_CLOEXEC) Passed");
182 cleanup();
183 }
184 }
185 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700186}