1
0
mirror of https://github.com/golang/go synced 2024-11-22 06:14:39 -07:00

casify linux syscall dependents, plus a few stragglers

R=rsc
DELTA=97  (0 added, 0 deleted, 97 changed)
OCL=22971
CL=22973
This commit is contained in:
Rob Pike 2009-01-16 14:16:31 -08:00
parent 83c17606d7
commit ec9f2b0cd4
6 changed files with 97 additions and 97 deletions

View File

@ -13,8 +13,8 @@ import (
) )
const ( const (
Read = syscall.EPOLLIN | syscall.EPOLLRDHUP; readFlags = syscall.EPOLLIN | syscall.EPOLLRDHUP;
Write = syscall.EPOLLOUT writeFlags = syscall.EPOLLOUT
) )
export type Pollster struct { export type Pollster struct {
@ -31,7 +31,7 @@ export func NewPollster() (p *Pollster, err *os.Error) {
// The arg to epoll_create is a hint to the kernel // The arg to epoll_create is a hint to the kernel
// about the number of FDs we will care about. // about the number of FDs we will care about.
// We don't know. // We don't know.
if p.epfd, e = syscall.epoll_create(16); e != 0 { if p.epfd, e = syscall.Epoll_create(16); e != 0 {
return nil, os.ErrnoToError(e) return nil, os.ErrnoToError(e)
} }
p.events = make(map[int64] uint32); p.events = make(map[int64] uint32);
@ -41,15 +41,15 @@ export func NewPollster() (p *Pollster, err *os.Error) {
func (p *Pollster) AddFD(fd int64, mode int, repeat bool) *os.Error { func (p *Pollster) AddFD(fd int64, mode int, repeat bool) *os.Error {
var ev syscall.EpollEvent; var ev syscall.EpollEvent;
var already bool; var already bool;
ev.fd = int32(fd); ev.Fd = int32(fd);
ev.events, already = p.events[fd]; ev.Events, already = p.events[fd];
if !repeat { if !repeat {
ev.events |= syscall.EPOLLONESHOT ev.Events |= syscall.EPOLLONESHOT
} }
if mode == 'r' { if mode == 'r' {
ev.events |= Read ev.Events |= readFlags
} else { } else {
ev.events |= Write ev.Events |= writeFlags
} }
var op int64; var op int64;
@ -58,10 +58,10 @@ func (p *Pollster) AddFD(fd int64, mode int, repeat bool) *os.Error {
} else { } else {
op = syscall.EPOLL_CTL_ADD op = syscall.EPOLL_CTL_ADD
} }
if e := syscall.epoll_ctl(p.epfd, op, fd, &ev); e != 0 { if e := syscall.Epoll_ctl(p.epfd, op, fd, &ev); e != 0 {
return os.ErrnoToError(e) return os.ErrnoToError(e)
} }
p.events[fd] = ev.events; p.events[fd] = ev.Events;
return nil return nil
} }
@ -84,14 +84,14 @@ func (p *Pollster) StopWaiting(fd int64, bits uint) {
events &= ^uint32(bits); events &= ^uint32(bits);
if int32(events) & ^syscall.EPOLLONESHOT != 0 { if int32(events) & ^syscall.EPOLLONESHOT != 0 {
var ev syscall.EpollEvent; var ev syscall.EpollEvent;
ev.fd = int32(fd); ev.Fd = int32(fd);
ev.events = events; ev.Events = events;
if e := syscall.epoll_ctl(p.epfd, syscall.EPOLL_CTL_MOD, fd, &ev); e != 0 { if e := syscall.Epoll_ctl(p.epfd, syscall.EPOLL_CTL_MOD, fd, &ev); e != 0 {
print("Epoll modify fd=", fd, ": ", os.ErrnoToError(e).String(), "\n") print("Epoll modify fd=", fd, ": ", os.ErrnoToError(e).String(), "\n")
} }
p.events[fd] = events p.events[fd] = events
} else { } else {
if e := syscall.epoll_ctl(p.epfd, syscall.EPOLL_CTL_DEL, fd, nil); e != 0 { if e := syscall.Epoll_ctl(p.epfd, syscall.EPOLL_CTL_DEL, fd, nil); e != 0 {
print("Epoll delete fd=", fd, ": ", os.ErrnoToError(e).String(), "\n") print("Epoll delete fd=", fd, ": ", os.ErrnoToError(e).String(), "\n")
} }
p.events[fd] = 0, false p.events[fd] = 0, false
@ -102,35 +102,35 @@ func (p *Pollster) WaitFD() (fd int64, mode int, err *os.Error) {
// Get an event. // Get an event.
var evarray [1]syscall.EpollEvent; var evarray [1]syscall.EpollEvent;
ev := &evarray[0]; ev := &evarray[0];
n, e := syscall.epoll_wait(p.epfd, evarray, -1); n, e := syscall.Epoll_wait(p.epfd, evarray, -1);
for e == syscall.EAGAIN || e == syscall.EINTR { for e == syscall.EAGAIN || e == syscall.EINTR {
n, e = syscall.epoll_wait(p.epfd, evarray, -1) n, e = syscall.Epoll_wait(p.epfd, evarray, -1)
} }
if e != 0 { if e != 0 {
return -1, 0, os.ErrnoToError(e) return -1, 0, os.ErrnoToError(e)
} }
fd = int64(ev.fd); fd = int64(ev.Fd);
if ev.events & Write != 0 { if ev.Events & writeFlags != 0 {
p.StopWaiting(fd, Write); p.StopWaiting(fd, writeFlags);
return fd, 'w', nil return fd, 'w', nil
} }
if ev.events & Read != 0 { if ev.Events & readFlags != 0 {
p.StopWaiting(fd, Read); p.StopWaiting(fd, readFlags);
return fd, 'r', nil return fd, 'r', nil
} }
// Other events are error conditions - wake whoever is waiting. // Other events are error conditions - wake whoever is waiting.
events, already := p.events[fd]; events, already := p.events[fd];
if events & Write != 0 { if events & writeFlags != 0 {
p.StopWaiting(fd, Write); p.StopWaiting(fd, writeFlags);
return fd, 'w', nil return fd, 'w', nil
} }
p.StopWaiting(fd, Read); p.StopWaiting(fd, readFlags);
return fd, 'r', nil return fd, 'r', nil
} }
func (p *Pollster) Close() *os.Error { func (p *Pollster) Close() *os.Error {
r, e := syscall.close(p.epfd); r, e := syscall.Close(p.epfd);
return os.ErrnoToError(e) return os.ErrnoToError(e)
} }

View File

@ -9,11 +9,11 @@ import (
"testing" "testing"
) )
func IPv4(a, b, c, d byte) []byte { func _IPv4(a, b, c, d byte) []byte {
return []byte{ 0,0,0,0, 0,0,0,0, 0,0,255,255, a,b,c,d } return []byte{ 0,0,0,0, 0,0,0,0, 0,0,255,255, a,b,c,d }
} }
func Equal(a []byte, b []byte) bool { func isEqual(a []byte, b []byte) bool {
if a == nil && b == nil { if a == nil && b == nil {
return true return true
} }
@ -28,25 +28,25 @@ func Equal(a []byte, b []byte) bool {
return true return true
} }
type ParseIPTest struct { type parseIPTest struct {
in string; in string;
out []byte; out []byte;
} }
var parseiptests = []ParseIPTest { var parseiptests = []parseIPTest {
ParseIPTest{"127.0.1.2", IPv4(127, 0, 1, 2)}, parseIPTest{"127.0.1.2", _IPv4(127, 0, 1, 2)},
ParseIPTest{"127.0.0.1", IPv4(127, 0, 0, 1)}, parseIPTest{"127.0.0.1", _IPv4(127, 0, 0, 1)},
ParseIPTest{"127.0.0.256", nil}, parseIPTest{"127.0.0.256", nil},
ParseIPTest{"abc", nil}, parseIPTest{"abc", nil},
ParseIPTest{"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)}, parseIPTest{"::ffff:127.0.0.1", _IPv4(127, 0, 0, 1)},
ParseIPTest{"2001:4860:0:2001::68", parseIPTest{"2001:4860:0:2001::68",
[]byte{0x20,0x01, 0x48,0x60, 0,0, 0x20,0x01, 0,0, 0,0, 0,0, 0x00,0x68}}, []byte{0x20,0x01, 0x48,0x60, 0,0, 0x20,0x01, 0,0, 0,0, 0,0, 0x00,0x68}},
ParseIPTest{"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)}, parseIPTest{"::ffff:4a7d:1363", _IPv4(74, 125, 19, 99)},
} }
export func TestParseIP(t *testing.T) { export func TestParseIP(t *testing.T) {
for i := 0; i < len(parseiptests); i++ { for i := 0; i < len(parseiptests); i++ {
tt := parseiptests[i]; tt := parseiptests[i];
if out := ParseIP(tt.in); !Equal(out, tt.out) { if out := ParseIP(tt.in); !isEqual(out, tt.out) {
t.Errorf("ParseIP(%#q) = %v, want %v", tt.in, out, tt.out); t.Errorf("ParseIP(%#q) = %v, want %v", tt.in, out, tt.out);
} }
} }

View File

@ -17,16 +17,16 @@ export func IPv4ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.E
return nil, os.EINVAL return nil, os.EINVAL
} }
sa := new(syscall.SockaddrInet4); sa := new(syscall.SockaddrInet4);
sa.family = syscall.AF_INET; sa.Family = syscall.AF_INET;
sa.port[0] = byte(port>>8); sa.Port[0] = byte(port>>8);
sa.port[1] = byte(port); sa.Port[1] = byte(port);
for i := 0; i < IPv4len; i++ { for i := 0; i < IPv4len; i++ {
sa.addr[i] = p[i] sa.Addr[i] = p[i]
} }
return unsafe.pointer(sa).(*syscall.Sockaddr), nil return unsafe.pointer(sa).(*syscall.Sockaddr), nil
} }
var IPv6zero [16]byte; var _IPv6zero [16]byte;
export func IPv6ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) { export func IPv6ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) {
p = ToIPv6(p); p = ToIPv6(p);
@ -38,35 +38,35 @@ export func IPv6ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.E
// In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0", // In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
// which it refuses to do. Rewrite to the IPv6 all zeros. // which it refuses to do. Rewrite to the IPv6 all zeros.
if p4 := ToIPv4(p); p4 != nil && p4[0] == 0 && p4[1] == 0 && p4[2] == 0 && p4[3] == 0 { if p4 := ToIPv4(p); p4 != nil && p4[0] == 0 && p4[1] == 0 && p4[2] == 0 && p4[3] == 0 {
p = IPv6zero; p = _IPv6zero;
} }
sa := new(syscall.SockaddrInet6); sa := new(syscall.SockaddrInet6);
sa.family = syscall.AF_INET6; sa.Family = syscall.AF_INET6;
sa.port[0] = byte(port>>8); sa.Port[0] = byte(port>>8);
sa.port[1] = byte(port); sa.Port[1] = byte(port);
for i := 0; i < IPv6len; i++ { for i := 0; i < IPv6len; i++ {
sa.addr[i] = p[i] sa.Addr[i] = p[i]
} }
return unsafe.pointer(sa).(*syscall.Sockaddr), nil return unsafe.pointer(sa).(*syscall.Sockaddr), nil
} }
export func SockaddrToIP(sa1 *syscall.Sockaddr) (p []byte, port int, err *os.Error) { export func SockaddrToIP(sa1 *syscall.Sockaddr) (p []byte, port int, err *os.Error) {
switch sa1.family { switch sa1.Family {
case syscall.AF_INET: case syscall.AF_INET:
sa := unsafe.pointer(sa1).(*syscall.SockaddrInet4); sa := unsafe.pointer(sa1).(*syscall.SockaddrInet4);
a := ToIPv6(sa.addr); a := ToIPv6(sa.Addr);
if a == nil { if a == nil {
return nil, 0, os.EINVAL return nil, 0, os.EINVAL
} }
return a, int(sa.port[0])<<8 + int(sa.port[1]), nil; return a, int(sa.Port[0])<<8 + int(sa.Port[1]), nil;
case syscall.AF_INET6: case syscall.AF_INET6:
sa := unsafe.pointer(sa1).(*syscall.SockaddrInet6); sa := unsafe.pointer(sa1).(*syscall.SockaddrInet6);
a := ToIPv6(sa.addr); a := ToIPv6(sa.Addr);
if a == nil { if a == nil {
return nil, 0, os.EINVAL return nil, 0, os.EINVAL
} }
return a, int(sa.port[0])<<8 + int(sa.port[1]), nil; return a, int(sa.Port[0])<<8 + int(sa.Port[1]), nil;
default: default:
return nil, 0, os.EINVAL return nil, 0, os.EINVAL
} }

View File

@ -9,43 +9,43 @@ import (
"testing"; "testing";
) )
type PortTest struct { type portTest struct {
netw string; netw string;
name string; name string;
port int; port int;
ok bool; ok bool;
} }
var porttests = []PortTest { var porttests = []portTest {
PortTest{ "tcp", "echo", 7, true }, portTest{ "tcp", "echo", 7, true },
PortTest{ "tcp", "discard", 9, true }, portTest{ "tcp", "discard", 9, true },
PortTest{ "tcp", "systat", 11, true }, portTest{ "tcp", "systat", 11, true },
PortTest{ "tcp", "daytime", 13, true }, portTest{ "tcp", "daytime", 13, true },
PortTest{ "tcp", "chargen", 19, true }, portTest{ "tcp", "chargen", 19, true },
PortTest{ "tcp", "ftp-data", 20, true }, portTest{ "tcp", "ftp-data", 20, true },
PortTest{ "tcp", "ftp", 21, true }, portTest{ "tcp", "ftp", 21, true },
PortTest{ "tcp", "ssh", 22, true }, portTest{ "tcp", "ssh", 22, true },
PortTest{ "tcp", "telnet", 23, true }, portTest{ "tcp", "telnet", 23, true },
PortTest{ "tcp", "smtp", 25, true }, portTest{ "tcp", "smtp", 25, true },
PortTest{ "tcp", "time", 37, true }, portTest{ "tcp", "time", 37, true },
PortTest{ "tcp", "domain", 53, true }, portTest{ "tcp", "domain", 53, true },
PortTest{ "tcp", "gopher", 70, true }, portTest{ "tcp", "gopher", 70, true },
PortTest{ "tcp", "finger", 79, true }, portTest{ "tcp", "finger", 79, true },
PortTest{ "tcp", "http", 80, true }, portTest{ "tcp", "http", 80, true },
PortTest{ "udp", "echo", 7, true }, portTest{ "udp", "echo", 7, true },
PortTest{ "udp", "tacacs", 49, true }, portTest{ "udp", "tacacs", 49, true },
PortTest{ "udp", "tftp", 69, true }, portTest{ "udp", "tftp", 69, true },
PortTest{ "udp", "bootpc", 68, true }, portTest{ "udp", "bootpc", 68, true },
PortTest{ "udp", "bootps", 67, true }, portTest{ "udp", "bootps", 67, true },
PortTest{ "udp", "domain", 53, true }, portTest{ "udp", "domain", 53, true },
PortTest{ "udp", "ntp", 123, true }, portTest{ "udp", "ntp", 123, true },
PortTest{ "udp", "snmp", 161, true }, portTest{ "udp", "snmp", 161, true },
PortTest{ "udp", "syslog", 514, true }, portTest{ "udp", "syslog", 514, true },
PortTest{ "udp", "nfs", 2049, true }, portTest{ "udp", "nfs", 2049, true },
PortTest{ "--badnet--", "zzz", 0, false }, portTest{ "--badnet--", "zzz", 0, false },
PortTest{ "tcp", "--badport--", 0, false }, portTest{ "tcp", "--badport--", 0, false },
} }
export func TestLookupPort(t *testing.T) { export func TestLookupPort(t *testing.T) {

View File

@ -11,7 +11,7 @@ import (
"testing"; "testing";
) )
func Echo(fd io.ReadWrite, done chan<- int) { func runEcho(fd io.ReadWrite, done chan<- int) {
var buf [1024]byte; var buf [1024]byte;
for { for {
@ -24,7 +24,7 @@ func Echo(fd io.ReadWrite, done chan<- int) {
done <- 1 done <- 1
} }
func Serve(t *testing.T, network, addr string, listening, done chan<- int) { func runServe(t *testing.T, network, addr string, listening, done chan<- int) {
l, err := net.Listen(network, addr); l, err := net.Listen(network, addr);
if err != nil { if err != nil {
t.Fatalf("net.Listen(%q, %q) = _, %v", network, addr, err); t.Fatalf("net.Listen(%q, %q) = _, %v", network, addr, err);
@ -37,14 +37,14 @@ func Serve(t *testing.T, network, addr string, listening, done chan<- int) {
break; break;
} }
echodone := make(chan int); echodone := make(chan int);
go Echo(fd, echodone); go runEcho(fd, echodone);
<-echodone; // make sure Echo stops <-echodone; // make sure Echo stops
l.Close(); l.Close();
} }
done <- 1 done <- 1
} }
func Connect(t *testing.T, network, addr string) { func connect(t *testing.T, network, addr string) {
fd, err := net.Dial(network, "", addr); fd, err := net.Dial(network, "", addr);
if err != nil { if err != nil {
t.Fatalf("net.Dial(%q, %q, %q) = _, %v", network, "", addr, err); t.Fatalf("net.Dial(%q, %q, %q) = _, %v", network, "", addr, err);
@ -65,21 +65,21 @@ func Connect(t *testing.T, network, addr string) {
fd.Close(); fd.Close();
} }
func DoTest(t *testing.T, network, listenaddr, dialaddr string) { func doTest(t *testing.T, network, listenaddr, dialaddr string) {
t.Logf("Test %s %s %s\n", network, listenaddr, dialaddr); t.Logf("Test %s %s %s\n", network, listenaddr, dialaddr);
listening := make(chan int); listening := make(chan int);
done := make(chan int); done := make(chan int);
go Serve(t, network, listenaddr, listening, done); go runServe(t, network, listenaddr, listening, done);
<-listening; // wait for server to start <-listening; // wait for server to start
Connect(t, network, dialaddr); connect(t, network, dialaddr);
<-done; // make sure server stopped <-done; // make sure server stopped
} }
export func TestTcpServer(t *testing.T) { export func TestTcpServer(t *testing.T) {
DoTest(t, "tcp", "0.0.0.0:9997", "127.0.0.1:9997"); doTest(t, "tcp", "0.0.0.0:9997", "127.0.0.1:9997");
DoTest(t, "tcp", "[::]:9997", "[::ffff:127.0.0.1]:9997"); doTest(t, "tcp", "[::]:9997", "[::ffff:127.0.0.1]:9997");
DoTest(t, "tcp", "[::]:9997", "127.0.0.1:9997"); doTest(t, "tcp", "[::]:9997", "127.0.0.1:9997");
DoTest(t, "tcp", ":9997", "127.0.0.1:9997"); doTest(t, "tcp", ":9997", "127.0.0.1:9997");
DoTest(t, "tcp", "0.0.0.0:9997", "[::ffff:127.0.0.1]:9997"); doTest(t, "tcp", "0.0.0.0:9997", "[::ffff:127.0.0.1]:9997");
} }

View File

@ -11,7 +11,7 @@ import (
"testing" "testing"
) )
func HammerSemaphore(s *int32, cdone chan bool) { func hammerSemaphore(s *int32, cdone chan bool) {
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
sys.semacquire(s); sys.semacquire(s);
sys.semrelease(s); sys.semrelease(s);
@ -24,7 +24,7 @@ export func TestSemaphore(t *testing.T) {
*s = 1; *s = 1;
c := make(chan bool); c := make(chan bool);
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
go HammerSemaphore(s, c); go hammerSemaphore(s, c);
} }
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
<-c; <-c;
@ -32,7 +32,7 @@ export func TestSemaphore(t *testing.T) {
} }
func HammerMutex(m *Mutex, cdone chan bool) { func hammerMutex(m *Mutex, cdone chan bool) {
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
m.Lock(); m.Lock();
m.Unlock(); m.Unlock();
@ -44,7 +44,7 @@ export func TestMutex(t *testing.T) {
m := new(Mutex); m := new(Mutex);
c := make(chan bool); c := make(chan bool);
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
go HammerMutex(m, c); go hammerMutex(m, c);
} }
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
<-c; <-c;