Whamcloud - gitweb
LU-2800 autoconf: clean up sysctl table handling
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-tcpip.c
index 0a31b55..200f6be 100644 (file)
 /* For sys_open & sys_close */
 #include <linux/syscalls.h>
 
+#ifndef HAVE_SK_SLEEP
+static inline wait_queue_head_t *sk_sleep(struct sock *sk)
+{
+       return sk->sk_sleep;
+}
+#endif
+
 int
 libcfs_sock_ioctl(int cmd, unsigned long arg)
 {
-        mm_segment_t   oldmm = get_fs();
-        struct socket  *sock;
-        int             fd;
-        int             rc;
-        struct file     *sock_filp;
+       mm_segment_t    oldmm = get_fs();
+       struct socket  *sock;
+       int             fd = -1;
+       int             rc;
+       struct file    *sock_filp;
 
         rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
         if (rc != 0) {
@@ -59,21 +66,25 @@ libcfs_sock_ioctl(int cmd, unsigned long arg)
                 return rc;
         }
 
-#ifdef HAVE_SOCK_MAP_FD_2ARG
-        fd = sock_map_fd(sock,0);
+#if !defined(HAVE_SOCK_ALLOC_FILE) && !defined(HAVE_SOCK_ALLOC_FILE_3ARGS)
+       fd = sock_map_fd(sock, 0);
+       if (fd < 0) {
+               rc = fd;
+               sock_release(sock);
+               goto out;
+       }
+       sock_filp = fget(fd);
 #else
-        fd = sock_map_fd(sock);
+# ifdef HAVE_SOCK_ALLOC_FILE_3ARGS
+       sock_filp = sock_alloc_file(sock, 0, NULL);
+# else
+       sock_filp = sock_alloc_file(sock, 0);
+# endif
 #endif
-        if (fd < 0) {
-                rc = fd;
-                sock_release(sock);
-                goto out;
-        }
-
-        sock_filp = fget(fd);
         if (!sock_filp) {
                 rc = -ENOMEM;
-                goto out_fd;
+               sock_release(sock);
+                goto out;
         }
 
        set_fs(KERNEL_DS);
@@ -83,9 +94,10 @@ libcfs_sock_ioctl(int cmd, unsigned long arg)
 
         fput(sock_filp);
 
- out_fd:
-        sys_close(fd);
  out:
+       if (fd >= 0)
+               sys_close(fd);
+
         return rc;
 }
 
@@ -105,7 +117,10 @@ libcfs_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
 
         CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
 
-        strcpy(ifr.ifr_name, name);
+       if (strlen(name) > sizeof(ifr.ifr_name)-1)
+               return -E2BIG;
+       strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+
         rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
 
         if (rc != 0) {
@@ -122,7 +137,10 @@ libcfs_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
 
         *up = 1;
 
-        strcpy(ifr.ifr_name, name);
+       if (strlen(name) > sizeof(ifr.ifr_name)-1)
+               return -E2BIG;
+       strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+
         ifr.ifr_addr.sa_family = AF_INET;
         rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
 
@@ -134,7 +152,10 @@ libcfs_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
         val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
         *ip = ntohl(val);
 
-        strcpy(ifr.ifr_name, name);
+       if (strlen(name) > sizeof(ifr.ifr_name)-1)
+               return -E2BIG;
+       strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+
         ifr.ifr_addr.sa_family = AF_INET;
         rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
 
@@ -169,12 +190,12 @@ libcfs_ipif_enumerate (char ***namesp)
         nalloc = 16;        /* first guess at max interfaces */
         toobig = 0;
         for (;;) {
-                if (nalloc * sizeof(*ifr) > CFS_PAGE_SIZE) {
-                        toobig = 1;
-                        nalloc = CFS_PAGE_SIZE/sizeof(*ifr);
-                        CWARN("Too many interfaces: only enumerating first %d\n",
-                              nalloc);
-                }
+               if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
+                       toobig = 1;
+                       nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
+                       CWARN("Too many interfaces: only enumerating first %d\n",
+                             nalloc);
+               }
 
                 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
                 if (ifr == NULL) {
@@ -591,18 +612,18 @@ libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
 
         newsock->ops = sock->ops;
 
-        set_current_state(TASK_INTERRUPTIBLE);
-       add_wait_queue(cfs_sk_sleep(sock->sk), &wait);
+       set_current_state(TASK_INTERRUPTIBLE);
+       add_wait_queue(sk_sleep(sock->sk), &wait);
 
-        rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
-        if (rc == -EAGAIN) {
-                /* Nothing ready, so wait for activity */
-                schedule();
-                rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
-        }
+       rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
+       if (rc == -EAGAIN) {
+               /* Nothing ready, so wait for activity */
+               schedule();
+               rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
+       }
 
-       remove_wait_queue(cfs_sk_sleep(sock->sk), &wait);
-        set_current_state(TASK_RUNNING);
+       remove_wait_queue(sk_sleep(sock->sk), &wait);
+       set_current_state(TASK_RUNNING);
 
         if (rc != 0)
                 goto failed;
@@ -620,7 +641,7 @@ EXPORT_SYMBOL(libcfs_sock_accept);
 void
 libcfs_sock_abort_accept (struct socket *sock)
 {
-       wake_up_all(cfs_sk_sleep(sock->sk));
+       wake_up_all(sk_sleep(sock->sk));
 }
 
 EXPORT_SYMBOL(libcfs_sock_abort_accept);