2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 #if defined(LIBC_SCCS) && !defined(lint)
31 /*static char *sccsid = "from: @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";*/
32 /*static char *sccsid = "from: @(#)svc.c 2.4 88/08/11 4.0 RPCSRC";*/
33 static char *rcsid = "$FreeBSD: src/lib/libc/rpc/svc.c,v 1.14.2.1 2001/03/05 10:50:36 obrien Exp $";
37 * svc.c, Server-side remote procedure call interface.
39 * There are two sets of procedures here. The xprt routines are
40 * for handling transport handles. The svc routines handle the
41 * list of service routines.
43 * Copyright (C) 1984, Sun Microsystems, Inc.
49 #include <rpc/pmap_clnt.h>
51 static SVCXPRT **xports;
52 static int xportssize;
54 #define NULL_SVC ((struct svc_callout *)0)
55 #define RQCRED_SIZE 400 /* this size is excessive */
57 #define max(a, b) (a > b ? a : b)
61 * Each entry represents a set of procedures (an rpc program).
62 * The dispatch routine takes request structs and runs the
63 * apropriate procedure.
65 static struct svc_callout {
66 struct svc_callout *sc_next;
69 void (*sc_dispatch)();
72 static struct svc_callout *svc_find();
74 int __svc_fdsetsize = 0;
75 fd_set *__svc_fdset = NULL;
77 /* *************** SVCXPRT related stuff **************** */
80 * Activate a transport handle.
86 register int sock = xprt->xp_sock;
88 if (sock + 1 > __svc_fdsetsize) {
89 int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
92 fds = (fd_set *)malloc(bytes);
93 memset(fds, 0, bytes);
95 memcpy(fds, __svc_fdset, howmany(__svc_fdsetsize,
96 NFDBITS) * sizeof(fd_mask));
100 __svc_fdsetsize = howmany(sock+1, NFDBITS) * NFDBITS;
103 if (sock < FD_SETSIZE)
104 FD_SET(sock, &svc_fdset);
105 FD_SET(sock, __svc_fdset);
107 if (xports == NULL || sock + 1 > xportssize) {
109 int size = FD_SETSIZE;
113 xp = (SVCXPRT **)mem_alloc(size * sizeof(SVCXPRT *));
114 memset(xp, 0, size * sizeof(SVCXPRT *));
116 memcpy(xp, xports, xportssize * sizeof(SVCXPRT *));
123 svc_maxfd = max(svc_maxfd, sock);
127 * De-activate a transport handle.
130 xprt_unregister(xprt)
133 register int sock = xprt->xp_sock;
135 if (xports[sock] == xprt) {
136 xports[sock] = (SVCXPRT *)0;
137 if (sock < FD_SETSIZE)
138 FD_CLR(sock, &svc_fdset);
139 FD_CLR(sock, __svc_fdset);
140 if (sock == svc_maxfd) {
141 for (svc_maxfd--; svc_maxfd >= 0; svc_maxfd--)
142 if (xports[svc_maxfd])
146 * XXX could use svc_maxfd as a hint to
147 * decrease the size of __svc_fdset
153 /* ********************** CALLOUT list related stuff ************* */
156 * Add a service program to the callout list.
157 * The dispatch routine will be called when a rpc request for this
158 * program number comes in.
161 svc_register(xprt, prog, vers, dispatch, protocol)
168 struct svc_callout *prev;
169 register struct svc_callout *s;
171 if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
172 if (s->sc_dispatch == dispatch)
173 goto pmap_it; /* he is registering another xptr */
176 s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
177 if (s == (struct svc_callout *)0) {
182 s->sc_dispatch = dispatch;
183 s->sc_next = svc_head;
186 /* now register the information with the local binder service */
188 return (pmap_set(prog, vers, protocol, xprt->xp_port));
194 * Remove a service program from the callout list.
197 svc_unregister(prog, vers)
201 struct svc_callout *prev;
202 register struct svc_callout *s;
204 if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
206 if (prev == NULL_SVC) {
207 svc_head = s->sc_next;
209 prev->sc_next = s->sc_next;
211 s->sc_next = NULL_SVC;
212 mem_free((char *) s, (u_int) sizeof(struct svc_callout));
213 /* now unregister the information with the local binder service */
214 (void)pmap_unset(prog, vers);
218 * Search the callout list for a program number, return the callout
221 static struct svc_callout *
222 svc_find(prog, vers, prev)
225 struct svc_callout **prev;
227 register struct svc_callout *s, *p;
230 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
231 if ((s->sc_prog == prog) && (s->sc_vers == vers))
240 /* ******************* REPLY GENERATION ROUTINES ************ */
243 * Send a reply to an rpc request
246 svc_sendreply(xprt, xdr_results, xdr_location)
247 register SVCXPRT *xprt;
248 xdrproc_t xdr_results;
249 caddr_t xdr_location;
253 rply.rm_direction = REPLY;
254 rply.rm_reply.rp_stat = MSG_ACCEPTED;
255 rply.acpted_rply.ar_verf = xprt->xp_verf;
256 rply.acpted_rply.ar_stat = SUCCESS;
257 rply.acpted_rply.ar_results.where = xdr_location;
258 rply.acpted_rply.ar_results.proc = xdr_results;
259 return (SVC_REPLY(xprt, &rply));
263 * No procedure error reply
267 register SVCXPRT *xprt;
271 rply.rm_direction = REPLY;
272 rply.rm_reply.rp_stat = MSG_ACCEPTED;
273 rply.acpted_rply.ar_verf = xprt->xp_verf;
274 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
275 SVC_REPLY(xprt, &rply);
279 * Can't decode args error reply
283 register SVCXPRT *xprt;
287 rply.rm_direction = REPLY;
288 rply.rm_reply.rp_stat = MSG_ACCEPTED;
289 rply.acpted_rply.ar_verf = xprt->xp_verf;
290 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
291 SVC_REPLY(xprt, &rply);
298 svcerr_systemerr(xprt)
299 register SVCXPRT *xprt;
303 rply.rm_direction = REPLY;
304 rply.rm_reply.rp_stat = MSG_ACCEPTED;
305 rply.acpted_rply.ar_verf = xprt->xp_verf;
306 rply.acpted_rply.ar_stat = SYSTEM_ERR;
307 SVC_REPLY(xprt, &rply);
311 * Authentication error reply
314 svcerr_auth(xprt, why)
320 rply.rm_direction = REPLY;
321 rply.rm_reply.rp_stat = MSG_DENIED;
322 rply.rjcted_rply.rj_stat = AUTH_ERROR;
323 rply.rjcted_rply.rj_why = why;
324 SVC_REPLY(xprt, &rply);
328 * Auth too weak error reply
331 svcerr_weakauth(xprt)
335 svcerr_auth(xprt, AUTH_TOOWEAK);
339 * Program unavailable error reply
343 register SVCXPRT *xprt;
347 rply.rm_direction = REPLY;
348 rply.rm_reply.rp_stat = MSG_ACCEPTED;
349 rply.acpted_rply.ar_verf = xprt->xp_verf;
350 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
351 SVC_REPLY(xprt, &rply);
355 * Program version mismatch error reply
358 svcerr_progvers(xprt, low_vers, high_vers)
359 register SVCXPRT *xprt;
365 rply.rm_direction = REPLY;
366 rply.rm_reply.rp_stat = MSG_ACCEPTED;
367 rply.acpted_rply.ar_verf = xprt->xp_verf;
368 rply.acpted_rply.ar_stat = PROG_MISMATCH;
369 rply.acpted_rply.ar_vers.low = low_vers;
370 rply.acpted_rply.ar_vers.high = high_vers;
371 SVC_REPLY(xprt, &rply);
374 /* ******************* SERVER INPUT STUFF ******************* */
377 * Get server side input from some transport.
379 * Statement of authentication parameters management:
380 * This function owns and manages all authentication parameters, specifically
381 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
382 * the "cooked" credentials (rqst->rq_clntcred).
383 * However, this function does not know the structure of the cooked
384 * credentials, so it make the following assumptions:
385 * a) the structure is contiguous (no pointers), and
386 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
387 * In all events, all three parameters are freed upon exit from this routine.
388 * The storage is trivially management on the call stack in user land, but
389 * is mallocated in kernel land.
399 readfds.fds_bits[0] = rdfds;
400 svc_getreqset(&readfds);
404 svc_getreqset(readfds)
407 svc_getreqset2(readfds, FD_SETSIZE);
411 svc_getreqset2(readfds, width)
421 register SVCXPRT *xprt;
424 register fd_mask mask, *maskp;
425 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
426 msg.rm_call.cb_cred.oa_base = cred_area;
427 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
428 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
431 maskp = readfds->fds_bits;
432 for (sock = 0; sock < width; sock += NFDBITS) {
433 for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
434 /* sock has input waiting */
435 xprt = xports[sock + bit - 1];
437 /* But do we control sock? */
439 /* now receive msgs from xprtprt (support batch calls) */
441 if (SVC_RECV(xprt, &msg)) {
443 /* now find the exported program and call it */
444 register struct svc_callout *s;
448 r.rq_prog = msg.rm_call.cb_prog;
449 r.rq_vers = msg.rm_call.cb_vers;
450 r.rq_proc = msg.rm_call.cb_proc;
451 r.rq_cred = msg.rm_call.cb_cred;
452 /* first authenticate the message */
453 if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
454 svcerr_auth(xprt, why);
457 /* now match message with a registered service*/
459 low_vers = (u_long) - 1;
461 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
462 if (s->sc_prog == r.rq_prog) {
463 if (s->sc_vers == r.rq_vers) {
464 (*s->sc_dispatch)(&r, xprt);
466 } /* found correct version */
468 if (s->sc_vers < low_vers)
469 low_vers = s->sc_vers;
470 if (s->sc_vers > high_vers)
471 high_vers = s->sc_vers;
472 } /* found correct program */
475 * if we got here, the program or version
479 svcerr_progvers(xprt,
480 low_vers, high_vers);
483 /* Fall through to ... */
486 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
490 } while (stat == XPRT_MOREREQS);