Implement pthread_sigqueue.

Bug: http://b/112770187
Test: bionic-unit-tests
Change-Id: I03382cd5df2490b2e87265dba9007e2cb1b14cd2
This commit is contained in:
Josh Gao 2018-08-27 16:00:58 -07:00
parent f3aa3007e3
commit 726b63f725
11 changed files with 99 additions and 0 deletions

View file

@ -1456,6 +1456,7 @@ cc_library_static {
"bionic/pthread_mutex.cpp", "bionic/pthread_mutex.cpp",
"bionic/pthread_once.cpp", "bionic/pthread_once.cpp",
"bionic/pthread_rwlock.cpp", "bionic/pthread_rwlock.cpp",
"bionic/pthread_sigqueue.cpp",
"bionic/pthread_self.cpp", "bionic/pthread_self.cpp",
"bionic/pthread_setname_np.cpp", "bionic/pthread_setname_np.cpp",
"bionic/pthread_setschedparam.cpp", "bionic/pthread_setschedparam.cpp",

View file

@ -0,0 +1,51 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "private/ErrnoRestorer.h"
#include "pthread_internal.h"
int pthread_sigqueue(pthread_t t, int sig, const union sigval value) {
ErrnoRestorer errno_restorer;
pid_t tid = pthread_gettid_np(t);
if (tid == -1) return ESRCH;
siginfo_t siginfo;
siginfo.si_code = SI_QUEUE;
siginfo.si_pid = getpid();
siginfo.si_uid = getuid();
siginfo.si_value = value;
return syscall(__NR_rt_tgsigqueueinfo, getpid(), tid, sig, &siginfo) ? errno : 0;
}

View file

@ -113,6 +113,10 @@ void psiginfo(const siginfo_t* __info, const char* __msg) __INTRODUCED_IN(17);
void psignal(int __signal, const char* __msg) __INTRODUCED_IN(17); void psignal(int __signal, const char* __msg) __INTRODUCED_IN(17);
int pthread_kill(pthread_t __pthread, int __signal); int pthread_kill(pthread_t __pthread, int __signal);
#if defined(__USE_GNU)
int pthread_sigqueue(pthread_t __pthread, int __signal, const union sigval __value) __INTRODUCED_IN(__ANDROID_API_Q__);
#endif
int pthread_sigmask(int __how, const sigset_t* __new_set, sigset_t* __old_set); int pthread_sigmask(int __how, const sigset_t* __new_set, sigset_t* __old_set);
int pthread_sigmask64(int __how, const sigset64_t* __new_set, sigset64_t* __old_set) __INTRODUCED_IN(28); int pthread_sigmask64(int __how, const sigset64_t* __new_set, sigset64_t* __old_set) __INTRODUCED_IN(28);

View file

@ -1431,6 +1431,7 @@ LIBC_Q { # introduced=Q
android_fdsan_get_tag_value; android_fdsan_get_tag_value;
android_fdsan_get_error_level; android_fdsan_get_error_level;
android_fdsan_set_error_level; android_fdsan_set_error_level;
pthread_sigqueue;
timespec_get; timespec_get;
} LIBC_P; } LIBC_P;

View file

@ -1352,6 +1352,7 @@ LIBC_Q { # introduced=Q
android_fdsan_get_tag_value; android_fdsan_get_tag_value;
android_fdsan_get_error_level; android_fdsan_get_error_level;
android_fdsan_set_error_level; android_fdsan_set_error_level;
pthread_sigqueue;
timespec_get; timespec_get;
} LIBC_P; } LIBC_P;

View file

@ -1456,6 +1456,7 @@ LIBC_Q { # introduced=Q
android_fdsan_get_tag_value; android_fdsan_get_tag_value;
android_fdsan_get_error_level; android_fdsan_get_error_level;
android_fdsan_set_error_level; android_fdsan_set_error_level;
pthread_sigqueue;
timespec_get; timespec_get;
} LIBC_P; } LIBC_P;

View file

@ -1415,6 +1415,7 @@ LIBC_Q { # introduced=Q
android_fdsan_get_tag_value; android_fdsan_get_tag_value;
android_fdsan_get_error_level; android_fdsan_get_error_level;
android_fdsan_set_error_level; android_fdsan_set_error_level;
pthread_sigqueue;
timespec_get; timespec_get;
} LIBC_P; } LIBC_P;

View file

@ -1352,6 +1352,7 @@ LIBC_Q { # introduced=Q
android_fdsan_get_tag_value; android_fdsan_get_tag_value;
android_fdsan_get_error_level; android_fdsan_get_error_level;
android_fdsan_set_error_level; android_fdsan_set_error_level;
pthread_sigqueue;
timespec_get; timespec_get;
} LIBC_P; } LIBC_P;

View file

@ -1413,6 +1413,7 @@ LIBC_Q { # introduced=Q
android_fdsan_get_tag_value; android_fdsan_get_tag_value;
android_fdsan_get_error_level; android_fdsan_get_error_level;
android_fdsan_set_error_level; android_fdsan_set_error_level;
pthread_sigqueue;
timespec_get; timespec_get;
} LIBC_P; } LIBC_P;

View file

@ -1352,6 +1352,7 @@ LIBC_Q { # introduced=Q
android_fdsan_get_tag_value; android_fdsan_get_tag_value;
android_fdsan_get_error_level; android_fdsan_get_error_level;
android_fdsan_set_error_level; android_fdsan_set_error_level;
pthread_sigqueue;
timespec_get; timespec_get;
} LIBC_P; } LIBC_P;

View file

@ -597,6 +597,42 @@ TEST(signal, sigqueue) {
ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
} }
TEST(signal, pthread_sigqueue_self) {
ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
sigval_t sigval;
sigval.sival_int = 1;
errno = 0;
ASSERT_EQ(0, pthread_sigqueue(pthread_self(), SIGALRM, sigval));
ASSERT_EQ(0, errno);
ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
}
TEST(signal, pthread_sigqueue_other) {
ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
sigval_t sigval;
sigval.sival_int = 1;
sigset_t mask;
sigfillset(&mask);
pthread_sigmask(SIG_SETMASK, &mask, nullptr);
pthread_t thread;
int rc = pthread_create(&thread, nullptr,
[](void*) -> void* {
sigset_t mask;
sigemptyset(&mask);
sigsuspend(&mask);
return nullptr;
},
nullptr);
ASSERT_EQ(0, rc);
errno = 0;
ASSERT_EQ(0, pthread_sigqueue(thread, SIGALRM, sigval));
ASSERT_EQ(0, errno);
pthread_join(thread, nullptr);
ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
}
TEST(signal, sigwaitinfo) { TEST(signal, sigwaitinfo) {
SignalMaskRestorer smr; SignalMaskRestorer smr;