Download | Plain Text | Line Numbers


--- pinba.cc	2010-11-26 10:05:25.000000000 +0100
+++ pinba.cc	2011-03-18 14:27:34.000000000 +0100
@@ -103,6 +103,7 @@
 
 #define timeval_cvt(a, b) do { (a)->tv_sec = (b)->tv_sec; (a)->tv_usec = (b)->tv_usec; } while (0);
 #define timeval_to_float(t) (float)(t).tv_sec + (float)(t).tv_usec / 1000000.0
+#define min(a, b) (a < b ? a : b)
 
 /* {{{ internal funcs */
 
@@ -1255,7 +1256,7 @@
 	PHP_FE(pinba_timer_get_info, NULL)
 	PHP_FE(pinba_timers_stop, NULL)
 	PHP_FE(pinba_script_name_set, NULL)
-	PHP_FE(pinba_hostname_set, NULL)
+	//PHP_FE(pinba_hostname_set, NULL)
 	{NULL, NULL, NULL}
 };
 /* }}} */
@@ -1396,6 +1397,17 @@
 	zval **tmp;
 	struct timeval t;
 	struct rusage u;
+	zval suexec_user;
+	uid_t euid;
+	int user_len;
+	struct passwd *pw = NULL;
+#if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R)
+	struct passwd _pw;
+	struct passwd *retpwptr = NULL;
+	long pwbuflen;
+	char *pwbuf = NULL;
+	int ret;
+#endif
 
 	PINBA_G(timers_stopped) = 0;
 
@@ -1420,13 +1432,45 @@
 	PINBA_G(server_name) = NULL;
 	PINBA_G(script_name) = NULL;
 
-	gethostname(PINBA_G(host_name), sizeof(PINBA_G(host_name)));
-	PINBA_G(host_name)[sizeof(PINBA_G(host_name))] = '\0';
-
 #if PHP_MAJOR_VERSION >= 5
 	zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC);
 #endif
 
+	PINBA_G(host_name)[0] = '\0';
+	if (zend_get_constant("SUEXEC_USER", sizeof("SUEXEC_USER") - 1, &suexec_user TSRMLS_CC)) {
+		if (Z_TYPE(suexec_user) == IS_STRING && Z_STRLEN(suexec_user) != 0) {
+			user_len = min(Z_STRLEN(suexec_user), sizeof(PINBA_G(host_name)));
+			memcpy(PINBA_G(host_name), Z_STRVAL(suexec_user), user_len);
+			PINBA_G(host_name)[user_len] = '\0';
+		}
+		zval_dtor(&suexec_user);
+	}
+	if (PINBA_G(host_name)[0] == '\0') {
+		euid = geteuid();
+#if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R)
+		pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+		if (pwbuflen > 1) {
+			pwbuf = (char *)emalloc(pwbuflen);
+			ret = getpwuid_r(euid, &_pw, pwbuf, pwbuflen, &retpwptr);
+			if (ret == 0 && retpwptr != NULL) {
+				pw = &_pw;
+			}
+		}
+#else
+		pw = getpwuid(euid);
+#endif
+		if (pw != NULL) {
+			user_len = (pw->pw_name == NULL) ? 0 : min(strlen(pw->pw_name), sizeof(PINBA_G(host_name)));
+			memcpy(PINBA_G(host_name), pw->pw_name, user_len);
+			PINBA_G(host_name)[user_len] = '\0';
+		}
+#if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R)
+		if (pwbuf != NULL) {
+			efree(pwbuf);
+		}
+#endif
+	}
+
 	if (PG(http_globals)[TRACK_VARS_SERVER] && zend_hash_find(HASH_OF(PG(http_globals)[TRACK_VARS_SERVER]), "SCRIPT_NAME", sizeof("SCRIPT_NAME"), (void **) &tmp) != FAILURE && Z_TYPE_PP(tmp) == IS_STRING && Z_STRLEN_PP(tmp) > 0) {
 		PINBA_G(script_name) = estrndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
 	}