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:        signalfd4_02.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=5fb5e04926a54bc1c22bba7ca166840f4476196f */
28f08c3bdfSopenharmony_ci/* which says:                                                                */
29f08c3bdfSopenharmony_ci/* This patch adds support for the SFD_NONBLOCK flag to signalfd4.  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/* signalfd4_02 [-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:   signalfd4_02                                                  */
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 <fcntl.h>
54f08c3bdfSopenharmony_ci#include <signal.h>
55f08c3bdfSopenharmony_ci#include <stdio.h>
56f08c3bdfSopenharmony_ci#include <unistd.h>
57f08c3bdfSopenharmony_ci#include <sys/syscall.h>
58f08c3bdfSopenharmony_ci#include <errno.h>
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci#include "test.h"
61f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
62f08c3bdfSopenharmony_ci#include "ltp_signal.h"
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci#define SFD_NONBLOCK O_NONBLOCK
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cichar *TCID = "signalfd4_02";
67f08c3bdfSopenharmony_ciint testno;
68f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci/* Extern Global Functions */
71f08c3bdfSopenharmony_ci/******************************************************************************/
72f08c3bdfSopenharmony_ci/*                                                                            */
73f08c3bdfSopenharmony_ci/* Function:    cleanup                                                       */
74f08c3bdfSopenharmony_ci/*                                                                            */
75f08c3bdfSopenharmony_ci/* Description: Performs all one time clean up for this test on successful    */
76f08c3bdfSopenharmony_ci/*              completion,  premature exit or  failure. Closes all temporary */
77f08c3bdfSopenharmony_ci/*              files, removes all temporary directories exits the test with  */
78f08c3bdfSopenharmony_ci/*              appropriate return code by calling tst_exit() function.       */
79f08c3bdfSopenharmony_ci/*                                                                            */
80f08c3bdfSopenharmony_ci/* Input:       None.                                                         */
81f08c3bdfSopenharmony_ci/*                                                                            */
82f08c3bdfSopenharmony_ci/* Output:      None.                                                         */
83f08c3bdfSopenharmony_ci/*                                                                            */
84f08c3bdfSopenharmony_ci/* Return:      On failure - Exits calling tst_exit(). Non '0' return code.   */
85f08c3bdfSopenharmony_ci/*              On success - Exits calling tst_exit(). With '0' return code.  */
86f08c3bdfSopenharmony_ci/*                                                                            */
87f08c3bdfSopenharmony_ci/******************************************************************************/
88f08c3bdfSopenharmony_civoid cleanup(void)
89f08c3bdfSopenharmony_ci{
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_ci	tst_rmdir();
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci}
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci/* Local  Functions */
96f08c3bdfSopenharmony_ci/******************************************************************************/
97f08c3bdfSopenharmony_ci/*                                                                            */
98f08c3bdfSopenharmony_ci/* Function:    setup                                                         */
99f08c3bdfSopenharmony_ci/*                                                                            */
100f08c3bdfSopenharmony_ci/* Description: Performs all one time setup for this test. This function is   */
101f08c3bdfSopenharmony_ci/*              typically used to capture signals, create temporary dirs      */
102f08c3bdfSopenharmony_ci/*              and temporary files that may be used in the course of this    */
103f08c3bdfSopenharmony_ci/*              test.                                                         */
104f08c3bdfSopenharmony_ci/*                                                                            */
105f08c3bdfSopenharmony_ci/* Input:       None.                                                         */
106f08c3bdfSopenharmony_ci/*                                                                            */
107f08c3bdfSopenharmony_ci/* Output:      None.                                                         */
108f08c3bdfSopenharmony_ci/*                                                                            */
109f08c3bdfSopenharmony_ci/* Return:      On failure - Exits by calling cleanup().                      */
110f08c3bdfSopenharmony_ci/*              On success - returns 0.                                       */
111f08c3bdfSopenharmony_ci/*                                                                            */
112f08c3bdfSopenharmony_ci/******************************************************************************/
113f08c3bdfSopenharmony_civoid setup(void)
114f08c3bdfSopenharmony_ci{
115f08c3bdfSopenharmony_ci	/* Capture signals if any */
116f08c3bdfSopenharmony_ci	/* Create temporary directories */
117f08c3bdfSopenharmony_ci	TEST_PAUSE;
118f08c3bdfSopenharmony_ci	tst_tmpdir();
119f08c3bdfSopenharmony_ci}
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ciint main(int argc, char *argv[])
122f08c3bdfSopenharmony_ci{
123f08c3bdfSopenharmony_ci	sigset_t ss;
124f08c3bdfSopenharmony_ci	int fd, fl;
125f08c3bdfSopenharmony_ci	int lc;
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, NULL, NULL);
128f08c3bdfSopenharmony_ci	setup();
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); ++lc) {
131f08c3bdfSopenharmony_ci		tst_count = 0;
132f08c3bdfSopenharmony_ci		for (testno = 0; testno < TST_TOTAL; ++testno) {
133f08c3bdfSopenharmony_ci			sigemptyset(&ss);
134f08c3bdfSopenharmony_ci			sigaddset(&ss, SIGUSR1);
135f08c3bdfSopenharmony_ci			fd = tst_syscall(__NR_signalfd4, -1, &ss,
136f08c3bdfSopenharmony_ci				SIGSETSIZE, 0);
137f08c3bdfSopenharmony_ci			if (fd == -1) {
138f08c3bdfSopenharmony_ci				tst_brkm(TFAIL, cleanup,
139f08c3bdfSopenharmony_ci					 "signalfd4(0) failed");
140f08c3bdfSopenharmony_ci			}
141f08c3bdfSopenharmony_ci			fl = fcntl(fd, F_GETFL);
142f08c3bdfSopenharmony_ci			if (fl == -1) {
143f08c3bdfSopenharmony_ci				tst_brkm(TBROK, cleanup, "fcntl failed");
144f08c3bdfSopenharmony_ci			}
145f08c3bdfSopenharmony_ci			if (fl & O_NONBLOCK) {
146f08c3bdfSopenharmony_ci				tst_brkm(TFAIL,
147f08c3bdfSopenharmony_ci					 cleanup,
148f08c3bdfSopenharmony_ci					 "signalfd4(0) set non-blocking mode");
149f08c3bdfSopenharmony_ci			}
150f08c3bdfSopenharmony_ci			close(fd);
151f08c3bdfSopenharmony_ci
152f08c3bdfSopenharmony_ci			fd = tst_syscall(__NR_signalfd4, -1, &ss, SIGSETSIZE,
153f08c3bdfSopenharmony_ci				     SFD_NONBLOCK);
154f08c3bdfSopenharmony_ci			if (fd == -1) {
155f08c3bdfSopenharmony_ci				tst_brkm(TFAIL,
156f08c3bdfSopenharmony_ci					 cleanup,
157f08c3bdfSopenharmony_ci					 "signalfd4(SFD_NONBLOCK) failed");
158f08c3bdfSopenharmony_ci			}
159f08c3bdfSopenharmony_ci			fl = fcntl(fd, F_GETFL);
160f08c3bdfSopenharmony_ci			if (fl == -1) {
161f08c3bdfSopenharmony_ci				tst_brkm(TBROK, cleanup, "fcntl failed");
162f08c3bdfSopenharmony_ci			}
163f08c3bdfSopenharmony_ci			if ((fl & O_NONBLOCK) == 0) {
164f08c3bdfSopenharmony_ci				tst_brkm(TFAIL,
165f08c3bdfSopenharmony_ci					 cleanup,
166f08c3bdfSopenharmony_ci					 "signalfd4(SFD_NONBLOCK) does not set non-blocking mode");
167f08c3bdfSopenharmony_ci			}
168f08c3bdfSopenharmony_ci			close(fd);
169f08c3bdfSopenharmony_ci			tst_resm(TPASS, "signalfd4(SFD_NONBLOCK) PASSED");
170f08c3bdfSopenharmony_ci			cleanup();
171f08c3bdfSopenharmony_ci		}
172f08c3bdfSopenharmony_ci	}
173f08c3bdfSopenharmony_ci	tst_exit();
174f08c3bdfSopenharmony_ci}
175