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