source: rtems-libbsd/services/librpc/src/rpc/svc.c @ e599318

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e599318 was e599318, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:52:54

Update files to match FreeBSD layout

Add compatibility with Newlib header files. Some FreeBSD header files
are mapped by the translation script:

o rtems/bsd/sys/_types.h
o rtems/bsd/sys/errno.h
o rtems/bsd/sys/lock.h
o rtems/bsd/sys/param.h
o rtems/bsd/sys/resource.h
o rtems/bsd/sys/time.h
o rtems/bsd/sys/timespec.h
o rtems/bsd/sys/types.h
o rtems/bsd/sys/unistd.h

It is now possible to include <sys/socket.h> directly for example.

Generate one Makefile which builds everything including tests.

  • Property mode set to 100644
File size: 11.7 KB
Line 
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#if defined(LIBC_SCCS) && !defined(lint)
31/*static char *sccsid = "from: @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";*/
32/*static char *sccsid = "from: @(#)svc.c        2.4 88/08/11 4.0 RPCSRC";*/
33static char *rcsid = "$FreeBSD: src/lib/libc/rpc/svc.c,v 1.14 1999/08/28 00:00:48 peter Exp $";
34#endif
35
36/*
37 * svc.c, Server-side remote procedure call interface.
38 *
39 * There are two sets of procedures here.  The xprt routines are
40 * for handling transport handles.  The svc routines handle the
41 * list of service routines.
42 *
43 * Copyright (C) 1984, Sun Microsystems, Inc.
44 */
45
46#ifdef HAVE_CONFIG_H
47#include "config.h"
48#endif
49
50#include <string.h>
51#ifdef HAVE_STRINGS_H
52#include <strings.h> /* for ffs */
53#endif
54#include <stdlib.h>
55#include <sys/errno.h>
56#include <rpc/rpc.h>
57#include <rpc/pmap_clnt.h>
58#ifdef __rtems__
59        /* XXX in rpc.h in old .. not new */
60        #include <rpc/rpc_rtems.h>
61#endif
62
63#define xports (rtems_rpc_task_variables->svc_xports)
64#define xportssize (rtems_rpc_task_variables->svc_xportssize)
65
66#define NULL_SVC ((struct svc_callout *)0)
67#define RQCRED_SIZE     400             /* this size is excessive */
68
69#define max(a, b) (a > b ? a : b)
70
71/*
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 * apropriate procedure.
76 */
77struct svc_callout {
78        struct svc_callout *sc_next;
79        u_long              sc_prog;
80        u_long              sc_vers;
81        void                (*sc_dispatch)(struct svc_req *r, SVCXPRT *xprt);
82};
83#define svc_head (rtems_rpc_task_variables->svc_svc_head)
84
85static struct svc_callout *svc_find(u_long prog, u_long vers,
86  struct svc_callout **prev);
87
88/* ***************  SVCXPRT related stuff **************** */
89
90/*
91 * Activate a transport handle.
92 */
93void
94xprt_register(
95        SVCXPRT *xprt )
96{
97        register int sock = xprt->xp_fd;
98
99        if (sock + 1 > __svc_fdsetsize) {
100                int bytes = sizeof (fd_set);
101                fd_set *fds;
102
103                fds = (fd_set *)malloc(bytes);
104                memset(fds, 0, bytes);
105                if (__svc_fdset) {
106                        memcpy(fds, __svc_fdset, bytes);
107                        free(__svc_fdset);
108                }
109                __svc_fdset = fds;
110                __svc_fdsetsize = bytes * NBBY;
111        }
112
113        if (sock < FD_SETSIZE)
114                FD_SET(sock, &svc_fdset);
115        FD_SET(sock, __svc_fdset);
116
117        if (xports == NULL || sock + 1 > xportssize) {
118                SVCXPRT **xp;
119                int size = FD_SETSIZE;
120
121                if (sock + 1 > size)
122                        size = sock + 1;
123                xp = (SVCXPRT **)mem_alloc(size * sizeof(SVCXPRT *));
124                memset(xp, 0, size * sizeof(SVCXPRT *));
125                if (xports) {
126                        memcpy(xp, xports, xportssize * sizeof(SVCXPRT *));
127                        free(xports);
128                }
129                xportssize = size;
130                xports = xp;
131        }
132        xports[sock] = xprt;
133        svc_maxfd = max(svc_maxfd, sock);
134}
135
136/*
137 * De-activate a transport handle.
138 */
139void
140xprt_unregister(
141        SVCXPRT *xprt )
142{
143        register int sock = xprt->xp_fd;
144
145        if (xports[sock] == xprt) {
146                xports[sock] = (SVCXPRT *)0;
147                if (sock < FD_SETSIZE)
148                        FD_CLR(sock, &svc_fdset);
149                FD_CLR(sock, __svc_fdset);
150                if (sock == svc_maxfd) {
151                        for (svc_maxfd--; svc_maxfd >= 0; svc_maxfd--)
152                                if (xports[svc_maxfd])
153                                        break;
154                }
155                /*
156                 * XXX could use svc_maxfd as a hint to
157                 * decrease the size of __svc_fdset
158                 */
159        }
160}
161
162
163/* ********************** CALLOUT list related stuff ************* */
164
165/*
166 * Add a service program to the callout list.
167 * The dispatch routine will be called when a rpc request for this
168 * program number comes in.
169 */
170bool_t
171svc_register(
172        SVCXPRT *xprt,
173        u_long prog,
174        u_long vers,
175        void (*dispatch)(struct svc_req *r, SVCXPRT *xprt),
176        int protocol )
177{
178        struct svc_callout *prev;
179        register struct svc_callout *s;
180
181        if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
182                if (s->sc_dispatch == dispatch)
183                        goto pmap_it;  /* he is registering another xptr */
184                return (FALSE);
185        }
186        s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
187        if (s == (struct svc_callout *)0) {
188                return (FALSE);
189        }
190        s->sc_prog = prog;
191        s->sc_vers = vers;
192        s->sc_dispatch = dispatch;
193        s->sc_next = svc_head;
194        svc_head = s;
195pmap_it:
196        /* now register the information with the local binder service */
197        if (protocol) {
198                return (pmap_set(prog, vers, protocol, xprt->xp_port));
199        }
200        return (TRUE);
201}
202
203/*
204 * Remove a service program from the callout list.
205 */
206void
207svc_unregister(
208        u_long prog,
209        u_long vers )
210{
211        struct svc_callout *prev;
212        register struct svc_callout *s;
213
214        if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
215                return;
216        if (prev == NULL_SVC) {
217                svc_head = s->sc_next;
218        } else {
219                prev->sc_next = s->sc_next;
220        }
221        s->sc_next = NULL_SVC;
222        mem_free((char *) s, (u_int) sizeof(struct svc_callout));
223        /* now unregister the information with the local binder service */
224        (void)pmap_unset(prog, vers);
225}
226
227/*
228 * Search the callout list for a program number, return the callout
229 * struct.
230 */
231static struct svc_callout *
232svc_find(
233        u_long prog,
234        u_long vers,
235        struct svc_callout **prev )
236{
237        register struct svc_callout *s, *p;
238
239        p = NULL_SVC;
240        for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
241                if ((s->sc_prog == prog) && (s->sc_vers == vers))
242                        goto done;
243                p = s;
244        }
245done:
246        *prev = p;
247        return (s);
248}
249
250/* ******************* REPLY GENERATION ROUTINES  ************ */
251
252/*
253 * Send a reply to an rpc request
254 */
255bool_t
256svc_sendreply(
257        register SVCXPRT *xprt,
258        xdrproc_t xdr_results,
259        void *xdr_location )
260{
261        struct rpc_msg rply;
262
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 = SUCCESS;
267        rply.acpted_rply.ar_results.where = xdr_location;
268        rply.acpted_rply.ar_results.proc = xdr_results;
269        return (SVC_REPLY(xprt, &rply));
270}
271
272/*
273 * No procedure error reply
274 */
275void
276svcerr_noproc(
277        register SVCXPRT *xprt )
278{
279        struct rpc_msg rply;
280
281        rply.rm_direction = REPLY;
282        rply.rm_reply.rp_stat = MSG_ACCEPTED;
283        rply.acpted_rply.ar_verf = xprt->xp_verf;
284        rply.acpted_rply.ar_stat = PROC_UNAVAIL;
285        SVC_REPLY(xprt, &rply);
286}
287
288/*
289 * Can't decode args error reply
290 */
291void
292svcerr_decode(
293        register SVCXPRT *xprt )
294{
295        struct rpc_msg rply;
296
297        rply.rm_direction = REPLY;
298        rply.rm_reply.rp_stat = MSG_ACCEPTED;
299        rply.acpted_rply.ar_verf = xprt->xp_verf;
300        rply.acpted_rply.ar_stat = GARBAGE_ARGS;
301        SVC_REPLY(xprt, &rply);
302}
303
304/*
305 * Some system error
306 */
307void
308svcerr_systemerr(
309        register SVCXPRT *xprt )
310{
311        struct rpc_msg rply;
312
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);
318}
319
320/*
321 * Authentication error reply
322 */
323void
324svcerr_auth(
325        SVCXPRT *xprt,
326        enum auth_stat why )
327{
328        struct rpc_msg rply;
329
330        rply.rm_direction = REPLY;
331        rply.rm_reply.rp_stat = MSG_DENIED;
332        rply.rjcted_rply.rj_stat = AUTH_ERROR;
333        rply.rjcted_rply.rj_why = why;
334        SVC_REPLY(xprt, &rply);
335}
336
337/*
338 * Auth too weak error reply
339 */
340void
341svcerr_weakauth(
342        SVCXPRT *xprt )
343{
344
345        svcerr_auth(xprt, AUTH_TOOWEAK);
346}
347
348/*
349 * Program unavailable error reply
350 */
351void
352svcerr_noprog(
353        register SVCXPRT *xprt )
354{
355        struct rpc_msg rply;
356
357        rply.rm_direction = REPLY;
358        rply.rm_reply.rp_stat = MSG_ACCEPTED;
359        rply.acpted_rply.ar_verf = xprt->xp_verf;
360        rply.acpted_rply.ar_stat = PROG_UNAVAIL;
361        SVC_REPLY(xprt, &rply);
362}
363
364/*
365 * Program version mismatch error reply
366 */
367void
368svcerr_progvers(
369        register SVCXPRT *xprt,
370        rpcvers_t low_vers,
371        rpcvers_t high_vers )
372{
373        struct rpc_msg rply;
374
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);
382}
383
384/* ******************* SERVER INPUT STUFF ******************* */
385
386/*
387 * Get server side input from some transport.
388 *
389 * Statement of authentication parameters management:
390 * This function owns and manages all authentication parameters, specifically
391 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
392 * the "cooked" credentials (rqst->rq_clntcred).
393 * However, this function does not know the structure of the cooked
394 * credentials, so it make the following assumptions:
395 *   a) the structure is contiguous (no pointers), and
396 *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
397 * In all events, all three parameters are freed upon exit from this routine.
398 * The storage is trivially management on the call stack in user land, but
399 * is mallocated in kernel land.
400 */
401
402void
403svc_getreq(
404        int rdfds )
405{
406        fd_set readfds;
407
408        FD_ZERO(&readfds);
409        readfds.fds_bits[0] = rdfds;
410        svc_getreqset(&readfds);
411}
412
413void
414svc_getreqset(
415        fd_set *readfds )
416{
417        svc_getreqset2(readfds, FD_SETSIZE);
418}
419
420void
421svc_getreqset2(
422        fd_set *readfds,
423        int width )
424{
425        enum xprt_stat stat;
426        struct rpc_msg msg;
427        int prog_found;
428        u_long low_vers;
429        u_long high_vers;
430        struct svc_req r;
431        register SVCXPRT *xprt;
432        register int bit;
433        register int sock;
434        register fd_mask mask, *maskp;
435        char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
436        msg.rm_call.cb_cred.oa_base = cred_area;
437        msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
438        r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
439
440
441        maskp = readfds->fds_bits;
442        for (sock = 0; sock < width; sock += NFDBITS) {
443            for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
444                /* sock has input waiting */
445                xprt = xports[sock + bit - 1];
446                if (xprt == NULL)
447                        /* But do we control sock? */
448                        continue;
449                /* now receive msgs from xprtprt (support batch calls) */
450                do {
451                        if (SVC_RECV(xprt, &msg)) {
452
453                                /* now find the exported program and call it */
454                                register struct svc_callout *s;
455                                enum auth_stat why;
456
457                                r.rq_xprt = xprt;
458                                r.rq_prog = msg.rm_call.cb_prog;
459                                r.rq_vers = msg.rm_call.cb_vers;
460                                r.rq_proc = msg.rm_call.cb_proc;
461                                r.rq_cred = msg.rm_call.cb_cred;
462                                /* first authenticate the message */
463                                if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
464                                        svcerr_auth(xprt, why);
465                                        goto call_done;
466                                }
467                                /* now match message with a registered service*/
468                                prog_found = FALSE;
469                                low_vers = (u_long) - 1;
470                                high_vers = 0;
471                                for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
472                                        if (s->sc_prog == r.rq_prog) {
473                                                if (s->sc_vers == r.rq_vers) {
474                                                        (*s->sc_dispatch)(&r, xprt);
475                                                        goto call_done;
476                                                }  /* found correct version */
477                                                prog_found = TRUE;
478                                                if (s->sc_vers < low_vers)
479                                                        low_vers = s->sc_vers;
480                                                if (s->sc_vers > high_vers)
481                                                        high_vers = s->sc_vers;
482                                        }   /* found correct program */
483                                }
484                                /*
485                                 * if we got here, the program or version
486                                 * is not served ...
487                                 */
488                                if (prog_found)
489                                        svcerr_progvers(xprt,
490                                        low_vers, high_vers);
491                                else
492                                         svcerr_noprog(xprt);
493                                /* Fall through to ... */
494                        }
495                call_done:
496                        if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
497                                SVC_DESTROY(xprt);
498                                break;
499                        }
500                } while (stat == XPRT_MOREREQS);
501            }
502        }
503}
Note: See TracBrowser for help on using the repository browser.