1 /******************************************************************************/
2 /* */
3 /* Copyright (c) Ulrich Drepper <[email protected]> */
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 <[email protected]> */
52 /* */
53 /* History: Created - Jan 08 2009 - Ulrich Drepper <[email protected]> */
54 /* Ported to LTP */
55 /* - Jan 08 2009 - Subrata <[email protected]> */
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 setup();
133
134 for (lc = 0; TEST_LOOPING(lc); ++lc) {
135 tst_count = 0;
136 for (testno = 0; testno < TST_TOTAL; ++testno) {
137 sigemptyset(&ss);
138 sigaddset(&ss, SIGUSR1);
139 fd = tst_syscall(__NR_signalfd4, -1, &ss,
140 SIGSETSIZE, 0);
141 if (fd == -1) {
142 tst_brkm(TFAIL, cleanup,
143 "signalfd4(0) failed");
144 }
145 coe = fcntl(fd, F_GETFD);
146 if (coe == -1) {
147 tst_brkm(TBROK, cleanup, "fcntl failed");
148 }
149 if (coe & FD_CLOEXEC) {
150 tst_brkm(TFAIL,
151 cleanup,
152 "signalfd4(0) set close-on-exec flag");
153 }
154 close(fd);
155
156 fd = tst_syscall(__NR_signalfd4, -1, &ss, SIGSETSIZE,
157 SFD_CLOEXEC);
158 if (fd == -1) {
159 tst_brkm(TFAIL,
160 cleanup,
161 "signalfd4(SFD_CLOEXEC) failed");
162 }
163 coe = fcntl(fd, F_GETFD);
164 if (coe == -1) {
165 tst_brkm(TBROK, cleanup, "fcntl failed");
166 }
167 if ((coe & FD_CLOEXEC) == 0) {
168 tst_brkm(TFAIL,
169 cleanup,
170 "signalfd4(SFD_CLOEXEC) does not set close-on-exec flag");
171 }
172 close(fd);
173 tst_resm(TPASS, "signalfd4(SFD_CLOEXEC) Passed");
174 cleanup();
175 }
176 }
177 tst_exit();
178 }
179