blob: 89b5c9a5f97c8e26277365283eae91b4870c2d14 [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);
Emeric Brund8b3b652017-11-07 11:19:48 +010058 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +010059 }
Willy Tarreaubd9a0a72011-10-23 21:14:29 +020060#ifdef F_SETPIPE_SZ
61 if (global.tune.pipesize)
62 fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize);
63#endif
Willy Tarreau982b6e32009-01-25 13:49:53 +010064 ret->data = 0;
65 ret->prod = pipefd[1];
66 ret->cons = pipefd[0];
67 ret->next = NULL;
68 pipes_used++;
Emeric Brund8b3b652017-11-07 11:19:48 +010069 out:
70 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010071 return ret;
72}
73
Emeric Brund8b3b652017-11-07 11:19:48 +010074static void inline __kill_pipe(struct pipe *p)
Willy Tarreau982b6e32009-01-25 13:49:53 +010075{
76 close(p->prod);
77 close(p->cons);
Willy Tarreaubafbe012017-11-24 17:34:44 +010078 pool_free(pool_head_pipe, p);
Willy Tarreau982b6e32009-01-25 13:49:53 +010079 pipes_used--;
80 return;
81}
82
Emeric Brund8b3b652017-11-07 11:19:48 +010083/* destroy a pipe, possibly because an error was encountered on it. Its FDs
84 * will be closed and it will not be reinjected into the live pool.
85 */
86void kill_pipe(struct pipe *p)
87{
88 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
89 __kill_pipe(p);
90 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
91 return;
92}
93
Willy Tarreau982b6e32009-01-25 13:49:53 +010094/* put back a unused pipe into the live pool. If it still has data in it, it is
95 * closed and not reinjected into the live pool. The caller is not allowed to
96 * use it once released.
97 */
98void put_pipe(struct pipe *p)
99{
Emeric Brund8b3b652017-11-07 11:19:48 +0100100 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100101 if (p->data) {
Emeric Brund8b3b652017-11-07 11:19:48 +0100102 __kill_pipe(p);
103 goto out;
Willy Tarreau982b6e32009-01-25 13:49:53 +0100104 }
105 p->next = pipes_live;
106 pipes_live = p;
107 pipes_free++;
108 pipes_used--;
Emeric Brund8b3b652017-11-07 11:19:48 +0100109 out:
110 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100111}
112
Willy Tarreau982b6e32009-01-25 13:49:53 +0100113/*
114 * Local variables:
115 * c-indent-level: 8
116 * c-basic-offset: 8
117 * End:
118 */