Skip to content

sapi/cli: Refactor process title setting code #17177

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

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions sapi/cli/php_cli_process_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down
28 changes: 19 additions & 9 deletions sapi/cli/ps_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ char** save_ps_args(int argc, char** argv)
* 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 */
Expand All @@ -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)
{
Expand All @@ -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";
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
21 changes: 12 additions & 9 deletions sapi/cli/ps_title.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading