本文整理匯總了Golang中syscall.GetErrno函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetErrno函數的具體用法?Golang GetErrno怎麽用?Golang GetErrno使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetErrno函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: lookupUnix
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
}
示例2: sysconf
func sysconf(name int) (n int64, err syscall.Errno) {
r := realSysconf(name)
if r < 0 {
return 0, syscall.GetErrno()
}
return r, 0
}
示例3: gettimeofday
func gettimeofday(tv *Timeval) (err syscall.Errno) {
r := realGettimeofday(tv, nil)
if r < 0 {
return syscall.GetErrno()
}
return 0
}
示例4: close
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
}
示例5: lookupUnixUid
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
}
示例6: lookupUser
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
}
示例7: lookupUnixGid
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
}
示例8: lookupGroup
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
}
示例9: cgoLookupPort
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
}
示例10: GetSize
// GetSize returns the dimensions of the given terminal.
func GetSize(fd int) (width, height int, err error) {
var dimensions [4]uint16
if ioctl(fd, syscall.TIOCGWINSZ, unsafe.Pointer(&dimensions)) < 0 {
return -1, -1, syscall.GetErrno()
}
return int(dimensions[1]), int(dimensions[0]), nil
}
示例11: readdirnames
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
}
示例12: cgoLookupIPCNAME
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
}
示例13: cgoNameinfoPTR
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
}
示例14: cgoLookupIPCNAME
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
}
示例15: Readdirnames
// Negative count means read until EOF.
func (file *File) Readdirnames(count int) (names []string, err Error) {
if elen == 0 {
var dummy syscall.Dirent;
elen = (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)
file.dirinfo.dir = libc_opendir(syscall.StringBytePtr(file.name))
}
entry_dirent := unsafe.Pointer(&file.dirinfo.buf[0]).(*syscall.Dirent)
size := count
if size < 0 {
size = 100
}
names = make([]string, 0, size) // Empty with room to grow.
dir := file.dirinfo.dir
if dir == nil {
return names, NewSyscallError("opendir", syscall.GetErrno())
}
for count != 0 {
var result *syscall.Dirent
i := libc_readdir_r(dir, entry_dirent, &result)
if result == nil {
break
}
var name = string(result.Name[0:clen(result.Name[0:])])
if name == "." || name == ".." { // Useless names
continue
}
count--
names = append(names, name)
}
return names, nil
}