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-2018 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
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.
59 #include <rpc/pmap_clnt.h>
62 #include <shlib-compat.h>
64 #define xports RPC_THREAD_VARIABLE(svc_xports_s)
66 #define NULL_SVC ((struct svc_callout *)0)
67 #define RQCRED_SIZE 400 /* this size is excessive */
70 Each entry represents a set of procedures (an rpc program).
71 The dispatch routine takes request structs and runs the
72 appropriate procedure. */
74 struct svc_callout
*sc_next
;
77 void (*sc_dispatch
) (struct svc_req
*, SVCXPRT
*);
80 #define svc_head RPC_THREAD_VARIABLE(svc_head_s)
82 /* *************** SVCXPRT related stuff **************** */
84 /* Activate a transport handle. */
86 xprt_register (SVCXPRT
*xprt
)
88 register int sock
= xprt
->xp_sock
;
93 xports
= (SVCXPRT
**) calloc (_rpc_dtablesize (), sizeof (SVCXPRT
*));
94 if (xports
== NULL
) /* Don't add handle */
98 if (sock
< _rpc_dtablesize ())
100 struct pollfd
*new_svc_pollfd
;
103 if (sock
< FD_SETSIZE
)
104 FD_SET (sock
, &svc_fdset
);
106 /* Check if we have an empty slot */
107 for (i
= 0; i
< svc_max_pollfd
; ++i
)
108 if (svc_pollfd
[i
].fd
== -1)
110 svc_pollfd
[i
].fd
= sock
;
111 svc_pollfd
[i
].events
= (POLLIN
| POLLPRI
|
112 POLLRDNORM
| POLLRDBAND
);
116 new_svc_pollfd
= (struct pollfd
*) realloc (svc_pollfd
,
117 sizeof (struct pollfd
)
118 * (svc_max_pollfd
+ 1));
119 if (new_svc_pollfd
== NULL
) /* Out of memory */
121 svc_pollfd
= new_svc_pollfd
;
124 svc_pollfd
[svc_max_pollfd
- 1].fd
= sock
;
125 svc_pollfd
[svc_max_pollfd
- 1].events
= (POLLIN
| POLLPRI
|
126 POLLRDNORM
| POLLRDBAND
);
129 libc_hidden_nolink_sunrpc (xprt_register
, GLIBC_2_0
)
131 /* De-activate a transport handle. */
133 xprt_unregister (SVCXPRT
*xprt
)
135 register int sock
= xprt
->xp_sock
;
138 if ((sock
< _rpc_dtablesize ()) && (xports
[sock
] == xprt
))
140 xports
[sock
] = (SVCXPRT
*) 0;
142 if (sock
< FD_SETSIZE
)
143 FD_CLR (sock
, &svc_fdset
);
145 for (i
= 0; i
< svc_max_pollfd
; ++i
)
146 if (svc_pollfd
[i
].fd
== sock
)
147 svc_pollfd
[i
].fd
= -1;
150 #ifdef EXPORT_RPC_SYMBOLS
151 libc_hidden_def (xprt_unregister
)
153 libc_hidden_nolink_sunrpc (xprt_unregister
, GLIBC_2_0
)
157 /* ********************** CALLOUT list related stuff ************* */
159 /* Search the callout list for a program number, return the callout
161 static struct svc_callout
*
162 svc_find (rpcprog_t prog
, rpcvers_t vers
, struct svc_callout
**prev
)
164 register struct svc_callout
*s
, *p
;
167 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
)
169 if ((s
->sc_prog
== prog
) && (s
->sc_vers
== vers
))
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. */
182 svc_register (SVCXPRT
* xprt
, rpcprog_t prog
, rpcvers_t vers
,
183 void (*dispatch
) (struct svc_req
*, SVCXPRT
*),
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 */
195 s
= (struct svc_callout
*) mem_alloc (sizeof (struct svc_callout
));
196 if (s
== (struct svc_callout
*) 0)
201 s
->sc_dispatch
= dispatch
;
202 s
->sc_next
= svc_head
;
203 s
->sc_mapped
= FALSE
;
207 /* now register the information with the local binder service */
210 if (! pmap_set (prog
, vers
, protocol
, xprt
->xp_port
))
218 #ifdef EXPORT_RPC_SYMBOLS
219 libc_hidden_def (svc_register
)
221 libc_hidden_nolink_sunrpc (svc_register
, GLIBC_2_0
)
224 /* Remove a service program from the callout list. */
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
)
233 bool is_mapped
= s
->sc_mapped
;
235 if (prev
== NULL_SVC
)
236 svc_head
= s
->sc_next
;
238 prev
->sc_next
= s
->sc_next
;
240 s
->sc_next
= NULL_SVC
;
241 mem_free ((char *) s
, (u_int
) sizeof (struct svc_callout
));
242 /* now unregister the information with the local binder service */
244 pmap_unset (prog
, vers
);
246 libc_hidden_nolink_sunrpc (svc_unregister
, GLIBC_2_0
)
248 /* ******************* REPLY GENERATION ROUTINES ************ */
250 /* Send a reply to an rpc request */
252 svc_sendreply (register SVCXPRT
*xprt
, xdrproc_t xdr_results
,
253 caddr_t xdr_location
)
257 rply
.rm_direction
= REPLY
;
258 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
259 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
260 rply
.acpted_rply
.ar_stat
= SUCCESS
;
261 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
262 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
263 return SVC_REPLY (xprt
, &rply
);
265 #ifdef EXPORT_RPC_SYMBOLS
266 libc_hidden_def (svc_sendreply
)
268 libc_hidden_nolink_sunrpc (svc_sendreply
, GLIBC_2_0
)
271 /* No procedure error reply */
273 svcerr_noproc (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
= PROC_UNAVAIL
;
281 SVC_REPLY (xprt
, &rply
);
283 #ifdef EXPORT_RPC_SYMBOLS
284 libc_hidden_def (svcerr_noproc
)
286 libc_hidden_nolink_sunrpc (svcerr_noproc
, GLIBC_2_0
)
289 /* Can't decode args error reply */
291 svcerr_decode (register SVCXPRT
*xprt
)
295 rply
.rm_direction
= REPLY
;
296 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
297 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
298 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
299 SVC_REPLY (xprt
, &rply
);
301 #ifdef EXPORT_RPC_SYMBOLS
302 libc_hidden_def (svcerr_decode
)
304 libc_hidden_nolink_sunrpc (svcerr_decode
, GLIBC_2_0
)
307 /* Some system error */
309 svcerr_systemerr (register SVCXPRT
*xprt
)
313 rply
.rm_direction
= REPLY
;
314 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
315 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
316 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
317 SVC_REPLY (xprt
, &rply
);
319 #ifdef EXPORT_RPC_SYMBOLS
320 libc_hidden_def (svcerr_systemerr
)
322 libc_hidden_nolink_sunrpc (svcerr_systemerr
, GLIBC_2_0
)
325 /* Authentication error reply */
327 svcerr_auth (SVCXPRT
*xprt
, enum auth_stat why
)
331 rply
.rm_direction
= REPLY
;
332 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
333 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
334 rply
.rjcted_rply
.rj_why
= why
;
335 SVC_REPLY (xprt
, &rply
);
337 libc_hidden_nolink_sunrpc (svcerr_auth
, GLIBC_2_0
)
339 /* Auth too weak error reply */
341 svcerr_weakauth (SVCXPRT
*xprt
)
343 svcerr_auth (xprt
, AUTH_TOOWEAK
);
345 libc_hidden_nolink_sunrpc (svcerr_weakauth
, GLIBC_2_0
)
347 /* Program unavailable error reply */
349 svcerr_noprog (register SVCXPRT
*xprt
)
353 rply
.rm_direction
= REPLY
;
354 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
355 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
356 rply
.acpted_rply
.ar_stat
= PROG_UNAVAIL
;
357 SVC_REPLY (xprt
, &rply
);
359 libc_hidden_nolink_sunrpc (svcerr_noprog
, GLIBC_2_0
)
361 /* Program version mismatch error reply */
363 svcerr_progvers (register SVCXPRT
*xprt
, rpcvers_t low_vers
,
368 rply
.rm_direction
= REPLY
;
369 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
370 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
371 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
372 rply
.acpted_rply
.ar_vers
.low
= low_vers
;
373 rply
.acpted_rply
.ar_vers
.high
= high_vers
;
374 SVC_REPLY (xprt
, &rply
);
376 libc_hidden_nolink_sunrpc (svcerr_progvers
, GLIBC_2_0
)
378 /* ******************* SERVER INPUT STUFF ******************* */
381 * Get server side input from some transport.
383 * Statement of authentication parameters management:
384 * This function owns and manages all authentication parameters, specifically
385 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
386 * the "cooked" credentials (rqst->rq_clntcred).
387 * However, this function does not know the structure of the cooked
388 * credentials, so it make the following assumptions:
389 * a) the structure is contiguous (no pointers), and
390 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
391 * In all events, all three parameters are freed upon exit from this routine.
392 * The storage is trivially management on the call stack in user land, but
393 * is mallocated in kernel land.
397 svc_getreq (int rdfds
)
402 readfds
.fds_bits
[0] = rdfds
;
403 svc_getreqset (&readfds
);
405 libc_hidden_nolink_sunrpc (svc_getreq
, GLIBC_2_0
)
408 svc_getreqset (fd_set
*readfds
)
410 register fd_mask mask
;
411 register fd_mask
*maskp
;
412 register int setsize
;
416 setsize
= _rpc_dtablesize ();
417 if (setsize
> FD_SETSIZE
)
418 setsize
= FD_SETSIZE
;
419 maskp
= readfds
->fds_bits
;
420 for (sock
= 0; sock
< setsize
; sock
+= NFDBITS
)
421 for (mask
= *maskp
++; (bit
= ffsl (mask
)); mask
^= (1L << (bit
- 1)))
422 svc_getreq_common (sock
+ bit
- 1);
424 libc_hidden_nolink_sunrpc (svc_getreqset
, GLIBC_2_0
)
427 svc_getreq_poll (struct pollfd
*pfdp
, int pollretval
)
432 register int fds_found
;
433 for (int i
= fds_found
= 0; i
< svc_max_pollfd
; ++i
)
435 register struct pollfd
*p
= &pfdp
[i
];
437 if (p
->fd
!= -1 && p
->revents
)
439 /* fd has input waiting */
440 if (p
->revents
& POLLNVAL
)
441 xprt_unregister (xports
[p
->fd
]);
443 svc_getreq_common (p
->fd
);
445 if (++fds_found
>= pollretval
)
450 #ifdef EXPORT_RPC_SYMBOLS
451 libc_hidden_def (svc_getreq_poll
)
453 libc_hidden_nolink_sunrpc (svc_getreq_poll
, GLIBC_2_2
)
458 svc_getreq_common (const int fd
)
462 register SVCXPRT
*xprt
;
463 char cred_area
[2 * MAX_AUTH_BYTES
+ RQCRED_SIZE
];
464 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
465 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
468 /* Do we control fd? */
472 /* now receive msgs from xprtprt (support batch calls) */
475 if (SVC_RECV (xprt
, &msg
))
477 /* now find the exported program and call it */
478 struct svc_callout
*s
;
485 r
.rq_clntcred
= &(cred_area
[2 * MAX_AUTH_BYTES
]);
487 r
.rq_prog
= msg
.rm_call
.cb_prog
;
488 r
.rq_vers
= msg
.rm_call
.cb_vers
;
489 r
.rq_proc
= msg
.rm_call
.cb_proc
;
490 r
.rq_cred
= msg
.rm_call
.cb_cred
;
492 /* first authenticate the message */
493 /* Check for null flavor and bypass these calls if possible */
495 if (msg
.rm_call
.cb_cred
.oa_flavor
== AUTH_NULL
)
497 r
.rq_xprt
->xp_verf
.oa_flavor
= _null_auth
.oa_flavor
;
498 r
.rq_xprt
->xp_verf
.oa_length
= 0;
500 else if ((why
= _authenticate (&r
, &msg
)) != AUTH_OK
)
502 svcerr_auth (xprt
, why
);
506 /* now match message with a registered service */
511 for (s
= svc_head
; s
!= NULL_SVC
; s
= s
->sc_next
)
513 if (s
->sc_prog
== r
.rq_prog
)
515 if (s
->sc_vers
== r
.rq_vers
)
517 (*s
->sc_dispatch
) (&r
, xprt
);
520 /* found correct version */
522 if (s
->sc_vers
< low_vers
)
523 low_vers
= s
->sc_vers
;
524 if (s
->sc_vers
> high_vers
)
525 high_vers
= s
->sc_vers
;
527 /* found correct program */
529 /* if we got here, the program or version
532 svcerr_progvers (xprt
, low_vers
, high_vers
);
534 svcerr_noprog (xprt
);
535 /* Fall through to ... */
538 if ((stat
= SVC_STAT (xprt
)) == XPRT_DIED
)
544 while (stat
== XPRT_MOREREQS
);
546 libc_hidden_nolink_sunrpc (svc_getreq_common
, GLIBC_2_2
)
548 /* If there are no file descriptors available, then accept will fail.
549 We want to delay here so the connection request can be dequeued;
550 otherwise we can bounce between polling and accepting, never giving the
551 request a chance to dequeue and eating an enormous amount of cpu time
552 in svc_run if we're polling on many file descriptors. */
554 __svc_accept_failed (void)
558 struct timespec ts
= { .tv_sec
= 0, .tv_nsec
= 50000000 };
559 __nanosleep (&ts
, NULL
);
565 __rpc_thread_svc_cleanup (void)
567 struct svc_callout
*svcp
;
569 while ((svcp
= svc_head
) != NULL
)
570 svc_unregister (svcp
->sc_prog
, svcp
->sc_vers
);