Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / opensock.c
blobbcf7f5fc402e20f917dc2113c0c043476463915c
1 /* Copyright (C) 1999-2014 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 { AF_UNIX, "net/unix" },
40 { AF_INET, "" },
41 { AF_INET6, "net/if_inet6" },
42 { AF_AX25, "net/ax25" },
43 { AF_NETROM, "net/nr" },
44 { AF_ROSE, "net/rose" },
45 { AF_IPX, "net/ipx" },
46 { AF_APPLETALK, "net/appletalk" },
47 { AF_ECONET, "sys/net/econet" },
48 { AF_ASH, "sys/net/ash" },
49 { AF_X25, "net/x25" },
50 #ifdef NEED_AF_IUCV
51 { AF_IUCV, "net/iucv" }
52 #endif
54 #define nafs (sizeof (afs) / sizeof (afs[0]))
55 char fname[sizeof "/proc/" + 14];
56 int result;
57 int has_proc;
58 size_t cnt;
60 /* We already know which family to use from the last call. Use it
61 again. */
62 if (last_family != 0)
64 assert (last_type != 0);
66 #ifdef SOCK_CLOEXEC
67 # ifndef __ASSUME_SOCK_CLOEXEC
68 if (__have_sock_cloexec >= 0)
69 # endif
71 result = __socket (last_family, last_type | SOCK_CLOEXEC, 0);
72 # ifndef __ASSUME_SOCK_CLOEXEC
73 if (__have_sock_cloexec == 0)
74 __have_sock_cloexec = result != -1 || errno != EINVAL ? 1 : -1;
75 # endif
77 #endif
78 #ifndef __ASSUME_SOCK_CLOEXEC
79 # ifdef SOCK_CLOEXEC
80 if (__have_sock_cloexec < 0)
81 # endif
82 result = __socket (last_family, last_type, 0);
83 #endif
84 if (result != -1 || errno != EAFNOSUPPORT)
85 /* Maybe the socket type isn't supported anymore (module is
86 unloaded). In this case again try to find the type. */
87 return result;
89 /* Reset the values. They seem not valid anymore. */
90 last_family = 0;
91 last_type = 0;
94 /* Check whether the /proc filesystem is available. */
95 has_proc = __access ("/proc/net", R_OK) != -1;
96 strcpy (fname, "/proc/");
98 /* Iterate over the interface families and find one which is
99 available. */
100 for (cnt = 0; cnt < nafs; ++cnt)
102 int type = SOCK_DGRAM;
104 if (has_proc && afs[cnt].procname[0] != '\0')
106 strcpy (fname + 6, afs[cnt].procname);
107 if (__access (fname, R_OK) == -1)
108 /* The /proc entry is not available. I.e., we cannot
109 create a socket of this type (without loading the
110 module). Don't look for it since this might trigger
111 loading the module. */
112 continue;
115 if (afs[cnt].family == AF_NETROM || afs[cnt].family == AF_X25)
116 type = SOCK_SEQPACKET;
118 #ifdef SOCK_CLOEXEC
119 # ifndef __ASSUME_SOCK_CLOEXEC
120 if (__have_sock_cloexec >= 0)
121 # endif
123 result = __socket (afs[cnt].family, type | SOCK_CLOEXEC, 0);
124 # ifndef __ASSUME_SOCK_CLOEXEC
125 if (__have_sock_cloexec == 0)
126 __have_sock_cloexec = result != -1 || errno != EINVAL ? 1 : -1;
127 # endif
129 #endif
130 #ifndef __ASSUME_SOCK_CLOEXEC
131 # ifdef SOCK_CLOEXEC
132 if (__have_sock_cloexec < 0)
133 # endif
134 result = __socket (afs[cnt].family, type, 0);
135 #endif
136 if (result != -1)
138 /* Found an available family. */
139 last_type = type;
140 last_family = afs[cnt].family;
141 return result;
145 /* None of the protocol families is available. It is unclear what kind
146 of error is returned. ENOENT seems like a reasonable choice. */
147 __set_errno (ENOENT);
148 return -1;