forked from memcached/memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_text.c
2752 lines (2454 loc) · 87.1 KB
/
proto_text.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Functions for handling the text related protocols, original and meta.
*/
#include "memcached.h"
#include "proto_text.h"
#include "authfile.h"
#include "storage.h"
#ifdef TLS
#include "tls.h"
#endif
#include <string.h>
#include <stdlib.h>
static void process_command(conn *c, char *command);
typedef struct token_s {
char *value;
size_t length;
} token_t;
/*
* we get here after reading the value in set/add/replace commands. The command
* has been stored in c->cmd, and the item is ready in c->item.
*/
void complete_nread_ascii(conn *c) {
assert(c != NULL);
item *it = c->item;
int comm = c->cmd;
enum store_item_type ret;
bool is_valid = false;
pthread_mutex_lock(&c->thread->stats.mutex);
c->thread->stats.slab_stats[ITEM_clsid(it)].set_cmds++;
pthread_mutex_unlock(&c->thread->stats.mutex);
if ((it->it_flags & ITEM_CHUNKED) == 0) {
if (strncmp(ITEM_data(it) + it->nbytes - 2, "\r\n", 2) == 0) {
is_valid = true;
}
} else {
char buf[2];
/* should point to the final item chunk */
item_chunk *ch = (item_chunk *) c->ritem;
assert(ch->used != 0);
/* :( We need to look at the last two bytes. This could span two
* chunks.
*/
if (ch->used > 1) {
buf[0] = ch->data[ch->used - 2];
buf[1] = ch->data[ch->used - 1];
} else {
assert(ch->prev);
assert(ch->used == 1);
buf[0] = ch->prev->data[ch->prev->used - 1];
buf[1] = ch->data[ch->used - 1];
}
if (strncmp(buf, "\r\n", 2) == 0) {
is_valid = true;
} else {
assert(1 == 0);
}
}
if (!is_valid) {
// metaset mode always returns errors.
if (c->mset_res) {
c->noreply = false;
}
out_string(c, "CLIENT_ERROR bad data chunk");
} else {
ret = store_item(it, comm, c);
#ifdef ENABLE_DTRACE
uint64_t cas = ITEM_get_cas(it);
switch (c->cmd) {
case NREAD_ADD:
MEMCACHED_COMMAND_ADD(c->sfd, ITEM_key(it), it->nkey,
(ret == 1) ? it->nbytes : -1, cas);
break;
case NREAD_REPLACE:
MEMCACHED_COMMAND_REPLACE(c->sfd, ITEM_key(it), it->nkey,
(ret == 1) ? it->nbytes : -1, cas);
break;
case NREAD_APPEND:
MEMCACHED_COMMAND_APPEND(c->sfd, ITEM_key(it), it->nkey,
(ret == 1) ? it->nbytes : -1, cas);
break;
case NREAD_PREPEND:
MEMCACHED_COMMAND_PREPEND(c->sfd, ITEM_key(it), it->nkey,
(ret == 1) ? it->nbytes : -1, cas);
break;
case NREAD_SET:
MEMCACHED_COMMAND_SET(c->sfd, ITEM_key(it), it->nkey,
(ret == 1) ? it->nbytes : -1, cas);
break;
case NREAD_CAS:
MEMCACHED_COMMAND_CAS(c->sfd, ITEM_key(it), it->nkey, it->nbytes,
cas);
break;
}
#endif
if (c->mset_res) {
// Replace the status code in the response.
// Rest was prepared during mset parsing.
mc_resp *resp = c->resp;
conn_set_state(c, conn_new_cmd);
switch (ret) {
case STORED:
memcpy(resp->wbuf, "OK ", 3);
// Only place noreply is used for meta cmds is a nominal response.
if (c->noreply) {
resp->skip = true;
}
break;
case EXISTS:
memcpy(resp->wbuf, "EX ", 3);
break;
case NOT_FOUND:
memcpy(resp->wbuf, "NF ", 3);
break;
case NOT_STORED:
memcpy(resp->wbuf, "NS ", 3);
break;
default:
c->noreply = false;
out_string(c, "SERVER_ERROR Unhandled storage type.");
}
} else {
switch (ret) {
case STORED:
out_string(c, "STORED");
break;
case EXISTS:
out_string(c, "EXISTS");
break;
case NOT_FOUND:
out_string(c, "NOT_FOUND");
break;
case NOT_STORED:
out_string(c, "NOT_STORED");
break;
default:
out_string(c, "SERVER_ERROR Unhandled storage type.");
}
}
}
c->set_stale = false; /* force flag to be off just in case */
c->mset_res = false;
item_remove(c->item); /* release the c->item reference */
c->item = 0;
}
#define COMMAND_TOKEN 0
#define SUBCOMMAND_TOKEN 1
#define KEY_TOKEN 1
#define MAX_TOKENS 24
#define WANT_TOKENS(ntokens, min, max) \
do { \
if ((min != -1 && ntokens < min) || (max != -1 && ntokens > max)) { \
out_string(c, "ERROR"); \
return; \
} \
} while (0)
#define WANT_TOKENS_OR(ntokens, a, b) \
do { \
if (ntokens != a && ntokens != b) { \
out_string(c, "ERROR"); \
return; \
} \
} while (0)
#define WANT_TOKENS_MIN(ntokens, min) \
do { \
if (ntokens < min) { \
out_string(c, "ERROR"); \
return; \
} \
} while (0)
/*
* Tokenize the command string by replacing whitespace with '\0' and update
* the token array tokens with pointer to start of each token and length.
* Returns total number of tokens. The last valid token is the terminal
* token (value points to the first unprocessed character of the string and
* length zero).
*
* Usage example:
*
* while(tokenize_command(command, ncommand, tokens, max_tokens) > 0) {
* for(int ix = 0; tokens[ix].length != 0; ix++) {
* ...
* }
* ncommand = tokens[ix].value - command;
* command = tokens[ix].value;
* }
*/
static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) {
char *s, *e;
size_t ntokens = 0;
size_t len = strlen(command);
unsigned int i = 0;
assert(command != NULL && tokens != NULL && max_tokens > 1);
s = e = command;
for (i = 0; i < len; i++) {
if (*e == ' ') {
if (s != e) {
tokens[ntokens].value = s;
tokens[ntokens].length = e - s;
ntokens++;
*e = '\0';
if (ntokens == max_tokens - 1) {
e++;
s = e; /* so we don't add an extra token */
break;
}
}
s = e + 1;
}
e++;
}
if (s != e) {
tokens[ntokens].value = s;
tokens[ntokens].length = e - s;
ntokens++;
}
/*
* If we scanned the whole string, the terminal value pointer is null,
* otherwise it is the first unprocessed character.
*/
tokens[ntokens].value = *e == '\0' ? NULL : e;
tokens[ntokens].length = 0;
ntokens++;
return ntokens;
}
int try_read_command_asciiauth(conn *c) {
token_t tokens[MAX_TOKENS];
size_t ntokens;
char *cont = NULL;
// TODO: move to another function.
if (!c->sasl_started) {
char *el;
uint32_t size = 0;
// impossible for the auth command to be this short.
if (c->rbytes < 2)
return 0;
el = memchr(c->rcurr, '\n', c->rbytes);
// If no newline after 1k, getting junk data, close out.
if (!el) {
if (c->rbytes > 1024) {
conn_set_state(c, conn_closing);
return 1;
}
return 0;
}
// Looking for: "set foo 0 0 N\r\nuser pass\r\n"
// key, flags, and ttl are ignored. N is used to see if we have the rest.
// so tokenize doesn't walk past into the value.
// it's fine to leave the \r in, as strtoul will stop at it.
*el = '\0';
ntokens = tokenize_command(c->rcurr, tokens, MAX_TOKENS);
// ensure the buffer is consumed.
c->rbytes -= (el - c->rcurr) + 1;
c->rcurr += (el - c->rcurr) + 1;
// final token is a NULL ender, so we have one more than expected.
if (ntokens < 6
|| strcmp(tokens[0].value, "set") != 0
|| !safe_strtoul(tokens[4].value, &size)) {
if (!c->resp) {
if (!resp_start(c)) {
conn_set_state(c, conn_closing);
return 1;
}
}
out_string(c, "CLIENT_ERROR unauthenticated");
return 1;
}
// we don't actually care about the key at all; it can be anything.
// we do care about the size of the remaining read.
c->rlbytes = size + 2;
c->sasl_started = true; // reuse from binprot sasl, but not sasl :)
}
if (c->rbytes < c->rlbytes) {
// need more bytes.
return 0;
}
// Going to respond at this point, so attach a response object.
if (!c->resp) {
if (!resp_start(c)) {
conn_set_state(c, conn_closing);
return 1;
}
}
cont = c->rcurr;
// advance buffer. no matter what we're stopping.
c->rbytes -= c->rlbytes;
c->rcurr += c->rlbytes;
c->sasl_started = false;
// must end with \r\n
// NB: I thought ASCII sets also worked with just \n, but according to
// complete_nread_ascii only \r\n is valid.
if (strncmp(cont + c->rlbytes - 2, "\r\n", 2) != 0) {
out_string(c, "CLIENT_ERROR bad command line termination");
return 1;
}
// payload should be "user pass", so we can use the tokenizer.
cont[c->rlbytes - 2] = '\0';
ntokens = tokenize_command(cont, tokens, MAX_TOKENS);
if (ntokens < 3) {
out_string(c, "CLIENT_ERROR bad authentication token format");
return 1;
}
if (authfile_check(tokens[0].value, tokens[1].value) == 1) {
out_string(c, "STORED");
c->authenticated = true;
c->try_read_command = try_read_command_ascii;
pthread_mutex_lock(&c->thread->stats.mutex);
c->thread->stats.auth_cmds++;
pthread_mutex_unlock(&c->thread->stats.mutex);
} else {
out_string(c, "CLIENT_ERROR authentication failure");
pthread_mutex_lock(&c->thread->stats.mutex);
c->thread->stats.auth_cmds++;
c->thread->stats.auth_errors++;
pthread_mutex_unlock(&c->thread->stats.mutex);
}
return 1;
}
int try_read_command_ascii(conn *c) {
char *el, *cont;
if (c->rbytes == 0)
return 0;
el = memchr(c->rcurr, '\n', c->rbytes);
if (!el) {
if (c->rbytes > 1024) {
/*
* We didn't have a '\n' in the first k. This _has_ to be a
* large multiget, if not we should just nuke the connection.
*/
char *ptr = c->rcurr;
while (*ptr == ' ') { /* ignore leading whitespaces */
++ptr;
}
if (ptr - c->rcurr > 100 ||
(strncmp(ptr, "get ", 4) && strncmp(ptr, "gets ", 5))) {
conn_set_state(c, conn_closing);
return 1;
}
// ASCII multigets are unbound, so our fixed size rbuf may not
// work for this particular workload... For backcompat we'll use a
// malloc/realloc/free routine just for this.
if (!c->rbuf_malloced) {
if (!rbuf_switch_to_malloc(c)) {
conn_set_state(c, conn_closing);
return 1;
}
}
}
return 0;
}
cont = el + 1;
if ((el - c->rcurr) > 1 && *(el - 1) == '\r') {
el--;
}
*el = '\0';
assert(cont <= (c->rcurr + c->rbytes));
c->last_cmd_time = current_time;
process_command(c, c->rcurr);
c->rbytes -= (cont - c->rcurr);
c->rcurr = cont;
assert(c->rcurr <= (c->rbuf + c->rsize));
return 1;
}
static inline bool set_noreply_maybe(conn *c, token_t *tokens, size_t ntokens)
{
int noreply_index = ntokens - 2;
/*
NOTE: this function is not the first place where we are going to
send the reply. We could send it instead from process_command()
if the request line has wrong number of tokens. However parsing
malformed line for "noreply" option is not reliable anyway, so
it can't be helped.
*/
if (tokens[noreply_index].value
&& strcmp(tokens[noreply_index].value, "noreply") == 0) {
c->noreply = true;
}
return c->noreply;
}
/* client flags == 0 means use no storage for client flags */
static inline int make_ascii_get_suffix(char *suffix, item *it, bool return_cas, int nbytes) {
char *p = suffix;
*p = ' ';
p++;
if (FLAGS_SIZE(it) == 0) {
*p = '0';
p++;
} else {
p = itoa_u32(*((uint32_t *) ITEM_suffix(it)), p);
}
*p = ' ';
p = itoa_u32(nbytes-2, p+1);
if (return_cas) {
*p = ' ';
p = itoa_u64(ITEM_get_cas(it), p+1);
}
*p = '\r';
*(p+1) = '\n';
*(p+2) = '\0';
return (p - suffix) + 2;
}
/* ntokens is overwritten here... shrug.. */
static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens, bool return_cas, bool should_touch) {
char *key;
size_t nkey;
item *it;
token_t *key_token = &tokens[KEY_TOKEN];
int32_t exptime_int = 0;
rel_time_t exptime = 0;
bool fail_length = false;
assert(c != NULL);
mc_resp *resp = c->resp;
if (should_touch) {
// For get and touch commands, use first token as exptime
if (!safe_strtol(tokens[1].value, &exptime_int)) {
out_string(c, "CLIENT_ERROR invalid exptime argument");
return;
}
key_token++;
exptime = realtime(EXPTIME_TO_POSITIVE_TIME(exptime_int));
}
do {
while(key_token->length != 0) {
bool overflow; // not used here.
key = key_token->value;
nkey = key_token->length;
if (nkey > KEY_MAX_LENGTH) {
fail_length = true;
goto stop;
}
it = limited_get(key, nkey, c, exptime, should_touch, DO_UPDATE, &overflow);
if (settings.detail_enabled) {
stats_prefix_record_get(key, nkey, NULL != it);
}
if (it) {
/*
* Construct the response. Each hit adds three elements to the
* outgoing data list:
* "VALUE "
* key
* " " + flags + " " + data length + "\r\n" + data (with \r\n)
*/
{
MEMCACHED_COMMAND_GET(c->sfd, ITEM_key(it), it->nkey,
it->nbytes, ITEM_get_cas(it));
int nbytes = it->nbytes;;
nbytes = it->nbytes;
char *p = resp->wbuf;
memcpy(p, "VALUE ", 6);
p += 6;
memcpy(p, ITEM_key(it), it->nkey);
p += it->nkey;
p += make_ascii_get_suffix(p, it, return_cas, nbytes);
resp_add_iov(resp, resp->wbuf, p - resp->wbuf);
#ifdef EXTSTORE
if (it->it_flags & ITEM_HDR) {
if (storage_get_item(c, it, resp) != 0) {
pthread_mutex_lock(&c->thread->stats.mutex);
c->thread->stats.get_oom_extstore++;
pthread_mutex_unlock(&c->thread->stats.mutex);
item_remove(it);
goto stop;
}
} else if ((it->it_flags & ITEM_CHUNKED) == 0) {
resp_add_iov(resp, ITEM_data(it), it->nbytes);
} else {
resp_add_chunked_iov(resp, it, it->nbytes);
}
#else
if ((it->it_flags & ITEM_CHUNKED) == 0) {
resp_add_iov(resp, ITEM_data(it), it->nbytes);
} else {
resp_add_chunked_iov(resp, it, it->nbytes);
}
#endif
}
if (settings.verbose > 1) {
int ii;
fprintf(stderr, ">%d sending key ", c->sfd);
for (ii = 0; ii < it->nkey; ++ii) {
fprintf(stderr, "%c", key[ii]);
}
fprintf(stderr, "\n");
}
/* item_get() has incremented it->refcount for us */
pthread_mutex_lock(&c->thread->stats.mutex);
if (should_touch) {
c->thread->stats.touch_cmds++;
c->thread->stats.slab_stats[ITEM_clsid(it)].touch_hits++;
} else {
c->thread->stats.lru_hits[it->slabs_clsid]++;
c->thread->stats.get_cmds++;
}
pthread_mutex_unlock(&c->thread->stats.mutex);
#ifdef EXTSTORE
/* If ITEM_HDR, an io_wrap owns the reference. */
if ((it->it_flags & ITEM_HDR) == 0) {
resp->item = it;
}
#else
resp->item = it;
#endif
} else {
pthread_mutex_lock(&c->thread->stats.mutex);
if (should_touch) {
c->thread->stats.touch_cmds++;
c->thread->stats.touch_misses++;
} else {
c->thread->stats.get_misses++;
c->thread->stats.get_cmds++;
}
MEMCACHED_COMMAND_GET(c->sfd, key, nkey, -1, 0);
pthread_mutex_unlock(&c->thread->stats.mutex);
}
key_token++;
if (key_token->length != 0) {
if (!resp_start(c)) {
goto stop;
}
resp = c->resp;
}
}
/*
* If the command string hasn't been fully processed, get the next set
* of tokens.
*/
if (key_token->value != NULL) {
ntokens = tokenize_command(key_token->value, tokens, MAX_TOKENS);
key_token = tokens;
if (!resp_start(c)) {
goto stop;
}
resp = c->resp;
}
} while(key_token->value != NULL);
stop:
if (settings.verbose > 1)
fprintf(stderr, ">%d END\n", c->sfd);
/*
If the loop was terminated because of out-of-memory, it is not
reliable to add END\r\n to the buffer, because it might not end
in \r\n. So we send SERVER_ERROR instead.
*/
if (key_token->value != NULL) {
// Kill any stacked responses we had.
conn_release_items(c);
// Start a new response object for the error message.
if (!resp_start(c)) {
// severe out of memory error.
conn_set_state(c, conn_closing);
return;
}
if (fail_length) {
out_string(c, "CLIENT_ERROR bad command line format");
} else {
out_of_memory(c, "SERVER_ERROR out of memory writing get response");
}
} else {
// Tag the end token onto the most recent response object.
resp_add_iov(resp, "END\r\n", 5);
conn_set_state(c, conn_mwrite);
}
}
inline static void process_stats_detail(conn *c, const char *command) {
assert(c != NULL);
if (strcmp(command, "on") == 0) {
settings.detail_enabled = 1;
out_string(c, "OK");
}
else if (strcmp(command, "off") == 0) {
settings.detail_enabled = 0;
out_string(c, "OK");
}
else if (strcmp(command, "dump") == 0) {
int len;
char *stats = stats_prefix_dump(&len);
write_and_free(c, stats, len);
}
else {
out_string(c, "CLIENT_ERROR usage: stats detail on|off|dump");
}
}
static void process_stat(conn *c, token_t *tokens, const size_t ntokens) {
const char *subcommand = tokens[SUBCOMMAND_TOKEN].value;
assert(c != NULL);
if (ntokens < 2) {
out_string(c, "CLIENT_ERROR bad command line");
return;
}
if (ntokens == 2) {
server_stats(&append_stats, c);
(void)get_stats(NULL, 0, &append_stats, c);
} else if (strcmp(subcommand, "reset") == 0) {
stats_reset();
out_string(c, "RESET");
return;
} else if (strcmp(subcommand, "detail") == 0) {
/* NOTE: how to tackle detail with binary? */
if (ntokens < 4)
process_stats_detail(c, ""); /* outputs the error message */
else
process_stats_detail(c, tokens[2].value);
/* Output already generated */
return;
} else if (strcmp(subcommand, "settings") == 0) {
process_stat_settings(&append_stats, c);
} else if (strcmp(subcommand, "cachedump") == 0) {
char *buf;
unsigned int bytes, id, limit = 0;
if (!settings.dump_enabled) {
out_string(c, "CLIENT_ERROR stats cachedump not allowed");
return;
}
if (ntokens < 5) {
out_string(c, "CLIENT_ERROR bad command line");
return;
}
if (!safe_strtoul(tokens[2].value, &id) ||
!safe_strtoul(tokens[3].value, &limit)) {
out_string(c, "CLIENT_ERROR bad command line format");
return;
}
if (id >= MAX_NUMBER_OF_SLAB_CLASSES) {
out_string(c, "CLIENT_ERROR Illegal slab id");
return;
}
buf = item_cachedump(id, limit, &bytes);
write_and_free(c, buf, bytes);
return;
} else if (strcmp(subcommand, "conns") == 0) {
process_stats_conns(&append_stats, c);
#ifdef EXTSTORE
} else if (strcmp(subcommand, "extstore") == 0) {
process_extstore_stats(&append_stats, c);
#endif
} else {
/* getting here means that the subcommand is either engine specific or
is invalid. query the engine and see. */
if (get_stats(subcommand, strlen(subcommand), &append_stats, c)) {
if (c->stats.buffer == NULL) {
out_of_memory(c, "SERVER_ERROR out of memory writing stats");
} else {
write_and_free(c, c->stats.buffer, c->stats.offset);
c->stats.buffer = NULL;
}
} else {
out_string(c, "ERROR");
}
return;
}
/* append terminator and start the transfer */
append_stats(NULL, 0, NULL, 0, c);
if (c->stats.buffer == NULL) {
out_of_memory(c, "SERVER_ERROR out of memory writing stats");
} else {
write_and_free(c, c->stats.buffer, c->stats.offset);
c->stats.buffer = NULL;
}
}
// slow snprintf for debugging purposes.
static void process_meta_command(conn *c, token_t *tokens, const size_t ntokens) {
assert(c != NULL);
if (tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) {
out_string(c, "CLIENT_ERROR bad command line format");
return;
}
char *key = tokens[KEY_TOKEN].value;
size_t nkey = tokens[KEY_TOKEN].length;
bool overflow; // not used here.
item *it = limited_get(key, nkey, c, 0, false, DONT_UPDATE, &overflow);
if (it) {
mc_resp *resp = c->resp;
size_t total = 0;
size_t ret;
// similar to out_string().
memcpy(resp->wbuf, "ME ", 3);
total += 3;
memcpy(resp->wbuf + total, ITEM_key(it), it->nkey);
total += it->nkey;
resp->wbuf[total] = ' ';
total++;
ret = snprintf(resp->wbuf + total, WRITE_BUFFER_SIZE - (it->nkey + 12),
"exp=%d la=%llu cas=%llu fetch=%s cls=%u size=%lu\r\n",
(it->exptime == 0) ? -1 : (current_time - it->exptime),
(unsigned long long)(current_time - it->time),
(unsigned long long)ITEM_get_cas(it),
(it->it_flags & ITEM_FETCHED) ? "yes" : "no",
ITEM_clsid(it),
(unsigned long) ITEM_ntotal(it));
item_remove(it);
resp->wbytes = total + ret;
resp_add_iov(resp, resp->wbuf, resp->wbytes);
conn_set_state(c, conn_new_cmd);
} else {
out_string(c, "EN");
}
pthread_mutex_lock(&c->thread->stats.mutex);
c->thread->stats.meta_cmds++;
pthread_mutex_unlock(&c->thread->stats.mutex);
}
#define MFLAG_MAX_OPT_LENGTH 20
#define MFLAG_MAX_OPAQUE_LENGTH 32
struct _meta_flags {
unsigned int has_error :1; // flipped if we found an error during parsing.
unsigned int no_update :1;
unsigned int locked :1;
unsigned int vivify :1;
unsigned int la :1;
unsigned int hit :1;
unsigned int value :1;
unsigned int set_stale :1;
unsigned int no_reply :1;
unsigned int has_cas :1;
unsigned int new_ttl :1;
char mode; // single character mode switch, common to ms/ma
rel_time_t exptime;
rel_time_t autoviv_exptime;
rel_time_t recache_time;
int32_t value_len;
uint32_t client_flags;
uint64_t req_cas_id;
uint64_t delta; // ma
uint64_t initial; // ma
};
static int _meta_flag_preparse(token_t *tokens, const size_t ntokens,
struct _meta_flags *of, char **errstr) {
unsigned int i;
int32_t tmp_int;
uint8_t seen[127] = {0};
// Start just past the key token. Look at first character of each token.
for (i = KEY_TOKEN+1; i < ntokens-1; i++) {
uint8_t o = (uint8_t)tokens[i].value[0];
// zero out repeat flags so we don't over-parse for return data.
if (o >= 127 || seen[o] != 0) {
*errstr = "CLIENT_ERROR duplicate flag";
return -1;
}
seen[o] = 1;
switch (o) {
/* Negative exptimes can underflow and end up immortal. realtime() will
immediately expire values that are greater than REALTIME_MAXDELTA, but less
than process_started, so lets aim for that. */
case 'N':
of->locked = 1;
of->vivify = 1;
if (!safe_strtol(tokens[i].value+1, &tmp_int)) {
*errstr = "CLIENT_ERROR bad token in command line format";
of->has_error = 1;
} else {
of->autoviv_exptime = realtime(EXPTIME_TO_POSITIVE_TIME(tmp_int));
}
break;
case 'T':
of->locked = 1;
if (!safe_strtol(tokens[i].value+1, &tmp_int)) {
*errstr = "CLIENT_ERROR bad token in command line format";
of->has_error = 1;
} else {
of->exptime = realtime(EXPTIME_TO_POSITIVE_TIME(tmp_int));
of->new_ttl = true;
}
break;
case 'R':
of->locked = 1;
if (!safe_strtol(tokens[i].value+1, &tmp_int)) {
*errstr = "CLIENT_ERROR bad token in command line format";
of->has_error = 1;
} else {
of->recache_time = realtime(EXPTIME_TO_POSITIVE_TIME(tmp_int));
}
break;
case 'l':
of->la = 1;
of->locked = 1; // need locked to delay LRU bump
break;
case 'O':
break;
case 'k': // known but no special handling
case 's':
case 't':
case 'c':
case 'f':
break;
case 'v':
of->value = 1;
break;
case 'h':
of->locked = 1; // need locked to delay LRU bump
break;
case 'u':
of->no_update = 1;
break;
case 'q':
of->no_reply = 1;
break;
// mset-related.
case 'F':
if (!safe_strtoul(tokens[i].value+1, &of->client_flags)) {
of->has_error = true;
}
break;
case 'S':
if (!safe_strtol(tokens[i].value+1, &tmp_int)) {
of->has_error = true;
} else {
// Size is adjusted for underflow or overflow once the
// \r\n terminator is added.
if (tmp_int < 0 || tmp_int > (INT_MAX - 2)) {
*errstr = "CLIENT_ERROR invalid length";
of->has_error = true;
} else {
of->value_len = tmp_int + 2; // \r\n
}
}
break;
case 'C': // mset, mdelete, marithmetic
if (!safe_strtoull(tokens[i].value+1, &of->req_cas_id)) {
*errstr = "CLIENT_ERROR bad token in command line format";
of->has_error = true;
} else {
of->has_cas = true;
}
break;
case 'M': // mset and marithmetic mode switch
if (tokens[i].length != 2) {
*errstr = "CLIENT_ERROR incorrect length for M token";
of->has_error = 1;
} else {
of->mode = tokens[i].value[1];
}
break;
case 'J': // marithmetic initial value
if (!safe_strtoull(tokens[i].value+1, &of->initial)) {
*errstr = "CLIENT_ERROR invalid numeric initial value";
of->has_error = 1;
}
break;
case 'D': // marithmetic delta value
if (!safe_strtoull(tokens[i].value+1, &of->delta)) {
*errstr = "CLIENT_ERROR invalid numeric delta value";
of->has_error = 1;
}
break;
case 'I':
of->set_stale = 1;
break;
default: // unknown flag, bail.
*errstr = "CLIENT_ERROR invalid flag";
return -1;
}
}
return of->has_error ? -1 : 0;
}
#define META_SPACE(p) { \
*p = ' '; \
p++; \
}
#define META_CHAR(p, c) { \
*p = ' '; \
*(p+1) = c; \
p += 2; \
}
static void process_mget_command(conn *c, token_t *tokens, const size_t ntokens) {
char *key;
size_t nkey;
item *it;
unsigned int i = 0;
struct _meta_flags of = {0}; // option bitflags.
uint32_t hv; // cached hash value for unlocking an item.
bool failed = false;
bool item_created = false;
bool won_token = false;
bool ttl_set = false;
char *errstr = "CLIENT_ERROR bad command line format";
mc_resp *resp = c->resp;
char *p = resp->wbuf;
assert(c != NULL);
WANT_TOKENS_MIN(ntokens, 3);
if (tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) {
out_errstring(c, "CLIENT_ERROR bad command line format");
return;
}
key = tokens[KEY_TOKEN].value;
nkey = tokens[KEY_TOKEN].length;
// NOTE: final token has length == 0.
// KEY_TOKEN == 1. 0 is command.
if (ntokens == 3) {
// TODO: any way to fix this?
out_errstring(c, "CLIENT_ERROR bad command line format");
return;
} else if (ntokens > MFLAG_MAX_OPT_LENGTH) {
// TODO: ensure the command tokenizer gives us at least this many
out_errstring(c, "CLIENT_ERROR options flags are too long");
return;