(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / unix / sysv / linux / ifreq.c
blob098f11750ffa6f2b96c263dd35dbdfa082aa14a5
1 /* Copyright (C) 1999, 2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include "ifreq.h"
22 /* Variable to signal whether SIOCGIFCONF is not available. */
23 #if __ASSUME_SIOCGIFNAME == 0 || 1
24 static int old_siocgifconf;
25 #else
26 # define old_siocgifconf 0
27 #endif
30 void
31 __ifreq (struct ifreq **ifreqs, int *num_ifs, int sockfd)
33 int fd = sockfd;
34 struct ifconf ifc;
35 int rq_len;
36 int nifs;
37 # define RQ_IFS 4
39 if (fd < 0)
40 fd = __opensock ();
41 if (fd < 0)
43 *num_ifs = 0;
44 *ifreqs = NULL;
45 return;
48 ifc.ifc_buf = NULL;
50 /* We may be able to get the needed buffer size directly, rather than
51 guessing. */
52 if (! old_siocgifconf)
54 ifc.ifc_buf = NULL;
55 ifc.ifc_len = 0;
56 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
58 # if __ASSUME_SIOCGIFNAME == 0
59 old_siocgifconf = 1;
60 # endif
61 rq_len = RQ_IFS * sizeof (struct ifreq);
63 else
64 rq_len = ifc.ifc_len;
66 else
67 rq_len = RQ_IFS * sizeof (struct ifreq);
69 /* Read all the interfaces out of the kernel. */
70 while (1)
72 ifc.ifc_len = rq_len;
73 void *newp = realloc (ifc.ifc_buf, ifc.ifc_len);
74 if (newp == NULL
75 || (ifc.ifc_buf = newp, __ioctl (fd, SIOCGIFCONF, &ifc)) < 0)
77 free (ifc.ifc_buf);
79 if (fd != sockfd)
80 __close (fd);
82 *num_ifs = 0;
83 *ifreqs = NULL;
84 return;
87 if (!old_siocgifconf || ifc.ifc_len < rq_len)
88 break;
90 rq_len *= 2;
93 nifs = ifc.ifc_len / sizeof (struct ifreq);
95 if (fd != sockfd)
96 __close (fd);
98 *num_ifs = nifs;
99 *ifreqs = realloc (ifc.ifc_buf, nifs * sizeof (struct ifreq));