blob: bab86203e7ee5fc2f1b685a3e996d818d4a868e5 [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
23struct pool_head *pool2_pipe = NULL;
24struct 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{
34 pool2_pipe = create_pool("pipe", sizeof(struct pipe), MEM_F_SHARED);
35 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
60 ret = pool_alloc2(pool2_pipe);
61 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) {
65 pool_free2(pool2_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);
86 pool_free2(pool2_pipe, p);
87 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 */