* sysdeps/m68k/setjmp.c: Also define setjmp and _setjmp if
[glibc.git] / sunrpc / svc.c
blob6357d51354b4a022c2b5d802362d5e81759fa6a3
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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
30 * svc.c, Server-side remote procedure call interface.
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.
36 * Copyright (C) 1984, Sun Microsystems, Inc.
39 #include <errno.h>
40 #include <unistd.h>
41 #include <rpc/rpc.h>
42 #include <rpc/svc.h>
43 #include <rpc/pmap_clnt.h>
44 #include <sys/poll.h>
46 #ifdef _RPC_THREAD_SAFE_
47 #define xports ((SVCXPRT **)RPC_THREAD_VARIABLE(svc_xports_s))
48 #else
49 static SVCXPRT **xports;
50 #endif
52 #define NULL_SVC ((struct svc_callout *)0)
53 #define RQCRED_SIZE 400 /* this size is excessive */
55 /* The services list
56 Each entry represents a set of procedures (an rpc program).
57 The dispatch routine takes request structs and runs the
58 appropriate procedure. */
59 struct svc_callout {
60 struct svc_callout *sc_next;
61 rpcprog_t sc_prog;
62 rpcvers_t sc_vers;
63 void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
65 #ifdef _RPC_THREAD_SAFE_
66 #define svc_head ((struct svc_callout *)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 xports[sock] = xprt;
90 if (sock < FD_SETSIZE)
91 FD_SET (sock, &svc_fdset);
93 /* Check if we have an empty slot */
94 for (i = 0; i < svc_max_pollfd; ++i)
95 if (svc_pollfd[i].fd == -1)
97 svc_pollfd[i].fd = sock;
98 svc_pollfd[i].events = (POLLIN | POLLPRI |
99 POLLRDNORM | POLLRDBAND);
100 return;
103 ++svc_max_pollfd;
104 svc_pollfd = realloc (svc_pollfd,
105 sizeof (struct pollfd) * svc_max_pollfd);
106 if (svc_pollfd == NULL) /* Out of memory */
107 return;
109 svc_pollfd[svc_max_pollfd - 1].fd = sock;
110 svc_pollfd[svc_max_pollfd - 1].events = (POLLIN | POLLPRI |
111 POLLRDNORM | POLLRDBAND);
115 /* De-activate a transport handle. */
116 void
117 xprt_unregister (SVCXPRT *xprt)
119 register int sock = xprt->xp_sock;
120 register int i;
122 if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
124 xports[sock] = (SVCXPRT *) 0;
126 if (sock < FD_SETSIZE)
127 FD_CLR (sock, &svc_fdset);
129 for (i = 0; i < svc_max_pollfd; ++i)
130 if (svc_pollfd[i].fd == sock)
131 svc_pollfd[i].fd = -1;
136 /* ********************** CALLOUT list related stuff ************* */
138 /* Search the callout list for a program number, return the callout
139 struct. */
140 static struct svc_callout *
141 svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
143 register struct svc_callout *s, *p;
145 p = NULL_SVC;
146 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
148 if ((s->sc_prog == prog) && (s->sc_vers == vers))
149 goto done;
150 p = s;
152 done:
153 *prev = p;
154 return s;
157 /* Add a service program to the callout list.
158 The dispatch routine will be called when a rpc request for this
159 program number comes in. */
160 bool_t
161 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
162 void (*dispatch) (struct svc_req *, SVCXPRT *),
163 rpcproc_t protocol)
165 struct svc_callout *prev;
166 register struct svc_callout *s;
168 if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
170 if (s->sc_dispatch == dispatch)
171 goto pmap_it; /* he is registering another xptr */
172 return FALSE;
174 s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
175 if (s == (struct svc_callout *) 0)
176 return FALSE;
178 s->sc_prog = prog;
179 s->sc_vers = vers;
180 s->sc_dispatch = dispatch;
181 s->sc_next = svc_head;
182 svc_head = s;
184 pmap_it:
185 /* now register the information with the local binder service */
186 if (protocol)
187 return pmap_set (prog, vers, protocol, xprt->xp_port);
189 return TRUE;
192 /* Remove a service program from the callout list. */
193 void
194 svc_unregister (rpcprog_t prog, rpcvers_t vers)
196 struct svc_callout *prev;
197 register struct svc_callout *s;
199 if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
200 return;
202 if (prev == NULL_SVC)
203 svc_head = s->sc_next;
204 else
205 prev->sc_next = s->sc_next;
207 s->sc_next = NULL_SVC;
208 mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
209 /* now unregister the information with the local binder service */
210 pmap_unset (prog, vers);
213 /* ******************* REPLY GENERATION ROUTINES ************ */
215 /* Send a reply to an rpc request */
216 bool_t
217 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
218 caddr_t xdr_location)
220 struct rpc_msg rply;
222 rply.rm_direction = REPLY;
223 rply.rm_reply.rp_stat = MSG_ACCEPTED;
224 rply.acpted_rply.ar_verf = xprt->xp_verf;
225 rply.acpted_rply.ar_stat = SUCCESS;
226 rply.acpted_rply.ar_results.where = xdr_location;
227 rply.acpted_rply.ar_results.proc = xdr_results;
228 return SVC_REPLY (xprt, &rply);
231 /* No procedure error reply */
232 void
233 svcerr_noproc (register SVCXPRT *xprt)
235 struct rpc_msg rply;
237 rply.rm_direction = REPLY;
238 rply.rm_reply.rp_stat = MSG_ACCEPTED;
239 rply.acpted_rply.ar_verf = xprt->xp_verf;
240 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
241 SVC_REPLY (xprt, &rply);
244 /* Can't decode args error reply */
245 void
246 svcerr_decode (register SVCXPRT *xprt)
248 struct rpc_msg rply;
250 rply.rm_direction = REPLY;
251 rply.rm_reply.rp_stat = MSG_ACCEPTED;
252 rply.acpted_rply.ar_verf = xprt->xp_verf;
253 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
254 SVC_REPLY (xprt, &rply);
257 /* Some system error */
258 void
259 svcerr_systemerr (register SVCXPRT *xprt)
261 struct rpc_msg rply;
263 rply.rm_direction = REPLY;
264 rply.rm_reply.rp_stat = MSG_ACCEPTED;
265 rply.acpted_rply.ar_verf = xprt->xp_verf;
266 rply.acpted_rply.ar_stat = SYSTEM_ERR;
267 SVC_REPLY (xprt, &rply);
270 /* Authentication error reply */
271 void
272 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
274 struct rpc_msg rply;
276 rply.rm_direction = REPLY;
277 rply.rm_reply.rp_stat = MSG_DENIED;
278 rply.rjcted_rply.rj_stat = AUTH_ERROR;
279 rply.rjcted_rply.rj_why = why;
280 SVC_REPLY (xprt, &rply);
283 /* Auth too weak error reply */
284 void
285 svcerr_weakauth (SVCXPRT *xprt)
287 svcerr_auth (xprt, AUTH_TOOWEAK);
290 /* Program unavailable error reply */
291 void
292 svcerr_noprog (register SVCXPRT *xprt)
294 struct rpc_msg rply;
296 rply.rm_direction = REPLY;
297 rply.rm_reply.rp_stat = MSG_ACCEPTED;
298 rply.acpted_rply.ar_verf = xprt->xp_verf;
299 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
300 SVC_REPLY (xprt, &rply);
303 /* Program version mismatch error reply */
304 void
305 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
306 rpcvers_t high_vers)
308 struct rpc_msg rply;
310 rply.rm_direction = REPLY;
311 rply.rm_reply.rp_stat = MSG_ACCEPTED;
312 rply.acpted_rply.ar_verf = xprt->xp_verf;
313 rply.acpted_rply.ar_stat = PROG_MISMATCH;
314 rply.acpted_rply.ar_vers.low = low_vers;
315 rply.acpted_rply.ar_vers.high = high_vers;
316 SVC_REPLY (xprt, &rply);
319 /* ******************* SERVER INPUT STUFF ******************* */
322 * Get server side input from some transport.
324 * Statement of authentication parameters management:
325 * This function owns and manages all authentication parameters, specifically
326 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
327 * the "cooked" credentials (rqst->rq_clntcred).
328 * However, this function does not know the structure of the cooked
329 * credentials, so it make the following assumptions:
330 * a) the structure is contiguous (no pointers), and
331 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
332 * In all events, all three parameters are freed upon exit from this routine.
333 * The storage is trivially management on the call stack in user land, but
334 * is mallocated in kernel land.
337 void
338 svc_getreq (int rdfds)
340 fd_set readfds;
342 FD_ZERO (&readfds);
343 readfds.fds_bits[0] = rdfds;
344 svc_getreqset (&readfds);
347 void
348 svc_getreqset (fd_set *readfds)
350 register u_int32_t mask;
351 register u_int32_t *maskp;
352 register int setsize;
353 register int sock;
354 register int bit;
356 setsize = _rpc_dtablesize ();
357 maskp = (u_int32_t *) readfds->fds_bits;
358 for (sock = 0; sock < setsize; sock += 32)
359 for (mask = *maskp++; (bit = ffs (mask)); mask ^= (1 << (bit - 1)))
360 svc_getreq_common (sock + bit - 1);
363 void
364 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
366 register int i;
367 register int fds_found;
369 for (i = fds_found = 0; i < svc_max_pollfd && fds_found < pollretval; ++i)
371 register struct pollfd *p = &pfdp[i];
373 if (p->fd != -1 && p->revents)
375 /* fd has input waiting */
376 ++fds_found;
378 if (p->revents & POLLNVAL)
379 xprt_unregister (xports[p->fd]);
380 else
381 svc_getreq_common (p->fd);
387 void
388 svc_getreq_common (const int fd)
390 enum xprt_stat stat;
391 struct rpc_msg msg;
392 register SVCXPRT *xprt;
393 char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
394 msg.rm_call.cb_cred.oa_base = cred_area;
395 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
397 xprt = xports[fd];
398 /* Do we control fd? */
399 if (xprt == NULL)
400 return;
402 /* now receive msgs from xprtprt (support batch calls) */
405 if (SVC_RECV (xprt, &msg))
407 /* now find the exported program and call it */
408 struct svc_callout *s;
409 struct svc_req r;
410 enum auth_stat why;
411 rpcvers_t low_vers;
412 rpcvers_t high_vers;
413 int prog_found;
415 r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
416 r.rq_xprt = xprt;
417 r.rq_prog = msg.rm_call.cb_prog;
418 r.rq_vers = msg.rm_call.cb_vers;
419 r.rq_proc = msg.rm_call.cb_proc;
420 r.rq_cred = msg.rm_call.cb_cred;
422 /* first authenticate the message */
423 /* Check for null flavor and bypass these calls if possible */
425 if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
427 r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
428 r.rq_xprt->xp_verf.oa_length = 0;
430 else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
432 svcerr_auth (xprt, why);
433 goto call_done;
436 /* now match message with a registered service */
437 prog_found = FALSE;
438 low_vers = 0 - 1;
439 high_vers = 0;
441 for (s = svc_head; s != NULL_SVC; s = s->sc_next)
443 if (s->sc_prog == r.rq_prog)
445 if (s->sc_vers == r.rq_vers)
447 (*s->sc_dispatch) (&r, xprt);
448 goto call_done;
450 /* found correct version */
451 prog_found = TRUE;
452 if (s->sc_vers < low_vers)
453 low_vers = s->sc_vers;
454 if (s->sc_vers > high_vers)
455 high_vers = s->sc_vers;
457 /* found correct program */
459 /* if we got here, the program or version
460 is not served ... */
461 if (prog_found)
462 svcerr_progvers (xprt, low_vers, high_vers);
463 else
464 svcerr_noprog (xprt);
465 /* Fall through to ... */
467 call_done:
468 if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
470 SVC_DESTROY (xprt);
471 break;
474 while (stat == XPRT_MOREREQS);
477 #ifdef _RPC_THREAD_SAFE_
479 void
480 __rpc_thread_svc_cleanup (void)
482 struct svc_callout *svcp;
484 while ((svcp = svc_head) != NULL)
485 svc_unregister (svcp->sc_prog, svcp->sc_vers);
488 #endif /* _RPC_THREAD_SAFE_ */