Skip to content

Commit be61a86

Browse files
Ulrich Dreppertorvalds
authored andcommitted
flag parameters: NONBLOCK in pipe
This patch adds O_NONBLOCK support to pipe2. It is minimally more involved than the patches for eventfd et.al but still trivial. The interfaces of the create_write_pipe and create_read_pipe helper functions were changed and the one other caller as well. 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 fds[2]; if (syscall (__NR_pipe2, fds, 0) == -1) { puts ("pipe2(0) failed"); return 1; } for (int i = 0; i < 2; ++i) { int fl = fcntl (fds[i], F_GETFL); if (fl == -1) { puts ("fcntl failed"); return 1; } if (fl & O_NONBLOCK) { printf ("pipe2(0) set non-blocking mode for fds[%d]\n", i); return 1; } close (fds[i]); } if (syscall (__NR_pipe2, fds, O_NONBLOCK) == -1) { puts ("pipe2(O_NONBLOCK) failed"); return 1; } for (int i = 0; i < 2; ++i) { int fl = fcntl (fds[i], F_GETFL); if (fl == -1) { puts ("fcntl failed"); return 1; } if ((fl & O_NONBLOCK) == 0) { printf ("pipe2(O_NONBLOCK) does not set non-blocking mode for fds[%d]\n", i); return 1; } close (fds[i]); } 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> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 6b1ef0e commit be61a86

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

fs/pipe.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ static struct inode * get_pipe_inode(void)
950950
return NULL;
951951
}
952952

953-
struct file *create_write_pipe(void)
953+
struct file *create_write_pipe(int flags)
954954
{
955955
int err;
956956
struct inode *inode;
@@ -983,7 +983,7 @@ struct file *create_write_pipe(void)
983983
goto err_dentry;
984984
f->f_mapping = inode->i_mapping;
985985

986-
f->f_flags = O_WRONLY;
986+
f->f_flags = O_WRONLY | (flags & O_NONBLOCK);
987987
f->f_version = 0;
988988

989989
return f;
@@ -1007,7 +1007,7 @@ void free_write_pipe(struct file *f)
10071007
put_filp(f);
10081008
}
10091009

1010-
struct file *create_read_pipe(struct file *wrf)
1010+
struct file *create_read_pipe(struct file *wrf, int flags)
10111011
{
10121012
struct file *f = get_empty_filp();
10131013
if (!f)
@@ -1019,7 +1019,7 @@ struct file *create_read_pipe(struct file *wrf)
10191019
f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
10201020

10211021
f->f_pos = 0;
1022-
f->f_flags = O_RDONLY;
1022+
f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
10231023
f->f_op = &read_pipe_fops;
10241024
f->f_mode = FMODE_READ;
10251025
f->f_version = 0;
@@ -1033,13 +1033,13 @@ int do_pipe_flags(int *fd, int flags)
10331033
int error;
10341034
int fdw, fdr;
10351035

1036-
if (flags & ~O_CLOEXEC)
1036+
if (flags & ~(O_CLOEXEC | O_NONBLOCK))
10371037
return -EINVAL;
10381038

1039-
fw = create_write_pipe();
1039+
fw = create_write_pipe(flags);
10401040
if (IS_ERR(fw))
10411041
return PTR_ERR(fw);
1042-
fr = create_read_pipe(fw);
1042+
fr = create_read_pipe(fw, flags);
10431043
error = PTR_ERR(fr);
10441044
if (IS_ERR(fr))
10451045
goto err_write_pipe;

include/linux/fs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,8 +1778,8 @@ static inline void allow_write_access(struct file *file)
17781778
}
17791779
extern int do_pipe(int *);
17801780
extern int do_pipe_flags(int *, int);
1781-
extern struct file *create_read_pipe(struct file *f);
1782-
extern struct file *create_write_pipe(void);
1781+
extern struct file *create_read_pipe(struct file *f, int flags);
1782+
extern struct file *create_write_pipe(int flags);
17831783
extern void free_write_pipe(struct file *);
17841784

17851785
extern struct file *do_filp_open(int dfd, const char *pathname,

kernel/kmod.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,12 @@ int call_usermodehelper_stdinpipe(struct subprocess_info *sub_info,
417417
{
418418
struct file *f;
419419

420-
f = create_write_pipe();
420+
f = create_write_pipe(0);
421421
if (IS_ERR(f))
422422
return PTR_ERR(f);
423423
*filp = f;
424424

425-
f = create_read_pipe(f);
425+
f = create_read_pipe(f, 0);
426426
if (IS_ERR(f)) {
427427
free_write_pipe(*filp);
428428
return PTR_ERR(f);

0 commit comments

Comments
 (0)