Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / svc_simple.c
1 #ifndef lint
2 static char sfs_svc_simple_id[] = "@(#)svc_simple.c     2.1     97/10/23";
3 #endif
4 /* @(#)svc_simple.c     2.2 88/08/01 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /* 
56  * svc_simple.c
57  * Simplified front end to rpc.
58  *
59  * Copyright (C) 1984, Sun Microsystems, Inc.
60  */
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65 #include <string.h>
66 #include "rpc/rpc.h"
67 #include "rpc/osdep.h"
68 #include <netdb.h>
69
70 static struct proglst {
71         char *(*p_progname)();
72         int  p_prognum;
73         int  p_procnum;
74         xdrproc_t p_inproc, p_outproc;
75         struct proglst *p_nxt;
76 } *proglst;
77 static void universal(struct svc_req *, SVCXPRT *);
78 static SVCXPRT *transp;
79
80 registerrpc(
81         int prognum,
82         int versnum,
83         int procnum,
84         char *(*progname)(),
85         xdrproc_t inproc,
86         xdrproc_t outproc)
87 {
88         struct proglst *pl;
89         
90         if (procnum == NULLPROC) {
91                 (void) fprintf(stderr,
92                     "can't reassign procedure number %d\n", NULLPROC);
93                 return (-1);
94         }
95         if (transp == 0) {
96                 transp = svcudp_create(RPC_ANYSOCK);
97                 if (transp == NULL) {
98                         (void) fprintf(stderr, "couldn't create an rpc server\n");
99                         return (-1);
100                 }
101         }
102         (void) pmap_unset((uint32_t)prognum, (uint32_t)versnum);
103         if (!svc_register(transp, (uint32_t)prognum, (uint32_t)versnum, 
104             universal, IPPROTO_UDP)) {
105                 (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
106                     prognum, versnum);
107                 return (-1);
108         }
109         pl = (struct proglst *)malloc(sizeof(struct proglst));
110         if (pl == NULL) {
111                 (void) fprintf(stderr, "registerrpc: out of memory\n");
112                 return (-1);
113         }
114         pl->p_progname = progname;
115         pl->p_prognum = prognum;
116         pl->p_procnum = procnum;
117         pl->p_inproc = inproc;
118         pl->p_outproc = outproc;
119         pl->p_nxt = proglst;
120         proglst = pl;
121         return (0);
122 }
123
124 static void
125 universal(
126         struct svc_req *rqstp,
127         SVCXPRT *transp)
128 {
129         int prog, proc;
130         char *outdata;
131         char xdrbuf[UDPMSGSIZE];
132         struct proglst *pl;
133
134         /* 
135          * enforce "procnum 0 is echo" convention
136          */
137         if (rqstp->rq_proc == NULLPROC) {
138                 if (svc_sendreply(transp, xdr_void, (char *)NULL) == FALSE) {
139                         (void) fprintf(stderr, "xxx\n");
140                         exit(1);
141                 }
142                 return;
143         }
144         prog = rqstp->rq_prog;
145         proc = rqstp->rq_proc;
146         for (pl = proglst; pl != NULL; pl = pl->p_nxt)
147                 if (pl->p_prognum == prog && pl->p_procnum == proc) {
148                         /* decode arguments into a CLEAN buffer */
149                         memset(xdrbuf, '\0', sizeof(xdrbuf)); /* required ! */
150                         if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
151                                 svcerr_decode(transp);
152                                 return;
153                         }
154                         outdata = (*(pl->p_progname))(xdrbuf);
155                         if (outdata == NULL && pl->p_outproc != xdr_void)
156                                 /* there was an error */
157                                 return;
158                         if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
159                                 (void) fprintf(stderr,
160                                     "trouble replying to prog %d\n",
161                                     pl->p_prognum);
162                                 exit(1);
163                         }
164                         /* free the decoded arguments */
165                         (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
166                         return;
167                 }
168         (void) fprintf(stderr, "never registered prog %d\n", prog);
169         exit(1);
170 }
171