OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / inet / rpc / svc.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 /*
30  * svc.c, Server-side remote procedure call interface.
31  *
32  * There are two sets of procedures here.  The xprt routines are
33  * for handling transport handles.  The svc routines handle the
34  * list of service routines.
35  *
36  * Copyright (C) 1984, Sun Microsystems, Inc.
37  */
38
39 #define __FORCE_GLIBC
40 #define _GNU_SOURCE
41 #include <features.h>
42
43 #include <errno.h>
44 #include <unistd.h>
45 #include "rpc_private.h"
46 #include <rpc/svc.h>
47 #include <rpc/pmap_clnt.h>
48 #include <sys/poll.h>
49
50 libc_hidden_proto(ffs)
51 libc_hidden_proto(pmap_set)
52 libc_hidden_proto(pmap_unset)
53 libc_hidden_proto(_authenticate)
54 libc_hidden_proto(_rpc_dtablesize)
55 /* used by svc_[max_]pollfd */
56 libc_hidden_proto(__rpc_thread_svc_pollfd)
57 libc_hidden_proto(__rpc_thread_svc_max_pollfd)
58 /* used by svc_fdset */
59 libc_hidden_proto(__rpc_thread_svc_fdset)
60
61 #ifdef __UCLIBC_HAS_THREADS__
62 #define xports (*(SVCXPRT ***)&RPC_THREAD_VARIABLE(svc_xports_s))
63 #else
64 static SVCXPRT **xports;
65 #endif
66
67 #define NULL_SVC ((struct svc_callout *)0)
68 #define RQCRED_SIZE     400     /* this size is excessive */
69
70 /* The services list
71    Each entry represents a set of procedures (an rpc program).
72    The dispatch routine takes request structs and runs the
73    appropriate procedure. */
74 struct svc_callout {
75   struct svc_callout *sc_next;
76   rpcprog_t sc_prog;
77   rpcvers_t sc_vers;
78   void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
79 };
80 #ifdef __UCLIBC_HAS_THREADS__
81 #define svc_head (*(struct svc_callout **)&RPC_THREAD_VARIABLE(svc_head_s))
82 #else
83 static struct svc_callout *svc_head;
84 #endif
85
86 /* ***************  SVCXPRT related stuff **************** */
87
88 /* Activate a transport handle. */
89 void
90 xprt_register (SVCXPRT *xprt)
91 {
92   register int sock = xprt->xp_sock;
93   register int i;
94
95   if (xports == NULL)
96     {
97       xports = (SVCXPRT **) malloc (_rpc_dtablesize () * sizeof (SVCXPRT *));
98       if (xports == NULL) /* DonĀ“t add handle */
99         return;
100     }
101
102   if (sock < _rpc_dtablesize ())
103     {
104       xports[sock] = xprt;
105       if (sock < FD_SETSIZE)
106         FD_SET (sock, &svc_fdset);
107
108       /* Check if we have an empty slot */
109       for (i = 0; i < svc_max_pollfd; ++i)
110         if (svc_pollfd[i].fd == -1)
111           {
112             svc_pollfd[i].fd = sock;
113             svc_pollfd[i].events = (POLLIN | POLLPRI |
114                                     POLLRDNORM | POLLRDBAND);
115             return;
116           }
117
118       ++svc_max_pollfd;
119       svc_pollfd = realloc (svc_pollfd,
120                             sizeof (struct pollfd) * svc_max_pollfd);
121       if (svc_pollfd == NULL) /* Out of memory */
122         return;
123
124       svc_pollfd[svc_max_pollfd - 1].fd = sock;
125       svc_pollfd[svc_max_pollfd - 1].events = (POLLIN | POLLPRI |
126                                                POLLRDNORM | POLLRDBAND);
127     }
128 }
129 libc_hidden_proto(xprt_register)
130 libc_hidden_def(xprt_register)
131
132 /* De-activate a transport handle. */
133 void
134 xprt_unregister (SVCXPRT *xprt)
135 {
136   register int sock = xprt->xp_sock;
137   register int i;
138
139   if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
140     {
141       xports[sock] = (SVCXPRT *) 0;
142
143       if (sock < FD_SETSIZE)
144         FD_CLR (sock, &svc_fdset);
145
146       for (i = 0; i < svc_max_pollfd; ++i)
147         if (svc_pollfd[i].fd == sock)
148           svc_pollfd[i].fd = -1;
149     }
150 }
151 libc_hidden_proto(xprt_unregister)
152 libc_hidden_def(xprt_unregister)
153
154
155 /* ********************** CALLOUT list related stuff ************* */
156
157 /* Search the callout list for a program number, return the callout
158    struct. */
159 static struct svc_callout *
160 svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
161 {
162   register struct svc_callout *s, *p;
163
164   p = NULL_SVC;
165   for (s = svc_head; s != NULL_SVC; s = s->sc_next)
166     {
167       if ((s->sc_prog == prog) && (s->sc_vers == vers))
168         goto done;
169       p = s;
170     }
171 done:
172   *prev = p;
173   return s;
174 }
175
176 /* Add a service program to the callout list.
177    The dispatch routine will be called when a rpc request for this
178    program number comes in. */
179 bool_t
180 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
181               void (*dispatch) (struct svc_req *, SVCXPRT *),
182               rpcproc_t protocol)
183 {
184   struct svc_callout *prev;
185   register struct svc_callout *s;
186
187   if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
188     {
189       if (s->sc_dispatch == dispatch)
190         goto pmap_it;           /* he is registering another xptr */
191       return FALSE;
192     }
193   s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
194   if (s == (struct svc_callout *) 0)
195     return FALSE;
196
197   s->sc_prog = prog;
198   s->sc_vers = vers;
199   s->sc_dispatch = dispatch;
200   s->sc_next = svc_head;
201   svc_head = s;
202
203 pmap_it:
204   /* now register the information with the local binder service */
205   if (protocol)
206     return pmap_set (prog, vers, protocol, xprt->xp_port);
207
208   return TRUE;
209 }
210 libc_hidden_proto(svc_register)
211 libc_hidden_def(svc_register)
212
213 /* Remove a service program from the callout list. */
214 void
215 svc_unregister (rpcprog_t prog, rpcvers_t vers)
216 {
217   struct svc_callout *prev;
218   register struct svc_callout *s;
219
220   if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
221     return;
222
223   if (prev == NULL_SVC)
224     svc_head = s->sc_next;
225   else
226     prev->sc_next = s->sc_next;
227
228   s->sc_next = NULL_SVC;
229   mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
230   /* now unregister the information with the local binder service */
231   pmap_unset (prog, vers);
232 }
233 libc_hidden_proto(svc_unregister)
234 libc_hidden_def(svc_unregister)
235
236 /* ******************* REPLY GENERATION ROUTINES  ************ */
237
238 /* Send a reply to an rpc request */
239 bool_t
240 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
241                caddr_t xdr_location)
242 {
243   struct rpc_msg rply;
244
245   rply.rm_direction = REPLY;
246   rply.rm_reply.rp_stat = MSG_ACCEPTED;
247   rply.acpted_rply.ar_verf = xprt->xp_verf;
248   rply.acpted_rply.ar_stat = SUCCESS;
249   rply.acpted_rply.ar_results.where = xdr_location;
250   rply.acpted_rply.ar_results.proc = xdr_results;
251   return SVC_REPLY (xprt, &rply);
252 }
253 libc_hidden_proto(svc_sendreply)
254 libc_hidden_def(svc_sendreply)
255
256 /* No procedure error reply */
257 void
258 svcerr_noproc (register SVCXPRT *xprt)
259 {
260   struct rpc_msg rply;
261
262   rply.rm_direction = REPLY;
263   rply.rm_reply.rp_stat = MSG_ACCEPTED;
264   rply.acpted_rply.ar_verf = xprt->xp_verf;
265   rply.acpted_rply.ar_stat = PROC_UNAVAIL;
266   SVC_REPLY (xprt, &rply);
267 }
268
269 /* Can't decode args error reply */
270 void
271 svcerr_decode (register SVCXPRT *xprt)
272 {
273   struct rpc_msg rply;
274
275   rply.rm_direction = REPLY;
276   rply.rm_reply.rp_stat = MSG_ACCEPTED;
277   rply.acpted_rply.ar_verf = xprt->xp_verf;
278   rply.acpted_rply.ar_stat = GARBAGE_ARGS;
279   SVC_REPLY (xprt, &rply);
280 }
281 libc_hidden_proto(svcerr_decode)
282 libc_hidden_def(svcerr_decode)
283
284 /* Some system error */
285 void
286 svcerr_systemerr (register SVCXPRT *xprt)
287 {
288   struct rpc_msg rply;
289
290   rply.rm_direction = REPLY;
291   rply.rm_reply.rp_stat = MSG_ACCEPTED;
292   rply.acpted_rply.ar_verf = xprt->xp_verf;
293   rply.acpted_rply.ar_stat = SYSTEM_ERR;
294   SVC_REPLY (xprt, &rply);
295 }
296
297 /* Authentication error reply */
298 void
299 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
300 {
301   struct rpc_msg rply;
302
303   rply.rm_direction = REPLY;
304   rply.rm_reply.rp_stat = MSG_DENIED;
305   rply.rjcted_rply.rj_stat = AUTH_ERROR;
306   rply.rjcted_rply.rj_why = why;
307   SVC_REPLY (xprt, &rply);
308 }
309 libc_hidden_proto(svcerr_auth)
310 libc_hidden_def(svcerr_auth)
311
312 /* Auth too weak error reply */
313 void
314 svcerr_weakauth (SVCXPRT *xprt)
315 {
316   svcerr_auth (xprt, AUTH_TOOWEAK);
317 }
318
319 /* Program unavailable error reply */
320 void
321 svcerr_noprog (register SVCXPRT *xprt)
322 {
323   struct rpc_msg rply;
324
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);
330 }
331 libc_hidden_proto(svcerr_noprog)
332 libc_hidden_def(svcerr_noprog)
333
334 /* Program version mismatch error reply */
335 void
336 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
337                  rpcvers_t high_vers)
338 {
339   struct rpc_msg rply;
340
341   rply.rm_direction = REPLY;
342   rply.rm_reply.rp_stat = MSG_ACCEPTED;
343   rply.acpted_rply.ar_verf = xprt->xp_verf;
344   rply.acpted_rply.ar_stat = PROG_MISMATCH;
345   rply.acpted_rply.ar_vers.low = low_vers;
346   rply.acpted_rply.ar_vers.high = high_vers;
347   SVC_REPLY (xprt, &rply);
348 }
349 libc_hidden_proto(svcerr_progvers)
350 libc_hidden_def(svcerr_progvers)
351
352 /* ******************* SERVER INPUT STUFF ******************* */
353
354 /*
355  * Get server side input from some transport.
356  *
357  * Statement of authentication parameters management:
358  * This function owns and manages all authentication parameters, specifically
359  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
360  * the "cooked" credentials (rqst->rq_clntcred).
361  * However, this function does not know the structure of the cooked
362  * credentials, so it make the following assumptions:
363  *   a) the structure is contiguous (no pointers), and
364  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
365  * In all events, all three parameters are freed upon exit from this routine.
366  * The storage is trivially management on the call stack in user land, but
367  * is mallocated in kernel land.
368  */
369
370 void
371 svc_getreq_common (const int fd)
372 {
373   enum xprt_stat stat;
374   struct rpc_msg msg;
375   register SVCXPRT *xprt;
376   char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
377   msg.rm_call.cb_cred.oa_base = cred_area;
378   msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
379
380   xprt = xports[fd];
381   /* Do we control fd? */
382   if (xprt == NULL)
383      return;
384
385   /* now receive msgs from xprtprt (support batch calls) */
386   do
387     {
388       if (SVC_RECV (xprt, &msg))
389         {
390           /* now find the exported program and call it */
391           struct svc_callout *s;
392           struct svc_req r;
393           enum auth_stat why;
394           rpcvers_t low_vers;
395           rpcvers_t high_vers;
396           int prog_found;
397
398           r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
399           r.rq_xprt = xprt;
400           r.rq_prog = msg.rm_call.cb_prog;
401           r.rq_vers = msg.rm_call.cb_vers;
402           r.rq_proc = msg.rm_call.cb_proc;
403           r.rq_cred = msg.rm_call.cb_cred;
404
405           /* first authenticate the message */
406           /* Check for null flavor and bypass these calls if possible */
407
408           if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
409             {
410               r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
411               r.rq_xprt->xp_verf.oa_length = 0;
412             }
413           else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
414             {
415               svcerr_auth (xprt, why);
416               goto call_done;
417             }
418
419           /* now match message with a registered service */
420           prog_found = FALSE;
421           low_vers = 0 - 1;
422           high_vers = 0;
423
424           for (s = svc_head; s != NULL_SVC; s = s->sc_next)
425             {
426               if (s->sc_prog == r.rq_prog)
427                 {
428                   if (s->sc_vers == r.rq_vers)
429                     {
430                       (*s->sc_dispatch) (&r, xprt);
431                       goto call_done;
432                     }
433                   /* found correct version */
434                   prog_found = TRUE;
435                   if (s->sc_vers < low_vers)
436                     low_vers = s->sc_vers;
437                   if (s->sc_vers > high_vers)
438                     high_vers = s->sc_vers;
439                 }
440               /* found correct program */
441             }
442           /* if we got here, the program or version
443              is not served ... */
444           if (prog_found)
445             svcerr_progvers (xprt, low_vers, high_vers);
446           else
447             svcerr_noprog (xprt);
448           /* Fall through to ... */
449         }
450     call_done:
451       if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
452         {
453           SVC_DESTROY (xprt);
454           break;
455         }
456     }
457   while (stat == XPRT_MOREREQS);
458 }
459 libc_hidden_proto(svc_getreq_common)
460 libc_hidden_def(svc_getreq_common)
461
462 void
463 svc_getreqset (fd_set *readfds)
464 {
465   register u_int32_t mask;
466   register u_int32_t *maskp;
467   register int setsize;
468   register int sock;
469   register int bit;
470
471   setsize = _rpc_dtablesize ();
472   maskp = (u_int32_t *) readfds->fds_bits;
473   for (sock = 0; sock < setsize; sock += 32)
474     for (mask = *maskp++; (bit = ffs (mask)); mask ^= (1 << (bit - 1)))
475       svc_getreq_common (sock + bit - 1);
476 }
477 libc_hidden_proto(svc_getreqset)
478 libc_hidden_def(svc_getreqset)
479
480 void
481 svc_getreq (int rdfds)
482 {
483   fd_set readfds;
484
485   FD_ZERO (&readfds);
486   readfds.fds_bits[0] = rdfds;
487   svc_getreqset (&readfds);
488 }
489 libc_hidden_proto(svc_getreq)
490 libc_hidden_def(svc_getreq)
491
492 void
493 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
494 {
495   register int i;
496   register int fds_found;
497
498   for (i = fds_found = 0; i < svc_max_pollfd && fds_found < pollretval; ++i)
499     {
500       register struct pollfd *p = &pfdp[i];
501
502       if (p->fd != -1 && p->revents)
503         {
504           /* fd has input waiting */
505           ++fds_found;
506
507           if (p->revents & POLLNVAL)
508             xprt_unregister (xports[p->fd]);
509           else
510             svc_getreq_common (p->fd);
511         }
512     }
513 }
514 libc_hidden_proto(svc_getreq_poll)
515 libc_hidden_def(svc_getreq_poll)
516
517 #ifdef __UCLIBC_HAS_THREADS__
518
519 void attribute_hidden __rpc_thread_svc_cleanup (void)
520 {
521   struct svc_callout *svcp;
522
523   while ((svcp = svc_head) != NULL)
524     svc_unregister (svcp->sc_prog, svcp->sc_vers);
525 }
526
527 #endif /* __UCLIBC_HAS_THREADS__ */