PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/fpm/fpm_php.c

http://github.com/dreamcat4/php-fpm
C | 189 lines | 151 code | 36 blank | 2 comment | 24 complexity | 8f04473c5f100d24dae4d776be5b416f MD5 | raw file
  1. /* $Id: fpm_php.c,v 1.22.2.4 2008/12/13 03:21:18 anight Exp $ */
  2. /* (c) 2007,2008 Andrei Nigmatulin */
  3. #include "fpm_config.h"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include "php.h"
  8. #include "php_main.h"
  9. #include "php_ini.h"
  10. #include "ext/standard/dl.h"
  11. #include "cgi/fastcgi.h"
  12. #include "fpm.h"
  13. #include "fpm_php.h"
  14. #include "fpm_cleanup.h"
  15. #include "fpm_worker_pool.h"
  16. static int zend_ini_alter_master(char *name, int name_length, char *new_value, int new_value_length, int stage TSRMLS_DC)
  17. {
  18. zend_ini_entry *ini_entry;
  19. char *duplicate;
  20. if (zend_hash_find(EG(ini_directives), name, name_length, (void **) &ini_entry) == FAILURE) {
  21. return FAILURE;
  22. }
  23. duplicate = strdup(new_value);
  24. if (!ini_entry->on_modify
  25. || ini_entry->on_modify(ini_entry, duplicate, new_value_length,
  26. ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage TSRMLS_CC) == SUCCESS) {
  27. ini_entry->value = duplicate;
  28. ini_entry->value_length = new_value_length;
  29. } else {
  30. free(duplicate);
  31. }
  32. return SUCCESS;
  33. }
  34. static void fpm_php_disable(char *value, int (*zend_disable)(char *, uint TSRMLS_DC) TSRMLS_DC)
  35. {
  36. char *s = 0, *e = value;
  37. while (*e) {
  38. switch (*e) {
  39. case ' ':
  40. case ',':
  41. if (s) {
  42. *e = '\0';
  43. zend_disable(s, e - s TSRMLS_CC);
  44. s = 0;
  45. }
  46. break;
  47. default:
  48. if (!s) {
  49. s = e;
  50. }
  51. break;
  52. }
  53. e++;
  54. }
  55. if (s) {
  56. zend_disable(s, e - s TSRMLS_CC);
  57. }
  58. }
  59. static int fpm_php_apply_defines(struct fpm_worker_pool_s *wp)
  60. {
  61. TSRMLS_FETCH();
  62. struct key_value_s *kv;
  63. for (kv = wp->config->php_defines; kv; kv = kv->next) {
  64. char *name = kv->key;
  65. char *value = kv->value;
  66. int name_len = strlen(name);
  67. int value_len = strlen(value);
  68. if (!strcmp(name, "extension") && *value) {
  69. zval zv;
  70. #if defined(PHP_VERSION_ID) && (PHP_VERSION_ID >= 50300)
  71. php_dl(value, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
  72. #else
  73. zval filename;
  74. ZVAL_STRINGL(&filename, value, value_len, 0);
  75. #if (PHP_MAJOR_VERSION >= 5)
  76. php_dl(&filename, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
  77. #else
  78. php_dl(&filename, MODULE_PERSISTENT, &zv TSRMLS_CC);
  79. #endif
  80. #endif
  81. continue;
  82. }
  83. zend_ini_alter_master(name, name_len + 1, value, value_len, PHP_INI_STAGE_ACTIVATE TSRMLS_CC);
  84. if (!strcmp(name, "disable_functions") && *value) {
  85. char *v = strdup(value);
  86. #if (PHP_MAJOR_VERSION >= 5)
  87. PG(disable_functions) = v;
  88. #endif
  89. fpm_php_disable(v, zend_disable_function TSRMLS_CC);
  90. }
  91. else if (!strcmp(name, "disable_classes") && *value) {
  92. char *v = strdup(value);
  93. #if (PHP_MAJOR_VERSION >= 5)
  94. PG(disable_classes) = v;
  95. #endif
  96. fpm_php_disable(v, zend_disable_class TSRMLS_CC);
  97. }
  98. }
  99. return 0;
  100. }
  101. static int fpm_php_set_allowed_clients(struct fpm_worker_pool_s *wp)
  102. {
  103. if (wp->listen_address_domain == FPM_AF_INET) {
  104. fcgi_set_allowed_clients(wp->config->allowed_clients);
  105. }
  106. return 0;
  107. }
  108. static int fpm_php_set_fcgi_mgmt_vars(struct fpm_worker_pool_s *wp)
  109. {
  110. char max_workers[10 + 1]; /* 4294967295 */
  111. int len;
  112. len = sprintf(max_workers, "%u", (unsigned int) wp->config->pm->max_children);
  113. fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, max_workers, len);
  114. fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, max_workers, len);
  115. return 0;
  116. }
  117. char *fpm_php_script_filename(TSRMLS_D)
  118. {
  119. return SG(request_info).path_translated;
  120. }
  121. char *fpm_php_request_method(TSRMLS_D)
  122. {
  123. return (char *) SG(request_info).request_method;
  124. }
  125. size_t fpm_php_content_length(TSRMLS_D)
  126. {
  127. return SG(request_info).content_length;
  128. }
  129. static void fpm_php_cleanup(int which, void *arg)
  130. {
  131. TSRMLS_FETCH();
  132. php_module_shutdown(TSRMLS_C);
  133. sapi_shutdown();
  134. }
  135. void fpm_php_soft_quit()
  136. {
  137. fcgi_set_in_shutdown(1);
  138. }
  139. int fpm_php_init_main()
  140. {
  141. if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_php_cleanup, 0)) {
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. int fpm_php_init_child(struct fpm_worker_pool_s *wp)
  147. {
  148. if (0 > fpm_php_apply_defines(wp) ||
  149. 0 > fpm_php_set_allowed_clients(wp)) {
  150. return -1;
  151. }
  152. return 0;
  153. }