xref: /php-src/sapi/fpm/fpm/fpm_worker_pool.c (revision 94702c56)
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->set_user) {
38 		free(wp->set_user);
39 	}
40 	if (wp->home) {
41 		free(wp->home);
42 	}
43 	if (wp->limit_extensions) {
44 		fpm_worker_pool_free_limit_extensions(wp->limit_extensions);
45 	}
46 	fpm_unix_free_socket_permissions(wp);
47 	free(wp);
48 }
49 /* }}} */
50 
fpm_worker_pool_cleanup(int which,void * arg)51 static void fpm_worker_pool_cleanup(int which, void *arg) /* {{{ */
52 {
53 	struct fpm_worker_pool_s *wp, *wp_next;
54 
55 	for (wp = fpm_worker_all_pools; wp; wp = wp_next) {
56 		wp_next = wp->next;
57 		fpm_worker_pool_config_free(wp->config);
58 		fpm_children_free(wp->children);
59 		if ((which & FPM_CLEANUP_CHILD) == 0 && fpm_globals.parent_pid == getpid()) {
60 			fpm_scoreboard_free(wp);
61 		}
62 		fpm_worker_pool_free(wp);
63 	}
64 	fpm_worker_all_pools = NULL;
65 }
66 /* }}} */
67 
fpm_worker_pool_alloc(void)68 struct fpm_worker_pool_s *fpm_worker_pool_alloc(void)
69 {
70 	struct fpm_worker_pool_s *ret;
71 
72 	ret = malloc(sizeof(struct fpm_worker_pool_s));
73 	if (!ret) {
74 		return 0;
75 	}
76 
77 	memset(ret, 0, sizeof(struct fpm_worker_pool_s));
78 
79 	ret->idle_spawn_rate = 1;
80 	ret->log_fd = -1;
81 	return ret;
82 }
83 
fpm_worker_pool_init_main(void)84 int fpm_worker_pool_init_main(void)
85 {
86 	if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0)) {
87 		return -1;
88 	}
89 	return 0;
90 }
91