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