Skip to content

ext/mysqli: mysql_thread_id/mysqli_kill internal changes proposal. #12473

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
39 changes: 34 additions & 5 deletions ext/mysqli/mysqli_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,9 @@ PHP_FUNCTION(mysqli_insert_id)
/* {{{ Kill a mysql process on the server */
PHP_FUNCTION(mysqli_kill)
{
MY_MYSQL *mysql;
zval *mysql_link;
zend_long processid;
MY_MYSQL *mysql;
zval *mysql_link;
zend_long processid;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &mysql_link, mysqli_link_class_entry, &processid) == FAILURE) {
RETURN_THROWS();
Expand All @@ -1055,7 +1055,14 @@ PHP_FUNCTION(mysqli_kill)

MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);

if (mysql_kill(mysql->mysql, processid)) {
char query[64];
snprintf(query, sizeof(query), "KILL CONNECTION " ZEND_LONG_FMT, processid);

if (mysql_real_query(mysql->mysql, query, strlen(query))) {
// 1317 is ER_QUERY_INTERRUPTED from server's side
if (mysql_errno(mysql->mysql) == 1317) {
RETURN_TRUE;
}
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
RETURN_FALSE;
}
Expand Down Expand Up @@ -1981,14 +1988,36 @@ PHP_FUNCTION(mysqli_store_result)
PHP_FUNCTION(mysqli_thread_id)
{
MY_MYSQL *mysql;
MYSQL_RES *result;
MYSQL_ROW row;
zval *mysql_link;
zend_long processid;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);

RETURN_LONG((zend_long) mysql_thread_id(mysql->mysql));
static const char *query = "SELECT CONNECTION_ID()";
size_t query_len = strlen(query);

if (mysql_real_query(mysql->mysql, query, query_len)) {
RETURN_LONG((zend_long)mysql_thread_id(mysql->mysql));
}

result = mysql_store_result(mysql->mysql);
if (!result) {
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
RETURN_THROWS();
}

row = mysql_fetch_row(result);
processid = (zend_long)strtoll(row[0], NULL, 10);

efree(row);
mysql_free_result(result);

RETURN_LONG(processid);
}
/* }}} */

Expand Down
12 changes: 6 additions & 6 deletions ext/mysqli/tests/mysqli_kill.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require_once 'skipifconnectfailure.inc';
printf("[005] Expecting boolean/any, got %s/%s\n", gettype($tmp), $tmp);

if ($res = mysqli_query($link, "SELECT id FROM test LIMIT 1"))
pintf("[006] Expecting boolean/false, got %s/%s\n", gettype($res), $res);
printf("[006] Expecting boolean/false, got %s/%s\n", gettype($res), $res);

var_dump($error = mysqli_error($link));
if (!is_string($error) || ('' === $error))
Expand Down Expand Up @@ -103,25 +103,25 @@ object(mysqli)#%d (%d) {
}
}
["field_count"]=>
int(0)
int(1)
["host_info"]=>
string(%d) "%s"
["info"]=>
%s
NULL
["insert_id"]=>
int(0)
["server_info"]=>
string(%d) "%s"
["server_version"]=>
int(%d)
["sqlstate"]=>
string(5) "HY000"
string(%d) "%s"
["protocol_version"]=>
int(10)
int(%d)
["thread_id"]=>
int(%d)
["warning_count"]=>
int(0)
int(%d)
}
mysqli_kill(): Argument #2 ($process_id) must be greater than 0
array(1) {
Expand Down