Comment on slow compilation of math/test-tgmath2.c
[glibc.git] / sunrpc / svc.c
blob103770a42ce58df5006f33b02394b93d318f8643
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) 2010, Oracle America, 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 the "Oracle America, 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_nolink_sunrpc (xprt_register, GLIBC_2_0)
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 #ifdef EXPORT_RPC_SYMBOLS
140 libc_hidden_def (xprt_unregister)
141 #else
142 libc_hidden_nolink_sunrpc (xprt_unregister, GLIBC_2_0)
143 #endif
146 /* ********************** CALLOUT list related stuff ************* */
148 /* Search the callout list for a program number, return the callout
149 struct. */
150 static struct svc_callout *
151 svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
153 register struct svc_callout *s, *p;
155 p = NULL_SVC;
156 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
158 if ((s->sc_prog == prog) && (s->sc_vers == vers))
159 goto done;
160 p = s;
162 done:
163 *prev = p;
164 return s;
168 static bool_t
169 svc_is_mapped (rpcprog_t prog, rpcvers_t vers)
171 struct svc_callout *prev;
172 register struct svc_callout *s;
173 s = svc_find (prog, vers, &prev);
174 return s!= NULL_SVC && s->sc_mapped;
178 /* Add a service program to the callout list.
179 The dispatch routine will be called when a rpc request for this
180 program number comes in. */
181 bool_t
182 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
183 void (*dispatch) (struct svc_req *, SVCXPRT *),
184 rpcproc_t protocol)
186 struct svc_callout *prev;
187 register struct svc_callout *s;
189 if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
191 if (s->sc_dispatch == dispatch)
192 goto pmap_it; /* he is registering another xptr */
193 return FALSE;
195 s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
196 if (s == (struct svc_callout *) 0)
197 return FALSE;
199 s->sc_prog = prog;
200 s->sc_vers = vers;
201 s->sc_dispatch = dispatch;
202 s->sc_next = svc_head;
203 s->sc_mapped = FALSE;
204 svc_head = s;
206 pmap_it:
207 /* now register the information with the local binder service */
208 if (protocol)
210 if (! pmap_set (prog, vers, protocol, xprt->xp_port))
211 return FALSE;
213 s->sc_mapped = TRUE;
216 return TRUE;
218 #ifdef EXPORT_RPC_SYMBOLS
219 libc_hidden_def (svc_register)
220 #else
221 libc_hidden_nolink_sunrpc (svc_register, GLIBC_2_0)
222 #endif
224 /* Remove a service program from the callout list. */
225 void
226 svc_unregister (rpcprog_t prog, rpcvers_t vers)
228 struct svc_callout *prev;
229 register struct svc_callout *s;
231 if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
232 return;
234 if (prev == NULL_SVC)
235 svc_head = s->sc_next;
236 else
237 prev->sc_next = s->sc_next;
239 s->sc_next = NULL_SVC;
240 mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
241 /* now unregister the information with the local binder service */
242 if (! svc_is_mapped (prog, vers))
243 pmap_unset (prog, vers);
245 libc_hidden_nolink_sunrpc (svc_unregister, GLIBC_2_0)
247 /* ******************* REPLY GENERATION ROUTINES ************ */
249 /* Send a reply to an rpc request */
250 bool_t
251 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
252 caddr_t xdr_location)
254 struct rpc_msg rply;
256 rply.rm_direction = REPLY;
257 rply.rm_reply.rp_stat = MSG_ACCEPTED;
258 rply.acpted_rply.ar_verf = xprt->xp_verf;
259 rply.acpted_rply.ar_stat = SUCCESS;
260 rply.acpted_rply.ar_results.where = xdr_location;
261 rply.acpted_rply.ar_results.proc = xdr_results;
262 return SVC_REPLY (xprt, &rply);
264 #ifdef EXPORT_RPC_SYMBOLS
265 libc_hidden_def (svc_sendreply)
266 #else
267 libc_hidden_nolink_sunrpc (svc_sendreply, GLIBC_2_0)
268 #endif
270 /* No procedure error reply */
271 void
272 svcerr_noproc (register SVCXPRT *xprt)
274 struct rpc_msg rply;
276 rply.rm_direction = REPLY;
277 rply.rm_reply.rp_stat = MSG_ACCEPTED;
278 rply.acpted_rply.ar_verf = xprt->xp_verf;
279 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
280 SVC_REPLY (xprt, &rply);
282 #ifdef EXPORT_RPC_SYMBOLS
283 libc_hidden_def (svcerr_noproc)
284 #else
285 libc_hidden_nolink_sunrpc (svcerr_noproc, GLIBC_2_0)
286 #endif
288 /* Can't decode args error reply */
289 void
290 svcerr_decode (register SVCXPRT *xprt)
292 struct rpc_msg rply;
294 rply.rm_direction = REPLY;
295 rply.rm_reply.rp_stat = MSG_ACCEPTED;
296 rply.acpted_rply.ar_verf = xprt->xp_verf;
297 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
298 SVC_REPLY (xprt, &rply);
300 #ifdef EXPORT_RPC_SYMBOLS
301 libc_hidden_def (svcerr_decode)
302 #else
303 libc_hidden_nolink_sunrpc (svcerr_decode, GLIBC_2_0)
304 #endif
306 /* Some system error */
307 void
308 svcerr_systemerr (register SVCXPRT *xprt)
310 struct rpc_msg rply;
312 rply.rm_direction = REPLY;
313 rply.rm_reply.rp_stat = MSG_ACCEPTED;
314 rply.acpted_rply.ar_verf = xprt->xp_verf;
315 rply.acpted_rply.ar_stat = SYSTEM_ERR;
316 SVC_REPLY (xprt, &rply);
318 #ifdef EXPORT_RPC_SYMBOLS
319 libc_hidden_def (svcerr_systemerr)
320 #else
321 libc_hidden_nolink_sunrpc (svcerr_systemerr, GLIBC_2_0)
322 #endif
324 /* Authentication error reply */
325 void
326 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
328 struct rpc_msg rply;
330 rply.rm_direction = REPLY;
331 rply.rm_reply.rp_stat = MSG_DENIED;
332 rply.rjcted_rply.rj_stat = AUTH_ERROR;
333 rply.rjcted_rply.rj_why = why;
334 SVC_REPLY (xprt, &rply);
336 libc_hidden_nolink_sunrpc (svcerr_auth, GLIBC_2_0)
338 /* Auth too weak error reply */
339 void
340 svcerr_weakauth (SVCXPRT *xprt)
342 svcerr_auth (xprt, AUTH_TOOWEAK);
344 libc_hidden_nolink_sunrpc (svcerr_weakauth, GLIBC_2_0)
346 /* Program unavailable error reply */
347 void
348 svcerr_noprog (register SVCXPRT *xprt)
350 struct rpc_msg rply;
352 rply.rm_direction = REPLY;
353 rply.rm_reply.rp_stat = MSG_ACCEPTED;
354 rply.acpted_rply.ar_verf = xprt->xp_verf;
355 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
356 SVC_REPLY (xprt, &rply);
358 libc_hidden_nolink_sunrpc (svcerr_noprog, GLIBC_2_0)
360 /* Program version mismatch error reply */
361 void
362 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
363 rpcvers_t high_vers)
365 struct rpc_msg rply;
367 rply.rm_direction = REPLY;
368 rply.rm_reply.rp_stat = MSG_ACCEPTED;
369 rply.acpted_rply.ar_verf = xprt->xp_verf;
370 rply.acpted_rply.ar_stat = PROG_MISMATCH;
371 rply.acpted_rply.ar_vers.low = low_vers;
372 rply.acpted_rply.ar_vers.high = high_vers;
373 SVC_REPLY (xprt, &rply);
375 libc_hidden_nolink_sunrpc (svcerr_progvers, GLIBC_2_0)
377 /* ******************* SERVER INPUT STUFF ******************* */
380 * Get server side input from some transport.
382 * Statement of authentication parameters management:
383 * This function owns and manages all authentication parameters, specifically
384 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
385 * the "cooked" credentials (rqst->rq_clntcred).
386 * However, this function does not know the structure of the cooked
387 * credentials, so it make the following assumptions:
388 * a) the structure is contiguous (no pointers), and
389 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
390 * In all events, all three parameters are freed upon exit from this routine.
391 * The storage is trivially management on the call stack in user land, but
392 * is mallocated in kernel land.
395 void
396 svc_getreq (int rdfds)
398 fd_set readfds;
400 FD_ZERO (&readfds);
401 readfds.fds_bits[0] = rdfds;
402 svc_getreqset (&readfds);
404 libc_hidden_nolink_sunrpc (svc_getreq, GLIBC_2_0)
406 void
407 svc_getreqset (fd_set *readfds)
409 register fd_mask mask;
410 register fd_mask *maskp;
411 register int setsize;
412 register int sock;
413 register int bit;
415 setsize = _rpc_dtablesize ();
416 if (setsize > FD_SETSIZE)
417 setsize = FD_SETSIZE;
418 maskp = readfds->fds_bits;
419 for (sock = 0; sock < setsize; sock += NFDBITS)
420 for (mask = *maskp++; (bit = ffsl (mask)); mask ^= (1L << (bit - 1)))
421 svc_getreq_common (sock + bit - 1);
423 libc_hidden_nolink_sunrpc (svc_getreqset, GLIBC_2_0)
425 void
426 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
428 if (pollretval == 0)
429 return;
431 register int fds_found;
432 for (int i = fds_found = 0; i < svc_max_pollfd; ++i)
434 register struct pollfd *p = &pfdp[i];
436 if (p->fd != -1 && p->revents)
438 /* fd has input waiting */
439 if (p->revents & POLLNVAL)
440 xprt_unregister (xports[p->fd]);
441 else
442 svc_getreq_common (p->fd);
444 if (++fds_found >= pollretval)
445 break;
449 #ifdef EXPORT_RPC_SYMBOLS
450 libc_hidden_def (svc_getreq_poll)
451 #else
452 libc_hidden_nolink_sunrpc (svc_getreq_poll, GLIBC_2_2)
453 #endif
456 void
457 svc_getreq_common (const int fd)
459 enum xprt_stat stat;
460 struct rpc_msg msg;
461 register SVCXPRT *xprt;
462 char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
463 msg.rm_call.cb_cred.oa_base = cred_area;
464 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
466 xprt = xports[fd];
467 /* Do we control fd? */
468 if (xprt == NULL)
469 return;
471 /* now receive msgs from xprtprt (support batch calls) */
474 if (SVC_RECV (xprt, &msg))
476 /* now find the exported program and call it */
477 struct svc_callout *s;
478 struct svc_req r;
479 enum auth_stat why;
480 rpcvers_t low_vers;
481 rpcvers_t high_vers;
482 int prog_found;
484 r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
485 r.rq_xprt = xprt;
486 r.rq_prog = msg.rm_call.cb_prog;
487 r.rq_vers = msg.rm_call.cb_vers;
488 r.rq_proc = msg.rm_call.cb_proc;
489 r.rq_cred = msg.rm_call.cb_cred;
491 /* first authenticate the message */
492 /* Check for null flavor and bypass these calls if possible */
494 if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
496 r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
497 r.rq_xprt->xp_verf.oa_length = 0;
499 else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
501 svcerr_auth (xprt, why);
502 goto call_done;
505 /* now match message with a registered service */
506 prog_found = FALSE;
507 low_vers = 0 - 1;
508 high_vers = 0;
510 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
512 if (s->sc_prog == r.rq_prog)
514 if (s->sc_vers == r.rq_vers)
516 (*s->sc_dispatch) (&r, xprt);
517 goto call_done;
519 /* found correct version */
520 prog_found = TRUE;
521 if (s->sc_vers < low_vers)
522 low_vers = s->sc_vers;
523 if (s->sc_vers > high_vers)
524 high_vers = s->sc_vers;
526 /* found correct program */
528 /* if we got here, the program or version
529 is not served ... */
530 if (prog_found)
531 svcerr_progvers (xprt, low_vers, high_vers);
532 else
533 svcerr_noprog (xprt);
534 /* Fall through to ... */
536 call_done:
537 if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
539 SVC_DESTROY (xprt);
540 break;
543 while (stat == XPRT_MOREREQS);
545 libc_hidden_nolink_sunrpc (svc_getreq_common, GLIBC_2_2)
547 #ifdef _RPC_THREAD_SAFE_
549 void
550 __rpc_thread_svc_cleanup (void)
552 struct svc_callout *svcp;
554 while ((svcp = svc_head) != NULL)
555 svc_unregister (svcp->sc_prog, svcp->sc_vers);
558 #endif /* _RPC_THREAD_SAFE_ */