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