]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - sunrpc/clnt_udp.c
Assume that SOCK_CLOEXEC is available and works
[thirdparty/glibc.git] / sunrpc / clnt_udp.c
index 62ee3a1c99b3578d784da61695691fcd74050efc..cc1da474c437ff6886d17aa08f648c64187f6d8b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
  *
- * Copyright (C) 1984, Sun Microsystems, Inc.
+ * Copyright (c) 2010, Oracle America, Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -13,7 +13,7 @@
  *       copyright notice, this list of conditions and the following
  *       disclaimer in the documentation and/or other materials
  *       provided with the distribution.
- *     * Neither the name of Sun Microsystems, Inc. nor the names of its
+ *     * Neither the name of the "Oracle America, Inc." nor the names of its
  *       contributors may be used to endorse or promote products derived
  *       from this software without specific prior written permission.
  *
 #include <sys/ioctl.h>
 #include <netdb.h>
 #include <errno.h>
+#include <stdint.h>
 #include <rpc/pmap_clnt.h>
 #include <net/if.h>
 #include <ifaddrs.h>
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
+#include <wchar.h>
 #include <fcntl.h>
 
 #ifdef IP_RECVERR
@@ -57,7 +56,6 @@
 
 #include <kernel-features.h>
 
-extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
 extern u_long _create_xid (void);
 
 /*
@@ -165,42 +163,16 @@ __libc_clntudp_bufcreate (struct sockaddr_in *raddr, u_long program,
   call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
   call_msg.rm_call.cb_prog = program;
   call_msg.rm_call.cb_vers = version;
-  INTUSE(xdrmem_create) (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
-  if (!INTUSE(xdr_callhdr) (&(cu->cu_outxdrs), &call_msg))
+  xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
+  if (!xdr_callhdr (&(cu->cu_outxdrs), &call_msg))
     {
       goto fooy;
     }
   cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
   if (*sockp < 0)
     {
-      int dontblock = 1;
-
-#ifdef SOCK_NONBLOCK
-# ifndef __ASSUME_SOCK_CLOEXEC
-      if (__have_sock_cloexec >= 0)
-# endif
-       {
-         *sockp = __socket (AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|flags,
-                            IPPROTO_UDP);
-# ifndef __ASSUME_SOCK_CLOEXEC
-         if (__have_sock_cloexec == 0)
-           __have_sock_cloexec = *sockp >= 0 || errno != EINVAL ? 1 : -1;
-# endif
-       }
-#endif
-#ifndef __ASSUME_SOCK_CLOEXEC
-# ifdef SOCK_CLOEXEC
-      if (__have_sock_cloexec < 0)
-# endif
-       {
-         *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
-# ifdef SOCK_CLOEXEC
-         if (flags & SOCK_CLOEXEC)
-           __fcntl (*sockp, F_SETFD, FD_CLOEXEC);
-# endif
-       }
-#endif
-      if (__builtin_expect (*sockp < 0, 0))
+      *sockp = __socket (AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|flags, IPPROTO_UDP);
+      if (__glibc_unlikely (*sockp < 0))
        {
          struct rpc_createerr *ce = &get_rpc_createerr ();
          ce->cf_stat = RPC_SYSTEMERROR;
@@ -209,13 +181,6 @@ __libc_clntudp_bufcreate (struct sockaddr_in *raddr, u_long program,
        }
       /* attempt to bind to prov port */
       (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
-#ifndef __ASSUME_SOCK_CLOEXEC
-# ifdef SOCK_CLOEXEC
-      if (__have_sock_cloexec < 0)
-# endif
-       /* the sockets rpc controls are non-blocking */
-       (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock);
-#endif
 #ifdef IP_RECVERR
       {
        int on = 1;
@@ -229,7 +194,7 @@ __libc_clntudp_bufcreate (struct sockaddr_in *raddr, u_long program,
       cu->cu_closeit = FALSE;
     }
   cu->cu_sock = *sockp;
-  cl->cl_auth = INTUSE(authnone_create) ();
+  cl->cl_auth = authnone_create ();
   return cl;
 fooy:
   if (cu)
@@ -238,17 +203,21 @@ fooy:
     mem_free ((caddr_t) cl, sizeof (CLIENT));
   return (CLIENT *) NULL;
 }
-INTDEF (__libc_clntudp_bufcreate)
+#ifdef EXPORT_RPC_SYMBOLS
+libc_hidden_def (__libc_clntudp_bufcreate)
+#else
+libc_hidden_nolink_sunrpc (__libc_clntudp_bufcreate, GLIBC_PRIVATE)
+#endif
 
 CLIENT *
 clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
                   struct timeval wait, int *sockp, u_int sendsz,
                   u_int recvsz)
 {
-  return INTUSE(__libc_clntudp_bufcreate) (raddr, program, version, wait,
-                                          sockp, sendsz, recvsz, 0);
+  return __libc_clntudp_bufcreate (raddr, program, version, wait,
+                                  sockp, sendsz, recvsz, 0);
 }
-INTDEF (clntudp_bufcreate)
+libc_hidden_nolink_sunrpc (clntudp_bufcreate, GLIBC_2_0)
 
 CLIENT *
 clntudp_create (raddr, program, version, wait, sockp)
@@ -258,10 +227,14 @@ clntudp_create (raddr, program, version, wait, sockp)
      struct timeval wait;
      int *sockp;
 {
-  return INTUSE(__libc_clntudp_bufcreate) (raddr, program, version, wait,
-                                          sockp, UDPMSGSIZE, UDPMSGSIZE, 0);
+  return __libc_clntudp_bufcreate (raddr, program, version, wait,
+                                  sockp, UDPMSGSIZE, UDPMSGSIZE, 0);
 }
-INTDEF (clntudp_create)
+#ifdef EXPORT_RPC_SYMBOLS
+libc_hidden_def (clntudp_create)
+#else
+libc_hidden_nolink_sunrpc (clntudp_create, GLIBC_2_0)
+#endif
 
 static int
 is_network_up (int sock)
@@ -465,10 +438,9 @@ send_again:
        continue;
 
       /* see if reply transaction id matches sent id.
-        Don't do this if we only wait for a replay */
+       Don't do this if we only wait for a replay */
       if (xargs != NULL
-         && (*((u_int32_t *) (cu->cu_inbuf))
-             != *((u_int32_t *) (cu->cu_outbuf))))
+         && memcmp (cu->cu_inbuf, cu->cu_outbuf, sizeof (u_int32_t)) != 0)
        continue;
       /* we now assume we have the proper reply */
       break;
@@ -477,8 +449,8 @@ send_again:
   /*
    * now decode and validate the response
    */
-  INTUSE(xdrmem_create) (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
-  ok = INTUSE(xdr_replymsg) (&reply_xdrs, &reply_msg);
+  xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
+  ok = xdr_replymsg (&reply_xdrs, &reply_msg);
   /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
   if (ok)
     {
@@ -494,8 +466,7 @@ send_again:
          if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
            {
              xdrs->x_op = XDR_FREE;
-             (void) INTUSE(xdr_opaque_auth) (xdrs,
-                                             &(reply_msg.acpted_rply.ar_verf));
+             (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
            }
        }                       /* end successful completion */
       else
@@ -543,6 +514,8 @@ static bool_t
 clntudp_control (CLIENT *cl, int request, char *info)
 {
   struct cu_data *cu = (struct cu_data *) cl->cl_private;
+  u_long ul;
+  u_int32_t ui32;
 
   switch (request)
     {
@@ -576,39 +549,48 @@ clntudp_control (CLIENT *cl, int request, char *info)
        * first element in the call structure *.
        * This will get the xid of the PREVIOUS call
        */
-      *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
+      memcpy (&ui32, cu->cu_outbuf, sizeof (ui32));
+      ul = ntohl (ui32);
+      memcpy (info, &ul, sizeof (ul));
       break;
     case CLSET_XID:
       /* This will set the xid of the NEXT call */
-      *(u_long *)cu->cu_outbuf =  htonl(*(u_long *)info - 1);
+      memcpy (&ul, info, sizeof (ul));
+      ui32 = htonl (ul - 1);
+      memcpy (cu->cu_outbuf, &ui32, sizeof (ui32));
       /* decrement by 1 as clntudp_call() increments once */
+      break;
     case CLGET_VERS:
       /*
        * This RELIES on the information that, in the call body,
        * the version number field is the fifth field from the
-       * begining of the RPC header. MUST be changed if the
+       * beginning of the RPC header. MUST be changed if the
        * call_struct is changed
        */
-      *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
-                                         4 * BYTES_PER_XDR_UNIT));
+      memcpy (&ui32, cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT, sizeof (ui32));
+      ul = ntohl (ui32);
+      memcpy (info, &ul, sizeof (ul));
       break;
     case CLSET_VERS:
-      *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
-       = htonl(*(u_long *)info);
+      memcpy (&ul, info, sizeof (ul));
+      ui32 = htonl (ul);
+      memcpy (cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
       break;
     case CLGET_PROG:
       /*
        * This RELIES on the information that, in the call body,
        * the program number field is the  field from the
-       * begining of the RPC header. MUST be changed if the
+       * beginning of the RPC header. MUST be changed if the
        * call_struct is changed
        */
-      *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
-                                         3 * BYTES_PER_XDR_UNIT));
+      memcpy (&ui32, cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT, sizeof (ui32));
+      ul = ntohl (ui32);
+      memcpy (info, &ul, sizeof (ul));
       break;
     case CLSET_PROG:
-      *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
-       = htonl(*(u_long *)info);
+      memcpy (&ul, info, sizeof (ul));
+      ui32 = htonl (ul);
+      memcpy (cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
       break;
     /* The following are only possible with TI-RPC */
     case CLGET_SVC_ADDR: