PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/bad/ext/mysql/tests/mysql_pconn_max_links.php

http://github.com/facebook/hiphop-php
PHP | 127 lines | 95 code | 25 blank | 7 comment | 23 complexity | eaaaee943e63eee8ed2c2d19fd786560 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. require_once("connect.inc");
  3. require_once('table.inc');
  4. if ($socket)
  5. $host = sprintf("%s:%s", $host, $socket);
  6. else if ($port)
  7. $host = sprintf("%s:%s", $host, $port);
  8. if (!$plink = mysql_pconnect($host, $user, $passwd))
  9. printf("[001] Cannot connect using the second DB user created during SKIPIF, [%d] %s\n",
  10. mysql_errno(), mysql_error());
  11. if (!mysql_select_db($db, $plink))
  12. printf("[002] [%d] %s\n", mysql_errno($plink), mysql_error($plink));
  13. ob_start();
  14. phpinfo();
  15. $phpinfo = strip_tags(ob_get_contents());
  16. ob_end_clean();
  17. $phpinfo = substr($phpinfo, strpos($phpinfo, 'MySQL Support => enabled'), 500);
  18. if (!preg_match('@Active Persistent Links\s+=>\s+(\d+)@ismU', $phpinfo, $matches))
  19. printf("[003] Cannot get # active persistent links from phpinfo()");
  20. $num_plinks = $matches[1];
  21. if (!$res = mysql_query('SELECT id, label FROM test WHERE id = 1', $plink))
  22. printf("[004] Cannot run query on persistent connection of second DB user, [%d] %s\n",
  23. mysql_errno($plink), mysql_error($plink));
  24. if (!$row = mysql_fetch_assoc($res))
  25. printf("[005] Cannot run fetch result, [%d] %s\n",
  26. mysql_errno($plink), mysql_error($plink));
  27. mysql_free_result($res);
  28. var_dump($row);
  29. // change the password for the second DB user and kill the persistent connection
  30. if (!$res = mysql_query("SELECT CURRENT_USER() AS _user", $link))
  31. printf("[006] [%d] %s", mysql_errno($link), mysql_error($link));
  32. $row = mysql_fetch_assoc($res);
  33. mysql_free_result($res);
  34. $host = substr($row['_user'], strrpos($row['_user'], "@") + 1, strlen($row['_user']));
  35. $sql = sprintf('SET PASSWORD FOR pcontest@"%s" = PASSWORD("newpass")', mysql_real_escape_string($host, $link));
  36. if (!mysql_query($sql, $link))
  37. printf("[007] Cannot change PW of second DB user, [%d] %s\n", mysql_errno($link), mysql_error($link));
  38. // persistent connections cannot be closed but only be killed
  39. $pthread_id = mysql_thread_id($plink);
  40. if (!mysql_query(sprintf('KILL %d', $pthread_id), $link))
  41. printf("[008] Cannot KILL persistent connection of second DB user, [%d] %s\n", mysql_errno($link), mysql_error($link));
  42. // give the server a second to really kill the thread
  43. sleep(1);
  44. if (!$res = mysql_query("SHOW FULL PROCESSLIST", $link))
  45. printf("[009] [%d] %s\n", mysql_errno($link), mysql_error($link));
  46. $running_threads = array();
  47. while ($row = mysql_fetch_assoc($res))
  48. $running_threads[$row['Id']] = $row;
  49. mysql_free_result($res);
  50. if (isset($running_threads[$pthread_id]))
  51. printf("[010] Persistent connection has not been killed\n");
  52. // we might get the old handle
  53. if ($plink = @mysql_pconnect($host, 'pcontest', 'pcontest'))
  54. printf("[011] Can connect using the old password, [%d] %s\n",
  55. mysql_errno(), mysql_error());
  56. ob_start();
  57. phpinfo();
  58. $phpinfo = strip_tags(ob_get_contents());
  59. ob_end_clean();
  60. $phpinfo = substr($phpinfo, strpos($phpinfo, 'MySQL Support => enabled'), 500);
  61. if (!preg_match('@Active Persistent Links\s+=>\s+(\d+)@ismU', $phpinfo, $matches))
  62. printf("[012] Cannot get # active persistent links from phpinfo()\n");
  63. $num_plinks_kill = $matches[1];
  64. if ($num_plinks_kill > $num_plinks)
  65. printf("[013] Statistics seems to be wrong, got %d active persistent links, expecting < %d links\n",
  66. $num_plinks_kill, $num_plinks);
  67. // The first connection has been closed, the last pconnect() was unable to connect -> no connection open
  68. // We must be able to connect because max_persistent limit has not been reached
  69. if (!$plink = mysql_pconnect($host, 'pcontest', 'newpass'))
  70. die(sprintf("[014] Cannot connect using the second DB, [%d] %s\n",
  71. mysql_errno(), mysql_error()));
  72. if (!mysql_select_db($db, $plink))
  73. printf("[015] [%d] %s\n", mysql_errno($plink), mysql_error($plink));
  74. if (!$res = mysql_query('SELECT id, label FROM test WHERE id = 1', $plink))
  75. printf("[016] Cannot run query on persistent connection of second DB user, [%d] %s\n",
  76. mysql_errno($plink), mysql_error($plink));
  77. if (!$row = mysql_fetch_assoc($res))
  78. printf("[017] Cannot run fetch result, [%d] %s\n",
  79. mysql_errno($plink), mysql_error($plink));
  80. mysql_free_result($res);
  81. var_dump($row);
  82. mysql_query(sprintf('REVOKE ALL PRIVILEGES, GRANT OPTION FROM pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  83. mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  84. mysql_close($link);
  85. print "done!";
  86. ?>
  87. <?php
  88. // connect + select_db
  89. require_once("connect.inc");
  90. if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) {
  91. printf("[c001] Cannot connect to the server using host=%s/%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  92. $host, $myhost, $user, $db, $port, $socket);
  93. }
  94. if (!$res = mysql_query("SELECT CURRENT_USER() AS _user", $link))
  95. printf("[c002] [%d] %s", mysql_errno($link), mysql_error($link));
  96. $row = mysql_fetch_assoc($res);
  97. mysql_free_result($res);
  98. $host = substr($row['_user'], strrpos($row['_user'], "@") + 1, strlen($row['_user']));
  99. @mysql_query(sprintf('REVOKE ALL PRIVILEGES, GRANT OPTION FROM pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  100. @mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  101. mysql_close($link);
  102. ?>