xref: /PHP-7.3/sapi/fpm/fpm/fpm_worker_pool.c (revision f47798e6)
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(struct fpm_worker_pool_s * wp)20 void fpm_worker_pool_free(struct fpm_worker_pool_s *wp) /* {{{ */
21 {
22 	if (wp->config) {
23 		free(wp->config);
24 	}
25 	if (wp->user) {
26 		free(wp->user);
27 	}
28 	if (wp->home) {
29 		free(wp->home);
30 	}
31 	fpm_unix_free_socket_premissions(wp);
32 	free(wp);
33 }
34 /* }}} */
35 
fpm_worker_pool_cleanup(int which,void * arg)36 static void fpm_worker_pool_cleanup(int which, void *arg) /* {{{ */
37 {
38 	struct fpm_worker_pool_s *wp, *wp_next;
39 
40 	for (wp = fpm_worker_all_pools; wp; wp = wp_next) {
41 		wp_next = wp->next;
42 		fpm_worker_pool_config_free(wp->config);
43 		fpm_children_free(wp->children);
44 		if ((which & FPM_CLEANUP_CHILD) == 0 && fpm_globals.parent_pid == getpid()) {
45 			fpm_scoreboard_free(wp);
46 		}
47 		fpm_worker_pool_free(wp);
48 	}
49 	fpm_worker_all_pools = NULL;
50 }
51 /* }}} */
52 
fpm_worker_pool_alloc()53 struct fpm_worker_pool_s *fpm_worker_pool_alloc() /* {{{ */
54 {
55 	struct fpm_worker_pool_s *ret;
56 
57 	ret = malloc(sizeof(struct fpm_worker_pool_s));
58 	if (!ret) {
59 		return 0;
60 	}
61 
62 	memset(ret, 0, sizeof(struct fpm_worker_pool_s));
63 
64 	ret->idle_spawn_rate = 1;
65 	ret->log_fd = -1;
66 	return ret;
67 }
68 /* }}} */
69 
fpm_worker_pool_init_main()70 int fpm_worker_pool_init_main() /* {{{ */
71 {
72 	if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0)) {
73 		return -1;
74 	}
75 	return 0;
76 }
77 /* }}} */
78