1c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes/* 25905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes * Copyright (C) 2018 The Android Open Source Project 3c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * All rights reserved. 4c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * 5c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * Redistribution and use in source and binary forms, with or without 6c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * modification, are permitted provided that the following conditions 7c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * are met: 8c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * * Redistributions of source code must retain the above copyright 9c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * notice, this list of conditions and the following disclaimer. 10c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * * Redistributions in binary form must reproduce the above copyright 11c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * notice, this list of conditions and the following disclaimer in 12c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * the documentation and/or other materials provided with the 13c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * distribution. 14c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * 15c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes * SUCH DAMAGE. 27c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes */ 28c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes 295905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes#include <gtest/gtest.h> 305905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 31c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes#include <errno.h> 325905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes#include <fcntl.h> 33c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes#include <signal.h> 345905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes#include <sys/signalfd.h> 355905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes#include <unistd.h> 365905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 375905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes#include <thread> 385905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 395905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes#include "ScopedSignalHandler.h" 405905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 415905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughesstatic void TestSignalFd(int fd, int signal) { 425905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes ASSERT_NE(-1, fd) << strerror(errno); 435905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 445905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes ASSERT_EQ(0, raise(signal)); 455905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 465905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes signalfd_siginfo sfd_si; 475905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes ASSERT_EQ(static_cast<ssize_t>(sizeof(sfd_si)), read(fd, &sfd_si, sizeof(sfd_si))); 485905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 495905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes ASSERT_EQ(signal, static_cast<int>(sfd_si.ssi_signo)); 505905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 515905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes close(fd); 525905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes} 535905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 545905d6f8797056ca4178d42bf1220b6692e557a5Elliott HughesTEST(sys_signalfd, signalfd) { 555905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes SignalMaskRestorer smr; 565905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 575905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes sigset_t mask = {}; 585905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes sigaddset(&mask, SIGALRM); 595905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &mask, nullptr)); 605905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 615905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes TestSignalFd(signalfd(-1, &mask, SFD_CLOEXEC), SIGALRM); 625905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes} 635905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes 645905d6f8797056ca4178d42bf1220b6692e557a5Elliott HughesTEST(sys_signalfd, signalfd64) { 655905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes#if defined(__BIONIC__) 665905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes SignalMaskRestorer smr; 67c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes 685905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes sigset64_t mask = {}; 695905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes sigaddset64(&mask, SIGRTMIN); 705905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes ASSERT_EQ(0, sigprocmask64(SIG_SETMASK, &mask, nullptr)); 71c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes 725905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes TestSignalFd(signalfd64(-1, &mask, SFD_CLOEXEC), SIGRTMIN); 735905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes#endif 74c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes} 75