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: 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 <signal.h>
58 #include <stdio.h>
59 #include <unistd.h>
60 #include <sys/syscall.h>
61 #include <errno.h>
62
63 #include "test.h"
64 #include "lapi/fcntl.h"
65 #include "lapi/syscalls.h"
66 #include "ltp_signal.h"
67
68 #define SFD_CLOEXEC O_CLOEXEC
69
70 char *TCID = "signalfd4_01";
71 int testno;
72 int 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 /******************************************************************************/
cleanup(void)92 void 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 /******************************************************************************/
setup(void)117 void setup(void)
118 {
119 /* Capture signals if any */
120 /* Create temporary directories */
121 TEST_PAUSE;
122 tst_tmpdir();
123 }
124
main(int argc,char * argv[])125 int main(int argc, char *argv[])
126 {
127 int fd, coe;
128 sigset_t ss;
129 int lc;
130
131 tst_parse_opts(argc, argv, NULL, NULL);
132 if ((tst_kvercmp(2, 6, 27)) < 0) {
133 tst_brkm(TCONF,
134 NULL,
135 "This test can only run on kernels that are 2.6.27 and higher");
136 }
137 setup();
138
139 for (lc = 0; TEST_LOOPING(lc); ++lc) {
140 tst_count = 0;
141 for (testno = 0; testno < TST_TOTAL; ++testno) {
142 sigemptyset(&ss);
143 sigaddset(&ss, SIGUSR1);
144 fd = ltp_syscall(__NR_signalfd4, -1, &ss,
145 SIGSETSIZE, 0);
146 if (fd == -1) {
147 tst_brkm(TFAIL, cleanup,
148 "signalfd4(0) failed");
149 }
150 coe = fcntl(fd, F_GETFD);
151 if (coe == -1) {
152 tst_brkm(TBROK, cleanup, "fcntl failed");
153 }
154 if (coe & FD_CLOEXEC) {
155 tst_brkm(TFAIL,
156 cleanup,
157 "signalfd4(0) set close-on-exec flag");
158 }
159 close(fd);
160
161 fd = ltp_syscall(__NR_signalfd4, -1, &ss, SIGSETSIZE,
162 SFD_CLOEXEC);
163 if (fd == -1) {
164 tst_brkm(TFAIL,
165 cleanup,
166 "signalfd4(SFD_CLOEXEC) failed");
167 }
168 coe = fcntl(fd, F_GETFD);
169 if (coe == -1) {
170 tst_brkm(TBROK, cleanup, "fcntl failed");
171 }
172 if ((coe & FD_CLOEXEC) == 0) {
173 tst_brkm(TFAIL,
174 cleanup,
175 "signalfd4(SFD_CLOEXEC) does not set close-on-exec flag");
176 }
177 close(fd);
178 tst_resm(TPASS, "signalfd4(SFD_CLOEXEC) Passed");
179 cleanup();
180 }
181 }
182 tst_exit();
183 }
184