blob: 5927646bb4032173ffdef1fb56edddfc41a0c205 [file] [log] [blame]
subrata_modak99b52e52009-01-16 10:07:55 +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_modak99b52e52009-01-16 10:07:55 +000019/* */
20/******************************************************************************/
21/******************************************************************************/
22/* */
23/* File: signalfd4_01.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=9deb27baedb79759c3ab9435a7d8b841842d56e9 */
28/* says: */
29/* This patch adds the new signalfd4 syscall. It extends the old signalfd */
30/* syscall by one parameter which is meant to hold a flag value. In this */
31/* patch the only flag support is SFD_CLOEXEC which causes the close-on-exec */
32/* flag for the returned file descriptor to be set. A new name SFD_CLOEXEC is */
33/* introduced which in this implementation must have the same value as */
34/* O_CLOEXEC */
35/* The following test must be adjusted for architectures other than x86 and */
36/* x86-64 and in case the syscall numbers changed. */
37/* */
38/* Usage: <for command-line> */
39/* signalfd4_01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */
40/* where, -c n : Run n copies concurrently. */
41/* -e : Turn on errno logging. */
42/* -i n : Execute test n times. */
43/* -I x : Execute test for x seconds. */
44/* -P x : Pause for x seconds between iterations. */
45/* -t : Turn on syscall timing. */
46/* */
47/* Total Tests: 1 */
48/* */
49/* Test Name: signalfd4_01 */
50/* */
51/* Author: Ulrich Drepper <drepper@redhat.com> */
52/* */
53/* History: Created - Jan 08 2009 - Ulrich Drepper <drepper@redhat.com> */
54/* Ported to LTP */
55/* - Jan 08 2009 - Subrata <subrata@linux.vnet.ibm.com> */
56/******************************************************************************/
57#include <fcntl.h>
58#include <signal.h>
59#include <stdio.h>
60#include <unistd.h>
61#include <sys/syscall.h>
subrata_modakc488f902009-02-23 07:17:33 +000062#include <errno.h>
subrata_modak99b52e52009-01-16 10:07:55 +000063
subrata_modak99b52e52009-01-16 10:07:55 +000064#include "test.h"
Cyril Hrubis98f87472014-01-08 13:32:41 +010065#include "lapi/fcntl.h"
subrata_modak115006c2009-02-04 06:16:40 +000066#include "linux_syscall_numbers.h"
Garrett Coopera9a04b52010-09-06 11:44:51 -070067#include "ltp_signal.h"
subrata_modak99b52e52009-01-16 10:07:55 +000068
subrata_modak99b52e52009-01-16 10:07:55 +000069#define SFD_CLOEXEC O_CLOEXEC
70
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020071char *TCID = "signalfd4_01";
subrata_modak56207ce2009-03-23 13:35:39 +000072int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020073int TST_TOTAL = 1;
subrata_modak99b52e52009-01-16 10:07:55 +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 tst_rmdir();
subrata_modak99b52e52009-01-16 10:07:55 +000097
subrata_modak99b52e52009-01-16 10:07:55 +000098}
99
100/* Local Functions */
101/******************************************************************************/
102/* */
103/* Function: setup */
104/* */
105/* Description: Performs all one time setup for this test. This function is */
106/* typically used to capture signals, create temporary dirs */
107/* and temporary files that may be used in the course of this */
108/* test. */
109/* */
110/* Input: None. */
111/* */
112/* Output: None. */
113/* */
114/* Return: On failure - Exits by calling cleanup(). */
115/* On success - returns 0. */
116/* */
117/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400118void setup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000119{
120 /* Capture signals if any */
121 /* Create temporary directories */
122 TEST_PAUSE;
123 tst_tmpdir();
subrata_modak99b52e52009-01-16 10:07:55 +0000124}
125
subrata_modak56207ce2009-03-23 13:35:39 +0000126int main(int argc, char *argv[])
127{
128 int fd, coe;
129 sigset_t ss;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200130 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200131 const char *msg;
subrata_modak99b52e52009-01-16 10:07:55 +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_modak99b52e52009-01-16 10:07:55 +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) {
147 sigemptyset(&ss);
148 sigaddset(&ss, SIGUSR1);
Jan Stancek359980f2013-02-15 10:16:05 +0100149 fd = ltp_syscall(__NR_signalfd4, -1, &ss,
150 SIGSETSIZE, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000151 if (fd == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100152 tst_brkm(TFAIL, cleanup,
153 "signalfd4(0) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000154 }
155 coe = fcntl(fd, F_GETFD);
156 if (coe == -1) {
157 tst_brkm(TBROK, cleanup, "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000158 }
159 if (coe & FD_CLOEXEC) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100160 tst_brkm(TFAIL,
161 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000162 "signalfd4(0) set close-on-exec flag");
subrata_modak56207ce2009-03-23 13:35:39 +0000163 }
164 close(fd);
subrata_modak99b52e52009-01-16 10:07:55 +0000165
Jan Stancek359980f2013-02-15 10:16:05 +0100166 fd = ltp_syscall(__NR_signalfd4, -1, &ss, SIGSETSIZE,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 SFD_CLOEXEC);
subrata_modak56207ce2009-03-23 13:35:39 +0000168 if (fd == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100169 tst_brkm(TFAIL,
170 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000171 "signalfd4(SFD_CLOEXEC) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000172 }
173 coe = fcntl(fd, F_GETFD);
174 if (coe == -1) {
175 tst_brkm(TBROK, cleanup, "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000176 }
177 if ((coe & FD_CLOEXEC) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100178 tst_brkm(TFAIL,
179 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000180 "signalfd4(SFD_CLOEXEC) does not set close-on-exec flag");
subrata_modak56207ce2009-03-23 13:35:39 +0000181 }
182 close(fd);
183 tst_resm(TPASS, "signalfd4(SFD_CLOEXEC) Passed");
184 cleanup();
185 }
186 }
187 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700188}