2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / opensock.c
blob191d72011d997c2e93c7d5c4cd2c84349f3b4b04
1 /* Copyright (C) 1999, 2001, 2002, 2007, 2008 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
17 02111-1307 USA. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/socket.h>
25 #include <kernel-features.h>
27 /* Return a socket of any type. The socket can be used in subsequent
28 ioctl calls to talk to the kernel. */
29 int internal_function
30 __opensock (void)
32 static int last_family; /* Available socket family we will use. */
33 static int last_type;
34 static const struct
36 int family;
37 const char procname[15];
38 } afs[] =
40 /* The 2.2 kernels cannot handle ioctl(SIOCGIFCONF) on AF_UNIX sockets.
41 Give the kernel a chance to user inet sockets on old kernels. */
42 #if __LINUX_KERNEL_VERSION < 132096
43 { AF_INET, "" },
44 { AF_UNIX, "net/unix" },
45 #else
46 { AF_UNIX, "net/unix" },
47 { AF_INET, "" },
48 #endif
49 { AF_INET6, "net/if_inet6" },
50 { AF_AX25, "net/ax25" },
51 { AF_NETROM, "net/nr" },
52 { AF_ROSE, "net/rose" },
53 { AF_IPX, "net/ipx" },
54 { AF_APPLETALK, "net/appletalk" },
55 { AF_ECONET, "sys/net/econet" },
56 { AF_ASH, "sys/net/ash" },
57 { AF_X25, "net/x25" },
58 #ifdef NEED_AF_IUCV
59 { AF_IUCV, "net/iucv" }
60 #endif
62 #define nafs (sizeof (afs) / sizeof (afs[0]))
63 char fname[sizeof "/proc/" + 14];
64 int result;
65 int has_proc;
66 size_t cnt;
68 /* We already know which family to use from the last call. Use it
69 again. */
70 if (last_family != 0)
72 assert (last_type != 0);
74 #ifdef SOCK_CLOEXEC
75 # ifndef __ASSUME_SOCK_CLOEXEC
76 if (__have_sock_cloexec >= 0)
77 # endif
79 result = __socket (last_family, last_type | SOCK_CLOEXEC, 0);
80 # ifndef __ASSUME_SOCK_CLOEXEC
81 if (__have_sock_cloexec == 0)
82 __have_sock_cloexec = result != -1 || errno != EINVAL ? 1 : -1;
83 # endif
85 #endif
86 #ifndef __ASSUME_SOCK_CLOEXEC
87 # ifdef SOCK_CLOEXEC
88 if (__have_sock_cloexec < 0)
89 # endif
90 result = __socket (last_family, last_type, 0);
91 #endif
92 if (result != -1 || errno != EAFNOSUPPORT)
93 /* Maybe the socket type isn't supported anymore (module is
94 unloaded). In this case again try to find the type. */
95 return result;
97 /* Reset the values. They seem not valid anymore. */
98 last_family = 0;
99 last_type = 0;
102 /* Check whether the /proc filesystem is available. */
103 has_proc = __access ("/proc/net", R_OK) != -1;
104 strcpy (fname, "/proc/");
106 /* Iterate over the interface families and find one which is
107 available. */
108 for (cnt = 0; cnt < nafs; ++cnt)
110 int type = SOCK_DGRAM;
112 if (has_proc && afs[cnt].procname[0] != '\0')
114 strcpy (fname + 6, afs[cnt].procname);
115 if (__access (fname, R_OK) == -1)
116 /* The /proc entry is not available. I.e., we cannot
117 create a socket of this type (without loading the
118 module). Don't look for it since this might trigger
119 loading the module. */
120 continue;
123 if (afs[cnt].family == AF_NETROM || afs[cnt].family == AF_X25)
124 type = SOCK_SEQPACKET;
126 #ifdef SOCK_CLOEXEC
127 # ifndef __ASSUME_SOCK_CLOEXEC
128 if (__have_sock_cloexec >= 0)
129 # endif
131 result = __socket (afs[cnt].family, type | SOCK_CLOEXEC, 0);
132 # ifndef __ASSUME_SOCK_CLOEXEC
133 if (__have_sock_cloexec == 0)
134 __have_sock_cloexec = result != -1 || errno != EINVAL ? 1 : -1;
135 # endif
137 #endif
138 #ifndef __ASSUME_SOCK_CLOEXEC
139 # ifdef SOCK_CLOEXEC
140 if (__have_sock_cloexec < 0)
141 # endif
142 result = __socket (afs[cnt].family, type, 0);
143 #endif
144 if (result != -1)
146 /* Found an available family. */
147 last_type = type;
148 last_family = afs[cnt].family;
149 return result;
153 /* None of the protocol families is available. It is unclear what kind
154 of error is returned. ENOENT seems like a reasonable choice. */
155 __set_errno (ENOENT);
156 return -1;