Back to index

glibc  2.9
opensock.c
Go to the documentation of this file.
00001 /* Copyright (C) 1999, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <assert.h>
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <unistd.h>
00024 #include <sys/socket.h>
00025 #include <kernel-features.h>
00026 
00027 /* Return a socket of any type.  The socket can be used in subsequent
00028    ioctl calls to talk to the kernel.  */
00029 int internal_function
00030 __opensock (void)
00031 {
00032   static int last_family;   /* Available socket family we will use.  */
00033   static int last_type;
00034   static const struct
00035   {
00036     int family;
00037     const char procname[15];
00038   } afs[] =
00039     {
00040       /* The 2.2 kernels cannot handle ioctl(SIOCGIFCONF) on AF_UNIX sockets.
00041         Give the kernel a chance to user inet sockets on old kernels.  */
00042 #if __LINUX_KERNEL_VERSION < 132096
00043       { AF_INET, "" },
00044       { AF_UNIX, "net/unix" },
00045 #else
00046       { AF_UNIX, "net/unix" },
00047       { AF_INET, "" },
00048 #endif
00049       { AF_INET6, "net/if_inet6" },
00050       { AF_AX25, "net/ax25" },
00051       { AF_NETROM, "net/nr" },
00052       { AF_ROSE, "net/rose" },
00053       { AF_IPX, "net/ipx" },
00054       { AF_APPLETALK, "net/appletalk" },
00055       { AF_ECONET, "sys/net/econet" },
00056       { AF_ASH, "sys/net/ash" },
00057       { AF_X25, "net/x25" },
00058 #ifdef NEED_AF_IUCV
00059       { AF_IUCV, "net/iucv" }
00060 #endif
00061     };
00062 #define nafs (sizeof (afs) / sizeof (afs[0]))
00063   char fname[sizeof "/proc/" + 14];
00064   int result;
00065   int has_proc;
00066   size_t cnt;
00067 
00068   /* We already know which family to use from the last call.  Use it
00069      again.  */
00070   if (last_family != 0)
00071     {
00072       assert (last_type != 0);
00073 
00074 #ifdef SOCK_CLOEXEC
00075 # ifndef __ASSUME_SOCK_CLOEXEC
00076       if (__have_sock_cloexec >= 0)
00077 # endif
00078        {
00079          result = __socket (last_family, last_type | SOCK_CLOEXEC, 0);
00080 # ifndef __ASSUME_SOCK_CLOEXEC
00081          if (__have_sock_cloexec == 0)
00082            __have_sock_cloexec = result != -1 || errno != EINVAL ? 1 : -1;
00083 # endif
00084        }
00085 #endif
00086 #ifndef __ASSUME_SOCK_CLOEXEC
00087 # ifdef SOCK_CLOEXEC
00088       if (__have_sock_cloexec < 0)
00089 # endif
00090        result = __socket (last_family, last_type, 0);
00091 #endif
00092       if (result != -1 || errno != EAFNOSUPPORT)
00093        /* Maybe the socket type isn't supported anymore (module is
00094           unloaded).  In this case again try to find the type.  */
00095        return result;
00096 
00097       /* Reset the values.  They seem not valid anymore.  */
00098       last_family = 0;
00099       last_type = 0;
00100     }
00101 
00102   /* Check whether the /proc filesystem is available.  */
00103   has_proc = __access ("/proc/net", R_OK) != -1;
00104   strcpy (fname, "/proc/");
00105 
00106   /* Iterate over the interface families and find one which is
00107      available.  */
00108   for (cnt = 0; cnt < nafs; ++cnt)
00109     {
00110       int type = SOCK_DGRAM;
00111 
00112       if (has_proc && afs[cnt].procname[0] != '\0')
00113        {
00114          strcpy (fname + 6, afs[cnt].procname);
00115          if (__access (fname, R_OK) == -1)
00116            /* The /proc entry is not available.  I.e., we cannot
00117               create a socket of this type (without loading the
00118               module).  Don't look for it since this might trigger
00119               loading the module.  */
00120            continue;
00121        }
00122 
00123       if (afs[cnt].family == AF_NETROM || afs[cnt].family == AF_X25)
00124        type = SOCK_SEQPACKET;
00125 
00126 #ifdef SOCK_CLOEXEC
00127 # ifndef __ASSUME_SOCK_CLOEXEC
00128       if (__have_sock_cloexec >= 0)
00129 # endif
00130        {
00131          result = __socket (afs[cnt].family, type | SOCK_CLOEXEC, 0);
00132 # ifndef __ASSUME_SOCK_CLOEXEC
00133          if (__have_sock_cloexec == 0)
00134            __have_sock_cloexec = result != -1 || errno != EINVAL ? 1 : -1;
00135 # endif
00136        }
00137 #endif
00138 #ifndef __ASSUME_SOCK_CLOEXEC
00139 # ifdef SOCK_CLOEXEC
00140       if (__have_sock_cloexec < 0)
00141 # endif
00142        result = __socket (afs[cnt].family, type, 0);
00143 #endif
00144       if (result != -1)
00145        {
00146          /* Found an available family.  */
00147          last_type = type;
00148          last_family = afs[cnt].family;
00149          return result;
00150        }
00151     }
00152 
00153   /* None of the protocol families is available.  It is unclear what kind
00154      of error is returned.  ENOENT seems like a reasonable choice.  */
00155   __set_errno (ENOENT);
00156   return -1;
00157 }