diff options
author | Sebastien Marie <semarie@cvs.openbsd.org> | 2019-07-16 12:16:59 +0000 |
---|---|---|
committer | Sebastien Marie <semarie@cvs.openbsd.org> | 2019-07-16 12:16:59 +0000 |
commit | 9b6eeec44506c95b26f5faee0451b6e4c0093015 (patch) | |
tree | a2ed5d29c99f81a4f4ba09563c0a751f7f31de58 | |
parent | 3c823b6450b98a2a5b5308476dd113ce8dab6022 (diff) |
move the whole `struct pipe' allocation and initialization inside pipe_create()
rename pipclose() to pipe_destroy(), and return early instead of having the whole code in if-body.
ok claudio@ anton@ visa@ mpi@
-rw-r--r-- | sys/kern/sys_pipe.c | 100 |
1 files changed, 52 insertions, 48 deletions
diff --git a/sys/kern/sys_pipe.c b/sys/kern/sys_pipe.c index 5397009835e..2f3a9a045e2 100644 --- a/sys/kern/sys_pipe.c +++ b/sys/kern/sys_pipe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sys_pipe.c,v 1.94 2019/07/15 09:05:46 semarie Exp $ */ +/* $OpenBSD: sys_pipe.c,v 1.95 2019/07/16 12:16:58 semarie Exp $ */ /* * Copyright (c) 1996 John S. Dyson @@ -97,12 +97,12 @@ static unsigned int amountpipekva; struct pool pipe_pool; int dopipe(struct proc *, int *, int); -void pipeclose(struct pipe *); -int pipe_create(struct pipe *); int pipelock(struct pipe *); void pipeunlock(struct pipe *); void pipeselwakeup(struct pipe *); +struct pipe *pipe_create(void); +void pipe_destroy(struct pipe *); int pipe_buffer_realloc(struct pipe *, u_int); void pipe_buffer_free(struct pipe *); @@ -144,14 +144,11 @@ dopipe(struct proc *p, int *ufds, int flags) cloexec = (flags & O_CLOEXEC) ? UF_EXCLOSE : 0; - rpipe = pool_get(&pipe_pool, PR_WAITOK | PR_ZERO); - error = pipe_create(rpipe); - if (error != 0) - goto free1; - wpipe = pool_get(&pipe_pool, PR_WAITOK | PR_ZERO); - error = pipe_create(wpipe); - if (error != 0) + if (((rpipe = pipe_create()) == NULL) || + ((wpipe = pipe_create()) == NULL)) { + error = ENOMEM; goto free1; + } fdplock(fdp); @@ -202,8 +199,8 @@ free3: free2: fdpunlock(fdp); free1: - pipeclose(wpipe); - pipeclose(rpipe); + pipe_destroy(wpipe); + pipe_destroy(rpipe); return (error); } @@ -247,22 +244,27 @@ pipe_buffer_realloc(struct pipe *cpipe, u_int size) /* * initialize and allocate VM and memory for pipe */ -int -pipe_create(struct pipe *cpipe) +struct pipe * +pipe_create(void) { + struct pipe *cpipe; int error; - sigio_init(&cpipe->pipe_sigio); + cpipe = pool_get(&pipe_pool, PR_WAITOK | PR_ZERO); error = pipe_buffer_realloc(cpipe, PIPE_SIZE); - if (error != 0) - return (error); + if (error != 0) { + pool_put(&pipe_pool, cpipe); + return (NULL); + } + + sigio_init(&cpipe->pipe_sigio); getnanotime(&cpipe->pipe_ctime); cpipe->pipe_atime = cpipe->pipe_ctime; cpipe->pipe_mtime = cpipe->pipe_ctime; - return (0); + return (cpipe); } @@ -768,7 +770,7 @@ pipe_close(struct file *fp, struct proc *p) fp->f_ops = NULL; fp->f_data = NULL; KERNEL_LOCK(); - pipeclose(cpipe); + pipe_destroy(cpipe); KERNEL_UNLOCK(); return (0); } @@ -799,44 +801,46 @@ pipe_buffer_free(struct pipe *cpipe) } /* - * shutdown the pipe + * shutdown the pipe, and free resources. */ void -pipeclose(struct pipe *cpipe) +pipe_destroy(struct pipe *cpipe) { struct pipe *ppipe; - if (cpipe) { - pipeselwakeup(cpipe); - sigio_free(&cpipe->pipe_sigio); - /* - * If the other side is blocked, wake it up saying that - * we want to close it down. - */ - cpipe->pipe_state |= PIPE_EOF; - while (cpipe->pipe_busy) { - wakeup(cpipe); - cpipe->pipe_state |= PIPE_WANTD; - tsleep(cpipe, PRIBIO, "pipecl", 0); - } + if (cpipe == NULL) + return; - /* - * Disconnect from peer - */ - if ((ppipe = cpipe->pipe_peer) != NULL) { - pipeselwakeup(ppipe); + pipeselwakeup(cpipe); + sigio_free(&cpipe->pipe_sigio); - ppipe->pipe_state |= PIPE_EOF; - wakeup(ppipe); - ppipe->pipe_peer = NULL; - } + /* + * If the other side is blocked, wake it up saying that + * we want to close it down. + */ + cpipe->pipe_state |= PIPE_EOF; + while (cpipe->pipe_busy) { + wakeup(cpipe); + cpipe->pipe_state |= PIPE_WANTD; + tsleep(cpipe, PRIBIO, "pipecl", 0); + } - /* - * free resources - */ - pipe_buffer_free(cpipe); - pool_put(&pipe_pool, cpipe); + /* + * Disconnect from peer + */ + if ((ppipe = cpipe->pipe_peer) != NULL) { + pipeselwakeup(ppipe); + + ppipe->pipe_state |= PIPE_EOF; + wakeup(ppipe); + ppipe->pipe_peer = NULL; } + + /* + * free resources + */ + pipe_buffer_free(cpipe); + pool_put(&pipe_pool, cpipe); } int |