blob: 8941f89bdb070656e5681cbbacbedae3ba33b1b9 [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 Tarreau8ceae722018-11-26 11:58:30 +010023DECLARE_STATIC_POOL(pool_head_pipe, "pipe", sizeof(struct pipe));
24
Willy Tarreau982b6e32009-01-25 13:49:53 +010025struct pipe *pipes_live = NULL; /* pipes which are still ready to use */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010026
Willy Tarreau86abe442018-11-25 20:12:18 +010027__decl_spinlock(pipes_lock); /* lock used to protect pipes list */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010028
Willy Tarreau982b6e32009-01-25 13:49:53 +010029int pipes_used = 0; /* # of pipes in use (2 fds each) */
30int pipes_free = 0; /* # of pipes unused */
31
Willy Tarreau982b6e32009-01-25 13:49:53 +010032/* return a pre-allocated empty pipe. Try to allocate one if there isn't any
33 * left. NULL is returned if a pipe could not be allocated.
34 */
35struct pipe *get_pipe()
36{
Emeric Brund8b3b652017-11-07 11:19:48 +010037 struct pipe *ret = NULL;
Willy Tarreau982b6e32009-01-25 13:49:53 +010038 int pipefd[2];
39
Emeric Brund8b3b652017-11-07 11:19:48 +010040 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010041 if (likely(pipes_live)) {
42 ret = pipes_live;
43 pipes_live = pipes_live->next;
44 pipes_free--;
45 pipes_used++;
Emeric Brund8b3b652017-11-07 11:19:48 +010046 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010047 }
48
49 if (pipes_used >= global.maxpipes)
Emeric Brund8b3b652017-11-07 11:19:48 +010050 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010051
Willy Tarreaubafbe012017-11-24 17:34:44 +010052 ret = pool_alloc(pool_head_pipe);
Willy Tarreau982b6e32009-01-25 13:49:53 +010053 if (!ret)
Emeric Brund8b3b652017-11-07 11:19:48 +010054 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010055
56 if (pipe(pipefd) < 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +010057 pool_free(pool_head_pipe, ret);
Willy Tarreaub0710792020-02-04 10:23:54 +010058 ret = NULL;
Emeric Brund8b3b652017-11-07 11:19:48 +010059 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010060 }
Willy Tarreaubd9a0a72011-10-23 21:14:29 +020061#ifdef F_SETPIPE_SZ
62 if (global.tune.pipesize)
63 fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize);
64#endif
Willy Tarreau982b6e32009-01-25 13:49:53 +010065 ret->data = 0;
66 ret->prod = pipefd[1];
67 ret->cons = pipefd[0];
68 ret->next = NULL;
69 pipes_used++;
Emeric Brund8b3b652017-11-07 11:19:48 +010070 out:
71 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010072 return ret;
73}
74
Willy Tarreau0e492e22019-04-15 21:25:03 +020075static inline void __kill_pipe(struct pipe *p)
Willy Tarreau982b6e32009-01-25 13:49:53 +010076{
77 close(p->prod);
78 close(p->cons);
Willy Tarreaubafbe012017-11-24 17:34:44 +010079 pool_free(pool_head_pipe, p);
Willy Tarreau982b6e32009-01-25 13:49:53 +010080 pipes_used--;
81 return;
82}
83
Emeric Brund8b3b652017-11-07 11:19:48 +010084/* destroy a pipe, possibly because an error was encountered on it. Its FDs
85 * will be closed and it will not be reinjected into the live pool.
86 */
87void kill_pipe(struct pipe *p)
88{
89 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
90 __kill_pipe(p);
91 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
92 return;
93}
94
Willy Tarreau982b6e32009-01-25 13:49:53 +010095/* put back a unused pipe into the live pool. If it still has data in it, it is
96 * closed and not reinjected into the live pool. The caller is not allowed to
97 * use it once released.
98 */
99void put_pipe(struct pipe *p)
100{
Emeric Brund8b3b652017-11-07 11:19:48 +0100101 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100102 if (p->data) {
Emeric Brund8b3b652017-11-07 11:19:48 +0100103 __kill_pipe(p);
104 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +0100105 }
106 p->next = pipes_live;
107 pipes_live = p;
108 pipes_free++;
109 pipes_used--;
Emeric Brund8b3b652017-11-07 11:19:48 +0100110 out:
111 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100112}
113
Willy Tarreau982b6e32009-01-25 13:49:53 +0100114/*
115 * Local variables:
116 * c-indent-level: 8
117 * c-basic-offset: 8
118 * End:
119 */