power7-optimized 64-bit and 32-bit memcpy
[glibc.git] / sunrpc / svc.c
blobd389a5285d0275d1cbce49e3c666b51462e5aaf8
1 /*
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
12 * met:
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.
38 #include <errno.h>
39 #include <unistd.h>
40 #include <rpc/rpc.h>
41 #include <rpc/svc.h>
42 #include <rpc/pmap_clnt.h>
43 #include <sys/poll.h>
45 #ifdef _RPC_THREAD_SAFE_
46 #define xports RPC_THREAD_VARIABLE(svc_xports_s)
47 #else
48 static SVCXPRT **xports;
49 #endif
51 #define NULL_SVC ((struct svc_callout *)0)
52 #define RQCRED_SIZE 400 /* this size is excessive */
54 /* The services list
55 Each entry represents a set of procedures (an rpc program).
56 The dispatch routine takes request structs and runs the
57 appropriate procedure. */
58 struct svc_callout {
59 struct svc_callout *sc_next;
60 rpcprog_t sc_prog;
61 rpcvers_t sc_vers;
62 void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
63 bool_t sc_mapped;
65 #ifdef _RPC_THREAD_SAFE_
66 #define svc_head RPC_THREAD_VARIABLE(svc_head_s)
67 #else
68 static struct svc_callout *svc_head;
69 #endif
71 /* *************** SVCXPRT related stuff **************** */
73 /* Activate a transport handle. */
74 void
75 xprt_register (SVCXPRT *xprt)
77 register int sock = xprt->xp_sock;
78 register int i;
80 if (xports == NULL)
82 xports = (SVCXPRT **) malloc (_rpc_dtablesize () * sizeof (SVCXPRT *));
83 if (xports == NULL) /* DonĀ“t add handle */
84 return;
87 if (sock < _rpc_dtablesize ())
89 struct pollfd *new_svc_pollfd;
91 xports[sock] = xprt;
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);
102 return;
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 */
109 return;
110 svc_pollfd = new_svc_pollfd;
111 ++svc_max_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. */
121 void
122 xprt_unregister (SVCXPRT *xprt)
124 register int sock = xprt->xp_sock;
125 register int i;
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
145 struct. */
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;
151 p = NULL_SVC;
152 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
154 if ((s->sc_prog == prog) && (s->sc_vers == vers))
155 goto done;
156 p = s;
158 done:
159 *prev = p;
160 return s;
164 static bool_t
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. */
177 bool_t
178 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
179 void (*dispatch) (struct svc_req *, SVCXPRT *),
180 rpcproc_t protocol)
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 */
189 return FALSE;
191 s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
192 if (s == (struct svc_callout *) 0)
193 return FALSE;
195 s->sc_prog = prog;
196 s->sc_vers = vers;
197 s->sc_dispatch = dispatch;
198 s->sc_next = svc_head;
199 s->sc_mapped = FALSE;
200 svc_head = s;
202 pmap_it:
203 /* now register the information with the local binder service */
204 if (protocol)
206 if (! pmap_set (prog, vers, protocol, xprt->xp_port))
207 return FALSE;
209 s->sc_mapped = TRUE;
212 return TRUE;
214 libc_hidden_def (svc_register)
216 /* Remove a service program from the callout list. */
217 void
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)
224 return;
226 if (prev == NULL_SVC)
227 svc_head = s->sc_next;
228 else
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 */
242 bool_t
243 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
244 caddr_t xdr_location)
246 struct rpc_msg rply;
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 */
259 void
260 svcerr_noproc (register SVCXPRT *xprt)
262 struct rpc_msg rply;
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 */
272 void
273 svcerr_decode (register SVCXPRT *xprt)
275 struct rpc_msg rply;
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 */
286 void
287 svcerr_systemerr (register SVCXPRT *xprt)
289 struct rpc_msg rply;
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 */
299 void
300 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
302 struct rpc_msg rply;
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 */
313 void
314 svcerr_weakauth (SVCXPRT *xprt)
316 svcerr_auth (xprt, AUTH_TOOWEAK);
319 /* Program unavailable error reply */
320 void
321 svcerr_noprog (register SVCXPRT *xprt)
323 struct rpc_msg rply;
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 */
334 void
335 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
336 rpcvers_t high_vers)
338 struct rpc_msg rply;
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.
368 void
369 svc_getreq (int rdfds)
371 fd_set readfds;
373 FD_ZERO (&readfds);
374 readfds.fds_bits[0] = rdfds;
375 INTUSE(svc_getreqset) (&readfds);
377 INTDEF (svc_getreq)
379 void
380 svc_getreqset (fd_set *readfds)
382 register fd_mask mask;
383 register fd_mask *maskp;
384 register int setsize;
385 register int sock;
386 register int bit;
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)
398 void
399 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
401 if (pollretval == 0)
402 return;
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]);
414 else
415 INTUSE(svc_getreq_common) (p->fd);
417 if (++fds_found >= pollretval)
418 break;
422 INTDEF (svc_getreq_poll)
425 void
426 svc_getreq_common (const int fd)
428 enum xprt_stat stat;
429 struct rpc_msg msg;
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]);
435 xprt = xports[fd];
436 /* Do we control fd? */
437 if (xprt == NULL)
438 return;
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;
447 struct svc_req r;
448 enum auth_stat why;
449 rpcvers_t low_vers;
450 rpcvers_t high_vers;
451 int prog_found;
453 r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
454 r.rq_xprt = xprt;
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);
471 goto call_done;
474 /* now match message with a registered service */
475 prog_found = FALSE;
476 low_vers = 0 - 1;
477 high_vers = 0;
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);
486 goto call_done;
488 /* found correct version */
489 prog_found = TRUE;
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
498 is not served ... */
499 if (prog_found)
500 svcerr_progvers (xprt, low_vers, high_vers);
501 else
502 svcerr_noprog (xprt);
503 /* Fall through to ... */
505 call_done:
506 if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
508 SVC_DESTROY (xprt);
509 break;
512 while (stat == XPRT_MOREREQS);
514 INTDEF (svc_getreq_common)
516 #ifdef _RPC_THREAD_SAFE_
518 void
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_ */