Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in support for setting a process title. #280

Closed
wants to merge 10 commits into from
19 changes: 18 additions & 1 deletion sapi/cli/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ PHP_ARG_ENABLE(cli,,
[ --disable-cli Disable building CLI version of PHP
(this forces --without-pear)], yes, no)

AC_CHECK_FUNCS(setproctitle)

AC_CHECK_HEADERS([sys/pstat.h])

AC_CACHE_CHECK([for PS_STRINGS], [cli_cv_var_PS_STRINGS],
[AC_TRY_LINK(
[#include <machine/vmparam.h>
#include <sys/exec.h>
],
[PS_STRINGS->ps_nargvstr = 1;
PS_STRINGS->ps_argvstr = "foo";],
[cli_cv_var_PS_STRINGS=yes],
[cli_cv_var_PS_STRINGS=no])])
if test "$cli_cv_var_PS_STRINGS" = yes ; then
AC_DEFINE([HAVE_PS_STRINGS], [], [Define to 1 if the PS_STRINGS thing exists.])
fi

AC_MSG_CHECKING(for CLI build)
if test "$PHP_CLI" != "no"; then
PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/sapi/cli/Makefile.frag)
Expand All @@ -14,7 +31,7 @@ if test "$PHP_CLI" != "no"; then
SAPI_CLI_PATH=sapi/cli/php

dnl Select SAPI
PHP_SELECT_SAPI(cli, program, php_cli.c php_http_parser.c php_cli_server.c,, '$(SAPI_CLI_PATH)')
PHP_SELECT_SAPI(cli, program, php_cli.c php_http_parser.c php_cli_server.c ps_title.c php_cli_process_title.c,, '$(SAPI_CLI_PATH)')

case $host_alias in
*aix*)
Expand Down
4 changes: 2 additions & 2 deletions sapi/cli/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG_ENABLE('crt-debug', 'Enable CRT memory dumps for debugging sent to STDERR',
ARG_ENABLE('cli-win32', 'Build console-less CLI version of PHP', 'no');

if (PHP_CLI == "yes") {
SAPI('cli', 'php_cli.c php_http_parser.c php_cli_server.c', 'php.exe');
SAPI('cli', 'php_cli.c php_http_parser.c php_cli_server.c php_cli_process_title.c ps_title.c', 'php.exe');
ADD_FLAG("LIBS_CLI", "ws2_32.lib");
if (PHP_CRT_DEBUG == "yes") {
ADD_FLAG("CFLAGS_CLI", "/D PHP_WIN32_DEBUG_HEAP");
Expand All @@ -15,7 +15,7 @@ if (PHP_CLI == "yes") {
}

if (PHP_CLI_WIN32 == "yes") {
SAPI('cli_win32', 'cli_win32.c', 'php-win.exe');
SAPI('cli_win32', 'cli_win32.c php_cli_process_title.c ps_title.c', 'php-win.exe');
ADD_FLAG("LDFLAGS_CLI_WIN32", "/stack:8388608");
}

18 changes: 18 additions & 0 deletions sapi/cli/php_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
#include "php_cli_server.h"
#endif

#include "ps_title.h"
#include "php_cli_process_title.h"

#ifndef PHP_WIN32
# define php_select(m, r, w, e, t) select(m, r, w, e, t)
#else
Expand Down Expand Up @@ -478,6 +481,8 @@ ZEND_END_ARG_INFO()

static const zend_function_entry additional_functions[] = {
ZEND_FE(dl, arginfo_dl)
PHP_FE(cli_set_process_title, arginfo_cli_set_process_title)
PHP_FE(cli_get_process_title, arginfo_cli_get_process_title)
{NULL, NULL, NULL}
};

Expand Down Expand Up @@ -1200,6 +1205,7 @@ int main(int argc, char *argv[])
int argc = __argc;
char **argv = __argv;
#endif

int c;
int exit_status = SUCCESS;
int module_started = 0, sapi_started = 0;
Expand All @@ -1211,6 +1217,12 @@ int main(int argc, char *argv[])
int ini_ignore = 0;
sapi_module_struct *sapi_module = &cli_sapi_module;

/*
* Do not move this initialization. It needs to happen before argv is used
* in any way.
*/
argv = save_ps_args(argc, argv);

cli_sapi_module.additional_functions = additional_functions;

#if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
Expand Down Expand Up @@ -1299,6 +1311,7 @@ int main(int argc, char *argv[])
#ifndef PHP_CLI_WIN32_NO_CONSOLE
case 'S':
sapi_module = &cli_server_sapi_module;
cli_server_sapi_module.additional_functions = server_additional_functions;
break;
#endif
case 'h': /* help & quit */
Expand Down Expand Up @@ -1385,6 +1398,11 @@ int main(int argc, char *argv[])
tsrm_shutdown();
#endif

/*
* Do not move this de-initialization. It needs to happen right before
* exiting.
*/
cleanup_ps_args(argv);
exit(exit_status);
}
/* }}} */
Expand Down
80 changes: 80 additions & 0 deletions sapi/cli/php_cli_process_title.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Keyur Govande ([email protected]) |
+----------------------------------------------------------------------+
*/

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_cli_process_title.h"
#include "ps_title.h"

/* {{{ proto boolean cli_set_process_title(string arg)
Return a boolean to confirm if the process title was successfully changed or not */
PHP_FUNCTION(cli_set_process_title)
{
char *title = NULL;
int title_len;
int rc;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &title, &title_len) == FAILURE) {
return;
}

rc = set_ps_title(title);
if (rc == PS_TITLE_SUCCESS) {
RETURN_TRUE;
}

php_error_docref(NULL TSRMLS_CC, E_WARNING, "cli_set_process_title had an error: %s", ps_title_errno(rc));
RETURN_FALSE;
}
/* }}} */

/* {{{ proto string cli_get_process_title()
Return a string with the current process title. NULL if error. */
PHP_FUNCTION(cli_get_process_title)
{
int length = 0;
const char* title = NULL;
int rc;

if (zend_parse_parameters_none() == FAILURE) {
return;
}

rc = get_ps_title(&length, &title);
if (rc != PS_TITLE_SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cli_get_process_title had an error: %s", ps_title_errno(rc));
RETURN_NULL();
}

RETURN_STRINGL(title, length, 1);
}
/* }}} */

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
43 changes: 43 additions & 0 deletions sapi/cli/php_cli_process_title.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Keyur Govande ([email protected]) |
+----------------------------------------------------------------------+
*/

/* $Id$ */

#ifndef PHP_PS_TITLE_HEADER
#define PHP_PS_TITLE_HEADER

ZEND_BEGIN_ARG_INFO(arginfo_cli_set_process_title, 0)
ZEND_ARG_INFO(0, title)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_cli_get_process_title, 0)
ZEND_END_ARG_INFO()

PHP_FUNCTION(cli_set_process_title);
PHP_FUNCTION(cli_get_process_title);

#endif

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
8 changes: 8 additions & 0 deletions sapi/cli/php_cli_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
#include "php_http_parser.h"
#include "php_cli_server.h"

#include "php_cli_process_title.h"

#define OUTPUT_NOT_CHECKED -1
#define OUTPUT_IS_TTY 1
#define OUTPUT_NOT_TTY 0
Expand Down Expand Up @@ -429,6 +431,12 @@ zend_module_entry cli_server_module_entry = {
};
/* }}} */

const zend_function_entry server_additional_functions[] = {
PHP_FE(cli_set_process_title, arginfo_cli_set_process_title)
PHP_FE(cli_get_process_title, arginfo_cli_get_process_title)
{NULL, NULL, NULL}
};

static int sapi_cli_server_startup(sapi_module_struct *sapi_module) /* {{{ */
{
if (php_module_startup(sapi_module, &cli_server_module_entry, 1) == FAILURE) {
Expand Down
1 change: 1 addition & 0 deletions sapi/cli/php_cli_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "SAPI.h"

extern const zend_function_entry server_additional_functions[];
extern sapi_module_struct cli_server_sapi_module;
extern int do_cli_server(int argc, char **argv TSRMLS_DC);

Expand Down
Loading