1 /*
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>
14 #include <fcntl.h>
15 
16 #include <haproxy/api.h>
17 #include <haproxy/global.h>
18 #include <haproxy/pipe-t.h>
19 #include <haproxy/pool.h>
20 #include <haproxy/thread.h>
21 
22 
23 DECLARE_STATIC_POOL(pool_head_pipe, "pipe", sizeof(struct pipe));
24 
25 struct pipe *pipes_live = NULL; /* pipes which are still ready to use */
26 
27 __decl_spinlock(pipes_lock); /* lock used to protect pipes list */
28 
29 static THREAD_LOCAL int local_pipes_free = 0;  /* #cache objects   */
30 static THREAD_LOCAL struct pipe *local_pipes = NULL;
31 
32 int pipes_used = 0;             /* # of pipes in use (2 fds each) */
33 int pipes_free = 0;             /* # of pipes unused */
34 
35 /* return a pre-allocated empty pipe. Try to allocate one if there isn't any
36  * left. NULL is returned if a pipe could not be allocated.
37  */
get_pipe()38 struct pipe *get_pipe()
39 {
40 	struct pipe *ret = NULL;
41 	int pipefd[2];
42 
43 	ret = local_pipes;
44 	if (likely(ret)) {
45 		local_pipes = ret->next;
46 		local_pipes_free--;
47 		HA_ATOMIC_SUB(&pipes_free, 1);
48 		HA_ATOMIC_ADD(&pipes_used, 1);
49 		goto out;
50 	}
51 
52 	if (likely(pipes_live)) {
53 		HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
54 		ret = pipes_live;
55 		if (likely(ret))
56 			pipes_live = ret->next;
57 		HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
58 		if (ret) {
59 			HA_ATOMIC_SUB(&pipes_free, 1);
60 			HA_ATOMIC_ADD(&pipes_used, 1);
61 			goto out;
62 		}
63 	}
64 
65 	HA_ATOMIC_ADD(&pipes_used, 1);
66 	if (pipes_used + pipes_free >= global.maxpipes)
67 		goto fail;
68 
69 	ret = pool_alloc(pool_head_pipe);
70 	if (!ret)
71 		goto fail;
72 
73 	if (pipe(pipefd) < 0)
74 		goto fail;
75 
76 #ifdef F_SETPIPE_SZ
77 	if (global.tune.pipesize)
78 		fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize);
79 #endif
80 	ret->data = 0;
81 	ret->prod = pipefd[1];
82 	ret->cons = pipefd[0];
83 	ret->next = NULL;
84  out:
85 	return ret;
86  fail:
87 	pool_free(pool_head_pipe, ret);
88 	HA_ATOMIC_SUB(&pipes_used, 1);
89 	return NULL;
90 
91 }
92 
93 /* destroy a pipe, possibly because an error was encountered on it. Its FDs
94  * will be closed and it will not be reinjected into the live pool.
95  */
kill_pipe(struct pipe * p)96 void kill_pipe(struct pipe *p)
97 {
98 	close(p->prod);
99 	close(p->cons);
100 	pool_free(pool_head_pipe, p);
101 	HA_ATOMIC_SUB(&pipes_used, 1);
102 }
103 
104 /* put back a unused pipe into the live pool. If it still has data in it, it is
105  * closed and not reinjected into the live pool. The caller is not allowed to
106  * use it once released.
107  */
put_pipe(struct pipe * p)108 void put_pipe(struct pipe *p)
109 {
110 	if (unlikely(p->data)) {
111 		kill_pipe(p);
112 		return;
113 	}
114 
115 	if (likely(local_pipes_free * global.nbthread < global.maxpipes - pipes_used)) {
116 		p->next = local_pipes;
117 		local_pipes = p;
118 		local_pipes_free++;
119 		goto out;
120 	}
121 
122 	HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
123 	p->next = pipes_live;
124 	pipes_live = p;
125 	HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
126  out:
127 	HA_ATOMIC_ADD(&pipes_free, 1);
128 	HA_ATOMIC_SUB(&pipes_used, 1);
129 }
130 
131 /*
132  * Local variables:
133  *  c-indent-level: 8
134  *  c-basic-offset: 8
135  * End:
136  */
137