xref: /PHP-8.0/sapi/fpm/fpm/fpm_env.c (revision e2a5428c)
1 	/* (c) 2007,2008 Andrei Nigmatulin */
2 
3 #include "fpm_config.h"
4 
5 #ifdef HAVE_ALLOCA_H
6 #include <alloca.h>
7 #endif
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include "fpm_env.h"
13 #include "fpm.h"
14 
15 #ifndef HAVE_SETPROCTITLE
16 #ifdef __linux__
17 static char **fpm_env_argv = NULL;
18 static size_t fpm_env_argv_len = 0;
19 #endif
20 #endif
21 
22 #ifndef HAVE_SETENV
23 # ifdef (__sparc__ || __sparc)
setenv(char * name,char * value,int clobber)24 int setenv(char *name, char *value, int clobber) /* {{{ */
25 {
26 	char   *malloc();
27 	char   *getenv();
28 	char   *cp;
29 
30 	if (clobber == 0 && getenv(name) != 0) {
31 		return 0;
32 	}
33 
34 	if ((cp = malloc(strlen(name) + strlen(value) + 2)) == 0) {
35 		return 1;
36 	}
37 	sprintf(cp, "%s=%s", name, value);
38 	return putenv(cp);
39 }
40 /* }}} */
41 # else
setenv(char * name,char * value,int overwrite)42 int setenv(char *name, char *value, int overwrite) /* {{{ */
43 {
44 	int name_len = strlen(name);
45 	int value_len = strlen(value);
46 	char *var = alloca(name_len + 1 + value_len + 1);
47 
48 	memcpy(var, name, name_len);
49 
50 	var[name_len] = '=';
51 
52 	memcpy(var + name_len + 1, value, value_len);
53 
54 	var[name_len + 1 + value_len] = '\0';
55 
56 	return putenv(var);
57 }
58 /* }}} */
59 # endif
60 #endif
61 
62 #ifndef HAVE_CLEARENV
clearenv(void)63 void clearenv(void)
64 {
65 	char **envp;
66 	char *s;
67 
68 	/* this algo is the only one known to me
69 		that works well on all systems */
70 	while (*(envp = environ)) {
71 		char *eq = strchr(*envp, '=');
72 
73 		s = strdup(*envp);
74 
75 		if (eq) s[eq - *envp] = '\0';
76 
77 		unsetenv(s);
78 		free(s);
79 	}
80 
81 }
82 #endif
83 
84 #ifndef HAVE_UNSETENV
unsetenv(const char * name)85 void unsetenv(const char *name) /* {{{ */
86 {
87 	if(getenv(name) != NULL) {
88 		int ct = 0;
89 		int del = 0;
90 
91 		while(environ[ct] != NULL) {
92 			if (nvmatch(name, environ[ct]) != 0) del=ct; /* <--- WTF?! */
93 			{ ct++; } /* <--- WTF?! */
94 		}
95 		/* isn't needed free here?? */
96 		environ[del] = environ[ct-1];
97 		environ[ct-1] = NULL;
98 	}
99 }
100 /* }}} */
101 
nvmatch(char * s1,char * s2)102 static char * nvmatch(char *s1, char *s2) /* {{{ */
103 {
104 	while(*s1 == *s2++)
105 	{
106 		if(*s1++ == '=') {
107 			return s2;
108 		}
109 	}
110 	if(*s1 == '\0' && *(s2-1) == '=') {
111 		return s2;
112 	}
113 	return NULL;
114 }
115 /* }}} */
116 #endif
117 
fpm_env_setproctitle(char * title)118 void fpm_env_setproctitle(char *title) /* {{{ */
119 {
120 #if defined(HAVE_SETPROCTITLE_FAST)
121 	setproctitle_fast("%s", title);
122 #elif defined(HAVE_SETPROCTITLE)
123 	setproctitle("%s", title);
124 #else
125 #ifdef __linux__
126 	if (fpm_env_argv != NULL && fpm_env_argv_len > strlen(SETPROCTITLE_PREFIX) + 3) {
127 		memset(fpm_env_argv[0], 0, fpm_env_argv_len);
128 		strncpy(fpm_env_argv[0], SETPROCTITLE_PREFIX, fpm_env_argv_len - 2);
129 		strncpy(fpm_env_argv[0] + strlen(SETPROCTITLE_PREFIX), title, fpm_env_argv_len - strlen(SETPROCTITLE_PREFIX) - 2);
130 		fpm_env_argv[1] = NULL;
131 	}
132 #endif
133 #endif
134 }
135 /* }}} */
136 
fpm_env_init_child(struct fpm_worker_pool_s * wp)137 int fpm_env_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
138 {
139 	struct key_value_s *kv;
140 	char *title;
141 	spprintf(&title, 0, "pool %s", wp->config->name);
142 	fpm_env_setproctitle(title);
143 	efree(title);
144 
145 	if (wp->config->clear_env) {
146 		clearenv();
147 	}
148 
149 	for (kv = wp->config->env; kv; kv = kv->next) {
150 		setenv(kv->key, kv->value, 1);
151 	}
152 
153 	if (wp->user) {
154 		setenv("USER", wp->user, 1);
155 	}
156 
157 	if (wp->home) {
158 		setenv("HOME", wp->home, 1);
159 	}
160 
161 	return 0;
162 }
163 /* }}} */
164 
fpm_env_conf_wp(struct fpm_worker_pool_s * wp)165 static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
166 {
167 	struct key_value_s *kv;
168 
169 	for (kv = wp->config->env; kv; kv = kv->next) {
170 		if (*kv->value == '$') {
171 			char *value = getenv(kv->value + 1);
172 
173 			if (!value) {
174 				value = "";
175 			}
176 
177 			free(kv->value);
178 			kv->value = strdup(value);
179 		}
180 
181 		/* autodetected values should be removed
182 			if these vars specified in config */
183 		if (!strcmp(kv->key, "USER")) {
184 			free(wp->user);
185 			wp->user = 0;
186 		}
187 
188 		if (!strcmp(kv->key, "HOME")) {
189 			free(wp->home);
190 			wp->home = 0;
191 		}
192 	}
193 
194 	return 0;
195 }
196 /* }}} */
197 
fpm_env_init_main(void)198 int fpm_env_init_main(void)
199 {
200 	struct fpm_worker_pool_s *wp;
201 	char *title;
202 
203 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
204 		if (0 > fpm_env_conf_wp(wp)) {
205 			return -1;
206 		}
207 	}
208 #ifndef HAVE_SETPROCTITLE
209 #ifdef __linux__
210 	int i;
211 	char *first = NULL;
212 	char *last = NULL;
213 	/*
214 	 * This piece of code has been inspirated from nginx and pureftpd code, which
215 	 * are under BSD licence.
216 	 *
217 	 * To change the process title in Linux we have to set argv[1] to NULL
218 	 * and to copy the title to the same place where the argv[0] points to.
219 	 * However, argv[0] may be too small to hold a new title.  Fortunately, Linux
220 	 * store argv[] and environ[] one after another.  So we should ensure that is
221 	 * the continuous memory and then we allocate the new memory for environ[]
222 	 * and copy it.  After this we could use the memory starting from argv[0] for
223 	 * our process title.
224 	 */
225 
226 	for (i = 0; i < fpm_globals.argc; i++) {
227 		if (first == NULL) {
228 			first = fpm_globals.argv[i];
229 		}
230 		if (last == NULL || fpm_globals.argv[i] == last + 1) {
231 			last = fpm_globals.argv[i] + strlen(fpm_globals.argv[i]);
232 		}
233 	}
234 	if (environ) {
235 		for (i = 0; environ[i]; i++) {
236 			if (first == NULL) {
237 				first = environ[i];
238 			}
239 			if (last == NULL || environ[i] == last + 1) {
240 				last = environ[i] + strlen(environ[i]);
241 			}
242 		}
243 	}
244 	if (first == NULL || last == NULL) {
245 		return 0;
246 	}
247 
248 	fpm_env_argv_len = last - first;
249 	fpm_env_argv = fpm_globals.argv;
250 	if (environ != NULL) {
251 		char **new_environ;
252 		unsigned int env_nb = 0U;
253 
254 		while (environ[env_nb]) {
255 			env_nb++;
256 		}
257 
258 		if ((new_environ = malloc((1U + env_nb) * sizeof (char *))) == NULL) {
259 			return -1;
260 		}
261 		new_environ[env_nb] = NULL;
262 		while (env_nb > 0U) {
263 			env_nb--;
264 			new_environ[env_nb] = strdup(environ[env_nb]);
265 		}
266 		environ = new_environ;
267 	}
268 #endif
269 #endif
270 
271 	spprintf(&title, 0, "master process (%s)", fpm_globals.config);
272 	fpm_env_setproctitle(title);
273 	efree(title);
274 	return 0;
275 }
276