[3d8666]: testcases / kernel / syscalls / signalfd / signalfd01.c  Maximize  Restore  History

Download this file

366 lines (311 with data), 7.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
*
* Copyright (c) Red Hat Inc., 2008
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* NAME
* signalfd01.c
*
* DESCRIPTION
* Check signalfd can receive signals
*
* USAGE
* signalfd01
*
* HISTORY
* 9/2008 Initial version by Masatake YAMATO <yamato@redhat.com>
*
* RESTRICTIONS
* None
*/
# define _GNU_SOURCE
#include "config.h"
#include "test.h"
#include "usctest.h"
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
TCID_DEFINE(signalfd01);
int TST_TOTAL = 1;
extern int Tst_count;
#ifdef HAVE_LINUX_TYPES_H
#include <linux/types.h>
#endif
#if defined HAVE_SYS_SIGNALFD_H
#include <sys/signalfd.h>
#elif defined HAVE_LINUX_SIGNALFD_H
#include <linux/signalfd.h>
#elif defined HAVE_SIGNALFD_H
#include <signalfd.h>
#else
#define USE_STUB
#endif
#ifndef HAVE_SIGNALFD
#include "linux_syscall_numbers.h"
#ifndef __NR_signalfd
#define __NR_signalfd 0
#endif
int
signalfd(int fd, const sigset_t *mask, int flags)
{
/* Taken from GLIBC. */
return (syscall(__NR_signalfd, fd, mask, _NSIG / 8));
}
#endif
#if defined HAVE_SIGNALFD_SIGINFO_SSI_SIGNO
#define LTP_SYSCALL_SIGNALFD_FIELD_PREFIX(FIELD) ssi_##FIELD
#elif defined HAVE_SIGNALFD_SIGINFO_SIGNO
#define LTP_SYSCALL_SIGNALFD_FIELD_PREFIX(FIELD) FIELD
#else
#define USE_STUB
#endif
#ifdef USE_STUB
int
main(int argc, char** argv)
{
tst_resm(TCONF,
"System doesn't support execution of the test");
return 0;
}
#else
void cleanup(void);
void setup(void);
int
do_test1(int ntst, int sig)
{
int sfd_for_next;
int sfd;
sigset_t mask;
pid_t pid;
struct signalfd_siginfo fdsi;
ssize_t s;
sigemptyset(&mask);
sigaddset(&mask, sig);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
tst_brkm(TBROK, cleanup,
"sigprocmask() Failed: errno=%d : %s",
errno,
strerror(errno));
}
TEST(signalfd(-1, &mask, 0));
if ((sfd = TEST_RETURN) == -1) {
tst_resm(TFAIL,
"signalfd() Failed, errno=%d : %s",
TEST_ERRNO, strerror(TEST_ERRNO));
sfd_for_next = -1;
return sfd_for_next;
} else if (!STD_FUNCTIONAL_TEST) {
tst_resm(TPASS, "signalfd is created successfully");
sfd_for_next = sfd;
goto out;
}
if (fcntl(sfd, F_SETFL, O_NONBLOCK) == -1) {
close(sfd);
tst_brkm(TBROK, cleanup,
"setting signalfd nonblocking mode failed: errno=%d : %s",
errno,
strerror(errno));
}
pid = getpid();
if (kill(pid, sig) == -1) {
close(sfd);
tst_brkm(TBROK, cleanup,
"kill(self, %s) failed: errno=%d : %s",
strsignal(sig),
errno,
strerror(errno));
}
s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
if ((s > 0) && (s != sizeof(struct signalfd_siginfo))) {
tst_resm(TFAIL,
"getting incomplete signalfd_siginfo data: "
"actual-size=%d, expected-size= %d",
s, sizeof(struct signalfd_siginfo));
sfd_for_next = -1;
close(sfd);
goto out;
}
else if (s < 0) {
if (errno == EAGAIN) {
tst_resm(TFAIL,
"signalfd_siginfo data is not delivered yet");
sfd_for_next = -1;
close(sfd);
goto out;
} else {
close(sfd);
tst_brkm(TBROK, cleanup,
"read signalfd_siginfo data failed: errno=%d : %s",
errno,
strerror(errno));
}
}
else if (s == 0) {
tst_resm(TFAIL, "got EOF unexpectedly");
sfd_for_next = -1;
close(sfd);
goto out;
}
if (fdsi.LTP_SYSCALL_SIGNALFD_FIELD_PREFIX(signo) == sig) {
tst_resm(TPASS, "got expected signal");
sfd_for_next = sfd;
goto out;
}
else {
tst_resm(TFAIL, "got unexpected signal: signal=%d : %s",
fdsi.LTP_SYSCALL_SIGNALFD_FIELD_PREFIX(signo),
strsignal(fdsi.LTP_SYSCALL_SIGNALFD_FIELD_PREFIX(signo)));
sfd_for_next = -1;
close(sfd);
goto out;
}
out:
return sfd_for_next;
}
void
do_test2(int ntst, int fd, int sig)
{
int sfd;
sigset_t mask;
pid_t pid;
struct signalfd_siginfo fdsi;
ssize_t s;
sigemptyset(&mask);
sigaddset(&mask, sig);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
close(fd);
tst_brkm(TBROK, cleanup,
"sigprocmask() Failed: errno=%d : %s",
errno,
strerror(errno));
}
TEST(signalfd(fd, &mask, 0));
if ((sfd = TEST_RETURN) == -1) {
tst_resm(TFAIL,
"reassignment the file descriptor by signalfd() failed, errno=%d : %s",
TEST_ERRNO, strerror(TEST_ERRNO));
return;
} else if (sfd != fd) {
tst_resm(TFAIL,
"different fd is returned in reassignment: expected-fd=%d, actual-fd=%d",
fd, sfd);
close(sfd);
return;
} else if (!STD_FUNCTIONAL_TEST) {
tst_resm(TPASS, "signalfd is successfully reassigned");
goto out;
}
pid = getpid();
if (kill(pid, sig) == -1) {
close(sfd);
tst_brkm(TBROK, cleanup,
"kill(self, %s) failed: errno=%d : %s",
strsignal(sig),
errno,
strerror(errno));
}
s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
if ((s > 0) && (s != sizeof(struct signalfd_siginfo))) {
tst_resm(TFAIL,
"getting incomplete signalfd_siginfo data: "
"actual-size=%d, expected-size= %d",
s, sizeof(struct signalfd_siginfo));
goto out;
}
else if (s < 0) {
if (errno == EAGAIN) {
tst_resm(TFAIL,
"signalfd_siginfo data is not delivered yet");
goto out;
} else {
close(sfd);
tst_brkm(TBROK, cleanup,
"read signalfd_siginfo data failed: errno=%d : %s",
errno,
strerror(errno));
}
}
else if (s == 0) {
tst_resm(TFAIL, "got EOF unexpectedly");
goto out;
}
if (fdsi.LTP_SYSCALL_SIGNALFD_FIELD_PREFIX(signo) == sig) {
tst_resm(TPASS, "got expected signal");
goto out;
}
else {
tst_resm(TFAIL, "got unexpected signal: signal=%d : %s",
fdsi.LTP_SYSCALL_SIGNALFD_FIELD_PREFIX(signo),
strsignal(fdsi.LTP_SYSCALL_SIGNALFD_FIELD_PREFIX(signo)));
goto out;
}
out:
return;
}
int
main(int argc, char** argv)
{
int lc;
char *msg; /* message returned from parse_opts */
int sfd;
if((tst_kvercmp(2, 6, 22)) < 0)
{
tst_resm(TWARN, "This test can only run on kernels that are 2.6.22 and higher");
exit(0);
}
if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
}
setup ();
for (lc = 0; TEST_LOOPING(lc); lc++) {
Tst_count = 0;
sfd = do_test1(lc, SIGUSR1);
if (sfd < 0)
continue;
do_test2(lc, sfd, SIGUSR2);
close(sfd);
}
cleanup();
return 0;
}
/*
* setup() - performs all the ONE TIME setup for this test.
*/
void
setup(void)
{
/* Pause if that option was specified */
TEST_PAUSE;
}
/*
* cleanup() - performs all the ONE TIME cleanup for this test at completion
* or premature exit.
*/
void
cleanup(void)
{
/*
* print timing stats if that option was specified.
* print errno log if that option was specified.
*/
TEST_CLEANUP;
/* exit with return code appropriate for results */
tst_exit();
}
#endif

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks