blob: 5af108a50385ecc31c864aa3558a215f0fcf6f58 [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// DragonFly BSD system calls.
6// This file is compiled as ordinary Go code,
7// but it is also input to mksyscall,
8// which parses the //sys lines and generates system call stubs.
9// Note that sometimes we use a lowercase //sys name and wrap
10// it in our own nicer implementation, either here or in
11// syscall_bsd.go or syscall_unix.go.
12
13package unix
14
khenaidoo106c61a2021-08-11 18:05:46 -040015import (
16 "sync"
17 "unsafe"
18)
William Kurkianea869482019-04-09 15:16:11 -040019
khenaidoo106c61a2021-08-11 18:05:46 -040020// See version list in https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/sys/sys/param.h
21var (
22 osreldateOnce sync.Once
23 osreldate uint32
24)
25
26// First __DragonFly_version after September 2019 ABI changes
27// http://lists.dragonflybsd.org/pipermail/users/2019-September/358280.html
28const _dragonflyABIChangeVersion = 500705
29
30func supportsABI(ver uint32) bool {
31 osreldateOnce.Do(func() { osreldate, _ = SysctlUint32("kern.osreldate") })
32 return osreldate >= ver
33}
David Bainbridge788e5202019-10-21 18:49:40 +000034
William Kurkianea869482019-04-09 15:16:11 -040035// SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
36type SockaddrDatalink struct {
37 Len uint8
38 Family uint8
39 Index uint16
40 Type uint8
41 Nlen uint8
42 Alen uint8
43 Slen uint8
44 Data [12]int8
45 Rcf uint16
46 Route [16]uint16
47 raw RawSockaddrDatalink
48}
49
khenaidoo106c61a2021-08-11 18:05:46 -040050func anyToSockaddrGOOS(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
51 return nil, EAFNOSUPPORT
52}
53
William Kurkianea869482019-04-09 15:16:11 -040054// Translate "kern.hostname" to []_C_int{0,1,2,3}.
55func nametomib(name string) (mib []_C_int, err error) {
56 const siz = unsafe.Sizeof(mib[0])
57
58 // NOTE(rsc): It seems strange to set the buffer to have
59 // size CTL_MAXNAME+2 but use only CTL_MAXNAME
60 // as the size. I don't know why the +2 is here, but the
61 // kernel uses +2 for its own implementation of this function.
62 // I am scared that if we don't include the +2 here, the kernel
63 // will silently write 2 words farther than we specify
64 // and we'll get memory corruption.
65 var buf [CTL_MAXNAME + 2]_C_int
66 n := uintptr(CTL_MAXNAME) * siz
67
68 p := (*byte)(unsafe.Pointer(&buf[0]))
69 bytes, err := ByteSliceFromString(name)
70 if err != nil {
71 return nil, err
72 }
73
74 // Magic sysctl: "setting" 0.3 to a string name
75 // lets you read back the array of integers form.
76 if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
77 return nil, err
78 }
79 return buf[0 : n/siz], nil
80}
81
Abhilash S.L3b494632019-07-16 15:51:09 +053082func direntIno(buf []byte) (uint64, bool) {
83 return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
84}
85
86func direntReclen(buf []byte) (uint64, bool) {
87 namlen, ok := direntNamlen(buf)
88 if !ok {
89 return 0, false
90 }
91 return (16 + namlen + 1 + 7) &^ 7, true
92}
93
94func direntNamlen(buf []byte) (uint64, bool) {
95 return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
96}
97
khenaidoo106c61a2021-08-11 18:05:46 -040098//sysnb pipe() (r int, w int, err error)
William Kurkianea869482019-04-09 15:16:11 -040099
100func Pipe(p []int) (err error) {
101 if len(p) != 2 {
102 return EINVAL
103 }
104 p[0], p[1], err = pipe()
105 return
106}
107
khenaidoo106c61a2021-08-11 18:05:46 -0400108//sysnb pipe2(p *[2]_C_int, flags int) (r int, w int, err error)
109
110func Pipe2(p []int, flags int) (err error) {
111 if len(p) != 2 {
112 return EINVAL
113 }
114 var pp [2]_C_int
115 // pipe2 on dragonfly takes an fds array as an argument, but still
116 // returns the file descriptors.
117 p[0], p[1], err = pipe2(&pp, flags)
118 return err
119}
120
William Kurkianea869482019-04-09 15:16:11 -0400121//sys extpread(fd int, p []byte, flags int, offset int64) (n int, err error)
122func Pread(fd int, p []byte, offset int64) (n int, err error) {
123 return extpread(fd, p, 0, offset)
124}
125
126//sys extpwrite(fd int, p []byte, flags int, offset int64) (n int, err error)
127func Pwrite(fd int, p []byte, offset int64) (n int, err error) {
128 return extpwrite(fd, p, 0, offset)
129}
130
131func Accept4(fd, flags int) (nfd int, sa Sockaddr, err error) {
132 var rsa RawSockaddrAny
133 var len _Socklen = SizeofSockaddrAny
134 nfd, err = accept4(fd, &rsa, &len, flags)
135 if err != nil {
136 return
137 }
138 if len > SizeofSockaddrAny {
139 panic("RawSockaddrAny too small")
140 }
141 sa, err = anyToSockaddr(fd, &rsa)
142 if err != nil {
143 Close(nfd)
144 nfd = 0
145 }
146 return
147}
148
William Kurkianea869482019-04-09 15:16:11 -0400149//sys Getcwd(buf []byte) (n int, err error) = SYS___GETCWD
150
William Kurkianea869482019-04-09 15:16:11 -0400151func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
152 var _p0 unsafe.Pointer
153 var bufsize uintptr
154 if len(buf) > 0 {
155 _p0 = unsafe.Pointer(&buf[0])
156 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
157 }
158 r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
159 n = int(r0)
160 if e1 != 0 {
161 err = e1
162 }
163 return
164}
165
166func setattrlistTimes(path string, times []Timespec, flags int) error {
167 // used on Darwin for UtimesNano
168 return ENOSYS
169}
170
171//sys ioctl(fd int, req uint, arg uintptr) (err error)
172
khenaidoo106c61a2021-08-11 18:05:46 -0400173//sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
174
William Kurkianea869482019-04-09 15:16:11 -0400175func sysctlUname(mib []_C_int, old *byte, oldlen *uintptr) error {
176 err := sysctl(mib, old, oldlen, nil, 0)
177 if err != nil {
178 // Utsname members on Dragonfly are only 32 bytes and
179 // the syscall returns ENOMEM in case the actual value
180 // is longer.
181 if err == ENOMEM {
182 err = nil
183 }
184 }
185 return err
186}
187
188func Uname(uname *Utsname) error {
189 mib := []_C_int{CTL_KERN, KERN_OSTYPE}
190 n := unsafe.Sizeof(uname.Sysname)
191 if err := sysctlUname(mib, &uname.Sysname[0], &n); err != nil {
192 return err
193 }
194 uname.Sysname[unsafe.Sizeof(uname.Sysname)-1] = 0
195
196 mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
197 n = unsafe.Sizeof(uname.Nodename)
198 if err := sysctlUname(mib, &uname.Nodename[0], &n); err != nil {
199 return err
200 }
201 uname.Nodename[unsafe.Sizeof(uname.Nodename)-1] = 0
202
203 mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
204 n = unsafe.Sizeof(uname.Release)
205 if err := sysctlUname(mib, &uname.Release[0], &n); err != nil {
206 return err
207 }
208 uname.Release[unsafe.Sizeof(uname.Release)-1] = 0
209
210 mib = []_C_int{CTL_KERN, KERN_VERSION}
211 n = unsafe.Sizeof(uname.Version)
212 if err := sysctlUname(mib, &uname.Version[0], &n); err != nil {
213 return err
214 }
215
216 // The version might have newlines or tabs in it, convert them to
217 // spaces.
218 for i, b := range uname.Version {
219 if b == '\n' || b == '\t' {
220 if i == len(uname.Version)-1 {
221 uname.Version[i] = 0
222 } else {
223 uname.Version[i] = ' '
224 }
225 }
226 }
227
228 mib = []_C_int{CTL_HW, HW_MACHINE}
229 n = unsafe.Sizeof(uname.Machine)
230 if err := sysctlUname(mib, &uname.Machine[0], &n); err != nil {
231 return err
232 }
233 uname.Machine[unsafe.Sizeof(uname.Machine)-1] = 0
234
235 return nil
236}
237
238func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
239 if raceenabled {
240 raceReleaseMerge(unsafe.Pointer(&ioSync))
241 }
242 return sendfile(outfd, infd, offset, count)
243}
244
245/*
246 * Exposed directly
247 */
248//sys Access(path string, mode uint32) (err error)
249//sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
250//sys Chdir(path string) (err error)
251//sys Chflags(path string, flags int) (err error)
252//sys Chmod(path string, mode uint32) (err error)
253//sys Chown(path string, uid int, gid int) (err error)
254//sys Chroot(path string) (err error)
255//sys Close(fd int) (err error)
256//sys Dup(fd int) (nfd int, err error)
257//sys Dup2(from int, to int) (err error)
258//sys Exit(code int)
259//sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
260//sys Fchdir(fd int) (err error)
261//sys Fchflags(fd int, flags int) (err error)
262//sys Fchmod(fd int, mode uint32) (err error)
263//sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
264//sys Fchown(fd int, uid int, gid int) (err error)
265//sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
266//sys Flock(fd int, how int) (err error)
267//sys Fpathconf(fd int, name int) (val int, err error)
268//sys Fstat(fd int, stat *Stat_t) (err error)
269//sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
270//sys Fstatfs(fd int, stat *Statfs_t) (err error)
271//sys Fsync(fd int) (err error)
272//sys Ftruncate(fd int, length int64) (err error)
Abhilash S.L3b494632019-07-16 15:51:09 +0530273//sys Getdents(fd int, buf []byte) (n int, err error)
William Kurkianea869482019-04-09 15:16:11 -0400274//sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error)
275//sys Getdtablesize() (size int)
276//sysnb Getegid() (egid int)
277//sysnb Geteuid() (uid int)
278//sysnb Getgid() (gid int)
279//sysnb Getpgid(pid int) (pgid int, err error)
280//sysnb Getpgrp() (pgrp int)
281//sysnb Getpid() (pid int)
282//sysnb Getppid() (ppid int)
283//sys Getpriority(which int, who int) (prio int, err error)
284//sysnb Getrlimit(which int, lim *Rlimit) (err error)
285//sysnb Getrusage(who int, rusage *Rusage) (err error)
286//sysnb Getsid(pid int) (sid int, err error)
287//sysnb Gettimeofday(tv *Timeval) (err error)
288//sysnb Getuid() (uid int)
289//sys Issetugid() (tainted bool)
290//sys Kill(pid int, signum syscall.Signal) (err error)
291//sys Kqueue() (fd int, err error)
292//sys Lchown(path string, uid int, gid int) (err error)
293//sys Link(path string, link string) (err error)
294//sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
295//sys Listen(s int, backlog int) (err error)
296//sys Lstat(path string, stat *Stat_t) (err error)
297//sys Mkdir(path string, mode uint32) (err error)
298//sys Mkdirat(dirfd int, path string, mode uint32) (err error)
299//sys Mkfifo(path string, mode uint32) (err error)
300//sys Mknod(path string, mode uint32, dev int) (err error)
301//sys Mknodat(fd int, path string, mode uint32, dev int) (err error)
302//sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
303//sys Open(path string, mode int, perm uint32) (fd int, err error)
304//sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
305//sys Pathconf(path string, name int) (val int, err error)
306//sys read(fd int, p []byte) (n int, err error)
307//sys Readlink(path string, buf []byte) (n int, err error)
308//sys Rename(from string, to string) (err error)
309//sys Renameat(fromfd int, from string, tofd int, to string) (err error)
310//sys Revoke(path string) (err error)
311//sys Rmdir(path string) (err error)
312//sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
David Bainbridge788e5202019-10-21 18:49:40 +0000313//sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
William Kurkianea869482019-04-09 15:16:11 -0400314//sysnb Setegid(egid int) (err error)
315//sysnb Seteuid(euid int) (err error)
316//sysnb Setgid(gid int) (err error)
317//sys Setlogin(name string) (err error)
318//sysnb Setpgid(pid int, pgid int) (err error)
319//sys Setpriority(which int, who int, prio int) (err error)
320//sysnb Setregid(rgid int, egid int) (err error)
321//sysnb Setreuid(ruid int, euid int) (err error)
322//sysnb Setresgid(rgid int, egid int, sgid int) (err error)
323//sysnb Setresuid(ruid int, euid int, suid int) (err error)
324//sysnb Setrlimit(which int, lim *Rlimit) (err error)
325//sysnb Setsid() (pid int, err error)
326//sysnb Settimeofday(tp *Timeval) (err error)
327//sysnb Setuid(uid int) (err error)
328//sys Stat(path string, stat *Stat_t) (err error)
329//sys Statfs(path string, stat *Statfs_t) (err error)
330//sys Symlink(path string, link string) (err error)
331//sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
332//sys Sync() (err error)
333//sys Truncate(path string, length int64) (err error)
334//sys Umask(newmask int) (oldmask int)
335//sys Undelete(path string) (err error)
336//sys Unlink(path string) (err error)
337//sys Unlinkat(dirfd int, path string, flags int) (err error)
338//sys Unmount(path string, flags int) (err error)
339//sys write(fd int, p []byte) (n int, err error)
khenaidoo106c61a2021-08-11 18:05:46 -0400340//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
341//sys munmap(addr uintptr, length uintptr) (err error)
William Kurkianea869482019-04-09 15:16:11 -0400342//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
343//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
344//sys accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error)
345//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
346
347/*
348 * Unimplemented
349 * TODO(jsing): Update this list for DragonFly.
350 */
351// Profil
352// Sigaction
353// Sigprocmask
354// Getlogin
355// Sigpending
356// Sigaltstack
357// Reboot
358// Execve
359// Vfork
360// Sbrk
361// Sstk
362// Ovadvise
363// Mincore
364// Setitimer
365// Swapon
366// Select
367// Sigsuspend
368// Readv
369// Writev
370// Nfssvc
371// Getfh
372// Quotactl
373// Mount
374// Csops
375// Waitid
376// Add_profil
377// Kdebug_trace
378// Sigreturn
379// Atsocket
380// Kqueue_from_portset_np
381// Kqueue_portset
382// Getattrlist
383// Setattrlist
384// Getdirentriesattr
385// Searchfs
386// Delete
387// Copyfile
388// Watchevent
389// Waitevent
390// Modwatch
391// Getxattr
392// Fgetxattr
393// Setxattr
394// Fsetxattr
395// Removexattr
396// Fremovexattr
397// Listxattr
398// Flistxattr
399// Fsctl
400// Initgroups
401// Posix_spawn
402// Nfsclnt
403// Fhopen
404// Minherit
405// Semsys
406// Msgsys
407// Shmsys
408// Semctl
409// Semget
410// Semop
411// Msgctl
412// Msgget
413// Msgsnd
414// Msgrcv
415// Shmat
416// Shmctl
417// Shmdt
418// Shmget
419// Shm_open
420// Shm_unlink
421// Sem_open
422// Sem_close
423// Sem_unlink
424// Sem_wait
425// Sem_trywait
426// Sem_post
427// Sem_getvalue
428// Sem_init
429// Sem_destroy
430// Open_extended
431// Umask_extended
432// Stat_extended
433// Lstat_extended
434// Fstat_extended
435// Chmod_extended
436// Fchmod_extended
437// Access_extended
438// Settid
439// Gettid
440// Setsgroups
441// Getsgroups
442// Setwgroups
443// Getwgroups
444// Mkfifo_extended
445// Mkdir_extended
446// Identitysvc
447// Shared_region_check_np
448// Shared_region_map_np
449// __pthread_mutex_destroy
450// __pthread_mutex_init
451// __pthread_mutex_lock
452// __pthread_mutex_trylock
453// __pthread_mutex_unlock
454// __pthread_cond_init
455// __pthread_cond_destroy
456// __pthread_cond_broadcast
457// __pthread_cond_signal
458// Setsid_with_pid
459// __pthread_cond_timedwait
460// Aio_fsync
461// Aio_return
462// Aio_suspend
463// Aio_cancel
464// Aio_error
465// Aio_read
466// Aio_write
467// Lio_listio
468// __pthread_cond_wait
469// Iopolicysys
470// __pthread_kill
471// __pthread_sigmask
472// __sigwait
473// __disable_threadsignal
474// __pthread_markcancel
475// __pthread_canceled
476// __semwait_signal
477// Proc_info
478// Stat64_extended
479// Lstat64_extended
480// Fstat64_extended
481// __pthread_chdir
482// __pthread_fchdir
483// Audit
484// Auditon
485// Getauid
486// Setauid
487// Getaudit
488// Setaudit
489// Getaudit_addr
490// Setaudit_addr
491// Auditctl
492// Bsdthread_create
493// Bsdthread_terminate
494// Stack_snapshot
495// Bsdthread_register
496// Workq_open
497// Workq_ops
498// __mac_execve
499// __mac_syscall
500// __mac_get_file
501// __mac_set_file
502// __mac_get_link
503// __mac_set_link
504// __mac_get_proc
505// __mac_set_proc
506// __mac_get_fd
507// __mac_set_fd
508// __mac_get_pid
509// __mac_get_lcid
510// __mac_get_lctx
511// __mac_set_lctx
512// Setlcid
513// Read_nocancel
514// Write_nocancel
515// Open_nocancel
516// Close_nocancel
517// Wait4_nocancel
518// Recvmsg_nocancel
519// Sendmsg_nocancel
520// Recvfrom_nocancel
521// Accept_nocancel
522// Fcntl_nocancel
523// Select_nocancel
524// Fsync_nocancel
525// Connect_nocancel
526// Sigsuspend_nocancel
527// Readv_nocancel
528// Writev_nocancel
529// Sendto_nocancel
530// Pread_nocancel
531// Pwrite_nocancel
532// Waitid_nocancel
533// Msgsnd_nocancel
534// Msgrcv_nocancel
535// Sem_wait_nocancel
536// Aio_suspend_nocancel
537// __sigwait_nocancel
538// __semwait_signal_nocancel
539// __mac_mount
540// __mac_get_mount
541// __mac_getfsstat