xref: /php-src/sapi/cli/php_cli_process_title.c (revision 9a7d37ac)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Keyur Govande (kgovande@gmail.com)                           |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_cli_process_title.h"
23 #include "ps_title.h"
24 
25 /* {{{ Return a boolean to confirm if the process title was successfully changed or not */
PHP_FUNCTION(cli_set_process_title)26 PHP_FUNCTION(cli_set_process_title)
27 {
28 	char *title = NULL;
29 	size_t title_len;
30 	int rc;
31 
32 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &title, &title_len) == FAILURE) {
33 		RETURN_THROWS();
34 	}
35 
36 	rc = set_ps_title(title);
37 	if (rc == PS_TITLE_SUCCESS) {
38 		RETURN_TRUE;
39 	}
40 
41 	php_error_docref(NULL, E_WARNING, "cli_set_process_title had an error: %s", ps_title_errno(rc));
42 	RETURN_FALSE;
43 }
44 /* }}} */
45 
46 /* {{{ Return a string with the current process title. NULL if error. */
PHP_FUNCTION(cli_get_process_title)47 PHP_FUNCTION(cli_get_process_title)
48 {
49 	size_t length = 0;
50 	const char* title = NULL;
51 	int rc;
52 
53 	if (zend_parse_parameters_none() == FAILURE) {
54 		RETURN_THROWS();
55 	}
56 
57 	rc = get_ps_title(&length, &title);
58 	if (rc != PS_TITLE_SUCCESS) {
59 			php_error_docref(NULL, E_WARNING, "cli_get_process_title had an error: %s", ps_title_errno(rc));
60 			RETURN_NULL();
61 	}
62 
63 	RETURN_STRINGL(title, length);
64 }
65 /* }}} */
66