mirror of
https://github.com/php/php-src.git
synced 2025-08-15 13:38:49 +02:00
sapi/cli: Refactor process title setting code (#17177)
This commit is contained in:
parent
6972612e1e
commit
d21777d4a2
3 changed files with 33 additions and 22 deletions
|
@ -27,13 +27,12 @@ PHP_FUNCTION(cli_set_process_title)
|
||||||
{
|
{
|
||||||
char *title = NULL;
|
char *title = NULL;
|
||||||
size_t title_len;
|
size_t title_len;
|
||||||
int rc;
|
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &title, &title_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &title, &title_len) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = set_ps_title(title);
|
ps_title_status rc = set_ps_title(title, title_len);
|
||||||
if (rc == PS_TITLE_SUCCESS) {
|
if (rc == PS_TITLE_SUCCESS) {
|
||||||
RETURN_TRUE;
|
RETURN_TRUE;
|
||||||
}
|
}
|
||||||
|
@ -48,13 +47,12 @@ PHP_FUNCTION(cli_get_process_title)
|
||||||
{
|
{
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
const char* title = NULL;
|
const char* title = NULL;
|
||||||
int rc;
|
|
||||||
|
|
||||||
if (zend_parse_parameters_none() == FAILURE) {
|
if (zend_parse_parameters_none() == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = get_ps_title(&length, &title);
|
ps_title_status rc = get_ps_title(&length, &title);
|
||||||
if (rc != PS_TITLE_SUCCESS) {
|
if (rc != PS_TITLE_SUCCESS) {
|
||||||
php_error_docref(NULL, E_WARNING, "cli_get_process_title had an error: %s", ps_title_errno(rc));
|
php_error_docref(NULL, E_WARNING, "cli_get_process_title had an error: %s", ps_title_errno(rc));
|
||||||
RETURN_NULL();
|
RETURN_NULL();
|
||||||
|
|
|
@ -284,7 +284,7 @@ clobber_error:
|
||||||
* and the init function was called.
|
* and the init function was called.
|
||||||
* Otherwise returns NOT_AVAILABLE or NOT_INITIALIZED
|
* Otherwise returns NOT_AVAILABLE or NOT_INITIALIZED
|
||||||
*/
|
*/
|
||||||
int is_ps_title_available(void)
|
ps_title_status is_ps_title_available(void)
|
||||||
{
|
{
|
||||||
#ifdef PS_USE_NONE
|
#ifdef PS_USE_NONE
|
||||||
return PS_TITLE_NOT_AVAILABLE; /* disabled functionality */
|
return PS_TITLE_NOT_AVAILABLE; /* disabled functionality */
|
||||||
|
@ -304,7 +304,7 @@ int is_ps_title_available(void)
|
||||||
/*
|
/*
|
||||||
* Convert error codes into error strings
|
* Convert error codes into error strings
|
||||||
*/
|
*/
|
||||||
const char* ps_title_errno(int rc)
|
const char* ps_title_errno(ps_title_status rc)
|
||||||
{
|
{
|
||||||
switch(rc)
|
switch(rc)
|
||||||
{
|
{
|
||||||
|
@ -320,11 +320,16 @@ const char* ps_title_errno(int rc)
|
||||||
case PS_TITLE_BUFFER_NOT_AVAILABLE:
|
case PS_TITLE_BUFFER_NOT_AVAILABLE:
|
||||||
return "Buffer not contiguous";
|
return "Buffer not contiguous";
|
||||||
|
|
||||||
#ifdef PS_USE_WIN32
|
case PS_TITLE_TOO_LONG:
|
||||||
|
// TODO Indicate max length?
|
||||||
|
return "Too long";
|
||||||
|
|
||||||
case PS_TITLE_WINDOWS_ERROR:
|
case PS_TITLE_WINDOWS_ERROR:
|
||||||
|
#ifdef PS_USE_WIN32
|
||||||
snprintf(windows_error_details, sizeof(windows_error_details), "Windows error code: %lu", GetLastError());
|
snprintf(windows_error_details, sizeof(windows_error_details), "Windows error code: %lu", GetLastError());
|
||||||
return windows_error_details;
|
return windows_error_details;
|
||||||
#endif
|
#endif
|
||||||
|
return "Windows error";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Unknown error code";
|
return "Unknown error code";
|
||||||
|
@ -337,14 +342,19 @@ const char* ps_title_errno(int rc)
|
||||||
* save_ps_args() was not called.
|
* save_ps_args() was not called.
|
||||||
* Else returns 0 on success.
|
* Else returns 0 on success.
|
||||||
*/
|
*/
|
||||||
int set_ps_title(const char* title)
|
ps_title_status set_ps_title(const char *title, size_t title_len)
|
||||||
{
|
{
|
||||||
int rc = is_ps_title_available();
|
if (title_len >= ps_buffer_size) {
|
||||||
|
return PS_TITLE_TOO_LONG;
|
||||||
|
}
|
||||||
|
|
||||||
|
ps_title_status rc = is_ps_title_available();
|
||||||
if (rc != PS_TITLE_SUCCESS)
|
if (rc != PS_TITLE_SUCCESS)
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
size_t title_len = strlcpy(ps_buffer, title, ps_buffer_size);
|
/* Include final null byte */
|
||||||
ps_buffer_cur_len = (title_len >= ps_buffer_size) ? ps_buffer_size - 1 : title_len;
|
memcpy(ps_buffer, title, title_len+1);
|
||||||
|
ps_buffer_cur_len = title_len;
|
||||||
|
|
||||||
#ifdef PS_USE_SETPROCTITLE
|
#ifdef PS_USE_SETPROCTITLE
|
||||||
setproctitle("%s", ps_buffer);
|
setproctitle("%s", ps_buffer);
|
||||||
|
@ -394,9 +404,9 @@ int set_ps_title(const char* title)
|
||||||
* length into *displen.
|
* length into *displen.
|
||||||
* The return code indicates the error.
|
* The return code indicates the error.
|
||||||
*/
|
*/
|
||||||
int get_ps_title(size_t *displen, const char** string)
|
ps_title_status get_ps_title(size_t *displen, const char** string)
|
||||||
{
|
{
|
||||||
int rc = is_ps_title_available();
|
ps_title_status rc = is_ps_title_available();
|
||||||
if (rc != PS_TITLE_SUCCESS)
|
if (rc != PS_TITLE_SUCCESS)
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
|
|
|
@ -17,21 +17,24 @@
|
||||||
#ifndef PS_TITLE_HEADER
|
#ifndef PS_TITLE_HEADER
|
||||||
#define PS_TITLE_HEADER
|
#define PS_TITLE_HEADER
|
||||||
|
|
||||||
#define PS_TITLE_SUCCESS 0
|
typedef enum {
|
||||||
#define PS_TITLE_NOT_AVAILABLE 1
|
PS_TITLE_SUCCESS = 0,
|
||||||
#define PS_TITLE_NOT_INITIALIZED 2
|
PS_TITLE_NOT_AVAILABLE = 1,
|
||||||
#define PS_TITLE_BUFFER_NOT_AVAILABLE 3
|
PS_TITLE_NOT_INITIALIZED = 2,
|
||||||
#define PS_TITLE_WINDOWS_ERROR 4
|
PS_TITLE_BUFFER_NOT_AVAILABLE = 3,
|
||||||
|
PS_TITLE_WINDOWS_ERROR = 4,
|
||||||
|
PS_TITLE_TOO_LONG = 5,
|
||||||
|
} ps_title_status;
|
||||||
|
|
||||||
extern char** save_ps_args(int argc, char** argv);
|
extern char** save_ps_args(int argc, char** argv);
|
||||||
|
|
||||||
extern int set_ps_title(const char* new_str);
|
extern ps_title_status set_ps_title(const char *title, size_t title_len);
|
||||||
|
|
||||||
extern int get_ps_title(size_t* displen, const char** string);
|
extern ps_title_status get_ps_title(size_t* displen, const char** string);
|
||||||
|
|
||||||
extern const char* ps_title_errno(int rc);
|
extern const char* ps_title_errno(ps_title_status rc);
|
||||||
|
|
||||||
extern int is_ps_title_available(void);
|
extern ps_title_status is_ps_title_available(void);
|
||||||
|
|
||||||
extern void cleanup_ps_args(char **argv);
|
extern void cleanup_ps_args(char **argv);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue