1/*
2 * svc_simple.c
3 * Simplified front end to rpc.
4 *
5 * Copyright (c) 2010, Oracle America, Inc.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
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.
20 *
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.
33 */
34
35#include <stdio.h>
36#include <string.h>
37#include <libintl.h>
38#include <unistd.h>
39#include <rpc/rpc.h>
40#include <rpc/pmap_clnt.h>
41#include <sys/socket.h>
42#include <netdb.h>
43
44#include <wchar.h>
45#include <libio/iolibio.h>
46#include <shlib-compat.h>
47
48struct proglst_
49 {
50 char *(*p_progname) (char *);
51 int p_prognum;
52 int p_procnum;
53 xdrproc_t p_inproc, p_outproc;
54 struct proglst_ *p_nxt;
55 };
56#ifdef _RPC_THREAD_SAFE_
57#define proglst RPC_THREAD_VARIABLE(svcsimple_proglst_s)
58#else
59static struct proglst_ *proglst;
60#endif
61
62
63static void universal (struct svc_req *rqstp, SVCXPRT *transp_s);
64#ifdef _RPC_THREAD_SAFE_
65#define transp RPC_THREAD_VARIABLE(svcsimple_transp_s)
66#else
67static SVCXPRT *transp;
68#endif
69
70int
71__registerrpc (u_long prognum, u_long versnum, u_long procnum,
72 char *(*progname) (char *), xdrproc_t inproc, xdrproc_t outproc)
73{
74 struct proglst_ *pl;
75 char *buf;
76
77 if (procnum == NULLPROC)
78 {
79
80 if (__asprintf (&buf, _("can't reassign procedure number %ld\n"),
81 NULLPROC) < 0)
82 buf = NULL;
83 goto err_out;
84 }
85 if (transp == 0)
86 {
87 transp = svcudp_create (RPC_ANYSOCK);
88 if (transp == NULL)
89 {
90 buf = strdup (_("couldn't create an rpc server\n"));
91 goto err_out;
92 }
93 }
94 (void) pmap_unset ((u_long) prognum, (u_long) versnum);
95 if (!svc_register (transp, (u_long) prognum, (u_long) versnum,
96 universal, IPPROTO_UDP))
97 {
98 if (__asprintf (&buf, _("couldn't register prog %ld vers %ld\n"),
99 prognum, versnum) < 0)
100 buf = NULL;
101 goto err_out;
102 }
103 pl = (struct proglst_ *) malloc (sizeof (struct proglst_));
104 if (pl == NULL)
105 {
106 buf = strdup (_("registerrpc: out of memory\n"));
107 goto err_out;
108 }
109 pl->p_progname = progname;
110 pl->p_prognum = prognum;
111 pl->p_procnum = procnum;
112 pl->p_inproc = inproc;
113 pl->p_outproc = outproc;
114 pl->p_nxt = proglst;
115 proglst = pl;
116 return 0;
117
118 err_out:
119 if (buf == NULL)
120 return -1;
121 (void) __fxprintf (NULL, "%s", buf);
122 free (buf);
123 return -1;
124}
125
126libc_sunrpc_symbol (__registerrpc, registerrpc, GLIBC_2_0)
127
128
129static void
130universal (struct svc_req *rqstp, SVCXPRT *transp_l)
131{
132 int prog, proc;
133 char *outdata;
134 char xdrbuf[UDPMSGSIZE];
135 struct proglst_ *pl;
136 char *buf = NULL;
137
138 /*
139 * enforce "procnum 0 is echo" convention
140 */
141 if (rqstp->rq_proc == NULLPROC)
142 {
143 if (svc_sendreply (transp_l, (xdrproc_t)xdr_void,
144 (char *) NULL) == FALSE)
145 {
146 __write (STDERR_FILENO, "xxx\n", 4);
147 exit (1);
148 }
149 return;
150 }
151 prog = rqstp->rq_prog;
152 proc = rqstp->rq_proc;
153 for (pl = proglst; pl != NULL; pl = pl->p_nxt)
154 if (pl->p_prognum == prog && pl->p_procnum == proc)
155 {
156 /* decode arguments into a CLEAN buffer */
157 __bzero (xdrbuf, sizeof (xdrbuf)); /* required ! */
158 if (!svc_getargs (transp_l, pl->p_inproc, xdrbuf))
159 {
160 svcerr_decode (transp_l);
161 return;
162 }
163 outdata = (*(pl->p_progname)) (xdrbuf);
164 if (outdata == NULL && pl->p_outproc != (xdrproc_t)xdr_void)
165 /* there was an error */
166 return;
167 if (!svc_sendreply (transp_l, pl->p_outproc, outdata))
168 {
169 if (__asprintf (&buf, _("trouble replying to prog %d\n"),
170 pl->p_prognum) < 0)
171 buf = NULL;
172 goto err_out2;
173 }
174 /* free the decoded arguments */
175 (void) svc_freeargs (transp_l, pl->p_inproc, xdrbuf);
176 return;
177 }
178 if (__asprintf (&buf, _("never registered prog %d\n"), prog) < 0)
179 buf = NULL;
180 err_out2:
181 if (buf == NULL)
182 exit (1);
183 __fxprintf (NULL, "%s", buf);
184 free (buf);
185 exit (1);
186}
187