blob: a3d7ccd18a7da5303c9a3e1fd2b009abd8604e44 [file] [log] [blame]
Willy Tarreau982b6e32009-01-25 13:49:53 +01001/*
2 * Pipe management
3 *
4 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <unistd.h>
Willy Tarreau9ed560e2011-10-24 17:09:22 +020014#include <fcntl.h>
Willy Tarreau982b6e32009-01-25 13:49:53 +010015
16#include <common/config.h>
Willy Tarreau3e5e4172017-11-11 17:58:31 +010017#include <common/hathreads.h>
Willy Tarreau982b6e32009-01-25 13:49:53 +010018#include <common/memory.h>
19
20#include <types/global.h>
21#include <types/pipe.h>
22
Willy Tarreaubafbe012017-11-24 17:34:44 +010023struct pool_head *pool_head_pipe = NULL;
Willy Tarreau982b6e32009-01-25 13:49:53 +010024struct pipe *pipes_live = NULL; /* pipes which are still ready to use */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010025
26__decl_hathreads(HA_SPINLOCK_T pipes_lock); /* lock used to protect pipes list */
27
Willy Tarreau982b6e32009-01-25 13:49:53 +010028int pipes_used = 0; /* # of pipes in use (2 fds each) */
29int pipes_free = 0; /* # of pipes unused */
30
31/* allocate memory for the pipes */
32static void init_pipe()
33{
Willy Tarreaubafbe012017-11-24 17:34:44 +010034 pool_head_pipe = create_pool("pipe", sizeof(struct pipe), MEM_F_SHARED);
Willy Tarreau982b6e32009-01-25 13:49:53 +010035 pipes_used = 0;
36 pipes_free = 0;
Emeric Brund8b3b652017-11-07 11:19:48 +010037 HA_SPIN_INIT(&pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010038}
39
40/* return a pre-allocated empty pipe. Try to allocate one if there isn't any
41 * left. NULL is returned if a pipe could not be allocated.
42 */
43struct pipe *get_pipe()
44{
Emeric Brund8b3b652017-11-07 11:19:48 +010045 struct pipe *ret = NULL;
Willy Tarreau982b6e32009-01-25 13:49:53 +010046 int pipefd[2];
47
Emeric Brund8b3b652017-11-07 11:19:48 +010048 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010049 if (likely(pipes_live)) {
50 ret = pipes_live;
51 pipes_live = pipes_live->next;
52 pipes_free--;
53 pipes_used++;
Emeric Brund8b3b652017-11-07 11:19:48 +010054 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010055 }
56
57 if (pipes_used >= global.maxpipes)
Emeric Brund8b3b652017-11-07 11:19:48 +010058 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010059
Willy Tarreaubafbe012017-11-24 17:34:44 +010060 ret = pool_alloc(pool_head_pipe);
Willy Tarreau982b6e32009-01-25 13:49:53 +010061 if (!ret)
Emeric Brund8b3b652017-11-07 11:19:48 +010062 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010063
64 if (pipe(pipefd) < 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +010065 pool_free(pool_head_pipe, ret);
Emeric Brund8b3b652017-11-07 11:19:48 +010066 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010067 }
Willy Tarreaubd9a0a72011-10-23 21:14:29 +020068#ifdef F_SETPIPE_SZ
69 if (global.tune.pipesize)
70 fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize);
71#endif
Willy Tarreau982b6e32009-01-25 13:49:53 +010072 ret->data = 0;
73 ret->prod = pipefd[1];
74 ret->cons = pipefd[0];
75 ret->next = NULL;
76 pipes_used++;
Emeric Brund8b3b652017-11-07 11:19:48 +010077 out:
78 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010079 return ret;
80}
81
Emeric Brund8b3b652017-11-07 11:19:48 +010082static void inline __kill_pipe(struct pipe *p)
Willy Tarreau982b6e32009-01-25 13:49:53 +010083{
84 close(p->prod);
85 close(p->cons);
Willy Tarreaubafbe012017-11-24 17:34:44 +010086 pool_free(pool_head_pipe, p);
Willy Tarreau982b6e32009-01-25 13:49:53 +010087 pipes_used--;
88 return;
89}
90
Emeric Brund8b3b652017-11-07 11:19:48 +010091/* destroy a pipe, possibly because an error was encountered on it. Its FDs
92 * will be closed and it will not be reinjected into the live pool.
93 */
94void kill_pipe(struct pipe *p)
95{
96 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
97 __kill_pipe(p);
98 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
99 return;
100}
101
Willy Tarreau982b6e32009-01-25 13:49:53 +0100102/* put back a unused pipe into the live pool. If it still has data in it, it is
103 * closed and not reinjected into the live pool. The caller is not allowed to
104 * use it once released.
105 */
106void put_pipe(struct pipe *p)
107{
Emeric Brund8b3b652017-11-07 11:19:48 +0100108 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100109 if (p->data) {
Emeric Brund8b3b652017-11-07 11:19:48 +0100110 __kill_pipe(p);
111 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +0100112 }
113 p->next = pipes_live;
114 pipes_live = p;
115 pipes_free++;
116 pipes_used--;
Emeric Brund8b3b652017-11-07 11:19:48 +0100117 out:
118 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100119}
120
121
122__attribute__((constructor))
123static void __pipe_module_init(void)
124{
125 init_pipe();
126}
127
128/*
129 * Local variables:
130 * c-indent-level: 8
131 * c-basic-offset: 8
132 * End:
133 */