blob: c7ce3c58dc99f0c7d12f57e9af1b9d3e235b6479 [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>
17#include <common/memory.h>
18
19#include <types/global.h>
20#include <types/pipe.h>
21
22struct pool_head *pool2_pipe = NULL;
23struct pipe *pipes_live = NULL; /* pipes which are still ready to use */
Emeric Brund8b3b652017-11-07 11:19:48 +010024HA_SPINLOCK_T pipes_lock; /* lock used to protect pipes list */
Willy Tarreau982b6e32009-01-25 13:49:53 +010025int pipes_used = 0; /* # of pipes in use (2 fds each) */
26int pipes_free = 0; /* # of pipes unused */
27
28/* allocate memory for the pipes */
29static void init_pipe()
30{
31 pool2_pipe = create_pool("pipe", sizeof(struct pipe), MEM_F_SHARED);
32 pipes_used = 0;
33 pipes_free = 0;
Emeric Brund8b3b652017-11-07 11:19:48 +010034 HA_SPIN_INIT(&pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010035}
36
37/* return a pre-allocated empty pipe. Try to allocate one if there isn't any
38 * left. NULL is returned if a pipe could not be allocated.
39 */
40struct pipe *get_pipe()
41{
Emeric Brund8b3b652017-11-07 11:19:48 +010042 struct pipe *ret = NULL;
Willy Tarreau982b6e32009-01-25 13:49:53 +010043 int pipefd[2];
44
Emeric Brund8b3b652017-11-07 11:19:48 +010045 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010046 if (likely(pipes_live)) {
47 ret = pipes_live;
48 pipes_live = pipes_live->next;
49 pipes_free--;
50 pipes_used++;
Emeric Brund8b3b652017-11-07 11:19:48 +010051 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010052 }
53
54 if (pipes_used >= global.maxpipes)
Emeric Brund8b3b652017-11-07 11:19:48 +010055 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010056
57 ret = pool_alloc2(pool2_pipe);
58 if (!ret)
Emeric Brund8b3b652017-11-07 11:19:48 +010059 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010060
61 if (pipe(pipefd) < 0) {
62 pool_free2(pool2_pipe, ret);
Emeric Brund8b3b652017-11-07 11:19:48 +010063 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010064 }
Willy Tarreaubd9a0a72011-10-23 21:14:29 +020065#ifdef F_SETPIPE_SZ
66 if (global.tune.pipesize)
67 fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize);
68#endif
Willy Tarreau982b6e32009-01-25 13:49:53 +010069 ret->data = 0;
70 ret->prod = pipefd[1];
71 ret->cons = pipefd[0];
72 ret->next = NULL;
73 pipes_used++;
Emeric Brund8b3b652017-11-07 11:19:48 +010074 out:
75 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010076 return ret;
77}
78
Emeric Brund8b3b652017-11-07 11:19:48 +010079static void inline __kill_pipe(struct pipe *p)
Willy Tarreau982b6e32009-01-25 13:49:53 +010080{
81 close(p->prod);
82 close(p->cons);
83 pool_free2(pool2_pipe, p);
84 pipes_used--;
85 return;
86}
87
Emeric Brund8b3b652017-11-07 11:19:48 +010088/* destroy a pipe, possibly because an error was encountered on it. Its FDs
89 * will be closed and it will not be reinjected into the live pool.
90 */
91void kill_pipe(struct pipe *p)
92{
93 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
94 __kill_pipe(p);
95 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
96 return;
97}
98
Willy Tarreau982b6e32009-01-25 13:49:53 +010099/* put back a unused pipe into the live pool. If it still has data in it, it is
100 * closed and not reinjected into the live pool. The caller is not allowed to
101 * use it once released.
102 */
103void put_pipe(struct pipe *p)
104{
Emeric Brund8b3b652017-11-07 11:19:48 +0100105 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100106 if (p->data) {
Emeric Brund8b3b652017-11-07 11:19:48 +0100107 __kill_pipe(p);
108 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +0100109 }
110 p->next = pipes_live;
111 pipes_live = p;
112 pipes_free++;
113 pipes_used--;
Emeric Brund8b3b652017-11-07 11:19:48 +0100114 out:
115 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100116}
117
118
119__attribute__((constructor))
120static void __pipe_module_init(void)
121{
122 init_pipe();
123}
124
125/*
126 * Local variables:
127 * c-indent-level: 8
128 * c-basic-offset: 8
129 * End:
130 */