summaryrefslogtreecommitdiff
path: root/usr/rsc/nacl/srpc/server.go
blob: 4fd778d635930a4510b6a35ee06ac0fb8d5ffb15 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// 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.

// SRPC server

package srpc

import (
	"bytes";
	"log";
	"os";
	"syscall";
)

// TODO(rsc): I'd prefer to make this
//	type Handler func(m *msg) Errno
// but NaCl can't use closures.
// The explicit interface is a way to attach state.

// A Handler is a handler for an SRPC method.
// It reads arguments from m.Arg, checks m.Size for array limits,
// writes return values to m.Ret, and returns an Errno status code.
type Handler interface {
	Run(m *msg) Errno
}

type method struct {
	name string;
	fmt string;
	handler Handler;
}

var rpcMethod []method

// BUG(rsc): Add's format string should be replaced by analyzing the
// type of an arbitrary func passed in an interface{} using reflection.

// Add registers a handler for the named method.
// Fmt is a Native Client format string, a sequence of
// alphabetic characters representing the types of the parameter values,
// a colon, and then a sequence of alphabetic characters
// representing the types of the returned values.
// The format characters and corresponding dynamic types are:
//
//	b	bool
//	C	[]byte
//	d	float64
//	D	[]float64
//	h	int	// a file descriptor (aka handle)
//	i	int32
//	I	[]int32
//	s	string
//
func Add(name, fmt string, handler Handler) {
	n := len(rpcMethod);
	if n >= cap(rpcMethod) {
		a := make([]method, n, (n+4)*2);
		for i := range a {
			a[i] = rpcMethod[i];
		}
		rpcMethod = a;
	}
	rpcMethod = rpcMethod[0:n+1];
	rpcMethod[n] = method{name, fmt, handler};
}

// Serve accepts new SRPC connections from the file descriptor fd
// and answers RPCs issued on those connections.
// It closes fd and returns an error if the imc_accept system call fails.
func Serve(fd int) os.Error {
	defer syscall.Close(fd);

	for {
		cfd, _, e := syscall.Syscall(syscall.SYS_IMC_ACCEPT, uintptr(fd), 0, 0);
		if e != 0 {
			return os.NewSyscallError("imc_accept", int(e));
		}
		go serveLoop(int(cfd));
	}
	panic("unreachable");
}

func serveLoop(fd int) {
	c := make(chan *msg);
	go sendLoop(fd, c);

	var r msgReceiver;
	r.fd = fd;
	for {
		m, err := r.recv();
		if err != nil {
			break;
		}
		m.unpackRequest();
		if !m.gotHeader {
			log.Stderrf("cannot unpack header: %s", m.status);
			continue;
		}
		// log.Stdoutf("<- %#v", m);
		m.isReq = false;	// set up for response
		go serveMsg(m, c);
	}
	close(c);
}

func sendLoop(fd int, c <-chan *msg) {
	var s msgSender;
	s.fd = fd;
	for m := range c {
		// log.Stdoutf("-> %#v", m);
		m.packResponse();
		s.send(m);
	}
	syscall.Close(fd);
}

func serveMsg(m *msg, c chan<- *msg) {
	if m.status != OK {
		c <- m;
		return;
	}
	if m.rpcNumber >= uint32(len(rpcMethod)) {
		m.status = ErrBadRPCNumber;
		c <- m;
		return;
	}

	meth := &rpcMethod[m.rpcNumber];
	if meth.fmt != m.fmt {
		switch {
		case len(m.fmt) < len(meth.fmt):
			m.status = ErrTooFewArgs;
		case len(m.fmt) > len(meth.fmt):
			m.status = ErrTooManyArgs;
		default:
			// There's a type mismatch.
			// It's an in-arg mismatch if the mismatch happens
			// before the colon; otherwise it's an out-arg mismatch.
			m.status = ErrInArgTypeMismatch;
			for i := 0; i < len(m.fmt) && m.fmt[i] == meth.fmt[i]; i++ {
				if m.fmt[i] == ':' {
					m.status = ErrOutArgTypeMismatch;
					break;
				}
			}
		}
		c <- m;
		return;
	}

	m.status = meth.handler.Run(m);
	c <- m;
}

// ServeRuntime serves RPCs issued by the Native Client embedded runtime.
// This should be called by main once all methods have been registered using Add.
func ServeRuntime() os.Error {
	// Call getFd to check that we are running embedded.
	if _, err := getFd(); err != nil {
		return err;
	}

	// We are running embedded.
	// The fd returned by getFd is a red herring.
	// Accept connections on magic fd 3.
	return Serve(3);
}

// getFd runs the srpc_get_fd system call.
func getFd() (fd int, err os.Error) {
	r1, _, e := syscall.Syscall(syscall.SYS_SRPC_GET_FD, 0, 0, 0);
	return int(r1), os.NewSyscallError("srpc_get_fd", int(e));
}

// Enabled returns true if SRPC is enabled in the Native Client runtime.
func Enabled() bool {
	_, err:= getFd();
	return err == nil;
}

// Service #0, service_discovery, returns a list of the other services
// and their argument formats.
type serviceDiscovery struct{}

func (serviceDiscovery) Run(m *msg) Errno {
	var b bytes.Buffer;
	for _, m := range rpcMethod {
		b.WriteString(m.name);
		b.WriteByte(':');
		b.WriteString(m.fmt);
		b.WriteByte('\n');
	}
	if b.Len() > m.Size[0] {
		return ErrNoMemory;
	}
	m.Ret[0] = b.Bytes();
	return OK;
}

func init() {
	Add("service_discovery", ":C", serviceDiscovery{});
}