Skip to contentSkip to conversation

flag parameters: paccept

PATCH In review Ulrich Drepper 4 participants 8 messages May 6, 2008 – May 13, 2008

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 2/18 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;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 include/asm-x86/unistd_64.h |    2 +
 include/linux/net.h         |    3 +
 include/linux/syscalls.h    |    2 +
 net/compat.c                |   52 +++++++++++++++++++++++++++++---
 net/socket.c                |   71 +++++++++++++++++++++++++++++++++++++++-----
 5 files changed, 118 insertions(+), 12 deletions(-)
include/asm-x86/unistd_64.h +2 -0
index fe26e36..d751135 100644
@@ -639,6 +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)
 
 
 #ifndef __NO_STUBS
include/linux/net.h +3 -0
index 0c1ad01..b4ad432 100644
@@ -47,6 +47,7 @@ struct net;
 #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)		*/
 
 typedef enum {
 	SS_FREE = 0,			/* not allocated		*/
@@ -219,6 +220,8 @@ extern int 	     sock_map_fd(struct socket *sock, int flags);
 extern 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 +2 -0
index 0522f36..e022405 100644
@@ -409,6 +409,8 @@ asmlinkage long sys_getsockopt(int fd, int level, int optname,
 asmlinkage 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 sigset_t *, size_t, 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);
net/compat.c +48 -4

83 lines not on this page · raw message

net/socket.c +63 -8

121 lines not on this page · raw message

#1 lore raw
Quoted 24 lines · On Tue, 6 May 2008 17:18:07 -0400 Ulrich Drepper <[email protected]> wrote:

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.

Quoted 6 lines

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.

I have a bit of a mystery going on here.

Quoted 29 lines

...

net/socket.c +27 -0
+asmlinkage long sys_paccept(int fd, struct sockaddr __user *upeer_sockaddr,
+			    int __user *upeer_addrlen,
+			    const sigset_t __user *sigmask,
+			    size_t sigsetsize, int flags)
+{
+	sigset_t ksigmask, sigsaved;
+	int ret;
+
+	if (sigmask) {
+		/* XXX: Don't preclude handling different sized sigset_t's.  */
+		if (sigsetsize != sizeof(sigset_t))
+			return -EINVAL;
+		if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
+			return -EFAULT;
+
+		sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
+		sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
+        }
+
+	ret = do_accept(fd, upeer_sockaddr, upeer_addrlen, flags);
+
+	if (ret < 0 && signal_pending(current)) {
+		/*
+		 * Don't restore the signal mask yet. Let do_signal() deliver
+		 * the signal on the way back to userspace, before the signal
+		 * mask is restored.
+		 */

net/socket.c out_fd: · line 1532

Quoted 10 lines
net/socket.c +10 -0
+		if (sigmask) {
+			memcpy(&current->saved_sigmask, &sigsaved,
+			       sizeof(sigsaved));
+			set_thread_flag(TIF_RESTORE_SIGMASK);
+		}
+	} else if (sigmask)
+		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
+
+	return ret;
+}

Some architectures (including x86) do not implement TIF_RESTORE_SIGMASK.

Ah, you must have prepared the patches against something prehistoric like 2.6.25. Please prefer to prepare 2.6.x patches against the 2.6.x tree, not the 2.6.x-1 tree?

Whatever you're trying to do here, we'll need to find a non-arch-specific way of doing it.

maintainer
#2 lore raw

From: Andrew Morton <[email protected]>
Date: Thu, 8 May 2008 15:38:22 -0700

Quoted 2 lines

Whatever you're trying to do here, we'll need to find a non-arch-specific way of doing it.

Probably: set_restore_sigmask();

#3 lore raw
In reply to Andrew Morton
Quoted 1 line

Some architectures (including x86) do not implement TIF_RESTORE_SIGMASK.

It's now spelled:

       set_restore_sigmask();

For the other syscalls that depend on this (e.g. sys_ppoll), we just put the whole definition in #ifdef HAVE_SET_RESTORE_SIGMASK.

The few machines that don't have RESTORE_SIGMASK support of some flavor, also have not assigned numbers to the syscalls that need it.

Thanks,
Roland

maintainer
#4 lore raw

From: Roland McGrath <[email protected]>
Date: Thu, 8 May 2008 15:50:43 -0700 (PDT)

Quoted 9 lines

It's now spelled:

       set_restore_sigmask();

For the other syscalls that depend on this (e.g. sys_ppoll), we just put the whole definition in #ifdef HAVE_SET_RESTORE_SIGMASK.

The few machines that don't have RESTORE_SIGMASK support of some flavor, also have not assigned numbers to the syscalls that need it.

I notice that this macro also sets the sigpending bit, although some paths that end up calling set_restore_sigmask() already do that.

#5 lore raw
In reply to Andrew Morton

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 3 lines · Andrew Morton

Ah, you must have prepared the patches against something prehistoric like 2.6.25. Please prefer to prepare 2.6.x patches against the 2.6.x tree, not the 2.6.x-1 tree?

Well, pre-historic == before 2008-4-30. That's apparently when Roland's patch to change this went in. Yes, I wrote the code before that day and didn't notice the pselect implementation changed. Sorry.

- --
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖ -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkgkBOkACgkQ2ijCOnn/RHSrgACeP1pFdt4QDnE6OOJHy/d7D5H+ sHQAoLpnEb3FwKh89viFtWXOVfDaVyWf
=tiZM
-----END PGP SIGNATURE-----

#6 lore raw
In reply to Ulrich Drepper
Quoted 24 lines · On Tue, 6 May 2008 17:18:07 -0400 Ulrich Drepper <[email protected]> wrote:

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.

Quoted 5 lines

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.

Am getting moderately bored of these patches :(

arm exploded thusly:

net/socket.c: In function `sys_paccept':
net/socket.c:1534: error: implicit declaration of function `set_restore_sigmask'

because afacit arm doesn't implement set_restore_sigmask() and nor does it need to, because it doesn't set HAVE_SET_RESTORE_SIGMASK.

I will continue to fumble along in the rc2-mm1 direction. Fixes against that kernel would suit, thanks.

#7 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 2 lines · Andrew Morton

because afacit arm doesn't implement set_restore_sigmask() and nor does it need to, because it doesn't set HAVE_SET_RESTORE_SIGMASK.

pselect isn't define if HAVE_SET_RESTORE_SIGMASK isn't defined. That's fine since pselect offers nothing over select except the signal handling. For paccept we'll have to be a bit more sophisticated, I'm afraid. Patch in a few minutes.

- --
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖ -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFIKRw32ijCOnn/RHQRArz0AJ9RXoICEi/1Wmc2g/Xg7QMpaRfMjACggwhC ON/2WP6i03QBerzu6qe/iPs=
=S2H+
-----END PGP SIGNATURE-----