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