summaryrefslogtreecommitdiff
path: root/src/pkg/syscall/syscall_linux_386.go
blob: b41c60a552f53cd815d6091a93c85d960dab9955 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package syscall

import (
	"syscall";
	"unsafe";
)

func Getpagesize() int {
	return 4096
}

func TimespecToNsec(ts Timespec) int64 {
	return int64(ts.Sec)*1e9 + int64(ts.Nsec);
}

func NsecToTimespec(nsec int64) (ts Timespec) {
	ts.Sec = int32(nsec / 1e9);
	ts.Nsec = int32(nsec % 1e9);
	return;
}

func TimevalToNsec(tv Timeval) int64 {
	return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3;
}

func NsecToTimeval(nsec int64) (tv Timeval) {
	nsec += 999;	// round up to microsecond
	tv.Sec = int32(nsec/1e9);
	tv.Usec = int32(nsec%1e9 / 1e3);
	return;
}

// 64-bit file system and 32-bit uid calls
// (386 default is 32-bit file system and 16-bit uid).
//sys	Chown(path string, uid int, gid int) (errno int) = SYS_CHOWN32
//sys	Fchown(fd int, uid int, gid int) (errno int) = SYS_FCHOWN32
//sys	Fstat(fd int, stat *Stat_t) (errno int) = SYS_FSTAT64
//sys	Fstatfs(fd int, buf *Statfs_t) (errno int) = SYS_FSTATFS64
//sys	Getegid() (egid int) = SYS_GETEGID32
//sys	Geteuid() (euid int) = SYS_GETEUID32
//sys	Getgid() (gid int) = SYS_GETGID32
//sys	Getuid() (uid int) = SYS_GETUID32
//sys	Lchown(path string, uid int, gid int) (errno int) = SYS_LCHOWN32
//sys	Lstat(path string, stat *Stat_t) (errno int) = SYS_LSTAT64
//sys	Setfsgid(gid int) (errno int) = SYS_SETFSGID32
//sys	Setfsuid(uid int) (errno int) = SYS_SETFSUID32
//sys	Setgid(gid int) (errno int) = SYS_SETGID32
//sys	Setregid(rgid int, egid int) (errno int) = SYS_SETREGID32
//sys	Setresgid(rgid int, egid int, sgid int) (errno int) = SYS_SETRESGID32
//sys	Setresuid(ruid int, euid int, suid int) (errno int) = SYS_SETRESUID32
//sys	Setreuid(ruid int, euid int) (errno int) = SYS_SETREUID32
//sys	Stat(path string, stat *Stat_t) (errno int) = SYS_STAT64
//sys	Statfs(path string, buf *Statfs_t) (errno int) = SYS_STATFS64
//sys	getgroups(n int, list *_Gid_t) (nn int, errno int) = SYS_GETGROUPS32
//sys	setgroups(n int, list *_Gid_t) (errno int) = SYS_SETGROUPS32

// Underlying system call writes to newoffset via pointer.
// Implemented in assembly to avoid allocation.
func Seek(fd int, offset int64, whence int) (newoffset int64, errno int)

// On x86 Linux, all the socket calls go through an extra indirection,
// I think because the 5-register system call interface can't handle
// the 6-argument calls like sendto and recvfrom.  Instead the
// arguments to the underlying system call are the number below
// and a pointer to an array of uintptr.  We hide the pointer in the
// socketcall assembly to avoid allocation on every system call.

const (
	// see linux/net.h
	_SOCKET = 1;
	_BIND = 2;
	_CONNECT = 3;
	_LISTEN = 4;
	_ACCEPT = 5;
	_GETSOCKNAME = 6;
	_GETPEERNAME = 7;
	_SOCKETPAIR = 8;
	_SEND = 9;
	_RECV = 10;
	_SENDTO = 11;
	_RECVFROM = 12;
	_SHUTDOWN = 13;
	_SETSOCKOPT = 14;
	_GETSOCKOPT = 15;
	_SENDMSG = 16;
	_RECVMSG = 17;
)

func socketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, errno int)

func accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, errno int) {
	fd, errno = socketcall(_SOCKET, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0);
	return;
}

func bind(s int, addr uintptr, addrlen _Socklen) (errno int) {
	var _ int;
	_, errno = socketcall(_BIND, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0);
	return;
}

func connect(s int, addr uintptr, addrlen _Socklen) (errno int) {
	var _ int;
	_, errno = socketcall(_CONNECT, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0);
	return;
}

func socket(domain int, typ int, proto int) (fd int, errno int) {
	fd, errno = socketcall(_SOCKET, uintptr(domain), uintptr(typ), uintptr(proto), 0, 0, 0);
	return;
}

func setsockopt(s int, level int, name int, val uintptr, vallen int) (errno int) {
	var _ int;
	_, errno = socketcall(_SETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(vallen), 0);
	return;
}

func Listen(s int, n int) (errno int) {
	var _ int;
	_, errno = socketcall(_LISTEN, uintptr(s), uintptr(n), 0, 0, 0, 0);
	return;
}