2 * Minimal portability layer for system call differences between
8 /************************************************************
10 ************************************************************/
13 /* Map umount2 (Linux) syscall to unmount (FreeBSD) syscall */
14 #define umount2(T, F) unmount(T, F)
16 /* Map sighandler_y (Linux) to sig_t (FreeBSD) */
17 #define sighandler_t sig_t
19 /* profil(2) has a first argument of char* */
20 #define profil_arg1_t char
22 /* FreeBSD has getdents(2) available */
23 #include <sys/types.h>
25 inline int getdents_(unsigned int fd, void *dirp, unsigned int count) {
26 return getdents(fd, (char*)dirp, count);
29 inline int mincore_(void *addr, size_t length, unsigned char *vec) {
30 return mincore(addr, length, (char*)vec);
32 #define getpid_ getpid
34 /* Map Linux-style sendfile to FreeBSD sendfile */
35 #include <sys/socket.h>
37 inline ssize_t sendfile_(int out_fd, int in_fd, off_t *offset, size_t count) {
38 return sendfile(in_fd, out_fd, *offset, count, NULL, offset, 0);
41 /* A sample mount(2) call */
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 inline int bogus_mount_() {
45 return mount("procfs", "/not_mounted", 0, NULL);
48 /* Mappings for extended attribute functions */
49 #include <sys/extattr.h>
50 inline ssize_t flistxattr_(int fd, char *list, size_t size) {
51 return extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, list, size);
53 inline ssize_t fgetxattr_(int fd, const char *name, void *value, size_t size) {
54 return extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, name, value, size);
56 inline int fsetxattr_(int fd, const char *name, const void *value, size_t size, int) {
57 return extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, name, value, size);
59 inline int fremovexattr_(int fd, const char *name) {
60 return extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, name);
63 /* mq_* functions are wrappers in FreeBSD so go through to underlying syscalls */
64 #include <sys/syscall.h>
66 extern int __sys_kmq_notify(int, const struct sigevent *);
67 extern int __sys_kmq_open(const char *, int, mode_t, const struct mq_attr *);
68 extern int __sys_kmq_setattr(int, const struct mq_attr *__restrict, struct mq_attr *__restrict);
69 extern ssize_t __sys_kmq_timedreceive(int, char *__restrict, size_t,
70 unsigned *__restrict, const struct timespec *__restrict);
71 extern int __sys_kmq_timedsend(int, const char *, size_t, unsigned,
72 const struct timespec *);
73 extern int __sys_kmq_unlink(const char *);
75 #define mq_notify_ __sys_kmq_notify
76 #define mq_open_ __sys_kmq_open
77 #define mq_setattr_ __sys_kmq_setattr
78 #define mq_getattr_(A, B) __sys_kmq_setattr(A, NULL, B)
79 #define mq_timedreceive_ __sys_kmq_timedreceive
80 #define mq_timedsend_ __sys_kmq_timedsend
81 #define mq_unlink_ __sys_kmq_unlink
82 #define mq_close_ close
83 #include <sys/ptrace.h>
84 inline long ptrace_(int request, pid_t pid, void *addr, void *data) {
85 return ptrace(request, pid, (caddr_t)addr, static_cast<int>((long)data));
87 #define PTRACE_PEEKDATA_ PT_READ_D
88 #define getegid_ getegid
89 #define getgid_ getgid
90 #define geteuid_ geteuid
91 #define getuid_ getuid
92 #define getgroups_ getgroups
93 #define getrlimit_ getrlimit
95 #define connect_ connect
97 /* Features available */
98 #if __FreeBSD_version >= 1000000
99 #define HAVE_CHFLAGSAT
101 #define HAVE_CONNECTAT
104 #define HAVE_GETFSSTAT
106 #define HAVE_GETLOGIN
107 #define HAVE_MKFIFOAT
109 #include <machine/sysarch.h>
110 #define HAVE_STAT_BIRTHTIME
112 #define HAVE_FPATHCONF
113 #define HAVE_F_DUP2FD
117 /* FreeBSD only allows root to call mlock[all]/munlock[all] */
118 #define MLOCK_REQUIRES_ROOT 1
119 /* FreeBSD effectively only allows root to call sched_setscheduler */
120 #define SCHED_SETSCHEDULER_REQUIRES_ROOT 1
124 /************************************************************
126 ************************************************************/
130 #include <sys/prctl.h>
131 #include <sys/syscall.h>
132 #include <sys/types.h>
133 #include <sys/time.h>
134 #include <sys/resource.h>
135 #include <sys/wait.h>
136 #include <sys/sendfile.h>
137 #include <sys/statfs.h>
138 #include <sys/xattr.h>
139 #include <sys/mount.h>
140 #include <linux/net.h>
142 /* profil(2) has a first argument of unsigned short* */
143 #define profil_arg1_t unsigned short
145 static inline int getdents_(unsigned int fd, void *dirp, unsigned int count) {
146 return syscall(__NR_getdents, fd, dirp, count);
148 /* A sample mount(2) call */
149 static inline int bogus_mount_() {
150 return mount("/dev/bogus", "/bogus", "debugfs", MS_RDONLY, "");
153 /* libc's getpid() wrapper caches the pid value, and doesn't invalidate
154 * the cached value on pdfork(), so directly syscall. */
155 static inline pid_t getpid_() {
156 return syscall(__NR_getpid);
158 static inline int execveat(int fd, const char *path,
159 char *const argv[], char *const envp[], int flags) {
160 return syscall(__NR_execveat, fd, path, argv, envp, flags);
164 * Linux glibc includes an fexecve() function, implemented via the /proc
165 * filesystem. Bypass this and go directly to the execveat(2) syscall.
167 static inline int fexecve_(int fd, char *const argv[], char *const envp[]) {
168 return execveat(fd, "", argv, envp, AT_EMPTY_PATH);
171 * Linux glibc attempts to be clever and intercepts various uid/gid functions.
172 * Bypass by calling the syscalls directly.
174 static inline gid_t getegid_(void) { return syscall(__NR_getegid); }
175 static inline gid_t getgid_(void) { return syscall(__NR_getgid); }
176 static inline uid_t geteuid_(void) { return syscall(__NR_geteuid); }
177 static inline uid_t getuid_(void) { return syscall(__NR_getuid); }
178 static inline int getgroups_(int size, gid_t list[]) { return syscall(__NR_getgroups, size, list); }
179 static inline int getrlimit_(int resource, struct rlimit *rlim) {
180 return syscall(__NR_getrlimit, resource, rlim);
184 * Linux glibc for i386 consumes the errno returned from the raw socketcall(2) operation,
185 * so use the raw syscall for those operations that are disallowed in capability mode.
190 static inline int bind_(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
191 unsigned long args[3] = {(unsigned long)sockfd, (unsigned long)(intptr_t)addr, (unsigned long)addrlen};
192 return syscall(__NR_socketcall, SYS_BIND, args);
196 #define connect_ connect
198 static inline int connect_(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
199 unsigned long args[3] = {(unsigned long)sockfd, (unsigned long)(intptr_t)addr, (unsigned long)addrlen};
200 return syscall(__NR_socketcall, SYS_CONNECT, args);
204 #define mincore_ mincore
205 #define sendfile_ sendfile
206 #define flistxattr_ flistxattr
207 #define fgetxattr_ fgetxattr
208 #define fsetxattr_ fsetxattr
209 #define fremovexattr_ fremovexattr
210 #define mq_notify_ mq_notify
211 #define mq_open_ mq_open
212 #define mq_setattr_ mq_setattr
213 #define mq_getattr_ mq_getattr
214 #define mq_timedreceive_ mq_timedreceive
215 #define mq_timedsend_ mq_timedsend
216 #define mq_unlink_ mq_unlink
217 #define mq_close_ mq_close
218 #define ptrace_ ptrace
219 #define PTRACE_PEEKDATA_ PTRACE_PEEKDATA
221 /* Features available */
224 #include <sys/fsuid.h> /* for setfsgid()/setfsuid() */
225 #define HAVE_SETFSUID
226 #define HAVE_SETFSGID
227 #define HAVE_READAHEAD
228 #define HAVE_SEND_RECV_MMSG
230 #define HAVE_SYNC_FILE_RANGE
231 #include <sys/uio.h> /* for vmsplice */
234 #define HAVE_VMSPLICE
237 #define HAVE_EXECVEAT
239 #define HAVE_MKNOD_REG
240 #define HAVE_MKNOD_SOCKET
242 * O_BENEATH is arch-specific, via <asm/fcntl.h>; however we cannot include both that file
243 * and the normal <fcntl.h> as they have some clashing definitions. Bypass by directly
244 * defining O_BENEATH, using the current proposed x86 value. (This will therefore not
245 * work for non-x86, and may need changing in future if a different value gets merged.)
248 #define O_BENEATH 040000000 /* no / or .. in openat path */
252 /* Linux allows anyone to call mlock[all]/munlock[all] */
253 #define MLOCK_REQUIRES_ROOT 0
254 /* Linux allows anyone to call sched_setscheduler */
255 #define SCHED_SETSCHEDULER_REQUIRES_ROOT 1
259 #endif /*__SYSCALLS_H__*/