forked from php-memcached-dev/php-memcached
-
Notifications
You must be signed in to change notification settings - Fork 1
/
php_memcached_session.c
471 lines (411 loc) · 14.4 KB
/
php_memcached_session.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
+----------------------------------------------------------------------+
| Copyright (c) 2009-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andrei Zmievski <[email protected]> |
+----------------------------------------------------------------------+
*/
#include <stdlib.h>
#include <string.h>
#include <php.h>
#include <php_main.h>
#ifdef ZTS
#include "TSRM.h"
#endif
#include <php_ini.h>
#include <SAPI.h>
#include <ext/standard/info.h>
#include <zend_extensions.h>
#include <zend_exceptions.h>
#include <ext/standard/php_smart_str.h>
#include <ext/standard/php_var.h>
#include "php_memcached.h"
#include "php_memcached_session.h"
#include "fastlz/fastlz.h"
#include <zlib.h>
extern ZEND_DECLARE_MODULE_GLOBALS(php_memcached)
#define MEMC_SESS_DEFAULT_LOCK_WAIT 150000
#define MEMC_SESS_LOCK_EXPIRATION 30
ps_module ps_mod_memcached = {
PS_MOD(memcached)
};
static int php_memc_sess_lock(memcached_st *memc, const char *key TSRMLS_DC)
{
char *lock_key = NULL;
int lock_key_len = 0;
unsigned long attempts;
long write_retry_attempts = 0;
long lock_maxwait;
long lock_wait = MEMC_G(sess_lock_wait);
time_t expiration;
memcached_return status;
/* set max timeout for session_start = max_execution_time. (c) Andrei Darashenka, Richter & Poweleit GmbH */
lock_maxwait = zend_ini_long(ZEND_STRS("max_execution_time"), 0);
if (lock_maxwait <= 0) {
lock_maxwait = MEMC_SESS_LOCK_EXPIRATION;
}
if (lock_wait == 0) {
lock_wait = MEMC_SESS_DEFAULT_LOCK_WAIT;
}
expiration = time(NULL) + lock_maxwait + 1;
attempts = (unsigned long)((1000000.0 / lock_wait) * lock_maxwait);
/* Set the number of write retry attempts to the number of replicas times the number of attempts to remove a server */
if (MEMC_G(sess_remove_failed_enabled)) {
write_retry_attempts = MEMC_G(sess_number_of_replicas) * ( memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT) + 1);
}
lock_key_len = spprintf(&lock_key, 0, "lock.%s", key);
do {
status = memcached_add(memc, lock_key, lock_key_len, "1", sizeof("1")-1, expiration, 0);
if (status == MEMCACHED_SUCCESS) {
MEMC_G(sess_locked) = 1;
MEMC_G(sess_lock_key) = lock_key;
MEMC_G(sess_lock_key_len) = lock_key_len;
return 0;
} else if (status != MEMCACHED_NOTSTORED && status != MEMCACHED_DATA_EXISTS) {
if (write_retry_attempts > 0) {
write_retry_attempts--;
continue;
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Write of lock failed");
break;
}
if (lock_wait > 0) {
usleep(lock_wait);
}
} while(--attempts > 0);
efree(lock_key);
return -1;
}
static void php_memc_sess_unlock(memcached_st *memc TSRMLS_DC)
{
if (MEMC_G(sess_locked)) {
memcached_delete(memc, MEMC_G(sess_lock_key), MEMC_G(sess_lock_key_len), 0);
MEMC_G(sess_locked) = 0;
efree(MEMC_G(sess_lock_key));
MEMC_G(sess_lock_key_len) = 0;
}
}
PS_OPEN_FUNC(memcached)
{
memcached_sess *memc_sess = PS_GET_MOD_DATA();
memcached_return status;
char *p, *plist_key = NULL;
int plist_key_len;
if (!strncmp((char *)save_path, "PERSISTENT=", sizeof("PERSISTENT=") - 1)) {
zend_rsrc_list_entry *le = NULL;
char *e;
p = (char *)save_path + sizeof("PERSISTENT=") - 1;
if (!*p) {
error:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid persistent id for session storage");
return FAILURE;
}
if ((e = strchr(p, ' '))) {
plist_key_len = spprintf(&plist_key, 0, "memcached_sessions:id=%.*s", (int)(e - p), p);
} else {
goto error;
}
plist_key_len++;
if (zend_hash_find(&EG(persistent_list), plist_key, plist_key_len, (void *)&le) == SUCCESS) {
if (le->type == php_memc_sess_list_entry()) {
memc_sess = (memcached_sess *) le->ptr;
PS_SET_MOD_DATA(memc_sess);
if (plist_key) {
efree(plist_key);
}
return SUCCESS;
}
}
p = e + 1;
memc_sess = pecalloc(sizeof(*memc_sess), 1, 1);
memc_sess->is_persisent = 1;
} else {
p = (char *)save_path;
memc_sess = ecalloc(sizeof(*memc_sess), 1);
memc_sess->is_persisent = 0;
}
if (!strstr(p, "--SERVER") && !strstr(p, "--SOCKET")) {
memcached_server_st *servers = memcached_servers_parse(p);
if (servers) {
memc_sess->memc_sess = memcached_create(NULL);
if (memc_sess->memc_sess) {
if (MEMC_G(sess_consistent_hash_enabled)) {
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, (uint64_t) 1) == MEMCACHED_FAILURE) {
PS_SET_MOD_DATA(NULL);
if (plist_key) {
efree(plist_key);
}
memcached_free(memc_sess->memc_sess);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to enable memcached consistent hashing");
return FAILURE;
}
}
status = memcached_server_push(memc_sess->memc_sess, servers);
memcached_server_list_free(servers);
if (MEMC_G(sess_prefix) && MEMC_G(sess_prefix)[0] != 0 && memcached_callback_set(memc_sess->memc_sess, MEMCACHED_CALLBACK_PREFIX_KEY, MEMC_G(sess_prefix)) != MEMCACHED_SUCCESS) {
PS_SET_MOD_DATA(NULL);
if (plist_key) {
efree(plist_key);
}
memcached_free(memc_sess->memc_sess);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "bad memcached key prefix in memcached.sess_prefix");
return FAILURE;
}
if (status == MEMCACHED_SUCCESS) {
goto success;
}
} else {
memcached_server_list_free(servers);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not allocate libmemcached structure");
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to parse session.save_path");
}
} else {
memc_sess->memc_sess = memcached(p, strlen(p));
if (!memc_sess->memc_sess) {
char error_buffer[1024];
if (libmemcached_check_configuration(p, strlen(p), error_buffer, sizeof(error_buffer)) != MEMCACHED_SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "session.save_path configuration error %s", error_buffer);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to initialize memcached session storage");
}
} else {
success:
PS_SET_MOD_DATA(memc_sess);
if (plist_key) {
zend_rsrc_list_entry le;
le.type = php_memc_sess_list_entry();
le.ptr = memc_sess;
if (zend_hash_update(&EG(persistent_list), (char *)plist_key, plist_key_len, (void *)&le, sizeof(le), NULL) == FAILURE) {
efree(plist_key);
php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not register persistent entry");
}
efree(plist_key);
}
if (MEMC_G(sess_binary_enabled)) {
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) 1) == MEMCACHED_FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to set memcached session binary protocol");
return FAILURE;
}
}
if (MEMC_G(sess_number_of_replicas) > 0) {
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, (uint64_t) MEMC_G(sess_number_of_replicas)) == MEMCACHED_FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to set memcached session number of replicas");
return FAILURE;
}
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, (uint64_t) MEMC_G(sess_randomize_replica_read)) == MEMCACHED_FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to set memcached session randomize replica read");
}
}
if (MEMC_G(sess_consistent_hashing_enabled)) {
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_KETAMA, (uint64_t) 1) == MEMCACHED_FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to set memcached consistent hashing");
return FAILURE;
}
}
/* Allow libmemcached remove failed servers */
if (MEMC_G(sess_remove_failed_enabled)) {
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS, (uint64_t) 1) == MEMCACHED_FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to set: remove failed servers");
return FAILURE;
}
}
return SUCCESS;
}
}
if (plist_key) {
efree(plist_key);
}
PS_SET_MOD_DATA(NULL);
return FAILURE;
}
PS_CLOSE_FUNC(memcached)
{
memcached_sess *memc_sess = PS_GET_MOD_DATA();
if (MEMC_G(sess_locking_enabled)) {
php_memc_sess_unlock(memc_sess->memc_sess TSRMLS_CC);
}
if (memc_sess->memc_sess) {
if (!memc_sess->is_persisent) {
memcached_free(memc_sess->memc_sess);
efree(memc_sess);
}
PS_SET_MOD_DATA(NULL);
}
return SUCCESS;
}
#define MEMC_FAILOVER_RETRY \
if (!server_count) { \
server_count = memcached_server_count(memc_sess->memc_sess); \
} \
if (server_count > 1 && retry < server_count) { \
switch (status) { \
case MEMCACHED_HOST_LOOKUP_FAILURE: \
case MEMCACHED_CONNECTION_FAILURE: \
case MEMCACHED_CONNECTION_BIND_FAILURE: \
case MEMCACHED_WRITE_FAILURE: \
case MEMCACHED_READ_FAILURE: \
case MEMCACHED_UNKNOWN_READ_FAILURE: \
case MEMCACHED_PROTOCOL_ERROR: \
case MEMCACHED_SERVER_ERROR: \
case MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE: \
case MEMCACHED_TIMEOUT: \
case MEMCACHED_FAIL_UNIX_SOCKET: \
case MEMCACHED_SERVER_MARKED_DEAD: \
retry++; \
continue; \
default: \
break; \
} \
} \
PS_READ_FUNC(memcached)
{
char *payload = NULL;
size_t payload_len = 0;
int key_len = strlen(key);
uint32_t flags = 0;
memcached_return status;
memcached_sess *memc_sess = PS_GET_MOD_DATA();
size_t key_length;
int server_count = 0, retry = 0;
key_length = strlen(MEMC_G(sess_prefix)) + key_len + 5; // prefix + "lock."
if (!key_length || key_length >= MEMCACHED_MAX_KEY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The session id is too long or contains illegal characters");
PS(invalid_session_id) = 1;
return FAILURE;
}
if (MEMC_G(sess_locking_enabled)) {
if (php_memc_sess_lock(memc_sess->memc_sess, key TSRMLS_CC) < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to clear session lock record");
return FAILURE;
}
}
while (1) {
payload = memcached_get(memc_sess->memc_sess, key, key_len, &payload_len, &flags, &status);
if (status == MEMCACHED_SUCCESS) {
char *p = payload;
if (flags & MEMC_VAL_COMPRESSION_FASTLZ || flags & MEMC_VAL_COMPRESSION_ZLIB) {
uint32_t len;
zend_bool decompress_status;
char *dval;
size_t dval_len;
memcpy(&len, payload, sizeof(uint32_t));
dval = emalloc(len + 1);
payload_len -= sizeof(uint32_t);
payload += sizeof(uint32_t);
if (flags & MEMC_VAL_COMPRESSION_FASTLZ) {
decompress_status = ((dval_len = fastlz_decompress(payload, payload_len, dval, len)) > 0);
dval[dval_len] = '\0';
} else if (flags & MEMC_VAL_COMPRESSION_ZLIB) {
decompress_status = (uncompress((Bytef *)dval, len, (Bytef *)payload, payload_len) == Z_OK);
}
if (!decompress_status) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not decompress value");
free(p);
efree(dval);
return FAILURE;
}
*val = dval;
*vallen = dval_len;
} else {
*val = estrndup(payload, payload_len);
*vallen = payload_len;
}
free(p);
return SUCCESS;
}
MEMC_FAILOVER_RETRY
return FAILURE;
}
}
PS_WRITE_FUNC(memcached)
{
int key_len = strlen(key);
time_t expiration = 0;
long write_try_attempts = 1;
memcached_return status;
memcached_sess *memc_sess = PS_GET_MOD_DATA();
size_t key_length;
int server_count = 0, retry = 0;
uint32_t flags = 0;
size_t final_val_len;
char *final_val;
key_length = strlen(MEMC_G(sess_prefix)) + key_len + 5; // prefix + "lock."
if (!key_length || key_length >= MEMCACHED_MAX_KEY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The session id is too long or contains illegal characters");
PS(invalid_session_id) = 1;
return FAILURE;
}
if (PS(gc_maxlifetime) > 0) {
expiration = PS(gc_maxlifetime);
}
if (vallen > MEMC_G(compression_threshold) && MEMC_G(compression_type_real)) {
zend_bool compress_status = 0;
uint32_t len = vallen;
unsigned long payload_comp_len = (unsigned long)((vallen * 1.05) + 1);
char *payload_comp = emalloc(payload_comp_len + sizeof(uint32_t));
char *payload = payload_comp;
memcpy(payload_comp, &len, sizeof(uint32_t));
payload_comp += sizeof(uint32_t);
if (MEMC_G(compression_type_real) == COMPRESSION_TYPE_FASTLZ) {
compress_status = ((payload_comp_len = fastlz_compress(val, vallen, payload_comp)) > 0);
flags |= MEMC_VAL_COMPRESSION_FASTLZ;
} else if (MEMC_G(compression_type_real) == COMPRESSION_TYPE_ZLIB) {
compress_status = (compress((Bytef *)payload_comp, &payload_comp_len, (Bytef *)val, vallen) == Z_OK);
flags |= MEMC_VAL_COMPRESSION_ZLIB;
}
if (!compress_status) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not compress value");
efree(payload);
return FAILURE;
}
final_val = payload;
final_val_len = payload_comp_len + sizeof(uint32_t);
final_val[final_val_len] = '\0';
} else {
final_val_len = vallen;
final_val = val;
}
/* Set the number of write retry attempts to the number of replicas times the number of attempts to remove a server plus the initial write */
if (MEMC_G(sess_remove_failed_enabled)) {
write_try_attempts = 1 + MEMC_G(sess_number_of_replicas) * ( memcached_behavior_get(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT) + 1);
}
do {
status = memcached_set(memc_sess->memc_sess, key, key_len, final_val, final_val_len, expiration, flags);
if (status == MEMCACHED_SUCCESS) {
if (val != final_val) {
efree(final_val);
}
return SUCCESS;
} else {
write_try_attempts--;
}
} while (write_try_attempts > 0);
if (val != final_val) {
efree(final_val);
}
return FAILURE;
}
PS_DESTROY_FUNC(memcached)
{
memcached_sess *memc_sess = PS_GET_MOD_DATA();
memcached_delete(memc_sess->memc_sess, key, strlen(key), 0);
if (MEMC_G(sess_locking_enabled)) {
php_memc_sess_unlock(memc_sess->memc_sess TSRMLS_CC);
}
return SUCCESS;
}
PS_GC_FUNC(memcached)
{
return SUCCESS;
}
/* }}} */