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 * svc.c, Server-side remote procedure call interface.
32 * There are two sets of procedures here. The xprt routines are
33 * for handling transport handles. The svc routines handle the
34 * list of service routines.
36 * Copyright (C) 1984, Sun Microsystems, Inc.
43 #include <rpc/pmap_clnt.h>
46 static SVCXPRT
**xports
;
48 #define NULL_SVC ((struct svc_callout *)0)
49 #define RQCRED_SIZE 400 /* this size is excessive */
52 Each entry represents a set of procedures (an rpc program).
53 The dispatch routine takes request structs and runs the
54 appropriate procedure. */
55 static struct svc_callout
{
56 struct svc_callout
*sc_next
;
59 void (*sc_dispatch
) (struct svc_req
*, SVCXPRT
*);
62 /* *************** SVCXPRT related stuff **************** */
64 /* Activate a transport handle. */
66 xprt_register (SVCXPRT
*xprt
)
68 register int sock
= xprt
->xp_sock
;
73 xports
= (SVCXPRT
**) malloc (_rpc_dtablesize () * sizeof (SVCXPRT
*));
74 if (xports
== NULL
) /* DonĀ“t add handle */
78 if (sock
< _rpc_dtablesize ())
81 if (sock
< FD_SETSIZE
)
82 FD_SET (sock
, &svc_fdset
);
84 /* Check if we have an empty slot */
85 for (i
= 0; i
< svc_max_pollfd
; ++i
)
86 if (svc_pollfd
[i
].fd
== -1)
88 svc_pollfd
[i
].fd
= sock
;
89 svc_pollfd
[i
].events
= (POLLIN
| POLLPRI
|
90 POLLRDNORM
| POLLRDBAND
);
95 svc_pollfd
= realloc (svc_pollfd
,
96 sizeof (struct pollfd
) * svc_max_pollfd
);
97 if (svc_pollfd
== NULL
) /* Out of memory */
100 svc_pollfd
[svc_max_pollfd
- 1].fd
= sock
;
101 svc_pollfd
[svc_max_pollfd
- 1].events
= (POLLIN
| POLLPRI
|
102 POLLRDNORM
| POLLRDBAND
);
106 /* De-activate a transport handle. */
108 xprt_unregister (SVCXPRT
*xprt
)
110 register int sock
= xprt
->xp_sock
;
113 if ((sock
< _rpc_dtablesize ()) && (xports
[sock
] == xprt
))
115 xports
[sock
] = (SVCXPRT
*) 0;
117 if (sock
< FD_SETSIZE
)
118 FD_CLR (sock
, &svc_fdset
);
120 for (i
= 0; i
< svc_max_pollfd
; ++i
)
121 if (svc_pollfd
[i
].fd
== sock
)
122 svc_pollfd
[i
].fd
= -1;
127 /* ********************** CALLOUT list related stuff ************* */
129 /* Search the callout list for a program number, return the callout
131 static struct svc_callout
*
132 svc_find (rpcprog_t prog
, rpcvers_t vers
, struct svc_callout
**prev
)
134 register struct svc_callout
*s
, *p
;
137 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
)
139 if ((s
->sc_prog
== prog
) && (s
->sc_vers
== vers
))
148 /* Add a service program to the callout list.
149 The dispatch routine will be called when a rpc request for this
150 program number comes in. */
152 svc_register (SVCXPRT
* xprt
, rpcprog_t prog
, rpcvers_t vers
,
153 void (*dispatch
) (struct svc_req
*, SVCXPRT
*),
156 struct svc_callout
*prev
;
157 register struct svc_callout
*s
;
159 if ((s
= svc_find (prog
, vers
, &prev
)) != NULL_SVC
)
161 if (s
->sc_dispatch
== dispatch
)
162 goto pmap_it
; /* he is registering another xptr */
165 s
= (struct svc_callout
*) mem_alloc (sizeof (struct svc_callout
));
166 if (s
== (struct svc_callout
*) 0)
171 s
->sc_dispatch
= dispatch
;
172 s
->sc_next
= svc_head
;
176 /* now register the information with the local binder service */
178 return pmap_set (prog
, vers
, protocol
, xprt
->xp_port
);
183 /* Remove a service program from the callout list. */
185 svc_unregister (rpcprog_t prog
, rpcvers_t vers
)
187 struct svc_callout
*prev
;
188 register struct svc_callout
*s
;
190 if ((s
= svc_find (prog
, vers
, &prev
)) == NULL_SVC
)
193 if (prev
== NULL_SVC
)
194 svc_head
= s
->sc_next
;
196 prev
->sc_next
= s
->sc_next
;
198 s
->sc_next
= NULL_SVC
;
199 mem_free ((char *) s
, (u_int
) sizeof (struct svc_callout
));
200 /* now unregister the information with the local binder service */
201 pmap_unset (prog
, vers
);
204 /* ******************* REPLY GENERATION ROUTINES ************ */
206 /* Send a reply to an rpc request */
208 svc_sendreply (register SVCXPRT
*xprt
, xdrproc_t xdr_results
,
209 caddr_t xdr_location
)
213 rply
.rm_direction
= REPLY
;
214 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
215 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
216 rply
.acpted_rply
.ar_stat
= SUCCESS
;
217 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
218 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
219 return SVC_REPLY (xprt
, &rply
);
222 /* No procedure error reply */
224 svcerr_noproc (register SVCXPRT
*xprt
)
228 rply
.rm_direction
= REPLY
;
229 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
230 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
231 rply
.acpted_rply
.ar_stat
= PROC_UNAVAIL
;
232 SVC_REPLY (xprt
, &rply
);
235 /* Can't decode args error reply */
237 svcerr_decode (register SVCXPRT
*xprt
)
241 rply
.rm_direction
= REPLY
;
242 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
243 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
244 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
245 SVC_REPLY (xprt
, &rply
);
248 /* Some system error */
250 svcerr_systemerr (register SVCXPRT
*xprt
)
254 rply
.rm_direction
= REPLY
;
255 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
256 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
257 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
258 SVC_REPLY (xprt
, &rply
);
261 /* Authentication error reply */
263 svcerr_auth (SVCXPRT
*xprt
, enum auth_stat why
)
267 rply
.rm_direction
= REPLY
;
268 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
269 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
270 rply
.rjcted_rply
.rj_why
= why
;
271 SVC_REPLY (xprt
, &rply
);
274 /* Auth too weak error reply */
276 svcerr_weakauth (SVCXPRT
*xprt
)
278 svcerr_auth (xprt
, AUTH_TOOWEAK
);
281 /* Program unavailable error reply */
283 svcerr_noprog (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
= PROG_UNAVAIL
;
291 SVC_REPLY (xprt
, &rply
);
294 /* Program version mismatch error reply */
296 svcerr_progvers (register SVCXPRT
*xprt
, rpcvers_t low_vers
,
301 rply
.rm_direction
= REPLY
;
302 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
303 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
304 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
305 rply
.acpted_rply
.ar_vers
.low
= low_vers
;
306 rply
.acpted_rply
.ar_vers
.high
= high_vers
;
307 SVC_REPLY (xprt
, &rply
);
310 /* ******************* SERVER INPUT STUFF ******************* */
313 * Get server side input from some transport.
315 * Statement of authentication parameters management:
316 * This function owns and manages all authentication parameters, specifically
317 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
318 * the "cooked" credentials (rqst->rq_clntcred).
319 * However, this function does not know the structure of the cooked
320 * credentials, so it make the following assumptions:
321 * a) the structure is contiguous (no pointers), and
322 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
323 * In all events, all three parameters are freed upon exit from this routine.
324 * The storage is trivially management on the call stack in user land, but
325 * is mallocated in kernel land.
329 svc_getreq (int rdfds
)
334 readfds
.fds_bits
[0] = rdfds
;
335 svc_getreqset (&readfds
);
339 svc_getreqset (fd_set
*readfds
)
341 register u_int32_t mask
;
342 register u_int32_t
*maskp
;
343 register int setsize
;
347 setsize
= _rpc_dtablesize ();
348 maskp
= (u_int32_t
*) readfds
->fds_bits
;
349 for (sock
= 0; sock
< setsize
; sock
+= 32)
350 for (mask
= *maskp
++; (bit
= ffs (mask
)); mask
^= (1 << (bit
- 1)))
351 svc_getreq_common (sock
+ bit
- 1);
355 svc_getreq_poll (struct pollfd
*pfdp
, int pollretval
)
358 register int fds_found
;
360 for (i
= fds_found
= 0; i
< svc_max_pollfd
&& fds_found
< pollretval
; ++i
)
362 register struct pollfd
*p
= &pfdp
[i
];
364 if (p
->fd
!= -1 && p
->revents
)
366 /* fd has input waiting */
369 if (p
->revents
& POLLNVAL
)
370 xprt_unregister (xports
[p
->fd
]);
372 svc_getreq_common (p
->fd
);
379 svc_getreq_common (const int fd
)
383 register SVCXPRT
*xprt
;
384 char cred_area
[2 * MAX_AUTH_BYTES
+ RQCRED_SIZE
];
385 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
386 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
389 /* Do we control fd? */
393 /* now receive msgs from xprtprt (support batch calls) */
396 if (SVC_RECV (xprt
, &msg
))
398 /* now find the exported program and call it */
399 struct svc_callout
*s
;
406 r
.rq_clntcred
= &(cred_area
[2 * MAX_AUTH_BYTES
]);
408 r
.rq_prog
= msg
.rm_call
.cb_prog
;
409 r
.rq_vers
= msg
.rm_call
.cb_vers
;
410 r
.rq_proc
= msg
.rm_call
.cb_proc
;
411 r
.rq_cred
= msg
.rm_call
.cb_cred
;
413 /* first authenticate the message */
414 /* Check for null flavor and bypass these calls if possible */
416 if (msg
.rm_call
.cb_cred
.oa_flavor
== AUTH_NULL
)
418 r
.rq_xprt
->xp_verf
.oa_flavor
= _null_auth
.oa_flavor
;
419 r
.rq_xprt
->xp_verf
.oa_length
= 0;
421 else if ((why
= _authenticate (&r
, &msg
)) != AUTH_OK
)
423 svcerr_auth (xprt
, why
);
427 /* now match message with a registered service */
432 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
)
434 if (s
->sc_prog
== r
.rq_prog
)
436 if (s
->sc_vers
== r
.rq_vers
)
438 (*s
->sc_dispatch
) (&r
, xprt
);
441 /* found correct version */
443 if (s
->sc_vers
< low_vers
)
444 low_vers
= s
->sc_vers
;
445 if (s
->sc_vers
> high_vers
)
446 high_vers
= s
->sc_vers
;
448 /* found correct program */
450 /* if we got here, the program or version
453 svcerr_progvers (xprt
, low_vers
, high_vers
);
455 svcerr_noprog (xprt
);
456 /* Fall through to ... */
459 if ((stat
= SVC_STAT (xprt
)) == XPRT_DIED
)
465 while (stat
== XPRT_MOREREQS
);