1f08c3bdfSopenharmony_ci/******************************************************************************/ 2f08c3bdfSopenharmony_ci/* */ 3f08c3bdfSopenharmony_ci/* Copyright (c) Ulrich Drepper <drepper@redhat.com> */ 4f08c3bdfSopenharmony_ci/* Copyright (c) International Business Machines Corp., 2009 */ 5f08c3bdfSopenharmony_ci/* */ 6f08c3bdfSopenharmony_ci/* This program is free software; you can redistribute it and/or modify */ 7f08c3bdfSopenharmony_ci/* it under the terms of the GNU General Public License as published by */ 8f08c3bdfSopenharmony_ci/* the Free Software Foundation; either version 2 of the License, or */ 9f08c3bdfSopenharmony_ci/* (at your option) any later version. */ 10f08c3bdfSopenharmony_ci/* */ 11f08c3bdfSopenharmony_ci/* This program is distributed in the hope that it will be useful, */ 12f08c3bdfSopenharmony_ci/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 13f08c3bdfSopenharmony_ci/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */ 14f08c3bdfSopenharmony_ci/* the GNU General Public License for more details. */ 15f08c3bdfSopenharmony_ci/* */ 16f08c3bdfSopenharmony_ci/* You should have received a copy of the GNU General Public License */ 17f08c3bdfSopenharmony_ci/* along with this program; if not, write to the Free Software */ 18f08c3bdfSopenharmony_ci/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ 19f08c3bdfSopenharmony_ci/* */ 20f08c3bdfSopenharmony_ci/******************************************************************************/ 21f08c3bdfSopenharmony_ci/******************************************************************************/ 22f08c3bdfSopenharmony_ci/* */ 23f08c3bdfSopenharmony_ci/* File: signalfd4_01.c */ 24f08c3bdfSopenharmony_ci/* */ 25f08c3bdfSopenharmony_ci/* Description: This Program tests the new system call introduced in 2.6.27. */ 26f08c3bdfSopenharmony_ci/* Ulrich´s comment as in: */ 27f08c3bdfSopenharmony_ci/* http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=9deb27baedb79759c3ab9435a7d8b841842d56e9 */ 28f08c3bdfSopenharmony_ci/* says: */ 29f08c3bdfSopenharmony_ci/* This patch adds the new signalfd4 syscall. It extends the old signalfd */ 30f08c3bdfSopenharmony_ci/* syscall by one parameter which is meant to hold a flag value. In this */ 31f08c3bdfSopenharmony_ci/* patch the only flag support is SFD_CLOEXEC which causes the close-on-exec */ 32f08c3bdfSopenharmony_ci/* flag for the returned file descriptor to be set. A new name SFD_CLOEXEC is */ 33f08c3bdfSopenharmony_ci/* introduced which in this implementation must have the same value as */ 34f08c3bdfSopenharmony_ci/* O_CLOEXEC */ 35f08c3bdfSopenharmony_ci/* The following test must be adjusted for architectures other than x86 and */ 36f08c3bdfSopenharmony_ci/* x86-64 and in case the syscall numbers changed. */ 37f08c3bdfSopenharmony_ci/* */ 38f08c3bdfSopenharmony_ci/* Usage: <for command-line> */ 39f08c3bdfSopenharmony_ci/* signalfd4_01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */ 40f08c3bdfSopenharmony_ci/* where, -c n : Run n copies concurrently. */ 41f08c3bdfSopenharmony_ci/* -e : Turn on errno logging. */ 42f08c3bdfSopenharmony_ci/* -i n : Execute test n times. */ 43f08c3bdfSopenharmony_ci/* -I x : Execute test for x seconds. */ 44f08c3bdfSopenharmony_ci/* -P x : Pause for x seconds between iterations. */ 45f08c3bdfSopenharmony_ci/* -t : Turn on syscall timing. */ 46f08c3bdfSopenharmony_ci/* */ 47f08c3bdfSopenharmony_ci/* Total Tests: 1 */ 48f08c3bdfSopenharmony_ci/* */ 49f08c3bdfSopenharmony_ci/* Test Name: signalfd4_01 */ 50f08c3bdfSopenharmony_ci/* */ 51f08c3bdfSopenharmony_ci/* Author: Ulrich Drepper <drepper@redhat.com> */ 52f08c3bdfSopenharmony_ci/* */ 53f08c3bdfSopenharmony_ci/* History: Created - Jan 08 2009 - Ulrich Drepper <drepper@redhat.com> */ 54f08c3bdfSopenharmony_ci/* Ported to LTP */ 55f08c3bdfSopenharmony_ci/* - Jan 08 2009 - Subrata <subrata@linux.vnet.ibm.com> */ 56f08c3bdfSopenharmony_ci/******************************************************************************/ 57f08c3bdfSopenharmony_ci#include <signal.h> 58f08c3bdfSopenharmony_ci#include <stdio.h> 59f08c3bdfSopenharmony_ci#include <unistd.h> 60f08c3bdfSopenharmony_ci#include <sys/syscall.h> 61f08c3bdfSopenharmony_ci#include <errno.h> 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_ci#include "test.h" 64f08c3bdfSopenharmony_ci#include "lapi/fcntl.h" 65f08c3bdfSopenharmony_ci#include "lapi/syscalls.h" 66f08c3bdfSopenharmony_ci#include "ltp_signal.h" 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci#define SFD_CLOEXEC O_CLOEXEC 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_cichar *TCID = "signalfd4_01"; 71f08c3bdfSopenharmony_ciint testno; 72f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci/* Extern Global Functions */ 75f08c3bdfSopenharmony_ci/******************************************************************************/ 76f08c3bdfSopenharmony_ci/* */ 77f08c3bdfSopenharmony_ci/* Function: cleanup */ 78f08c3bdfSopenharmony_ci/* */ 79f08c3bdfSopenharmony_ci/* Description: Performs all one time clean up for this test on successful */ 80f08c3bdfSopenharmony_ci/* completion, premature exit or failure. Closes all temporary */ 81f08c3bdfSopenharmony_ci/* files, removes all temporary directories exits the test with */ 82f08c3bdfSopenharmony_ci/* appropriate return code by calling tst_exit() function. */ 83f08c3bdfSopenharmony_ci/* */ 84f08c3bdfSopenharmony_ci/* Input: None. */ 85f08c3bdfSopenharmony_ci/* */ 86f08c3bdfSopenharmony_ci/* Output: None. */ 87f08c3bdfSopenharmony_ci/* */ 88f08c3bdfSopenharmony_ci/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */ 89f08c3bdfSopenharmony_ci/* On success - Exits calling tst_exit(). With '0' return code. */ 90f08c3bdfSopenharmony_ci/* */ 91f08c3bdfSopenharmony_ci/******************************************************************************/ 92f08c3bdfSopenharmony_civoid cleanup(void) 93f08c3bdfSopenharmony_ci{ 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci tst_rmdir(); 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci} 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci/* Local Functions */ 100f08c3bdfSopenharmony_ci/******************************************************************************/ 101f08c3bdfSopenharmony_ci/* */ 102f08c3bdfSopenharmony_ci/* Function: setup */ 103f08c3bdfSopenharmony_ci/* */ 104f08c3bdfSopenharmony_ci/* Description: Performs all one time setup for this test. This function is */ 105f08c3bdfSopenharmony_ci/* typically used to capture signals, create temporary dirs */ 106f08c3bdfSopenharmony_ci/* and temporary files that may be used in the course of this */ 107f08c3bdfSopenharmony_ci/* test. */ 108f08c3bdfSopenharmony_ci/* */ 109f08c3bdfSopenharmony_ci/* Input: None. */ 110f08c3bdfSopenharmony_ci/* */ 111f08c3bdfSopenharmony_ci/* Output: None. */ 112f08c3bdfSopenharmony_ci/* */ 113f08c3bdfSopenharmony_ci/* Return: On failure - Exits by calling cleanup(). */ 114f08c3bdfSopenharmony_ci/* On success - returns 0. */ 115f08c3bdfSopenharmony_ci/* */ 116f08c3bdfSopenharmony_ci/******************************************************************************/ 117f08c3bdfSopenharmony_civoid setup(void) 118f08c3bdfSopenharmony_ci{ 119f08c3bdfSopenharmony_ci /* Capture signals if any */ 120f08c3bdfSopenharmony_ci /* Create temporary directories */ 121f08c3bdfSopenharmony_ci TEST_PAUSE; 122f08c3bdfSopenharmony_ci tst_tmpdir(); 123f08c3bdfSopenharmony_ci} 124f08c3bdfSopenharmony_ci 125f08c3bdfSopenharmony_ciint main(int argc, char *argv[]) 126f08c3bdfSopenharmony_ci{ 127f08c3bdfSopenharmony_ci int fd, coe; 128f08c3bdfSopenharmony_ci sigset_t ss; 129f08c3bdfSopenharmony_ci int lc; 130f08c3bdfSopenharmony_ci 131f08c3bdfSopenharmony_ci tst_parse_opts(argc, argv, NULL, NULL); 132f08c3bdfSopenharmony_ci setup(); 133f08c3bdfSopenharmony_ci 134f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); ++lc) { 135f08c3bdfSopenharmony_ci tst_count = 0; 136f08c3bdfSopenharmony_ci for (testno = 0; testno < TST_TOTAL; ++testno) { 137f08c3bdfSopenharmony_ci sigemptyset(&ss); 138f08c3bdfSopenharmony_ci sigaddset(&ss, SIGUSR1); 139f08c3bdfSopenharmony_ci fd = tst_syscall(__NR_signalfd4, -1, &ss, 140f08c3bdfSopenharmony_ci SIGSETSIZE, 0); 141f08c3bdfSopenharmony_ci if (fd == -1) { 142f08c3bdfSopenharmony_ci tst_brkm(TFAIL, cleanup, 143f08c3bdfSopenharmony_ci "signalfd4(0) failed"); 144f08c3bdfSopenharmony_ci } 145f08c3bdfSopenharmony_ci coe = fcntl(fd, F_GETFD); 146f08c3bdfSopenharmony_ci if (coe == -1) { 147f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "fcntl failed"); 148f08c3bdfSopenharmony_ci } 149f08c3bdfSopenharmony_ci if (coe & FD_CLOEXEC) { 150f08c3bdfSopenharmony_ci tst_brkm(TFAIL, 151f08c3bdfSopenharmony_ci cleanup, 152f08c3bdfSopenharmony_ci "signalfd4(0) set close-on-exec flag"); 153f08c3bdfSopenharmony_ci } 154f08c3bdfSopenharmony_ci close(fd); 155f08c3bdfSopenharmony_ci 156f08c3bdfSopenharmony_ci fd = tst_syscall(__NR_signalfd4, -1, &ss, SIGSETSIZE, 157f08c3bdfSopenharmony_ci SFD_CLOEXEC); 158f08c3bdfSopenharmony_ci if (fd == -1) { 159f08c3bdfSopenharmony_ci tst_brkm(TFAIL, 160f08c3bdfSopenharmony_ci cleanup, 161f08c3bdfSopenharmony_ci "signalfd4(SFD_CLOEXEC) failed"); 162f08c3bdfSopenharmony_ci } 163f08c3bdfSopenharmony_ci coe = fcntl(fd, F_GETFD); 164f08c3bdfSopenharmony_ci if (coe == -1) { 165f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "fcntl failed"); 166f08c3bdfSopenharmony_ci } 167f08c3bdfSopenharmony_ci if ((coe & FD_CLOEXEC) == 0) { 168f08c3bdfSopenharmony_ci tst_brkm(TFAIL, 169f08c3bdfSopenharmony_ci cleanup, 170f08c3bdfSopenharmony_ci "signalfd4(SFD_CLOEXEC) does not set close-on-exec flag"); 171f08c3bdfSopenharmony_ci } 172f08c3bdfSopenharmony_ci close(fd); 173f08c3bdfSopenharmony_ci tst_resm(TPASS, "signalfd4(SFD_CLOEXEC) Passed"); 174f08c3bdfSopenharmony_ci cleanup(); 175f08c3bdfSopenharmony_ci } 176f08c3bdfSopenharmony_ci } 177f08c3bdfSopenharmony_ci tst_exit(); 178f08c3bdfSopenharmony_ci} 179