S390: Move utf8-utf32-z9.c to multiarch folder and use s390_libc_ifunc_expr macro.
[glibc.git] / sunrpc / svc.c
blob03f963018cedece7261207553a45ada4737458fb
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.
7 * Copyright (C) 2002-2017 Free Software Foundation, Inc.
8 * This file is part of the GNU C Library.
9 * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
11 * The GNU C Library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * The GNU C Library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with the GNU C Library; if not, see
23 * <http://www.gnu.org/licenses/>.
25 * Copyright (c) 2010, Oracle America, Inc.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions are
29 * met:
31 * * Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * * Redistributions in binary form must reproduce the above
34 * copyright notice, this list of conditions and the following
35 * disclaimer in the documentation and/or other materials
36 * provided with the distribution.
37 * * Neither the name of the "Oracle America, Inc." nor the names of its
38 * contributors may be used to endorse or promote products derived
39 * from this software without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
44 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
45 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
46 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
48 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
50 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 #include <errno.h>
56 #include <unistd.h>
57 #include <rpc/rpc.h>
58 #include <rpc/svc.h>
59 #include <rpc/pmap_clnt.h>
60 #include <sys/poll.h>
61 #include <time.h>
63 #ifdef _RPC_THREAD_SAFE_
64 #define xports RPC_THREAD_VARIABLE(svc_xports_s)
65 #else
66 static SVCXPRT **xports;
67 #endif
69 #define NULL_SVC ((struct svc_callout *)0)
70 #define RQCRED_SIZE 400 /* this size is excessive */
72 /* The services list
73 Each entry represents a set of procedures (an rpc program).
74 The dispatch routine takes request structs and runs the
75 appropriate procedure. */
76 struct svc_callout {
77 struct svc_callout *sc_next;
78 rpcprog_t sc_prog;
79 rpcvers_t sc_vers;
80 void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
81 bool_t sc_mapped;
83 #ifdef _RPC_THREAD_SAFE_
84 #define svc_head RPC_THREAD_VARIABLE(svc_head_s)
85 #else
86 static struct svc_callout *svc_head;
87 #endif
89 /* *************** SVCXPRT related stuff **************** */
91 /* Activate a transport handle. */
92 void
93 xprt_register (SVCXPRT *xprt)
95 register int sock = xprt->xp_sock;
96 register int i;
98 if (xports == NULL)
100 xports = (SVCXPRT **) calloc (_rpc_dtablesize (), sizeof (SVCXPRT *));
101 if (xports == NULL) /* Don't add handle */
102 return;
105 if (sock < _rpc_dtablesize ())
107 struct pollfd *new_svc_pollfd;
109 xports[sock] = xprt;
110 if (sock < FD_SETSIZE)
111 FD_SET (sock, &svc_fdset);
113 /* Check if we have an empty slot */
114 for (i = 0; i < svc_max_pollfd; ++i)
115 if (svc_pollfd[i].fd == -1)
117 svc_pollfd[i].fd = sock;
118 svc_pollfd[i].events = (POLLIN | POLLPRI |
119 POLLRDNORM | POLLRDBAND);
120 return;
123 new_svc_pollfd = (struct pollfd *) realloc (svc_pollfd,
124 sizeof (struct pollfd)
125 * (svc_max_pollfd + 1));
126 if (new_svc_pollfd == NULL) /* Out of memory */
127 return;
128 svc_pollfd = new_svc_pollfd;
129 ++svc_max_pollfd;
131 svc_pollfd[svc_max_pollfd - 1].fd = sock;
132 svc_pollfd[svc_max_pollfd - 1].events = (POLLIN | POLLPRI |
133 POLLRDNORM | POLLRDBAND);
136 libc_hidden_nolink_sunrpc (xprt_register, GLIBC_2_0)
138 /* De-activate a transport handle. */
139 void
140 xprt_unregister (SVCXPRT *xprt)
142 register int sock = xprt->xp_sock;
143 register int i;
145 if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
147 xports[sock] = (SVCXPRT *) 0;
149 if (sock < FD_SETSIZE)
150 FD_CLR (sock, &svc_fdset);
152 for (i = 0; i < svc_max_pollfd; ++i)
153 if (svc_pollfd[i].fd == sock)
154 svc_pollfd[i].fd = -1;
157 #ifdef EXPORT_RPC_SYMBOLS
158 libc_hidden_def (xprt_unregister)
159 #else
160 libc_hidden_nolink_sunrpc (xprt_unregister, GLIBC_2_0)
161 #endif
164 /* ********************** CALLOUT list related stuff ************* */
166 /* Search the callout list for a program number, return the callout
167 struct. */
168 static struct svc_callout *
169 svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
171 register struct svc_callout *s, *p;
173 p = NULL_SVC;
174 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
176 if ((s->sc_prog == prog) && (s->sc_vers == vers))
177 goto done;
178 p = s;
180 done:
181 *prev = p;
182 return s;
185 /* Add a service program to the callout list.
186 The dispatch routine will be called when a rpc request for this
187 program number comes in. */
188 bool_t
189 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
190 void (*dispatch) (struct svc_req *, SVCXPRT *),
191 rpcproc_t protocol)
193 struct svc_callout *prev;
194 register struct svc_callout *s;
196 if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
198 if (s->sc_dispatch == dispatch)
199 goto pmap_it; /* he is registering another xptr */
200 return FALSE;
202 s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
203 if (s == (struct svc_callout *) 0)
204 return FALSE;
206 s->sc_prog = prog;
207 s->sc_vers = vers;
208 s->sc_dispatch = dispatch;
209 s->sc_next = svc_head;
210 s->sc_mapped = FALSE;
211 svc_head = s;
213 pmap_it:
214 /* now register the information with the local binder service */
215 if (protocol)
217 if (! pmap_set (prog, vers, protocol, xprt->xp_port))
218 return FALSE;
220 s->sc_mapped = TRUE;
223 return TRUE;
225 #ifdef EXPORT_RPC_SYMBOLS
226 libc_hidden_def (svc_register)
227 #else
228 libc_hidden_nolink_sunrpc (svc_register, GLIBC_2_0)
229 #endif
231 /* Remove a service program from the callout list. */
232 void
233 svc_unregister (rpcprog_t prog, rpcvers_t vers)
235 struct svc_callout *prev;
236 register struct svc_callout *s;
238 if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
239 return;
240 bool is_mapped = s->sc_mapped;
242 if (prev == NULL_SVC)
243 svc_head = s->sc_next;
244 else
245 prev->sc_next = s->sc_next;
247 s->sc_next = NULL_SVC;
248 mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
249 /* now unregister the information with the local binder service */
250 if (is_mapped)
251 pmap_unset (prog, vers);
253 libc_hidden_nolink_sunrpc (svc_unregister, GLIBC_2_0)
255 /* ******************* REPLY GENERATION ROUTINES ************ */
257 /* Send a reply to an rpc request */
258 bool_t
259 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
260 caddr_t xdr_location)
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 = SUCCESS;
268 rply.acpted_rply.ar_results.where = xdr_location;
269 rply.acpted_rply.ar_results.proc = xdr_results;
270 return SVC_REPLY (xprt, &rply);
272 #ifdef EXPORT_RPC_SYMBOLS
273 libc_hidden_def (svc_sendreply)
274 #else
275 libc_hidden_nolink_sunrpc (svc_sendreply, GLIBC_2_0)
276 #endif
278 /* No procedure error reply */
279 void
280 svcerr_noproc (register SVCXPRT *xprt)
282 struct rpc_msg rply;
284 rply.rm_direction = REPLY;
285 rply.rm_reply.rp_stat = MSG_ACCEPTED;
286 rply.acpted_rply.ar_verf = xprt->xp_verf;
287 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
288 SVC_REPLY (xprt, &rply);
290 #ifdef EXPORT_RPC_SYMBOLS
291 libc_hidden_def (svcerr_noproc)
292 #else
293 libc_hidden_nolink_sunrpc (svcerr_noproc, GLIBC_2_0)
294 #endif
296 /* Can't decode args error reply */
297 void
298 svcerr_decode (register SVCXPRT *xprt)
300 struct rpc_msg rply;
302 rply.rm_direction = REPLY;
303 rply.rm_reply.rp_stat = MSG_ACCEPTED;
304 rply.acpted_rply.ar_verf = xprt->xp_verf;
305 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
306 SVC_REPLY (xprt, &rply);
308 #ifdef EXPORT_RPC_SYMBOLS
309 libc_hidden_def (svcerr_decode)
310 #else
311 libc_hidden_nolink_sunrpc (svcerr_decode, GLIBC_2_0)
312 #endif
314 /* Some system error */
315 void
316 svcerr_systemerr (register SVCXPRT *xprt)
318 struct rpc_msg rply;
320 rply.rm_direction = REPLY;
321 rply.rm_reply.rp_stat = MSG_ACCEPTED;
322 rply.acpted_rply.ar_verf = xprt->xp_verf;
323 rply.acpted_rply.ar_stat = SYSTEM_ERR;
324 SVC_REPLY (xprt, &rply);
326 #ifdef EXPORT_RPC_SYMBOLS
327 libc_hidden_def (svcerr_systemerr)
328 #else
329 libc_hidden_nolink_sunrpc (svcerr_systemerr, GLIBC_2_0)
330 #endif
332 /* Authentication error reply */
333 void
334 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
336 struct rpc_msg rply;
338 rply.rm_direction = REPLY;
339 rply.rm_reply.rp_stat = MSG_DENIED;
340 rply.rjcted_rply.rj_stat = AUTH_ERROR;
341 rply.rjcted_rply.rj_why = why;
342 SVC_REPLY (xprt, &rply);
344 libc_hidden_nolink_sunrpc (svcerr_auth, GLIBC_2_0)
346 /* Auth too weak error reply */
347 void
348 svcerr_weakauth (SVCXPRT *xprt)
350 svcerr_auth (xprt, AUTH_TOOWEAK);
352 libc_hidden_nolink_sunrpc (svcerr_weakauth, GLIBC_2_0)
354 /* Program unavailable error reply */
355 void
356 svcerr_noprog (register SVCXPRT *xprt)
358 struct rpc_msg rply;
360 rply.rm_direction = REPLY;
361 rply.rm_reply.rp_stat = MSG_ACCEPTED;
362 rply.acpted_rply.ar_verf = xprt->xp_verf;
363 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
364 SVC_REPLY (xprt, &rply);
366 libc_hidden_nolink_sunrpc (svcerr_noprog, GLIBC_2_0)
368 /* Program version mismatch error reply */
369 void
370 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
371 rpcvers_t high_vers)
373 struct rpc_msg rply;
375 rply.rm_direction = REPLY;
376 rply.rm_reply.rp_stat = MSG_ACCEPTED;
377 rply.acpted_rply.ar_verf = xprt->xp_verf;
378 rply.acpted_rply.ar_stat = PROG_MISMATCH;
379 rply.acpted_rply.ar_vers.low = low_vers;
380 rply.acpted_rply.ar_vers.high = high_vers;
381 SVC_REPLY (xprt, &rply);
383 libc_hidden_nolink_sunrpc (svcerr_progvers, GLIBC_2_0)
385 /* ******************* SERVER INPUT STUFF ******************* */
388 * Get server side input from some transport.
390 * Statement of authentication parameters management:
391 * This function owns and manages all authentication parameters, specifically
392 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
393 * the "cooked" credentials (rqst->rq_clntcred).
394 * However, this function does not know the structure of the cooked
395 * credentials, so it make the following assumptions:
396 * a) the structure is contiguous (no pointers), and
397 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
398 * In all events, all three parameters are freed upon exit from this routine.
399 * The storage is trivially management on the call stack in user land, but
400 * is mallocated in kernel land.
403 void
404 svc_getreq (int rdfds)
406 fd_set readfds;
408 FD_ZERO (&readfds);
409 readfds.fds_bits[0] = rdfds;
410 svc_getreqset (&readfds);
412 libc_hidden_nolink_sunrpc (svc_getreq, GLIBC_2_0)
414 void
415 svc_getreqset (fd_set *readfds)
417 register fd_mask mask;
418 register fd_mask *maskp;
419 register int setsize;
420 register int sock;
421 register int bit;
423 setsize = _rpc_dtablesize ();
424 if (setsize > FD_SETSIZE)
425 setsize = FD_SETSIZE;
426 maskp = readfds->fds_bits;
427 for (sock = 0; sock < setsize; sock += NFDBITS)
428 for (mask = *maskp++; (bit = ffsl (mask)); mask ^= (1L << (bit - 1)))
429 svc_getreq_common (sock + bit - 1);
431 libc_hidden_nolink_sunrpc (svc_getreqset, GLIBC_2_0)
433 void
434 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
436 if (pollretval == 0)
437 return;
439 register int fds_found;
440 for (int i = fds_found = 0; i < svc_max_pollfd; ++i)
442 register struct pollfd *p = &pfdp[i];
444 if (p->fd != -1 && p->revents)
446 /* fd has input waiting */
447 if (p->revents & POLLNVAL)
448 xprt_unregister (xports[p->fd]);
449 else
450 svc_getreq_common (p->fd);
452 if (++fds_found >= pollretval)
453 break;
457 #ifdef EXPORT_RPC_SYMBOLS
458 libc_hidden_def (svc_getreq_poll)
459 #else
460 libc_hidden_nolink_sunrpc (svc_getreq_poll, GLIBC_2_2)
461 #endif
464 void
465 svc_getreq_common (const int fd)
467 enum xprt_stat stat;
468 struct rpc_msg msg;
469 register SVCXPRT *xprt;
470 char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
471 msg.rm_call.cb_cred.oa_base = cred_area;
472 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
474 xprt = xports[fd];
475 /* Do we control fd? */
476 if (xprt == NULL)
477 return;
479 /* now receive msgs from xprtprt (support batch calls) */
482 if (SVC_RECV (xprt, &msg))
484 /* now find the exported program and call it */
485 struct svc_callout *s;
486 struct svc_req r;
487 enum auth_stat why;
488 rpcvers_t low_vers;
489 rpcvers_t high_vers;
490 int prog_found;
492 r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
493 r.rq_xprt = xprt;
494 r.rq_prog = msg.rm_call.cb_prog;
495 r.rq_vers = msg.rm_call.cb_vers;
496 r.rq_proc = msg.rm_call.cb_proc;
497 r.rq_cred = msg.rm_call.cb_cred;
499 /* first authenticate the message */
500 /* Check for null flavor and bypass these calls if possible */
502 if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
504 r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
505 r.rq_xprt->xp_verf.oa_length = 0;
507 else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
509 svcerr_auth (xprt, why);
510 goto call_done;
513 /* now match message with a registered service */
514 prog_found = FALSE;
515 low_vers = 0 - 1;
516 high_vers = 0;
518 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
520 if (s->sc_prog == r.rq_prog)
522 if (s->sc_vers == r.rq_vers)
524 (*s->sc_dispatch) (&r, xprt);
525 goto call_done;
527 /* found correct version */
528 prog_found = TRUE;
529 if (s->sc_vers < low_vers)
530 low_vers = s->sc_vers;
531 if (s->sc_vers > high_vers)
532 high_vers = s->sc_vers;
534 /* found correct program */
536 /* if we got here, the program or version
537 is not served ... */
538 if (prog_found)
539 svcerr_progvers (xprt, low_vers, high_vers);
540 else
541 svcerr_noprog (xprt);
542 /* Fall through to ... */
544 call_done:
545 if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
547 SVC_DESTROY (xprt);
548 break;
551 while (stat == XPRT_MOREREQS);
553 libc_hidden_nolink_sunrpc (svc_getreq_common, GLIBC_2_2)
555 /* If there are no file descriptors available, then accept will fail.
556 We want to delay here so the connection request can be dequeued;
557 otherwise we can bounce between polling and accepting, never giving the
558 request a chance to dequeue and eating an enormous amount of cpu time
559 in svc_run if we're polling on many file descriptors. */
560 void
561 __svc_accept_failed (void)
563 if (errno == EMFILE)
565 struct timespec ts = { .tv_sec = 0, .tv_nsec = 50000000 };
566 __nanosleep (&ts, NULL);
570 #ifdef _RPC_THREAD_SAFE_
572 void
573 __rpc_thread_svc_cleanup (void)
575 struct svc_callout *svcp;
577 while ((svcp = svc_head) != NULL)
578 svc_unregister (svcp->sc_prog, svcp->sc_vers);
581 #endif /* _RPC_THREAD_SAFE_ */