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