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 <fcntl.h>
58#include <stdio.h>
59#include <time.h>
60#include <unistd.h>
61#include <sys/syscall.h>
62#include <errno.h>
63
64#include "test.h"
65#include "lapi/fcntl.h"
66#include "linux_syscall_numbers.h"
67
68#define TFD_CLOEXEC O_CLOEXEC
69
70char *TCID = "timerfd02";
71int testno;
72int TST_TOTAL = 1;
73
74/* Extern Global Functions */
75/******************************************************************************/
76/*                                                                            */
77/* Function:    cleanup                                                       */
78/*                                                                            */
79/* Description: Performs all one time clean up for this test on successful    */
80/*              completion,  premature exit or  failure. Closes all temporary */
81/*              files, removes all temporary directories exits the test with  */
82/*              appropriate return code by calling tst_exit() function.       */
83/*                                                                            */
84/* Input:       None.                                                         */
85/*                                                                            */
86/* Output:      None.                                                         */
87/*                                                                            */
88/* Return:      On failure - Exits calling tst_exit(). Non '0' return code.   */
89/*              On success - Exits calling tst_exit(). With '0' return code.  */
90/*                                                                            */
91/******************************************************************************/
92void cleanup(void)
93{
94
95	tst_rmdir();
96
97}
98
99/* Local  Functions */
100/******************************************************************************/
101/*                                                                            */
102/* Function:    setup                                                         */
103/*                                                                            */
104/* Description: Performs all one time setup for this test. This function is   */
105/*              typically used to capture signals, create temporary dirs      */
106/*              and temporary files that may be used in the course of this    */
107/*              test.                                                         */
108/*                                                                            */
109/* Input:       None.                                                         */
110/*                                                                            */
111/* Output:      None.                                                         */
112/*                                                                            */
113/* Return:      On failure - Exits by calling cleanup().                      */
114/*              On success - returns 0.                                       */
115/*                                                                            */
116/******************************************************************************/
117void setup(void)
118{
119	/* Capture signals if any */
120	/* Create temporary directories */
121	TEST_PAUSE;
122	tst_tmpdir();
123}
124
125int main(int argc, char *argv[])
126{
127	int fd, coe;
128	int lc;
129
130	tst_parse_opts(argc, argv, NULL, NULL);
131	if ((tst_kvercmp(2, 6, 27)) < 0) {
132		tst_brkm(TCONF,
133			 NULL,
134			 "This test can only run on kernels that are 2.6.27 and higher");
135	}
136	setup();
137
138	for (lc = 0; TEST_LOOPING(lc); ++lc) {
139		tst_count = 0;
140		for (testno = 0; testno < TST_TOTAL; ++testno) {
141			fd = ltp_syscall(__NR_timerfd_create,
142				CLOCK_REALTIME, 0);
143			if (fd == -1) {
144				tst_brkm(TFAIL, cleanup,
145					 "timerfd_create(0) failed");
146			}
147			coe = fcntl(fd, F_GETFD);
148			if (coe == -1) {
149				tst_brkm(TBROK, cleanup, "fcntl failed");
150			}
151			if (coe & FD_CLOEXEC) {
152				tst_brkm(TFAIL,
153					 cleanup,
154					 "timerfd_create(0) set close-on-exec flag");
155			}
156			close(fd);
157
158			fd = ltp_syscall(__NR_timerfd_create, CLOCK_REALTIME,
159				     TFD_CLOEXEC);
160			if (fd == -1) {
161				tst_brkm(TFAIL,
162					 cleanup,
163					 "timerfd_create(TFD_CLOEXEC) failed");
164			}
165			coe = fcntl(fd, F_GETFD);
166			if (coe == -1) {
167				tst_brkm(TBROK, cleanup, "fcntl failed");
168			}
169			if ((coe & FD_CLOEXEC) == 0) {
170				tst_brkm(TFAIL,
171					 cleanup,
172					 "timerfd_create(TFD_CLOEXEC) set close-on-exec flag");
173			}
174			close(fd);
175			tst_resm(TPASS, "timerfd_create(TFD_CLOEXEC) Passed");
176			cleanup();
177		}
178	}
179	tst_exit();
180}
181