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