fat                                      Fri, 12 Nov 2010 00:30:35 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=305287

Log:
- Fixed #52660 (custom process title for FPM)

Bug: http://bugs.php.net/52660 (Analyzed) change FPM processes title according 
to their type
      
Changed paths:
    U   php/php-src/branches/PHP_5_3/NEWS
    U   php/php-src/branches/PHP_5_3/sapi/fpm/config.m4
    U   php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm.c
    U   php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_conf.c
    U   php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_env.c
    U   php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_env.h
    U   php/php-src/trunk/sapi/fpm/config.m4
    U   php/php-src/trunk/sapi/fpm/fpm/fpm.c
    U   php/php-src/trunk/sapi/fpm/fpm/fpm_conf.c
    U   php/php-src/trunk/sapi/fpm/fpm/fpm_env.c
    U   php/php-src/trunk/sapi/fpm/fpm/fpm_env.h

Modified: php/php-src/branches/PHP_5_3/NEWS
===================================================================
--- php/php-src/branches/PHP_5_3/NEWS	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/branches/PHP_5_3/NEWS	2010-11-12 00:30:35 UTC (rev 305287)
@@ -14,6 +14,7 @@
   (Andrey)
 - Improved support for is_link and related functions on Windows. (Pierre)

+- Added custom process title for FPM. (fat)
 - Added '-t/--test' to php-fpm to check and validate FPM conf file. (fat)
 - Added statistics about listening socket queue length for FPM.
   (andrei dot nigmatulin at gmail dot com, fat)

Modified: php/php-src/branches/PHP_5_3/sapi/fpm/config.m4
===================================================================
--- php/php-src/branches/PHP_5_3/sapi/fpm/config.m4	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/branches/PHP_5_3/sapi/fpm/config.m4	2010-11-12 00:30:35 UTC (rev 305287)
@@ -219,7 +219,7 @@
 dnl configure checks {{{
 AC_DEFUN([AC_FPM_STDLIBS],
 [
-  AC_CHECK_FUNCS(setenv clearenv)
+  AC_CHECK_FUNCS(setenv clearenv setproctitle)

   AC_SEARCH_LIBS(socket, socket)
   AC_SEARCH_LIBS(inet_addr, nsl)

Modified: php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm.c
===================================================================
--- php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm.c	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm.c	2010-11-12 00:30:35 UTC (rev 305287)
@@ -33,9 +33,9 @@
 		0 > fpm_stdio_init_main()            ||
 		0 > fpm_conf_init_main()             ||
 		0 > fpm_unix_init_main()             ||
+		0 > fpm_pctl_init_main()             ||
 		0 > fpm_env_init_main()              ||
 		0 > fpm_signals_init_main()          ||
-		0 > fpm_pctl_init_main()             ||
 		0 > fpm_children_init_main()         ||
 		0 > fpm_sockets_init_main()          ||
 		0 > fpm_worker_pool_init_main()      ||

Modified: php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_conf.c
===================================================================
--- php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_conf.c	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_conf.c	2010-11-12 00:30:35 UTC (rev 305287)
@@ -698,6 +698,7 @@
 	free(fpm_global_config.error_log);
 	fpm_global_config.pid_file = 0;
 	fpm_global_config.error_log = 0;
+	efree(fpm_globals.config);
 }
 /* }}} */

@@ -1010,38 +1011,34 @@

 int fpm_conf_init_main() /* {{{ */
 {
-	char *filename = fpm_globals.config;
-	int free = 0;
 	int ret;
 	TSRMLS_FETCH();

-	if (filename == NULL) {
-		spprintf(&filename, 0, "%s/php-fpm.conf", PHP_SYSCONFDIR);
-		free = 1;
+	if (fpm_globals.config == NULL) {
+		spprintf(&fpm_globals.config, 0, "%s/php-fpm.conf", PHP_SYSCONFDIR);
+		if (!fpm_globals.config) {
+			zlog(ZLOG_SYSERROR, "spprintf() failed (\"%s/php-fpm.conf\")", PHP_SYSCONFDIR);
+			return -1;
+		}
 	}

-	ret = fpm_conf_load_ini_file(filename TSRMLS_CC);
+	ret = fpm_conf_load_ini_file(fpm_globals.config TSRMLS_CC);

 	if (0 > ret) {
-		zlog(ZLOG_ERROR, "failed to load configuration file '%s'", filename);
-		if (free) efree(filename);
+		zlog(ZLOG_ERROR, "failed to load configuration file '%s'", fpm_globals.config);
 		return -1;
 	}

 	if (0 > fpm_conf_post_process()) {
 		zlog(ZLOG_ERROR, "failed to post process the configuration");
-		if (free) efree(filename);
 		return -1;
 	}

 	if (fpm_globals.test_conf) {
-		zlog(ZLOG_NOTICE, "configuration file %s test is successful\n", filename);
-		if (free) efree(filename);
+		zlog(ZLOG_NOTICE, "configuration file %s test is successful\n", fpm_globals.config);
 		return -1;
 	}

-	if (free) efree(filename);
-
 	if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0)) {
 		return -1;
 	}

Modified: php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_env.c
===================================================================
--- php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_env.c	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_env.c	2010-11-12 00:30:35 UTC (rev 305287)
@@ -12,8 +12,16 @@
 #include <string.h>

 #include "fpm_env.h"
+#include "fpm.h"
 #include "zlog.h"

+#ifndef HAVE_SETPROCTITLE
+#ifdef __linux__
+static char **fpm_env_argv = NULL;
+static size_t fpm_env_argv_len = 0;
+#endif
+#endif
+
 #ifndef HAVE_SETENV
 # ifdef (__sparc__ || __sparc)
 int setenv(char *name, char *value, int clobber) /* {{{ */
@@ -111,9 +119,30 @@
 /* }}} */
 #endif

+void fpm_env_setproctitle(char *title) /* {{{ */
+{
+#ifdef HAVE_SETPROCTITLE
+	setproctitle("%s", title);
+#else
+#ifdef __linux__
+	if (fpm_env_argv != NULL && fpm_env_argv_len > strlen(SETPROCTITLE_PREFIX) + 3) {
+		memset(fpm_env_argv[0], 0, fpm_env_argv_len);
+		strncpy(fpm_env_argv[0], SETPROCTITLE_PREFIX, fpm_env_argv_len - 2);
+		strncpy(fpm_env_argv[0] + strlen(SETPROCTITLE_PREFIX), title, fpm_env_argv_len - strlen(SETPROCTITLE_PREFIX) - 2);
+		fpm_env_argv[1] = NULL;
+	}
+#endif
+#endif
+}
+/* }}} */
+
 int fpm_env_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
 {
 	struct key_value_s *kv;
+	char *title;
+	spprintf(&title, 0, "pool %s", wp->config->name);
+	fpm_env_setproctitle(title);
+	efree(title);

 	clearenv();

@@ -169,12 +198,79 @@
 int fpm_env_init_main() /* {{{ */
 {
 	struct fpm_worker_pool_s *wp;
+	int i;
+	char *first = NULL;
+	char *last = NULL;
+	char *title;

 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
 		if (0 > fpm_env_conf_wp(wp)) {
 			return -1;
 		}
 	}
+#ifndef HAVE_SETPROCTITLE
+#ifdef __linux__
+	/*
+	 * This piece of code has been inspirated from nginx and pureftpd code, whic
+	 * are under BSD licence.
+	 *
+	 * To change the process title in Linux we have to set argv[1] to NULL
+	 * and to copy the title to the same place where the argv[0] points to.
+	 * However, argv[0] may be too small to hold a new title.  Fortunately, Linux
+	 * store argv[] and environ[] one after another.  So we should ensure that is
+	 * the continuous memory and then we allocate the new memory for environ[]
+	 * and copy it.  After this we could use the memory starting from argv[0] for
+	 * our process title.
+	 */
+
+	for (i = 0; i < fpm_globals.argc; i++) {
+		if (first == NULL) {
+			first = fpm_globals.argv[i];
+		}
+		if (last == NULL || fpm_globals.argv[i] == last + 1) {
+			last = fpm_globals.argv[i] + strlen(fpm_globals.argv[i]);
+		}
+	}
+	if (environ) {
+		for (i = 0; environ[i]; i++) {
+			if (first == NULL) {
+				first = environ[i];
+			}
+			if (last == NULL || environ[i] == last + 1) {
+				last = environ[i] + strlen(environ[i]);
+			}
+		}
+	}
+	if (first == NULL || last == NULL) {
+		return 0;
+	}
+
+	fpm_env_argv_len = last - first;
+	fpm_env_argv = fpm_globals.argv;
+	if (environ != NULL) {
+		char **new_environ;
+		unsigned int env_nb = 0U;
+
+		while (environ[env_nb]) {
+			env_nb++;
+		}
+
+		if ((new_environ = malloc((1U + env_nb) * sizeof (char *))) == NULL) {
+			return -1;
+		}
+		new_environ[env_nb] = NULL;
+		while (env_nb > 0U) {
+			env_nb--;
+			new_environ[env_nb] = strdup(environ[env_nb]);
+		}
+		environ = new_environ;
+	}
+#endif
+#endif
+
+	spprintf(&title, 0, "master process (%s)", fpm_globals.config);
+	fpm_env_setproctitle(title);
+	efree(title);
 	return 0;
 }
 /* }}} */

Modified: php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_env.h
===================================================================
--- php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_env.h	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/branches/PHP_5_3/sapi/fpm/fpm/fpm_env.h	2010-11-12 00:30:35 UTC (rev 305287)
@@ -7,8 +7,11 @@

 #include "fpm_worker_pool.h"

+#define SETPROCTITLE_PREFIX "php-fpm: "
+
 int fpm_env_init_child(struct fpm_worker_pool_s *wp);
 int fpm_env_init_main();
+void fpm_env_setproctitle(char *title);

 extern char **environ;


Modified: php/php-src/trunk/sapi/fpm/config.m4
===================================================================
--- php/php-src/trunk/sapi/fpm/config.m4	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/trunk/sapi/fpm/config.m4	2010-11-12 00:30:35 UTC (rev 305287)
@@ -219,7 +219,7 @@
 dnl configure checks {{{
 AC_DEFUN([AC_FPM_STDLIBS],
 [
-  AC_CHECK_FUNCS(setenv clearenv)
+  AC_CHECK_FUNCS(setenv clearenv setproctitle)

   AC_SEARCH_LIBS(socket, socket)
   AC_SEARCH_LIBS(inet_addr, nsl)

Modified: php/php-src/trunk/sapi/fpm/fpm/fpm.c
===================================================================
--- php/php-src/trunk/sapi/fpm/fpm/fpm.c	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/trunk/sapi/fpm/fpm/fpm.c	2010-11-12 00:30:35 UTC (rev 305287)
@@ -33,9 +33,9 @@
 		0 > fpm_stdio_init_main()            ||
 		0 > fpm_conf_init_main()             ||
 		0 > fpm_unix_init_main()             ||
+		0 > fpm_pctl_init_main()             ||
 		0 > fpm_env_init_main()              ||
 		0 > fpm_signals_init_main()          ||
-		0 > fpm_pctl_init_main()             ||
 		0 > fpm_children_init_main()         ||
 		0 > fpm_sockets_init_main()          ||
 		0 > fpm_worker_pool_init_main()      ||

Modified: php/php-src/trunk/sapi/fpm/fpm/fpm_conf.c
===================================================================
--- php/php-src/trunk/sapi/fpm/fpm/fpm_conf.c	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/trunk/sapi/fpm/fpm/fpm_conf.c	2010-11-12 00:30:35 UTC (rev 305287)
@@ -698,6 +698,7 @@
 	free(fpm_global_config.error_log);
 	fpm_global_config.pid_file = 0;
 	fpm_global_config.error_log = 0;
+	efree(fpm_globals.config);
 }
 /* }}} */

@@ -1010,38 +1011,34 @@

 int fpm_conf_init_main() /* {{{ */
 {
-	char *filename = fpm_globals.config;
-	int free = 0;
 	int ret;
 	TSRMLS_FETCH();

-	if (filename == NULL) {
-		spprintf(&filename, 0, "%s/php-fpm.conf", PHP_SYSCONFDIR);
-		free = 1;
+	if (fpm_globals.config == NULL) {
+		spprintf(&fpm_globals.config, 0, "%s/php-fpm.conf", PHP_SYSCONFDIR);
+		if (!fpm_globals.config) {
+			zlog(ZLOG_SYSERROR, "spprintf() failed (\"%s/php-fpm.conf\")", PHP_SYSCONFDIR);
+			return -1;
+		}
 	}

-	ret = fpm_conf_load_ini_file(filename TSRMLS_CC);
+	ret = fpm_conf_load_ini_file(fpm_globals.config TSRMLS_CC);

 	if (0 > ret) {
-		zlog(ZLOG_ERROR, "failed to load configuration file '%s'", filename);
-		if (free) efree(filename);
+		zlog(ZLOG_ERROR, "failed to load configuration file '%s'", fpm_globals.config);
 		return -1;
 	}

 	if (0 > fpm_conf_post_process()) {
 		zlog(ZLOG_ERROR, "failed to post process the configuration");
-		if (free) efree(filename);
 		return -1;
 	}

 	if (fpm_globals.test_conf) {
-		zlog(ZLOG_NOTICE, "configuration file %s test is successful\n", filename);
-		if (free) efree(filename);
+		zlog(ZLOG_NOTICE, "configuration file %s test is successful\n", fpm_globals.config);
 		return -1;
 	}

-	if (free) efree(filename);
-
 	if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0)) {
 		return -1;
 	}

Modified: php/php-src/trunk/sapi/fpm/fpm/fpm_env.c
===================================================================
--- php/php-src/trunk/sapi/fpm/fpm/fpm_env.c	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/trunk/sapi/fpm/fpm/fpm_env.c	2010-11-12 00:30:35 UTC (rev 305287)
@@ -12,8 +12,16 @@
 #include <string.h>

 #include "fpm_env.h"
+#include "fpm.h"
 #include "zlog.h"

+#ifndef HAVE_SETPROCTITLE
+#ifdef __linux__
+static char **fpm_env_argv = NULL;
+static size_t fpm_env_argv_len = 0;
+#endif
+#endif
+
 #ifndef HAVE_SETENV
 # ifdef (__sparc__ || __sparc)
 int setenv(char *name, char *value, int clobber) /* {{{ */
@@ -111,9 +119,30 @@
 /* }}} */
 #endif

+void fpm_env_setproctitle(char *title) /* {{{ */
+{
+#ifdef HAVE_SETPROCTITLE
+	setproctitle("%s", title);
+#else
+#ifdef __linux__
+	if (fpm_env_argv != NULL && fpm_env_argv_len > strlen(SETPROCTITLE_PREFIX) + 3) {
+		memset(fpm_env_argv[0], 0, fpm_env_argv_len);
+		strncpy(fpm_env_argv[0], SETPROCTITLE_PREFIX, fpm_env_argv_len - 2);
+		strncpy(fpm_env_argv[0] + strlen(SETPROCTITLE_PREFIX), title, fpm_env_argv_len - strlen(SETPROCTITLE_PREFIX) - 2);
+		fpm_env_argv[1] = NULL;
+	}
+#endif
+#endif
+}
+/* }}} */
+
 int fpm_env_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
 {
 	struct key_value_s *kv;
+	char *title;
+	spprintf(&title, 0, "pool %s", wp->config->name);
+	fpm_env_setproctitle(title);
+	efree(title);

 	clearenv();

@@ -169,12 +198,79 @@
 int fpm_env_init_main() /* {{{ */
 {
 	struct fpm_worker_pool_s *wp;
+	int i;
+	char *first = NULL;
+	char *last = NULL;
+	char *title;

 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
 		if (0 > fpm_env_conf_wp(wp)) {
 			return -1;
 		}
 	}
+#ifndef HAVE_SETPROCTITLE
+#ifdef __linux__
+	/*
+	 * This piece of code has been inspirated from nginx and pureftpd code, whic
+	 * are under BSD licence.
+	 *
+	 * To change the process title in Linux we have to set argv[1] to NULL
+	 * and to copy the title to the same place where the argv[0] points to.
+	 * However, argv[0] may be too small to hold a new title.  Fortunately, Linux
+	 * store argv[] and environ[] one after another.  So we should ensure that is
+	 * the continuous memory and then we allocate the new memory for environ[]
+	 * and copy it.  After this we could use the memory starting from argv[0] for
+	 * our process title.
+	 */
+
+	for (i = 0; i < fpm_globals.argc; i++) {
+		if (first == NULL) {
+			first = fpm_globals.argv[i];
+		}
+		if (last == NULL || fpm_globals.argv[i] == last + 1) {
+			last = fpm_globals.argv[i] + strlen(fpm_globals.argv[i]);
+		}
+	}
+	if (environ) {
+		for (i = 0; environ[i]; i++) {
+			if (first == NULL) {
+				first = environ[i];
+			}
+			if (last == NULL || environ[i] == last + 1) {
+				last = environ[i] + strlen(environ[i]);
+			}
+		}
+	}
+	if (first == NULL || last == NULL) {
+		return 0;
+	}
+
+	fpm_env_argv_len = last - first;
+	fpm_env_argv = fpm_globals.argv;
+	if (environ != NULL) {
+		char **new_environ;
+		unsigned int env_nb = 0U;
+
+		while (environ[env_nb]) {
+			env_nb++;
+		}
+
+		if ((new_environ = malloc((1U + env_nb) * sizeof (char *))) == NULL) {
+			return -1;
+		}
+		new_environ[env_nb] = NULL;
+		while (env_nb > 0U) {
+			env_nb--;
+			new_environ[env_nb] = strdup(environ[env_nb]);
+		}
+		environ = new_environ;
+	}
+#endif
+#endif
+
+	spprintf(&title, 0, "master process (%s)", fpm_globals.config);
+	fpm_env_setproctitle(title);
+	efree(title);
 	return 0;
 }
 /* }}} */

Modified: php/php-src/trunk/sapi/fpm/fpm/fpm_env.h
===================================================================
--- php/php-src/trunk/sapi/fpm/fpm/fpm_env.h	2010-11-11 23:42:39 UTC (rev 305286)
+++ php/php-src/trunk/sapi/fpm/fpm/fpm_env.h	2010-11-12 00:30:35 UTC (rev 305287)
@@ -7,8 +7,11 @@

 #include "fpm_worker_pool.h"

+#define SETPROCTITLE_PREFIX "php-fpm: "
+
 int fpm_env_init_child(struct fpm_worker_pool_s *wp);
 int fpm_env_init_main();
+void fpm_env_setproctitle(char *title);

 extern char **environ;

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to