reintroduce accept4
Subsystems (5)
PATCH 5/35 reintroduce accept4
From: Ulrich Drepper <[email protected]>
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
<[email protected]>
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 intaccept4(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 intcreate_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;
}
intmain(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);
}
---
arch/x86/include/asm/unistd_64.h | 4 - include/linux/net.h | 6 -- include/linux/syscalls.h | 3 - kernel/sys_ni.c | 2 net/compat.c | 50 +----------------- net/socket.c | 80 +++-------------------------- 6 files changed, 21 insertions(+), 124 deletions(-)
arch/x86/include/asm/unistd_64.h renamed from arch/x86/include/asm/unistd_64.h~reintroduce-accept4 +2 -2
@@ -639,8 +639,8 @@ __SYSCALL(__NR_fallocate, sys_fallocate) __SYSCALL(__NR_timerfd_settime, sys_timerfd_settime)#define __NR_timerfd_gettime 287 __SYSCALL(__NR_timerfd_gettime, sys_timerfd_gettime) -#define __NR_paccept 288 -__SYSCALL(__NR_paccept, sys_paccept) +#define __NR_accept4 288 +__SYSCALL(__NR_accept4, sys_accept4)#define __NR_signalfd4 289 __SYSCALL(__NR_signalfd4, sys_signalfd4)#define __NR_eventfd2 290
include/linux/net.h renamed from include/linux/net.h~reintroduce-accept4 +2 -4
@@ -40,7 +40,7 @@#define SYS_GETSOCKOPT 15/* sys_getsockopt(2) */ #define SYS_SENDMSG 16/* sys_sendmsg(2) */ #define SYS_RECVMSG 17/* sys_recvmsg(2) */ -#define SYS_PACCEPT 18/* sys_paccept(2) */ +#define SYS_ACCEPT4 18/* sys_accept4(2) */ typedef enum { SS_FREE = 0, /* not allocated */
@@ -100,7 +100,7 @@ enum sock_type {* remaining bits are used as flags. */ #define SOCK_TYPE_MASK 0xf -/* Flags for socket, socketpair, paccept */ +/* Flags for socket, socketpair, accept4 */ #define SOCK_CLOEXEC O_CLOEXEC#ifndef SOCK_NONBLOCK #define SOCK_NONBLOCK O_NONBLOCK
@@ -223,8 +223,6 @@ extern int sock_map_fd(struct sockextern struct socket *sockfd_lookup (int fd,int *err);#define sockfd_put (sock) fput(sock->file)extern int net_ratelimit (void ); -extern long do_accept (int fd,struct sockaddr __user *upeer_sockaddr, -int __user *upeer_addrlen,int flags);#define net_random () random32()#define net_srandom (seed) srandom32((__force u32)seed)
include/linux/syscalls.h renamed from include/linux/syscalls.h~reintroduce-accept4 +1 -2
@@ -410,8 +410,7 @@ asmlinkage long sys_getsockopt(int fd, iasmlinkage long sys_bind (int ,struct sockaddr __user *,int );asmlinkage long sys_connect (int ,struct sockaddr __user *,int );asmlinkage long sys_accept (int ,struct sockaddr __user *,int __user *); -asmlinkage long sys_paccept (int ,struct sockaddr __user *,int __user *, -const __user sigset_t *,size_t ,int ); +asmlinkage long sys_accept4 (int ,struct sockaddr __user *,int __user *,int );asmlinkage long sys_getsockname (int ,struct sockaddr __user *,int __user *);asmlinkage long sys_getpeername (int ,struct sockaddr __user *,int __user *);asmlinkage long sys_send (int ,void __user *,size_t ,unsigned );
kernel/sys_ni.c renamed from kernel/sys_ni.c~reintroduce-accept4 +1 -1
@@ -31,7 +31,7 @@ cond_syscall(sys_socketpair);cond_syscall (sys_bind);cond_syscall (sys_listen);cond_syscall (sys_accept); -cond_syscall (sys_paccept); +cond_syscall (sys_accept4);cond_syscall (sys_connect);cond_syscall (sys_getsockname);cond_syscall (sys_getpeername);
net/compat.c renamed from net/compat.c~reintroduce-accept4 +5 -45
80 lines not on this page · raw message
net/socket.c renamed from net/socket.c~reintroduce-accept4 +10 -70
119 lines not on this page · raw message
_
Quoted 9 lines · On Wed, 19 Nov 2008, [email protected] wrote:
From: Ulrich Drepper <[email protected]>
Introduce a new
accept4()system call. The addition of this system call/*
test_accept4.c#ifdef
__x86_64__
#defineSYS_accept4288
#elif__i386__
^^^^^^^^ defined(__i386__)
Quoted 5 lines
#define
USE_SOCKETCALL1
#defineSYS_ACCEPT418
#else
#error "Sorry -- don't know the syscall # on this architecture" #endif
Anyway, it's just a test program. Succeeded on m68k using socketcall.
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Quoted 13 lines · Geert Uytterhoeven
Quoted 9 lines · On Wed, 19 Nov 2008, [email protected] wrote:
From: Ulrich Drepper <[email protected]>
Introduce a new
accept4()system call. The addition of this system call/*
test_accept4.c#ifdef
__x86_64__
#defineSYS_accept4288
#elif__i386__^^^^^^^^ defined(__i386__)
Quoted 7 lines
Quoted 5 lines
#define
USE_SOCKETCALL1
#defineSYS_ACCEPT418
#else
#error "Sorry -- don't know the syscall # on this architecture" #endifAnyway, it's just a test program. Succeeded on m68k using socketcall.
It _should_ work even without the 'defined()'. Didn't it?
Unknown symbols should expand to '0' in preprocessor value evaluation. Of course, the compiler may warn about the practice, but it shouldn't be technically wrong.
Linus
A message that is not in the archive (sent off-list or not yet received): alpine.LFD.2.00.0811201412040.21698-nfNrOhbfy2R17+2ddN/4kux8cNe9sq/[email protected]
Quoted 22 lines · Linus Torvalds
Quoted 19 lines · Geert Uytterhoeven
Quoted 9 lines · On Wed, 19 Nov 2008, akpm-de/[email protected]…
From: Ulrich Drepper <[email protected]>
Introduce a new
accept4()system call. The addition of this system call/*
test_accept4.c#ifdef
__x86_64__
#defineSYS_accept4288
#elif__i386__^^^^^^^^ defined(__i386__)Quoted 5 lines
#define
USE_SOCKETCALL1
#defineSYS_ACCEPT418
#else
#error "Sorry -- don't know the syscall # on this architecture" #endif
Quoted 3 lines
Quoted 1 line
Anyway, it's just a test program. Succeeded on m68k using socketcall.
It _should_ work even without the '
defined()'. Didn't it?
Lots of symbols are defined with no value, which will result in an empty #if expression and a cpp error.
Signature · 6 lines
Mathematics is the supreme nostalgia of our time. -- To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 9 lines · Linus Torvalds
Quoted 5 lines · Geert Uytterhoeven
Quoted 3 lines · On Wed, 19 Nov 2008, akpm-de/[email protected]…
From: Ulrich Drepper <[email protected]>
Introduce a new
accept4()system call. The addition of this system call
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
/*
test_accept4.c#ifdef
__x86_64__
#defineSYS_accept4288
#elif__i386__^^^^^^^^ defined(__i386__)
Whoops.
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
#define
USE_SOCKETCALL1
#defineSYS_ACCEPT418
#else
#error "Sorry -- don't know the syscall # on this architecture" #endifAnyway, it's just a test program. Succeeded on m68k using socketcall.
It _should_ work even without the '
defined()'. Didn't it?
It did. I successfully used it for testing on i386.
Quoted 3 lines
Unknown symbols should expand to '0' in preprocessor value evaluation. Of course, the compiler may warn about the practice, but it shouldn't be technically wrong.
Signature · 9 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ git://git.kernel.org/pub/scm/docs/man-pages/man-pages.git man-pages online: http://www.kernel.org/doc/man-pages/online_pages.html Found a bug? http://www.kernel.org/doc/man-pages/reporting_bugs.html -- To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 20 lines · Linus Torvalds
Quoted 19 lines · Geert Uytterhoeven
Quoted 9 lines · On Wed, 19 Nov 2008, akpm-de/[email protected]…
From: Ulrich Drepper <[email protected]>
Introduce a new
accept4()system call. The addition of this system call/*
test_accept4.c#ifdef
__x86_64__
#defineSYS_accept4288
#elif__i386__^^^^^^^^ defined(__i386__)Quoted 5 lines
#define
USE_SOCKETCALL1
#defineSYS_ACCEPT418
#else
#error "Sorry -- don't know the syscall # on this architecture" #endif
Quoted 3 lines
Quoted 1 line
Anyway, it's just a test program. Succeeded on m68k using socketcall.
It _should_ work even without the '
defined()'. Didn't it?
Yes it worked.
Quoted 3 lines
Unknown symbols should expand to '0' in preprocessor value evaluation. Of course, the compiler may warn about the practice, but it shouldn't be technically wrong.
gcc 4.4 complains:
http://www.cyrius.com/journal/gcc/gcc-4.4-preprocessor-errors
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html