File: exec-sigmask.c

package info (click to toggle)
valgrind 1:3.12.0~svn20160714-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,428 kB
  • ctags: 70,855
  • sloc: ansic: 674,645; exp: 26,134; xml: 21,574; asm: 7,570; cpp: 7,567; makefile: 7,380; sh: 6,188; perl: 5,855; haskell: 195
file content (83 lines) | stat: -rw-r--r-- 1,508 bytes parent folder | download | duplicates (3)
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
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>

static void do_exec(const char *path, const char *arg, const sigset_t *mask)
{
	pid_t pid;
	int status;

	pid = fork();
	if (pid == -1) {
		perror("fork");
		exit(1);
	}
	if (pid == 0) {
		sigprocmask(SIG_SETMASK, mask, NULL);
		execl(path, path, arg, (char *) NULL);
			
		fprintf(stderr, "FAILED: execl failed with %s\n",
			strerror(errno));
	} else {
		int ret;
		do 
			ret = waitpid(pid, &status, 0);
		while(ret == -1 && errno == EINTR);
		if (ret != pid) {
			perror("waitpid");
			exit(1);
		}
		if (status != 0) {
			fprintf(stderr, "child exec failed\n");
			exit(1);
		}
	}
}

int main(int argc, char **argv)
{
	if (argc == 1) {
		sigset_t mask;

		sigfillset(&mask);
		do_exec(argv[0], "full", &mask);

		sigemptyset(&mask);
		do_exec(argv[0], "empty", &mask);
	} else {
		sigset_t mask;
		int i;
		int empty;

		if (strcmp(argv[1], "full") == 0)
			empty = 0;
		else if (strcmp(argv[1], "empty") == 0)
			empty = 1;
		else {
			fprintf(stderr, "empty or full?\n");
			exit(1);
		}

		sigprocmask(SIG_SETMASK, NULL, &mask);

		for(i = 1; i < NSIG; i++) {
			if (i == SIGKILL || i == SIGSTOP)
				continue;

			if (empty) {
				if (sigismember(&mask, i))
					printf("empty: signal %d added to mask\n", i);
			} else {
				if (!sigismember(&mask, i))
					printf("full: signal %d missing from mask\n", i);
			}
		}
	}

	return 0;
}