aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2008-07-24 00:29:30 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-07-24 13:47:28 -0400
commited8cae8ba01348bfd83333f4648dd807b04d7f08 (patch)
treec71a1c8e771c1c55728bb7c40612fbdcefbc858a
parent336dd1f70ff62d7dd8655228caed4c5bfc818c56 (diff)
flag parameters: pipe
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>
-rw-r--r--arch/ia64/ia32/sys_ia32.c2
-rw-r--r--arch/ia64/kernel/sys_ia64.c2
-rw-r--r--arch/mips/kernel/syscall.c2
-rw-r--r--arch/parisc/hpux/sys_hpux.c2
-rw-r--r--arch/sh/kernel/sys_sh32.c2
-rw-r--r--arch/sparc/kernel/sys_sparc.c2
-rw-r--r--arch/sparc64/kernel/sys_sparc.c2
-rw-r--r--arch/x86/ia32/ia32entry.S1
-rw-r--r--arch/x86/ia32/sys_ia32.c2
-rw-r--r--arch/x86/kernel/syscall_table_32.S1
-rw-r--r--arch/xtensa/kernel/syscall.c2
-rw-r--r--fs/pipe.c23
-rw-r--r--include/asm-x86/unistd_32.h1
-rw-r--r--include/asm-x86/unistd_64.h2
-rw-r--r--include/linux/fs.h1
15 files changed, 33 insertions, 14 deletions
diff --git a/arch/ia64/ia32/sys_ia32.c b/arch/ia64/ia32/sys_ia32.c
index 7e028ceb93ba..465116aecb85 100644
--- a/arch/ia64/ia32/sys_ia32.c
+++ b/arch/ia64/ia32/sys_ia32.c
@@ -1139,7 +1139,7 @@ sys32_pipe (int __user *fd)
1139 int retval; 1139 int retval;
1140 int fds[2]; 1140 int fds[2];
1141 1141
1142 retval = do_pipe(fds); 1142 retval = do_pipe_flags(fds, 0);
1143 if (retval) 1143 if (retval)
1144 goto out; 1144 goto out;
1145 if (copy_to_user(fd, fds, sizeof(fds))) 1145 if (copy_to_user(fd, fds, sizeof(fds)))
diff --git a/arch/ia64/kernel/sys_ia64.c b/arch/ia64/kernel/sys_ia64.c
index 1eda194b9559..bcbb6d8792d3 100644
--- a/arch/ia64/kernel/sys_ia64.c
+++ b/arch/ia64/kernel/sys_ia64.c
@@ -160,7 +160,7 @@ sys_pipe (void)
160 int fd[2]; 160 int fd[2];
161 int retval; 161 int retval;
162 162
163 retval = do_pipe(fd); 163 retval = do_pipe_flags(fd, 0);
164 if (retval) 164 if (retval)
165 goto out; 165 goto out;
166 retval = fd[0]; 166 retval = fd[0];
diff --git a/arch/mips/kernel/syscall.c b/arch/mips/kernel/syscall.c
index 3523c8d12eda..343015a2f418 100644
--- a/arch/mips/kernel/syscall.c
+++ b/arch/mips/kernel/syscall.c
@@ -52,7 +52,7 @@ asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
52 int fd[2]; 52 int fd[2];
53 int error, res; 53 int error, res;
54 54
55 error = do_pipe(fd); 55 error = do_pipe_flags(fd, 0);
56 if (error) { 56 if (error) {
57 res = error; 57 res = error;
58 goto out; 58 goto out;
diff --git a/arch/parisc/hpux/sys_hpux.c b/arch/parisc/hpux/sys_hpux.c
index 0c5b9dabb475..be255ebb609c 100644
--- a/arch/parisc/hpux/sys_hpux.c
+++ b/arch/parisc/hpux/sys_hpux.c
@@ -448,7 +448,7 @@ int hpux_pipe(int *kstack_fildes)
448 int error; 448 int error;
449 449
450 lock_kernel(); 450 lock_kernel();
451 error = do_pipe(kstack_fildes); 451 error = do_pipe_flags(kstack_fildes, 0);
452 unlock_kernel(); 452 unlock_kernel();
453 return error; 453 return error;
454} 454}
diff --git a/arch/sh/kernel/sys_sh32.c b/arch/sh/kernel/sys_sh32.c
index 125e493ead82..f0aa5c398656 100644
--- a/arch/sh/kernel/sys_sh32.c
+++ b/arch/sh/kernel/sys_sh32.c
@@ -29,7 +29,7 @@ asmlinkage int sys_pipe(unsigned long r4, unsigned long r5,
29 int fd[2]; 29 int fd[2];
30 int error; 30 int error;
31 31
32 error = do_pipe(fd); 32 error = do_pipe_flags(fd, 0);
33 if (!error) { 33 if (!error) {
34 regs->regs[1] = fd[1]; 34 regs->regs[1] = fd[1];
35 return fd[0]; 35 return fd[0];
diff --git a/arch/sparc/kernel/sys_sparc.c b/arch/sparc/kernel/sys_sparc.c
index 3c6b49a53ae8..4d73421559c3 100644
--- a/arch/sparc/kernel/sys_sparc.c
+++ b/arch/sparc/kernel/sys_sparc.c
@@ -97,7 +97,7 @@ asmlinkage int sparc_pipe(struct pt_regs *regs)
97 int fd[2]; 97 int fd[2];
98 int error; 98 int error;
99 99
100 error = do_pipe(fd); 100 error = do_pipe_flags(fd, 0);
101 if (error) 101 if (error)
102 goto out; 102 goto out;
103 regs->u_regs[UREG_I1] = fd[1]; 103 regs->u_regs[UREG_I1] = fd[1];
diff --git a/arch/sparc64/kernel/sys_sparc.c b/arch/sparc64/kernel/sys_sparc.c
index e1f4eba2e576..39749e32dc7e 100644
--- a/arch/sparc64/kernel/sys_sparc.c
+++ b/arch/sparc64/kernel/sys_sparc.c
@@ -418,7 +418,7 @@ asmlinkage long sparc_pipe(struct pt_regs *regs)
418 int fd[2]; 418 int fd[2];
419 int error; 419 int error;
420 420
421 error = do_pipe(fd); 421 error = do_pipe_flags(fd, 0);
422 if (error) 422 if (error)
423 goto out; 423 goto out;
424 regs->u_regs[UREG_I1] = fd[1]; 424 regs->u_regs[UREG_I1] = fd[1];
diff --git a/arch/x86/ia32/ia32entry.S b/arch/x86/ia32/ia32entry.S
index 5614a8f7bed4..18808b164570 100644
--- a/arch/x86/ia32/ia32entry.S
+++ b/arch/x86/ia32/ia32entry.S
@@ -830,4 +830,5 @@ ia32_sys_call_table:
830 .quad sys_eventfd2 830 .quad sys_eventfd2
831 .quad sys_epoll_create2 831 .quad sys_epoll_create2
832 .quad sys_dup3 /* 330 */ 832 .quad sys_dup3 /* 330 */
833 .quad sys_pipe2
833ia32_syscall_end: 834ia32_syscall_end:
diff --git a/arch/x86/ia32/sys_ia32.c b/arch/x86/ia32/sys_ia32.c
index f00afdf61e67..d3c64088b981 100644
--- a/arch/x86/ia32/sys_ia32.c
+++ b/arch/x86/ia32/sys_ia32.c
@@ -238,7 +238,7 @@ asmlinkage long sys32_pipe(int __user *fd)
238 int retval; 238 int retval;
239 int fds[2]; 239 int fds[2];
240 240
241 retval = do_pipe(fds); 241 retval = do_pipe_flags(fds, 0);
242 if (retval) 242 if (retval)
243 goto out; 243 goto out;
244 if (copy_to_user(fd, fds, sizeof(fds))) 244 if (copy_to_user(fd, fds, sizeof(fds)))
diff --git a/arch/x86/kernel/syscall_table_32.S b/arch/x86/kernel/syscall_table_32.S
index 24a3f1ea6a0e..66154769d52f 100644
--- a/arch/x86/kernel/syscall_table_32.S
+++ b/arch/x86/kernel/syscall_table_32.S
@@ -330,3 +330,4 @@ ENTRY(sys_call_table)
330 .long sys_eventfd2 330 .long sys_eventfd2
331 .long sys_epoll_create2 331 .long sys_epoll_create2
332 .long sys_dup3 /* 330 */ 332 .long sys_dup3 /* 330 */
333 .long sys_pipe2
diff --git a/arch/xtensa/kernel/syscall.c b/arch/xtensa/kernel/syscall.c
index f3e16efcd47a..ac15ecbdf919 100644
--- a/arch/xtensa/kernel/syscall.c
+++ b/arch/xtensa/kernel/syscall.c
@@ -49,7 +49,7 @@ asmlinkage long xtensa_pipe(int __user *userfds)
49 int fd[2]; 49 int fd[2];
50 int error; 50 int error;
51 51
52 error = do_pipe(fd); 52 error = do_pipe_flags(fd, 0);
53 if (!error) { 53 if (!error) {
54 if (copy_to_user(userfds, fd, 2 * sizeof(int))) 54 if (copy_to_user(userfds, fd, 2 * sizeof(int)))
55 error = -EFAULT; 55 error = -EFAULT;
diff --git a/fs/pipe.c b/fs/pipe.c
index 700f4e0d9572..68e82061070c 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -1027,12 +1027,15 @@ struct file *create_read_pipe(struct file *wrf)
1027 return f; 1027 return f;
1028} 1028}
1029 1029
1030int do_pipe(int *fd) 1030int do_pipe_flags(int *fd, int flags)
1031{ 1031{
1032 struct file *fw, *fr; 1032 struct file *fw, *fr;
1033 int error; 1033 int error;
1034 int fdw, fdr; 1034 int fdw, fdr;
1035 1035
1036 if (flags & ~O_CLOEXEC)
1037 return -EINVAL;
1038
1036 fw = create_write_pipe(); 1039 fw = create_write_pipe();
1037 if (IS_ERR(fw)) 1040 if (IS_ERR(fw))
1038 return PTR_ERR(fw); 1041 return PTR_ERR(fw);
@@ -1041,12 +1044,12 @@ int do_pipe(int *fd)
1041 if (IS_ERR(fr)) 1044 if (IS_ERR(fr))
1042 goto err_write_pipe; 1045 goto err_write_pipe;
1043 1046
1044 error = get_unused_fd(); 1047 error = get_unused_fd_flags(flags);
1045 if (error < 0) 1048 if (error < 0)
1046 goto err_read_pipe; 1049 goto err_read_pipe;
1047 fdr = error; 1050 fdr = error;
1048 1051
1049 error = get_unused_fd(); 1052 error = get_unused_fd_flags(flags);
1050 if (error < 0) 1053 if (error < 0)
1051 goto err_fdr; 1054 goto err_fdr;
1052 fdw = error; 1055 fdw = error;
@@ -1074,16 +1077,21 @@ int do_pipe(int *fd)
1074 return error; 1077 return error;
1075} 1078}
1076 1079
1080int do_pipe(int *fd)
1081{
1082 return do_pipe_flags(fd, 0);
1083}
1084
1077/* 1085/*
1078 * sys_pipe() is the normal C calling standard for creating 1086 * sys_pipe() is the normal C calling standard for creating
1079 * a pipe. It's not the way Unix traditionally does this, though. 1087 * a pipe. It's not the way Unix traditionally does this, though.
1080 */ 1088 */
1081asmlinkage long __weak sys_pipe(int __user *fildes) 1089asmlinkage long __weak sys_pipe2(int __user *fildes, int flags)
1082{ 1090{
1083 int fd[2]; 1091 int fd[2];
1084 int error; 1092 int error;
1085 1093
1086 error = do_pipe(fd); 1094 error = do_pipe_flags(fd, flags);
1087 if (!error) { 1095 if (!error) {
1088 if (copy_to_user(fildes, fd, sizeof(fd))) { 1096 if (copy_to_user(fildes, fd, sizeof(fd))) {
1089 sys_close(fd[0]); 1097 sys_close(fd[0]);
@@ -1094,6 +1102,11 @@ asmlinkage long __weak sys_pipe(int __user *fildes)
1094 return error; 1102 return error;
1095} 1103}
1096 1104
1105asmlinkage long __weak sys_pipe(int __user *fildes)
1106{
1107 return sys_pipe2(fildes, 0);
1108}
1109
1097/* 1110/*
1098 * pipefs should _never_ be mounted by userland - too much of security hassle, 1111 * pipefs should _never_ be mounted by userland - too much of security hassle,
1099 * no real gain from having the whole whorehouse mounted. So we don't need 1112 * no real gain from having the whole whorehouse mounted. So we don't need
diff --git a/include/asm-x86/unistd_32.h b/include/asm-x86/unistd_32.h
index a1f6383bf695..748a05c77da4 100644
--- a/include/asm-x86/unistd_32.h
+++ b/include/asm-x86/unistd_32.h
@@ -336,6 +336,7 @@
336#define __NR_eventfd2 328 336#define __NR_eventfd2 328
337#define __NR_epoll_create2 329 337#define __NR_epoll_create2 329
338#define __NR_dup3 330 338#define __NR_dup3 330
339#define __NR_pipe2 331
339 340
340#ifdef __KERNEL__ 341#ifdef __KERNEL__
341 342
diff --git a/include/asm-x86/unistd_64.h b/include/asm-x86/unistd_64.h
index f0fb2bd40cdb..d2284b43ad58 100644
--- a/include/asm-x86/unistd_64.h
+++ b/include/asm-x86/unistd_64.h
@@ -649,6 +649,8 @@ __SYSCALL(__NR_eventfd2, sys_eventfd2)
649__SYSCALL(__NR_epoll_create2, sys_epoll_create2) 649__SYSCALL(__NR_epoll_create2, sys_epoll_create2)
650#define __NR_dup3 292 650#define __NR_dup3 292
651__SYSCALL(__NR_dup3, sys_dup3) 651__SYSCALL(__NR_dup3, sys_dup3)
652#define __NR_pipe2 293
653__SYSCALL(__NR_pipe2, sys_pipe2)
652 654
653 655
654#ifndef __NO_STUBS 656#ifndef __NO_STUBS
diff --git a/include/linux/fs.h b/include/linux/fs.h
index e5e6a244096c..0e80cd717d32 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1777,6 +1777,7 @@ static inline void allow_write_access(struct file *file)
1777 atomic_inc(&file->f_path.dentry->d_inode->i_writecount); 1777 atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
1778} 1778}
1779extern int do_pipe(int *); 1779extern int do_pipe(int *);
1780extern int do_pipe_flags(int *, int);
1780extern struct file *create_read_pipe(struct file *f); 1781extern struct file *create_read_pipe(struct file *f);
1781extern struct file *create_write_pipe(void); 1782extern struct file *create_write_pipe(void);
1782extern void free_write_pipe(struct file *); 1783extern void free_write_pipe(struct file *);