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