xref: /aosp_15_r20/external/libcap/tests/libcap_psx_test.c (revision 2810ac1b38eead2603277920c78344c84ddf3aff)
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/types.h>
12 #include <sys/wait.h>
13 #include <unistd.h>
14 
thread_fork_exit(void * data)15 static void *thread_fork_exit(void *data) {
16     usleep(1234);
17     pid_t pid = fork();
18     cap_t start = cap_get_proc();
19     if (start == NULL) {
20 	perror("FAILED: unable to start");
21 	exit(1);
22     }
23     if (pid == 0) {
24 	if (cap_set_proc(start)) {
25 	    perror("setting empty caps failed");
26 	    exit(1);
27 	}
28 	exit(0);
29     }
30     int res;
31     if (waitpid(pid, &res, 0) != pid || res != 0) {
32 	printf("FAILED: pid=%d wait returned %d and/or error: %d\n",
33 	       pid, res, errno);
34 	exit(1);
35     }
36     cap_set_proc(start);
37     cap_free(start);
38     return NULL;
39 }
40 
main(int argc,char ** argv)41 int main(int argc, char **argv) {
42     int i;
43     printf("hello libcap and libpsx ");
44     fflush(stdout);
45     cap_t start = cap_get_proc();
46     if (start == NULL) {
47 	perror("FAILED: to actually start");
48 	exit(1);
49     }
50     pthread_t ignored[10];
51     for (i = 0; i < 10; i++) {
52 	pthread_create(&ignored[i], NULL, thread_fork_exit, NULL);
53     }
54     for (i = 0; i < 10; i++) {
55 	printf(".");     /* because of fork, this may print double */
56 	fflush(stdout);  /* try to limit the above effect */
57 	if (cap_set_proc(start)) {
58 	    perror("failed to set proc");
59 	    exit(1);
60 	}
61 	usleep(1000);
62     }
63     printf(" PASSED\n");
64     exit(0);
65 }
66