Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / opensock.c
blob595938062181135de8c967f9b422cd55340093a8
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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/socket.h>
24 #include <kernel-features.h>
26 /* Return a socket of any type. The socket can be used in subsequent
27 ioctl calls to talk to the kernel. */
28 int internal_function
29 __opensock (void)
31 static int last_family; /* Available socket family we will use. */
32 static int last_type;
33 static const struct
35 int family;
36 const char procname[15];
37 } afs[] =
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
42 { AF_INET, "" },
43 { AF_UNIX, "net/unix" },
44 #else
45 { AF_UNIX, "net/unix" },
46 { AF_INET, "" },
47 #endif
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" },
56 { AF_X25, "net/x25" },
57 #ifdef NEED_AF_IUCV
58 { AF_IUCV, "net/iucv" }
59 #endif
61 #define nafs (sizeof (afs) / sizeof (afs[0]))
62 char fname[sizeof "/proc/" + 14];
63 int result;
64 int has_proc;
65 size_t cnt;
67 /* We already know which family to use from the last call. Use it
68 again. */
69 if (last_family != 0)
71 assert (last_type != 0);
73 #ifdef SOCK_CLOEXEC
74 # ifndef __ASSUME_SOCK_CLOEXEC
75 if (__have_sock_cloexec >= 0)
76 # endif
78 result = __socket (last_family, last_type | SOCK_CLOEXEC, 0);
79 # ifndef __ASSUME_SOCK_CLOEXEC
80 if (__have_sock_cloexec == 0)
81 __have_sock_cloexec = result != -1 || errno != EINVAL ? 1 : -1;
82 # endif
84 #endif
85 #ifndef __ASSUME_SOCK_CLOEXEC
86 # ifdef SOCK_CLOEXEC
87 if (__have_sock_cloexec < 0)
88 # endif
89 result = __socket (last_family, last_type, 0);
90 #endif
91 if (result != -1 || errno != EAFNOSUPPORT)
92 /* Maybe the socket type isn't supported anymore (module is
93 unloaded). In this case again try to find the type. */
94 return result;
96 /* Reset the values. They seem not valid anymore. */
97 last_family = 0;
98 last_type = 0;
101 /* Check whether the /proc filesystem is available. */
102 has_proc = __access ("/proc/net", R_OK) != -1;
103 strcpy (fname, "/proc/");
105 /* Iterate over the interface families and find one which is
106 available. */
107 for (cnt = 0; cnt < nafs; ++cnt)
109 int type = SOCK_DGRAM;
111 if (has_proc && afs[cnt].procname[0] != '\0')
113 strcpy (fname + 6, afs[cnt].procname);
114 if (__access (fname, R_OK) == -1)
115 /* The /proc entry is not available. I.e., we cannot
116 create a socket of this type (without loading the
117 module). Don't look for it since this might trigger
118 loading the module. */
119 continue;
122 if (afs[cnt].family == AF_NETROM || afs[cnt].family == AF_X25)
123 type = SOCK_SEQPACKET;
125 #ifdef SOCK_CLOEXEC
126 # ifndef __ASSUME_SOCK_CLOEXEC
127 if (__have_sock_cloexec >= 0)
128 # endif
130 result = __socket (afs[cnt].family, type | SOCK_CLOEXEC, 0);
131 # ifndef __ASSUME_SOCK_CLOEXEC
132 if (__have_sock_cloexec == 0)
133 __have_sock_cloexec = result != -1 || errno != EINVAL ? 1 : -1;
134 # endif
136 #endif
137 #ifndef __ASSUME_SOCK_CLOEXEC
138 # ifdef SOCK_CLOEXEC
139 if (__have_sock_cloexec < 0)
140 # endif
141 result = __socket (afs[cnt].family, type, 0);
142 #endif
143 if (result != -1)
145 /* Found an available family. */
146 last_type = type;
147 last_family = afs[cnt].family;
148 return result;
152 /* None of the protocol families is available. It is unclear what kind
153 of error is returned. ENOENT seems like a reasonable choice. */
154 __set_errno (ENOENT);
155 return -1;