3 * Simplified front end to rpc.
5 * Copyright (c) 2010, Oracle America, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 * * Neither the name of the "Oracle America, Inc." nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <rpc/pmap_clnt.h>
41 #include <sys/socket.h>
45 #include <libio/iolibio.h>
49 char *(*p_progname
) (char *);
52 xdrproc_t p_inproc
, p_outproc
;
53 struct proglst_
*p_nxt
;
55 #ifdef _RPC_THREAD_SAFE_
56 #define proglst RPC_THREAD_VARIABLE(svcsimple_proglst_s)
58 static struct proglst_
*proglst
;
62 static void universal (struct svc_req
*rqstp
, SVCXPRT
*transp_s
);
63 #ifdef _RPC_THREAD_SAFE_
64 #define transp RPC_THREAD_VARIABLE(svcsimple_transp_s)
66 static SVCXPRT
*transp
;
70 __registerrpc (u_long prognum
, u_long versnum
, u_long procnum
,
71 char *(*progname
) (char *), xdrproc_t inproc
, xdrproc_t outproc
)
76 if (procnum
== NULLPROC
)
79 if (__asprintf (&buf
, _("can't reassign procedure number %ld\n"),
86 transp
= svcudp_create (RPC_ANYSOCK
);
89 buf
= strdup (_("couldn't create an rpc server\n"));
93 (void) pmap_unset ((u_long
) prognum
, (u_long
) versnum
);
94 if (!svc_register (transp
, (u_long
) prognum
, (u_long
) versnum
,
95 universal
, IPPROTO_UDP
))
97 if (__asprintf (&buf
, _("couldn't register prog %ld vers %ld\n"),
98 prognum
, versnum
) < 0)
102 pl
= (struct proglst_
*) malloc (sizeof (struct proglst_
));
105 buf
= strdup (_("registerrpc: out of memory\n"));
108 pl
->p_progname
= progname
;
109 pl
->p_prognum
= prognum
;
110 pl
->p_procnum
= procnum
;
111 pl
->p_inproc
= inproc
;
112 pl
->p_outproc
= outproc
;
120 (void) __fxprintf (NULL
, "%s", buf
);
124 compat_symbol (libc
, __registerrpc
, registerrpc
, GLIBC_2_0
);
127 universal (struct svc_req
*rqstp
, SVCXPRT
*transp_l
)
131 char xdrbuf
[UDPMSGSIZE
];
136 * enforce "procnum 0 is echo" convention
138 if (rqstp
->rq_proc
== NULLPROC
)
140 if (svc_sendreply (transp_l
, (xdrproc_t
)xdr_void
,
141 (char *) NULL
) == FALSE
)
143 __write (STDERR_FILENO
, "xxx\n", 4);
148 prog
= rqstp
->rq_prog
;
149 proc
= rqstp
->rq_proc
;
150 for (pl
= proglst
; pl
!= NULL
; pl
= pl
->p_nxt
)
151 if (pl
->p_prognum
== prog
&& pl
->p_procnum
== proc
)
153 /* decode arguments into a CLEAN buffer */
154 __bzero (xdrbuf
, sizeof (xdrbuf
)); /* required ! */
155 if (!svc_getargs (transp_l
, pl
->p_inproc
, xdrbuf
))
157 svcerr_decode (transp_l
);
160 outdata
= (*(pl
->p_progname
)) (xdrbuf
);
161 if (outdata
== NULL
&& pl
->p_outproc
!= (xdrproc_t
)xdr_void
)
162 /* there was an error */
164 if (!svc_sendreply (transp_l
, pl
->p_outproc
, outdata
))
166 if (__asprintf (&buf
, _("trouble replying to prog %d\n"),
171 /* free the decoded arguments */
172 (void) svc_freeargs (transp_l
, pl
->p_inproc
, xdrbuf
);
175 if (__asprintf (&buf
, _("never registered prog %d\n"), prog
) < 0)
180 __fxprintf (NULL
, "%s", buf
);