Path: blob/main/tools/regression/sigqueue/sigqtest2/sigqtest2.c
39481 views
#include <sys/types.h>1#include <sys/wait.h>2#include <err.h>3#include <errno.h>4#include <signal.h>5#include <stdio.h>6#include <stdlib.h>7#include <unistd.h>89int stop_received;10int exit_received;11int cont_received;1213void14job_handler(int sig, siginfo_t *si, void *ctx)15{16int status;17int ret;1819if (si->si_code == CLD_STOPPED) {20printf("%d: stop received\n", si->si_pid);21stop_received = 1;22kill(si->si_pid, SIGCONT);23} else if (si->si_code == CLD_EXITED) {24printf("%d: exit received\n", si->si_pid);25ret = waitpid(si->si_pid, &status, 0);26if (ret == -1)27errx(1, "waitpid");28if (!WIFEXITED(status))29errx(1, "!WIFEXITED(status)");30exit_received = 1;31} else if (si->si_code == CLD_CONTINUED) {32printf("%d: cont received\n", si->si_pid);33cont_received = 1;34}35}3637void38job_control_test(void)39{40struct sigaction sa;41pid_t pid;42int count = 10;4344sigemptyset(&sa.sa_mask);45sa.sa_flags = SA_SIGINFO;46sa.sa_sigaction = job_handler;47sigaction(SIGCHLD, &sa, NULL);48stop_received = 0;49cont_received = 0;50exit_received = 0;51fflush(stdout);52pid = fork();53if (pid == 0) {54printf("child %d\n", getpid());55kill(getpid(), SIGSTOP);56sleep(2);57exit(1);58}5960while (!(cont_received && stop_received && exit_received)) {61sleep(1);62if (--count == 0)63break;64}65if (!(cont_received && stop_received && exit_received))66errx(1, "job signals lost");6768printf("job control test OK.\n");69}7071void72rtsig_handler(int sig, siginfo_t *si, void *ctx)73{74}7576int77main()78{79struct sigaction sa;80sigset_t set;81union sigval val;8283/* test job control with empty signal queue */84job_control_test();8586/* now full fill signal queue in kernel */87sigemptyset(&sa.sa_mask);88sa.sa_flags = SA_SIGINFO;89sa.sa_sigaction = rtsig_handler;90sigaction(SIGRTMIN, &sa, NULL);91sigemptyset(&set);92sigaddset(&set, SIGRTMIN);93sigprocmask(SIG_BLOCK, &set, NULL);94val.sival_int = 1;95while (sigqueue(getpid(), SIGRTMIN, val))96;9798/* signal queue is fully filled, test the job control again. */99job_control_test();100return (0);101}102103104