mod_mm.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #ifdef HAVE_LIBMM
  20. #include <unistd.h>
  21. #include <mm.h>
  22. #include <time.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <fcntl.h>
  26. #include "php_stdint.h"
  27. #include "php_session.h"
  28. #include "mod_mm.h"
  29. #include "SAPI.h"
  30. #ifdef ZTS
  31. # error mm is not thread-safe
  32. #endif
  33. #define PS_MM_FILE "session_mm_"
  34. /* This list holds all data associated with one session. */
  35. typedef struct ps_sd {
  36. struct ps_sd *next;
  37. uint32_t hv; /* hash value of key */
  38. time_t ctime; /* time of last change */
  39. void *data;
  40. size_t datalen; /* amount of valid data */
  41. size_t alloclen; /* amount of allocated memory for data */
  42. char key[1]; /* inline key */
  43. } ps_sd;
  44. typedef struct {
  45. MM *mm;
  46. ps_sd **hash;
  47. uint32_t hash_max;
  48. uint32_t hash_cnt;
  49. pid_t owner;
  50. } ps_mm;
  51. static ps_mm *ps_mm_instance = NULL;
  52. #if 0
  53. # define ps_mm_debug(a) printf a
  54. #else
  55. # define ps_mm_debug(a)
  56. #endif
  57. static inline uint32_t ps_sd_hash(const char *data, int len)
  58. {
  59. uint32_t h;
  60. const char *e = data + len;
  61. for (h = 2166136261U; data < e; ) {
  62. h *= 16777619;
  63. h ^= *data++;
  64. }
  65. return h;
  66. }
  67. static void hash_split(ps_mm *data)
  68. {
  69. uint32_t nmax;
  70. ps_sd **nhash;
  71. ps_sd **ohash, **ehash;
  72. ps_sd *ps, *next;
  73. nmax = ((data->hash_max + 1) << 1) - 1;
  74. nhash = mm_calloc(data->mm, nmax + 1, sizeof(*data->hash));
  75. if (!nhash) {
  76. /* no further memory to expand hash table */
  77. return;
  78. }
  79. ehash = data->hash + data->hash_max + 1;
  80. for (ohash = data->hash; ohash < ehash; ohash++) {
  81. for (ps = *ohash; ps; ps = next) {
  82. next = ps->next;
  83. ps->next = nhash[ps->hv & nmax];
  84. nhash[ps->hv & nmax] = ps;
  85. }
  86. }
  87. mm_free(data->mm, data->hash);
  88. data->hash = nhash;
  89. data->hash_max = nmax;
  90. }
  91. static ps_sd *ps_sd_new(ps_mm *data, const char *key)
  92. {
  93. uint32_t hv, slot;
  94. ps_sd *sd;
  95. int keylen;
  96. keylen = strlen(key);
  97. sd = mm_malloc(data->mm, sizeof(ps_sd) + keylen);
  98. if (!sd) {
  99. php_error_docref(NULL, E_WARNING, "mm_malloc failed, avail %ld, err %s", mm_available(data->mm), mm_error());
  100. return NULL;
  101. }
  102. hv = ps_sd_hash(key, keylen);
  103. slot = hv & data->hash_max;
  104. sd->ctime = 0;
  105. sd->hv = hv;
  106. sd->data = NULL;
  107. sd->alloclen = sd->datalen = 0;
  108. memcpy(sd->key, key, keylen + 1);
  109. sd->next = data->hash[slot];
  110. data->hash[slot] = sd;
  111. data->hash_cnt++;
  112. if (!sd->next) {
  113. if (data->hash_cnt >= data->hash_max) {
  114. hash_split(data);
  115. }
  116. }
  117. ps_mm_debug(("inserting %s(%p) into slot %d\n", key, sd, slot));
  118. return sd;
  119. }
  120. static void ps_sd_destroy(ps_mm *data, ps_sd *sd)
  121. {
  122. uint32_t slot;
  123. slot = ps_sd_hash(sd->key, strlen(sd->key)) & data->hash_max;
  124. if (data->hash[slot] == sd) {
  125. data->hash[slot] = sd->next;
  126. } else {
  127. ps_sd *prev;
  128. /* There must be some entry before the one we want to delete */
  129. for (prev = data->hash[slot]; prev->next != sd; prev = prev->next);
  130. prev->next = sd->next;
  131. }
  132. data->hash_cnt--;
  133. if (sd->data) {
  134. mm_free(data->mm, sd->data);
  135. }
  136. mm_free(data->mm, sd);
  137. }
  138. static ps_sd *ps_sd_lookup(ps_mm *data, const char *key, int rw)
  139. {
  140. uint32_t hv, slot;
  141. ps_sd *ret, *prev;
  142. hv = ps_sd_hash(key, strlen(key));
  143. slot = hv & data->hash_max;
  144. for (prev = NULL, ret = data->hash[slot]; ret; prev = ret, ret = ret->next) {
  145. if (ret->hv == hv && !strcmp(ret->key, key)) {
  146. break;
  147. }
  148. }
  149. if (ret && rw && ret != data->hash[slot]) {
  150. /* Move the entry to the top of the linked list */
  151. if (prev) {
  152. prev->next = ret->next;
  153. }
  154. ret->next = data->hash[slot];
  155. data->hash[slot] = ret;
  156. }
  157. ps_mm_debug(("lookup(%s): ret=%p,hv=%u,slot=%d\n", key, ret, hv, slot));
  158. return ret;
  159. }
  160. static int ps_mm_key_exists(ps_mm *data, const char *key)
  161. {
  162. ps_sd *sd;
  163. if (!key) {
  164. return FAILURE;
  165. }
  166. sd = ps_sd_lookup(data, key, 0);
  167. if (sd) {
  168. return SUCCESS;
  169. }
  170. return FAILURE;
  171. }
  172. const ps_module ps_mod_mm = {
  173. PS_MOD_SID(mm)
  174. };
  175. #define PS_MM_DATA ps_mm *data = PS_GET_MOD_DATA()
  176. static int ps_mm_initialize(ps_mm *data, const char *path)
  177. {
  178. data->owner = getpid();
  179. data->mm = mm_create(0, path);
  180. if (!data->mm) {
  181. return FAILURE;
  182. }
  183. data->hash_cnt = 0;
  184. data->hash_max = 511;
  185. data->hash = mm_calloc(data->mm, data->hash_max + 1, sizeof(ps_sd *));
  186. if (!data->hash) {
  187. mm_destroy(data->mm);
  188. return FAILURE;
  189. }
  190. return SUCCESS;
  191. }
  192. static void ps_mm_destroy(ps_mm *data)
  193. {
  194. int h;
  195. ps_sd *sd, *next;
  196. /* This function is called during each module shutdown,
  197. but we must not release the shared memory pool, when
  198. an Apache child dies! */
  199. if (data->owner != getpid()) {
  200. return;
  201. }
  202. for (h = 0; h < data->hash_max + 1; h++) {
  203. for (sd = data->hash[h]; sd; sd = next) {
  204. next = sd->next;
  205. ps_sd_destroy(data, sd);
  206. }
  207. }
  208. mm_free(data->mm, data->hash);
  209. mm_destroy(data->mm);
  210. free(data);
  211. }
  212. PHP_MINIT_FUNCTION(ps_mm)
  213. {
  214. int save_path_len = strlen(PS(save_path));
  215. int mod_name_len = strlen(sapi_module.name);
  216. int euid_len;
  217. char *ps_mm_path, euid[30];
  218. int ret;
  219. ps_mm_instance = calloc(sizeof(*ps_mm_instance), 1);
  220. if (!ps_mm_instance) {
  221. return FAILURE;
  222. }
  223. if (!(euid_len = slprintf(euid, sizeof(euid), "%d", geteuid()))) {
  224. free(ps_mm_instance);
  225. ps_mm_instance = NULL;
  226. return FAILURE;
  227. }
  228. /* Directory + '/' + File + Module Name + Effective UID + \0 */
  229. ps_mm_path = emalloc(save_path_len + 1 + (sizeof(PS_MM_FILE) - 1) + mod_name_len + euid_len + 1);
  230. memcpy(ps_mm_path, PS(save_path), save_path_len);
  231. if (save_path_len && PS(save_path)[save_path_len - 1] != DEFAULT_SLASH) {
  232. ps_mm_path[save_path_len] = DEFAULT_SLASH;
  233. save_path_len++;
  234. }
  235. memcpy(ps_mm_path + save_path_len, PS_MM_FILE, sizeof(PS_MM_FILE) - 1);
  236. save_path_len += sizeof(PS_MM_FILE) - 1;
  237. memcpy(ps_mm_path + save_path_len, sapi_module.name, mod_name_len);
  238. save_path_len += mod_name_len;
  239. memcpy(ps_mm_path + save_path_len, euid, euid_len);
  240. ps_mm_path[save_path_len + euid_len] = '\0';
  241. ret = ps_mm_initialize(ps_mm_instance, ps_mm_path);
  242. efree(ps_mm_path);
  243. if (ret != SUCCESS) {
  244. free(ps_mm_instance);
  245. ps_mm_instance = NULL;
  246. return FAILURE;
  247. }
  248. php_session_register_module(&ps_mod_mm);
  249. return SUCCESS;
  250. }
  251. PHP_MSHUTDOWN_FUNCTION(ps_mm)
  252. {
  253. if (ps_mm_instance) {
  254. ps_mm_destroy(ps_mm_instance);
  255. return SUCCESS;
  256. }
  257. return FAILURE;
  258. }
  259. PS_OPEN_FUNC(mm)
  260. {
  261. ps_mm_debug(("open: ps_mm_instance=%p\n", ps_mm_instance));
  262. if (!ps_mm_instance) {
  263. return FAILURE;
  264. }
  265. PS_SET_MOD_DATA(ps_mm_instance);
  266. return SUCCESS;
  267. }
  268. PS_CLOSE_FUNC(mm)
  269. {
  270. PS_SET_MOD_DATA(NULL);
  271. return SUCCESS;
  272. }
  273. PS_READ_FUNC(mm)
  274. {
  275. PS_MM_DATA;
  276. ps_sd *sd;
  277. int ret = FAILURE;
  278. mm_lock(data->mm, MM_LOCK_RD);
  279. /* If there is an ID and strict mode, verify existence */
  280. if (PS(use_strict_mode)
  281. && ps_mm_key_exists(data, key->val) == FAILURE) {
  282. /* key points to PS(id), but cannot change here. */
  283. if (key) {
  284. efree(PS(id));
  285. PS(id) = NULL;
  286. }
  287. PS(id) = PS(mod)->s_create_sid((void **)&data);
  288. if (!PS(id)) {
  289. return FAILURE;
  290. }
  291. if (PS(use_cookies)) {
  292. PS(send_cookie) = 1;
  293. }
  294. php_session_reset_id();
  295. PS(session_status) = php_session_active;
  296. }
  297. sd = ps_sd_lookup(data, PS(id)->val, 0);
  298. if (sd) {
  299. *val = zend_string_init(sd->data, sd->datalen, 0);
  300. ret = SUCCESS;
  301. }
  302. mm_unlock(data->mm);
  303. return ret;
  304. }
  305. PS_WRITE_FUNC(mm)
  306. {
  307. PS_MM_DATA;
  308. ps_sd *sd;
  309. mm_lock(data->mm, MM_LOCK_RW);
  310. sd = ps_sd_lookup(data, key->val, 1);
  311. if (!sd) {
  312. sd = ps_sd_new(data, key->val);
  313. ps_mm_debug(("new entry for %s\n", key->val));
  314. }
  315. if (sd) {
  316. if (val->len >= sd->alloclen) {
  317. if (data->mm) {
  318. mm_free(data->mm, sd->data);
  319. }
  320. sd->alloclen = val->len + 1;
  321. sd->data = mm_malloc(data->mm, sd->alloclen);
  322. if (!sd->data) {
  323. ps_sd_destroy(data, sd);
  324. php_error_docref(NULL, E_WARNING, "cannot allocate new data segment");
  325. sd = NULL;
  326. }
  327. }
  328. if (sd) {
  329. sd->datalen = val->len;
  330. memcpy(sd->data, val->val, val->len);
  331. time(&sd->ctime);
  332. }
  333. }
  334. mm_unlock(data->mm);
  335. return sd ? SUCCESS : FAILURE;
  336. }
  337. PS_DESTROY_FUNC(mm)
  338. {
  339. PS_MM_DATA;
  340. ps_sd *sd;
  341. mm_lock(data->mm, MM_LOCK_RW);
  342. sd = ps_sd_lookup(data, key->val, 0);
  343. if (sd) {
  344. ps_sd_destroy(data, sd);
  345. }
  346. mm_unlock(data->mm);
  347. return SUCCESS;
  348. }
  349. PS_GC_FUNC(mm)
  350. {
  351. PS_MM_DATA;
  352. time_t limit;
  353. ps_sd **ohash, **ehash;
  354. ps_sd *sd, *next;
  355. *nrdels = 0;
  356. ps_mm_debug(("gc\n"));
  357. time(&limit);
  358. limit -= maxlifetime;
  359. mm_lock(data->mm, MM_LOCK_RW);
  360. ehash = data->hash + data->hash_max + 1;
  361. for (ohash = data->hash; ohash < ehash; ohash++) {
  362. for (sd = *ohash; sd; sd = next) {
  363. next = sd->next;
  364. if (sd->ctime < limit) {
  365. ps_mm_debug(("purging %s\n", sd->key));
  366. ps_sd_destroy(data, sd);
  367. (*nrdels)++;
  368. }
  369. }
  370. }
  371. mm_unlock(data->mm);
  372. return nrdels;
  373. }
  374. PS_CREATE_SID_FUNC(mm)
  375. {
  376. zend_string *sid;
  377. int maxfail = 3;
  378. PS_MM_DATA;
  379. do {
  380. sid = php_session_create_id((void **)&data);
  381. /* Check collision */
  382. if (ps_mm_key_exists(data, sid->val) == SUCCESS) {
  383. if (sid) {
  384. zend_string_release_ex(sid, 0);
  385. sid = NULL;
  386. }
  387. if (!(maxfail--)) {
  388. return NULL;
  389. }
  390. }
  391. } while(!sid);
  392. return sid;
  393. }
  394. #endif
  395. /*
  396. * Local variables:
  397. * tab-width: 4
  398. * c-basic-offset: 4
  399. * End:
  400. * vim600: sw=4 ts=4 fdm=marker
  401. * vim<600: sw=4 ts=4
  402. */