Blame sysdeps/unix/sysv/linux/opensock.c

Packit 6c4009
/* Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
Packit 6c4009
/* Return a socket of any type.  The socket can be used in subsequent
Packit 6c4009
   ioctl calls to talk to the kernel.  */
Packit 6c4009
int
Packit 6c4009
__opensock (void)
Packit 6c4009
{
Packit 6c4009
  static int last_family;	/* Available socket family we will use.  */
Packit 6c4009
  static int last_type;
Packit 6c4009
  static const struct
Packit 6c4009
  {
Packit 6c4009
    int family;
Packit 6c4009
    const char procname[15];
Packit 6c4009
  } afs[] =
Packit 6c4009
    {
Packit 6c4009
      { AF_UNIX, "net/unix" },
Packit 6c4009
      { AF_INET, "" },
Packit 6c4009
      { AF_INET6, "net/if_inet6" },
Packit 6c4009
      { AF_AX25, "net/ax25" },
Packit 6c4009
      { AF_NETROM, "net/nr" },
Packit 6c4009
      { AF_ROSE, "net/rose" },
Packit 6c4009
      { AF_IPX, "net/ipx" },
Packit 6c4009
      { AF_APPLETALK, "net/appletalk" },
Packit 6c4009
      { AF_ECONET, "sys/net/econet" },
Packit 6c4009
      { AF_ASH, "sys/net/ash" },
Packit 6c4009
      { AF_X25, "net/x25" },
Packit 6c4009
#ifdef NEED_AF_IUCV
Packit 6c4009
      { AF_IUCV, "net/iucv" }
Packit 6c4009
#endif
Packit 6c4009
    };
Packit 6c4009
#define nafs (sizeof (afs) / sizeof (afs[0]))
Packit 6c4009
  char fname[sizeof "/proc/" + 14];
Packit 6c4009
  int result;
Packit 6c4009
  int has_proc;
Packit 6c4009
  size_t cnt;
Packit 6c4009
Packit 6c4009
  /* We already know which family to use from the last call.  Use it
Packit 6c4009
     again.  */
Packit 6c4009
  if (last_family != 0)
Packit 6c4009
    {
Packit 6c4009
      assert (last_type != 0);
Packit 6c4009
Packit 6c4009
      result = __socket (last_family, last_type | SOCK_CLOEXEC, 0);
Packit 6c4009
      if (result != -1 || errno != EAFNOSUPPORT)
Packit 6c4009
	/* Maybe the socket type isn't supported anymore (module is
Packit 6c4009
	   unloaded).  In this case again try to find the type.  */
Packit 6c4009
	return result;
Packit 6c4009
Packit 6c4009
      /* Reset the values.  They seem not valid anymore.  */
Packit 6c4009
      last_family = 0;
Packit 6c4009
      last_type = 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Check whether the /proc filesystem is available.  */
Packit 6c4009
  has_proc = __access ("/proc/net", R_OK) != -1;
Packit 6c4009
  strcpy (fname, "/proc/");
Packit 6c4009
Packit 6c4009
  /* Iterate over the interface families and find one which is
Packit 6c4009
     available.  */
Packit 6c4009
  for (cnt = 0; cnt < nafs; ++cnt)
Packit 6c4009
    {
Packit 6c4009
      int type = SOCK_DGRAM;
Packit 6c4009
Packit 6c4009
      if (has_proc && afs[cnt].procname[0] != '\0')
Packit 6c4009
	{
Packit 6c4009
	  strcpy (fname + 6, afs[cnt].procname);
Packit 6c4009
	  if (__access (fname, R_OK) == -1)
Packit 6c4009
	    /* The /proc entry is not available.  I.e., we cannot
Packit 6c4009
	       create a socket of this type (without loading the
Packit 6c4009
	       module).  Don't look for it since this might trigger
Packit 6c4009
	       loading the module.  */
Packit 6c4009
	    continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (afs[cnt].family == AF_NETROM || afs[cnt].family == AF_X25)
Packit 6c4009
	type = SOCK_SEQPACKET;
Packit 6c4009
Packit 6c4009
      result = __socket (afs[cnt].family, type | SOCK_CLOEXEC, 0);
Packit 6c4009
      if (result != -1)
Packit 6c4009
	{
Packit 6c4009
	  /* Found an available family.  */
Packit 6c4009
	  last_type = type;
Packit 6c4009
	  last_family = afs[cnt].family;
Packit 6c4009
	  return result;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* None of the protocol families is available.  It is unclear what kind
Packit 6c4009
     of error is returned.  ENOENT seems like a reasonable choice.  */
Packit 6c4009
  __set_errno (ENOENT);
Packit 6c4009
  return -1;
Packit 6c4009
}