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 #ifdef _RPC_THREAD_SAFE_
47 #define xports RPC_THREAD_VARIABLE(svc_xports_s)
49 static SVCXPRT
**xports
;
52 #define NULL_SVC ((struct svc_callout *)0)
53 #define RQCRED_SIZE 400 /* this size is excessive */
56 Each entry represents a set of procedures (an rpc program).
57 The dispatch routine takes request structs and runs the
58 appropriate procedure. */
60 struct svc_callout
*sc_next
;
63 void (*sc_dispatch
) (struct svc_req
*, SVCXPRT
*);
65 #ifdef _RPC_THREAD_SAFE_
66 #define svc_head RPC_THREAD_VARIABLE(svc_head_s)
68 static struct svc_callout
*svc_head
;
71 /* *************** SVCXPRT related stuff **************** */
73 /* Activate a transport handle. */
75 xprt_register (SVCXPRT
*xprt
)
77 register int sock
= xprt
->xp_sock
;
82 xports
= (SVCXPRT
**) malloc (_rpc_dtablesize () * sizeof (SVCXPRT
*));
83 if (xports
== NULL
) /* DonĀ“t add handle */
87 if (sock
< _rpc_dtablesize ())
89 struct pollfd
*new_svc_pollfd
;
92 if (sock
< FD_SETSIZE
)
93 FD_SET (sock
, &svc_fdset
);
95 /* Check if we have an empty slot */
96 for (i
= 0; i
< svc_max_pollfd
; ++i
)
97 if (svc_pollfd
[i
].fd
== -1)
99 svc_pollfd
[i
].fd
= sock
;
100 svc_pollfd
[i
].events
= (POLLIN
| POLLPRI
|
101 POLLRDNORM
| POLLRDBAND
);
105 new_svc_pollfd
= (struct pollfd
*) realloc (svc_pollfd
,
106 sizeof (struct pollfd
)
107 * (svc_max_pollfd
+ 1));
108 if (new_svc_pollfd
== NULL
) /* Out of memory */
110 svc_pollfd
= new_svc_pollfd
;
113 svc_pollfd
[svc_max_pollfd
- 1].fd
= sock
;
114 svc_pollfd
[svc_max_pollfd
- 1].events
= (POLLIN
| POLLPRI
|
115 POLLRDNORM
| POLLRDBAND
);
118 libc_hidden_def (xprt_register
)
120 /* De-activate a transport handle. */
122 xprt_unregister (SVCXPRT
*xprt
)
124 register int sock
= xprt
->xp_sock
;
127 if ((sock
< _rpc_dtablesize ()) && (xports
[sock
] == xprt
))
129 xports
[sock
] = (SVCXPRT
*) 0;
131 if (sock
< FD_SETSIZE
)
132 FD_CLR (sock
, &svc_fdset
);
134 for (i
= 0; i
< svc_max_pollfd
; ++i
)
135 if (svc_pollfd
[i
].fd
== sock
)
136 svc_pollfd
[i
].fd
= -1;
139 libc_hidden_def (xprt_unregister
)
142 /* ********************** CALLOUT list related stuff ************* */
144 /* Search the callout list for a program number, return the callout
146 static struct svc_callout
*
147 svc_find (rpcprog_t prog
, rpcvers_t vers
, struct svc_callout
**prev
)
149 register struct svc_callout
*s
, *p
;
152 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
)
154 if ((s
->sc_prog
== prog
) && (s
->sc_vers
== vers
))
163 /* Add a service program to the callout list.
164 The dispatch routine will be called when a rpc request for this
165 program number comes in. */
167 svc_register (SVCXPRT
* xprt
, rpcprog_t prog
, rpcvers_t vers
,
168 void (*dispatch
) (struct svc_req
*, SVCXPRT
*),
171 struct svc_callout
*prev
;
172 register struct svc_callout
*s
;
174 if ((s
= svc_find (prog
, vers
, &prev
)) != NULL_SVC
)
176 if (s
->sc_dispatch
== dispatch
)
177 goto pmap_it
; /* he is registering another xptr */
180 s
= (struct svc_callout
*) mem_alloc (sizeof (struct svc_callout
));
181 if (s
== (struct svc_callout
*) 0)
186 s
->sc_dispatch
= dispatch
;
187 s
->sc_next
= svc_head
;
191 /* now register the information with the local binder service */
193 return pmap_set (prog
, vers
, protocol
, xprt
->xp_port
);
197 libc_hidden_def (svc_register
)
199 /* Remove a service program from the callout list. */
201 svc_unregister (rpcprog_t prog
, rpcvers_t vers
)
203 struct svc_callout
*prev
;
204 register struct svc_callout
*s
;
206 if ((s
= svc_find (prog
, vers
, &prev
)) == NULL_SVC
)
209 if (prev
== NULL_SVC
)
210 svc_head
= s
->sc_next
;
212 prev
->sc_next
= s
->sc_next
;
214 s
->sc_next
= NULL_SVC
;
215 mem_free ((char *) s
, (u_int
) sizeof (struct svc_callout
));
216 /* now unregister the information with the local binder service */
217 pmap_unset (prog
, vers
);
219 libc_hidden_def (svc_unregister
)
221 /* ******************* REPLY GENERATION ROUTINES ************ */
223 /* Send a reply to an rpc request */
225 svc_sendreply (register SVCXPRT
*xprt
, xdrproc_t xdr_results
,
226 caddr_t xdr_location
)
230 rply
.rm_direction
= REPLY
;
231 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
232 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
233 rply
.acpted_rply
.ar_stat
= SUCCESS
;
234 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
235 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
236 return SVC_REPLY (xprt
, &rply
);
238 INTDEF (svc_sendreply
)
240 /* No procedure error reply */
242 svcerr_noproc (register SVCXPRT
*xprt
)
246 rply
.rm_direction
= REPLY
;
247 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
248 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
249 rply
.acpted_rply
.ar_stat
= PROC_UNAVAIL
;
250 SVC_REPLY (xprt
, &rply
);
253 /* Can't decode args error reply */
255 svcerr_decode (register SVCXPRT
*xprt
)
259 rply
.rm_direction
= REPLY
;
260 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
261 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
262 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
263 SVC_REPLY (xprt
, &rply
);
265 INTDEF (svcerr_decode
)
267 /* Some system error */
269 svcerr_systemerr (register SVCXPRT
*xprt
)
273 rply
.rm_direction
= REPLY
;
274 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
275 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
276 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
277 SVC_REPLY (xprt
, &rply
);
280 /* Authentication error reply */
282 svcerr_auth (SVCXPRT
*xprt
, enum auth_stat why
)
286 rply
.rm_direction
= REPLY
;
287 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
288 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
289 rply
.rjcted_rply
.rj_why
= why
;
290 SVC_REPLY (xprt
, &rply
);
292 libc_hidden_def (svcerr_auth
)
294 /* Auth too weak error reply */
296 svcerr_weakauth (SVCXPRT
*xprt
)
298 svcerr_auth (xprt
, AUTH_TOOWEAK
);
301 /* Program unavailable error reply */
303 svcerr_noprog (register SVCXPRT
*xprt
)
307 rply
.rm_direction
= REPLY
;
308 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
309 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
310 rply
.acpted_rply
.ar_stat
= PROG_UNAVAIL
;
311 SVC_REPLY (xprt
, &rply
);
313 libc_hidden_def (svcerr_noprog
)
315 /* Program version mismatch error reply */
317 svcerr_progvers (register SVCXPRT
*xprt
, rpcvers_t low_vers
,
322 rply
.rm_direction
= REPLY
;
323 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
324 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
325 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
326 rply
.acpted_rply
.ar_vers
.low
= low_vers
;
327 rply
.acpted_rply
.ar_vers
.high
= high_vers
;
328 SVC_REPLY (xprt
, &rply
);
330 libc_hidden_def (svcerr_progvers
)
332 /* ******************* SERVER INPUT STUFF ******************* */
335 * Get server side input from some transport.
337 * Statement of authentication parameters management:
338 * This function owns and manages all authentication parameters, specifically
339 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
340 * the "cooked" credentials (rqst->rq_clntcred).
341 * However, this function does not know the structure of the cooked
342 * credentials, so it make the following assumptions:
343 * a) the structure is contiguous (no pointers), and
344 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
345 * In all events, all three parameters are freed upon exit from this routine.
346 * The storage is trivially management on the call stack in user land, but
347 * is mallocated in kernel land.
351 svc_getreq (int rdfds
)
356 readfds
.fds_bits
[0] = rdfds
;
357 INTUSE(svc_getreqset
) (&readfds
);
362 svc_getreqset (fd_set
*readfds
)
364 register fd_mask mask
;
365 register fd_mask
*maskp
;
366 register int setsize
;
370 setsize
= _rpc_dtablesize ();
371 if (setsize
> FD_SETSIZE
)
372 setsize
= FD_SETSIZE
;
373 maskp
= readfds
->fds_bits
;
374 for (sock
= 0; sock
< setsize
; sock
+= NFDBITS
)
375 for (mask
= *maskp
++; (bit
= ffsl (mask
)); mask
^= (1 << (bit
- 1)))
376 INTUSE(svc_getreq_common
) (sock
+ bit
- 1);
378 INTDEF (svc_getreqset
)
381 svc_getreq_poll (struct pollfd
*pfdp
, int pollretval
)
384 register int fds_found
;
386 for (i
= fds_found
= 0; i
< svc_max_pollfd
&& fds_found
< pollretval
; ++i
)
388 register struct pollfd
*p
= &pfdp
[i
];
390 if (p
->fd
!= -1 && p
->revents
)
392 /* fd has input waiting */
395 if (p
->revents
& POLLNVAL
)
396 xprt_unregister (xports
[p
->fd
]);
398 INTUSE(svc_getreq_common
) (p
->fd
);
402 INTDEF (svc_getreq_poll
)
406 svc_getreq_common (const int fd
)
410 register SVCXPRT
*xprt
;
411 char cred_area
[2 * MAX_AUTH_BYTES
+ RQCRED_SIZE
];
412 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
413 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
416 /* Do we control fd? */
420 /* now receive msgs from xprtprt (support batch calls) */
423 if (SVC_RECV (xprt
, &msg
))
425 /* now find the exported program and call it */
426 struct svc_callout
*s
;
433 r
.rq_clntcred
= &(cred_area
[2 * MAX_AUTH_BYTES
]);
435 r
.rq_prog
= msg
.rm_call
.cb_prog
;
436 r
.rq_vers
= msg
.rm_call
.cb_vers
;
437 r
.rq_proc
= msg
.rm_call
.cb_proc
;
438 r
.rq_cred
= msg
.rm_call
.cb_cred
;
440 /* first authenticate the message */
441 /* Check for null flavor and bypass these calls if possible */
443 if (msg
.rm_call
.cb_cred
.oa_flavor
== AUTH_NULL
)
445 r
.rq_xprt
->xp_verf
.oa_flavor
= _null_auth
.oa_flavor
;
446 r
.rq_xprt
->xp_verf
.oa_length
= 0;
448 else if ((why
= INTUSE(_authenticate
) (&r
, &msg
)) != AUTH_OK
)
450 svcerr_auth (xprt
, why
);
454 /* now match message with a registered service */
459 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
)
461 if (s
->sc_prog
== r
.rq_prog
)
463 if (s
->sc_vers
== r
.rq_vers
)
465 (*s
->sc_dispatch
) (&r
, xprt
);
468 /* found correct version */
470 if (s
->sc_vers
< low_vers
)
471 low_vers
= s
->sc_vers
;
472 if (s
->sc_vers
> high_vers
)
473 high_vers
= s
->sc_vers
;
475 /* found correct program */
477 /* if we got here, the program or version
480 svcerr_progvers (xprt
, low_vers
, high_vers
);
482 svcerr_noprog (xprt
);
483 /* Fall through to ... */
486 if ((stat
= SVC_STAT (xprt
)) == XPRT_DIED
)
492 while (stat
== XPRT_MOREREQS
);
494 INTDEF (svc_getreq_common
)
496 #ifdef _RPC_THREAD_SAFE_
499 __rpc_thread_svc_cleanup (void)
501 struct svc_callout
*svcp
;
503 while ((svcp
= svc_head
) != NULL
)
504 svc_unregister (svcp
->sc_prog
, svcp
->sc_vers
);
507 #endif /* _RPC_THREAD_SAFE_ */