1 #include <sys/capsicum.h>
9 #include <sys/ioccom.h>
15 if (-1 == cap_getmode(&mode
)) {
16 perror("cap_getmode() failed:");
22 // example from man cap_rights_get
23 cap_rights_t setrights
, getrights
;
26 memset(&setrights
, 0, sizeof(setrights
));
27 memset(&getrights
, 0, sizeof(getrights
));
29 fd
= open("capsicum.c", O_RDONLY
);
31 err(1, "open() failed");
33 cap_rights_init(&setrights
, CAP_IOCTL
, CAP_FSTAT
, CAP_READ
);
34 if (cap_rights_limit(fd
, &setrights
) < 0 && errno
!= ENOSYS
)
35 err(1, "cap_rights_limit() failed");
37 unsigned long cmds
[] = { BIOCGSTATS
, BIOCROTZBUF
};
38 if (cap_ioctls_limit(fd
, cmds
, sizeof(cmds
) / sizeof(cmds
[0])) < 0 && errno
!= ENOSYS
) {
39 err(1, "cap_ioctls_limit() filed");
42 uint32_t fcntlrights
= CAP_FCNTL_GETFL
| CAP_FCNTL_SETFL
;
43 if (cap_fcntls_limit(STDIN_FILENO
, fcntlrights
) < 0 && errno
!= ENOSYS
) {
44 err(1, "cap_fcnls_limit() filed");
47 if (cap_rights_get(fd
, &getrights
) < 0 && errno
!= ENOSYS
)
48 err(1, "cap_rights_get() failed");
50 assert(memcmp(&setrights
, &getrights
, sizeof(setrights
)) == 0);
52 unsigned long getcmds
[2];
53 if (cap_ioctls_get(fd
, getcmds
, 2) < 0 && errno
!= ENOSYS
)
54 err(1, "cap_ioctls_get() failed");
56 assert(memcmp(cmds
, getcmds
, sizeof(cmds
)) == 0);
58 uint32_t getfcntlrights
;
59 if (cap_fcntls_get(STDIN_FILENO
, &getfcntlrights
) < 0 && errno
!= ENOSYS
) {
60 err(1, "cap_fcnls_limit() filed");
63 assert(fcntlrights
== getfcntlrights
);
69 if (-1 == cap_getmode(&mode
)) {
70 perror("cap_getmode() failed:");
78 int *px
= malloc(sizeof(int));
83 cap_rights_get(0, NULL
);
84 cap_rights_get(x
, &getrights
);
85 cap_rights_t
* badrights
= malloc(sizeof(cap_rights_t
));
86 cap_rights_init(badrights
, CAP_FSTAT
, CAP_READ
);
88 cap_rights_get(0, badrights
);
90 cap_rights_limit(x
, &setrights
);
92 cap_rights_limit(fd
, badrights
);
94 int fd2
= open("foo", O_RDWR
);
96 err(1, "open in write mode should have failed");