1 /* Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <sys/socket.h>
26 /* Return a socket of any type. The socket can be used in subsequent
27 ioctl calls to talk to the kernel. */
31 static int last_family
; /* Available socket family we will use. */
36 const char procname
[15];
39 /* The 2.2 kernels cannot handle ioctl(SIOCGIFCONF) on AF_UNIX sockets.
40 Give the kernel a chance to user inet sockets on old kernels. */
41 #if __LINUX_KERNEL_VERSION < 132096
43 { AF_UNIX
, "net/unix" },
45 { AF_UNIX
, "net/unix" },
48 { AF_INET6
, "net/if_inet6" },
49 { AF_AX25
, "net/ax25" },
50 { AF_NETROM
, "net/nr" },
51 { AF_ROSE
, "net/rose" },
52 { AF_IPX
, "net/ipx" },
53 { AF_APPLETALK
, "net/appletalk" },
54 { AF_ECONET
, "sys/net/econet" },
55 { AF_ASH
, "sys/net/ash" },
58 #define nafs (sizeof (afs) / sizeof (afs[0]))
59 char fname
[sizeof "/proc/" + 14];
64 /* We already know which family to use from the last call. Use it
68 assert (last_type
!= 0);
70 result
= __socket (last_family
, last_type
, 0);
71 if (result
!= -1 || errno
!= EAFNOSUPPORT
)
72 /* Maybe the socket type isn't supported anymore (module is
73 unloaded). In this case again try to find the type. */
76 /* Reset the values. They seem not valid anymore. */
81 /* Check whether the /proc filesystem is available. */
82 has_proc
= __access ("/proc/net", R_OK
) != -1;
83 strcpy (fname
, "/proc/");
85 /* Iterate over the interface families and find one which is
87 for (cnt
= 0; cnt
< nafs
; ++cnt
)
89 int type
= SOCK_DGRAM
;
91 if (has_proc
&& afs
[cnt
].procname
[0] != '\0')
93 strcpy (fname
+ 6, afs
[cnt
].procname
);
94 if (__access (fname
, R_OK
) == -1)
95 /* The /proc entry is not available. I.e., we cannot
96 create a socket of this type (without loading the
97 module). Don't look for it since this might trigger
98 loading the module. */
102 if (afs
[cnt
].family
== AF_NETROM
|| afs
[cnt
].family
== AF_X25
)
103 type
= SOCK_SEQPACKET
;
105 result
= __socket (afs
[cnt
].family
, type
, 0);
108 /* Found an available family. */
110 last_family
= afs
[cnt
].family
;
115 /* None of the protocol families is available. It is unclear what kind
116 of error is returned. ENOENT seems like a reasonable choice. */
117 __set_errno (ENOENT
);