diff options
author | Ulrich Drepper <drepper@redhat.com> | 2004-06-30 07:43:11 +0000 |
---|---|---|
committer | Ulrich Drepper <drepper@redhat.com> | 2004-06-30 07:43:11 +0000 |
commit | f5164429135806c41da679afb662d8482eec0a41 (patch) | |
tree | 3cd260d001c1fdb72635223a258d066e989337dd /sysdeps/unix/sysv | |
parent | 606135cf62624432a4b6fac724cad0e23b01202a (diff) | |
download | glibc-f5164429135806c41da679afb662d8482eec0a41.tar.gz glibc-f5164429135806c41da679afb662d8482eec0a41.tar.xz glibc-f5164429135806c41da679afb662d8482eec0a41.zip |
Update.
2004-06-30 Ulrich Drepper <drepper@redhat.com> * include/net/if.h: Handle if_nameindex and if_freenameindex with libc_proto_hidden. * sysdeps/unix/sysv/linux/netlinkaccess.h: New file. * sysdeps/unix/sysv/linux/ifaddrs.c: Export netlink handling functions. * sysdeps/unix/sysv/linux/if_index.c (if_nameindex): Implement using netlink if possible. Fall back on ioctl method if necessary. * include/unistd.h: Declare __truncate. * sysdeps/generic/truncate.c: Also define __truncate. * sysdeps/mach/hurd/truncate.c: Likewise. * sysdeps/unix/common/syscalls.list: Likewise. * sysdeps/unix/sysv/linux/truncate64.c: Use __truncate, not truncate.
Diffstat (limited to 'sysdeps/unix/sysv')
-rw-r--r-- | sysdeps/unix/sysv/linux/if_index.c | 159 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/ifaddrs.c | 83 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/netlinkaccess.h | 62 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/truncate64.c | 4 |
4 files changed, 242 insertions, 66 deletions
diff --git a/sysdeps/unix/sysv/linux/if_index.c b/sysdeps/unix/sysv/linux/if_index.c index 92fb11b180..377ccf5199 100644 --- a/sysdeps/unix/sysv/linux/if_index.c +++ b/sysdeps/unix/sysv/linux/if_index.c @@ -28,10 +28,11 @@ #include <bits/libc-lock.h> #include <not-cancel.h> -#include "kernel-features.h" +#include "netlinkaccess.h" + /* Variable to signal whether SIOCGIFCONF is not available. */ -#if __ASSUME_SIOCGIFNAME == 0 +# if __ASSUME_SIOCGIFNAME == 0 static int old_siocgifconf; #else # define old_siocgifconf 0 @@ -73,20 +74,18 @@ if_freenameindex (struct if_nameindex *ifn) struct if_nameindex *ptr = ifn; while (ptr->if_name || ptr->if_index) { - if (ptr->if_name) - free (ptr->if_name); + free (ptr->if_name); ++ptr; } free (ifn); } +libc_hidden_def (if_freenameindex) -struct if_nameindex * -if_nameindex (void) + +#if __ASSUME_NETLINK_SUPPORT == 0 +static struct if_nameindex * +if_nameindex_ioctl (void) { -#ifndef SIOCGIFINDEX - __set_errno (ENOSYS); - return NULL; -#else int fd = __opensock (); struct ifconf ifc; unsigned int nifs, i; @@ -174,8 +173,147 @@ if_nameindex (void) close_not_cancel_no_status (fd); return idx; +} +#endif + + +static struct if_nameindex * +if_nameindex_netlink (void) +{ + struct netlink_handle nh = { 0, 0, 0, NULL, NULL }; + struct if_nameindex *idx = NULL; + + if (__no_netlink_support || __netlink_open (&nh) < 0) + return NULL; + + + /* Tell the kernel that we wish to get a list of all + active interfaces. */ + if (__netlink_sendreq (&nh, RTM_GETLINK) < 0) + goto exit_close; + + /* Collect all data for every interface. */ + if (__netlink_receive (&nh) < 0) + goto exit_free; + + /* Count the interfaces. */ + unsigned int nifs = 0; + for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next) + { + struct nlmsghdr *nlh; + size_t size = nlp->size; + + if (nlp->nlh == NULL) + continue; + + /* Walk through all entries we got from the kernel and look, which + message type they contain. */ + for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) + { + /* Check if the message is what we want. */ + if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) + continue; + + if (nlh->nlmsg_type == NLMSG_DONE) + break; /* ok */ + + if (nlh->nlmsg_type == RTM_NEWLINK) + ++nifs; + } + } + + idx = malloc ((nifs + 1) * sizeof (struct if_nameindex)); + if (idx == NULL) + { + nomem: + __set_errno (ENOBUFS); + goto exit_free; + } + + /* Add the interfaces. */ + nifs = 0; + for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next) + { + struct nlmsghdr *nlh; + size_t size = nlp->size; + + if (nlp->nlh == NULL) + continue; + + /* Walk through all entries we got from the kernel and look, which + message type they contain. */ + for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) + { + /* Check if the message is what we want. */ + if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) + continue; + + if (nlh->nlmsg_type == NLMSG_DONE) + break; /* ok */ + + if (nlh->nlmsg_type == RTM_NEWLINK) + { + struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh); + struct rtattr *rta = IFLA_RTA (ifim); + size_t rtasize = IFLA_PAYLOAD (nlh); + + idx[nifs].if_index = ifim->ifi_index; + + while (RTA_OK (rta, rtasize)) + { + char *rta_data = RTA_DATA (rta); + size_t rta_payload = RTA_PAYLOAD (rta); + + if (rta->rta_type == IFLA_IFNAME) + { + idx[nifs].if_name = __strndup (rta_data, rta_payload); + if (idx[nifs].if_name == NULL) + { + idx[nifs].if_index = 0; + if_freenameindex (idx); + idx = NULL; + goto nomem; + } + break; + } + + rta = RTA_NEXT (rta, rtasize); + } + + ++nifs; + } + } + } + + idx[nifs].if_index = 0; + idx[nifs].if_name = NULL; + + exit_free: + __netlink_free_handle (&nh); + exit_close: + __netlink_close (&nh); + + return idx; +} + + +struct if_nameindex * +if_nameindex (void) +{ +#ifndef SIOCGIFINDEX + __set_errno (ENOSYS); + return NULL; +#else + struct if_nameindex *result = if_nameindex_netlink (); +# if __ASSUME_NETLINK_SUPPORT == 0 + if (__no_netlink_support) + result = if_nameindex_ioctl (); +# endif + return result; #endif } +libc_hidden_def (if_nameindex) + char * if_indextoname (unsigned int ifindex, char *ifname) @@ -263,6 +401,7 @@ if_indextoname (unsigned int ifindex, char *ifname) } libc_hidden_def (if_indextoname) + #if 0 void internal_function diff --git a/sysdeps/unix/sysv/linux/ifaddrs.c b/sysdeps/unix/sysv/linux/ifaddrs.c index 439e26f76c..f4dce4cd61 100644 --- a/sysdeps/unix/sysv/linux/ifaddrs.c +++ b/sysdeps/unix/sysv/linux/ifaddrs.c @@ -1,5 +1,5 @@ /* getifaddrs -- get names and addresses of all network interfaces - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -32,11 +32,8 @@ #include <time.h> #include <unistd.h> -#include <asm/types.h> -#include <linux/netlink.h> -#include <linux/rtnetlink.h> +#include "netlinkaccess.h" -#include "kernel-features.h" /* We don't know if we have NETLINK support compiled in in our Kernel, so include the old implementation as fallback. */ @@ -46,33 +43,9 @@ int __no_netlink_support attribute_hidden; # define getifaddrs fallback_getifaddrs # include "sysdeps/gnu/ifaddrs.c" # undef getifaddrs - -#else - -# define __no_netlink_support 0 - #endif -struct netlink_res -{ - struct netlink_res *next; - struct nlmsghdr *nlh; - size_t size; /* Size of response. */ - uint32_t seq; /* sequential number we used. */ -}; - - -struct netlink_handle -{ - int fd; /* Netlink file descriptor. */ - pid_t pid; /* Process ID. */ - uint32_t seq; /* The sequence number we use currently. */ - struct netlink_res *nlm_list; /* Pointer to list of responses. */ - struct netlink_res *end_ptr; /* For faster append of new entries. */ -}; - - /* struct to hold the data for one ifaddrs entry, so we can allocate everything at once. */ struct ifaddrs_storage @@ -91,8 +64,8 @@ struct ifaddrs_storage }; -static void -free_netlink_handle (struct netlink_handle *h) +void +__netlink_free_handle (struct netlink_handle *h) { struct netlink_res *ptr; int saved_errno = errno; @@ -107,12 +80,12 @@ free_netlink_handle (struct netlink_handle *h) ptr = tmpptr; } - errno = saved_errno; + __set_errno (saved_errno); } -static int -netlink_sendreq (struct netlink_handle *h, int type) +int +__netlink_sendreq (struct netlink_handle *h, int type) { struct { @@ -140,8 +113,8 @@ netlink_sendreq (struct netlink_handle *h, int type) } -static int -netlink_receive (struct netlink_handle *h) +int +__netlink_receive (struct netlink_handle *h) { struct netlink_res *nlm_next; char buf[4096]; @@ -211,8 +184,8 @@ netlink_receive (struct netlink_handle *h) } -static void -netlink_close (struct netlink_handle *h) +void +__netlink_close (struct netlink_handle *h) { /* Don't modify errno. */ INTERNAL_SYSCALL_DECL (err); @@ -221,21 +194,25 @@ netlink_close (struct netlink_handle *h) /* Open a NETLINK socket. */ -static int -netlink_open (struct netlink_handle *h) +int +__netlink_open (struct netlink_handle *h) { struct sockaddr_nl nladdr; h->fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (h->fd < 0) - return -1; + goto out; memset (&nladdr, '\0', sizeof (nladdr)); nladdr.nl_family = AF_NETLINK; if (__bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0) { close_and_out: - netlink_close (h); + __netlink_close (h); + out: +#if __ASSUME_NETLINK_SUPPORT == 0 + __no_netlink_support = 1; +#endif return -1; } /* Determine the ID the kernel assigned for this netlink connection. @@ -298,12 +275,10 @@ getifaddrs (struct ifaddrs **ifap) if (ifap) *ifap = NULL; - if (! __no_netlink_support && netlink_open (&nh) < 0) + if (! __no_netlink_support && __netlink_open (&nh) < 0) { -#if __ASSUME_NETLINK_SUPPORT == 0 - __no_netlink_support = 1; -#else - return -1; +#if __ASSUME_NETLINK_SUPPORT != 0 + return -1; #endif } @@ -314,13 +289,13 @@ getifaddrs (struct ifaddrs **ifap) /* Tell the kernel that we wish to get a list of all active interfaces. */ - if (netlink_sendreq (&nh, RTM_GETLINK) < 0) + if (__netlink_sendreq (&nh, RTM_GETLINK) < 0) { result = -1; goto exit_close; } /* Collect all data for every interface. */ - if (netlink_receive (&nh) < 0) + if (__netlink_receive (&nh) < 0) { result = -1; goto exit_free; @@ -332,9 +307,9 @@ getifaddrs (struct ifaddrs **ifap) interfaces in the list, we will later always find the interface before the corresponding addresses. */ ++nh.seq; - if (netlink_sendreq (&nh, RTM_GETADDR) < 0 + if (__netlink_sendreq (&nh, RTM_GETADDR) < 0 /* Collect all data for every interface. */ - || netlink_receive (&nh) < 0) + || __netlink_receive (&nh) < 0) { result = -1; goto exit_free; @@ -355,7 +330,7 @@ getifaddrs (struct ifaddrs **ifap) message type they contain. */ for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) { - /* check if the message is what we want */ + /* Check if the message is what we want. */ if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) continue; @@ -791,10 +766,10 @@ getifaddrs (struct ifaddrs **ifap) *ifap = &ifas[0].ifa; exit_free: - free_netlink_handle (&nh); + __netlink_free_handle (&nh); exit_close: - netlink_close (&nh); + __netlink_close (&nh); return result; } diff --git a/sysdeps/unix/sysv/linux/netlinkaccess.h b/sysdeps/unix/sysv/linux/netlinkaccess.h new file mode 100644 index 0000000000..6672e714ff --- /dev/null +++ b/sysdeps/unix/sysv/linux/netlinkaccess.h @@ -0,0 +1,62 @@ +/* Copyright (C) 2004 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#ifndef _NETLINKACCESS_H +#define _NETLINKACCESS_H 1 + +#include <asm/types.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> + +#include "kernel-features.h" + + +struct netlink_res +{ + struct netlink_res *next; + struct nlmsghdr *nlh; + size_t size; /* Size of response. */ + uint32_t seq; /* sequential number we used. */ +}; + + +struct netlink_handle +{ + int fd; /* Netlink file descriptor. */ + pid_t pid; /* Process ID. */ + uint32_t seq; /* The sequence number we use currently. */ + struct netlink_res *nlm_list; /* Pointer to list of responses. */ + struct netlink_res *end_ptr; /* For faster append of new entries. */ +}; + + +#if __ASSUME_NETLINK_SUPPORT == 0 +extern int __no_netlink_support attribute_hidden; +#else +# define __no_netlink_support 0 +#endif + + +extern int __netlink_open (struct netlink_handle *h); +extern void __netlink_close (struct netlink_handle *h); +extern void __netlink_free_handle (struct netlink_handle *h); +extern int __netlink_sendreq (struct netlink_handle *h, int type); +extern int __netlink_receive (struct netlink_handle *h); + + +#endif /* netlinkaccess.h */ diff --git a/sysdeps/unix/sysv/linux/truncate64.c b/sysdeps/unix/sysv/linux/truncate64.c index 764e808386..e6af2c4b04 100644 --- a/sysdeps/unix/sysv/linux/truncate64.c +++ b/sysdeps/unix/sysv/linux/truncate64.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc. +/* Copyright (C) 1997-2000, 2003, 2004 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -65,7 +65,7 @@ truncate64 (const char *path, off64_t length) __set_errno (EINVAL); return -1; } - return truncate (path, (off_t) length); + return __truncate (path, (off_t) length); #endif } |