本文整理匯總了Golang中syscall.Entersyscall函數的典型用法代碼示例。如果您正苦於以下問題:Golang Entersyscall函數的具體用法?Golang Entersyscall怎麽用?Golang Entersyscall使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Entersyscall函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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
}
示例2: 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
}
示例3: dupCloseOnExec
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)
}
示例4: 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
}
示例5: 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
}
示例6: 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
}
示例7: 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
}
示例8: 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
}
示例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: 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
}
示例11: writeString
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
}
示例12: 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
}
示例13: cgoLookupPort
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
}
示例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: listGroups
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
}