2009-01-25 12:49:53 +00:00
|
|
|
/*
|
|
|
|
* Pipe management
|
|
|
|
*
|
|
|
|
* Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2011-10-24 15:09:22 +00:00
|
|
|
#include <fcntl.h>
|
2009-01-25 12:49:53 +00:00
|
|
|
|
|
|
|
#include <common/config.h>
|
2017-11-11 16:58:31 +00:00
|
|
|
#include <common/hathreads.h>
|
2009-01-25 12:49:53 +00:00
|
|
|
#include <common/memory.h>
|
|
|
|
|
|
|
|
#include <types/global.h>
|
|
|
|
#include <types/pipe.h>
|
|
|
|
|
2017-11-24 16:34:44 +00:00
|
|
|
struct pool_head *pool_head_pipe = NULL;
|
2009-01-25 12:49:53 +00:00
|
|
|
struct pipe *pipes_live = NULL; /* pipes which are still ready to use */
|
2017-11-13 09:34:01 +00:00
|
|
|
|
|
|
|
__decl_hathreads(HA_SPINLOCK_T pipes_lock); /* lock used to protect pipes list */
|
|
|
|
|
2009-01-25 12:49:53 +00:00
|
|
|
int pipes_used = 0; /* # of pipes in use (2 fds each) */
|
|
|
|
int pipes_free = 0; /* # of pipes unused */
|
|
|
|
|
|
|
|
/* allocate memory for the pipes */
|
|
|
|
static void init_pipe()
|
|
|
|
{
|
2017-11-24 16:34:44 +00:00
|
|
|
pool_head_pipe = create_pool("pipe", sizeof(struct pipe), MEM_F_SHARED);
|
2009-01-25 12:49:53 +00:00
|
|
|
pipes_used = 0;
|
|
|
|
pipes_free = 0;
|
2017-11-07 10:19:48 +00:00
|
|
|
HA_SPIN_INIT(&pipes_lock);
|
2009-01-25 12:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return a pre-allocated empty pipe. Try to allocate one if there isn't any
|
|
|
|
* left. NULL is returned if a pipe could not be allocated.
|
|
|
|
*/
|
|
|
|
struct pipe *get_pipe()
|
|
|
|
{
|
2017-11-07 10:19:48 +00:00
|
|
|
struct pipe *ret = NULL;
|
2009-01-25 12:49:53 +00:00
|
|
|
int pipefd[2];
|
|
|
|
|
2017-11-07 10:19:48 +00:00
|
|
|
HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
|
2009-01-25 12:49:53 +00:00
|
|
|
if (likely(pipes_live)) {
|
|
|
|
ret = pipes_live;
|
|
|
|
pipes_live = pipes_live->next;
|
|
|
|
pipes_free--;
|
|
|
|
pipes_used++;
|
2017-11-07 10:19:48 +00:00
|
|
|
goto out;
|
2009-01-25 12:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pipes_used >= global.maxpipes)
|
2017-11-07 10:19:48 +00:00
|
|
|
goto out;
|
2009-01-25 12:49:53 +00:00
|
|
|
|
2017-11-24 16:34:44 +00:00
|
|
|
ret = pool_alloc(pool_head_pipe);
|
2009-01-25 12:49:53 +00:00
|
|
|
if (!ret)
|
2017-11-07 10:19:48 +00:00
|
|
|
goto out;
|
2009-01-25 12:49:53 +00:00
|
|
|
|
|
|
|
if (pipe(pipefd) < 0) {
|
2017-11-24 16:34:44 +00:00
|
|
|
pool_free(pool_head_pipe, ret);
|
2017-11-07 10:19:48 +00:00
|
|
|
goto out;
|
2009-01-25 12:49:53 +00:00
|
|
|
}
|
2011-10-23 19:14:29 +00:00
|
|
|
#ifdef F_SETPIPE_SZ
|
|
|
|
if (global.tune.pipesize)
|
|
|
|
fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize);
|
|
|
|
#endif
|
2009-01-25 12:49:53 +00:00
|
|
|
ret->data = 0;
|
|
|
|
ret->prod = pipefd[1];
|
|
|
|
ret->cons = pipefd[0];
|
|
|
|
ret->next = NULL;
|
|
|
|
pipes_used++;
|
2017-11-07 10:19:48 +00:00
|
|
|
out:
|
|
|
|
HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
|
2009-01-25 12:49:53 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-07 10:19:48 +00:00
|
|
|
static void inline __kill_pipe(struct pipe *p)
|
2009-01-25 12:49:53 +00:00
|
|
|
{
|
|
|
|
close(p->prod);
|
|
|
|
close(p->cons);
|
2017-11-24 16:34:44 +00:00
|
|
|
pool_free(pool_head_pipe, p);
|
2009-01-25 12:49:53 +00:00
|
|
|
pipes_used--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 10:19:48 +00:00
|
|
|
/* destroy a pipe, possibly because an error was encountered on it. Its FDs
|
|
|
|
* will be closed and it will not be reinjected into the live pool.
|
|
|
|
*/
|
|
|
|
void kill_pipe(struct pipe *p)
|
|
|
|
{
|
|
|
|
HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
|
|
|
|
__kill_pipe(p);
|
|
|
|
HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-25 12:49:53 +00:00
|
|
|
/* put back a unused pipe into the live pool. If it still has data in it, it is
|
|
|
|
* closed and not reinjected into the live pool. The caller is not allowed to
|
|
|
|
* use it once released.
|
|
|
|
*/
|
|
|
|
void put_pipe(struct pipe *p)
|
|
|
|
{
|
2017-11-07 10:19:48 +00:00
|
|
|
HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
|
2009-01-25 12:49:53 +00:00
|
|
|
if (p->data) {
|
2017-11-07 10:19:48 +00:00
|
|
|
__kill_pipe(p);
|
|
|
|
goto out;
|
2009-01-25 12:49:53 +00:00
|
|
|
}
|
|
|
|
p->next = pipes_live;
|
|
|
|
pipes_live = p;
|
|
|
|
pipes_free++;
|
|
|
|
pipes_used--;
|
2017-11-07 10:19:48 +00:00
|
|
|
out:
|
|
|
|
HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
|
2009-01-25 12:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
__attribute__((constructor))
|
|
|
|
static void __pipe_module_init(void)
|
|
|
|
{
|
|
|
|
init_pipe();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|