2 * svc.c, Server-side remote procedure call interface.
4 * There are two sets of procedures here. The xprt routines are
5 * for handling transport handles. The svc routines handle the
6 * list of service routines.
8 * Copyright (C) 1984, Sun Microsystems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 * * Neither the name of Sun Microsystems, Inc. nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
31 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <rpc/pmap_clnt.h>
45 #ifdef _RPC_THREAD_SAFE_
46 #define xports RPC_THREAD_VARIABLE(svc_xports_s)
48 static SVCXPRT
**xports
;
51 #define NULL_SVC ((struct svc_callout *)0)
52 #define RQCRED_SIZE 400 /* this size is excessive */
55 Each entry represents a set of procedures (an rpc program).
56 The dispatch routine takes request structs and runs the
57 appropriate procedure. */
59 struct svc_callout
*sc_next
;
62 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
))
165 svc_is_mapped (rpcprog_t prog
, rpcvers_t vers
)
167 struct svc_callout
*prev
;
168 register struct svc_callout
*s
;
169 s
= svc_find (prog
, vers
, &prev
);
170 return s
!= NULL_SVC
&& s
->sc_mapped
;
174 /* Add a service program to the callout list.
175 The dispatch routine will be called when a rpc request for this
176 program number comes in. */
178 svc_register (SVCXPRT
* xprt
, rpcprog_t prog
, rpcvers_t vers
,
179 void (*dispatch
) (struct svc_req
*, SVCXPRT
*),
182 struct svc_callout
*prev
;
183 register struct svc_callout
*s
;
185 if ((s
= svc_find (prog
, vers
, &prev
)) != NULL_SVC
)
187 if (s
->sc_dispatch
== dispatch
)
188 goto pmap_it
; /* he is registering another xptr */
191 s
= (struct svc_callout
*) mem_alloc (sizeof (struct svc_callout
));
192 if (s
== (struct svc_callout
*) 0)
197 s
->sc_dispatch
= dispatch
;
198 s
->sc_next
= svc_head
;
199 s
->sc_mapped
= FALSE
;
203 /* now register the information with the local binder service */
206 if (! pmap_set (prog
, vers
, protocol
, xprt
->xp_port
))
214 libc_hidden_def (svc_register
)
216 /* Remove a service program from the callout list. */
218 svc_unregister (rpcprog_t prog
, rpcvers_t vers
)
220 struct svc_callout
*prev
;
221 register struct svc_callout
*s
;
223 if ((s
= svc_find (prog
, vers
, &prev
)) == NULL_SVC
)
226 if (prev
== NULL_SVC
)
227 svc_head
= s
->sc_next
;
229 prev
->sc_next
= s
->sc_next
;
231 s
->sc_next
= NULL_SVC
;
232 mem_free ((char *) s
, (u_int
) sizeof (struct svc_callout
));
233 /* now unregister the information with the local binder service */
234 if (! svc_is_mapped (prog
, vers
))
235 pmap_unset (prog
, vers
);
237 libc_hidden_def (svc_unregister
)
239 /* ******************* REPLY GENERATION ROUTINES ************ */
241 /* Send a reply to an rpc request */
243 svc_sendreply (register SVCXPRT
*xprt
, xdrproc_t xdr_results
,
244 caddr_t xdr_location
)
248 rply
.rm_direction
= REPLY
;
249 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
250 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
251 rply
.acpted_rply
.ar_stat
= SUCCESS
;
252 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
253 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
254 return SVC_REPLY (xprt
, &rply
);
256 INTDEF (svc_sendreply
)
258 /* No procedure error reply */
260 svcerr_noproc (register SVCXPRT
*xprt
)
264 rply
.rm_direction
= REPLY
;
265 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
266 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
267 rply
.acpted_rply
.ar_stat
= PROC_UNAVAIL
;
268 SVC_REPLY (xprt
, &rply
);
271 /* Can't decode args error reply */
273 svcerr_decode (register SVCXPRT
*xprt
)
277 rply
.rm_direction
= REPLY
;
278 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
279 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
280 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
281 SVC_REPLY (xprt
, &rply
);
283 INTDEF (svcerr_decode
)
285 /* Some system error */
287 svcerr_systemerr (register SVCXPRT
*xprt
)
291 rply
.rm_direction
= REPLY
;
292 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
293 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
294 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
295 SVC_REPLY (xprt
, &rply
);
298 /* Authentication error reply */
300 svcerr_auth (SVCXPRT
*xprt
, enum auth_stat why
)
304 rply
.rm_direction
= REPLY
;
305 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
306 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
307 rply
.rjcted_rply
.rj_why
= why
;
308 SVC_REPLY (xprt
, &rply
);
310 libc_hidden_def (svcerr_auth
)
312 /* Auth too weak error reply */
314 svcerr_weakauth (SVCXPRT
*xprt
)
316 svcerr_auth (xprt
, AUTH_TOOWEAK
);
319 /* Program unavailable error reply */
321 svcerr_noprog (register SVCXPRT
*xprt
)
325 rply
.rm_direction
= REPLY
;
326 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
327 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
328 rply
.acpted_rply
.ar_stat
= PROG_UNAVAIL
;
329 SVC_REPLY (xprt
, &rply
);
331 libc_hidden_def (svcerr_noprog
)
333 /* Program version mismatch error reply */
335 svcerr_progvers (register SVCXPRT
*xprt
, rpcvers_t low_vers
,
340 rply
.rm_direction
= REPLY
;
341 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
342 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
343 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
344 rply
.acpted_rply
.ar_vers
.low
= low_vers
;
345 rply
.acpted_rply
.ar_vers
.high
= high_vers
;
346 SVC_REPLY (xprt
, &rply
);
348 libc_hidden_def (svcerr_progvers
)
350 /* ******************* SERVER INPUT STUFF ******************* */
353 * Get server side input from some transport.
355 * Statement of authentication parameters management:
356 * This function owns and manages all authentication parameters, specifically
357 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
358 * the "cooked" credentials (rqst->rq_clntcred).
359 * However, this function does not know the structure of the cooked
360 * credentials, so it make the following assumptions:
361 * a) the structure is contiguous (no pointers), and
362 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
363 * In all events, all three parameters are freed upon exit from this routine.
364 * The storage is trivially management on the call stack in user land, but
365 * is mallocated in kernel land.
369 svc_getreq (int rdfds
)
374 readfds
.fds_bits
[0] = rdfds
;
375 INTUSE(svc_getreqset
) (&readfds
);
380 svc_getreqset (fd_set
*readfds
)
382 register fd_mask mask
;
383 register fd_mask
*maskp
;
384 register int setsize
;
388 setsize
= _rpc_dtablesize ();
389 if (setsize
> FD_SETSIZE
)
390 setsize
= FD_SETSIZE
;
391 maskp
= readfds
->fds_bits
;
392 for (sock
= 0; sock
< setsize
; sock
+= NFDBITS
)
393 for (mask
= *maskp
++; (bit
= ffsl (mask
)); mask
^= (1L << (bit
- 1)))
394 INTUSE(svc_getreq_common
) (sock
+ bit
- 1);
396 INTDEF (svc_getreqset
)
399 svc_getreq_poll (struct pollfd
*pfdp
, int pollretval
)
404 register int fds_found
;
405 for (int i
= fds_found
= 0; i
< svc_max_pollfd
; ++i
)
407 register struct pollfd
*p
= &pfdp
[i
];
409 if (p
->fd
!= -1 && p
->revents
)
411 /* fd has input waiting */
412 if (p
->revents
& POLLNVAL
)
413 xprt_unregister (xports
[p
->fd
]);
415 INTUSE(svc_getreq_common
) (p
->fd
);
417 if (++fds_found
>= pollretval
)
422 INTDEF (svc_getreq_poll
)
426 svc_getreq_common (const int fd
)
430 register SVCXPRT
*xprt
;
431 char cred_area
[2 * MAX_AUTH_BYTES
+ RQCRED_SIZE
];
432 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
433 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
436 /* Do we control fd? */
440 /* now receive msgs from xprtprt (support batch calls) */
443 if (SVC_RECV (xprt
, &msg
))
445 /* now find the exported program and call it */
446 struct svc_callout
*s
;
453 r
.rq_clntcred
= &(cred_area
[2 * MAX_AUTH_BYTES
]);
455 r
.rq_prog
= msg
.rm_call
.cb_prog
;
456 r
.rq_vers
= msg
.rm_call
.cb_vers
;
457 r
.rq_proc
= msg
.rm_call
.cb_proc
;
458 r
.rq_cred
= msg
.rm_call
.cb_cred
;
460 /* first authenticate the message */
461 /* Check for null flavor and bypass these calls if possible */
463 if (msg
.rm_call
.cb_cred
.oa_flavor
== AUTH_NULL
)
465 r
.rq_xprt
->xp_verf
.oa_flavor
= _null_auth
.oa_flavor
;
466 r
.rq_xprt
->xp_verf
.oa_length
= 0;
468 else if ((why
= INTUSE(_authenticate
) (&r
, &msg
)) != AUTH_OK
)
470 svcerr_auth (xprt
, why
);
474 /* now match message with a registered service */
479 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
)
481 if (s
->sc_prog
== r
.rq_prog
)
483 if (s
->sc_vers
== r
.rq_vers
)
485 (*s
->sc_dispatch
) (&r
, xprt
);
488 /* found correct version */
490 if (s
->sc_vers
< low_vers
)
491 low_vers
= s
->sc_vers
;
492 if (s
->sc_vers
> high_vers
)
493 high_vers
= s
->sc_vers
;
495 /* found correct program */
497 /* if we got here, the program or version
500 svcerr_progvers (xprt
, low_vers
, high_vers
);
502 svcerr_noprog (xprt
);
503 /* Fall through to ... */
506 if ((stat
= SVC_STAT (xprt
)) == XPRT_DIED
)
512 while (stat
== XPRT_MOREREQS
);
514 INTDEF (svc_getreq_common
)
516 #ifdef _RPC_THREAD_SAFE_
519 __rpc_thread_svc_cleanup (void)
521 struct svc_callout
*svcp
;
523 while ((svcp
= svc_head
) != NULL
)
524 svc_unregister (svcp
->sc_prog
, svcp
->sc_vers
);
527 #endif /* _RPC_THREAD_SAFE_ */