AtlatestRepositorysigil-wm

sigil-wm / tree / testenvironment-exec.c

1/* SPDX-License-Identifier: BSD-3-Clause */
2#define _GNU_SOURCE
3#include <errno.h>
4#include <signal.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10int main(int argc,char **argv) {
11 if(argc<2) return 2;
12 if(!strcmp(argv[1],"inherit")) {
13 struct sigaction action={.sa_handler=SIG_IGN};
14 sigemptyset(&action.sa_mask);
15 const int ignored[]={SIGINT,SIGQUIT,SIGHUP,SIGTERM,SIGPIPE,SIGUSR1};
16 for(size_t index=0;index<sizeof(ignored)/sizeof(ignored[0]);index++)
17 if(sigaction(ignored[index],&action,NULL)) return 3;
18 sigset_t blocked;
19 sigfillset(&blocked);
20 if(sigprocmask(SIG_SETMASK,&blocked,NULL)) return 4;
21 execvp(argv[2],argv+2);
22 return 5;
23 }
24 sigset_t blocked;
25 if(sigprocmask(SIG_SETMASK,NULL,&blocked)) return 6;
26 unsigned checked=0;
27 for(int signum=1;signum<NSIG;signum++) {
28 if(signum==SIGKILL || signum==SIGSTOP) continue;
29 struct sigaction action;
30 if(sigaction(signum,NULL,&action)) {
31 if(errno==EINVAL) continue;
32 return 7;
33 }
34 if(action.sa_handler!=SIG_DFL || sigismember(&blocked,signum)) {
35 fprintf(stderr,"SIGNAL_RESET_FAIL signum=%d ignored=%d blocked=%d\n",
36 signum,action.sa_handler==SIG_IGN,sigismember(&blocked,signum));
37 return 42;
38 }
39 checked++;
40 }
41 char line[64];
42 if(!fgets(line,sizeof(line),stdin) || strcmp(line,"signal-stdin\n")) return 8;
43 printf("SIGNALS_DEFAULT=PASS dispositions=%u mask=empty stdin=preserved\n",checked);
44 fflush(stdout);
45 if(!strcmp(argv[1],"interrupt")) {
46 raise(SIGINT);
47 return 43;
48 }
49 return 0;