Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
Add signal1 to test rt_sigaction
Browse files Browse the repository at this point in the history
  • Loading branch information
wangbj committed Mar 7, 2019
1 parent 927454c commit 954dac1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LD = lld
CFLAGS = -g -Wall -O2 -D_POSIX_C_SOURCE=20180920 -D_GNU_SOURCE=1 -fPIC
CXXFLAGS = -g -Wall -O2 -D_POSIX_C_SOURCE=20180920 -D_GNU_SOURCE=1 -std=c++1z -fPIC

TARGET := x64-save-return-address openat1 open-many getpid write-many forkExec clock-nanosleep threads1 threads2 threads3 getpid-pie nanosleep segfault threads4 threads5 threads6 threads7 forkMany
TARGET := x64-save-return-address openat1 open-many getpid write-many forkExec clock-nanosleep threads1 threads2 threads3 getpid-pie nanosleep segfault threads4 threads5 threads6 threads7 forkMany signal1

SYSTRACE_LIBRARY_PATH := $(shell realpath $(shell pwd)/../lib)
SYSTRACE_DEBUG := $(shell realpath ../bin/systrace) --library-path=$(SYSTRACE_LIBRARY_PATH) --debug=4 --
Expand Down Expand Up @@ -76,6 +76,9 @@ threads7: threads7.o
segfault: segfault.o
$(CC) $^ -o $@ $(CFLAGS) -lrt -lpthread

signal1: signal1.o
$(CC) $^ -o $@ $(CFLAGS) -lrt

clean:
$(RM) $(OBJS) *.o
$(RM) $(TARGET)
Expand All @@ -101,5 +104,6 @@ tests: build-tests
timeout 30s $(SYSTRACE_DEBUG) ./threads7
timeout 30s $(SYSTRACE_DEBUG) ./forkMany
timeout 30s $(SYSTRACE_DEBUG) ./forkMany --block-sigchld
./signal1

.PHONY: all tests clean
78 changes: 78 additions & 0 deletions tests/signal1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>

#ifndef SA_RESTORER
#define SA_RESTORER 0x04000000
#endif

struct kernel_sigaction {
unsigned long sa__;
unsigned long flags;
unsigned long restorer;
unsigned long masks;
};

static int rt_sigreturn(void* regs)
{
return (int)syscall(SYS_rt_sigreturn, regs);
}

static int rt_sigaction(int signum, const struct kernel_sigaction* new, struct kernel_sigaction* old)
{
unsigned long r = (unsigned long)syscall(SYS_rt_sigaction, signum, new, old, 8);
if ( r >= ~0xfffUL) {
return (int)r;
} else {
return (int)r;
}
}

static volatile int quit = 0;

static void handler(int sig, siginfo_t *info, void *ucontext)
{
printf("[OK] received signal %u\n", sig);
quit = 1;
}

extern int __restore_rt(void);

int main(int argc, char* argv[])
{
int ret;
struct kernel_sigaction old, new;

memset(&old, 0, sizeof(old));
memset(&new, 0, sizeof(new));

new.sa__ = (unsigned long)handler;
new.flags = SA_RESTART | SA_RESTORER;
new.restorer = (unsigned long)rt_sigreturn;

ret = rt_sigaction(SIGALRM, &new, NULL);
if (ret < 0) {
perror("rt_sigaction");
exit(1);
}

ret = rt_sigaction(SIGALRM, NULL, &old);
if (ret < 0) {
perror("rt_sigaction");
exit(1);
}

assert(old.sa__ == (unsigned long)handler);

alarm(1);

while(!quit);

return 0;
}

0 comments on commit 954dac1

Please sign in to comment.