posix: Correctly enable/disable cancellation on Linux posix_spawn
[glibc.git] / sunrpc / pmap_rmt.c
blobe54fe1447004c6b12e2af7884d4174661988333c
1 /*
2 * pmap_rmt.c
3 * Client interface to pmap rpc service.
4 * remote call and broadcast service
6 * Copyright (c) 2010, Oracle America, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials
17 * provided with the distribution.
18 * * Neither the name of the "Oracle America, Inc." nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <unistd.h>
37 #include <string.h>
38 #include <libintl.h>
39 #include <rpc/rpc.h>
40 #include <rpc/pmap_prot.h>
41 #include <rpc/pmap_clnt.h>
42 #include <rpc/pmap_rmt.h>
43 #include <sys/poll.h>
44 #include <sys/socket.h>
45 #include <stdio.h>
46 #include <errno.h>
47 #undef _POSIX_SOURCE /* Ultrix <sys/param.h> needs --roland@gnu */
48 #include <sys/param.h> /* Ultrix needs before net/if --roland@gnu */
49 #include <net/if.h>
50 #include <ifaddrs.h>
51 #include <sys/ioctl.h>
52 #include <arpa/inet.h>
53 #define MAX_BROADCAST_SIZE 1400
55 extern u_long _create_xid (void);
57 static const struct timeval timeout = {3, 0};
60 * pmapper remote-call-service interface.
61 * This routine is used to call the pmapper remote call service
62 * which will look up a service program in the port maps, and then
63 * remotely call that routine with the given parameters. This allows
64 * programs to do a lookup and call in one step.
66 enum clnt_stat
67 pmap_rmtcall (struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
68 xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
69 struct timeval tout, u_long *port_ptr)
71 int socket = -1;
72 CLIENT *client;
73 struct rmtcallargs a;
74 struct rmtcallres r;
75 enum clnt_stat stat;
77 addr->sin_port = htons (PMAPPORT);
78 client = clntudp_create (addr, PMAPPROG, PMAPVERS, timeout, &socket);
79 if (client != (CLIENT *) NULL)
81 a.prog = prog;
82 a.vers = vers;
83 a.proc = proc;
84 a.args_ptr = argsp;
85 a.xdr_args = xdrargs;
86 r.port_ptr = port_ptr;
87 r.results_ptr = resp;
88 r.xdr_results = xdrres;
89 stat = CLNT_CALL (client, PMAPPROC_CALLIT,
90 (xdrproc_t)xdr_rmtcall_args,
91 (caddr_t)&a, (xdrproc_t)xdr_rmtcallres,
92 (caddr_t)&r, tout);
93 CLNT_DESTROY (client);
95 else
97 stat = RPC_FAILED;
99 /* (void)__close(socket); CLNT_DESTROY already closed it */
100 addr->sin_port = 0;
101 return stat;
103 libc_hidden_nolink_sunrpc (pmap_rmtcall, GLIBC_2_0)
107 * XDR remote call arguments
108 * written for XDR_ENCODE direction only
110 bool_t
111 xdr_rmtcall_args (XDR *xdrs, struct rmtcallargs *cap)
113 u_int lenposition, argposition, position;
115 if (xdr_u_long (xdrs, &(cap->prog)) &&
116 xdr_u_long (xdrs, &(cap->vers)) &&
117 xdr_u_long (xdrs, &(cap->proc)))
119 u_long dummy_arglen = 0;
120 lenposition = XDR_GETPOS (xdrs);
121 if (!xdr_u_long (xdrs, &dummy_arglen))
122 return FALSE;
123 argposition = XDR_GETPOS (xdrs);
124 if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
125 return FALSE;
126 position = XDR_GETPOS (xdrs);
127 cap->arglen = (u_long) position - (u_long) argposition;
128 XDR_SETPOS (xdrs, lenposition);
129 if (!xdr_u_long (xdrs, &(cap->arglen)))
130 return FALSE;
131 XDR_SETPOS (xdrs, position);
132 return TRUE;
134 return FALSE;
136 libc_hidden_nolink_sunrpc (xdr_rmtcall_args, GLIBC_2_0)
139 * XDR remote call results
140 * written for XDR_DECODE direction only
142 bool_t
143 xdr_rmtcallres (XDR *xdrs, struct rmtcallres *crp)
145 caddr_t port_ptr;
147 port_ptr = (caddr_t) crp->port_ptr;
148 if (xdr_reference (xdrs, &port_ptr, sizeof (u_long),
149 (xdrproc_t) xdr_u_long)
150 && xdr_u_long (xdrs, &crp->resultslen))
152 crp->port_ptr = (u_long *) port_ptr;
153 return (*(crp->xdr_results)) (xdrs, crp->results_ptr);
155 return FALSE;
157 libc_hidden_nolink_sunrpc (xdr_rmtcallres, GLIBC_2_0)
161 * The following is kludged-up support for simple rpc broadcasts.
162 * Someday a large, complicated system will replace these trivial
163 * routines which only support udp/ip .
166 static int
167 internal_function
168 getbroadcastnets (struct in_addr *addrs, int naddrs)
170 struct ifaddrs *ifa;
172 if (getifaddrs (&ifa) != 0)
174 perror ("broadcast: getifaddrs");
175 return 0;
178 int i = 0;
179 struct ifaddrs *run = ifa;
180 while (run != NULL && i < naddrs)
182 if ((run->ifa_flags & IFF_BROADCAST) != 0
183 && (run->ifa_flags & IFF_UP) != 0
184 && run->ifa_addr != NULL
185 && run->ifa_addr->sa_family == AF_INET)
186 /* Copy the broadcast address. */
187 addrs[i++] = ((struct sockaddr_in *) run->ifa_broadaddr)->sin_addr;
189 run = run->ifa_next;
192 freeifaddrs (ifa);
194 return i;
198 enum clnt_stat
199 clnt_broadcast (/* program number */
200 u_long prog,
201 /* version number */
202 u_long vers,
203 /* procedure number */
204 u_long proc,
205 /* xdr routine for args */
206 xdrproc_t xargs,
207 /* pointer to args */
208 caddr_t argsp,
209 /* xdr routine for results */
210 xdrproc_t xresults,
211 /* pointer to results */
212 caddr_t resultsp,
213 /* call with each result obtained */
214 resultproc_t eachresult)
216 enum clnt_stat stat = RPC_FAILED;
217 AUTH *unix_auth = authunix_create_default ();
218 XDR xdr_stream;
219 XDR *xdrs = &xdr_stream;
220 struct timeval t;
221 int outlen, inlen, nets;
222 socklen_t fromlen;
223 int sock;
224 int on = 1;
225 struct pollfd fd;
226 int milliseconds;
227 int i;
228 bool_t done = FALSE;
229 u_long xid;
230 u_long port;
231 struct in_addr addrs[20];
232 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
233 struct rmtcallargs a;
234 struct rmtcallres r;
235 struct rpc_msg msg;
236 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
239 * initialization: create a socket, a broadcast address, and
240 * preserialize the arguments into a send buffer.
242 if ((sock = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
244 perror (_("Cannot create socket for broadcast rpc"));
245 stat = RPC_CANTSEND;
246 goto done_broad;
248 #ifdef SO_BROADCAST
249 if (__setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
251 perror (_("Cannot set socket option SO_BROADCAST"));
252 stat = RPC_CANTSEND;
253 goto done_broad;
255 #endif /* def SO_BROADCAST */
256 fd.fd = sock;
257 fd.events = POLLIN;
258 nets = getbroadcastnets (addrs, sizeof (addrs) / sizeof (addrs[0]));
259 __bzero ((char *) &baddr, sizeof (baddr));
260 baddr.sin_family = AF_INET;
261 baddr.sin_port = htons (PMAPPORT);
262 baddr.sin_addr.s_addr = htonl (INADDR_ANY);
263 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
264 msg.rm_xid = xid = _create_xid ();
265 t.tv_usec = 0;
266 msg.rm_direction = CALL;
267 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
268 msg.rm_call.cb_prog = PMAPPROG;
269 msg.rm_call.cb_vers = PMAPVERS;
270 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
271 msg.rm_call.cb_cred = unix_auth->ah_cred;
272 msg.rm_call.cb_verf = unix_auth->ah_verf;
273 a.prog = prog;
274 a.vers = vers;
275 a.proc = proc;
276 a.xdr_args = xargs;
277 a.args_ptr = argsp;
278 r.port_ptr = &port;
279 r.xdr_results = xresults;
280 r.results_ptr = resultsp;
281 xdrmem_create (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
282 if ((!xdr_callmsg (xdrs, &msg))
283 || (!xdr_rmtcall_args (xdrs, &a)))
285 stat = RPC_CANTENCODEARGS;
286 goto done_broad;
288 outlen = (int) xdr_getpos (xdrs);
289 xdr_destroy (xdrs);
291 * Basic loop: broadcast a packet and wait a while for response(s).
292 * The response timeout grows larger per iteration.
294 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
296 for (i = 0; i < nets; i++)
298 baddr.sin_addr = addrs[i];
299 if (__sendto (sock, outbuf, outlen, 0,
300 (struct sockaddr *) &baddr,
301 sizeof (struct sockaddr)) != outlen)
303 perror (_("Cannot send broadcast packet"));
304 stat = RPC_CANTSEND;
305 goto done_broad;
308 if (eachresult == NULL)
310 stat = RPC_SUCCESS;
311 goto done_broad;
313 recv_again:
314 msg.acpted_rply.ar_verf = _null_auth;
315 msg.acpted_rply.ar_results.where = (caddr_t) & r;
316 msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_rmtcallres;
317 milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
318 switch (__poll(&fd, 1, milliseconds))
321 case 0: /* timed out */
322 stat = RPC_TIMEDOUT;
323 continue;
325 case -1: /* some kind of error */
326 if (errno == EINTR)
327 goto recv_again;
328 perror (_("Broadcast poll problem"));
329 stat = RPC_CANTRECV;
330 goto done_broad;
332 } /* end of poll results switch */
333 try_again:
334 fromlen = sizeof (struct sockaddr);
335 inlen = __recvfrom (sock, inbuf, UDPMSGSIZE, 0,
336 (struct sockaddr *) &raddr, &fromlen);
337 if (inlen < 0)
339 if (errno == EINTR)
340 goto try_again;
341 perror (_("Cannot receive reply to broadcast"));
342 stat = RPC_CANTRECV;
343 goto done_broad;
345 if ((size_t) inlen < sizeof (u_long))
346 goto recv_again;
348 * see if reply transaction id matches sent id.
349 * If so, decode the results.
351 xdrmem_create (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
352 if (xdr_replymsg (xdrs, &msg))
354 if (((u_int32_t) msg.rm_xid == (u_int32_t) xid) &&
355 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
356 (msg.acpted_rply.ar_stat == SUCCESS))
358 raddr.sin_port = htons ((u_short) port);
359 done = (*eachresult) (resultsp, &raddr);
361 /* otherwise, we just ignore the errors ... */
363 else
365 #ifdef notdef
366 /* some kind of deserialization problem ... */
367 if ((u_int32_t) msg.rm_xid == (u_int32_t) xid)
368 fprintf (stderr, "Broadcast deserialization problem");
369 /* otherwise, just random garbage */
370 #endif
372 xdrs->x_op = XDR_FREE;
373 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
374 (void) xdr_replymsg (xdrs, &msg);
375 (void) (*xresults) (xdrs, resultsp);
376 xdr_destroy (xdrs);
377 if (done)
379 stat = RPC_SUCCESS;
380 goto done_broad;
382 else
384 goto recv_again;
387 done_broad:
388 (void) __close (sock);
389 AUTH_DESTROY (unix_auth);
390 return stat;
392 libc_hidden_nolink_sunrpc (clnt_broadcast, GLIBC_2_0)