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_02.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=5fb5e04926a54bc1c22bba7ca166840f4476196f */
28 /* which says:                                                                */
29 /* This patch adds support for the SFD_NONBLOCK flag to signalfd4.  The       */
30 /* additional changes needed are minimal. The following test must be adjusted */
31 /* for architectures other than x86 and x86-64 and in case the syscall numbers*/
32 /*  changed.                                                                  */
33 /*                                                                            */
34 /* Usage:  <for command-line>                                                 */
35 /* signalfd4_02 [-c n] [-e][-i n] [-I x] [-p x] [-t]                          */
36 /*      where,  -c n : Run n copies concurrently.                             */
37 /*              -e   : Turn on errno logging.                                 */
38 /*              -i n : Execute test n times.                                  */
39 /*              -I x : Execute test for x seconds.                            */
40 /*              -P x : Pause for x seconds between iterations.                */
41 /*              -t   : Turn on syscall timing.                                */
42 /*                                                                            */
43 /* Total Tests: 1                                                             */
44 /*                                                                            */
45 /* Test Name:   signalfd4_02                                                  */
46 /*                                                                            */
47 /* Author:      Ulrich Drepper <drepper@redhat.com>                           */
48 /*                                                                            */
49 /* History:     Created - Jan 13 2009 - Ulrich Drepper <drepper@redhat.com>   */
50 /*              Ported to LTP                                                 */
51 /*                      - Jan 13 2009 - Subrata <subrata@linux.vnet.ibm.com>  */
52 /******************************************************************************/
53 #include <fcntl.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <unistd.h>
57 #include <sys/syscall.h>
58 #include <errno.h>
59 
60 #include "test.h"
61 #include "lapi/syscalls.h"
62 #include "ltp_signal.h"
63 
64 #define SFD_NONBLOCK O_NONBLOCK
65 
66 char *TCID = "signalfd4_02";
67 int testno;
68 int TST_TOTAL = 1;
69 
70 /* Extern Global Functions */
71 /******************************************************************************/
72 /*                                                                            */
73 /* Function:    cleanup                                                       */
74 /*                                                                            */
75 /* Description: Performs all one time clean up for this test on successful    */
76 /*              completion,  premature exit or  failure. Closes all temporary */
77 /*              files, removes all temporary directories exits the test with  */
78 /*              appropriate return code by calling tst_exit() function.       */
79 /*                                                                            */
80 /* Input:       None.                                                         */
81 /*                                                                            */
82 /* Output:      None.                                                         */
83 /*                                                                            */
84 /* Return:      On failure - Exits calling tst_exit(). Non '0' return code.   */
85 /*              On success - Exits calling tst_exit(). With '0' return code.  */
86 /*                                                                            */
87 /******************************************************************************/
cleanup(void)88 void cleanup(void)
89 {
90 
91 	tst_rmdir();
92 
93 }
94 
95 /* Local  Functions */
96 /******************************************************************************/
97 /*                                                                            */
98 /* Function:    setup                                                         */
99 /*                                                                            */
100 /* Description: Performs all one time setup for this test. This function is   */
101 /*              typically used to capture signals, create temporary dirs      */
102 /*              and temporary files that may be used in the course of this    */
103 /*              test.                                                         */
104 /*                                                                            */
105 /* Input:       None.                                                         */
106 /*                                                                            */
107 /* Output:      None.                                                         */
108 /*                                                                            */
109 /* Return:      On failure - Exits by calling cleanup().                      */
110 /*              On success - returns 0.                                       */
111 /*                                                                            */
112 /******************************************************************************/
setup(void)113 void setup(void)
114 {
115 	/* Capture signals if any */
116 	/* Create temporary directories */
117 	TEST_PAUSE;
118 	tst_tmpdir();
119 }
120 
main(int argc,char * argv[])121 int main(int argc, char *argv[])
122 {
123 	sigset_t ss;
124 	int fd, fl;
125 	int lc;
126 
127 	tst_parse_opts(argc, argv, NULL, NULL);
128 	if ((tst_kvercmp(2, 6, 27)) < 0) {
129 		tst_brkm(TCONF,
130 			 NULL,
131 			 "This test can only run on kernels that are 2.6.27 and higher");
132 	}
133 	setup();
134 
135 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
136 		tst_count = 0;
137 		for (testno = 0; testno < TST_TOTAL; ++testno) {
138 			sigemptyset(&ss);
139 			sigaddset(&ss, SIGUSR1);
140 			fd = ltp_syscall(__NR_signalfd4, -1, &ss,
141 				SIGSETSIZE, 0);
142 			if (fd == -1) {
143 				tst_brkm(TFAIL, cleanup,
144 					 "signalfd4(0) failed");
145 			}
146 			fl = fcntl(fd, F_GETFL);
147 			if (fl == -1) {
148 				tst_brkm(TBROK, cleanup, "fcntl failed");
149 			}
150 			if (fl & O_NONBLOCK) {
151 				tst_brkm(TFAIL,
152 					 cleanup,
153 					 "signalfd4(0) set non-blocking mode");
154 			}
155 			close(fd);
156 
157 			fd = ltp_syscall(__NR_signalfd4, -1, &ss, SIGSETSIZE,
158 				     SFD_NONBLOCK);
159 			if (fd == -1) {
160 				tst_brkm(TFAIL,
161 					 cleanup,
162 					 "signalfd4(SFD_NONBLOCK) failed");
163 			}
164 			fl = fcntl(fd, F_GETFL);
165 			if (fl == -1) {
166 				tst_brkm(TBROK, cleanup, "fcntl failed");
167 			}
168 			if ((fl & O_NONBLOCK) == 0) {
169 				tst_brkm(TFAIL,
170 					 cleanup,
171 					 "signalfd4(SFD_NONBLOCK) does not set non-blocking mode");
172 			}
173 			close(fd);
174 			tst_resm(TPASS, "signalfd4(SFD_NONBLOCK) PASSED");
175 			cleanup();
176 		}
177 	}
178 	tst_exit();
179 }
180