Skip to content

Commit ed8cae8

Browse files
Ulrich Dreppertorvalds
authored andcommitted
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>
1 parent 336dd1f commit ed8cae8

File tree

15 files changed

+33
-14
lines changed

15 files changed

+33
-14
lines changed

arch/ia64/ia32/sys_ia32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ sys32_pipe (int __user *fd)
11391139
int retval;
11401140
int fds[2];
11411141

1142-
retval = do_pipe(fds);
1142+
retval = do_pipe_flags(fds, 0);
11431143
if (retval)
11441144
goto out;
11451145
if (copy_to_user(fd, fds, sizeof(fds)))

arch/ia64/kernel/sys_ia64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ sys_pipe (void)
160160
int fd[2];
161161
int retval;
162162

163-
retval = do_pipe(fd);
163+
retval = do_pipe_flags(fd, 0);
164164
if (retval)
165165
goto out;
166166
retval = fd[0];

arch/mips/kernel/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
5252
int fd[2];
5353
int error, res;
5454

55-
error = do_pipe(fd);
55+
error = do_pipe_flags(fd, 0);
5656
if (error) {
5757
res = error;
5858
goto out;

arch/parisc/hpux/sys_hpux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ int hpux_pipe(int *kstack_fildes)
448448
int error;
449449

450450
lock_kernel();
451-
error = do_pipe(kstack_fildes);
451+
error = do_pipe_flags(kstack_fildes, 0);
452452
unlock_kernel();
453453
return error;
454454
}

arch/sh/kernel/sys_sh32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ asmlinkage int sys_pipe(unsigned long r4, unsigned long r5,
2929
int fd[2];
3030
int error;
3131

32-
error = do_pipe(fd);
32+
error = do_pipe_flags(fd, 0);
3333
if (!error) {
3434
regs->regs[1] = fd[1];
3535
return fd[0];

arch/sparc/kernel/sys_sparc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ asmlinkage int sparc_pipe(struct pt_regs *regs)
9797
int fd[2];
9898
int error;
9999

100-
error = do_pipe(fd);
100+
error = do_pipe_flags(fd, 0);
101101
if (error)
102102
goto out;
103103
regs->u_regs[UREG_I1] = fd[1];

arch/sparc64/kernel/sys_sparc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ asmlinkage long sparc_pipe(struct pt_regs *regs)
418418
int fd[2];
419419
int error;
420420

421-
error = do_pipe(fd);
421+
error = do_pipe_flags(fd, 0);
422422
if (error)
423423
goto out;
424424
regs->u_regs[UREG_I1] = fd[1];

arch/x86/ia32/ia32entry.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,5 @@ ia32_sys_call_table:
830830
.quad sys_eventfd2
831831
.quad sys_epoll_create2
832832
.quad sys_dup3 /* 330 */
833+
.quad sys_pipe2
833834
ia32_syscall_end:

arch/x86/ia32/sys_ia32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ asmlinkage long sys32_pipe(int __user *fd)
238238
int retval;
239239
int fds[2];
240240

241-
retval = do_pipe(fds);
241+
retval = do_pipe_flags(fds, 0);
242242
if (retval)
243243
goto out;
244244
if (copy_to_user(fd, fds, sizeof(fds)))

arch/x86/kernel/syscall_table_32.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,4 @@ ENTRY(sys_call_table)
330330
.long sys_eventfd2
331331
.long sys_epoll_create2
332332
.long sys_dup3 /* 330 */
333+
.long sys_pipe2

arch/xtensa/kernel/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ asmlinkage long xtensa_pipe(int __user *userfds)
4949
int fd[2];
5050
int error;
5151

52-
error = do_pipe(fd);
52+
error = do_pipe_flags(fd, 0);
5353
if (!error) {
5454
if (copy_to_user(userfds, fd, 2 * sizeof(int)))
5555
error = -EFAULT;

fs/pipe.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,12 +1027,15 @@ struct file *create_read_pipe(struct file *wrf)
10271027
return f;
10281028
}
10291029

1030-
int do_pipe(int *fd)
1030+
int do_pipe_flags(int *fd, int flags)
10311031
{
10321032
struct file *fw, *fr;
10331033
int error;
10341034
int fdw, fdr;
10351035

1036+
if (flags & ~O_CLOEXEC)
1037+
return -EINVAL;
1038+
10361039
fw = create_write_pipe();
10371040
if (IS_ERR(fw))
10381041
return PTR_ERR(fw);
@@ -1041,12 +1044,12 @@ int do_pipe(int *fd)
10411044
if (IS_ERR(fr))
10421045
goto err_write_pipe;
10431046

1044-
error = get_unused_fd();
1047+
error = get_unused_fd_flags(flags);
10451048
if (error < 0)
10461049
goto err_read_pipe;
10471050
fdr = error;
10481051

1049-
error = get_unused_fd();
1052+
error = get_unused_fd_flags(flags);
10501053
if (error < 0)
10511054
goto err_fdr;
10521055
fdw = error;
@@ -1074,16 +1077,21 @@ int do_pipe(int *fd)
10741077
return error;
10751078
}
10761079

1080+
int do_pipe(int *fd)
1081+
{
1082+
return do_pipe_flags(fd, 0);
1083+
}
1084+
10771085
/*
10781086
* sys_pipe() is the normal C calling standard for creating
10791087
* a pipe. It's not the way Unix traditionally does this, though.
10801088
*/
1081-
asmlinkage long __weak sys_pipe(int __user *fildes)
1089+
asmlinkage long __weak sys_pipe2(int __user *fildes, int flags)
10821090
{
10831091
int fd[2];
10841092
int error;
10851093

1086-
error = do_pipe(fd);
1094+
error = do_pipe_flags(fd, flags);
10871095
if (!error) {
10881096
if (copy_to_user(fildes, fd, sizeof(fd))) {
10891097
sys_close(fd[0]);
@@ -1094,6 +1102,11 @@ asmlinkage long __weak sys_pipe(int __user *fildes)
10941102
return error;
10951103
}
10961104

1105+
asmlinkage long __weak sys_pipe(int __user *fildes)
1106+
{
1107+
return sys_pipe2(fildes, 0);
1108+
}
1109+
10971110
/*
10981111
* pipefs should _never_ be mounted by userland - too much of security hassle,
10991112
* no real gain from having the whole whorehouse mounted. So we don't need

include/asm-x86/unistd_32.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
#define __NR_eventfd2 328
337337
#define __NR_epoll_create2 329
338338
#define __NR_dup3 330
339+
#define __NR_pipe2 331
339340

340341
#ifdef __KERNEL__
341342

include/asm-x86/unistd_64.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ __SYSCALL(__NR_eventfd2, sys_eventfd2)
649649
__SYSCALL(__NR_epoll_create2, sys_epoll_create2)
650650
#define __NR_dup3 292
651651
__SYSCALL(__NR_dup3, sys_dup3)
652+
#define __NR_pipe2 293
653+
__SYSCALL(__NR_pipe2, sys_pipe2)
652654

653655

654656
#ifndef __NO_STUBS

include/linux/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,7 @@ static inline void allow_write_access(struct file *file)
17771777
atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
17781778
}
17791779
extern int do_pipe(int *);
1780+
extern int do_pipe_flags(int *, int);
17801781
extern struct file *create_read_pipe(struct file *f);
17811782
extern struct file *create_write_pipe(void);
17821783
extern void free_write_pipe(struct file *);

0 commit comments

Comments
 (0)