2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* NET An implementation of the SOCKET network access protocol.
|
|
|
|
*
|
|
|
|
* Version: @(#)socket.c 1.1.93 18/02/95
|
|
|
|
*
|
|
|
|
* Authors: Orest Zborowski, <obz@Kodak.COM>
|
2005-05-06 01:16:16 +02:00
|
|
|
* Ross Biro
|
2005-04-17 00:20:36 +02:00
|
|
|
* Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
|
|
|
|
*
|
|
|
|
* Fixes:
|
|
|
|
* Anonymous : NOTSOCK/BADF cleanup. Error fix in
|
|
|
|
* shutdown()
|
|
|
|
* Alan Cox : verify_area() fixes
|
|
|
|
* Alan Cox : Removed DDI
|
|
|
|
* Jonathan Kamens : SOCK_DGRAM reconnect bug
|
|
|
|
* Alan Cox : Moved a load of checks to the very
|
|
|
|
* top level.
|
|
|
|
* Alan Cox : Move address structures to/from user
|
|
|
|
* mode above the protocol layers.
|
|
|
|
* Rob Janssen : Allow 0 length sends.
|
|
|
|
* Alan Cox : Asynchronous I/O support (cribbed from the
|
|
|
|
* tty drivers).
|
|
|
|
* Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
|
|
|
|
* Jeff Uphoff : Made max number of sockets command-line
|
|
|
|
* configurable.
|
|
|
|
* Matti Aarnio : Made the number of sockets dynamic,
|
|
|
|
* to be allocated when needed, and mr.
|
|
|
|
* Uphoff's max is used as max to be
|
|
|
|
* allowed to allocate.
|
|
|
|
* Linus : Argh. removed all the socket allocation
|
|
|
|
* altogether: it's in the inode now.
|
|
|
|
* Alan Cox : Made sock_alloc()/sock_release() public
|
|
|
|
* for NetROM and future kernel nfsd type
|
|
|
|
* stuff.
|
|
|
|
* Alan Cox : sendmsg/recvmsg basics.
|
|
|
|
* Tom Dyas : Export net symbols.
|
|
|
|
* Marcin Dalecki : Fixed problems with CONFIG_NET="n".
|
|
|
|
* Alan Cox : Added thread locking to sys_* calls
|
|
|
|
* for sockets. May have errors at the
|
|
|
|
* moment.
|
|
|
|
* Kevin Buhr : Fixed the dumb errors in the above.
|
|
|
|
* Andi Kleen : Some small cleanups, optimizations,
|
|
|
|
* and fixed a copy_from_user() bug.
|
|
|
|
* Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
|
2006-09-01 09:19:31 +02:00
|
|
|
* Tigran Aivazian : Made listen(2) backlog sanity checks
|
2005-04-17 00:20:36 +02:00
|
|
|
* protocol-independent
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This module is effectively the top level interface to the BSD socket
|
2006-09-01 09:19:31 +02:00
|
|
|
* paradigm.
|
2005-04-17 00:20:36 +02:00
|
|
|
*
|
|
|
|
* Based upon Swansea University Computer Society NET3.039
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/interrupt.h>
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
#include <linux/thread_info.h>
|
2006-09-01 09:23:39 +02:00
|
|
|
#include <linux/rcupdate.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/seq_file.h>
|
2006-03-21 07:33:17 +01:00
|
|
|
#include <linux/mutex.h>
|
2008-07-24 06:29:21 +02:00
|
|
|
#include <linux/thread_info.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/wanrouter.h>
|
|
|
|
#include <linux/if_bridge.h>
|
2005-08-16 07:18:02 +02:00
|
|
|
#include <linux/if_frad.h>
|
|
|
|
#include <linux/if_vlan.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/cache.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/security.h>
|
|
|
|
#include <linux/syscalls.h>
|
|
|
|
#include <linux/compat.h>
|
|
|
|
#include <linux/kmod.h>
|
2005-05-17 13:08:48 +02:00
|
|
|
#include <linux/audit.h>
|
2006-01-21 00:46:55 +01:00
|
|
|
#include <linux/wireless.h>
|
2007-10-09 08:24:22 +02:00
|
|
|
#include <linux/nsproxy.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <asm/unistd.h>
|
|
|
|
|
|
|
|
#include <net/compat.h>
|
2008-06-03 18:14:03 +02:00
|
|
|
#include <net/wext.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
#include <net/sock.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
|
|
|
|
static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
|
2006-10-01 08:28:46 +02:00
|
|
|
static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
|
|
|
|
unsigned long nr_segs, loff_t pos);
|
|
|
|
static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
|
|
|
|
unsigned long nr_segs, loff_t pos);
|
2006-09-01 09:19:31 +02:00
|
|
|
static int sock_mmap(struct file *file, struct vm_area_struct *vma);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
static int sock_close(struct inode *inode, struct file *file);
|
|
|
|
static unsigned int sock_poll(struct file *file,
|
|
|
|
struct poll_table_struct *wait);
|
2006-09-01 09:19:31 +02:00
|
|
|
static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
|
2006-03-22 08:58:08 +01:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
static long compat_sock_ioctl(struct file *file,
|
2006-09-01 09:19:31 +02:00
|
|
|
unsigned int cmd, unsigned long arg);
|
2006-03-22 08:58:08 +01:00
|
|
|
#endif
|
2005-04-17 00:20:36 +02:00
|
|
|
static int sock_fasync(int fd, struct file *filp, int on);
|
|
|
|
static ssize_t sock_sendpage(struct file *file, struct page *page,
|
|
|
|
int offset, size_t size, loff_t *ppos, int more);
|
2007-11-07 08:30:13 +01:00
|
|
|
static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
|
|
|
|
struct pipe_inode_info *pipe, size_t len,
|
|
|
|
unsigned int flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
|
|
|
|
* in the operation structures but are done directly via the socketcall() multiplexor.
|
|
|
|
*/
|
|
|
|
|
2007-02-12 09:55:36 +01:00
|
|
|
static const struct file_operations socket_file_ops = {
|
2005-04-17 00:20:36 +02:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.aio_read = sock_aio_read,
|
|
|
|
.aio_write = sock_aio_write,
|
|
|
|
.poll = sock_poll,
|
|
|
|
.unlocked_ioctl = sock_ioctl,
|
2006-03-22 08:58:08 +01:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = compat_sock_ioctl,
|
|
|
|
#endif
|
2005-04-17 00:20:36 +02:00
|
|
|
.mmap = sock_mmap,
|
|
|
|
.open = sock_no_open, /* special open code to disallow open via /proc */
|
|
|
|
.release = sock_close,
|
|
|
|
.fasync = sock_fasync,
|
2006-03-30 15:15:30 +02:00
|
|
|
.sendpage = sock_sendpage,
|
|
|
|
.splice_write = generic_splice_sendpage,
|
2007-11-07 08:30:13 +01:00
|
|
|
.splice_read = sock_splice_read,
|
2005-04-17 00:20:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The protocol list. Each protocol is registered in here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(net_family_lock);
|
2006-08-10 06:03:17 +02:00
|
|
|
static const struct net_proto_family *net_families[NPROTO] __read_mostly;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Statistics counters of the socket lists
|
|
|
|
*/
|
|
|
|
|
|
|
|
static DEFINE_PER_CPU(int, sockets_in_use) = 0;
|
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* Support routines.
|
|
|
|
* Move socket addresses back and forth across the kernel/user
|
|
|
|
* divide and look after the messy bits.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
|
2005-04-17 00:20:36 +02:00
|
|
|
16 for IP, 16 for IPX,
|
|
|
|
24 for IPv6,
|
2006-09-01 09:19:31 +02:00
|
|
|
about 80 for AX.25
|
2005-04-17 00:20:36 +02:00
|
|
|
must be at least one bigger than
|
|
|
|
the AF_UNIX size (see net/unix/af_unix.c
|
2006-09-01 09:19:31 +02:00
|
|
|
:unix_mkname()).
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/**
|
|
|
|
* move_addr_to_kernel - copy a socket address into kernel space
|
|
|
|
* @uaddr: Address in user space
|
|
|
|
* @kaddr: Address in kernel space
|
|
|
|
* @ulen: Length in user space
|
|
|
|
*
|
|
|
|
* The address is copied into kernel space. If the provided address is
|
|
|
|
* too long an error code of -EINVAL is returned. If the copy gives
|
|
|
|
* invalid addresses -EFAULT is returned. On a success 0 is returned.
|
|
|
|
*/
|
|
|
|
|
2008-07-20 07:35:47 +02:00
|
|
|
int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-07-20 07:35:47 +02:00
|
|
|
if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
2006-09-01 09:19:31 +02:00
|
|
|
if (ulen == 0)
|
2005-04-17 00:20:36 +02:00
|
|
|
return 0;
|
2006-09-01 09:19:31 +02:00
|
|
|
if (copy_from_user(kaddr, uaddr, ulen))
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EFAULT;
|
2005-05-17 13:08:48 +02:00
|
|
|
return audit_sockaddr(ulen, kaddr);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* move_addr_to_user - copy an address to user space
|
|
|
|
* @kaddr: kernel space address
|
|
|
|
* @klen: length of address in kernel
|
|
|
|
* @uaddr: user space address
|
|
|
|
* @ulen: pointer to user length field
|
|
|
|
*
|
|
|
|
* The value pointed to by ulen on entry is the buffer length available.
|
|
|
|
* This is overwritten with the buffer space used. -EINVAL is returned
|
|
|
|
* if an overlong buffer is specified or a negative buffer size. -EFAULT
|
|
|
|
* is returned if either the buffer or the length field are not
|
|
|
|
* accessible.
|
|
|
|
* After copying the data up to the limit the user specifies, the true
|
|
|
|
* length of the data is written over the length limit the user
|
|
|
|
* specified. Zero is returned for a success.
|
|
|
|
*/
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2008-07-20 07:35:47 +02:00
|
|
|
int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
|
2006-09-01 09:19:31 +02:00
|
|
|
int __user *ulen)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
int len;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
err = get_user(len, ulen);
|
|
|
|
if (err)
|
2005-04-17 00:20:36 +02:00
|
|
|
return err;
|
2006-09-01 09:19:31 +02:00
|
|
|
if (len > klen)
|
|
|
|
len = klen;
|
2008-07-20 07:35:47 +02:00
|
|
|
if (len < 0 || len > sizeof(struct sockaddr_storage))
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
2006-09-01 09:19:31 +02:00
|
|
|
if (len) {
|
2006-03-30 19:20:22 +02:00
|
|
|
if (audit_sockaddr(klen, kaddr))
|
|
|
|
return -ENOMEM;
|
2006-09-01 09:19:31 +02:00
|
|
|
if (copy_to_user(uaddr, kaddr, len))
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* "fromlen shall refer to the value before truncation.."
|
|
|
|
* 1003.1g
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
return __put_user(klen, ulen);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SOCKFS_MAGIC 0x534F434B
|
|
|
|
|
2006-12-07 05:33:20 +01:00
|
|
|
static struct kmem_cache *sock_inode_cachep __read_mostly;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
static struct inode *sock_alloc_inode(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct socket_alloc *ei;
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2006-12-07 05:33:17 +01:00
|
|
|
ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!ei)
|
|
|
|
return NULL;
|
|
|
|
init_waitqueue_head(&ei->socket.wait);
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
ei->socket.fasync_list = NULL;
|
|
|
|
ei->socket.state = SS_UNCONNECTED;
|
|
|
|
ei->socket.flags = 0;
|
|
|
|
ei->socket.ops = NULL;
|
|
|
|
ei->socket.sk = NULL;
|
|
|
|
ei->socket.file = NULL;
|
|
|
|
|
|
|
|
return &ei->vfs_inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sock_destroy_inode(struct inode *inode)
|
|
|
|
{
|
|
|
|
kmem_cache_free(sock_inode_cachep,
|
|
|
|
container_of(inode, struct socket_alloc, vfs_inode));
|
|
|
|
}
|
|
|
|
|
2008-07-26 04:45:34 +02:00
|
|
|
static void init_once(void *foo)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-09-01 09:19:31 +02:00
|
|
|
struct socket_alloc *ei = (struct socket_alloc *)foo;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-05-17 07:10:57 +02:00
|
|
|
inode_init_once(&ei->vfs_inode);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
static int init_inodecache(void)
|
|
|
|
{
|
|
|
|
sock_inode_cachep = kmem_cache_create("sock_inode_cache",
|
2006-09-01 09:19:31 +02:00
|
|
|
sizeof(struct socket_alloc),
|
|
|
|
0,
|
|
|
|
(SLAB_HWCACHE_ALIGN |
|
|
|
|
SLAB_RECLAIM_ACCOUNT |
|
|
|
|
SLAB_MEM_SPREAD),
|
2007-07-20 03:11:58 +02:00
|
|
|
init_once);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (sock_inode_cachep == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct super_operations sockfs_ops = {
|
|
|
|
.alloc_inode = sock_alloc_inode,
|
|
|
|
.destroy_inode =sock_destroy_inode,
|
|
|
|
.statfs = simple_statfs,
|
|
|
|
};
|
|
|
|
|
[PATCH] VFS: Permit filesystem to override root dentry on mount
Extend the get_sb() filesystem operation to take an extra argument that
permits the VFS to pass in the target vfsmount that defines the mountpoint.
The filesystem is then required to manually set the superblock and root dentry
pointers. For most filesystems, this should be done with simple_set_mnt()
which will set the superblock pointer and then set the root dentry to the
superblock's s_root (as per the old default behaviour).
The get_sb() op now returns an integer as there's now no need to return the
superblock pointer.
This patch permits a superblock to be implicitly shared amongst several mount
points, such as can be done with NFS to avoid potential inode aliasing. In
such a case, simple_set_mnt() would not be called, and instead the mnt_root
and mnt_sb would be set directly.
The patch also makes the following changes:
(*) the get_sb_*() convenience functions in the core kernel now take a vfsmount
pointer argument and return an integer, so most filesystems have to change
very little.
(*) If one of the convenience function is not used, then get_sb() should
normally call simple_set_mnt() to instantiate the vfsmount. This will
always return 0, and so can be tail-called from get_sb().
(*) generic_shutdown_super() now calls shrink_dcache_sb() to clean up the
dcache upon superblock destruction rather than shrink_dcache_anon().
This is required because the superblock may now have multiple trees that
aren't actually bound to s_root, but that still need to be cleaned up. The
currently called functions assume that the whole tree is rooted at s_root,
and that anonymous dentries are not the roots of trees which results in
dentries being left unculled.
However, with the way NFS superblock sharing are currently set to be
implemented, these assumptions are violated: the root of the filesystem is
simply a dummy dentry and inode (the real inode for '/' may well be
inaccessible), and all the vfsmounts are rooted on anonymous[*] dentries
with child trees.
[*] Anonymous until discovered from another tree.
(*) The documentation has been adjusted, including the additional bit of
changing ext2_* into foo_* in the documentation.
[akpm@osdl.org: convert ipath_fs, do other stuff]
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Al Viro <viro@zeniv.linux.org.uk>
Cc: Nathan Scott <nathans@sgi.com>
Cc: Roland Dreier <rolandd@cisco.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-06-23 11:02:57 +02:00
|
|
|
static int sockfs_get_sb(struct file_system_type *fs_type,
|
2006-09-01 09:19:31 +02:00
|
|
|
int flags, const char *dev_name, void *data,
|
|
|
|
struct vfsmount *mnt)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
[PATCH] VFS: Permit filesystem to override root dentry on mount
Extend the get_sb() filesystem operation to take an extra argument that
permits the VFS to pass in the target vfsmount that defines the mountpoint.
The filesystem is then required to manually set the superblock and root dentry
pointers. For most filesystems, this should be done with simple_set_mnt()
which will set the superblock pointer and then set the root dentry to the
superblock's s_root (as per the old default behaviour).
The get_sb() op now returns an integer as there's now no need to return the
superblock pointer.
This patch permits a superblock to be implicitly shared amongst several mount
points, such as can be done with NFS to avoid potential inode aliasing. In
such a case, simple_set_mnt() would not be called, and instead the mnt_root
and mnt_sb would be set directly.
The patch also makes the following changes:
(*) the get_sb_*() convenience functions in the core kernel now take a vfsmount
pointer argument and return an integer, so most filesystems have to change
very little.
(*) If one of the convenience function is not used, then get_sb() should
normally call simple_set_mnt() to instantiate the vfsmount. This will
always return 0, and so can be tail-called from get_sb().
(*) generic_shutdown_super() now calls shrink_dcache_sb() to clean up the
dcache upon superblock destruction rather than shrink_dcache_anon().
This is required because the superblock may now have multiple trees that
aren't actually bound to s_root, but that still need to be cleaned up. The
currently called functions assume that the whole tree is rooted at s_root,
and that anonymous dentries are not the roots of trees which results in
dentries being left unculled.
However, with the way NFS superblock sharing are currently set to be
implemented, these assumptions are violated: the root of the filesystem is
simply a dummy dentry and inode (the real inode for '/' may well be
inaccessible), and all the vfsmounts are rooted on anonymous[*] dentries
with child trees.
[*] Anonymous until discovered from another tree.
(*) The documentation has been adjusted, including the additional bit of
changing ext2_* into foo_* in the documentation.
[akpm@osdl.org: convert ipath_fs, do other stuff]
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Al Viro <viro@zeniv.linux.org.uk>
Cc: Nathan Scott <nathans@sgi.com>
Cc: Roland Dreier <rolandd@cisco.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-06-23 11:02:57 +02:00
|
|
|
return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
|
|
|
|
mnt);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-08-26 21:05:31 +02:00
|
|
|
static struct vfsmount *sock_mnt __read_mostly;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
static struct file_system_type sock_fs_type = {
|
|
|
|
.name = "sockfs",
|
|
|
|
.get_sb = sockfs_get_sb,
|
|
|
|
.kill_sb = kill_anon_super,
|
|
|
|
};
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
static int sockfs_delete_dentry(struct dentry *dentry)
|
|
|
|
{
|
2006-12-07 05:38:49 +01:00
|
|
|
/*
|
|
|
|
* At creation time, we pretended this dentry was hashed
|
|
|
|
* (by clearing DCACHE_UNHASHED bit in d_flags)
|
|
|
|
* At delete time, we restore the truth : not hashed.
|
|
|
|
* (so that dput() can proceed correctly)
|
|
|
|
*/
|
|
|
|
dentry->d_flags |= DCACHE_UNHASHED;
|
|
|
|
return 0;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2007-05-08 09:26:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* sockfs_dname() is called from d_path().
|
|
|
|
*/
|
|
|
|
static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
|
|
|
|
{
|
|
|
|
return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
|
|
|
|
dentry->d_inode->i_ino);
|
|
|
|
}
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
static struct dentry_operations sockfs_dentry_operations = {
|
2006-09-01 09:19:31 +02:00
|
|
|
.d_delete = sockfs_delete_dentry,
|
2007-05-08 09:26:18 +02:00
|
|
|
.d_dname = sockfs_dname,
|
2005-04-17 00:20:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Obtains the first available file descriptor and sets it up for use.
|
|
|
|
*
|
2006-03-21 02:13:49 +01:00
|
|
|
* These functions create file structures and maps them to fd space
|
|
|
|
* of the current process. On success it returns file descriptor
|
2005-04-17 00:20:36 +02:00
|
|
|
* and file struct implicitly stored in sock->file.
|
|
|
|
* Note that another thread may close file descriptor before we return
|
|
|
|
* from this function. We use the fact that now we do not refer
|
|
|
|
* to socket after mapping. If one day we will need it, this
|
|
|
|
* function will increment ref. count on file by 1.
|
|
|
|
*
|
|
|
|
* In any case returned fd MAY BE not valid!
|
|
|
|
* This race condition is unavoidable
|
|
|
|
* with shared fd spaces, we cannot solve it inside kernel,
|
|
|
|
* but we take care of internal coherence yet.
|
|
|
|
*/
|
|
|
|
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
static int sock_alloc_fd(struct file **filep, int flags)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
fd = get_unused_fd_flags(flags);
|
2006-03-21 02:13:49 +01:00
|
|
|
if (likely(fd >= 0)) {
|
2005-04-17 00:20:36 +02:00
|
|
|
struct file *file = get_empty_filp();
|
|
|
|
|
2006-03-21 02:13:49 +01:00
|
|
|
*filep = file;
|
|
|
|
if (unlikely(!file)) {
|
2005-04-17 00:20:36 +02:00
|
|
|
put_unused_fd(fd);
|
2006-03-21 02:13:49 +01:00
|
|
|
return -ENFILE;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-03-21 02:13:49 +01:00
|
|
|
} else
|
|
|
|
*filep = NULL;
|
|
|
|
return fd;
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
|
2006-03-21 02:13:49 +01:00
|
|
|
{
|
2007-10-17 08:31:13 +02:00
|
|
|
struct dentry *dentry;
|
2007-05-08 09:26:18 +02:00
|
|
|
struct qstr name = { .name = "" };
|
2006-03-21 02:13:49 +01:00
|
|
|
|
2007-10-17 08:31:13 +02:00
|
|
|
dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
|
|
|
|
if (unlikely(!dentry))
|
2006-03-21 02:13:49 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2007-10-17 08:31:13 +02:00
|
|
|
dentry->d_op = &sockfs_dentry_operations;
|
2006-12-07 05:38:49 +01:00
|
|
|
/*
|
|
|
|
* We dont want to push this dentry into global dentry hash table.
|
|
|
|
* We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
|
|
|
|
* This permits a working /proc/$pid/fd/XXX on sockets
|
|
|
|
*/
|
2007-10-17 08:31:13 +02:00
|
|
|
dentry->d_flags &= ~DCACHE_UNHASHED;
|
|
|
|
d_instantiate(dentry, SOCK_INODE(sock));
|
2006-03-21 02:13:49 +01:00
|
|
|
|
|
|
|
sock->file = file;
|
2007-10-17 08:31:13 +02:00
|
|
|
init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
|
|
|
|
&socket_file_ops);
|
|
|
|
SOCK_INODE(sock)->i_fop = &socket_file_ops;
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
file->f_flags = O_RDWR | (flags & O_NONBLOCK);
|
2006-03-21 02:13:49 +01:00
|
|
|
file->f_pos = 0;
|
|
|
|
file->private_data = sock;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-03-21 02:13:49 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
int sock_map_fd(struct socket *sock, int flags)
|
2006-03-21 02:13:49 +01:00
|
|
|
{
|
|
|
|
struct file *newfile;
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
int fd = sock_alloc_fd(&newfile, flags);
|
2006-03-21 02:13:49 +01:00
|
|
|
|
|
|
|
if (likely(fd >= 0)) {
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
int err = sock_attach_fd(sock, newfile, flags);
|
2006-03-21 02:13:49 +01:00
|
|
|
|
|
|
|
if (unlikely(err < 0)) {
|
|
|
|
put_filp(newfile);
|
2005-04-17 00:20:36 +02:00
|
|
|
put_unused_fd(fd);
|
2006-03-21 02:13:49 +01:00
|
|
|
return err;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-03-21 02:13:49 +01:00
|
|
|
fd_install(fd, newfile);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2006-03-21 07:27:12 +01:00
|
|
|
static struct socket *sock_from_file(struct file *file, int *err)
|
|
|
|
{
|
|
|
|
if (file->f_op == &socket_file_ops)
|
|
|
|
return file->private_data; /* set in sock_map_fd */
|
|
|
|
|
2007-02-08 23:59:57 +01:00
|
|
|
*err = -ENOTSOCK;
|
|
|
|
return NULL;
|
2006-03-21 07:27:12 +01:00
|
|
|
}
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/**
|
|
|
|
* sockfd_lookup - Go from a file number to its socket slot
|
|
|
|
* @fd: file handle
|
|
|
|
* @err: pointer to an error code return
|
|
|
|
*
|
|
|
|
* The file handle passed in is locked and the socket it is bound
|
|
|
|
* too is returned. If an error occurs the err pointer is overwritten
|
|
|
|
* with a negative errno code and NULL is returned. The function checks
|
|
|
|
* for both invalid handles and passing a handle which is not a socket.
|
|
|
|
*
|
|
|
|
* On a success the socket object pointer is returned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct socket *sockfd_lookup(int fd, int *err)
|
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
struct socket *sock;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
file = fget(fd);
|
|
|
|
if (!file) {
|
2005-04-17 00:20:36 +02:00
|
|
|
*err = -EBADF;
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2006-03-21 07:27:12 +01:00
|
|
|
sock = sock_from_file(file, err);
|
|
|
|
if (!sock)
|
2005-04-17 00:20:36 +02:00
|
|
|
fput(file);
|
2006-03-21 07:27:12 +01:00
|
|
|
return sock;
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-03-21 07:27:12 +01:00
|
|
|
static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
|
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
struct socket *sock;
|
|
|
|
|
2006-04-20 00:25:02 +02:00
|
|
|
*err = -EBADF;
|
2006-03-21 07:27:12 +01:00
|
|
|
file = fget_light(fd, fput_needed);
|
|
|
|
if (file) {
|
|
|
|
sock = sock_from_file(file, err);
|
|
|
|
if (sock)
|
|
|
|
return sock;
|
|
|
|
fput_light(file, *fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-03-21 07:27:12 +01:00
|
|
|
return NULL;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sock_alloc - allocate a socket
|
2006-09-01 09:19:31 +02:00
|
|
|
*
|
2005-04-17 00:20:36 +02:00
|
|
|
* Allocate a new inode and socket object. The two are bound together
|
|
|
|
* and initialised. The socket is then returned. If we are out of inodes
|
|
|
|
* NULL is returned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct socket *sock_alloc(void)
|
|
|
|
{
|
2006-09-01 09:19:31 +02:00
|
|
|
struct inode *inode;
|
|
|
|
struct socket *sock;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
inode = new_inode(sock_mnt->mnt_sb);
|
|
|
|
if (!inode)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sock = SOCKET_I(inode);
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
inode->i_mode = S_IFSOCK | S_IRWXUGO;
|
2005-04-17 00:20:36 +02:00
|
|
|
inode->i_uid = current->fsuid;
|
|
|
|
inode->i_gid = current->fsgid;
|
|
|
|
|
|
|
|
get_cpu_var(sockets_in_use)++;
|
|
|
|
put_cpu_var(sockets_in_use);
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In theory you can't get an open on this inode, but /proc provides
|
|
|
|
* a back door. Remember to keep it shut otherwise you'll let the
|
|
|
|
* creepy crawlies in.
|
|
|
|
*/
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
|
|
|
|
{
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
2006-03-28 11:56:42 +02:00
|
|
|
const struct file_operations bad_sock_fops = {
|
2005-04-17 00:20:36 +02:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = sock_no_open,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sock_release - close a socket
|
|
|
|
* @sock: socket to close
|
|
|
|
*
|
|
|
|
* The socket is released from the protocol stack if it has a release
|
|
|
|
* callback, and the inode is then released if the socket is bound to
|
2006-09-01 09:19:31 +02:00
|
|
|
* an inode not a file.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
void sock_release(struct socket *sock)
|
|
|
|
{
|
|
|
|
if (sock->ops) {
|
|
|
|
struct module *owner = sock->ops->owner;
|
|
|
|
|
|
|
|
sock->ops->release(sock);
|
|
|
|
sock->ops = NULL;
|
|
|
|
module_put(owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->fasync_list)
|
|
|
|
printk(KERN_ERR "sock_release: fasync list not empty!\n");
|
|
|
|
|
|
|
|
get_cpu_var(sockets_in_use)--;
|
|
|
|
put_cpu_var(sockets_in_use);
|
|
|
|
if (!sock->file) {
|
|
|
|
iput(SOCK_INODE(sock));
|
|
|
|
return;
|
|
|
|
}
|
2006-09-01 09:19:31 +02:00
|
|
|
sock->file = NULL;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
|
2005-04-17 00:20:36 +02:00
|
|
|
struct msghdr *msg, size_t size)
|
|
|
|
{
|
|
|
|
struct sock_iocb *si = kiocb_to_siocb(iocb);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
si->sock = sock;
|
|
|
|
si->scm = NULL;
|
|
|
|
si->msg = msg;
|
|
|
|
si->size = size;
|
|
|
|
|
|
|
|
err = security_socket_sendmsg(sock, msg, size);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return sock->ops->sendmsg(iocb, sock, msg, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
|
|
|
|
{
|
|
|
|
struct kiocb iocb;
|
|
|
|
struct sock_iocb siocb;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
init_sync_kiocb(&iocb, NULL);
|
|
|
|
iocb.private = &siocb;
|
|
|
|
ret = __sock_sendmsg(&iocb, sock, msg, size);
|
|
|
|
if (-EIOCBQUEUED == ret)
|
|
|
|
ret = wait_on_sync_kiocb(&iocb);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
|
|
|
|
struct kvec *vec, size_t num, size_t size)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs = get_fs();
|
|
|
|
int result;
|
|
|
|
|
|
|
|
set_fs(KERNEL_DS);
|
|
|
|
/*
|
|
|
|
* the following is safe, since for compiler definitions of kvec and
|
|
|
|
* iovec are identical, yielding the same in-core layout and alignment
|
|
|
|
*/
|
2006-09-01 09:19:31 +02:00
|
|
|
msg->msg_iov = (struct iovec *)vec;
|
2005-04-17 00:20:36 +02:00
|
|
|
msg->msg_iovlen = num;
|
|
|
|
result = sock_sendmsg(sock, msg, size);
|
|
|
|
set_fs(oldfs);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-03-26 07:14:49 +02:00
|
|
|
/*
|
|
|
|
* called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
|
|
|
|
*/
|
|
|
|
void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
ktime_t kt = skb->tstamp;
|
|
|
|
|
|
|
|
if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
|
|
|
|
struct timeval tv;
|
|
|
|
/* Race occurred between timestamp enabling and packet
|
|
|
|
receiving. Fill in the current time for now. */
|
|
|
|
if (kt.tv64 == 0)
|
|
|
|
kt = ktime_get_real();
|
|
|
|
skb->tstamp = kt;
|
|
|
|
tv = ktime_to_timeval(kt);
|
|
|
|
put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP, sizeof(tv), &tv);
|
|
|
|
} else {
|
|
|
|
struct timespec ts;
|
|
|
|
/* Race occurred between timestamp enabling and packet
|
|
|
|
receiving. Fill in the current time for now. */
|
|
|
|
if (kt.tv64 == 0)
|
|
|
|
kt = ktime_get_real();
|
|
|
|
skb->tstamp = kt;
|
|
|
|
ts = ktime_to_timespec(kt);
|
|
|
|
put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof(ts), &ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 04:39:35 +01:00
|
|
|
EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
|
2005-04-17 00:20:36 +02:00
|
|
|
struct msghdr *msg, size_t size, int flags)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct sock_iocb *si = kiocb_to_siocb(iocb);
|
|
|
|
|
|
|
|
si->sock = sock;
|
|
|
|
si->scm = NULL;
|
|
|
|
si->msg = msg;
|
|
|
|
si->size = size;
|
|
|
|
si->flags = flags;
|
|
|
|
|
|
|
|
err = security_socket_recvmsg(sock, msg, size, flags);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return sock->ops->recvmsg(iocb, sock, msg, size, flags);
|
|
|
|
}
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
int sock_recvmsg(struct socket *sock, struct msghdr *msg,
|
2005-04-17 00:20:36 +02:00
|
|
|
size_t size, int flags)
|
|
|
|
{
|
|
|
|
struct kiocb iocb;
|
|
|
|
struct sock_iocb siocb;
|
|
|
|
int ret;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
init_sync_kiocb(&iocb, NULL);
|
2005-04-17 00:20:36 +02:00
|
|
|
iocb.private = &siocb;
|
|
|
|
ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
|
|
|
|
if (-EIOCBQUEUED == ret)
|
|
|
|
ret = wait_on_sync_kiocb(&iocb);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
|
|
|
|
struct kvec *vec, size_t num, size_t size, int flags)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
mm_segment_t oldfs = get_fs();
|
|
|
|
int result;
|
|
|
|
|
|
|
|
set_fs(KERNEL_DS);
|
|
|
|
/*
|
|
|
|
* the following is safe, since for compiler definitions of kvec and
|
|
|
|
* iovec are identical, yielding the same in-core layout and alignment
|
|
|
|
*/
|
2006-09-01 09:19:31 +02:00
|
|
|
msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
|
2005-04-17 00:20:36 +02:00
|
|
|
result = sock_recvmsg(sock, msg, size, flags);
|
|
|
|
set_fs(oldfs);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sock_aio_dtor(struct kiocb *iocb)
|
|
|
|
{
|
|
|
|
kfree(iocb->private);
|
|
|
|
}
|
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
static ssize_t sock_sendpage(struct file *file, struct page *page,
|
|
|
|
int offset, size_t size, loff_t *ppos, int more)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct socket *sock;
|
|
|
|
int flags;
|
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
sock = file->private_data;
|
|
|
|
|
|
|
|
flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
|
|
|
|
if (more)
|
|
|
|
flags |= MSG_MORE;
|
|
|
|
|
|
|
|
return sock->ops->sendpage(sock, page, offset, size, flags);
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-11-07 08:30:13 +01:00
|
|
|
static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
|
|
|
|
struct pipe_inode_info *pipe, size_t len,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
struct socket *sock = file->private_data;
|
|
|
|
|
2008-02-15 11:35:45 +01:00
|
|
|
if (unlikely(!sock->ops->splice_read))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2007-11-07 08:30:13 +01:00
|
|
|
return sock->ops->splice_read(sock, ppos, pipe, len, flags);
|
|
|
|
}
|
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
|
2006-09-01 09:19:31 +02:00
|
|
|
struct sock_iocb *siocb)
|
2005-12-23 06:08:46 +01:00
|
|
|
{
|
|
|
|
if (!is_sync_kiocb(iocb)) {
|
|
|
|
siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
|
|
|
|
if (!siocb)
|
|
|
|
return NULL;
|
2005-04-17 00:20:36 +02:00
|
|
|
iocb->ki_dtor = sock_aio_dtor;
|
|
|
|
}
|
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
siocb->kiocb = iocb;
|
|
|
|
iocb->private = siocb;
|
|
|
|
return siocb;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
|
2006-10-01 08:28:46 +02:00
|
|
|
struct file *file, const struct iovec *iov,
|
|
|
|
unsigned long nr_segs)
|
2005-12-23 06:08:46 +01:00
|
|
|
{
|
|
|
|
struct socket *sock = file->private_data;
|
|
|
|
size_t size = 0;
|
|
|
|
int i;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
for (i = 0; i < nr_segs; i++)
|
|
|
|
size += iov[i].iov_len;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
msg->msg_name = NULL;
|
|
|
|
msg->msg_namelen = 0;
|
|
|
|
msg->msg_control = NULL;
|
|
|
|
msg->msg_controllen = 0;
|
2006-09-01 09:19:31 +02:00
|
|
|
msg->msg_iov = (struct iovec *)iov;
|
2005-12-23 06:08:46 +01:00
|
|
|
msg->msg_iovlen = nr_segs;
|
|
|
|
msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
|
|
|
|
|
|
|
|
return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
|
|
|
|
}
|
|
|
|
|
2006-10-01 08:28:46 +02:00
|
|
|
static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
|
|
|
|
unsigned long nr_segs, loff_t pos)
|
2005-12-23 06:08:46 +01:00
|
|
|
{
|
|
|
|
struct sock_iocb siocb, *x;
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
if (pos != 0)
|
|
|
|
return -ESPIPE;
|
2006-10-01 08:28:46 +02:00
|
|
|
|
|
|
|
if (iocb->ki_left == 0) /* Match SYS5 behaviour */
|
2005-04-17 00:20:36 +02:00
|
|
|
return 0;
|
|
|
|
|
2006-10-01 08:28:46 +02:00
|
|
|
|
|
|
|
x = alloc_sock_iocb(iocb, &siocb);
|
2005-12-23 06:08:46 +01:00
|
|
|
if (!x)
|
|
|
|
return -ENOMEM;
|
2006-10-01 08:28:46 +02:00
|
|
|
return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
|
2006-10-01 08:28:46 +02:00
|
|
|
struct file *file, const struct iovec *iov,
|
|
|
|
unsigned long nr_segs)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2005-12-23 06:08:46 +01:00
|
|
|
struct socket *sock = file->private_data;
|
|
|
|
size_t size = 0;
|
|
|
|
int i;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
for (i = 0; i < nr_segs; i++)
|
|
|
|
size += iov[i].iov_len;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
msg->msg_name = NULL;
|
|
|
|
msg->msg_namelen = 0;
|
|
|
|
msg->msg_control = NULL;
|
|
|
|
msg->msg_controllen = 0;
|
2006-09-01 09:19:31 +02:00
|
|
|
msg->msg_iov = (struct iovec *)iov;
|
2005-12-23 06:08:46 +01:00
|
|
|
msg->msg_iovlen = nr_segs;
|
|
|
|
msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
|
|
|
|
if (sock->type == SOCK_SEQPACKET)
|
|
|
|
msg->msg_flags |= MSG_EOR;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
return __sock_sendmsg(iocb, sock, msg, size);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2006-10-01 08:28:46 +02:00
|
|
|
static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
|
|
|
|
unsigned long nr_segs, loff_t pos)
|
2005-12-23 06:08:46 +01:00
|
|
|
{
|
|
|
|
struct sock_iocb siocb, *x;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2005-12-23 06:08:46 +01:00
|
|
|
if (pos != 0)
|
|
|
|
return -ESPIPE;
|
2006-10-01 08:28:46 +02:00
|
|
|
|
|
|
|
x = alloc_sock_iocb(iocb, &siocb);
|
2005-12-23 06:08:46 +01:00
|
|
|
if (!x)
|
|
|
|
return -ENOMEM;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-10-01 08:28:46 +02:00
|
|
|
return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Atomic setting of ioctl hooks to avoid race
|
|
|
|
* with module unload.
|
|
|
|
*/
|
|
|
|
|
2006-03-21 07:33:17 +01:00
|
|
|
static DEFINE_MUTEX(br_ioctl_mutex);
|
2007-09-17 20:56:21 +02:00
|
|
|
static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-09-17 20:56:21 +02:00
|
|
|
void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_lock(&br_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
br_ioctl_hook = hook;
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_unlock(&br_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
EXPORT_SYMBOL(brioctl_set);
|
|
|
|
|
2006-03-21 07:33:17 +01:00
|
|
|
static DEFINE_MUTEX(vlan_ioctl_mutex);
|
2007-09-17 20:56:21 +02:00
|
|
|
static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-09-17 20:56:21 +02:00
|
|
|
void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_lock(&vlan_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
vlan_ioctl_hook = hook;
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_unlock(&vlan_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
EXPORT_SYMBOL(vlan_ioctl_set);
|
|
|
|
|
2006-03-21 07:33:17 +01:00
|
|
|
static DEFINE_MUTEX(dlci_ioctl_mutex);
|
2006-09-01 09:19:31 +02:00
|
|
|
static int (*dlci_ioctl_hook) (unsigned int, void __user *);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_lock(&dlci_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
dlci_ioctl_hook = hook;
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_unlock(&dlci_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
EXPORT_SYMBOL(dlci_ioctl_set);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* With an ioctl, arg may well be a user mode pointer, but we don't know
|
|
|
|
* what to do with it - that's up to the protocol still.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct socket *sock;
|
2007-09-17 20:56:21 +02:00
|
|
|
struct sock *sk;
|
2005-04-17 00:20:36 +02:00
|
|
|
void __user *argp = (void __user *)arg;
|
|
|
|
int pid, err;
|
2007-09-17 20:56:21 +02:00
|
|
|
struct net *net;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2005-09-06 23:42:45 +02:00
|
|
|
sock = file->private_data;
|
2007-09-17 20:56:21 +02:00
|
|
|
sk = sock->sk;
|
2008-03-25 18:26:21 +01:00
|
|
|
net = sock_net(sk);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
|
2007-09-17 20:56:21 +02:00
|
|
|
err = dev_ioctl(net, cmd, argp);
|
2005-04-17 00:20:36 +02:00
|
|
|
} else
|
2006-01-21 00:46:55 +01:00
|
|
|
#ifdef CONFIG_WIRELESS_EXT
|
2005-04-17 00:20:36 +02:00
|
|
|
if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
|
2007-09-17 20:56:21 +02:00
|
|
|
err = dev_ioctl(net, cmd, argp);
|
2005-04-17 00:20:36 +02:00
|
|
|
} else
|
2006-09-01 09:19:31 +02:00
|
|
|
#endif /* CONFIG_WIRELESS_EXT */
|
|
|
|
switch (cmd) {
|
2005-04-17 00:20:36 +02:00
|
|
|
case FIOSETOWN:
|
|
|
|
case SIOCSPGRP:
|
|
|
|
err = -EFAULT;
|
|
|
|
if (get_user(pid, (int __user *)argp))
|
|
|
|
break;
|
|
|
|
err = f_setown(sock->file, pid, 1);
|
|
|
|
break;
|
|
|
|
case FIOGETOWN:
|
|
|
|
case SIOCGPGRP:
|
2006-10-02 11:17:15 +02:00
|
|
|
err = put_user(f_getown(sock->file),
|
2006-09-01 09:19:31 +02:00
|
|
|
(int __user *)argp);
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
case SIOCGIFBR:
|
|
|
|
case SIOCSIFBR:
|
|
|
|
case SIOCBRADDBR:
|
|
|
|
case SIOCBRDELBR:
|
|
|
|
err = -ENOPKG;
|
|
|
|
if (!br_ioctl_hook)
|
|
|
|
request_module("bridge");
|
|
|
|
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_lock(&br_ioctl_mutex);
|
2006-09-01 09:19:31 +02:00
|
|
|
if (br_ioctl_hook)
|
2007-09-17 20:56:21 +02:00
|
|
|
err = br_ioctl_hook(net, cmd, argp);
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_unlock(&br_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
case SIOCGIFVLAN:
|
|
|
|
case SIOCSIFVLAN:
|
|
|
|
err = -ENOPKG;
|
|
|
|
if (!vlan_ioctl_hook)
|
|
|
|
request_module("8021q");
|
|
|
|
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_lock(&vlan_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (vlan_ioctl_hook)
|
2007-09-17 20:56:21 +02:00
|
|
|
err = vlan_ioctl_hook(net, argp);
|
2006-03-21 07:33:17 +01:00
|
|
|
mutex_unlock(&vlan_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
case SIOCADDDLCI:
|
|
|
|
case SIOCDELDLCI:
|
|
|
|
err = -ENOPKG;
|
|
|
|
if (!dlci_ioctl_hook)
|
|
|
|
request_module("dlci");
|
|
|
|
|
2008-03-21 23:58:52 +01:00
|
|
|
mutex_lock(&dlci_ioctl_mutex);
|
|
|
|
if (dlci_ioctl_hook)
|
2005-04-17 00:20:36 +02:00
|
|
|
err = dlci_ioctl_hook(cmd, argp);
|
2008-03-21 23:58:52 +01:00
|
|
|
mutex_unlock(&dlci_ioctl_mutex);
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = sock->ops->ioctl(sock, cmd, arg);
|
2006-01-03 23:18:33 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this ioctl is unknown try to hand it down
|
|
|
|
* to the NIC driver.
|
|
|
|
*/
|
|
|
|
if (err == -ENOIOCTLCMD)
|
2007-09-17 20:56:21 +02:00
|
|
|
err = dev_ioctl(net, cmd, argp);
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
2006-09-01 09:19:31 +02:00
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_create_lite(int family, int type, int protocol, struct socket **res)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct socket *sock = NULL;
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
err = security_socket_create(family, type, protocol, 1);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
sock = sock_alloc();
|
|
|
|
if (!sock) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock->type = type;
|
2006-08-05 08:17:57 +02:00
|
|
|
err = security_socket_post_create(sock, family, type, protocol, 1);
|
|
|
|
if (err)
|
|
|
|
goto out_release;
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
out:
|
|
|
|
*res = sock;
|
|
|
|
return err;
|
2006-08-05 08:17:57 +02:00
|
|
|
out_release:
|
|
|
|
sock_release(sock);
|
|
|
|
sock = NULL;
|
|
|
|
goto out;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No kernel lock held - perfect */
|
2006-09-01 09:19:31 +02:00
|
|
|
static unsigned int sock_poll(struct file *file, poll_table *wait)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct socket *sock;
|
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* We can't return errors to poll, so it's either yes or no.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
2005-09-06 23:42:45 +02:00
|
|
|
sock = file->private_data;
|
2005-04-17 00:20:36 +02:00
|
|
|
return sock->ops->poll(file, sock, wait);
|
|
|
|
}
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
static int sock_mmap(struct file *file, struct vm_area_struct *vma)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2005-09-06 23:42:45 +02:00
|
|
|
struct socket *sock = file->private_data;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
return sock->ops->mmap(file, sock, vma);
|
|
|
|
}
|
|
|
|
|
2005-08-16 07:18:02 +02:00
|
|
|
static int sock_close(struct inode *inode, struct file *filp)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* It was possible the inode is NULL we were
|
|
|
|
* closing an unfinished socket.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
if (!inode) {
|
2005-04-17 00:20:36 +02:00
|
|
|
printk(KERN_DEBUG "sock_close: NULL inode\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sock_release(SOCKET_I(inode));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the socket async list
|
|
|
|
*
|
|
|
|
* Fasync_list locking strategy.
|
|
|
|
*
|
|
|
|
* 1. fasync_list is modified only under process context socket lock
|
|
|
|
* i.e. under semaphore.
|
|
|
|
* 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
|
|
|
|
* or under socket lock.
|
|
|
|
* 3. fasync_list can be used from softirq context, so that
|
|
|
|
* modification under socket lock have to be enhanced with
|
|
|
|
* write_lock_bh(&sk->sk_callback_lock).
|
|
|
|
* --ANK (990710)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int sock_fasync(int fd, struct file *filp, int on)
|
|
|
|
{
|
2006-09-01 09:19:31 +02:00
|
|
|
struct fasync_struct *fa, *fna = NULL, **prev;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct socket *sock;
|
|
|
|
struct sock *sk;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
if (on) {
|
2006-01-12 00:56:43 +01:00
|
|
|
fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
|
2006-09-01 09:19:31 +02:00
|
|
|
if (fna == NULL)
|
2005-04-17 00:20:36 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2005-09-06 23:42:45 +02:00
|
|
|
sock = filp->private_data;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
sk = sock->sk;
|
|
|
|
if (sk == NULL) {
|
2005-04-17 00:20:36 +02:00
|
|
|
kfree(fna);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock_sock(sk);
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
prev = &(sock->fasync_list);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
|
|
|
|
if (fa->fa_file == filp)
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
if (on) {
|
|
|
|
if (fa != NULL) {
|
2005-04-17 00:20:36 +02:00
|
|
|
write_lock_bh(&sk->sk_callback_lock);
|
2006-09-01 09:19:31 +02:00
|
|
|
fa->fa_fd = fd;
|
2005-04-17 00:20:36 +02:00
|
|
|
write_unlock_bh(&sk->sk_callback_lock);
|
|
|
|
|
|
|
|
kfree(fna);
|
|
|
|
goto out;
|
|
|
|
}
|
2006-09-01 09:19:31 +02:00
|
|
|
fna->fa_file = filp;
|
|
|
|
fna->fa_fd = fd;
|
|
|
|
fna->magic = FASYNC_MAGIC;
|
|
|
|
fna->fa_next = sock->fasync_list;
|
2005-04-17 00:20:36 +02:00
|
|
|
write_lock_bh(&sk->sk_callback_lock);
|
2006-09-01 09:19:31 +02:00
|
|
|
sock->fasync_list = fna;
|
2005-04-17 00:20:36 +02:00
|
|
|
write_unlock_bh(&sk->sk_callback_lock);
|
2006-09-01 09:19:31 +02:00
|
|
|
} else {
|
|
|
|
if (fa != NULL) {
|
2005-04-17 00:20:36 +02:00
|
|
|
write_lock_bh(&sk->sk_callback_lock);
|
2006-09-01 09:19:31 +02:00
|
|
|
*prev = fa->fa_next;
|
2005-04-17 00:20:36 +02:00
|
|
|
write_unlock_bh(&sk->sk_callback_lock);
|
|
|
|
kfree(fa);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
release_sock(sock->sk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function may be called only under socket lock or callback_lock */
|
|
|
|
|
|
|
|
int sock_wake_async(struct socket *sock, int how, int band)
|
|
|
|
{
|
|
|
|
if (!sock || !sock->fasync_list)
|
|
|
|
return -1;
|
2006-09-01 09:19:31 +02:00
|
|
|
switch (how) {
|
2007-11-26 13:10:50 +01:00
|
|
|
case SOCK_WAKE_WAITD:
|
2005-04-17 00:20:36 +02:00
|
|
|
if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
|
|
|
|
break;
|
|
|
|
goto call_kill;
|
2007-11-26 13:10:50 +01:00
|
|
|
case SOCK_WAKE_SPACE:
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
|
|
|
|
break;
|
|
|
|
/* fall through */
|
2007-11-26 13:10:50 +01:00
|
|
|
case SOCK_WAKE_IO:
|
2006-09-01 09:19:31 +02:00
|
|
|
call_kill:
|
2005-04-17 00:20:36 +02:00
|
|
|
__kill_fasync(sock->fasync_list, SIGIO, band);
|
|
|
|
break;
|
2007-11-26 13:10:50 +01:00
|
|
|
case SOCK_WAKE_URG:
|
2005-04-17 00:20:36 +02:00
|
|
|
__kill_fasync(sock->fasync_list, SIGURG, band);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-09 08:24:22 +02:00
|
|
|
static int __sock_create(struct net *net, int family, int type, int protocol,
|
2006-09-01 09:19:31 +02:00
|
|
|
struct socket **res, int kern)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct socket *sock;
|
2006-09-01 09:23:39 +02:00
|
|
|
const struct net_proto_family *pf;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* Check protocol is in range
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
if (family < 0 || family >= NPROTO)
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
if (type < 0 || type >= SOCK_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Compatibility.
|
|
|
|
|
|
|
|
This uglymoron is moved from INET layer to here to avoid
|
|
|
|
deadlock in module load.
|
|
|
|
*/
|
|
|
|
if (family == PF_INET && type == SOCK_PACKET) {
|
2006-09-01 09:19:31 +02:00
|
|
|
static int warned;
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!warned) {
|
|
|
|
warned = 1;
|
2006-09-01 09:19:31 +02:00
|
|
|
printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
|
|
|
|
current->comm);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
family = PF_PACKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = security_socket_create(family, type, protocol, kern);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2006-09-01 09:23:39 +02:00
|
|
|
/*
|
|
|
|
* Allocate the socket and allow the family to set things up. if
|
|
|
|
* the protocol is 0, the family is instructed to select an appropriate
|
|
|
|
* default.
|
|
|
|
*/
|
|
|
|
sock = sock_alloc();
|
|
|
|
if (!sock) {
|
|
|
|
if (net_ratelimit())
|
|
|
|
printk(KERN_WARNING "socket: no more sockets\n");
|
|
|
|
return -ENFILE; /* Not exactly a match, but its the
|
|
|
|
closest posix thing */
|
|
|
|
}
|
|
|
|
|
|
|
|
sock->type = type;
|
|
|
|
|
2008-10-17 00:24:51 +02:00
|
|
|
#ifdef CONFIG_MODULES
|
2006-09-01 09:19:31 +02:00
|
|
|
/* Attempt to load a protocol module if the find failed.
|
|
|
|
*
|
|
|
|
* 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
|
2005-04-17 00:20:36 +02:00
|
|
|
* requested real, full-featured networking support upon configuration.
|
|
|
|
* Otherwise module support will break!
|
|
|
|
*/
|
2006-09-01 09:23:39 +02:00
|
|
|
if (net_families[family] == NULL)
|
2006-09-01 09:19:31 +02:00
|
|
|
request_module("net-pf-%d", family);
|
2005-04-17 00:20:36 +02:00
|
|
|
#endif
|
|
|
|
|
2006-09-01 09:23:39 +02:00
|
|
|
rcu_read_lock();
|
|
|
|
pf = rcu_dereference(net_families[family]);
|
|
|
|
err = -EAFNOSUPPORT;
|
|
|
|
if (!pf)
|
|
|
|
goto out_release;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We will call the ->create function, that possibly is in a loadable
|
|
|
|
* module, so we have to bump that loadable module refcnt first.
|
|
|
|
*/
|
2006-09-01 09:23:39 +02:00
|
|
|
if (!try_module_get(pf->owner))
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out_release;
|
|
|
|
|
2006-09-01 09:23:39 +02:00
|
|
|
/* Now protected by module ref count */
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
2007-10-09 08:24:22 +02:00
|
|
|
err = pf->create(net, sock, protocol);
|
2006-09-01 09:23:39 +02:00
|
|
|
if (err < 0)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out_module_put;
|
2005-09-28 00:23:38 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* Now to bump the refcnt of the [loadable] module that owns this
|
|
|
|
* socket at sock_release time we decrement its refcnt.
|
|
|
|
*/
|
2006-09-01 09:23:39 +02:00
|
|
|
if (!try_module_get(sock->ops->owner))
|
|
|
|
goto out_module_busy;
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* Now that we're done with the ->create function, the [loadable]
|
|
|
|
* module can have its refcnt decremented
|
|
|
|
*/
|
2006-09-01 09:23:39 +02:00
|
|
|
module_put(pf->owner);
|
2006-08-05 08:17:57 +02:00
|
|
|
err = security_socket_post_create(sock, family, type, protocol, kern);
|
|
|
|
if (err)
|
2007-08-15 23:46:02 +02:00
|
|
|
goto out_sock_release;
|
2006-09-01 09:23:39 +02:00
|
|
|
*res = sock;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:23:39 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_module_busy:
|
|
|
|
err = -EAFNOSUPPORT;
|
2005-04-17 00:20:36 +02:00
|
|
|
out_module_put:
|
2006-09-01 09:23:39 +02:00
|
|
|
sock->ops = NULL;
|
|
|
|
module_put(pf->owner);
|
|
|
|
out_sock_release:
|
2005-04-17 00:20:36 +02:00
|
|
|
sock_release(sock);
|
2006-09-01 09:23:39 +02:00
|
|
|
return err;
|
|
|
|
|
|
|
|
out_release:
|
|
|
|
rcu_read_unlock();
|
|
|
|
goto out_sock_release;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int sock_create(int family, int type, int protocol, struct socket **res)
|
|
|
|
{
|
2007-10-09 08:24:22 +02:00
|
|
|
return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int sock_create_kern(int family, int type, int protocol, struct socket **res)
|
|
|
|
{
|
2007-10-09 08:24:22 +02:00
|
|
|
return __sock_create(&init_net, family, type, protocol, res, 1);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
asmlinkage long sys_socket(int family, int type, int protocol)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
struct socket *sock;
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
int flags;
|
|
|
|
|
2008-07-24 06:29:42 +02:00
|
|
|
/* Check the SOCK_* constants for consistency. */
|
|
|
|
BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
|
|
|
|
BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
|
|
|
|
BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
|
|
|
|
BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
|
|
|
|
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
flags = type & ~SOCK_TYPE_MASK;
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
return -EINVAL;
|
|
|
|
type &= SOCK_TYPE_MASK;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
|
|
|
|
flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
retval = sock_create(family, type, protocol, &sock);
|
|
|
|
if (retval < 0)
|
|
|
|
goto out;
|
|
|
|
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
|
2005-04-17 00:20:36 +02:00
|
|
|
if (retval < 0)
|
|
|
|
goto out_release;
|
|
|
|
|
|
|
|
out:
|
|
|
|
/* It may be already another descriptor 8) Not kernel problem. */
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
out_release:
|
|
|
|
sock_release(sock);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a pair of connected sockets.
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_socketpair(int family, int type, int protocol,
|
|
|
|
int __user *usockvec)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct socket *sock1, *sock2;
|
|
|
|
int fd1, fd2, err;
|
2007-02-07 07:48:00 +01:00
|
|
|
struct file *newfile1, *newfile2;
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = type & ~SOCK_TYPE_MASK;
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
return -EINVAL;
|
|
|
|
type &= SOCK_TYPE_MASK;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
|
|
|
|
flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* Obtain the first socket and check if the underlying protocol
|
|
|
|
* supports the socketpair call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
err = sock_create(family, type, protocol, &sock1);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = sock_create(family, type, protocol, &sock2);
|
|
|
|
if (err < 0)
|
|
|
|
goto out_release_1;
|
|
|
|
|
|
|
|
err = sock1->ops->socketpair(sock1, sock2);
|
2006-09-01 09:19:31 +02:00
|
|
|
if (err < 0)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out_release_both;
|
|
|
|
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
|
2007-10-30 05:54:02 +01:00
|
|
|
if (unlikely(fd1 < 0)) {
|
|
|
|
err = fd1;
|
2007-02-07 07:48:00 +01:00
|
|
|
goto out_release_both;
|
2007-10-30 05:54:02 +01:00
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:17 +02:00
|
|
|
fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
|
2007-02-07 07:48:00 +01:00
|
|
|
if (unlikely(fd2 < 0)) {
|
2007-10-30 05:54:02 +01:00
|
|
|
err = fd2;
|
2007-02-07 07:48:00 +01:00
|
|
|
put_filp(newfile1);
|
|
|
|
put_unused_fd(fd1);
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out_release_both;
|
2007-02-07 07:48:00 +01:00
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
|
2007-02-07 07:48:00 +01:00
|
|
|
if (unlikely(err < 0)) {
|
|
|
|
goto out_fd2;
|
|
|
|
}
|
|
|
|
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
|
2007-02-07 07:48:00 +01:00
|
|
|
if (unlikely(err < 0)) {
|
|
|
|
fput(newfile1);
|
|
|
|
goto out_fd1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = audit_fd_pair(fd1, fd2);
|
|
|
|
if (err < 0) {
|
|
|
|
fput(newfile1);
|
|
|
|
fput(newfile2);
|
|
|
|
goto out_fd;
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-02-07 07:48:00 +01:00
|
|
|
fd_install(fd1, newfile1);
|
|
|
|
fd_install(fd2, newfile2);
|
2005-04-17 00:20:36 +02:00
|
|
|
/* fd1 and fd2 may be already another descriptors.
|
|
|
|
* Not kernel problem.
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
err = put_user(fd1, &usockvec[0]);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!err)
|
|
|
|
err = put_user(fd2, &usockvec[1]);
|
|
|
|
if (!err)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
sys_close(fd2);
|
|
|
|
sys_close(fd1);
|
|
|
|
return err;
|
|
|
|
|
|
|
|
out_release_both:
|
2006-09-01 09:19:31 +02:00
|
|
|
sock_release(sock2);
|
2005-04-17 00:20:36 +02:00
|
|
|
out_release_1:
|
2006-09-01 09:19:31 +02:00
|
|
|
sock_release(sock1);
|
2005-04-17 00:20:36 +02:00
|
|
|
out:
|
|
|
|
return err;
|
2007-02-07 07:48:00 +01:00
|
|
|
|
|
|
|
out_fd2:
|
|
|
|
put_filp(newfile1);
|
|
|
|
sock_release(sock1);
|
|
|
|
out_fd1:
|
|
|
|
put_filp(newfile2);
|
|
|
|
sock_release(sock2);
|
|
|
|
out_fd:
|
|
|
|
put_unused_fd(fd1);
|
|
|
|
put_unused_fd(fd2);
|
|
|
|
goto out;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind a name to a socket. Nothing much to do here since it's
|
|
|
|
* the protocol's responsibility to handle the local address.
|
|
|
|
*
|
|
|
|
* We move the socket address to kernel space before we call
|
|
|
|
* the protocol layer (having also checked the address is ok).
|
|
|
|
*/
|
|
|
|
|
|
|
|
asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
|
|
|
|
{
|
|
|
|
struct socket *sock;
|
2008-07-20 07:35:47 +02:00
|
|
|
struct sockaddr_storage address;
|
2006-03-21 07:27:12 +01:00
|
|
|
int err, fput_needed;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
2007-04-11 05:10:33 +02:00
|
|
|
if (sock) {
|
2008-07-20 07:35:47 +02:00
|
|
|
err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
|
2006-09-01 09:19:31 +02:00
|
|
|
if (err >= 0) {
|
|
|
|
err = security_socket_bind(sock,
|
2008-07-20 07:35:47 +02:00
|
|
|
(struct sockaddr *)&address,
|
2006-09-01 09:19:31 +02:00
|
|
|
addrlen);
|
2006-03-21 07:27:12 +01:00
|
|
|
if (!err)
|
|
|
|
err = sock->ops->bind(sock,
|
2006-09-01 09:19:31 +02:00
|
|
|
(struct sockaddr *)
|
2008-07-20 07:35:47 +02:00
|
|
|
&address, addrlen);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-03-21 07:27:12 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2006-09-01 09:19:31 +02:00
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a listen. Basically, we allow the protocol to do anything
|
|
|
|
* necessary for a listen, and if that works, we mark the socket as
|
|
|
|
* ready for listening.
|
|
|
|
*/
|
|
|
|
|
|
|
|
asmlinkage long sys_listen(int fd, int backlog)
|
|
|
|
{
|
|
|
|
struct socket *sock;
|
2006-03-21 07:27:12 +01:00
|
|
|
int err, fput_needed;
|
2007-12-08 09:12:33 +01:00
|
|
|
int somaxconn;
|
2006-09-01 09:19:31 +02:00
|
|
|
|
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
|
|
|
if (sock) {
|
2008-04-01 04:41:14 +02:00
|
|
|
somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
|
2007-12-08 09:12:33 +01:00
|
|
|
if ((unsigned)backlog > somaxconn)
|
|
|
|
backlog = somaxconn;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
err = security_socket_listen(sock, backlog);
|
2006-03-21 07:27:12 +01:00
|
|
|
if (!err)
|
|
|
|
err = sock->ops->listen(sock, backlog);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-03-21 07:27:12 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For accept, we attempt to create a new socket, set up the link
|
|
|
|
* with the client, wake up the client, then return the new
|
|
|
|
* connected fd. We collect the address of the connector in kernel
|
|
|
|
* space and move it to user at the very end. This is unclean because
|
|
|
|
* we open the socket then return an error.
|
|
|
|
*
|
|
|
|
* 1003.1g adds the ability to recvmsg() to query connection pending
|
|
|
|
* status to recvmsg. We need to add that support in a way thats
|
|
|
|
* clean when we restucture accept also.
|
|
|
|
*/
|
|
|
|
|
reintroduce accept4
Introduce a new accept4() system call. The addition of this system call
matches analogous changes in 2.6.27 (dup3(), evenfd2(), signalfd4(),
inotify_init1(), epoll_create1(), pipe2()) which added new system calls
that differed from analogous traditional system calls in adding a flags
argument that can be used to access additional functionality.
The accept4() system call is exactly the same as accept(), except that
it adds a flags bit-mask argument. Two flags are initially implemented.
(Most of the new system calls in 2.6.27 also had both of these flags.)
SOCK_CLOEXEC causes the close-on-exec (FD_CLOEXEC) flag to be enabled
for the new file descriptor returned by accept4(). This is a useful
security feature to avoid leaking information in a multithreaded
program where one thread is doing an accept() at the same time as
another thread is doing a fork() plus exec(). More details here:
http://udrepper.livejournal.com/20407.html "Secure File Descriptor Handling",
Ulrich Drepper).
The other flag is SOCK_NONBLOCK, which causes the O_NONBLOCK flag
to be enabled on the new open file description created by accept4().
(This flag is merely a convenience, saving the use of additional calls
fcntl(F_GETFL) and fcntl (F_SETFL) to achieve the same result.
Here's a test program. Works on x86-32. Should work on x86-64, but
I (mtk) don't have a system to hand to test with.
It tests accept4() with each of the four possible combinations of
SOCK_CLOEXEC and SOCK_NONBLOCK set/clear in 'flags', and verifies
that the appropriate flags are set on the file descriptor/open file
description returned by accept4().
I tested Ulrich's patch in this thread by applying against 2.6.28-rc2,
and it passes according to my test program.
/* test_accept4.c
Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
<mtk.manpages@gmail.com>
Licensed under the GNU GPLv2 or later.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define PORT_NUM 33333
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/**********************************************************************/
/* The following is what we need until glibc gets a wrapper for
accept4() */
/* Flags for socket(), socketpair(), accept4() */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC O_CLOEXEC
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifdef __x86_64__
#define SYS_accept4 288
#elif __i386__
#define USE_SOCKETCALL 1
#define SYS_ACCEPT4 18
#else
#error "Sorry -- don't know the syscall # on this architecture"
#endif
static int
accept4(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
{
printf("Calling accept4(): flags = %x", flags);
if (flags != 0) {
printf(" (");
if (flags & SOCK_CLOEXEC)
printf("SOCK_CLOEXEC");
if ((flags & SOCK_CLOEXEC) && (flags & SOCK_NONBLOCK))
printf(" ");
if (flags & SOCK_NONBLOCK)
printf("SOCK_NONBLOCK");
printf(")");
}
printf("\n");
#if USE_SOCKETCALL
long args[6];
args[0] = fd;
args[1] = (long) sockaddr;
args[2] = (long) addrlen;
args[3] = flags;
return syscall(SYS_socketcall, SYS_ACCEPT4, args);
#else
return syscall(SYS_accept4, fd, sockaddr, addrlen, flags);
#endif
}
/**********************************************************************/
static int
do_test(int lfd, struct sockaddr_in *conn_addr,
int closeonexec_flag, int nonblock_flag)
{
int connfd, acceptfd;
int fdf, flf, fdf_pass, flf_pass;
struct sockaddr_in claddr;
socklen_t addrlen;
printf("=======================================\n");
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
die("socket");
if (connect(connfd, (struct sockaddr *) conn_addr,
sizeof(struct sockaddr_in)) == -1)
die("connect");
addrlen = sizeof(struct sockaddr_in);
acceptfd = accept4(lfd, (struct sockaddr *) &claddr, &addrlen,
closeonexec_flag | nonblock_flag);
if (acceptfd == -1) {
perror("accept4()");
close(connfd);
return 0;
}
fdf = fcntl(acceptfd, F_GETFD);
if (fdf == -1)
die("fcntl:F_GETFD");
fdf_pass = ((fdf & FD_CLOEXEC) != 0) ==
((closeonexec_flag & SOCK_CLOEXEC) != 0);
printf("Close-on-exec flag is %sset (%s); ",
(fdf & FD_CLOEXEC) ? "" : "not ",
fdf_pass ? "OK" : "failed");
flf = fcntl(acceptfd, F_GETFL);
if (flf == -1)
die("fcntl:F_GETFD");
flf_pass = ((flf & O_NONBLOCK) != 0) ==
((nonblock_flag & SOCK_NONBLOCK) !=0);
printf("nonblock flag is %sset (%s)\n",
(flf & O_NONBLOCK) ? "" : "not ",
flf_pass ? "OK" : "failed");
close(acceptfd);
close(connfd);
printf("Test result: %s\n", (fdf_pass && flf_pass) ? "PASS" : "FAIL");
return fdf_pass && flf_pass;
}
static int
create_listening_socket(int port_num)
{
struct sockaddr_in svaddr;
int lfd;
int optval;
memset(&svaddr, 0, sizeof(struct sockaddr_in));
svaddr.sin_family = AF_INET;
svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
svaddr.sin_port = htons(port_num);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
die("socket");
optval = 1;
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) == -1)
die("setsockopt");
if (bind(lfd, (struct sockaddr *) &svaddr,
sizeof(struct sockaddr_in)) == -1)
die("bind");
if (listen(lfd, 5) == -1)
die("listen");
return lfd;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in conn_addr;
int lfd;
int port_num;
int passed;
passed = 1;
port_num = (argc > 1) ? atoi(argv[1]) : PORT_NUM;
memset(&conn_addr, 0, sizeof(struct sockaddr_in));
conn_addr.sin_family = AF_INET;
conn_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
conn_addr.sin_port = htons(port_num);
lfd = create_listening_socket(port_num);
if (!do_test(lfd, &conn_addr, 0, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, 0, SOCK_NONBLOCK))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, SOCK_NONBLOCK))
passed = 0;
close(lfd);
exit(passed ? EXIT_SUCCESS : EXIT_FAILURE);
}
[mtk.manpages@gmail.com: rewrote changelog, updated test program]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Tested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <linux-api@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-11-20 00:36:14 +01:00
|
|
|
asmlinkage long sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
|
|
|
|
int __user *upeer_addrlen, int flags)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct socket *sock, *newsock;
|
2006-03-21 02:13:49 +01:00
|
|
|
struct file *newfile;
|
2006-03-21 07:27:12 +01:00
|
|
|
int err, len, newfd, fput_needed;
|
2008-07-20 07:35:47 +02:00
|
|
|
struct sockaddr_storage address;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
|
|
|
|
flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
|
|
|
|
|
2006-03-21 07:27:12 +01:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!sock)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = -ENFILE;
|
2006-09-01 09:19:31 +02:00
|
|
|
if (!(newsock = sock_alloc()))
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out_put;
|
|
|
|
|
|
|
|
newsock->type = sock->type;
|
|
|
|
newsock->ops = sock->ops;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't need try_module_get here, as the listening socket (sock)
|
|
|
|
* has the protocol module (sock->ops->owner) held.
|
|
|
|
*/
|
|
|
|
__module_get(newsock->ops->owner);
|
|
|
|
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
|
2006-03-21 02:13:49 +01:00
|
|
|
if (unlikely(newfd < 0)) {
|
|
|
|
err = newfd;
|
2006-04-01 22:48:36 +02:00
|
|
|
sock_release(newsock);
|
|
|
|
goto out_put;
|
2006-03-21 02:13:49 +01:00
|
|
|
}
|
|
|
|
|
flag parameters: NONBLOCK in socket and socketpair
This patch introduces support for the SOCK_NONBLOCK flag in socket,
socketpair, and paccept. To do this the internal function sock_attach_fd
gets an additional parameter which it uses to set the appropriate flag for
the file descriptor.
Given that in modern, scalable programs almost all socket connections are
non-blocking and the minimal additional cost for the new functionality
I see no reason not to add this code.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_NONBLOCK O_NONBLOCK
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
return NULL;
}
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
puts ("socket(0) set non-blocking mode");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
if (fd == -1)
{
puts ("socket(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (fd, F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
puts ("socket(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if (fl & O_NONBLOCK)
{
printf ("socketpair(0) set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fds) == -1)
{
puts ("socketpair(SOCK_NONBLOCK) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
fl = fcntl (fds[i], F_GETFL);
if (fl == -1)
{
puts ("fcntl failed");
return 1;
}
if ((fl & O_NONBLOCK) == 0)
{
printf ("socketpair(SOCK_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if (fl & O_NONBLOCK)
{
puts ("paccept(0) set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_NONBLOCK);
if (s2 < 0)
{
puts ("paccept(SOCK_NONBLOCK) failed");
return 1;
}
fl = fcntl (s2, F_GETFL);
if ((fl & O_NONBLOCK) == 0)
{
puts ("paccept(SOCK_NONBLOCK) does not set non-blocking mode");
return 1;
}
close (s2);
close (s);
pthread_barrier_wait (&b);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:35 +02:00
|
|
|
err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
|
2006-03-21 02:13:49 +01:00
|
|
|
if (err < 0)
|
2007-03-26 23:09:52 +02:00
|
|
|
goto out_fd_simple;
|
2006-03-21 02:13:49 +01:00
|
|
|
|
2005-09-28 00:23:38 +02:00
|
|
|
err = security_socket_accept(sock, newsock);
|
|
|
|
if (err)
|
2006-03-21 02:13:49 +01:00
|
|
|
goto out_fd;
|
2005-09-28 00:23:38 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
err = sock->ops->accept(sock, newsock, sock->file->f_flags);
|
|
|
|
if (err < 0)
|
2006-03-21 02:13:49 +01:00
|
|
|
goto out_fd;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (upeer_sockaddr) {
|
2008-07-20 07:35:47 +02:00
|
|
|
if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
|
2006-09-01 09:19:31 +02:00
|
|
|
&len, 2) < 0) {
|
2005-04-17 00:20:36 +02:00
|
|
|
err = -ECONNABORTED;
|
2006-03-21 02:13:49 +01:00
|
|
|
goto out_fd;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2008-07-20 07:35:47 +02:00
|
|
|
err = move_addr_to_user((struct sockaddr *)&address,
|
|
|
|
len, upeer_sockaddr, upeer_addrlen);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (err < 0)
|
2006-03-21 02:13:49 +01:00
|
|
|
goto out_fd;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* File flags are not inherited via accept() unlike another OSes. */
|
|
|
|
|
2006-03-21 02:13:49 +01:00
|
|
|
fd_install(newfd, newfile);
|
|
|
|
err = newfd;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
security_socket_post_accept(sock, newsock);
|
|
|
|
|
|
|
|
out_put:
|
2006-03-21 07:27:12 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
out:
|
|
|
|
return err;
|
2007-03-26 23:09:52 +02:00
|
|
|
out_fd_simple:
|
|
|
|
sock_release(newsock);
|
|
|
|
put_filp(newfile);
|
|
|
|
put_unused_fd(newfd);
|
|
|
|
goto out_put;
|
2006-03-21 02:13:49 +01:00
|
|
|
out_fd:
|
2006-04-01 11:00:14 +02:00
|
|
|
fput(newfile);
|
2006-03-21 02:13:49 +01:00
|
|
|
put_unused_fd(newfd);
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out_put;
|
|
|
|
}
|
|
|
|
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
|
|
|
|
int __user *upeer_addrlen)
|
|
|
|
{
|
reintroduce accept4
Introduce a new accept4() system call. The addition of this system call
matches analogous changes in 2.6.27 (dup3(), evenfd2(), signalfd4(),
inotify_init1(), epoll_create1(), pipe2()) which added new system calls
that differed from analogous traditional system calls in adding a flags
argument that can be used to access additional functionality.
The accept4() system call is exactly the same as accept(), except that
it adds a flags bit-mask argument. Two flags are initially implemented.
(Most of the new system calls in 2.6.27 also had both of these flags.)
SOCK_CLOEXEC causes the close-on-exec (FD_CLOEXEC) flag to be enabled
for the new file descriptor returned by accept4(). This is a useful
security feature to avoid leaking information in a multithreaded
program where one thread is doing an accept() at the same time as
another thread is doing a fork() plus exec(). More details here:
http://udrepper.livejournal.com/20407.html "Secure File Descriptor Handling",
Ulrich Drepper).
The other flag is SOCK_NONBLOCK, which causes the O_NONBLOCK flag
to be enabled on the new open file description created by accept4().
(This flag is merely a convenience, saving the use of additional calls
fcntl(F_GETFL) and fcntl (F_SETFL) to achieve the same result.
Here's a test program. Works on x86-32. Should work on x86-64, but
I (mtk) don't have a system to hand to test with.
It tests accept4() with each of the four possible combinations of
SOCK_CLOEXEC and SOCK_NONBLOCK set/clear in 'flags', and verifies
that the appropriate flags are set on the file descriptor/open file
description returned by accept4().
I tested Ulrich's patch in this thread by applying against 2.6.28-rc2,
and it passes according to my test program.
/* test_accept4.c
Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
<mtk.manpages@gmail.com>
Licensed under the GNU GPLv2 or later.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define PORT_NUM 33333
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/**********************************************************************/
/* The following is what we need until glibc gets a wrapper for
accept4() */
/* Flags for socket(), socketpair(), accept4() */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC O_CLOEXEC
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifdef __x86_64__
#define SYS_accept4 288
#elif __i386__
#define USE_SOCKETCALL 1
#define SYS_ACCEPT4 18
#else
#error "Sorry -- don't know the syscall # on this architecture"
#endif
static int
accept4(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
{
printf("Calling accept4(): flags = %x", flags);
if (flags != 0) {
printf(" (");
if (flags & SOCK_CLOEXEC)
printf("SOCK_CLOEXEC");
if ((flags & SOCK_CLOEXEC) && (flags & SOCK_NONBLOCK))
printf(" ");
if (flags & SOCK_NONBLOCK)
printf("SOCK_NONBLOCK");
printf(")");
}
printf("\n");
#if USE_SOCKETCALL
long args[6];
args[0] = fd;
args[1] = (long) sockaddr;
args[2] = (long) addrlen;
args[3] = flags;
return syscall(SYS_socketcall, SYS_ACCEPT4, args);
#else
return syscall(SYS_accept4, fd, sockaddr, addrlen, flags);
#endif
}
/**********************************************************************/
static int
do_test(int lfd, struct sockaddr_in *conn_addr,
int closeonexec_flag, int nonblock_flag)
{
int connfd, acceptfd;
int fdf, flf, fdf_pass, flf_pass;
struct sockaddr_in claddr;
socklen_t addrlen;
printf("=======================================\n");
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
die("socket");
if (connect(connfd, (struct sockaddr *) conn_addr,
sizeof(struct sockaddr_in)) == -1)
die("connect");
addrlen = sizeof(struct sockaddr_in);
acceptfd = accept4(lfd, (struct sockaddr *) &claddr, &addrlen,
closeonexec_flag | nonblock_flag);
if (acceptfd == -1) {
perror("accept4()");
close(connfd);
return 0;
}
fdf = fcntl(acceptfd, F_GETFD);
if (fdf == -1)
die("fcntl:F_GETFD");
fdf_pass = ((fdf & FD_CLOEXEC) != 0) ==
((closeonexec_flag & SOCK_CLOEXEC) != 0);
printf("Close-on-exec flag is %sset (%s); ",
(fdf & FD_CLOEXEC) ? "" : "not ",
fdf_pass ? "OK" : "failed");
flf = fcntl(acceptfd, F_GETFL);
if (flf == -1)
die("fcntl:F_GETFD");
flf_pass = ((flf & O_NONBLOCK) != 0) ==
((nonblock_flag & SOCK_NONBLOCK) !=0);
printf("nonblock flag is %sset (%s)\n",
(flf & O_NONBLOCK) ? "" : "not ",
flf_pass ? "OK" : "failed");
close(acceptfd);
close(connfd);
printf("Test result: %s\n", (fdf_pass && flf_pass) ? "PASS" : "FAIL");
return fdf_pass && flf_pass;
}
static int
create_listening_socket(int port_num)
{
struct sockaddr_in svaddr;
int lfd;
int optval;
memset(&svaddr, 0, sizeof(struct sockaddr_in));
svaddr.sin_family = AF_INET;
svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
svaddr.sin_port = htons(port_num);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
die("socket");
optval = 1;
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) == -1)
die("setsockopt");
if (bind(lfd, (struct sockaddr *) &svaddr,
sizeof(struct sockaddr_in)) == -1)
die("bind");
if (listen(lfd, 5) == -1)
die("listen");
return lfd;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in conn_addr;
int lfd;
int port_num;
int passed;
passed = 1;
port_num = (argc > 1) ? atoi(argv[1]) : PORT_NUM;
memset(&conn_addr, 0, sizeof(struct sockaddr_in));
conn_addr.sin_family = AF_INET;
conn_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
conn_addr.sin_port = htons(port_num);
lfd = create_listening_socket(port_num);
if (!do_test(lfd, &conn_addr, 0, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, 0, SOCK_NONBLOCK))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, SOCK_NONBLOCK))
passed = 0;
close(lfd);
exit(passed ? EXIT_SUCCESS : EXIT_FAILURE);
}
[mtk.manpages@gmail.com: rewrote changelog, updated test program]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Tested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <linux-api@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-11-20 00:36:14 +01:00
|
|
|
return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
}
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* Attempt to connect to a socket with the server address. The address
|
|
|
|
* is in user space so we verify it is OK and move it to kernel space.
|
|
|
|
*
|
|
|
|
* For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
|
|
|
|
* break bindings
|
|
|
|
*
|
|
|
|
* NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
|
|
|
|
* other SEQPACKET protocols that take time to connect() as it doesn't
|
|
|
|
* include the -EINPROGRESS status for such sockets.
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
|
|
|
|
int addrlen)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct socket *sock;
|
2008-07-20 07:35:47 +02:00
|
|
|
struct sockaddr_storage address;
|
2006-03-21 07:27:12 +01:00
|
|
|
int err, fput_needed;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-03-21 07:27:12 +01:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!sock)
|
|
|
|
goto out;
|
2008-07-20 07:35:47 +02:00
|
|
|
err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (err < 0)
|
|
|
|
goto out_put;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
err =
|
2008-07-20 07:35:47 +02:00
|
|
|
security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (err)
|
|
|
|
goto out_put;
|
|
|
|
|
2008-07-20 07:35:47 +02:00
|
|
|
err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
|
2005-04-17 00:20:36 +02:00
|
|
|
sock->file->f_flags);
|
|
|
|
out_put:
|
2006-03-21 07:27:12 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the local address ('name') of a socket object. Move the obtained
|
|
|
|
* name to user space.
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
|
|
|
|
int __user *usockaddr_len)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct socket *sock;
|
2008-07-20 07:35:47 +02:00
|
|
|
struct sockaddr_storage address;
|
2006-03-21 07:27:12 +01:00
|
|
|
int len, err, fput_needed;
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2006-03-21 07:27:12 +01:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!sock)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = security_socket_getsockname(sock);
|
|
|
|
if (err)
|
|
|
|
goto out_put;
|
|
|
|
|
2008-07-20 07:35:47 +02:00
|
|
|
err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (err)
|
|
|
|
goto out_put;
|
2008-07-20 07:35:47 +02:00
|
|
|
err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
out_put:
|
2006-03-21 07:27:12 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the remote address ('name') of a socket object. Move the obtained
|
|
|
|
* name to user space.
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
|
|
|
|
int __user *usockaddr_len)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct socket *sock;
|
2008-07-20 07:35:47 +02:00
|
|
|
struct sockaddr_storage address;
|
2006-03-21 07:27:12 +01:00
|
|
|
int len, err, fput_needed;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
|
|
|
if (sock != NULL) {
|
2005-04-17 00:20:36 +02:00
|
|
|
err = security_socket_getpeername(sock);
|
|
|
|
if (err) {
|
2006-03-21 07:27:12 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
err =
|
2008-07-20 07:35:47 +02:00
|
|
|
sock->ops->getname(sock, (struct sockaddr *)&address, &len,
|
2006-09-01 09:19:31 +02:00
|
|
|
1);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!err)
|
2008-07-20 07:35:47 +02:00
|
|
|
err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
|
2006-09-01 09:19:31 +02:00
|
|
|
usockaddr_len);
|
2006-03-21 07:27:12 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a datagram to a given address. We move the address into kernel
|
|
|
|
* space and check the user space data area is readable before invoking
|
|
|
|
* the protocol.
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
|
|
|
|
unsigned flags, struct sockaddr __user *addr,
|
|
|
|
int addr_len)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct socket *sock;
|
2008-07-20 07:35:47 +02:00
|
|
|
struct sockaddr_storage address;
|
2005-04-17 00:20:36 +02:00
|
|
|
int err;
|
|
|
|
struct msghdr msg;
|
|
|
|
struct iovec iov;
|
2006-03-21 07:27:12 +01:00
|
|
|
int fput_needed;
|
|
|
|
|
2007-11-15 01:01:43 +01:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
|
|
|
if (!sock)
|
2007-02-09 00:06:08 +01:00
|
|
|
goto out;
|
2006-03-21 07:27:12 +01:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
iov.iov_base = buff;
|
|
|
|
iov.iov_len = len;
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
msg.msg_namelen = 0;
|
2006-03-21 07:27:12 +01:00
|
|
|
if (addr) {
|
2008-07-20 07:35:47 +02:00
|
|
|
err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (err < 0)
|
|
|
|
goto out_put;
|
2008-07-20 07:35:47 +02:00
|
|
|
msg.msg_name = (struct sockaddr *)&address;
|
2006-09-01 09:19:31 +02:00
|
|
|
msg.msg_namelen = addr_len;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
if (sock->file->f_flags & O_NONBLOCK)
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
msg.msg_flags = flags;
|
|
|
|
err = sock_sendmsg(sock, &msg, len);
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
out_put:
|
2007-11-15 01:01:43 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2007-02-09 00:06:08 +01:00
|
|
|
out:
|
2005-04-17 00:20:36 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* Send a datagram down a socket.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
return sys_sendto(fd, buff, len, flags, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* Receive a frame from the socket and optionally record the address of the
|
2005-04-17 00:20:36 +02:00
|
|
|
* sender. We verify the buffers are writable and if needed move the
|
|
|
|
* sender address from kernel to user space.
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
|
|
|
|
unsigned flags, struct sockaddr __user *addr,
|
|
|
|
int __user *addr_len)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct socket *sock;
|
|
|
|
struct iovec iov;
|
|
|
|
struct msghdr msg;
|
2008-07-20 07:35:47 +02:00
|
|
|
struct sockaddr_storage address;
|
2006-09-01 09:19:31 +02:00
|
|
|
int err, err2;
|
2006-03-21 07:27:12 +01:00
|
|
|
int fput_needed;
|
|
|
|
|
2007-11-15 01:01:43 +01:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!sock)
|
2007-11-15 01:01:43 +01:00
|
|
|
goto out;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
iov.iov_len = size;
|
|
|
|
iov.iov_base = ubuf;
|
2008-07-20 07:35:47 +02:00
|
|
|
msg.msg_name = (struct sockaddr *)&address;
|
|
|
|
msg.msg_namelen = sizeof(address);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (sock->file->f_flags & O_NONBLOCK)
|
|
|
|
flags |= MSG_DONTWAIT;
|
2006-09-01 09:19:31 +02:00
|
|
|
err = sock_recvmsg(sock, &msg, size, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
if (err >= 0 && addr != NULL) {
|
2008-07-20 07:35:47 +02:00
|
|
|
err2 = move_addr_to_user((struct sockaddr *)&address,
|
|
|
|
msg.msg_namelen, addr, addr_len);
|
2006-09-01 09:19:31 +02:00
|
|
|
if (err2 < 0)
|
|
|
|
err = err2;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2007-11-15 01:01:43 +01:00
|
|
|
|
|
|
|
fput_light(sock->file, fput_needed);
|
2007-02-09 00:06:08 +01:00
|
|
|
out:
|
2005-04-17 00:20:36 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* Receive a datagram from a socket.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
|
|
|
|
unsigned flags)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a socket option. Because we don't know the option lengths we have
|
|
|
|
* to pass the user mode parameter for the protocols to sort out.
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_setsockopt(int fd, int level, int optname,
|
|
|
|
char __user *optval, int optlen)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-03-21 07:27:12 +01:00
|
|
|
int err, fput_needed;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct socket *sock;
|
|
|
|
|
|
|
|
if (optlen < 0)
|
|
|
|
return -EINVAL;
|
2006-09-01 09:19:31 +02:00
|
|
|
|
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
|
|
|
if (sock != NULL) {
|
|
|
|
err = security_socket_setsockopt(sock, level, optname);
|
2006-03-21 07:27:12 +01:00
|
|
|
if (err)
|
|
|
|
goto out_put;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (level == SOL_SOCKET)
|
2006-09-01 09:19:31 +02:00
|
|
|
err =
|
|
|
|
sock_setsockopt(sock, level, optname, optval,
|
|
|
|
optlen);
|
2005-04-17 00:20:36 +02:00
|
|
|
else
|
2006-09-01 09:19:31 +02:00
|
|
|
err =
|
|
|
|
sock->ops->setsockopt(sock, level, optname, optval,
|
|
|
|
optlen);
|
2006-03-21 07:27:12 +01:00
|
|
|
out_put:
|
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a socket option. Because we don't know the option lengths we have
|
|
|
|
* to pass a user mode parameter for the protocols to sort out.
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_getsockopt(int fd, int level, int optname,
|
|
|
|
char __user *optval, int __user *optlen)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-03-21 07:27:12 +01:00
|
|
|
int err, fput_needed;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct socket *sock;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
|
|
|
if (sock != NULL) {
|
2006-03-21 07:27:12 +01:00
|
|
|
err = security_socket_getsockopt(sock, level, optname);
|
|
|
|
if (err)
|
|
|
|
goto out_put;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (level == SOL_SOCKET)
|
2006-09-01 09:19:31 +02:00
|
|
|
err =
|
|
|
|
sock_getsockopt(sock, level, optname, optval,
|
|
|
|
optlen);
|
2005-04-17 00:20:36 +02:00
|
|
|
else
|
2006-09-01 09:19:31 +02:00
|
|
|
err =
|
|
|
|
sock->ops->getsockopt(sock, level, optname, optval,
|
|
|
|
optlen);
|
2006-03-21 07:27:12 +01:00
|
|
|
out_put:
|
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shutdown a socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
asmlinkage long sys_shutdown(int fd, int how)
|
|
|
|
{
|
2006-03-21 07:27:12 +01:00
|
|
|
int err, fput_needed;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct socket *sock;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
|
|
|
if (sock != NULL) {
|
2005-04-17 00:20:36 +02:00
|
|
|
err = security_socket_shutdown(sock, how);
|
2006-03-21 07:27:12 +01:00
|
|
|
if (!err)
|
|
|
|
err = sock->ops->shutdown(sock, how);
|
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
/* A couple of helpful macros for getting the address of the 32/64 bit
|
2005-04-17 00:20:36 +02:00
|
|
|
* fields which are the same type (int / unsigned) on our platforms.
|
|
|
|
*/
|
|
|
|
#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
|
|
|
|
#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
|
|
|
|
#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BSD sendmsg interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
|
|
|
|
{
|
2006-09-01 09:19:31 +02:00
|
|
|
struct compat_msghdr __user *msg_compat =
|
|
|
|
(struct compat_msghdr __user *)msg;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct socket *sock;
|
2008-07-20 07:35:47 +02:00
|
|
|
struct sockaddr_storage address;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
|
2005-09-26 23:28:02 +02:00
|
|
|
unsigned char ctl[sizeof(struct cmsghdr) + 20]
|
2006-09-01 09:19:31 +02:00
|
|
|
__attribute__ ((aligned(sizeof(__kernel_size_t))));
|
|
|
|
/* 20 is size of ipv6_pktinfo */
|
2005-04-17 00:20:36 +02:00
|
|
|
unsigned char *ctl_buf = ctl;
|
|
|
|
struct msghdr msg_sys;
|
|
|
|
int err, ctl_len, iov_size, total_len;
|
2006-03-21 07:27:12 +01:00
|
|
|
int fput_needed;
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
err = -EFAULT;
|
|
|
|
if (MSG_CMSG_COMPAT & flags) {
|
|
|
|
if (get_compat_msghdr(&msg_sys, msg_compat))
|
|
|
|
return -EFAULT;
|
2006-09-01 09:19:31 +02:00
|
|
|
}
|
|
|
|
else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EFAULT;
|
|
|
|
|
2006-03-21 07:27:12 +01:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
2006-09-01 09:19:31 +02:00
|
|
|
if (!sock)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* do not move before msg_sys is valid */
|
|
|
|
err = -EMSGSIZE;
|
|
|
|
if (msg_sys.msg_iovlen > UIO_MAXIOV)
|
|
|
|
goto out_put;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
/* Check whether to allocate the iovec area */
|
2005-04-17 00:20:36 +02:00
|
|
|
err = -ENOMEM;
|
|
|
|
iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
|
|
|
|
if (msg_sys.msg_iovlen > UIO_FASTIOV) {
|
|
|
|
iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
|
|
|
|
if (!iov)
|
|
|
|
goto out_put;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This will also move the address data into kernel space */
|
|
|
|
if (MSG_CMSG_COMPAT & flags) {
|
2008-07-20 07:35:47 +02:00
|
|
|
err = verify_compat_iovec(&msg_sys, iov,
|
|
|
|
(struct sockaddr *)&address,
|
|
|
|
VERIFY_READ);
|
2005-04-17 00:20:36 +02:00
|
|
|
} else
|
2008-07-20 07:35:47 +02:00
|
|
|
err = verify_iovec(&msg_sys, iov,
|
|
|
|
(struct sockaddr *)&address,
|
|
|
|
VERIFY_READ);
|
2006-09-01 09:19:31 +02:00
|
|
|
if (err < 0)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out_freeiov;
|
|
|
|
total_len = err;
|
|
|
|
|
|
|
|
err = -ENOBUFS;
|
|
|
|
|
|
|
|
if (msg_sys.msg_controllen > INT_MAX)
|
|
|
|
goto out_freeiov;
|
2006-09-01 09:19:31 +02:00
|
|
|
ctl_len = msg_sys.msg_controllen;
|
2005-04-17 00:20:36 +02:00
|
|
|
if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
|
2006-09-01 09:19:31 +02:00
|
|
|
err =
|
|
|
|
cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
|
|
|
|
sizeof(ctl));
|
2005-04-17 00:20:36 +02:00
|
|
|
if (err)
|
|
|
|
goto out_freeiov;
|
|
|
|
ctl_buf = msg_sys.msg_control;
|
2005-09-08 03:28:51 +02:00
|
|
|
ctl_len = msg_sys.msg_controllen;
|
2005-04-17 00:20:36 +02:00
|
|
|
} else if (ctl_len) {
|
2006-09-01 09:19:31 +02:00
|
|
|
if (ctl_len > sizeof(ctl)) {
|
2005-04-17 00:20:36 +02:00
|
|
|
ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
|
2006-09-01 09:19:31 +02:00
|
|
|
if (ctl_buf == NULL)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out_freeiov;
|
|
|
|
}
|
|
|
|
err = -EFAULT;
|
|
|
|
/*
|
|
|
|
* Careful! Before this, msg_sys.msg_control contains a user pointer.
|
|
|
|
* Afterwards, it will be a kernel pointer. Thus the compiler-assisted
|
|
|
|
* checking falls down on this.
|
|
|
|
*/
|
2006-09-01 09:19:31 +02:00
|
|
|
if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
|
|
|
|
ctl_len))
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out_freectl;
|
|
|
|
msg_sys.msg_control = ctl_buf;
|
|
|
|
}
|
|
|
|
msg_sys.msg_flags = flags;
|
|
|
|
|
|
|
|
if (sock->file->f_flags & O_NONBLOCK)
|
|
|
|
msg_sys.msg_flags |= MSG_DONTWAIT;
|
|
|
|
err = sock_sendmsg(sock, &msg_sys, total_len);
|
|
|
|
|
|
|
|
out_freectl:
|
2006-09-01 09:19:31 +02:00
|
|
|
if (ctl_buf != ctl)
|
2005-04-17 00:20:36 +02:00
|
|
|
sock_kfree_s(sock->sk, ctl_buf, ctl_len);
|
|
|
|
out_freeiov:
|
|
|
|
if (iov != iovstack)
|
|
|
|
sock_kfree_s(sock->sk, iov, iov_size);
|
|
|
|
out_put:
|
2006-03-21 07:27:12 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2006-09-01 09:19:31 +02:00
|
|
|
out:
|
2005-04-17 00:20:36 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BSD recvmsg interface
|
|
|
|
*/
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
|
|
|
|
unsigned int flags)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-09-01 09:19:31 +02:00
|
|
|
struct compat_msghdr __user *msg_compat =
|
|
|
|
(struct compat_msghdr __user *)msg;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct socket *sock;
|
|
|
|
struct iovec iovstack[UIO_FASTIOV];
|
2006-09-01 09:19:31 +02:00
|
|
|
struct iovec *iov = iovstack;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct msghdr msg_sys;
|
|
|
|
unsigned long cmsg_ptr;
|
|
|
|
int err, iov_size, total_len, len;
|
2006-03-21 07:27:12 +01:00
|
|
|
int fput_needed;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* kernel mode address */
|
2008-07-20 07:35:47 +02:00
|
|
|
struct sockaddr_storage addr;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* user mode address pointers */
|
|
|
|
struct sockaddr __user *uaddr;
|
|
|
|
int __user *uaddr_len;
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
if (MSG_CMSG_COMPAT & flags) {
|
|
|
|
if (get_compat_msghdr(&msg_sys, msg_compat))
|
|
|
|
return -EFAULT;
|
2006-09-01 09:19:31 +02:00
|
|
|
}
|
|
|
|
else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
|
|
|
|
return -EFAULT;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-03-21 07:27:12 +01:00
|
|
|
sock = sockfd_lookup_light(fd, &err, &fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!sock)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = -EMSGSIZE;
|
|
|
|
if (msg_sys.msg_iovlen > UIO_MAXIOV)
|
|
|
|
goto out_put;
|
2006-09-01 09:19:31 +02:00
|
|
|
|
|
|
|
/* Check whether to allocate the iovec area */
|
2005-04-17 00:20:36 +02:00
|
|
|
err = -ENOMEM;
|
|
|
|
iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
|
|
|
|
if (msg_sys.msg_iovlen > UIO_FASTIOV) {
|
|
|
|
iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
|
|
|
|
if (!iov)
|
|
|
|
goto out_put;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* Save the user-mode address (verify_iovec will change the
|
|
|
|
* kernel msghdr to use the kernel address space)
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2007-10-09 10:59:42 +02:00
|
|
|
uaddr = (__force void __user *)msg_sys.msg_name;
|
2005-04-17 00:20:36 +02:00
|
|
|
uaddr_len = COMPAT_NAMELEN(msg);
|
|
|
|
if (MSG_CMSG_COMPAT & flags) {
|
2008-07-20 07:35:47 +02:00
|
|
|
err = verify_compat_iovec(&msg_sys, iov,
|
|
|
|
(struct sockaddr *)&addr,
|
|
|
|
VERIFY_WRITE);
|
2005-04-17 00:20:36 +02:00
|
|
|
} else
|
2008-07-20 07:35:47 +02:00
|
|
|
err = verify_iovec(&msg_sys, iov,
|
|
|
|
(struct sockaddr *)&addr,
|
|
|
|
VERIFY_WRITE);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (err < 0)
|
|
|
|
goto out_freeiov;
|
2006-09-01 09:19:31 +02:00
|
|
|
total_len = err;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
cmsg_ptr = (unsigned long)msg_sys.msg_control;
|
O_CLOEXEC for SCM_RIGHTS
Part two in the O_CLOEXEC saga: adding support for file descriptors received
through Unix domain sockets.
The patch is once again pretty minimal, it introduces a new flag for recvmsg
and passes it just like the existing MSG_CMSG_COMPAT flag. I think this bit
is not used otherwise but the networking people will know better.
This new flag is not recognized by recvfrom and recv. These functions cannot
be used for that purpose and the asymmetry this introduces is not worse than
the already existing MSG_CMSG_COMPAT situations.
The patch must be applied on the patch which introduced O_CLOEXEC. It has to
remove static from the new get_unused_fd_flags function but since scm.c cannot
live in a module the function still hasn't to be exported.
Here's a test program to make sure the code works. It's so much longer than
the actual patch...
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#ifndef O_CLOEXEC
# define O_CLOEXEC 02000000
#endif
#ifndef MSG_CMSG_CLOEXEC
# define MSG_CMSG_CLOEXEC 0x40000000
#endif
int
main (int argc, char *argv[])
{
if (argc > 1)
{
int fd = atol (argv[1]);
printf ("child: fd = %d\n", fd);
if (fcntl (fd, F_GETFD) == 0 || errno != EBADF)
{
puts ("file descriptor valid in child");
return 1;
}
return 0;
}
struct sockaddr_un sun;
strcpy (sun.sun_path, "./testsocket");
sun.sun_family = AF_UNIX;
char databuf[] = "hello";
struct iovec iov[1];
iov[0].iov_base = databuf;
iov[0].iov_len = sizeof (databuf);
union
{
struct cmsghdr hdr;
char bytes[CMSG_SPACE (sizeof (int))];
} buf;
struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1,
.msg_control = buf.bytes,
.msg_controllen = sizeof (buf) };
struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN (sizeof (int));
msg.msg_controllen = cmsg->cmsg_len;
pid_t child = fork ();
if (child == -1)
error (1, errno, "fork");
if (child == 0)
{
int sock = socket (PF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
error (1, errno, "socket");
if (bind (sock, (struct sockaddr *) &sun, sizeof (sun)) < 0)
error (1, errno, "bind");
if (listen (sock, SOMAXCONN) < 0)
error (1, errno, "listen");
int conn = accept (sock, NULL, NULL);
if (conn == -1)
error (1, errno, "accept");
*(int *) CMSG_DATA (cmsg) = sock;
if (sendmsg (conn, &msg, MSG_NOSIGNAL) < 0)
error (1, errno, "sendmsg");
return 0;
}
/* For a test suite this should be more robust like a
barrier in shared memory. */
sleep (1);
int sock = socket (PF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
error (1, errno, "socket");
if (connect (sock, (struct sockaddr *) &sun, sizeof (sun)) < 0)
error (1, errno, "connect");
unlink (sun.sun_path);
*(int *) CMSG_DATA (cmsg) = -1;
if (recvmsg (sock, &msg, MSG_CMSG_CLOEXEC) < 0)
error (1, errno, "recvmsg");
int fd = *(int *) CMSG_DATA (cmsg);
if (fd == -1)
error (1, 0, "no descriptor received");
char fdname[20];
snprintf (fdname, sizeof (fdname), "%d", fd);
execl ("/proc/self/exe", argv[0], fdname, NULL);
puts ("execl failed");
return 1;
}
[akpm@linux-foundation.org: Fix fastcall inconsistency noted by Michael Buesch]
[akpm@linux-foundation.org: build fix]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Michael Buesch <mb@bu3sch.de>
Cc: Michael Kerrisk <mtk-manpages@gmx.net>
Acked-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-16 08:40:34 +02:00
|
|
|
msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
if (sock->file->f_flags & O_NONBLOCK)
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
err = sock_recvmsg(sock, &msg_sys, total_len, flags);
|
|
|
|
if (err < 0)
|
|
|
|
goto out_freeiov;
|
|
|
|
len = err;
|
|
|
|
|
|
|
|
if (uaddr != NULL) {
|
2008-07-20 07:35:47 +02:00
|
|
|
err = move_addr_to_user((struct sockaddr *)&addr,
|
|
|
|
msg_sys.msg_namelen, uaddr,
|
2006-09-01 09:19:31 +02:00
|
|
|
uaddr_len);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (err < 0)
|
|
|
|
goto out_freeiov;
|
|
|
|
}
|
2005-09-17 01:51:01 +02:00
|
|
|
err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
|
|
|
|
COMPAT_FLAGS(msg));
|
2005-04-17 00:20:36 +02:00
|
|
|
if (err)
|
|
|
|
goto out_freeiov;
|
|
|
|
if (MSG_CMSG_COMPAT & flags)
|
2006-09-01 09:19:31 +02:00
|
|
|
err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
|
2005-04-17 00:20:36 +02:00
|
|
|
&msg_compat->msg_controllen);
|
|
|
|
else
|
2006-09-01 09:19:31 +02:00
|
|
|
err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
|
2005-04-17 00:20:36 +02:00
|
|
|
&msg->msg_controllen);
|
|
|
|
if (err)
|
|
|
|
goto out_freeiov;
|
|
|
|
err = len;
|
|
|
|
|
|
|
|
out_freeiov:
|
|
|
|
if (iov != iovstack)
|
|
|
|
sock_kfree_s(sock->sk, iov, iov_size);
|
|
|
|
out_put:
|
2006-03-21 07:27:12 +01:00
|
|
|
fput_light(sock->file, fput_needed);
|
2005-04-17 00:20:36 +02:00
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __ARCH_WANT_SYS_SOCKETCALL
|
|
|
|
|
|
|
|
/* Argument list sizes for sys_socketcall */
|
|
|
|
#define AL(x) ((x) * sizeof(unsigned long))
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
static const unsigned char nargs[19]={
|
2006-09-01 09:19:31 +02:00
|
|
|
AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
|
|
|
|
AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
|
reintroduce accept4
Introduce a new accept4() system call. The addition of this system call
matches analogous changes in 2.6.27 (dup3(), evenfd2(), signalfd4(),
inotify_init1(), epoll_create1(), pipe2()) which added new system calls
that differed from analogous traditional system calls in adding a flags
argument that can be used to access additional functionality.
The accept4() system call is exactly the same as accept(), except that
it adds a flags bit-mask argument. Two flags are initially implemented.
(Most of the new system calls in 2.6.27 also had both of these flags.)
SOCK_CLOEXEC causes the close-on-exec (FD_CLOEXEC) flag to be enabled
for the new file descriptor returned by accept4(). This is a useful
security feature to avoid leaking information in a multithreaded
program where one thread is doing an accept() at the same time as
another thread is doing a fork() plus exec(). More details here:
http://udrepper.livejournal.com/20407.html "Secure File Descriptor Handling",
Ulrich Drepper).
The other flag is SOCK_NONBLOCK, which causes the O_NONBLOCK flag
to be enabled on the new open file description created by accept4().
(This flag is merely a convenience, saving the use of additional calls
fcntl(F_GETFL) and fcntl (F_SETFL) to achieve the same result.
Here's a test program. Works on x86-32. Should work on x86-64, but
I (mtk) don't have a system to hand to test with.
It tests accept4() with each of the four possible combinations of
SOCK_CLOEXEC and SOCK_NONBLOCK set/clear in 'flags', and verifies
that the appropriate flags are set on the file descriptor/open file
description returned by accept4().
I tested Ulrich's patch in this thread by applying against 2.6.28-rc2,
and it passes according to my test program.
/* test_accept4.c
Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
<mtk.manpages@gmail.com>
Licensed under the GNU GPLv2 or later.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define PORT_NUM 33333
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/**********************************************************************/
/* The following is what we need until glibc gets a wrapper for
accept4() */
/* Flags for socket(), socketpair(), accept4() */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC O_CLOEXEC
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifdef __x86_64__
#define SYS_accept4 288
#elif __i386__
#define USE_SOCKETCALL 1
#define SYS_ACCEPT4 18
#else
#error "Sorry -- don't know the syscall # on this architecture"
#endif
static int
accept4(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
{
printf("Calling accept4(): flags = %x", flags);
if (flags != 0) {
printf(" (");
if (flags & SOCK_CLOEXEC)
printf("SOCK_CLOEXEC");
if ((flags & SOCK_CLOEXEC) && (flags & SOCK_NONBLOCK))
printf(" ");
if (flags & SOCK_NONBLOCK)
printf("SOCK_NONBLOCK");
printf(")");
}
printf("\n");
#if USE_SOCKETCALL
long args[6];
args[0] = fd;
args[1] = (long) sockaddr;
args[2] = (long) addrlen;
args[3] = flags;
return syscall(SYS_socketcall, SYS_ACCEPT4, args);
#else
return syscall(SYS_accept4, fd, sockaddr, addrlen, flags);
#endif
}
/**********************************************************************/
static int
do_test(int lfd, struct sockaddr_in *conn_addr,
int closeonexec_flag, int nonblock_flag)
{
int connfd, acceptfd;
int fdf, flf, fdf_pass, flf_pass;
struct sockaddr_in claddr;
socklen_t addrlen;
printf("=======================================\n");
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
die("socket");
if (connect(connfd, (struct sockaddr *) conn_addr,
sizeof(struct sockaddr_in)) == -1)
die("connect");
addrlen = sizeof(struct sockaddr_in);
acceptfd = accept4(lfd, (struct sockaddr *) &claddr, &addrlen,
closeonexec_flag | nonblock_flag);
if (acceptfd == -1) {
perror("accept4()");
close(connfd);
return 0;
}
fdf = fcntl(acceptfd, F_GETFD);
if (fdf == -1)
die("fcntl:F_GETFD");
fdf_pass = ((fdf & FD_CLOEXEC) != 0) ==
((closeonexec_flag & SOCK_CLOEXEC) != 0);
printf("Close-on-exec flag is %sset (%s); ",
(fdf & FD_CLOEXEC) ? "" : "not ",
fdf_pass ? "OK" : "failed");
flf = fcntl(acceptfd, F_GETFL);
if (flf == -1)
die("fcntl:F_GETFD");
flf_pass = ((flf & O_NONBLOCK) != 0) ==
((nonblock_flag & SOCK_NONBLOCK) !=0);
printf("nonblock flag is %sset (%s)\n",
(flf & O_NONBLOCK) ? "" : "not ",
flf_pass ? "OK" : "failed");
close(acceptfd);
close(connfd);
printf("Test result: %s\n", (fdf_pass && flf_pass) ? "PASS" : "FAIL");
return fdf_pass && flf_pass;
}
static int
create_listening_socket(int port_num)
{
struct sockaddr_in svaddr;
int lfd;
int optval;
memset(&svaddr, 0, sizeof(struct sockaddr_in));
svaddr.sin_family = AF_INET;
svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
svaddr.sin_port = htons(port_num);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
die("socket");
optval = 1;
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) == -1)
die("setsockopt");
if (bind(lfd, (struct sockaddr *) &svaddr,
sizeof(struct sockaddr_in)) == -1)
die("bind");
if (listen(lfd, 5) == -1)
die("listen");
return lfd;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in conn_addr;
int lfd;
int port_num;
int passed;
passed = 1;
port_num = (argc > 1) ? atoi(argv[1]) : PORT_NUM;
memset(&conn_addr, 0, sizeof(struct sockaddr_in));
conn_addr.sin_family = AF_INET;
conn_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
conn_addr.sin_port = htons(port_num);
lfd = create_listening_socket(port_num);
if (!do_test(lfd, &conn_addr, 0, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, 0, SOCK_NONBLOCK))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, SOCK_NONBLOCK))
passed = 0;
close(lfd);
exit(passed ? EXIT_SUCCESS : EXIT_FAILURE);
}
[mtk.manpages@gmail.com: rewrote changelog, updated test program]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Tested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <linux-api@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-11-20 00:36:14 +01:00
|
|
|
AL(4)
|
2006-09-01 09:19:31 +02:00
|
|
|
};
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
#undef AL
|
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* System call vectors.
|
2005-04-17 00:20:36 +02:00
|
|
|
*
|
|
|
|
* Argument checking cleaned up. Saved 20% in size.
|
|
|
|
* This function doesn't need to set the kernel lock because
|
2006-09-01 09:19:31 +02:00
|
|
|
* it is set by the callees.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
asmlinkage long sys_socketcall(int call, unsigned long __user *args)
|
|
|
|
{
|
|
|
|
unsigned long a[6];
|
2006-09-01 09:19:31 +02:00
|
|
|
unsigned long a0, a1;
|
2005-04-17 00:20:36 +02:00
|
|
|
int err;
|
|
|
|
|
reintroduce accept4
Introduce a new accept4() system call. The addition of this system call
matches analogous changes in 2.6.27 (dup3(), evenfd2(), signalfd4(),
inotify_init1(), epoll_create1(), pipe2()) which added new system calls
that differed from analogous traditional system calls in adding a flags
argument that can be used to access additional functionality.
The accept4() system call is exactly the same as accept(), except that
it adds a flags bit-mask argument. Two flags are initially implemented.
(Most of the new system calls in 2.6.27 also had both of these flags.)
SOCK_CLOEXEC causes the close-on-exec (FD_CLOEXEC) flag to be enabled
for the new file descriptor returned by accept4(). This is a useful
security feature to avoid leaking information in a multithreaded
program where one thread is doing an accept() at the same time as
another thread is doing a fork() plus exec(). More details here:
http://udrepper.livejournal.com/20407.html "Secure File Descriptor Handling",
Ulrich Drepper).
The other flag is SOCK_NONBLOCK, which causes the O_NONBLOCK flag
to be enabled on the new open file description created by accept4().
(This flag is merely a convenience, saving the use of additional calls
fcntl(F_GETFL) and fcntl (F_SETFL) to achieve the same result.
Here's a test program. Works on x86-32. Should work on x86-64, but
I (mtk) don't have a system to hand to test with.
It tests accept4() with each of the four possible combinations of
SOCK_CLOEXEC and SOCK_NONBLOCK set/clear in 'flags', and verifies
that the appropriate flags are set on the file descriptor/open file
description returned by accept4().
I tested Ulrich's patch in this thread by applying against 2.6.28-rc2,
and it passes according to my test program.
/* test_accept4.c
Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
<mtk.manpages@gmail.com>
Licensed under the GNU GPLv2 or later.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define PORT_NUM 33333
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/**********************************************************************/
/* The following is what we need until glibc gets a wrapper for
accept4() */
/* Flags for socket(), socketpair(), accept4() */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC O_CLOEXEC
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifdef __x86_64__
#define SYS_accept4 288
#elif __i386__
#define USE_SOCKETCALL 1
#define SYS_ACCEPT4 18
#else
#error "Sorry -- don't know the syscall # on this architecture"
#endif
static int
accept4(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
{
printf("Calling accept4(): flags = %x", flags);
if (flags != 0) {
printf(" (");
if (flags & SOCK_CLOEXEC)
printf("SOCK_CLOEXEC");
if ((flags & SOCK_CLOEXEC) && (flags & SOCK_NONBLOCK))
printf(" ");
if (flags & SOCK_NONBLOCK)
printf("SOCK_NONBLOCK");
printf(")");
}
printf("\n");
#if USE_SOCKETCALL
long args[6];
args[0] = fd;
args[1] = (long) sockaddr;
args[2] = (long) addrlen;
args[3] = flags;
return syscall(SYS_socketcall, SYS_ACCEPT4, args);
#else
return syscall(SYS_accept4, fd, sockaddr, addrlen, flags);
#endif
}
/**********************************************************************/
static int
do_test(int lfd, struct sockaddr_in *conn_addr,
int closeonexec_flag, int nonblock_flag)
{
int connfd, acceptfd;
int fdf, flf, fdf_pass, flf_pass;
struct sockaddr_in claddr;
socklen_t addrlen;
printf("=======================================\n");
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
die("socket");
if (connect(connfd, (struct sockaddr *) conn_addr,
sizeof(struct sockaddr_in)) == -1)
die("connect");
addrlen = sizeof(struct sockaddr_in);
acceptfd = accept4(lfd, (struct sockaddr *) &claddr, &addrlen,
closeonexec_flag | nonblock_flag);
if (acceptfd == -1) {
perror("accept4()");
close(connfd);
return 0;
}
fdf = fcntl(acceptfd, F_GETFD);
if (fdf == -1)
die("fcntl:F_GETFD");
fdf_pass = ((fdf & FD_CLOEXEC) != 0) ==
((closeonexec_flag & SOCK_CLOEXEC) != 0);
printf("Close-on-exec flag is %sset (%s); ",
(fdf & FD_CLOEXEC) ? "" : "not ",
fdf_pass ? "OK" : "failed");
flf = fcntl(acceptfd, F_GETFL);
if (flf == -1)
die("fcntl:F_GETFD");
flf_pass = ((flf & O_NONBLOCK) != 0) ==
((nonblock_flag & SOCK_NONBLOCK) !=0);
printf("nonblock flag is %sset (%s)\n",
(flf & O_NONBLOCK) ? "" : "not ",
flf_pass ? "OK" : "failed");
close(acceptfd);
close(connfd);
printf("Test result: %s\n", (fdf_pass && flf_pass) ? "PASS" : "FAIL");
return fdf_pass && flf_pass;
}
static int
create_listening_socket(int port_num)
{
struct sockaddr_in svaddr;
int lfd;
int optval;
memset(&svaddr, 0, sizeof(struct sockaddr_in));
svaddr.sin_family = AF_INET;
svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
svaddr.sin_port = htons(port_num);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
die("socket");
optval = 1;
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) == -1)
die("setsockopt");
if (bind(lfd, (struct sockaddr *) &svaddr,
sizeof(struct sockaddr_in)) == -1)
die("bind");
if (listen(lfd, 5) == -1)
die("listen");
return lfd;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in conn_addr;
int lfd;
int port_num;
int passed;
passed = 1;
port_num = (argc > 1) ? atoi(argv[1]) : PORT_NUM;
memset(&conn_addr, 0, sizeof(struct sockaddr_in));
conn_addr.sin_family = AF_INET;
conn_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
conn_addr.sin_port = htons(port_num);
lfd = create_listening_socket(port_num);
if (!do_test(lfd, &conn_addr, 0, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, 0, SOCK_NONBLOCK))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, SOCK_NONBLOCK))
passed = 0;
close(lfd);
exit(passed ? EXIT_SUCCESS : EXIT_FAILURE);
}
[mtk.manpages@gmail.com: rewrote changelog, updated test program]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Tested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <linux-api@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-11-20 00:36:14 +01:00
|
|
|
if (call < 1 || call > SYS_ACCEPT4)
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* copy_from_user should be SMP safe. */
|
|
|
|
if (copy_from_user(a, args, nargs[call]))
|
|
|
|
return -EFAULT;
|
2005-05-17 13:08:48 +02:00
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
|
2005-05-17 13:08:48 +02:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
a0 = a[0];
|
|
|
|
a1 = a[1];
|
|
|
|
|
|
|
|
switch (call) {
|
|
|
|
case SYS_SOCKET:
|
|
|
|
err = sys_socket(a0, a1, a[2]);
|
|
|
|
break;
|
|
|
|
case SYS_BIND:
|
|
|
|
err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
|
|
|
|
break;
|
|
|
|
case SYS_CONNECT:
|
|
|
|
err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
|
|
|
|
break;
|
|
|
|
case SYS_LISTEN:
|
|
|
|
err = sys_listen(a0, a1);
|
|
|
|
break;
|
|
|
|
case SYS_ACCEPT:
|
reintroduce accept4
Introduce a new accept4() system call. The addition of this system call
matches analogous changes in 2.6.27 (dup3(), evenfd2(), signalfd4(),
inotify_init1(), epoll_create1(), pipe2()) which added new system calls
that differed from analogous traditional system calls in adding a flags
argument that can be used to access additional functionality.
The accept4() system call is exactly the same as accept(), except that
it adds a flags bit-mask argument. Two flags are initially implemented.
(Most of the new system calls in 2.6.27 also had both of these flags.)
SOCK_CLOEXEC causes the close-on-exec (FD_CLOEXEC) flag to be enabled
for the new file descriptor returned by accept4(). This is a useful
security feature to avoid leaking information in a multithreaded
program where one thread is doing an accept() at the same time as
another thread is doing a fork() plus exec(). More details here:
http://udrepper.livejournal.com/20407.html "Secure File Descriptor Handling",
Ulrich Drepper).
The other flag is SOCK_NONBLOCK, which causes the O_NONBLOCK flag
to be enabled on the new open file description created by accept4().
(This flag is merely a convenience, saving the use of additional calls
fcntl(F_GETFL) and fcntl (F_SETFL) to achieve the same result.
Here's a test program. Works on x86-32. Should work on x86-64, but
I (mtk) don't have a system to hand to test with.
It tests accept4() with each of the four possible combinations of
SOCK_CLOEXEC and SOCK_NONBLOCK set/clear in 'flags', and verifies
that the appropriate flags are set on the file descriptor/open file
description returned by accept4().
I tested Ulrich's patch in this thread by applying against 2.6.28-rc2,
and it passes according to my test program.
/* test_accept4.c
Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
<mtk.manpages@gmail.com>
Licensed under the GNU GPLv2 or later.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define PORT_NUM 33333
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/**********************************************************************/
/* The following is what we need until glibc gets a wrapper for
accept4() */
/* Flags for socket(), socketpair(), accept4() */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC O_CLOEXEC
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifdef __x86_64__
#define SYS_accept4 288
#elif __i386__
#define USE_SOCKETCALL 1
#define SYS_ACCEPT4 18
#else
#error "Sorry -- don't know the syscall # on this architecture"
#endif
static int
accept4(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
{
printf("Calling accept4(): flags = %x", flags);
if (flags != 0) {
printf(" (");
if (flags & SOCK_CLOEXEC)
printf("SOCK_CLOEXEC");
if ((flags & SOCK_CLOEXEC) && (flags & SOCK_NONBLOCK))
printf(" ");
if (flags & SOCK_NONBLOCK)
printf("SOCK_NONBLOCK");
printf(")");
}
printf("\n");
#if USE_SOCKETCALL
long args[6];
args[0] = fd;
args[1] = (long) sockaddr;
args[2] = (long) addrlen;
args[3] = flags;
return syscall(SYS_socketcall, SYS_ACCEPT4, args);
#else
return syscall(SYS_accept4, fd, sockaddr, addrlen, flags);
#endif
}
/**********************************************************************/
static int
do_test(int lfd, struct sockaddr_in *conn_addr,
int closeonexec_flag, int nonblock_flag)
{
int connfd, acceptfd;
int fdf, flf, fdf_pass, flf_pass;
struct sockaddr_in claddr;
socklen_t addrlen;
printf("=======================================\n");
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
die("socket");
if (connect(connfd, (struct sockaddr *) conn_addr,
sizeof(struct sockaddr_in)) == -1)
die("connect");
addrlen = sizeof(struct sockaddr_in);
acceptfd = accept4(lfd, (struct sockaddr *) &claddr, &addrlen,
closeonexec_flag | nonblock_flag);
if (acceptfd == -1) {
perror("accept4()");
close(connfd);
return 0;
}
fdf = fcntl(acceptfd, F_GETFD);
if (fdf == -1)
die("fcntl:F_GETFD");
fdf_pass = ((fdf & FD_CLOEXEC) != 0) ==
((closeonexec_flag & SOCK_CLOEXEC) != 0);
printf("Close-on-exec flag is %sset (%s); ",
(fdf & FD_CLOEXEC) ? "" : "not ",
fdf_pass ? "OK" : "failed");
flf = fcntl(acceptfd, F_GETFL);
if (flf == -1)
die("fcntl:F_GETFD");
flf_pass = ((flf & O_NONBLOCK) != 0) ==
((nonblock_flag & SOCK_NONBLOCK) !=0);
printf("nonblock flag is %sset (%s)\n",
(flf & O_NONBLOCK) ? "" : "not ",
flf_pass ? "OK" : "failed");
close(acceptfd);
close(connfd);
printf("Test result: %s\n", (fdf_pass && flf_pass) ? "PASS" : "FAIL");
return fdf_pass && flf_pass;
}
static int
create_listening_socket(int port_num)
{
struct sockaddr_in svaddr;
int lfd;
int optval;
memset(&svaddr, 0, sizeof(struct sockaddr_in));
svaddr.sin_family = AF_INET;
svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
svaddr.sin_port = htons(port_num);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
die("socket");
optval = 1;
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) == -1)
die("setsockopt");
if (bind(lfd, (struct sockaddr *) &svaddr,
sizeof(struct sockaddr_in)) == -1)
die("bind");
if (listen(lfd, 5) == -1)
die("listen");
return lfd;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in conn_addr;
int lfd;
int port_num;
int passed;
passed = 1;
port_num = (argc > 1) ? atoi(argv[1]) : PORT_NUM;
memset(&conn_addr, 0, sizeof(struct sockaddr_in));
conn_addr.sin_family = AF_INET;
conn_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
conn_addr.sin_port = htons(port_num);
lfd = create_listening_socket(port_num);
if (!do_test(lfd, &conn_addr, 0, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, 0, SOCK_NONBLOCK))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, SOCK_NONBLOCK))
passed = 0;
close(lfd);
exit(passed ? EXIT_SUCCESS : EXIT_FAILURE);
}
[mtk.manpages@gmail.com: rewrote changelog, updated test program]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Tested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <linux-api@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-11-20 00:36:14 +01:00
|
|
|
err = sys_accept4(a0, (struct sockaddr __user *)a1,
|
|
|
|
(int __user *)a[2], 0);
|
2006-09-01 09:19:31 +02:00
|
|
|
break;
|
|
|
|
case SYS_GETSOCKNAME:
|
|
|
|
err =
|
|
|
|
sys_getsockname(a0, (struct sockaddr __user *)a1,
|
|
|
|
(int __user *)a[2]);
|
|
|
|
break;
|
|
|
|
case SYS_GETPEERNAME:
|
|
|
|
err =
|
|
|
|
sys_getpeername(a0, (struct sockaddr __user *)a1,
|
|
|
|
(int __user *)a[2]);
|
|
|
|
break;
|
|
|
|
case SYS_SOCKETPAIR:
|
|
|
|
err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
|
|
|
|
break;
|
|
|
|
case SYS_SEND:
|
|
|
|
err = sys_send(a0, (void __user *)a1, a[2], a[3]);
|
|
|
|
break;
|
|
|
|
case SYS_SENDTO:
|
|
|
|
err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
|
|
|
|
(struct sockaddr __user *)a[4], a[5]);
|
|
|
|
break;
|
|
|
|
case SYS_RECV:
|
|
|
|
err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
|
|
|
|
break;
|
|
|
|
case SYS_RECVFROM:
|
|
|
|
err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
|
|
|
|
(struct sockaddr __user *)a[4],
|
|
|
|
(int __user *)a[5]);
|
|
|
|
break;
|
|
|
|
case SYS_SHUTDOWN:
|
|
|
|
err = sys_shutdown(a0, a1);
|
|
|
|
break;
|
|
|
|
case SYS_SETSOCKOPT:
|
|
|
|
err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
|
|
|
|
break;
|
|
|
|
case SYS_GETSOCKOPT:
|
|
|
|
err =
|
|
|
|
sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
|
|
|
|
(int __user *)a[4]);
|
|
|
|
break;
|
|
|
|
case SYS_SENDMSG:
|
|
|
|
err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
|
|
|
|
break;
|
|
|
|
case SYS_RECVMSG:
|
|
|
|
err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
|
|
|
|
break;
|
reintroduce accept4
Introduce a new accept4() system call. The addition of this system call
matches analogous changes in 2.6.27 (dup3(), evenfd2(), signalfd4(),
inotify_init1(), epoll_create1(), pipe2()) which added new system calls
that differed from analogous traditional system calls in adding a flags
argument that can be used to access additional functionality.
The accept4() system call is exactly the same as accept(), except that
it adds a flags bit-mask argument. Two flags are initially implemented.
(Most of the new system calls in 2.6.27 also had both of these flags.)
SOCK_CLOEXEC causes the close-on-exec (FD_CLOEXEC) flag to be enabled
for the new file descriptor returned by accept4(). This is a useful
security feature to avoid leaking information in a multithreaded
program where one thread is doing an accept() at the same time as
another thread is doing a fork() plus exec(). More details here:
http://udrepper.livejournal.com/20407.html "Secure File Descriptor Handling",
Ulrich Drepper).
The other flag is SOCK_NONBLOCK, which causes the O_NONBLOCK flag
to be enabled on the new open file description created by accept4().
(This flag is merely a convenience, saving the use of additional calls
fcntl(F_GETFL) and fcntl (F_SETFL) to achieve the same result.
Here's a test program. Works on x86-32. Should work on x86-64, but
I (mtk) don't have a system to hand to test with.
It tests accept4() with each of the four possible combinations of
SOCK_CLOEXEC and SOCK_NONBLOCK set/clear in 'flags', and verifies
that the appropriate flags are set on the file descriptor/open file
description returned by accept4().
I tested Ulrich's patch in this thread by applying against 2.6.28-rc2,
and it passes according to my test program.
/* test_accept4.c
Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
<mtk.manpages@gmail.com>
Licensed under the GNU GPLv2 or later.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define PORT_NUM 33333
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/**********************************************************************/
/* The following is what we need until glibc gets a wrapper for
accept4() */
/* Flags for socket(), socketpair(), accept4() */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC O_CLOEXEC
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifdef __x86_64__
#define SYS_accept4 288
#elif __i386__
#define USE_SOCKETCALL 1
#define SYS_ACCEPT4 18
#else
#error "Sorry -- don't know the syscall # on this architecture"
#endif
static int
accept4(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
{
printf("Calling accept4(): flags = %x", flags);
if (flags != 0) {
printf(" (");
if (flags & SOCK_CLOEXEC)
printf("SOCK_CLOEXEC");
if ((flags & SOCK_CLOEXEC) && (flags & SOCK_NONBLOCK))
printf(" ");
if (flags & SOCK_NONBLOCK)
printf("SOCK_NONBLOCK");
printf(")");
}
printf("\n");
#if USE_SOCKETCALL
long args[6];
args[0] = fd;
args[1] = (long) sockaddr;
args[2] = (long) addrlen;
args[3] = flags;
return syscall(SYS_socketcall, SYS_ACCEPT4, args);
#else
return syscall(SYS_accept4, fd, sockaddr, addrlen, flags);
#endif
}
/**********************************************************************/
static int
do_test(int lfd, struct sockaddr_in *conn_addr,
int closeonexec_flag, int nonblock_flag)
{
int connfd, acceptfd;
int fdf, flf, fdf_pass, flf_pass;
struct sockaddr_in claddr;
socklen_t addrlen;
printf("=======================================\n");
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
die("socket");
if (connect(connfd, (struct sockaddr *) conn_addr,
sizeof(struct sockaddr_in)) == -1)
die("connect");
addrlen = sizeof(struct sockaddr_in);
acceptfd = accept4(lfd, (struct sockaddr *) &claddr, &addrlen,
closeonexec_flag | nonblock_flag);
if (acceptfd == -1) {
perror("accept4()");
close(connfd);
return 0;
}
fdf = fcntl(acceptfd, F_GETFD);
if (fdf == -1)
die("fcntl:F_GETFD");
fdf_pass = ((fdf & FD_CLOEXEC) != 0) ==
((closeonexec_flag & SOCK_CLOEXEC) != 0);
printf("Close-on-exec flag is %sset (%s); ",
(fdf & FD_CLOEXEC) ? "" : "not ",
fdf_pass ? "OK" : "failed");
flf = fcntl(acceptfd, F_GETFL);
if (flf == -1)
die("fcntl:F_GETFD");
flf_pass = ((flf & O_NONBLOCK) != 0) ==
((nonblock_flag & SOCK_NONBLOCK) !=0);
printf("nonblock flag is %sset (%s)\n",
(flf & O_NONBLOCK) ? "" : "not ",
flf_pass ? "OK" : "failed");
close(acceptfd);
close(connfd);
printf("Test result: %s\n", (fdf_pass && flf_pass) ? "PASS" : "FAIL");
return fdf_pass && flf_pass;
}
static int
create_listening_socket(int port_num)
{
struct sockaddr_in svaddr;
int lfd;
int optval;
memset(&svaddr, 0, sizeof(struct sockaddr_in));
svaddr.sin_family = AF_INET;
svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
svaddr.sin_port = htons(port_num);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
die("socket");
optval = 1;
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) == -1)
die("setsockopt");
if (bind(lfd, (struct sockaddr *) &svaddr,
sizeof(struct sockaddr_in)) == -1)
die("bind");
if (listen(lfd, 5) == -1)
die("listen");
return lfd;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in conn_addr;
int lfd;
int port_num;
int passed;
passed = 1;
port_num = (argc > 1) ? atoi(argv[1]) : PORT_NUM;
memset(&conn_addr, 0, sizeof(struct sockaddr_in));
conn_addr.sin_family = AF_INET;
conn_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
conn_addr.sin_port = htons(port_num);
lfd = create_listening_socket(port_num);
if (!do_test(lfd, &conn_addr, 0, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, 0, SOCK_NONBLOCK))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, SOCK_NONBLOCK))
passed = 0;
close(lfd);
exit(passed ? EXIT_SUCCESS : EXIT_FAILURE);
}
[mtk.manpages@gmail.com: rewrote changelog, updated test program]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Tested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <linux-api@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-11-20 00:36:14 +01:00
|
|
|
case SYS_ACCEPT4:
|
|
|
|
err = sys_accept4(a0, (struct sockaddr __user *)a1,
|
|
|
|
(int __user *)a[2], a[3]);
|
flag parameters: paccept
This patch is by far the most complex in the series. It adds a new syscall
paccept. This syscall differs from accept in that it adds (at the userlevel)
two additional parameters:
- a signal mask
- a flags value
The flags parameter can be used to set flag like SOCK_CLOEXEC. This is
imlpemented here as well. Some people argued that this is a property which
should be inherited from the file desriptor for the server but this is against
POSIX. Additionally, we really want the signal mask parameter as well
(similar to pselect, ppoll, etc). So an interface change in inevitable.
The flag value is the same as for socket and socketpair. I think diverging
here will only create confusion. Similar to the filesystem interfaces where
the use of the O_* constants differs, it is acceptable here.
The signal mask is handled as for pselect etc. The mask is temporarily
installed for the thread and removed before the call returns. I modeled the
code after pselect. If there is a problem it's likely also in pselect.
For architectures which use socketcall I maintained this interface instead of
adding a system call. The symmetry shouldn't be broken.
The following test must be adjusted for architectures other than x86 and
x86-64 and in case the syscall numbers changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#ifndef __NR_paccept
# ifdef __x86_64__
# define __NR_paccept 288
# elif defined __i386__
# define SYS_PACCEPT 18
# define USE_SOCKETCALL 1
# else
# error "need __NR_paccept"
# endif
#endif
#ifdef USE_SOCKETCALL
# define paccept(fd, addr, addrlen, mask, flags) \
({ long args[6] = { \
(long) fd, (long) addr, (long) addrlen, (long) mask, 8, (long) flags }; \
syscall (__NR_socketcall, SYS_PACCEPT, args); })
#else
# define paccept(fd, addr, addrlen, mask, flags) \
syscall (__NR_paccept, fd, addr, addrlen, mask, 8, flags)
#endif
#define PORT 57392
#define SOCK_CLOEXEC O_CLOEXEC
static pthread_barrier_t b;
static void *
tf (void *arg)
{
pthread_barrier_wait (&b);
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
s = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons (PORT);
connect (s, (const struct sockaddr *) &sin, sizeof (sin));
close (s);
pthread_barrier_wait (&b);
pthread_barrier_wait (&b);
sleep (2);
pthread_kill ((pthread_t) arg, SIGUSR1);
return NULL;
}
static void
handler (int s)
{
}
int
main (void)
{
pthread_barrier_init (&b, NULL, 2);
struct sockaddr_in sin;
pthread_t th;
if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
{
puts ("pthread_create failed");
return 1;
}
int s = socket (AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sin.sin_port = htons (PORT);
bind (s, (struct sockaddr *) &sin, sizeof (sin));
listen (s, SOMAXCONN);
pthread_barrier_wait (&b);
int s2 = paccept (s, NULL, 0, NULL, 0);
if (s2 < 0)
{
puts ("paccept(0) failed");
return 1;
}
int coe = fcntl (s2, F_GETFD);
if (coe & FD_CLOEXEC)
{
puts ("paccept(0) set close-on-exec-flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
s2 = paccept (s, NULL, 0, NULL, SOCK_CLOEXEC);
if (s2 < 0)
{
puts ("paccept(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (s2, F_GETFD);
if ((coe & FD_CLOEXEC) == 0)
{
puts ("paccept(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (s2);
pthread_barrier_wait (&b);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGUSR1, &sa, NULL);
sigset_t ss;
pthread_sigmask (SIG_SETMASK, NULL, &ss);
sigaddset (&ss, SIGUSR1);
pthread_sigmask (SIG_SETMASK, &ss, NULL);
sigdelset (&ss, SIGUSR1);
alarm (4);
pthread_barrier_wait (&b);
errno = 0 ;
s2 = paccept (s, NULL, 0, &ss, 0);
if (s2 != -1 || errno != EINTR)
{
puts ("paccept did not fail with EINTR");
return 1;
}
close (s);
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[akpm@linux-foundation.org: make it compile]
[akpm@linux-foundation.org: add sys_ni stub]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <linux-arch@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Roland McGrath <roland@redhat.com>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 06:29:20 +02:00
|
|
|
break;
|
2006-09-01 09:19:31 +02:00
|
|
|
default:
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
#endif /* __ARCH_WANT_SYS_SOCKETCALL */
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:23:39 +02:00
|
|
|
/**
|
|
|
|
* sock_register - add a socket protocol handler
|
|
|
|
* @ops: description of protocol
|
|
|
|
*
|
2005-04-17 00:20:36 +02:00
|
|
|
* This function is called by a protocol handler that wants to
|
|
|
|
* advertise its address family, and have it linked into the
|
2006-09-01 09:23:39 +02:00
|
|
|
* socket interface. The value ops->family coresponds to the
|
|
|
|
* socket system call protocol family.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
2006-08-10 06:03:17 +02:00
|
|
|
int sock_register(const struct net_proto_family *ops)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (ops->family >= NPROTO) {
|
2006-09-01 09:19:31 +02:00
|
|
|
printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
|
|
|
|
NPROTO);
|
2005-04-17 00:20:36 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
2006-09-01 09:23:39 +02:00
|
|
|
|
|
|
|
spin_lock(&net_family_lock);
|
|
|
|
if (net_families[ops->family])
|
|
|
|
err = -EEXIST;
|
|
|
|
else {
|
2006-09-01 09:19:31 +02:00
|
|
|
net_families[ops->family] = ops;
|
2005-04-17 00:20:36 +02:00
|
|
|
err = 0;
|
|
|
|
}
|
2006-09-01 09:23:39 +02:00
|
|
|
spin_unlock(&net_family_lock);
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
|
2005-04-17 00:20:36 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-09-01 09:23:39 +02:00
|
|
|
/**
|
|
|
|
* sock_unregister - remove a protocol handler
|
|
|
|
* @family: protocol family to remove
|
|
|
|
*
|
2005-04-17 00:20:36 +02:00
|
|
|
* This function is called by a protocol handler that wants to
|
|
|
|
* remove its address family, and have it unlinked from the
|
2006-09-01 09:23:39 +02:00
|
|
|
* new socket creation.
|
|
|
|
*
|
|
|
|
* If protocol handler is a module, then it can use module reference
|
|
|
|
* counts to protect against new references. If protocol handler is not
|
|
|
|
* a module then it needs to provide its own protection in
|
|
|
|
* the ops->create routine.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
2006-08-10 06:03:17 +02:00
|
|
|
void sock_unregister(int family)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-08-10 06:03:17 +02:00
|
|
|
BUG_ON(family < 0 || family >= NPROTO);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-01 09:23:39 +02:00
|
|
|
spin_lock(&net_family_lock);
|
2006-09-01 09:19:31 +02:00
|
|
|
net_families[family] = NULL;
|
2006-09-01 09:23:39 +02:00
|
|
|
spin_unlock(&net_family_lock);
|
|
|
|
|
|
|
|
synchronize_rcu();
|
|
|
|
|
2006-09-01 09:19:31 +02:00
|
|
|
printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-12-22 21:43:42 +01:00
|
|
|
static int __init sock_init(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* Initialize sock SLAB cache.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
2006-09-01 09:19:31 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
sk_init();
|
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* Initialize skbuff SLAB cache
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
skb_init();
|
|
|
|
|
|
|
|
/*
|
2006-09-01 09:19:31 +02:00
|
|
|
* Initialize the protocols module.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
init_inodecache();
|
|
|
|
register_filesystem(&sock_fs_type);
|
|
|
|
sock_mnt = kern_mount(&sock_fs_type);
|
2005-12-22 21:43:42 +01:00
|
|
|
|
|
|
|
/* The real protocol initialization is performed in later initcalls.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETFILTER
|
|
|
|
netfilter_init();
|
|
|
|
#endif
|
2005-12-22 21:58:55 +01:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-12-22 21:43:42 +01:00
|
|
|
core_initcall(sock_init); /* early initcall */
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
void socket_seq_show(struct seq_file *seq)
|
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
int counter = 0;
|
|
|
|
|
2006-04-11 07:52:50 +02:00
|
|
|
for_each_possible_cpu(cpu)
|
2006-09-01 09:19:31 +02:00
|
|
|
counter += per_cpu(sockets_in_use, cpu);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* It can be negative, by the way. 8) */
|
|
|
|
if (counter < 0)
|
|
|
|
counter = 0;
|
|
|
|
|
|
|
|
seq_printf(seq, "sockets: used %d\n", counter);
|
|
|
|
}
|
2006-09-01 09:19:31 +02:00
|
|
|
#endif /* CONFIG_PROC_FS */
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-03-22 08:58:08 +01:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
static long compat_sock_ioctl(struct file *file, unsigned cmd,
|
2006-09-01 09:19:31 +02:00
|
|
|
unsigned long arg)
|
2006-03-22 08:58:08 +01:00
|
|
|
{
|
|
|
|
struct socket *sock = file->private_data;
|
|
|
|
int ret = -ENOIOCTLCMD;
|
2008-06-03 18:14:03 +02:00
|
|
|
struct sock *sk;
|
|
|
|
struct net *net;
|
|
|
|
|
|
|
|
sk = sock->sk;
|
|
|
|
net = sock_net(sk);
|
2006-03-22 08:58:08 +01:00
|
|
|
|
|
|
|
if (sock->ops->compat_ioctl)
|
|
|
|
ret = sock->ops->compat_ioctl(sock, cmd, arg);
|
|
|
|
|
2008-06-03 18:14:03 +02:00
|
|
|
if (ret == -ENOIOCTLCMD &&
|
|
|
|
(cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
|
|
|
|
ret = compat_wext_handle_ioctl(net, cmd, arg);
|
|
|
|
|
2006-03-22 08:58:08 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-08-08 05:57:31 +02:00
|
|
|
int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
|
|
|
|
{
|
|
|
|
return sock->ops->bind(sock, addr, addrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_listen(struct socket *sock, int backlog)
|
|
|
|
{
|
|
|
|
return sock->ops->listen(sock, backlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
|
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
|
|
|
|
newsock);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err = sock->ops->accept(sock, *newsock, flags);
|
|
|
|
if (err < 0) {
|
|
|
|
sock_release(*newsock);
|
2007-10-11 06:09:04 +02:00
|
|
|
*newsock = NULL;
|
2006-08-08 05:57:31 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*newsock)->ops = sock->ops;
|
2008-12-19 04:35:10 +01:00
|
|
|
__module_get((*newsock)->ops->owner);
|
2006-08-08 05:57:31 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
|
2007-02-09 15:25:31 +01:00
|
|
|
int flags)
|
2006-08-08 05:57:31 +02:00
|
|
|
{
|
|
|
|
return sock->ops->connect(sock, addr, addrlen, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
|
|
|
|
int *addrlen)
|
|
|
|
{
|
|
|
|
return sock->ops->getname(sock, addr, addrlen, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
|
|
|
|
int *addrlen)
|
|
|
|
{
|
|
|
|
return sock->ops->getname(sock, addr, addrlen, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_getsockopt(struct socket *sock, int level, int optname,
|
|
|
|
char *optval, int *optlen)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs = get_fs();
|
|
|
|
int err;
|
|
|
|
|
|
|
|
set_fs(KERNEL_DS);
|
|
|
|
if (level == SOL_SOCKET)
|
|
|
|
err = sock_getsockopt(sock, level, optname, optval, optlen);
|
|
|
|
else
|
|
|
|
err = sock->ops->getsockopt(sock, level, optname, optval,
|
|
|
|
optlen);
|
|
|
|
set_fs(oldfs);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_setsockopt(struct socket *sock, int level, int optname,
|
|
|
|
char *optval, int optlen)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs = get_fs();
|
|
|
|
int err;
|
|
|
|
|
|
|
|
set_fs(KERNEL_DS);
|
|
|
|
if (level == SOL_SOCKET)
|
|
|
|
err = sock_setsockopt(sock, level, optname, optval, optlen);
|
|
|
|
else
|
|
|
|
err = sock->ops->setsockopt(sock, level, optname, optval,
|
|
|
|
optlen);
|
|
|
|
set_fs(oldfs);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_sendpage(struct socket *sock, struct page *page, int offset,
|
|
|
|
size_t size, int flags)
|
|
|
|
{
|
|
|
|
if (sock->ops->sendpage)
|
|
|
|
return sock->ops->sendpage(sock, page, offset, size, flags);
|
|
|
|
|
|
|
|
return sock_no_sendpage(sock, page, offset, size, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs = get_fs();
|
|
|
|
int err;
|
|
|
|
|
|
|
|
set_fs(KERNEL_DS);
|
|
|
|
err = sock->ops->ioctl(sock, cmd, arg);
|
|
|
|
set_fs(oldfs);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-11-13 03:10:39 +01:00
|
|
|
int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
|
|
|
|
{
|
|
|
|
return sock->ops->shutdown(sock, how);
|
|
|
|
}
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
EXPORT_SYMBOL(sock_create);
|
|
|
|
EXPORT_SYMBOL(sock_create_kern);
|
|
|
|
EXPORT_SYMBOL(sock_create_lite);
|
|
|
|
EXPORT_SYMBOL(sock_map_fd);
|
|
|
|
EXPORT_SYMBOL(sock_recvmsg);
|
|
|
|
EXPORT_SYMBOL(sock_register);
|
|
|
|
EXPORT_SYMBOL(sock_release);
|
|
|
|
EXPORT_SYMBOL(sock_sendmsg);
|
|
|
|
EXPORT_SYMBOL(sock_unregister);
|
|
|
|
EXPORT_SYMBOL(sock_wake_async);
|
|
|
|
EXPORT_SYMBOL(sockfd_lookup);
|
|
|
|
EXPORT_SYMBOL(kernel_sendmsg);
|
|
|
|
EXPORT_SYMBOL(kernel_recvmsg);
|
2006-08-08 05:57:31 +02:00
|
|
|
EXPORT_SYMBOL(kernel_bind);
|
|
|
|
EXPORT_SYMBOL(kernel_listen);
|
|
|
|
EXPORT_SYMBOL(kernel_accept);
|
|
|
|
EXPORT_SYMBOL(kernel_connect);
|
|
|
|
EXPORT_SYMBOL(kernel_getsockname);
|
|
|
|
EXPORT_SYMBOL(kernel_getpeername);
|
|
|
|
EXPORT_SYMBOL(kernel_getsockopt);
|
|
|
|
EXPORT_SYMBOL(kernel_setsockopt);
|
|
|
|
EXPORT_SYMBOL(kernel_sendpage);
|
|
|
|
EXPORT_SYMBOL(kernel_sock_ioctl);
|
2007-11-13 03:10:39 +01:00
|
|
|
EXPORT_SYMBOL(kernel_sock_shutdown);
|