xref: /PHP-7.4/sapi/fpm/fpm/fpm_worker_pool.c (revision cb2021e5)
1 	/* (c) 2007,2008 Andrei Nigmatulin */
2 
3 #include "fpm_config.h"
4 
5 #include <string.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 
9 #include "fpm.h"
10 #include "fpm_worker_pool.h"
11 #include "fpm_cleanup.h"
12 #include "fpm_children.h"
13 #include "fpm_shm.h"
14 #include "fpm_scoreboard.h"
15 #include "fpm_conf.h"
16 #include "fpm_unix.h"
17 
18 struct fpm_worker_pool_s *fpm_worker_all_pools;
19 
fpm_worker_pool_free_limit_extensions(char ** limit_extensions)20 void fpm_worker_pool_free_limit_extensions(char **limit_extensions) {
21 	char **ext = limit_extensions;
22 	while (*ext) {
23 		free(*ext);
24 		ext++;
25 	}
26 	free(limit_extensions);
27 }
28 
fpm_worker_pool_free(struct fpm_worker_pool_s * wp)29 void fpm_worker_pool_free(struct fpm_worker_pool_s *wp) /* {{{ */
30 {
31 	if (wp->config) {
32 		free(wp->config);
33 	}
34 	if (wp->user) {
35 		free(wp->user);
36 	}
37 	if (wp->home) {
38 		free(wp->home);
39 	}
40 	if (wp->limit_extensions) {
41 		fpm_worker_pool_free_limit_extensions(wp->limit_extensions);
42 	}
43 	fpm_unix_free_socket_premissions(wp);
44 	free(wp);
45 }
46 /* }}} */
47 
fpm_worker_pool_cleanup(int which,void * arg)48 static void fpm_worker_pool_cleanup(int which, void *arg) /* {{{ */
49 {
50 	struct fpm_worker_pool_s *wp, *wp_next;
51 
52 	for (wp = fpm_worker_all_pools; wp; wp = wp_next) {
53 		wp_next = wp->next;
54 		fpm_worker_pool_config_free(wp->config);
55 		fpm_children_free(wp->children);
56 		if ((which & FPM_CLEANUP_CHILD) == 0 && fpm_globals.parent_pid == getpid()) {
57 			fpm_scoreboard_free(wp);
58 		}
59 		fpm_worker_pool_free(wp);
60 	}
61 	fpm_worker_all_pools = NULL;
62 }
63 /* }}} */
64 
fpm_worker_pool_alloc()65 struct fpm_worker_pool_s *fpm_worker_pool_alloc() /* {{{ */
66 {
67 	struct fpm_worker_pool_s *ret;
68 
69 	ret = malloc(sizeof(struct fpm_worker_pool_s));
70 	if (!ret) {
71 		return 0;
72 	}
73 
74 	memset(ret, 0, sizeof(struct fpm_worker_pool_s));
75 
76 	ret->idle_spawn_rate = 1;
77 	ret->log_fd = -1;
78 	return ret;
79 }
80 /* }}} */
81 
fpm_worker_pool_init_main()82 int fpm_worker_pool_init_main() /* {{{ */
83 {
84 	if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0)) {
85 		return -1;
86 	}
87 	return 0;
88 }
89 /* }}} */
90