1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#!/bin/sh
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2020 Konstantin Belousov
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. 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 AUTHOR 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 AUTHOR 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.
#
# sigfastblock test scenario by Konstantin Belousov <kib@FreeBSD.org>
. ../default.cfg
[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
[ `uname -p` != "amd64" ] && exit 0
cat > /tmp/sigfastblock.c <<EOF
/* $Id: sigfastblock1.c,v 1.4 2020/02/16 19:53:14 kostik Exp kostik $ */
#include <sys/types.h>
#include <sys/signalvar.h>
#include <sys/sysctl.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifdef __ILP32
#define BITNESS "32"
#else
#define BITNESS "64"
#endif
int block;
static void
sighandler(int signo, siginfo_t *si, void *ucp __unused)
{
printf("block %#x sig %d si_code %d si_pid %d\n",
block, signo, si->si_code, si->si_pid);
}
int
main(void)
{
struct timespec rqts;
struct sigaction sa;
int error, val;
size_t valsize;
pid_t child;
valsize = sizeof(val);
error = sysctlbyname("kern.elf" BITNESS ".sigfastblock", &val,
&valsize, NULL, 0);
if (error != 0)
err(1, "sigfastblock sysctl");
if (val != 0)
errx(1, "sigfastblock use in rtld must be turned off");
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = sighandler;
sa.sa_flags = SA_SIGINFO;
error = sigaction(SIGUSR1, &sa, NULL);
child = fork();
if (child == -1)
err(1, "fork");
if (child == 0) {
sleep(2);
kill(getppid(), SIGUSR1);
_exit(0);
}
error = __sys_sigfastblock(SIGFASTBLOCK_SETPTR, &block);
if (error != 0)
err(1, "sigfastblock setptr");
printf("Registered block at %p\n", &block);
block = SIGFASTBLOCK_INC;
rqts.tv_sec = 4;
rqts.tv_nsec = 0;
errno = 0;
error = nanosleep(&rqts, 0);
printf("nanosleep 1 returned %d, errno %d, block %#x\n",
error, errno, block);
raise(SIGUSR1);
errno = 0;
error = nanosleep(&rqts, 0);
printf("nanosleep 2 returned %d, errno %d, block %#x\n",
error, errno, block);
errno = 0;
block -= SIGFASTBLOCK_INC;
error = __sys_sigfastblock(SIGFASTBLOCK_UNBLOCK, NULL);
printf("unblock returned %d, errno %d, block %#x\n",
error, errno, block);
}
EOF
mycc -o /tmp/sigfastblock -Wall -Wextra -O2 -g /tmp/sigfastblock.c || exit 1
echo "Expect:
Registered block at 0x2030c0
nanosleep 1 returned -1, errno 4, block 0x11
nanosleep 2 returned 0, errno 0, block 0x11
unblock returned 0, errno 0, block 0
block 0 sig 30 si_code 65543 si_pid 1151
block 0 sig 30 si_code 65537 si_pid 1152
"
cd /tmp
sysctl kern.elf64.sigfastblock=0
sysctl kern.elf32.sigfastblock=0
./sigfastblock; s=$?
sysctl kern.elf64.sigfastblock=1
sysctl kern.elf32.sigfastblock=1
rm -f /tmp/sigfastblock.c /tmp/sigfastblock
exit $s
|