do not rely on libc providing TEMP_FAILURE_RETRY
[project/librpc-uclibc.git] / 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 #include <features.h>
41
42 #include <errno.h>
43 #include <unistd.h>
44 #include "rpc_private.h"
45 #include <rpc/svc.h>
46 #include <rpc/pmap_clnt.h>
47 #include <poll.h>
48
49 /* used by svc_[max_]pollfd */
50 /* used by svc_fdset */
51
52 #ifdef __UCLIBC_HAS_THREADS__
53 #define xports (*(SVCXPRT ***)&RPC_THREAD_VARIABLE(svc_xports_s))
54 #else
55 static SVCXPRT **xports;
56 #endif
57
58 #define NULL_SVC ((struct svc_callout *)0)
59 #define RQCRED_SIZE     400     /* this size is excessive */
60
61 /* The services list
62    Each entry represents a set of procedures (an rpc program).
63    The dispatch routine takes request structs and runs the
64    appropriate procedure. */
65 struct svc_callout {
66   struct svc_callout *sc_next;
67   rpcprog_t sc_prog;
68   rpcvers_t sc_vers;
69   void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
70 };
71 #ifdef __UCLIBC_HAS_THREADS__
72 #define svc_head (*(struct svc_callout **)&RPC_THREAD_VARIABLE(svc_head_s))
73 #else
74 static struct svc_callout *svc_head;
75 #endif
76
77 /* ***************  SVCXPRT related stuff **************** */
78
79 /* Activate a transport handle. */
80 void
81 xprt_register (SVCXPRT *xprt)
82 {
83   register int sock = xprt->xp_sock;
84   register int i;
85
86   if (xports == NULL)
87     {
88       xports = (SVCXPRT **) malloc (_rpc_dtablesize () * sizeof (SVCXPRT *));
89       if (xports == NULL) /* DonĀ“t add handle */
90         return;
91     }
92
93   if (sock < _rpc_dtablesize ())
94     {
95       xports[sock] = xprt;
96       if (sock < FD_SETSIZE)
97         FD_SET (sock, &svc_fdset);
98
99       /* Check if we have an empty slot */
100       for (i = 0; i < svc_max_pollfd; ++i)
101         if (svc_pollfd[i].fd == -1)
102           {
103             svc_pollfd[i].fd = sock;
104             svc_pollfd[i].events = (POLLIN | POLLPRI |
105                                     POLLRDNORM | POLLRDBAND);
106             return;
107           }
108
109       ++svc_max_pollfd;
110       svc_pollfd = realloc (svc_pollfd,
111                             sizeof (struct pollfd) * svc_max_pollfd);
112       if (svc_pollfd == NULL) /* Out of memory */
113         return;
114
115       svc_pollfd[svc_max_pollfd - 1].fd = sock;
116       svc_pollfd[svc_max_pollfd - 1].events = (POLLIN | POLLPRI |
117                                                POLLRDNORM | POLLRDBAND);
118     }
119 }
120 libc_hidden_def(xprt_register)
121
122 /* De-activate a transport handle. */
123 void
124 xprt_unregister (SVCXPRT *xprt)
125 {
126   register int sock = xprt->xp_sock;
127   register int i;
128
129   if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
130     {
131       xports[sock] = (SVCXPRT *) 0;
132
133       if (sock < FD_SETSIZE)
134         FD_CLR (sock, &svc_fdset);
135
136       for (i = 0; i < svc_max_pollfd; ++i)
137         if (svc_pollfd[i].fd == sock)
138           svc_pollfd[i].fd = -1;
139     }
140 }
141 libc_hidden_def(xprt_unregister)
142
143
144 /* ********************** CALLOUT list related stuff ************* */
145
146 /* Search the callout list for a program number, return the callout
147    struct. */
148 static struct svc_callout *
149 svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
150 {
151   register struct svc_callout *s, *p;
152
153   p = NULL_SVC;
154   for (s = svc_head; s != NULL_SVC; s = s->sc_next)
155     {
156       if ((s->sc_prog == prog) && (s->sc_vers == vers))
157         goto done;
158       p = s;
159     }
160 done:
161   *prev = p;
162   return s;
163 }
164
165 /* Add a service program to the callout list.
166    The dispatch routine will be called when a rpc request for this
167    program number comes in. */
168 bool_t
169 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
170               void (*dispatch) (struct svc_req *, SVCXPRT *),
171               rpcproc_t protocol)
172 {
173   struct svc_callout *prev;
174   register struct svc_callout *s;
175
176   if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
177     {
178       if (s->sc_dispatch == dispatch)
179         goto pmap_it;           /* he is registering another xptr */
180       return FALSE;
181     }
182   s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
183   if (s == (struct svc_callout *) 0)
184     return FALSE;
185
186   s->sc_prog = prog;
187   s->sc_vers = vers;
188   s->sc_dispatch = dispatch;
189   s->sc_next = svc_head;
190   svc_head = s;
191
192 pmap_it:
193   /* now register the information with the local binder service */
194   if (protocol)
195     return pmap_set (prog, vers, protocol, xprt->xp_port);
196
197   return TRUE;
198 }
199 libc_hidden_def(svc_register)
200
201 /* Remove a service program from the callout list. */
202 void
203 svc_unregister (rpcprog_t prog, rpcvers_t vers)
204 {
205   struct svc_callout *prev;
206   register struct svc_callout *s;
207
208   if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
209     return;
210
211   if (prev == NULL_SVC)
212     svc_head = s->sc_next;
213   else
214     prev->sc_next = s->sc_next;
215
216   s->sc_next = NULL_SVC;
217   mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
218   /* now unregister the information with the local binder service */
219   pmap_unset (prog, vers);
220 }
221 libc_hidden_def(svc_unregister)
222
223 /* ******************* REPLY GENERATION ROUTINES  ************ */
224
225 /* Send a reply to an rpc request */
226 bool_t
227 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
228                caddr_t xdr_location)
229 {
230   struct rpc_msg rply;
231
232   rply.rm_direction = REPLY;
233   rply.rm_reply.rp_stat = MSG_ACCEPTED;
234   rply.acpted_rply.ar_verf = xprt->xp_verf;
235   rply.acpted_rply.ar_stat = SUCCESS;
236   rply.acpted_rply.ar_results.where = xdr_location;
237   rply.acpted_rply.ar_results.proc = xdr_results;
238   return SVC_REPLY (xprt, &rply);
239 }
240 libc_hidden_def(svc_sendreply)
241
242 /* No procedure error reply */
243 void
244 svcerr_noproc (register SVCXPRT *xprt)
245 {
246   struct rpc_msg rply;
247
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 = PROC_UNAVAIL;
252   SVC_REPLY (xprt, &rply);
253 }
254
255 /* Can't decode args error reply */
256 void
257 svcerr_decode (register SVCXPRT *xprt)
258 {
259   struct rpc_msg rply;
260
261   rply.rm_direction = REPLY;
262   rply.rm_reply.rp_stat = MSG_ACCEPTED;
263   rply.acpted_rply.ar_verf = xprt->xp_verf;
264   rply.acpted_rply.ar_stat = GARBAGE_ARGS;
265   SVC_REPLY (xprt, &rply);
266 }
267 libc_hidden_def(svcerr_decode)
268
269 /* Some system error */
270 void
271 svcerr_systemerr (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 = SYSTEM_ERR;
279   SVC_REPLY (xprt, &rply);
280 }
281
282 /* Authentication error reply */
283 void
284 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
285 {
286   struct rpc_msg rply;
287
288   rply.rm_direction = REPLY;
289   rply.rm_reply.rp_stat = MSG_DENIED;
290   rply.rjcted_rply.rj_stat = AUTH_ERROR;
291   rply.rjcted_rply.rj_why = why;
292   SVC_REPLY (xprt, &rply);
293 }
294 libc_hidden_def(svcerr_auth)
295
296 /* Auth too weak error reply */
297 void
298 svcerr_weakauth (SVCXPRT *xprt)
299 {
300   svcerr_auth (xprt, AUTH_TOOWEAK);
301 }
302
303 /* Program unavailable error reply */
304 void
305 svcerr_noprog (register SVCXPRT *xprt)
306 {
307   struct rpc_msg rply;
308
309   rply.rm_direction = REPLY;
310   rply.rm_reply.rp_stat = MSG_ACCEPTED;
311   rply.acpted_rply.ar_verf = xprt->xp_verf;
312   rply.acpted_rply.ar_stat = PROG_UNAVAIL;
313   SVC_REPLY (xprt, &rply);
314 }
315 libc_hidden_def(svcerr_noprog)
316
317 /* Program version mismatch error reply */
318 void
319 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
320                  rpcvers_t high_vers)
321 {
322   struct rpc_msg rply;
323
324   rply.rm_direction = REPLY;
325   rply.rm_reply.rp_stat = MSG_ACCEPTED;
326   rply.acpted_rply.ar_verf = xprt->xp_verf;
327   rply.acpted_rply.ar_stat = PROG_MISMATCH;
328   rply.acpted_rply.ar_vers.low = low_vers;
329   rply.acpted_rply.ar_vers.high = high_vers;
330   SVC_REPLY (xprt, &rply);
331 }
332 libc_hidden_def(svcerr_progvers)
333
334 /* ******************* SERVER INPUT STUFF ******************* */
335
336 /*
337  * Get server side input from some transport.
338  *
339  * Statement of authentication parameters management:
340  * This function owns and manages all authentication parameters, specifically
341  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
342  * the "cooked" credentials (rqst->rq_clntcred).
343  * However, this function does not know the structure of the cooked
344  * credentials, so it make the following assumptions:
345  *   a) the structure is contiguous (no pointers), and
346  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
347  * In all events, all three parameters are freed upon exit from this routine.
348  * The storage is trivially management on the call stack in user land, but
349  * is mallocated in kernel land.
350  */
351
352 void
353 svc_getreq_common (const int fd)
354 {
355   enum xprt_stat stat;
356   struct rpc_msg msg;
357   register SVCXPRT *xprt;
358   char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
359   msg.rm_call.cb_cred.oa_base = cred_area;
360   msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
361
362   xprt = xports[fd];
363   /* Do we control fd? */
364   if (xprt == NULL)
365      return;
366
367   /* now receive msgs from xprtprt (support batch calls) */
368   do
369     {
370       if (SVC_RECV (xprt, &msg))
371         {
372           /* now find the exported program and call it */
373           struct svc_callout *s;
374           struct svc_req r;
375           enum auth_stat why;
376           rpcvers_t low_vers;
377           rpcvers_t high_vers;
378           int prog_found;
379
380           r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
381           r.rq_xprt = xprt;
382           r.rq_prog = msg.rm_call.cb_prog;
383           r.rq_vers = msg.rm_call.cb_vers;
384           r.rq_proc = msg.rm_call.cb_proc;
385           r.rq_cred = msg.rm_call.cb_cred;
386
387           /* first authenticate the message */
388           /* Check for null flavor and bypass these calls if possible */
389
390           if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
391             {
392               r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
393               r.rq_xprt->xp_verf.oa_length = 0;
394             }
395           else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
396             {
397               svcerr_auth (xprt, why);
398               goto call_done;
399             }
400
401           /* now match message with a registered service */
402           prog_found = FALSE;
403           low_vers = 0 - 1;
404           high_vers = 0;
405
406           for (s = svc_head; s != NULL_SVC; s = s->sc_next)
407             {
408               if (s->sc_prog == r.rq_prog)
409                 {
410                   if (s->sc_vers == r.rq_vers)
411                     {
412                       (*s->sc_dispatch) (&r, xprt);
413                       goto call_done;
414                     }
415                   /* found correct version */
416                   prog_found = TRUE;
417                   if (s->sc_vers < low_vers)
418                     low_vers = s->sc_vers;
419                   if (s->sc_vers > high_vers)
420                     high_vers = s->sc_vers;
421                 }
422               /* found correct program */
423             }
424           /* if we got here, the program or version
425              is not served ... */
426           if (prog_found)
427             svcerr_progvers (xprt, low_vers, high_vers);
428           else
429             svcerr_noprog (xprt);
430           /* Fall through to ... */
431         }
432     call_done:
433       if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
434         {
435           SVC_DESTROY (xprt);
436           break;
437         }
438     }
439   while (stat == XPRT_MOREREQS);
440 }
441 libc_hidden_def(svc_getreq_common)
442
443 void
444 svc_getreqset (fd_set *readfds)
445 {
446   register u_int32_t mask;
447   register u_int32_t *maskp;
448   register int setsize;
449   register int sock;
450   register int bit;
451
452   setsize = _rpc_dtablesize ();
453   maskp = (u_int32_t *) readfds->fds_bits;
454   for (sock = 0; sock < setsize; sock += 32)
455     for (mask = *maskp++; (bit = ffs (mask)); mask ^= (1 << (bit - 1)))
456       svc_getreq_common (sock + bit - 1);
457 }
458 libc_hidden_def(svc_getreqset)
459
460 void
461 svc_getreq (int rdfds)
462 {
463   fd_set readfds;
464
465   FD_ZERO (&readfds);
466   readfds.fds_bits[0] = rdfds;
467   svc_getreqset (&readfds);
468 }
469 libc_hidden_def(svc_getreq)
470
471 void
472 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
473 {
474   register int i;
475   register int fds_found;
476
477   for (i = fds_found = 0; i < svc_max_pollfd && fds_found < pollretval; ++i)
478     {
479       register struct pollfd *p = &pfdp[i];
480
481       if (p->fd != -1 && p->revents)
482         {
483           /* fd has input waiting */
484           ++fds_found;
485
486           if (p->revents & POLLNVAL)
487             xprt_unregister (xports[p->fd]);
488           else
489             svc_getreq_common (p->fd);
490         }
491     }
492 }
493 libc_hidden_def(svc_getreq_poll)
494
495 #ifdef __UCLIBC_HAS_THREADS__
496
497 void attribute_hidden __rpc_thread_svc_cleanup (void)
498 {
499   struct svc_callout *svcp;
500
501   while ((svcp = svc_head) != NULL)
502     svc_unregister (svcp->sc_prog, svcp->sc_vers);
503 }
504
505 #endif /* __UCLIBC_HAS_THREADS__ */