#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "utils.h"
static void SIGUSR1_handler(int sig)
{
kill(getpid(), SIGUSR2);
}
static void SIGUSR2_handler(int sig)
{
}
static ssize_t raw_read(int fd, void *buf, size_t count)
{
register long nr asm("r0") = __NR_read;
register long _fd asm("r3") = fd;
register void *_buf asm("r4") = buf;
register size_t _count asm("r5") = count;
asm volatile(
" b 0f \n"
" b 1f \n"
" 0: sc 0 \n"
" bns 2f \n"
" neg %0,%0 \n"
" b 2f \n"
" 1: \n"
" li %0,%4 \n"
" 2: \n"
: "+r"(_fd), "+r"(nr), "+r"(_buf), "+r"(_count)
: "i"(-ENOANO)
: "memory", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "ctr", "cr0");
if (_fd < 0) {
errno = -_fd;
_fd = -1;
}
return _fd;
}
#define DATA "test 123"
#define DLEN (strlen(DATA)+1)
int test_restart(void)
{
int pipefd[2];
pid_t pid;
char buf[512];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
struct sigaction act;
int fd;
memset(&act, 0, sizeof(act));
sigaddset(&act.sa_mask, SIGUSR2);
act.sa_handler = SIGUSR1_handler;
act.sa_flags = SA_RESTART;
if (sigaction(SIGUSR1, &act, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
memset(&act, 0, sizeof(act));
act.sa_handler = SIGUSR2_handler;
act.sa_flags = SA_RESTART;
if (sigaction(SIGUSR2, &act, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
while ((fd = dup(pipefd[0])) != 512) {
if (fd == -1) {
perror("dup");
exit(EXIT_FAILURE);
}
}
if (raw_read(fd, buf, 512) == -1) {
if (errno == ENOANO) {
fprintf(stderr, "Double restart moved restart before sc instruction.\n");
_exit(EXIT_FAILURE);
}
perror("read");
exit(EXIT_FAILURE);
}
if (strncmp(buf, DATA, DLEN)) {
fprintf(stderr, "bad test string %s\n", buf);
exit(EXIT_FAILURE);
}
return 0;
} else {
int wstatus;
usleep(100000);
kill(pid, SIGUSR1);
usleep(100000);
if (write(pipefd[1], DATA, DLEN) != DLEN) {
perror("write");
exit(EXIT_FAILURE);
}
close(pipefd[0]);
close(pipefd[1]);
if (wait(&wstatus) == -1) {
perror("wait");
exit(EXIT_FAILURE);
}
if (!WIFEXITED(wstatus)) {
fprintf(stderr, "child exited abnormally\n");
exit(EXIT_FAILURE);
}
FAIL_IF(WEXITSTATUS(wstatus) != EXIT_SUCCESS);
return 0;
}
}
int main(void)
{
test_harness_set_timeout(10);
return test_harness(test_restart, "sig sys restart");
}