]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/commitdiff
flag parameters: pipe
authorUlrich Drepper <drepper@redhat.com>
Thu, 24 Jul 2008 04:29:30 +0000 (21:29 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 24 Jul 2008 17:47:28 +0000 (10:47 -0700)
This patch introduces the new syscall pipe2 which is like pipe but it also
takes an additional parameter which takes a flag value.  This patch implements
the handling of O_CLOEXEC for the flag.  I did not add support for the new
syscall for the architectures which have a special sys_pipe implementation.  I
think the maintainers of those archs have the chance to go with the unified
implementation but that's up to them.

The implementation introduces do_pipe_flags.  I did that instead of changing
all callers of do_pipe because some of the callers are written in assembler.
I would probably screw up changing the assembly code.  To avoid breaking code
do_pipe is now a small wrapper around do_pipe_flags.  Once all callers are
changed over to do_pipe_flags the old do_pipe function can be removed.

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 <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>

#ifndef __NR_pipe2
# ifdef __x86_64__
#  define __NR_pipe2 293
# elif defined __i386__
#  define __NR_pipe2 331
# else
#  error "need __NR_pipe2"
# endif
#endif

int
main (void)
{
  int fd[2];
  if (syscall (__NR_pipe2, fd, 0) != 0)
    {
      puts ("pipe2(0) failed");
      return 1;
    }
  for (int i = 0; i < 2; ++i)
    {
      int coe = fcntl (fd[i], F_GETFD);
      if (coe == -1)
        {
          puts ("fcntl failed");
          return 1;
        }
      if (coe & FD_CLOEXEC)
        {
          printf ("pipe2(0) set close-on-exit for fd[%d]\n", i);
          return 1;
        }
    }
  close (fd[0]);
  close (fd[1]);

  if (syscall (__NR_pipe2, fd, O_CLOEXEC) != 0)
    {
      puts ("pipe2(O_CLOEXEC) failed");
      return 1;
    }
  for (int i = 0; i < 2; ++i)
    {
      int coe = fcntl (fd[i], F_GETFD);
      if (coe == -1)
        {
          puts ("fcntl failed");
          return 1;
        }
      if ((coe & FD_CLOEXEC) == 0)
        {
          printf ("pipe2(O_CLOEXEC) does not set close-on-exit for fd[%d]\n", i);
          return 1;
        }
    }
  close (fd[0]);
  close (fd[1]);

  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: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
15 files changed:
arch/ia64/ia32/sys_ia32.c
arch/ia64/kernel/sys_ia64.c
arch/mips/kernel/syscall.c
arch/parisc/hpux/sys_hpux.c
arch/sh/kernel/sys_sh32.c
arch/sparc/kernel/sys_sparc.c
arch/sparc64/kernel/sys_sparc.c
arch/x86/ia32/ia32entry.S
arch/x86/ia32/sys_ia32.c
arch/x86/kernel/syscall_table_32.S
arch/xtensa/kernel/syscall.c
fs/pipe.c
include/asm-x86/unistd_32.h
include/asm-x86/unistd_64.h
include/linux/fs.h

index 7e028ceb93ba6272abfaa9635bec1fc4ef6e6d6a..465116aecb85adf03880caf8285abeecc8c300e3 100644 (file)
@@ -1139,7 +1139,7 @@ sys32_pipe (int __user *fd)
        int retval;
        int fds[2];
 
-       retval = do_pipe(fds);
+       retval = do_pipe_flags(fds, 0);
        if (retval)
                goto out;
        if (copy_to_user(fd, fds, sizeof(fds)))
index 1eda194b95595110497adb009e457f8fe5c9d116..bcbb6d8792d3344352d226cb6e4fdda7c7d4182e 100644 (file)
@@ -160,7 +160,7 @@ sys_pipe (void)
        int fd[2];
        int retval;
 
-       retval = do_pipe(fd);
+       retval = do_pipe_flags(fd, 0);
        if (retval)
                goto out;
        retval = fd[0];
index 3523c8d12eda8707af06e8054320374626635852..343015a2f418fd9fb741ef6ae7b8821008e1af19 100644 (file)
@@ -52,7 +52,7 @@ asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
        int fd[2];
        int error, res;
 
-       error = do_pipe(fd);
+       error = do_pipe_flags(fd, 0);
        if (error) {
                res = error;
                goto out;
index 0c5b9dabb4754b350d3763f1ae31b1b3f706e64a..be255ebb609c98ddda7c280c0a1ca938d5d6f571 100644 (file)
@@ -448,7 +448,7 @@ int hpux_pipe(int *kstack_fildes)
        int error;
 
        lock_kernel();
-       error = do_pipe(kstack_fildes);
+       error = do_pipe_flags(kstack_fildes, 0);
        unlock_kernel();
        return error;
 }
index 125e493ead8291b18a6e78d9e1d95c4f75c63aeb..f0aa5c398656ca5b7fc1e4d0812b41951c57a79b 100644 (file)
@@ -29,7 +29,7 @@ asmlinkage int sys_pipe(unsigned long r4, unsigned long r5,
        int fd[2];
        int error;
 
-       error = do_pipe(fd);
+       error = do_pipe_flags(fd, 0);
        if (!error) {
                regs->regs[1] = fd[1];
                return fd[0];
index 3c6b49a53ae82f8e7a77dc67b4445eb753591f6f..4d73421559c3aa1c62c79b77210fdabc9f5497fb 100644 (file)
@@ -97,7 +97,7 @@ asmlinkage int sparc_pipe(struct pt_regs *regs)
        int fd[2];
        int error;
 
-       error = do_pipe(fd);
+       error = do_pipe_flags(fd, 0);
        if (error)
                goto out;
        regs->u_regs[UREG_I1] = fd[1];
index e1f4eba2e5760191a728f472345ead476fc72ba5..39749e32dc7e74ced52506ea3e90381c41d5f048 100644 (file)
@@ -418,7 +418,7 @@ asmlinkage long sparc_pipe(struct pt_regs *regs)
        int fd[2];
        int error;
 
-       error = do_pipe(fd);
+       error = do_pipe_flags(fd, 0);
        if (error)
                goto out;
        regs->u_regs[UREG_I1] = fd[1];
index 5614a8f7bed42e618978185cdbab535cdf03bcd5..18808b164570b2b31a32bf3ce0b821f9adf350d4 100644 (file)
@@ -830,4 +830,5 @@ ia32_sys_call_table:
        .quad sys_eventfd2
        .quad sys_epoll_create2
        .quad sys_dup3                  /* 330 */
+       .quad sys_pipe2
 ia32_syscall_end:
index f00afdf61e6735bbee01085fa24dc42ea529bed7..d3c64088b981f02fe5614ec53c3539b5bd3b703a 100644 (file)
@@ -238,7 +238,7 @@ asmlinkage long sys32_pipe(int __user *fd)
        int retval;
        int fds[2];
 
-       retval = do_pipe(fds);
+       retval = do_pipe_flags(fds, 0);
        if (retval)
                goto out;
        if (copy_to_user(fd, fds, sizeof(fds)))
index 24a3f1ea6a0e5bcc5afd7e048291efe98bff5554..66154769d52f666a8a327675dff63b485a4dd005 100644 (file)
@@ -330,3 +330,4 @@ ENTRY(sys_call_table)
        .long sys_eventfd2
        .long sys_epoll_create2
        .long sys_dup3                  /* 330 */
+       .long sys_pipe2
index f3e16efcd47a23bb16fdeaedb9dcf865a573de37..ac15ecbdf9199871f3e31ef7e013fd62e1ce3b06 100644 (file)
@@ -49,7 +49,7 @@ asmlinkage long xtensa_pipe(int __user *userfds)
        int fd[2];
        int error;
 
-       error = do_pipe(fd);
+       error = do_pipe_flags(fd, 0);
        if (!error) {
                if (copy_to_user(userfds, fd, 2 * sizeof(int)))
                        error = -EFAULT;
index 700f4e0d9572170a4de51527bc1698768a848afe..68e82061070cfbcd8c80239678ba116ca79d9753 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -1027,12 +1027,15 @@ struct file *create_read_pipe(struct file *wrf)
        return f;
 }
 
-int do_pipe(int *fd)
+int do_pipe_flags(int *fd, int flags)
 {
        struct file *fw, *fr;
        int error;
        int fdw, fdr;
 
+       if (flags & ~O_CLOEXEC)
+               return -EINVAL;
+
        fw = create_write_pipe();
        if (IS_ERR(fw))
                return PTR_ERR(fw);
@@ -1041,12 +1044,12 @@ int do_pipe(int *fd)
        if (IS_ERR(fr))
                goto err_write_pipe;
 
-       error = get_unused_fd();
+       error = get_unused_fd_flags(flags);
        if (error < 0)
                goto err_read_pipe;
        fdr = error;
 
-       error = get_unused_fd();
+       error = get_unused_fd_flags(flags);
        if (error < 0)
                goto err_fdr;
        fdw = error;
@@ -1074,16 +1077,21 @@ int do_pipe(int *fd)
        return error;
 }
 
+int do_pipe(int *fd)
+{
+       return do_pipe_flags(fd, 0);
+}
+
 /*
  * sys_pipe() is the normal C calling standard for creating
  * a pipe. It's not the way Unix traditionally does this, though.
  */
-asmlinkage long __weak sys_pipe(int __user *fildes)
+asmlinkage long __weak sys_pipe2(int __user *fildes, int flags)
 {
        int fd[2];
        int error;
 
-       error = do_pipe(fd);
+       error = do_pipe_flags(fd, flags);
        if (!error) {
                if (copy_to_user(fildes, fd, sizeof(fd))) {
                        sys_close(fd[0]);
@@ -1094,6 +1102,11 @@ asmlinkage long __weak sys_pipe(int __user *fildes)
        return error;
 }
 
+asmlinkage long __weak sys_pipe(int __user *fildes)
+{
+       return sys_pipe2(fildes, 0);
+}
+
 /*
  * pipefs should _never_ be mounted by userland - too much of security hassle,
  * no real gain from having the whole whorehouse mounted. So we don't need
index a1f6383bf695f1a4eacafd83249ce1f789a86a35..748a05c77da481b180830cbff50b7fbdd29e2298 100644 (file)
 #define __NR_eventfd2          328
 #define __NR_epoll_create2     329
 #define __NR_dup3              330
+#define __NR_pipe2             331
 
 #ifdef __KERNEL__
 
index f0fb2bd40cdbd0692993d22347a1a2f52fafe426..d2284b43ad58408439d958a4e16ca498e40889a9 100644 (file)
@@ -649,6 +649,8 @@ __SYSCALL(__NR_eventfd2, sys_eventfd2)
 __SYSCALL(__NR_epoll_create2, sys_epoll_create2)
 #define __NR_dup3                              292
 __SYSCALL(__NR_dup3, sys_dup3)
+#define __NR_pipe2                             293
+__SYSCALL(__NR_pipe2, sys_pipe2)
 
 
 #ifndef __NO_STUBS
index e5e6a244096cd03ad0b9e878e72b8702e2c9cb12..0e80cd717d32ac9b335629d2c8b64d2427abb504 100644 (file)
@@ -1777,6 +1777,7 @@ static inline void allow_write_access(struct file *file)
                atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
 }
 extern int do_pipe(int *);
+extern int do_pipe_flags(int *, int);
 extern struct file *create_read_pipe(struct file *f);
 extern struct file *create_write_pipe(void);
 extern void free_write_pipe(struct file *);