sapi/cli: Refactor process title setting code (#17177)

This commit is contained in:
Gina Peter Banyard 2024-12-16 22:50:06 +00:00 committed by GitHub
parent 6972612e1e
commit d21777d4a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 22 deletions

View file

@ -27,13 +27,12 @@ PHP_FUNCTION(cli_set_process_title)
{
char *title = NULL;
size_t title_len;
int rc;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &title, &title_len) == FAILURE) {
RETURN_THROWS();
}
rc = set_ps_title(title);
ps_title_status rc = set_ps_title(title, title_len);
if (rc == PS_TITLE_SUCCESS) {
RETURN_TRUE;
}
@ -48,13 +47,12 @@ PHP_FUNCTION(cli_get_process_title)
{
size_t length = 0;
const char* title = NULL;
int rc;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
rc = get_ps_title(&length, &title);
ps_title_status rc = get_ps_title(&length, &title);
if (rc != PS_TITLE_SUCCESS) {
php_error_docref(NULL, E_WARNING, "cli_get_process_title had an error: %s", ps_title_errno(rc));
RETURN_NULL();

View file

@ -284,7 +284,7 @@ clobber_error:
* and the init function was called.
* 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
return PS_TITLE_NOT_AVAILABLE; /* disabled functionality */
@ -304,7 +304,7 @@ int is_ps_title_available(void)
/*
* Convert error codes into error strings
*/
const char* ps_title_errno(int rc)
const char* ps_title_errno(ps_title_status rc)
{
switch(rc)
{
@ -320,11 +320,16 @@ const char* ps_title_errno(int rc)
case PS_TITLE_BUFFER_NOT_AVAILABLE:
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:
#ifdef PS_USE_WIN32
snprintf(windows_error_details, sizeof(windows_error_details), "Windows error code: %lu", GetLastError());
return windows_error_details;
#endif
return "Windows error";
}
return "Unknown error code";
@ -337,14 +342,19 @@ const char* ps_title_errno(int rc)
* save_ps_args() was not called.
* 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)
return rc;
size_t title_len = strlcpy(ps_buffer, title, ps_buffer_size);
ps_buffer_cur_len = (title_len >= ps_buffer_size) ? ps_buffer_size - 1 : title_len;
/* Include final null byte */
memcpy(ps_buffer, title, title_len+1);
ps_buffer_cur_len = title_len;
#ifdef PS_USE_SETPROCTITLE
setproctitle("%s", ps_buffer);
@ -394,9 +404,9 @@ int set_ps_title(const char* title)
* length into *displen.
* 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)
return rc;

View file

@ -17,21 +17,24 @@
#ifndef PS_TITLE_HEADER
#define PS_TITLE_HEADER
#define PS_TITLE_SUCCESS 0
#define PS_TITLE_NOT_AVAILABLE 1
#define PS_TITLE_NOT_INITIALIZED 2
#define PS_TITLE_BUFFER_NOT_AVAILABLE 3
#define PS_TITLE_WINDOWS_ERROR 4
typedef enum {
PS_TITLE_SUCCESS = 0,
PS_TITLE_NOT_AVAILABLE = 1,
PS_TITLE_NOT_INITIALIZED = 2,
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 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);