blob: e94f51f68109c45cb94051a275d17e54c4cd7510 [file] [log] [blame]
Amaury Denoyellece444822021-03-25 15:09:38 +01001#include <stdio.h>
2#include <stdlib.h>
3
4#include <haproxy/init.h>
5#include <haproxy/list.h>
6
7/* These functions are called just after the point where the program exits
8 * after a config validity check, so they are generally suited for resource
9 * allocation and slow initializations that should be skipped during basic
10 * config checks. The functions must return 0 on success, or a combination
11 * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
12 * and immediate exit, so the function must have emitted any useful error.
13 */
14struct list post_check_list = LIST_HEAD_INIT(post_check_list);
15
16/* These functions are called for each proxy just after the config validity
17 * check. The functions must return 0 on success, or a combination of ERR_*
18 * flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause and immediate
19 * exit, so the function must have emitted any useful error.
20 */
21struct list post_proxy_check_list = LIST_HEAD_INIT(post_proxy_check_list);
22
23/* These functions are called for each server just after the config validity
24 * check. The functions must return 0 on success, or a combination of ERR_*
25 * flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause and immediate
26 * exit, so the function must have emitted any useful error.
27 */
28struct list post_server_check_list = LIST_HEAD_INIT(post_server_check_list);
29
30/* These functions are called for each thread just after the thread creation
31 * and before running the init functions. They should be used to do per-thread
32 * (re-)allocations that are needed by subsequent functoins. They must return 0
33 * if an error occurred. */
34struct list per_thread_alloc_list = LIST_HEAD_INIT(per_thread_alloc_list);
35
36/* These functions are called for each thread just after the thread creation
37 * and before running the scheduler. They should be used to do per-thread
38 * initializations. They must return 0 if an error occurred. */
39struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list);
40
41/* These functions are called when freeing the global sections at the end of
42 * deinit, after everything is stopped. They don't return anything. They should
43 * not release shared resources that are possibly used by other deinit
44 * functions, only close/release what is private. Use the per_thread_free_list
45 * to release shared resources.
46 */
47struct list post_deinit_list = LIST_HEAD_INIT(post_deinit_list);
48
49/* These functions are called when freeing a proxy during the deinit, after
50 * everything isg stopped. They don't return anything. They should not release
51 * the proxy itself or any shared resources that are possibly used by other
52 * deinit functions, only close/release what is private.
53 */
54struct list proxy_deinit_list = LIST_HEAD_INIT(proxy_deinit_list);
55
56/* These functions are called when freeing a server during the deinit, after
57 * everything isg stopped. They don't return anything. They should not release
58 * the proxy itself or any shared resources that are possibly used by other
59 * deinit functions, only close/release what is private.
60 */
61struct list server_deinit_list = LIST_HEAD_INIT(server_deinit_list);
62
63/* These functions are called when freeing the global sections at the end of
64 * deinit, after the thread deinit functions, to release unneeded memory
65 * allocations. They don't return anything, and they work in best effort mode
66 * as their sole goal is to make valgrind mostly happy.
67 */
68struct list per_thread_free_list = LIST_HEAD_INIT(per_thread_free_list);
69
70/* These functions are called for each thread just after the scheduler loop and
71 * before exiting the thread. They don't return anything and, as for post-deinit
72 * functions, they work in best effort mode as their sole goal is to make
73 * valgrind mostly happy. */
74struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list);
75
76/* used to register some initialization functions to call after the checks. */
77void hap_register_post_check(int (*fct)())
78{
79 struct post_check_fct *b;
80
81 b = calloc(1, sizeof(*b));
82 if (!b) {
83 fprintf(stderr, "out of memory\n");
84 exit(1);
85 }
86 b->fct = fct;
87 LIST_ADDQ(&post_check_list, &b->list);
88}
89
90/* used to register some initialization functions to call for each proxy after
91 * the checks.
92 */
93void hap_register_post_proxy_check(int (*fct)(struct proxy *))
94{
95 struct post_proxy_check_fct *b;
96
97 b = calloc(1, sizeof(*b));
98 if (!b) {
99 fprintf(stderr, "out of memory\n");
100 exit(1);
101 }
102 b->fct = fct;
103 LIST_ADDQ(&post_proxy_check_list, &b->list);
104}
105
106/* used to register some initialization functions to call for each server after
107 * the checks.
108 */
109void hap_register_post_server_check(int (*fct)(struct server *))
110{
111 struct post_server_check_fct *b;
112
113 b = calloc(1, sizeof(*b));
114 if (!b) {
115 fprintf(stderr, "out of memory\n");
116 exit(1);
117 }
118 b->fct = fct;
119 LIST_ADDQ(&post_server_check_list, &b->list);
120}
121
122/* used to register some de-initialization functions to call after everything
123 * has stopped.
124 */
125void hap_register_post_deinit(void (*fct)())
126{
127 struct post_deinit_fct *b;
128
129 b = calloc(1, sizeof(*b));
130 if (!b) {
131 fprintf(stderr, "out of memory\n");
132 exit(1);
133 }
134 b->fct = fct;
135 LIST_ADDQ(&post_deinit_list, &b->list);
136}
137
138/* used to register some per proxy de-initialization functions to call after
139 * everything has stopped.
140 */
141void hap_register_proxy_deinit(void (*fct)(struct proxy *))
142{
143 struct proxy_deinit_fct *b;
144
145 b = calloc(1, sizeof(*b));
146 if (!b) {
147 fprintf(stderr, "out of memory\n");
148 exit(1);
149 }
150 b->fct = fct;
151 LIST_ADDQ(&proxy_deinit_list, &b->list);
152}
153
154/* used to register some per server de-initialization functions to call after
155 * everything has stopped.
156 */
157void hap_register_server_deinit(void (*fct)(struct server *))
158{
159 struct server_deinit_fct *b;
160
161 b = calloc(1, sizeof(*b));
162 if (!b) {
163 fprintf(stderr, "out of memory\n");
164 exit(1);
165 }
166 b->fct = fct;
167 LIST_ADDQ(&server_deinit_list, &b->list);
168}
169
170/* used to register some allocation functions to call for each thread. */
171void hap_register_per_thread_alloc(int (*fct)())
172{
173 struct per_thread_alloc_fct *b;
174
175 b = calloc(1, sizeof(*b));
176 if (!b) {
177 fprintf(stderr, "out of memory\n");
178 exit(1);
179 }
180 b->fct = fct;
181 LIST_ADDQ(&per_thread_alloc_list, &b->list);
182}
183
184/* used to register some initialization functions to call for each thread. */
185void hap_register_per_thread_init(int (*fct)())
186{
187 struct per_thread_init_fct *b;
188
189 b = calloc(1, sizeof(*b));
190 if (!b) {
191 fprintf(stderr, "out of memory\n");
192 exit(1);
193 }
194 b->fct = fct;
195 LIST_ADDQ(&per_thread_init_list, &b->list);
196}
197
198/* used to register some de-initialization functions to call for each thread. */
199void hap_register_per_thread_deinit(void (*fct)())
200{
201 struct per_thread_deinit_fct *b;
202
203 b = calloc(1, sizeof(*b));
204 if (!b) {
205 fprintf(stderr, "out of memory\n");
206 exit(1);
207 }
208 b->fct = fct;
209 LIST_ADDQ(&per_thread_deinit_list, &b->list);
210}
211
212/* used to register some free functions to call for each thread. */
213void hap_register_per_thread_free(void (*fct)())
214{
215 struct per_thread_free_fct *b;
216
217 b = calloc(1, sizeof(*b));
218 if (!b) {
219 fprintf(stderr, "out of memory\n");
220 exit(1);
221 }
222 b->fct = fct;
223 LIST_ADDQ(&per_thread_free_list, &b->list);
224}