Home | History | Annotate | Download | only in syscall
      1 // Copyright 2009,2010 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 // DragonflyBSD 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) & ^uint64(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 Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
     96 	var _p0 unsafe.Pointer
     97 	var bufsize uintptr
     98 	if len(buf) > 0 {
     99 		_p0 = unsafe.Pointer(&buf[0])
    100 		bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
    101 	}
    102 	r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
    103 	use(unsafe.Pointer(_p0))
    104 	n = int(r0)
    105 	if e1 != 0 {
    106 		err = e1
    107 	}
    108 	return
    109 }
    110 
    111 /*
    112  * Exposed directly
    113  */
    114 //sys	Access(path string, mode uint32) (err error)
    115 //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
    116 //sys	Chdir(path string) (err error)
    117 //sys	Chflags(path string, flags int) (err error)
    118 //sys	Chmod(path string, mode uint32) (err error)
    119 //sys	Chown(path string, uid int, gid int) (err error)
    120 //sys	Chroot(path string) (err error)
    121 //sys	Close(fd int) (err error)
    122 //sys	Dup(fd int) (nfd int, err error)
    123 //sys	Dup2(from int, to int) (err error)
    124 //sys	Exit(code int)
    125 //sys	Fchdir(fd int) (err error)
    126 //sys	Fchflags(fd int, flags int) (err error)
    127 //sys	Fchmod(fd int, mode uint32) (err error)
    128 //sys	Fchown(fd int, uid int, gid int) (err error)
    129 //sys	Flock(fd int, how int) (err error)
    130 //sys	Fpathconf(fd int, name int) (val int, err error)
    131 //sys	Fstat(fd int, stat *Stat_t) (err error)
    132 //sys	Fstatfs(fd int, stat *Statfs_t) (err error)
    133 //sys	Fsync(fd int) (err error)
    134 //sys	Ftruncate(fd int, length int64) (err error)
    135 //sys	Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error)
    136 //sys	Getdtablesize() (size int)
    137 //sysnb	Getegid() (egid int)
    138 //sysnb	Geteuid() (uid int)
    139 //sysnb	Getgid() (gid int)
    140 //sysnb	Getpgid(pid int) (pgid int, err error)
    141 //sysnb	Getpgrp() (pgrp int)
    142 //sysnb	Getpid() (pid int)
    143 //sysnb	Getppid() (ppid int)
    144 //sys	Getpriority(which int, who int) (prio int, err error)
    145 //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
    146 //sysnb	Getrusage(who int, rusage *Rusage) (err error)
    147 //sysnb	Getsid(pid int) (sid int, err error)
    148 //sysnb	Gettimeofday(tv *Timeval) (err error)
    149 //sysnb	Getuid() (uid int)
    150 //sys	Issetugid() (tainted bool)
    151 //sys	Kill(pid int, signum Signal) (err error)
    152 //sys	Kqueue() (fd int, err error)
    153 //sys	Lchown(path string, uid int, gid int) (err error)
    154 //sys	Link(path string, link string) (err error)
    155 //sys	Listen(s int, backlog int) (err error)
    156 //sys	Lstat(path string, stat *Stat_t) (err error)
    157 //sys	Mkdir(path string, mode uint32) (err error)
    158 //sys	Mkfifo(path string, mode uint32) (err error)
    159 //sys	Mknod(path string, mode uint32, dev int) (err error)
    160 //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
    161 //sys	Open(path string, mode int, perm uint32) (fd int, err error)
    162 //sys	Pathconf(path string, name int) (val int, err error)
    163 //sys	read(fd int, p []byte) (n int, err error)
    164 //sys	Readlink(path string, buf []byte) (n int, err error)
    165 //sys	Rename(from string, to string) (err error)
    166 //sys	Revoke(path string) (err error)
    167 //sys	Rmdir(path string) (err error)
    168 //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
    169 //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
    170 //sysnb	Setegid(egid int) (err error)
    171 //sysnb	Seteuid(euid int) (err error)
    172 //sysnb	Setgid(gid int) (err error)
    173 //sys	Setlogin(name string) (err error)
    174 //sysnb	Setpgid(pid int, pgid int) (err error)
    175 //sys	Setpriority(which int, who int, prio int) (err error)
    176 //sysnb	Setregid(rgid int, egid int) (err error)
    177 //sysnb	Setreuid(ruid int, euid int) (err error)
    178 //sysnb	Setrlimit(which int, lim *Rlimit) (err error)
    179 //sysnb	Setsid() (pid int, err error)
    180 //sysnb	Settimeofday(tp *Timeval) (err error)
    181 //sysnb	Setuid(uid int) (err error)
    182 //sys	Stat(path string, stat *Stat_t) (err error)
    183 //sys	Statfs(path string, stat *Statfs_t) (err error)
    184 //sys	Symlink(path string, link string) (err error)
    185 //sys	Sync() (err error)
    186 //sys	Truncate(path string, length int64) (err error)
    187 //sys	Umask(newmask int) (oldmask int)
    188 //sys	Undelete(path string) (err error)
    189 //sys	Unlink(path string) (err error)
    190 //sys	Unmount(path string, flags int) (err error)
    191 //sys	write(fd int, p []byte) (n int, err error)
    192 //sys   mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
    193 //sys   munmap(addr uintptr, length uintptr) (err error)
    194 //sys	readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
    195 //sys	writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
    196 
    197 /*
    198  * Unimplemented
    199  * TODO(jsing): Update this list for DragonFly.
    200  */
    201 // Profil
    202 // Sigaction
    203 // Sigprocmask
    204 // Getlogin
    205 // Sigpending
    206 // Sigaltstack
    207 // Ioctl
    208 // Reboot
    209 // Execve
    210 // Vfork
    211 // Sbrk
    212 // Sstk
    213 // Ovadvise
    214 // Mincore
    215 // Setitimer
    216 // Swapon
    217 // Select
    218 // Sigsuspend
    219 // Readv
    220 // Writev
    221 // Nfssvc
    222 // Getfh
    223 // Quotactl
    224 // Mount
    225 // Csops
    226 // Waitid
    227 // Add_profil
    228 // Kdebug_trace
    229 // Sigreturn
    230 // Mmap
    231 // Mlock
    232 // Munlock
    233 // Atsocket
    234 // Kqueue_from_portset_np
    235 // Kqueue_portset
    236 // Getattrlist
    237 // Setattrlist
    238 // Getdirentriesattr
    239 // Searchfs
    240 // Delete
    241 // Copyfile
    242 // Poll
    243 // Watchevent
    244 // Waitevent
    245 // Modwatch
    246 // Getxattr
    247 // Fgetxattr
    248 // Setxattr
    249 // Fsetxattr
    250 // Removexattr
    251 // Fremovexattr
    252 // Listxattr
    253 // Flistxattr
    254 // Fsctl
    255 // Initgroups
    256 // Posix_spawn
    257 // Nfsclnt
    258 // Fhopen
    259 // Minherit
    260 // Semsys
    261 // Msgsys
    262 // Shmsys
    263 // Semctl
    264 // Semget
    265 // Semop
    266 // Msgctl
    267 // Msgget
    268 // Msgsnd
    269 // Msgrcv
    270 // Shmat
    271 // Shmctl
    272 // Shmdt
    273 // Shmget
    274 // Shm_open
    275 // Shm_unlink
    276 // Sem_open
    277 // Sem_close
    278 // Sem_unlink
    279 // Sem_wait
    280 // Sem_trywait
    281 // Sem_post
    282 // Sem_getvalue
    283 // Sem_init
    284 // Sem_destroy
    285 // Open_extended
    286 // Umask_extended
    287 // Stat_extended
    288 // Lstat_extended
    289 // Fstat_extended
    290 // Chmod_extended
    291 // Fchmod_extended
    292 // Access_extended
    293 // Settid
    294 // Gettid
    295 // Setsgroups
    296 // Getsgroups
    297 // Setwgroups
    298 // Getwgroups
    299 // Mkfifo_extended
    300 // Mkdir_extended
    301 // Identitysvc
    302 // Shared_region_check_np
    303 // Shared_region_map_np
    304 // __pthread_mutex_destroy
    305 // __pthread_mutex_init
    306 // __pthread_mutex_lock
    307 // __pthread_mutex_trylock
    308 // __pthread_mutex_unlock
    309 // __pthread_cond_init
    310 // __pthread_cond_destroy
    311 // __pthread_cond_broadcast
    312 // __pthread_cond_signal
    313 // Setsid_with_pid
    314 // __pthread_cond_timedwait
    315 // Aio_fsync
    316 // Aio_return
    317 // Aio_suspend
    318 // Aio_cancel
    319 // Aio_error
    320 // Aio_read
    321 // Aio_write
    322 // Lio_listio
    323 // __pthread_cond_wait
    324 // Iopolicysys
    325 // Mlockall
    326 // Munlockall
    327 // __pthread_kill
    328 // __pthread_sigmask
    329 // __sigwait
    330 // __disable_threadsignal
    331 // __pthread_markcancel
    332 // __pthread_canceled
    333 // __semwait_signal
    334 // Proc_info
    335 // Stat64_extended
    336 // Lstat64_extended
    337 // Fstat64_extended
    338 // __pthread_chdir
    339 // __pthread_fchdir
    340 // Audit
    341 // Auditon
    342 // Getauid
    343 // Setauid
    344 // Getaudit
    345 // Setaudit
    346 // Getaudit_addr
    347 // Setaudit_addr
    348 // Auditctl
    349 // Bsdthread_create
    350 // Bsdthread_terminate
    351 // Stack_snapshot
    352 // Bsdthread_register
    353 // Workq_open
    354 // Workq_ops
    355 // __mac_execve
    356 // __mac_syscall
    357 // __mac_get_file
    358 // __mac_set_file
    359 // __mac_get_link
    360 // __mac_set_link
    361 // __mac_get_proc
    362 // __mac_set_proc
    363 // __mac_get_fd
    364 // __mac_set_fd
    365 // __mac_get_pid
    366 // __mac_get_lcid
    367 // __mac_get_lctx
    368 // __mac_set_lctx
    369 // Setlcid
    370 // Read_nocancel
    371 // Write_nocancel
    372 // Open_nocancel
    373 // Close_nocancel
    374 // Wait4_nocancel
    375 // Recvmsg_nocancel
    376 // Sendmsg_nocancel
    377 // Recvfrom_nocancel
    378 // Accept_nocancel
    379 // Msync_nocancel
    380 // Fcntl_nocancel
    381 // Select_nocancel
    382 // Fsync_nocancel
    383 // Connect_nocancel
    384 // Sigsuspend_nocancel
    385 // Readv_nocancel
    386 // Writev_nocancel
    387 // Sendto_nocancel
    388 // Pread_nocancel
    389 // Pwrite_nocancel
    390 // Waitid_nocancel
    391 // Poll_nocancel
    392 // Msgsnd_nocancel
    393 // Msgrcv_nocancel
    394 // Sem_wait_nocancel
    395 // Aio_suspend_nocancel
    396 // __sigwait_nocancel
    397 // __semwait_signal_nocancel
    398 // __mac_mount
    399 // __mac_get_mount
    400 // __mac_getfsstat
    401