xref: /src/tests/sys/capsicum/syscalls.h (revision fba81b33aabff74ad03d5f9f9663c176cf060fa6)
1 /*
2  * Minimal portability layer for system call differences between
3  * Capsicum OSes.
4  */
5 #ifndef __SYSCALLS_H__
6 #define __SYSCALLS_H__
7 
8 /* Map umount2 (Linux) syscall to unmount (FreeBSD) syscall */
9 #define umount2(T, F) unmount(T, F)
10 
11 /* Map sighandler_y (Linux) to sig_t (FreeBSD) */
12 #define sighandler_t sig_t
13 
14 /* profil(2) has a first argument of char* */
15 #define profil_arg1_t char
16 
17 /* FreeBSD has getdents(2) available */
18 #include <sys/types.h>
19 #include <dirent.h>
getdents_(unsigned int fd,void * dirp,unsigned int count)20 inline int getdents_(unsigned int fd, void *dirp, unsigned int count) {
21   return getdents(fd, (char*)dirp, count);
22 }
23 #include <sys/mman.h>
mincore_(void * addr,size_t length,unsigned char * vec)24 inline int mincore_(void *addr, size_t length, unsigned char *vec) {
25   return mincore(addr, length, (char*)vec);
26 }
27 #define getpid_ getpid
28 
29 /* Map Linux-style sendfile to FreeBSD sendfile */
30 #include <sys/socket.h>
31 #include <sys/uio.h>
sendfile_(int out_fd,int in_fd,off_t * offset,size_t count)32 inline ssize_t sendfile_(int out_fd, int in_fd, off_t *offset, size_t count) {
33   return sendfile(in_fd, out_fd, *offset, count, NULL, offset, 0);
34 }
35 
36 /* A sample mount(2) call */
37 #include <sys/param.h>
38 #include <sys/mount.h>
bogus_mount_()39 inline int bogus_mount_() {
40   return mount("procfs", "/not_mounted", 0, NULL);
41 }
42 
43 /* Mappings for extended attribute functions */
44 #include <sys/extattr.h>
45 #include <errno.h>
fbsd_extattr_skip_prefix(const char * p)46 static const char *fbsd_extattr_skip_prefix(const char *p) {
47   if (*p++ == 'u' && *p++ == 's' && *p++ == 'e' && *p++ == 'r' && *p++ == '.')
48     return p;
49   errno = EINVAL;
50   return NULL;
51 }
flistxattr_(int fd,char * list,size_t size)52 inline ssize_t flistxattr_(int fd, char *list, size_t size) {
53   return extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, list, size);
54 }
fgetxattr_(int fd,const char * name,void * value,size_t size)55 inline ssize_t fgetxattr_(int fd, const char *name, void *value, size_t size) {
56   if (!(name = fbsd_extattr_skip_prefix(name)))
57     return -1;
58   return extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, name, value, size);
59 }
fsetxattr_(int fd,const char * name,const void * value,size_t size,int)60 inline int fsetxattr_(int fd, const char *name, const void *value, size_t size, int) {
61   if (!(name = fbsd_extattr_skip_prefix(name)))
62     return -1;
63   return extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, name, value, size);
64 }
fremovexattr_(int fd,const char * name)65 inline int fremovexattr_(int fd, const char *name) {
66   if (!(name = fbsd_extattr_skip_prefix(name)))
67     return -1;
68   return extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, name);
69 }
70 
71 /* mq_* functions are wrappers in FreeBSD so go through to underlying syscalls */
72 #include <sys/syscall.h>
73 extern "C" {
74 extern int __sys_kmq_notify(int, const struct sigevent *);
75 extern int __sys_kmq_open(const char *, int, mode_t, const struct mq_attr *);
76 extern int __sys_kmq_setattr(int, const struct mq_attr *__restrict, struct mq_attr *__restrict);
77 extern ssize_t __sys_kmq_timedreceive(int, char *__restrict, size_t,
78                                       unsigned *__restrict, const struct timespec *__restrict);
79 extern int __sys_kmq_timedsend(int, const char *, size_t, unsigned,
80                                const struct timespec *);
81 extern int  __sys_kmq_unlink(const char *);
82 }
83 #define mq_notify_ __sys_kmq_notify
84 #define mq_open_ __sys_kmq_open
85 #define mq_setattr_ __sys_kmq_setattr
86 #define mq_getattr_(A, B) __sys_kmq_setattr(A, NULL, B)
87 #define mq_timedreceive_ __sys_kmq_timedreceive
88 #define mq_timedsend_ __sys_kmq_timedsend
89 #define mq_unlink_ __sys_kmq_unlink
90 #define mq_close_ close
91 #include <sys/ptrace.h>
ptrace_(int request,pid_t pid,void * addr,void * data)92 inline long ptrace_(int request, pid_t pid, void *addr, void *data) {
93   return ptrace(request, pid, (caddr_t)addr, static_cast<int>((long)data));
94 }
95 #define PTRACE_PEEKDATA_ PT_READ_D
96 #define getegid_ getegid
97 #define getgid_ getgid
98 #define geteuid_ geteuid
99 #define getuid_ getuid
100 #define getgroups_ getgroups
101 #define getrlimit_ getrlimit
102 #define bind_ bind
103 #define connect_ connect
104 
105 /* Features available */
106 #define HAVE_CHFLAGSAT
107 #define HAVE_BINDAT
108 #define HAVE_CONNECTAT
109 #define HAVE_CHFLAGS
110 #define HAVE_GETFSSTAT
111 #define HAVE_REVOKE
112 #define HAVE_GETLOGIN
113 #define HAVE_MKFIFOAT
114 #define HAVE_SYSARCH
115 #include <machine/sysarch.h>
116 #define HAVE_STAT_BIRTHTIME
117 #define HAVE_SYSCTL
118 #define HAVE_FPATHCONF
119 #define HAVE_F_DUP2FD
120 #define HAVE_PSELECT
121 #define HAVE_SCTP
122 
123 /* FreeBSD only allows root to call mlock[all]/munlock[all] */
124 #define MLOCK_REQUIRES_ROOT 1
125 /* FreeBSD effectively only allows root to call sched_setscheduler */
126 #define SCHED_SETSCHEDULER_REQUIRES_ROOT 1
127 
128 #endif /*__SYSCALLS_H__*/
129