signals: add rt_tgsigqueueinfo syscall
cover signals: add rt_tgsigqueueinfo syscall
sys_kill has a counterpart sys_tgkill which allows to send signals to a particular thread. sys_rt_sigqueueinfo is lacking such a counterpart.
Aside of the asymetry it is a show stopper for migrating applications from other unix-alike RTOSes.
The following patch series implements rt_tgsigqueueinfo and hooks it up for x86.
Find below the raw documentation.
Thanks,
tglx
----
NAME
rt_tgsigqueueinfo - Send signal information to a signal to a thread
SYNOPSIS
long sys_rt_tgsigqueueinfo (int tgid, int tid, int sig, siginfo_t *uinfo);
DESCRIPTION
rt_tgsigqueueinfo sends signal sig information uinfo to the thread with the thread ID tid in the thread group tgid. (By contrast, rt_sigqueueinfo(2) can only be used to send a signal info to a process (i.e., thread group) as a whole, and the signal will be delivered to an arbitrary thread within that process.)
RETURN VALUE
rt_tgsigqueueinfo returns 0 on success; otherwise, rt_sigqueueinfo returns one of the errors listed in the "Errors" section.
ERRORS
-EFAULT
An invalid value for uinfo was specified.
-EINVAL
An invalid TID, TGID or signal was specified.
-EPERM
Permission denied. For the required permissions, see rt_sigqueueinfo(2).
-ESRCH
No process with the specified thread ID and thread group ID exists.
RFC PATCH 1/3 signals: split do_tkill
Split out the code from do_tkill to make it reusable by the follow up patch which implements sys_rt_tgsigqueueinfo
--- kernel/signal.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-)
kernel/signal.c +20 -11
@@ -2212,24 +2212,20 @@ sys_kill(pid_t pid, int sig)return kill_something_info (sig, &info, pid); } -static int do_tkill (pid_t tgid,pid_t pid,int sig) +static int +do_send_specific (pid_t tgid,pid_t pid,int sig,struct siginfo *info) { -int error; -struct siginfo info;struct task_struct *p;unsigned long flags; +int error = -ESRCH ; - error = -ESRCH ; - info.si_signo = sig; - info.si_errno =0 ; - info.si_code =SI_TKILL ; - info.si_pid =task_tgid_vnr (current); - info.si_uid = current->uid; + info->si_pid =task_tgid_vnr (current); + info->si_uid = current->uid;rcu_read_lock (); p =find_task_by_vpid (pid);if (p && (tgid <=0 ||task_tgid_vnr (p) == tgid)) { - error =check_kill_permission (sig, &info, p); + error =check_kill_permission (sig, info, p);/* * The null signal is a permissions and process existence * probe. No signal is actually delivered.
@@ -2239,7 +2235,7 @@ static int do_tkill(pid_t tgid, pid_t pi* signal is private anyway. */ if (!error && sig &&lock_task_sighand (p, &flags)) { - error =specific_send_sig_info (sig, &info, p); + error =specific_send_sig_info (sig, info, p);unlock_task_sighand (p, &flags); } }
@@ -2248,6 +2244,19 @@ static int do_tkill(pid_t tgid, pid_t pireturn error; } +static int do_tkill (pid_t tgid,pid_t pid,int sig) +{ +struct siginfo info; + + info.si_signo = sig; + info.si_errno =0 ; + info.si_code =SI_TKILL ; + info.si_pid =task_tgid_vnr (current); + info.si_uid = current->uid; + +return do_send_specific (tgid, pid, sig, &info); +} +/** * sys_tgkill - send signal to one specific thread * @tgid: the thread group ID of the thread
RFC PATCH 2/3 signals: implement sys_rt_tgsigqueueinfo
In reply to Thomas Gleixnersys_kill has the per thread counterpart sys_tgkill. sigqueueinfo is missing a thread directed counterpart. Such an interface is important for migrating applications from other OSes which have the per thread delivery implemented.
--- kernel/signal.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
kernel/signal.c +21 -0
@@ -2307,6 +2307,27 @@ sys_rt_sigqueueinfo(pid_t pid, int sig,return kill_proc_info (sig, &info, pid); } +asmlinkage long +sys_rt_tgsigqueueinfo (pid_t tgid,pid_t pid,int sig,siginfo_t __user *uinfo) +{ +siginfo_t info; + +/* This is only valid for single tasks */ +if (pid <=0 || tgid <=0 ) +return -EINVAL ; + +if (copy_from_user (&info, uinfo,sizeof (siginfo_t))) +return -EFAULT ; + +/* Not even root can pretend to send signals from the kernel. +Nor can they impersonate a kill(), which adds source info. */ +if (info.si_code >=0 ) +return -EPERM ; + info.si_signo = sig; + +return do_send_specific (tgid, pid, sig, &info); +} +int do_sigaction (int sig,struct k_sigaction *act,struct k_sigaction *oact) {struct task_struct *t = current;
RFC PATCH 3/3 x86: hookup sys_rt_tgsigqueueinfo
In reply to Thomas GleixnerMake the new sys_rt_tgsigqueueinfo available for x86.
--- arch/x86/kernel/syscall_table_32.S | 1 + include/asm-x86/unistd_32.h | 1 + include/asm-x86/unistd_64.h | 2 ++ 3 files changed, 4 insertions(+)
arch/x86/kernel/syscall_table_32.S +1 -0
@@ -332,3 +332,4 @@ ENTRY(sys_call_table) .long sys_dup3 /* 330 */ .long sys_pipe2 .long sys_inotify_init1 + .long sys_rt_tgsigqueueinfo
include/asm-x86/unistd_32.h +1 -0
@@ -338,6 +338,7 @@#define __NR_dup3 330#define __NR_pipe2 331#define __NR_inotify_init1 332 +#define __NR_rt_tgsigqueueinfo 333#ifdef __KERNEL__
include/asm-x86/unistd_64.h +2 -0
@@ -653,6 +653,8 @@ __SYSCALL(__NR_dup3, sys_dup3) __SYSCALL(__NR_pipe2, sys_pipe2)#define __NR_inotify_init1 294 __SYSCALL(__NR_inotify_init1, sys_inotify_init1) +#define __NR_rt_tgsigqueueinfo 295 +__SYSCALL(__NR_rt_tgsigqueueinfo, sys_rt_tgsigqueueinfo)#ifndef __NO_STUBS
The core of this looks fine to me. Presumably this would be expressed in userland as pthread_sigqueue.
You are missing compat_sys_rt_tgsigqueueinfo for e.g. the arch/x86/ia32/ia32entry.S table.
The clean way to do that would be a do_rt_tgsigqueueinfo taking the siginfo_t * (not __user). That is, just split out the copy_from_user, so compat_sys_rt_tgsigqueueinfo does copy_siginfo_from_user32 instead.
Thanks,
Roland
Quoted 2 lines · Roland McGrath
The core of this looks fine to me. Presumably this would be expressed in userland as
pthread_sigqueue.
Yes, that would match the pthread_kill then.
Quoted 2 lines
You are missing
compat_sys_rt_tgsigqueueinfofor e.g. thearch/x86/ia32/ia32entry.Stable.
Duh, did not think about that.
Quoted 3 lines
The clean way to do that would be a
do_rt_tgsigqueueinfotaking thesiginfo_t* (not__user). That is, just split out thecopy_from_user, socompat_sys_rt_tgsigqueueinfodoescopy_siginfo_from_user32instead.
Will do.
Thanks,
tglx
Thomas,
Quoted 8 lines · Thomas Gleixner
sys_killhas a counterpartsys_tgkillwhich allows to send signals to a particular thread.sys_rt_sigqueueinfois lacking such a counterpart.Aside of the asymetry it is a show stopper for migrating applications from other unix-alike RTOSes.
The following patch series implements
rt_tgsigqueueinfoand hooks it up for x86.
I'm not sure if I've run across a bug or (maybe more likely) have a bug in my test code. Could you take a look at the test programs below.
When I use t_rt_tgsigqueueinfo.c to send a signal to my receiving program it crashes because the siginfo_t argument to the signal handler has a bad value (0x33); it's not obvious to me how it should get to have that value (have I misconstructed something in the t_rt_tgsigqueuinfo.c program?).
Cheers,
Michael
==========
$ ./multithread_sig_receiver 44 x
Established handler for signal 44
Main: TGID = 6105; TID = 6105
Thread 1: PID = 6105; TID = 6106; arg = x
Thread 1: about to pause
$
./t_rt_tgsigqueuinfo 6105 6106 44 1
PID = 6105; TID = 6106; caught signal 44:
0x33
Segmentation fault (core dumped)=========
/*#* t_rt_tgsigqueueinfo.c
Copyright 2008, Linux Foundation;
written by Michael Kerrisk <[email protected]>
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)#if defined(__i386__)
#define SYS_rt_tgsigqueueinfo 333
#endif
static intrt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si)
{
return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si);
}intmain(int argc, char *argv[])
{
pid_t tgid, tid;
siginfo_t si;
int sig, val;
if (argc < 5) {
fprintf(stderr, "Usage: %s <tgid> <tid> <sig> <val>\n", argv[0]);
exit(EXIT_SUCCESS);
}
printf("My PID is %ld\n", (long) getpid());
tgid = atoi(argv[1]);
tid = atoi(argv[2]);
sig = atoi(argv[3]);
val = atoi(argv[4]);
si.si_signo = sig;
si.si_code = SI_QUEUE;
si.si_pid = getpid();
si.si_uid = getuid();
si.si_value.sival_int = val;
if (rt_tgsigqueueinfo(tgid, tid, sig, &si) == -1)
errExit("rt_tgsigqueueinfo");
exit(EXIT_SUCCESS);
} /* main */=====================
/*#* multithread_sig_receiver.c
Copyright 2008, Linux Foundation;
written by Michael Kerrisk <[email protected]>
*/
#define _GNU_SOURCE
#include <sys/syscall.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)#define errExitEN(en, msg) \
do { errno = en; perror(msg); \
exit(EXIT_FAILURE); } while (0)static int test_sig;
struct thread_arg {
int thread_num;
char *argv_string;
};static pid_t
gettid(void)
{
return syscall(SYS_gettid);
}static voidhandler(int sig, siginfo_t *si, void *uc)
{
printf("PID = %ld; TID = %ld; caught signal %d: \n",
(long) getpid(), (long) gettid(), sig);
printf("%p\n", si);
printf("si_signo=%d ", si->si_signo);
printf("si_code=%d ", si->si_code);
printf("si_pid=%ld ", (long) si->si_pid);
printf("si_uid=%ld ", (long) si->si_uid);
printf("si_value.sival_int=%d\n", si->si_value.sival_int);
}static void *threadFunc(void *arg)
{
struct thread_arg *ta = (struct thread_arg *) arg;
sigset_t new;
int s;
printf("Thread %d: PID = %ld; TID = %ld; arg = %s\n", ta->thread_num,
(long) getpid(), (long) gettid(), ta->argv_string);
if (ta->argv_string[0] == 'b') {
printf("Thread %d: blocking signal\n", ta->thread_num);
sigemptyset(&new);
sigaddset(&new, test_sig);
s = pthread_sigmask(SIG_SETMASK, &new, NULL);
if (s != 0)
errExit("pthread_sigmask");
}
if (isdigit(ta->argv_string[1])) {
sleep(atoi(&ta->argv_string[1]));
printf("Thread %d: unblocking signal\n", ta->thread_num);
sigemptyset(&new);
s = pthread_sigmask(SIG_SETMASK, &new, NULL);
if (s != 0)
errExit("pthread_sigmask");
}
printf("Thread %d: about to pause\n", ta->thread_num);
for (;;)
pause();
} /* threadFunc */#define MAX_ARGS 1000
intmain(int argc, char *argv[])
{
pthread_t t;
int s, j;
struct sigaction sa;
struct thread_arg ta[MAX_ARGS];
sigset_t new;
if (argc < 3) {
fprintf(stderr, "Usage: %s <sig> <thread-config>...\n", argv[0]);
exit(EXIT_SUCCESS);
}
if (argc > MAX_ARGS) {
fprintf(stderr, "Too many arguments\n");
exit(EXIT_SUCCESS);
}
test_sig = atoi(argv[1]);
sigemptyset(&new);
if (sigprocmask(SIG_SETMASK, &new, NULL) == -1)
errExit("sigprocmask");
sa.sa_flags = 0;
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
if (sigaction(test_sig, &sa, NULL) == -1)
errExit("sigaction");
printf("Established handler for signal %d\n", test_sig);
printf("Main: TGID = %ld; TID = %ld\n",
(long) getpid(), (long) gettid());
for (j = 0; j + 2 < argc; j++) {
ta[j].thread_num = j + 1;
ta[j].argv_string = argv[j + 2];
s = pthread_create(&t, NULL, threadFunc, &ta[j]);
if (s != 0)
errExitEN(s, "pthread_create");
}
pause();
} /* main */You need .sa_flags = SA_SIGINFO.
Thanks,
Roland
Quoted 1 line · Roland McGrath
You need .sa_flags =
SA_SIGINFO.
D'oh! Overlooking the obvious! Thanks Roland.