Debian Codegraph @

tests/libcap_psx_test.c

raw
1 #ifndef _DEFAULT_SOURCE
2 #define _DEFAULT_SOURCE
3 #endif
4
5 #include <errno.h>
6 #include <pthread.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/capability.h>
10 #include <sys/psx_syscall.h>
11 #include <sys/prctl.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <unistd.h>
15
16 static void *thread_fork_exit(void *data) {
17 usleep(1234);
18 pid_t pid = fork();
19 long int start = cap_prctl(PR_GET_KEEPCAPS, 0, 0, 0, 0, 0);
20 if (start == -1) {
21 perror("FAILED: unable to start");
22 exit(1);
23 }
24 if (pid == 0) {
25 if (cap_prctlw(PR_SET_KEEPCAPS, !start, 0, 0, 0, 0) != 0) {
26 perror("failed to set proc");
27 exit(1);
28 }
29 if (cap_prctlw(PR_GET_KEEPCAPS, 0, 0, 0, 0, 0) == start) {
30 perror("failed to have set forked proc");
31 exit(1);
32 }
33 exit(0);
34 }
35 int res;
36 if (waitpid(pid, &res, 0) != pid || res != 0) {
37 printf("FAILED: pid=%d wait returned %d and/or error: %d\n",
38 pid, res, errno);
39 exit(1);
40 }
41 return NULL;
42 }
43
44 int main(int argc, char **argv) {
45 int i;
46 printf("hello libcap and libpsx ");
47 fflush(stdout);
48 long int start = cap_prctl(PR_GET_KEEPCAPS, 0, 0, 0, 0, 0);
49 if (start == -1) {
50 perror("FAILED: to actually start");
51 exit(1);
52 }
53 pthread_t ignored[10];
54 for (i = 0; i < 10; i++) {
55 pthread_create(&ignored[i], NULL, thread_fork_exit, NULL);
56 printf("."); /* because of fork, this may print double */
57 fflush(stdout); /* try to limit the above effect */
58 if (cap_prctlw(PR_SET_KEEPCAPS, i & 1, 0, 0, 0, 0)) {
59 perror("failed to set proc");
60 exit(1);
61 }
62 /*
63 * This should validate all threads think the same thing,
64 * and none of the fork() calls mess anything up.
65 */
66 if (cap_prctlw(PR_GET_KEEPCAPS, 0, 0, 0, 0, 0) != (i & 1)) {
67 perror("failed to have set proc");
68 exit(1);
69 }
70 usleep(1000);
71 }
72 printf(" PASSED\n");
73 exit(0);
74 }