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:        timerfd03.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=6b1ef0e60d42f2fdaec26baee8327eb156347b4f */
28f08c3bdfSopenharmony_ci/* which says:                                                                */
29f08c3bdfSopenharmony_ci/* This patch adds support for the TFD_NONBLOCK flag to timerfd_create.  The  */
30f08c3bdfSopenharmony_ci/* additional changes needed are minimal. The following test must be adjusted */
31f08c3bdfSopenharmony_ci/* for architectures other than x86 and x86-64 and in case the syscall numbers*/
32f08c3bdfSopenharmony_ci/* changed.                                                                   */
33f08c3bdfSopenharmony_ci/*                                                                            */
34f08c3bdfSopenharmony_ci/* Usage:  <for command-line>                                                 */
35f08c3bdfSopenharmony_ci/* timerfd03 [-c n] [-e][-i n] [-I x] [-p x] [-t]                          */
36f08c3bdfSopenharmony_ci/*      where,  -c n : Run n copies concurrently.                             */
37f08c3bdfSopenharmony_ci/*              -e   : Turn on errno logging.                                 */
38f08c3bdfSopenharmony_ci/*              -i n : Execute test n times.                                  */
39f08c3bdfSopenharmony_ci/*              -I x : Execute test for x seconds.                            */
40f08c3bdfSopenharmony_ci/*              -P x : Pause for x seconds between iterations.                */
41f08c3bdfSopenharmony_ci/*              -t   : Turn on syscall timing.                                */
42f08c3bdfSopenharmony_ci/*                                                                            */
43f08c3bdfSopenharmony_ci/* Total Tests: 1                                                             */
44f08c3bdfSopenharmony_ci/*                                                                            */
45f08c3bdfSopenharmony_ci/* Test Name:   timerfd03                                                  */
46f08c3bdfSopenharmony_ci/*                                                                            */
47f08c3bdfSopenharmony_ci/* Author:      Ulrich Drepper <drepper@redhat.com>                           */
48f08c3bdfSopenharmony_ci/*                                                                            */
49f08c3bdfSopenharmony_ci/* History:     Created - Jan 13 2009 - Ulrich Drepper <drepper@redhat.com>   */
50f08c3bdfSopenharmony_ci/*              Ported to LTP                                                 */
51f08c3bdfSopenharmony_ci/*                      - Jan 13 2009 - Subrata <subrata@linux.vnet.ibm.com>  */
52f08c3bdfSopenharmony_ci/******************************************************************************/
53f08c3bdfSopenharmony_ci#include <stdio.h>
54f08c3bdfSopenharmony_ci#include <time.h>
55f08c3bdfSopenharmony_ci#include <unistd.h>
56f08c3bdfSopenharmony_ci#include <sys/syscall.h>
57f08c3bdfSopenharmony_ci#include <errno.h>
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci#include "test.h"
60f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
61f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci#define TFD_NONBLOCK O_NONBLOCK
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cichar *TCID = "timerfd03";
66f08c3bdfSopenharmony_ciint testno;
67f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci/* Extern Global Functions */
70f08c3bdfSopenharmony_ci/******************************************************************************/
71f08c3bdfSopenharmony_ci/*                                                                            */
72f08c3bdfSopenharmony_ci/* Function:    cleanup                                                       */
73f08c3bdfSopenharmony_ci/*                                                                            */
74f08c3bdfSopenharmony_ci/* Description: Performs all one time clean up for this test on successful    */
75f08c3bdfSopenharmony_ci/*              completion,  premature exit or  failure. Closes all temporary */
76f08c3bdfSopenharmony_ci/*              files, removes all temporary directories exits the test with  */
77f08c3bdfSopenharmony_ci/*              appropriate return code by calling tst_exit() function.       */
78f08c3bdfSopenharmony_ci/*                                                                            */
79f08c3bdfSopenharmony_ci/* Input:       None.                                                         */
80f08c3bdfSopenharmony_ci/*                                                                            */
81f08c3bdfSopenharmony_ci/* Output:      None.                                                         */
82f08c3bdfSopenharmony_ci/*                                                                            */
83f08c3bdfSopenharmony_ci/* Return:      On failure - Exits calling tst_exit(). Non '0' return code.   */
84f08c3bdfSopenharmony_ci/*              On success - Exits calling tst_exit(). With '0' return code.  */
85f08c3bdfSopenharmony_ci/*                                                                            */
86f08c3bdfSopenharmony_ci/******************************************************************************/
87f08c3bdfSopenharmony_civoid cleanup(void)
88f08c3bdfSopenharmony_ci{
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	tst_rmdir();
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci}
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci/* Local  Functions */
95f08c3bdfSopenharmony_ci/******************************************************************************/
96f08c3bdfSopenharmony_ci/*                                                                            */
97f08c3bdfSopenharmony_ci/* Function:    setup                                                         */
98f08c3bdfSopenharmony_ci/*                                                                            */
99f08c3bdfSopenharmony_ci/* Description: Performs all one time setup for this test. This function is   */
100f08c3bdfSopenharmony_ci/*              typically used to capture signals, create temporary dirs      */
101f08c3bdfSopenharmony_ci/*              and temporary files that may be used in the course of this    */
102f08c3bdfSopenharmony_ci/*              test.                                                         */
103f08c3bdfSopenharmony_ci/*                                                                            */
104f08c3bdfSopenharmony_ci/* Input:       None.                                                         */
105f08c3bdfSopenharmony_ci/*                                                                            */
106f08c3bdfSopenharmony_ci/* Output:      None.                                                         */
107f08c3bdfSopenharmony_ci/*                                                                            */
108f08c3bdfSopenharmony_ci/* Return:      On failure - Exits by calling cleanup().                      */
109f08c3bdfSopenharmony_ci/*              On success - returns 0.                                       */
110f08c3bdfSopenharmony_ci/*                                                                            */
111f08c3bdfSopenharmony_ci/******************************************************************************/
112f08c3bdfSopenharmony_civoid setup(void)
113f08c3bdfSopenharmony_ci{
114f08c3bdfSopenharmony_ci	/* Capture signals if any */
115f08c3bdfSopenharmony_ci	/* Create temporary directories */
116f08c3bdfSopenharmony_ci	TEST_PAUSE;
117f08c3bdfSopenharmony_ci	tst_tmpdir();
118f08c3bdfSopenharmony_ci}
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ciint main(int argc, char *argv[])
121f08c3bdfSopenharmony_ci{
122f08c3bdfSopenharmony_ci	int fd, fl;
123f08c3bdfSopenharmony_ci	int lc;
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, NULL, NULL);
126f08c3bdfSopenharmony_ci	setup();
127f08c3bdfSopenharmony_ci
128f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); ++lc) {
129f08c3bdfSopenharmony_ci		tst_count = 0;
130f08c3bdfSopenharmony_ci		for (testno = 0; testno < TST_TOTAL; ++testno) {
131f08c3bdfSopenharmony_ci			fd = tst_syscall(__NR_timerfd_create,
132f08c3bdfSopenharmony_ci				CLOCK_REALTIME, 0);
133f08c3bdfSopenharmony_ci			if (fd == -1) {
134f08c3bdfSopenharmony_ci				tst_brkm(TFAIL, cleanup,
135f08c3bdfSopenharmony_ci					 "timerfd_create(0) failed");
136f08c3bdfSopenharmony_ci			}
137f08c3bdfSopenharmony_ci			fl = fcntl(fd, F_GETFL);
138f08c3bdfSopenharmony_ci			if (fl == -1) {
139f08c3bdfSopenharmony_ci				tst_brkm(TBROK, cleanup, "fcntl failed");
140f08c3bdfSopenharmony_ci			}
141f08c3bdfSopenharmony_ci			if (fl & O_NONBLOCK) {
142f08c3bdfSopenharmony_ci				tst_brkm(TFAIL,
143f08c3bdfSopenharmony_ci					 cleanup,
144f08c3bdfSopenharmony_ci					 "timerfd_create(0) set non-blocking mode");
145f08c3bdfSopenharmony_ci			}
146f08c3bdfSopenharmony_ci			close(fd);
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_ci			fd = tst_syscall(__NR_timerfd_create, CLOCK_REALTIME,
149f08c3bdfSopenharmony_ci				     TFD_NONBLOCK);
150f08c3bdfSopenharmony_ci			if (fd == -1) {
151f08c3bdfSopenharmony_ci				tst_brkm(TFAIL,
152f08c3bdfSopenharmony_ci					 cleanup,
153f08c3bdfSopenharmony_ci					 "timerfd_create(TFD_NONBLOCK) failed");
154f08c3bdfSopenharmony_ci			}
155f08c3bdfSopenharmony_ci			fl = fcntl(fd, F_GETFL);
156f08c3bdfSopenharmony_ci			if (fl == -1) {
157f08c3bdfSopenharmony_ci				tst_brkm(TBROK, cleanup, "fcntl failed");
158f08c3bdfSopenharmony_ci			}
159f08c3bdfSopenharmony_ci			if ((fl & O_NONBLOCK) == 0) {
160f08c3bdfSopenharmony_ci				tst_brkm(TFAIL,
161f08c3bdfSopenharmony_ci					 cleanup,
162f08c3bdfSopenharmony_ci					 "timerfd_create(TFD_NONBLOCK) set non-blocking mode");
163f08c3bdfSopenharmony_ci			}
164f08c3bdfSopenharmony_ci			close(fd);
165f08c3bdfSopenharmony_ci			tst_resm(TPASS, "timerfd_create(TFD_NONBLOCK) PASSED");
166f08c3bdfSopenharmony_ci			cleanup();
167f08c3bdfSopenharmony_ci		}
168f08c3bdfSopenharmony_ci	}
169f08c3bdfSopenharmony_ci	tst_exit();
170f08c3bdfSopenharmony_ci}
171