2 * linux-user signal handling tests.
4 * Copyright (c) 2021 Linaro Ltd
6 * SPDX-License-Identifier: GPL-2.0-or-later
21 static void error1(const char *filename
, int line
, const char *fmt
, ...)
25 fprintf(stderr
, "%s:%d: ", filename
, line
);
26 vfprintf(stderr
, fmt
, ap
);
27 fprintf(stderr
, "\n");
32 static int __chk_error(const char *filename
, int line
, int ret
)
35 error1(filename
, line
, "%m (ret=%d, errno=%d/%s)",
36 ret
, errno
, strerror(errno
));
41 #define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
43 #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
48 typedef struct ThreadJob ThreadJob
;
56 static pthread_t
*threads
;
57 static int max_threads
= 10;
58 __thread
int signal_count
;
59 int total_signal_count
;
61 static void *background_thread_func(void *arg
)
63 ThreadJob
*job
= (ThreadJob
*) arg
;
65 printf("thread%d: started\n", job
->number
);
66 while (total_signal_count
< job
->count
) {
69 printf("thread%d: saw %d alarms from %d\n", job
->number
,
70 signal_count
, total_signal_count
);
74 static void spawn_threads(void)
77 threads
= calloc(sizeof(pthread_t
), max_threads
);
79 for (i
= 0; i
< max_threads
; i
++) {
80 ThreadJob
*job
= calloc(sizeof(ThreadJob
), 1);
82 job
->sleep
= i
* 1000;
84 pthread_create(threads
+ i
, NULL
, background_thread_func
, job
);
88 static void close_threads(void)
91 for (i
= 0; i
< max_threads
; i
++) {
92 pthread_join(threads
[i
], NULL
);
98 static void sig_alarm(int sig
, siginfo_t
*info
, void *puc
)
100 if (sig
!= SIGRTMIN
) {
101 error("unexpected signal");
104 __atomic_fetch_add(&total_signal_count
, 1, __ATOMIC_SEQ_CST
);
107 static void test_signals(void)
109 struct sigaction act
;
110 struct itimerspec it
;
114 /* Set up SIG handler */
115 act
.sa_sigaction
= sig_alarm
;
116 sigemptyset(&act
.sa_mask
);
117 act
.sa_flags
= SA_SIGINFO
;
118 chk_error(sigaction(SIGRTMIN
, &act
, NULL
));
120 /* Create POSIX timer */
121 sev
.sigev_notify
= SIGEV_SIGNAL
;
122 sev
.sigev_signo
= SIGRTMIN
;
123 sev
.sigev_value
.sival_ptr
= &tid
;
124 chk_error(timer_create(CLOCK_REALTIME
, &sev
, &tid
));
126 it
.it_interval
.tv_sec
= 0;
127 it
.it_interval
.tv_nsec
= 1000000;
128 it
.it_value
.tv_sec
= 0;
129 it
.it_value
.tv_nsec
= 1000000;
130 chk_error(timer_settime(tid
, 0, &it
, NULL
));
136 } while (total_signal_count
< 2000);
138 printf("shutting down after: %d signals\n", total_signal_count
);
142 chk_error(timer_delete(tid
));
145 int main(int argc
, char **argv
)