func (file *File) readdirnames(n int) (names []string, err error) { if elen == 0 { var dummy syscall.Dirent elen = (int(unsafe.Offsetof(dummy.Name)) + libc_pathconf(syscall.StringBytePtr(file.name), syscall.PC_NAME_MAX) + 1) } if file.dirinfo == nil { file.dirinfo = new(dirInfo) file.dirinfo.buf = make([]byte, elen) p := syscall.StringBytePtr(file.name) syscall.Entersyscall() r := libc_opendir(p) syscall.Exitsyscall() file.dirinfo.dir = r } entry_dirent := (*syscall.Dirent)(unsafe.Pointer(&file.dirinfo.buf[0])) size := n if size <= 0 { size = 100 n = -1 } names = make([]string, 0, size) // Empty with room to grow. dir := file.dirinfo.dir if dir == nil { return names, NewSyscallError("opendir", syscall.GetErrno()) } for n != 0 { var result *syscall.Dirent pr := &result syscall.Entersyscall() i := libc_readdir_r(dir, entry_dirent, pr) syscall.Exitsyscall() if i != 0 { return names, NewSyscallError("readdir_r", i) } if result == nil { break // EOF } var name = string(result.Name[0:clen(result.Name[0:])]) if name == "." || name == ".." { // Useless names continue } names = append(names, name) n-- } if n >= 0 && len(names) == 0 { return names, io.EOF } return names, nil }
func lookupUnix(uid int, username string, lookupByName bool) (*User, error) { var pwd syscall.Passwd var result *syscall.Passwd // FIXME: Should let buf grow if necessary. const bufSize = 1024 buf := make([]byte, bufSize) if lookupByName { nameC := syscall.StringBytePtr(username) syscall.Entersyscall() rv := libc_getpwnam_r(nameC, &pwd, &buf[0], bufSize, &result) syscall.Exitsyscall() if rv != 0 { return nil, fmt.Errorf("user: lookup username %s: %s", username, syscall.GetErrno()) } if result == nil { return nil, UnknownUserError(username) } } else { syscall.Entersyscall() rv := libc_getpwuid_r(syscall.Uid_t(uid), &pwd, &buf[0], bufSize, &result) syscall.Exitsyscall() if rv != 0 { return nil, fmt.Errorf("user: lookup userid %d: %s", uid, syscall.GetErrno()) } if result == nil { return nil, UnknownUserIdError(uid) } } u := &User{ Uid: strconv.Itoa(int(pwd.Pw_uid)), Gid: strconv.Itoa(int(pwd.Pw_gid)), Username: bytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_name))), Name: bytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_gecos))), HomeDir: bytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_dir))), } // The pw_gecos field isn't quite standardized. Some docs // say: "It is expected to be a comma separated list of // personal data where the first item is the full name of the // user." if i := strings.Index(u.Name, ","); i >= 0 { u.Name = u.Name[:i] } return u, nil }
func dupCloseOnExec(fd int) (newfd int, err error) { if atomic.LoadInt32(&tryDupCloexec) == 1 && syscall.F_DUPFD_CLOEXEC != 0 { syscall.Entersyscall() r0, errno := fcntl(uintptr(fd), syscall.F_DUPFD_CLOEXEC, 0) syscall.Exitsyscall() e1 := syscall.Errno(errno) if runtime.GOOS == "darwin" && e1 == syscall.EBADF { // On OS X 10.6 and below (but we only support // >= 10.6), F_DUPFD_CLOEXEC is unsupported // and fcntl there falls back (undocumented) // to doing an ioctl instead, returning EBADF // in this case because fd is not of the // expected device fd type. Treat it as // EINVAL instead, so we fall back to the // normal dup path. // TODO: only do this on 10.6 if we can detect 10.6 // cheaply. e1 = syscall.EINVAL } switch e1 { case 0: return int(r0), nil case syscall.EINVAL: // Old kernel. Fall back to the portable way // from now on. atomic.StoreInt32(&tryDupCloexec, 0) default: return -1, os.NewSyscallError("fcntl", e1) } } return dupCloseOnExecOld(fd) }
func lookupUnixUid(uid int) (*User, error) { var pwd syscall.Passwd var result *syscall.Passwd buf := alloc(userBuffer) defer buf.free() err := retryWithBuffer(buf, func() syscall.Errno { syscall.Entersyscall() rv := libc_getpwuid_r(syscall.Uid_t(uid), &pwd, buf.ptr, buf.size, &result) syscall.Exitsyscall() if rv != 0 { return syscall.GetErrno() } return 0 }) if err != nil { return nil, fmt.Errorf("user: lookup userid %d: %v", uid, err) } if result == nil { return nil, UnknownUserIdError(uid) } return buildUser(&pwd), nil }
func lookupUser(username string) (*User, error) { var pwd syscall.Passwd var result *syscall.Passwd p := syscall.StringBytePtr(username) buf := alloc(userBuffer) defer buf.free() err := retryWithBuffer(buf, func() syscall.Errno { syscall.Entersyscall() rv := libc_getpwnam_r(p, &pwd, buf.ptr, buf.size, &result) syscall.Exitsyscall() if rv != 0 { return syscall.GetErrno() } return 0 }) if err != nil { return nil, fmt.Errorf("user: lookup username %s: %v", username, err) } if result == nil { return nil, UnknownUserError(username) } return buildUser(&pwd), err }
func lookupUnixGid(gid int) (*Group, error) { var grp syscall.Group var result *syscall.Group buf := alloc(groupBuffer) defer buf.free() err := retryWithBuffer(buf, func() syscall.Errno { syscall.Entersyscall() rv := libc_getgrgid_r(syscall.Gid_t(gid), &grp, buf.ptr, buf.size, &result) syscall.Exitsyscall() if rv != 0 { return syscall.GetErrno() } return 0 }) if err != nil { return nil, fmt.Errorf("user: lookup groupid %d: %v", gid, err) } if result == nil { return nil, UnknownGroupIdError(strconv.Itoa(gid)) } return buildGroup(&grp), nil }
func lookupGroup(groupname string) (*Group, error) { var grp syscall.Group var result *syscall.Group buf := alloc(groupBuffer) defer buf.free() p := syscall.StringBytePtr(groupname) err := retryWithBuffer(buf, func() syscall.Errno { syscall.Entersyscall() rv := libc_getgrnam_r(p, &grp, buf.ptr, buf.size, &result) syscall.Exitsyscall() if rv != 0 { return syscall.GetErrno() } return 0 }) if err != nil { return nil, fmt.Errorf("user: lookup groupname %s: %v", groupname, err) } if result == nil { return nil, UnknownGroupError(groupname) } return buildGroup(&grp), nil }
func (file *file) close() error { if file == nil || file.fd < 0 { return syscall.EINVAL } var err error if e := syscall.Close(file.fd); e != nil { err = &PathError{"close", file.name, e} } if file.dirinfo != nil { syscall.Entersyscall() i := libc_closedir(file.dirinfo.dir) errno := syscall.GetErrno() syscall.Exitsyscall() file.dirinfo = nil if i < 0 && err == nil { err = &PathError{"closedir", file.name, errno} } } file.fd = -1 // so it can't be closed again // no need for a finalizer anymore runtime.SetFinalizer(file, nil) return err }
func cgoLookupPort(network, service string) (port int, err error, completed bool) { acquireThread() defer releaseThread() var hints syscall.Addrinfo switch network { case "": // no hints case "tcp", "tcp4", "tcp6": hints.Ai_socktype = syscall.SOCK_STREAM hints.Ai_protocol = syscall.IPPROTO_TCP case "udp", "udp4", "udp6": hints.Ai_socktype = syscall.SOCK_DGRAM hints.Ai_protocol = syscall.IPPROTO_UDP default: return 0, &DNSError{Err: "unknown network", Name: network + "/" + service}, true } if len(network) >= 4 { switch network[3] { case '4': hints.Ai_family = syscall.AF_INET case '6': hints.Ai_family = syscall.AF_INET6 } } s := syscall.StringBytePtr(service) var res *syscall.Addrinfo syscall.Entersyscall() gerrno := libc_getaddrinfo(nil, s, &hints, &res) syscall.Exitsyscall() if gerrno != 0 { switch gerrno { case syscall.EAI_SYSTEM: errno := syscall.GetErrno() if errno == 0 { // see golang.org/issue/6232 errno = syscall.EMFILE } err = errno default: err = addrinfoErrno(gerrno) } return 0, &DNSError{Err: err.Error(), Name: network + "/" + service}, true } defer libc_freeaddrinfo(res) for r := res; r != nil; r = r.Ai_next { switch r.Ai_family { case syscall.AF_INET: sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr)) p := (*[2]byte)(unsafe.Pointer(&sa.Port)) return int(p[0])<<8 | int(p[1]), nil, true case syscall.AF_INET6: sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr)) p := (*[2]byte)(unsafe.Pointer(&sa.Port)) return int(p[0])<<8 | int(p[1]), nil, true } } return 0, &DNSError{Err: "unknown port", Name: network + "/" + service}, true }
func cgoLookupIPCNAME(name string) (addrs []IP, cname string, err error, completed bool) { var res *syscall.Addrinfo var hints syscall.Addrinfo // NOTE(rsc): In theory there are approximately balanced // arguments for and against including AI_ADDRCONFIG // in the flags (it includes IPv4 results only on IPv4 systems, // and similarly for IPv6), but in practice setting it causes // getaddrinfo to return the wrong canonical name on Linux. // So definitely leave it out. hints.Ai_flags = int32((syscall.AI_ALL | syscall.AI_V4MAPPED | syscall.AI_CANONNAME) & cgoAddrInfoMask()) h := syscall.StringBytePtr(name) syscall.Entersyscall() gerrno := libc_getaddrinfo(h, nil, &hints, &res) syscall.Exitsyscall() if gerrno != 0 { var str string if gerrno == syscall.EAI_NONAME { str = noSuchHost } else if gerrno == syscall.EAI_SYSTEM { str = syscall.GetErrno().Error() } else { str = bytePtrToString(libc_gai_strerror(gerrno)) } return nil, "", &DNSError{Err: str, Name: name}, true } defer libc_freeaddrinfo(res) if res != nil { cname = bytePtrToString((*byte)(unsafe.Pointer(res.Ai_canonname))) if cname == "" { cname = name } if len(cname) > 0 && cname[len(cname)-1] != '.' { cname += "." } } for r := res; r != nil; r = r.Ai_next { // Everything comes back twice, once for UDP and once for TCP. if r.Ai_socktype != syscall.SOCK_STREAM { continue } switch r.Ai_family { default: continue case syscall.AF_INET: sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr)) addrs = append(addrs, copyIP(sa.Addr[:])) case syscall.AF_INET6: sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr)) addrs = append(addrs, copyIP(sa.Addr[:])) } } return addrs, cname, nil, true }
func (libcConn) writeString(p Priority, hostname, tag, msg, nl string) error { timestamp := time.Now().Format(time.RFC3339) log := fmt.Sprintf("%s %s %s[%d]: %s%s", timestamp, hostname, tag, os.Getpid(), msg, nl) buf, err := syscall.BytePtrFromString(log) if err != nil { return err } syscall.Entersyscall() syslog_c(int(p), buf) syscall.Exitsyscall() return nil }
func cgoNameinfoPTR(b []byte, sa *syscall.RawSockaddr, salen syscall.Socklen_t) (int, error) { syscall.Entersyscall() gerrno := libc_getnameinfo(sa, salen, &b[0], syscall.Size_t(len(b)), nil, 0, syscall.NI_NAMEREQD) syscall.Exitsyscall() var err error if gerrno == syscall.EAI_SYSTEM { errno := syscall.GetErrno() if errno != 0 { err = errno } } return gerrno, err }
func cgoLookupPort(net, service string) (port int, err error, completed bool) { acquireThread() defer releaseThread() var res *syscall.Addrinfo var hints syscall.Addrinfo switch net { case "": // no hints case "tcp", "tcp4", "tcp6": hints.Ai_socktype = syscall.SOCK_STREAM hints.Ai_protocol = syscall.IPPROTO_TCP case "udp", "udp4", "udp6": hints.Ai_socktype = syscall.SOCK_DGRAM hints.Ai_protocol = syscall.IPPROTO_UDP default: return 0, UnknownNetworkError(net), true } if len(net) >= 4 { switch net[3] { case '4': hints.Ai_family = syscall.AF_INET case '6': hints.Ai_family = syscall.AF_INET6 } } s := syscall.StringBytePtr(service) syscall.Entersyscall() gerrno := libc_getaddrinfo(nil, s, &hints, &res) syscall.Exitsyscall() if gerrno == 0 { defer libc_freeaddrinfo(res) for r := res; r != nil; r = r.Ai_next { switch r.Ai_family { default: continue case syscall.AF_INET: sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr)) p := (*[2]byte)(unsafe.Pointer(&sa.Port)) return int(p[0])<<8 | int(p[1]), nil, true case syscall.AF_INET6: sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr)) p := (*[2]byte)(unsafe.Pointer(&sa.Port)) return int(p[0])<<8 | int(p[1]), nil, true } } } return 0, &AddrError{"unknown port", net + "/" + service}, true }
func cgoLookupIPCNAME(name string) (addrs []IP, cname string, err error, completed bool) { var res *syscall.Addrinfo var hints syscall.Addrinfo hints.Ai_flags = int32(cgoAddrInfoFlags()) hints.Ai_socktype = syscall.SOCK_STREAM h := syscall.StringBytePtr(name) syscall.Entersyscall() gerrno := libc_getaddrinfo(h, nil, &hints, &res) syscall.Exitsyscall() if gerrno != 0 { var str string if gerrno == syscall.EAI_NONAME { str = noSuchHost } else if gerrno == syscall.EAI_SYSTEM { str = syscall.GetErrno().Error() } else { str = bytePtrToString(libc_gai_strerror(gerrno)) } return nil, "", &DNSError{Err: str, Name: name}, true } defer libc_freeaddrinfo(res) if res != nil { cname = bytePtrToString((*byte)(unsafe.Pointer(res.Ai_canonname))) if cname == "" { cname = name } if len(cname) > 0 && cname[len(cname)-1] != '.' { cname += "." } } for r := res; r != nil; r = r.Ai_next { // We only asked for SOCK_STREAM, but check anyhow. if r.Ai_socktype != syscall.SOCK_STREAM { continue } switch r.Ai_family { default: continue case syscall.AF_INET: sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr)) addrs = append(addrs, copyIP(sa.Addr[:])) case syscall.AF_INET6: sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr)) addrs = append(addrs, copyIP(sa.Addr[:])) } } return addrs, cname, nil, true }
func listGroups(u *User) ([]string, error) { ug, err := strconv.Atoi(u.Gid) if err != nil { return nil, fmt.Errorf("user: list groups for %s: invalid gid %q", u.Username, u.Gid) } userGID := syscall.Gid_t(ug) nameC, err := syscall.BytePtrFromString(u.Username) if err != nil { return nil, fmt.Errorf("user: invalid user name %q: %v", strconv.Quote(u.Username), err) } n := int32(256) gidsC := make([]syscall.Gid_t, n) syscall.Entersyscall() rv := libc_getgrouplist(nameC, userGID, &gidsC[0], &n) syscall.Exitsyscall() if rv == -1 { // More than initial buffer, but now n contains the correct size. const maxGroups = 2048 if n > maxGroups { return nil, fmt.Errorf("user: list groups for %s: member of more than %d groups", u.Username, maxGroups) } gidsC = make([]syscall.Gid_t, n) syscall.Entersyscall() rv := libc_getgrouplist(nameC, userGID, &gidsC[0], &n) syscall.Exitsyscall() if rv == -1 { return nil, fmt.Errorf("user: list groups for %s failed (changed groups?)", u.Username) } } gidsC = gidsC[:n] gids := make([]string, 0, n) for _, g := range gidsC[:n] { gids = append(gids, strconv.Itoa(int(g))) } return gids, nil }
func (file *File) readdirnames(n int) (names []string, err error) { if file.dirinfo == nil { p, err := syscall.BytePtrFromString(file.name) if err != nil { return nil, err } elen := int(atomic.LoadInt32(&nameMax)) if elen == 0 { syscall.Entersyscall() plen := libc_pathconf(p, syscall.PC_NAME_MAX) syscall.Exitsyscall() if plen < 1024 { plen = 1024 } var dummy syscall.Dirent elen = int(unsafe.Offsetof(dummy.Name)) + plen + 1 atomic.StoreInt32(&nameMax, int32(elen)) } syscall.Entersyscall() r := libc_opendir(p) errno := syscall.GetErrno() syscall.Exitsyscall() if r == nil { return nil, &PathError{"opendir", file.name, errno} } file.dirinfo = new(dirInfo) file.dirinfo.buf = make([]byte, elen) file.dirinfo.dir = r } entryDirent := (*syscall.Dirent)(unsafe.Pointer(&file.dirinfo.buf[0])) size := n if size <= 0 { size = 100 n = -1 } names = make([]string, 0, size) // Empty with room to grow. for n != 0 { var dirent *syscall.Dirent pr := &dirent syscall.Entersyscall() i := libc_readdir_r(file.dirinfo.dir, entryDirent, pr) syscall.Exitsyscall() if i != 0 { return names, NewSyscallError("readdir_r", i) } if dirent == nil { break // EOF } bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0])) var name = string(bytes[0:clen(bytes[:])]) if name == "." || name == ".." { // Useless names continue } names = append(names, name) n-- } if n >= 0 && len(names) == 0 { return names, io.EOF } return names, nil }
func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno) { syscall.Entersyscall() r, errno := realSyscall(trap, a1, a2, a3, a4, a5, a6) syscall.Exitsyscall() return r, 0, syscall.Errno(errno) }
func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, completed bool) { acquireThread() defer releaseThread() var hints syscall.Addrinfo hints.Ai_flags = int32(cgoAddrInfoFlags) hints.Ai_socktype = syscall.SOCK_STREAM h := syscall.StringBytePtr(name) var res *syscall.Addrinfo syscall.Entersyscall() gerrno := libc_getaddrinfo(h, nil, &hints, &res) syscall.Exitsyscall() if gerrno != 0 { switch gerrno { case syscall.EAI_SYSTEM: errno := syscall.GetErrno() if errno == 0 { // err should not be nil, but sometimes getaddrinfo returns // gerrno == C.EAI_SYSTEM with err == nil on Linux. // The report claims that it happens when we have too many // open files, so use syscall.EMFILE (too many open files in system). // Most system calls would return ENFILE (too many open files), // so at the least EMFILE should be easy to recognize if this // comes up again. golang.org/issue/6232. errno = syscall.EMFILE } err = errno case syscall.EAI_NONAME: err = errNoSuchHost default: err = addrinfoErrno(gerrno) } return nil, "", &DNSError{Err: err.Error(), Name: name}, true } defer libc_freeaddrinfo(res) if res != nil { cname = bytePtrToString((*byte)(unsafe.Pointer(res.Ai_canonname))) if cname == "" { cname = name } if len(cname) > 0 && cname[len(cname)-1] != '.' { cname += "." } } for r := res; r != nil; r = r.Ai_next { // We only asked for SOCK_STREAM, but check anyhow. if r.Ai_socktype != syscall.SOCK_STREAM { continue } switch r.Ai_family { case syscall.AF_INET: sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr)) addr := IPAddr{IP: copyIP(sa.Addr[:])} addrs = append(addrs, addr) case syscall.AF_INET6: sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr)) addr := IPAddr{IP: copyIP(sa.Addr[:]), Zone: zoneToString(int(sa.Scope_id))} addrs = append(addrs, addr) } } return addrs, cname, nil, true }