1
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3
* memcached - memory caching daemon
5
* http://www.danga.com/memcached/
7
* Copyright 2003 Danga Interactive, Inc. All rights reserved.
9
* Use and distribution licensed under the BSD license. See
10
* the LICENSE file for full text.
13
* Anatoly Vorobey <mellon@pobox.com>
14
* Brad Fitzpatrick <brad@danga.com>
16
* $Id: memcached.c,v 1.47 2004/04/24 20:00:59 avva Exp $
20
#include <sys/types.h>
23
#include <sys/socket.h>
24
#include <sys/signal.h>
25
#include <sys/resource.h>
26
/* some POSIX systems need the following definition
27
* to get mlockall flags out of sys/mman.h. */
28
#ifndef _P1003_1B_VISIBLE
29
#define _P1003_1B_VISIBLE
38
#include <netinet/in.h>
39
#include <netinet/tcp.h>
40
#include <arpa/inet.h>
50
#include "memcached.h"
53
struct settings settings;
55
static item **todelete = 0;
59
time_t realtime(time_t exptime) {
62
/* no. of seconds in 30 days - largest possible delta exptime */
63
#define REALTIME_MAXDELTA 60*60*24*30
65
if (exptime == 0) return 0; /* 0 means never expire */
67
if (exptime > REALTIME_MAXDELTA)
75
void stats_init(void) {
76
stats.curr_items = stats.total_items = stats.curr_conns = stats.total_conns = stats.conn_structs = 0;
77
stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = 0;
78
stats.curr_bytes = stats.bytes_read = stats.bytes_written = 0;
79
stats.started = time(0);
82
void stats_reset(void) {
83
stats.total_items = stats.total_conns = 0;
84
stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = 0;
85
stats.bytes_read = stats.bytes_written = 0;
88
void settings_init(void) {
89
settings.port = 11211;
90
settings.interface.s_addr = htonl(INADDR_ANY);
91
settings.maxbytes = 64*1024*1024; /* default is 64MB */
92
settings.maxconns = 1024; /* to limit connections-related memory to about 5MB */
94
settings.oldest_live = 0;
95
settings.evict_to_free = 1; /* push old items out of cache when memory runs out */
102
void set_cork (conn *c, int val) {
103
if (c->is_corked == val) return;
106
setsockopt(c->sfd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
110
void conn_init(void) {
113
freeconns = (conn **)malloc(sizeof (conn *)*freetotal);
117
conn *conn_new(int sfd, int init_state, int event_flags) {
120
/* do we have a free conn structure from a previous close? */
122
c = freeconns[--freecurr];
123
} else { /* allocate a new one */
124
if (!(c = (conn *)malloc(sizeof(conn)))) {
128
c->rbuf = c->wbuf = 0;
131
c->rbuf = (char *) malloc(DATA_BUFFER_SIZE);
132
c->wbuf = (char *) malloc(DATA_BUFFER_SIZE);
133
c->ilist = (item **) malloc(sizeof(item *)*200);
135
if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0) {
136
if (c->rbuf != 0) free(c->rbuf);
137
if (c->wbuf != 0) free(c->wbuf);
138
if (c->ilist !=0) free(c->ilist);
143
c->rsize = c->wsize = DATA_BUFFER_SIZE;
145
stats.conn_structs++;
148
if (settings.verbose > 1) {
149
if (init_state == conn_listening)
150
fprintf(stderr, "<%d server listening\n", sfd);
152
fprintf(stderr, "<%d new client connection\n", sfd);
156
c->state = init_state;
158
c->rbytes = c->wbytes = 0;
166
c->write_and_go = conn_read;
167
c->write_and_free = 0;
172
event_set(&c->event, sfd, event_flags, event_handler, (void *)c);
173
c->ev_flags = event_flags;
175
if (event_add(&c->event, 0) == -1) {
186
void conn_close(conn *c) {
187
/* delete the event, the socket and the conn */
188
event_del(&c->event);
190
if (settings.verbose > 1)
191
fprintf(stderr, "<%d connection closed.\n", c->sfd);
200
for (; c->ileft > 0; c->ileft--,c->icurr++) {
201
item_remove(*(c->icurr));
205
if (c->write_and_free) {
206
free(c->write_and_free);
209
/* if we have enough space in the free connections array, put the structure there */
210
if (freecurr < freetotal) {
211
freeconns[freecurr++] = c;
213
/* try to enlarge free connections array */
214
conn **new_freeconns = realloc(freeconns, sizeof(conn *)*freetotal*2);
217
freeconns = new_freeconns;
218
freeconns[freecurr++] = c;
232
void out_string(conn *c, char *str) {
235
if (settings.verbose > 1)
236
fprintf(stderr, ">%d %s\n", c->sfd, str);
239
if (len + 2 > c->wsize) {
240
/* ought to be always enough. just fail for simplicity */
241
str = "SERVER_ERROR output line too long";
245
strcpy(c->wbuf, str);
246
strcat(c->wbuf, "\r\n");
250
c->state = conn_write;
251
c->write_and_go = conn_read;
256
* we get here after reading the value in set/add/replace commands. The command
257
* has been stored in c->item_comm, and the item is ready in c->item.
260
void complete_nread(conn *c) {
262
int comm = c->item_comm;
264
time_t now = time(0);
269
if (strncmp(ITEM_data(it) + it->nbytes - 2, "\r\n", 2) != 0) {
270
out_string(c, "CLIENT_ERROR bad data chunk");
274
old_it = assoc_find(ITEM_key(it));
276
if (old_it && old_it->exptime && old_it->exptime < now) {
281
if (old_it && comm==NREAD_ADD) {
283
out_string(c, "NOT_STORED");
287
if (!old_it && comm == NREAD_REPLACE) {
288
out_string(c, "NOT_STORED");
292
if (old_it && (old_it->it_flags & ITEM_DELETED) && (comm == NREAD_REPLACE || comm == NREAD_ADD)) {
293
out_string(c, "NOT_STORED");
298
item_replace(old_it, it);
299
} else item_link(it);
302
out_string(c, "STORED");
311
void process_stat(conn *c, char *command) {
312
time_t now = time(0);
314
if (strcmp(command, "stats") == 0) {
316
pid_t pid = getpid();
320
getrusage(RUSAGE_SELF, &usage);
322
pos += sprintf(pos, "STAT pid %u\r\n", pid);
323
pos += sprintf(pos, "STAT uptime %lu\r\n", now - stats.started);
324
pos += sprintf(pos, "STAT time %u\r\n", now);
325
pos += sprintf(pos, "STAT version " VERSION "\r\n");
326
pos += sprintf(pos, "STAT rusage_user %u:%u\r\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
327
pos += sprintf(pos, "STAT rusage_system %u:%u\r\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
328
pos += sprintf(pos, "STAT curr_items %u\r\n", stats.curr_items);
329
pos += sprintf(pos, "STAT total_items %u\r\n", stats.total_items);
330
pos += sprintf(pos, "STAT bytes %llu\r\n", stats.curr_bytes);
331
pos += sprintf(pos, "STAT curr_connections %u\r\n", stats.curr_conns - 1); /* ignore listening conn */
332
pos += sprintf(pos, "STAT total_connections %u\r\n", stats.total_conns);
333
pos += sprintf(pos, "STAT connection_structures %u\r\n", stats.conn_structs);
334
pos += sprintf(pos, "STAT cmd_get %u\r\n", stats.get_cmds);
335
pos += sprintf(pos, "STAT cmd_set %u\r\n", stats.set_cmds);
336
pos += sprintf(pos, "STAT get_hits %u\r\n", stats.get_hits);
337
pos += sprintf(pos, "STAT get_misses %u\r\n", stats.get_misses);
338
pos += sprintf(pos, "STAT bytes_read %llu\r\n", stats.bytes_read);
339
pos += sprintf(pos, "STAT bytes_written %llu\r\n", stats.bytes_written);
340
pos += sprintf(pos, "STAT limit_maxbytes %u\r\n", settings.maxbytes);
341
pos += sprintf(pos, "END");
346
if (strcmp(command, "stats reset") == 0) {
348
out_string(c, "RESET");
353
#ifdef HAVE_STRUCT_MALLINFO
354
if (strcmp(command, "stats malloc") == 0) {
356
struct mallinfo info;
360
pos += sprintf(pos, "STAT arena_size %d\r\n", info.arena);
361
pos += sprintf(pos, "STAT free_chunks %d\r\n", info.ordblks);
362
pos += sprintf(pos, "STAT fastbin_blocks %d\r\n", info.smblks);
363
pos += sprintf(pos, "STAT mmapped_regions %d\r\n", info.hblks);
364
pos += sprintf(pos, "STAT mmapped_space %d\r\n", info.hblkhd);
365
pos += sprintf(pos, "STAT max_total_alloc %d\r\n", info.usmblks);
366
pos += sprintf(pos, "STAT fastbin_space %d\r\n", info.fsmblks);
367
pos += sprintf(pos, "STAT total_alloc %d\r\n", info.uordblks);
368
pos += sprintf(pos, "STAT total_free %d\r\n", info.fordblks);
369
pos += sprintf(pos, "STAT releasable_space %d\r\nEND", info.keepcost);
373
#endif /* HAVE_STRUCT_MALLINFO */
374
#endif /* HAVE_MALLOC_H */
376
if (strcmp(command, "stats maps") == 0) {
378
int wsize = 8192; /* should be enough */
382
wbuf = (char *)malloc(wsize);
384
out_string(c, "SERVER_ERROR out of memory");
388
fd = open("/proc/self/maps", O_RDONLY);
390
out_string(c, "SERVER_ERROR cannot open the maps file");
395
res = read(fd, wbuf, wsize - 6); /* 6 = END\r\n\0 */
396
if (res == wsize - 6) {
397
out_string(c, "SERVER_ERROR buffer overflow");
398
free(wbuf); close(fd);
401
if (res == 0 || res == -1) {
402
out_string(c, "SERVER_ERROR can't read the maps file");
403
free(wbuf); close(fd);
406
strcpy(wbuf + res, "END\r\n");
407
c->write_and_free=wbuf;
410
c->state = conn_write;
411
c->write_and_go = conn_read;
416
if (strncmp(command, "stats cachedump", 15) == 0) {
418
unsigned int bytes, id, limit = 0;
419
char *start = command + 15;
420
if (sscanf(start, "%u %u\r\n", &id, &limit) < 1) {
421
out_string(c, "CLIENT_ERROR bad command line");
425
buf = item_cachedump(id, limit, &bytes);
427
out_string(c, "SERVER_ERROR out of memory");
431
c->write_and_free = buf;
434
c->state = conn_write;
435
c->write_and_go = conn_read;
439
if (strcmp(command, "stats slabs")==0) {
441
char *buf = slabs_stats(&bytes);
443
out_string(c, "SERVER_ERROR out of memory");
446
c->write_and_free = buf;
449
c->state = conn_write;
450
c->write_and_go = conn_read;
454
if (strcmp(command, "stats items")==0) {
456
item_stats(buffer, 4096);
457
out_string(c, buffer);
461
if (strcmp(command, "stats sizes")==0) {
463
char *buf = item_stats_sizes(&bytes);
465
out_string(c, "SERVER_ERROR out of memory");
469
c->write_and_free = buf;
472
c->state = conn_write;
473
c->write_and_go = conn_read;
477
out_string(c, "ERROR");
480
void process_command(conn *c, char *command) {
486
* for commands set/add/replace, we build an item and read the data
487
* directly into it, then continue in nread_complete().
490
if (settings.verbose > 1)
491
fprintf(stderr, "<%d %s\n", c->sfd, command);
493
/* All incoming commands will require a response, so we cork at the beginning,
494
and uncork at the very end (usually by means of out_string) */
497
if ((strncmp(command, "add ", 4) == 0 && (comm = NREAD_ADD)) ||
498
(strncmp(command, "set ", 4) == 0 && (comm = NREAD_SET)) ||
499
(strncmp(command, "replace ", 8) == 0 && (comm = NREAD_REPLACE))) {
507
res = sscanf(command, "%*s %250s %u %lu %d\n", key, &flags, &expire, &len);
508
if (res!=4 || strlen(key)==0 ) {
509
out_string(c, "CLIENT_ERROR bad command line format");
512
expire = realtime(expire);
513
it = item_alloc(key, flags, expire, len+2);
515
out_string(c, "SERVER_ERROR out of memory");
516
/* swallow the data line */
517
c->write_and_go = conn_swallow;
524
c->rcurr = ITEM_data(it);
525
c->rlbytes = it->nbytes;
526
c->state = conn_nread;
530
if ((strncmp(command, "incr ", 5) == 0 && (incr = 1)) ||
531
(strncmp(command, "decr ", 5) == 0)) {
539
time_t now = time(0);
541
res = sscanf(command, "%*s %250s %u\n", key, &delta);
542
if (res!=2 || strlen(key)==0 ) {
543
out_string(c, "CLIENT_ERROR bad command line format");
547
it = assoc_find(key);
548
if (it && (it->it_flags & ITEM_DELETED)) {
551
if (it && it->exptime && it->exptime < now) {
557
out_string(c, "NOT_FOUND");
562
while (*ptr && (*ptr<'0' && *ptr>'9')) ptr++;
569
if (delta >= value) value = 0;
573
sprintf(temp, "%u", value);
575
if (res + 2 > it->nbytes) { /* need to realloc */
577
new_it = item_alloc(ITEM_key(it), it->flags, it->exptime, res + 2 );
579
out_string(c, "SERVER_ERROR out of memory");
582
memcpy(ITEM_data(new_it), temp, res);
583
memcpy(ITEM_data(new_it) + res, "\r\n", 2);
584
item_replace(it, new_it);
585
} else { /* replace in-place */
586
memcpy(ITEM_data(it), temp, res);
587
memset(ITEM_data(it) + res, ' ', it->nbytes-res-2);
593
if (strncmp(command, "get ", 4) == 0) {
595
char *start = command + 4;
600
time_t now = time(0);
602
while(sscanf(start, " %250s%n", key, &next) >= 1) {
605
it = assoc_find(key);
606
if (it && (it->it_flags & ITEM_DELETED)) {
609
if (settings.oldest_live && it &&
610
it->time <= settings.oldest_live) {
613
if (it && it->exptime && it->exptime < now) {
622
*(c->ilist + i) = it;
626
c->ilist = realloc(c->ilist, sizeof(item *)*c->isize);
628
} else stats.get_misses++;
634
c->state = conn_mwrite;
638
out_string(c, "END");
643
if (strncmp(command, "delete ", 7) == 0) {
649
res = sscanf(command, "%*s %250s %d", key, &exptime);
650
it = assoc_find(key);
652
out_string(c, "NOT_FOUND");
656
exptime = realtime(exptime);
659
/* use its expiration time as its deletion time now */
660
it->exptime = exptime;
661
it->it_flags |= ITEM_DELETED;
662
todelete[delcurr++] = it;
663
if (delcurr >= deltotal) {
665
todelete = realloc(todelete, sizeof(item *)*deltotal);
667
out_string(c, "DELETED");
671
if (strncmp(command, "stats", 5) == 0) {
672
process_stat(c, command);
676
if (strcmp(command, "flush_all") == 0) {
677
settings.oldest_live = time(0);
682
if (strcmp(command, "version") == 0) {
683
out_string(c, "VERSION " VERSION);
687
if (strcmp(command, "quit") == 0) {
688
c->state = conn_closing;
692
if (strncmp(command, "slabs reassign ", 15) == 0) {
694
char *start = command+15;
695
if (sscanf(start, "%u %u\r\n", &src, &dst) == 2) {
696
int rv = slabs_reassign(src, dst);
698
out_string(c, "DONE");
702
out_string(c, "CANT");
706
out_string(c, "BUSY");
710
out_string(c, "CLIENT_ERROR bogus command");
714
out_string(c, "ERROR");
719
* if we have a complete line in the buffer, process it and move whatever
720
* remains in the buffer to its beginning.
722
int try_read_command(conn *c) {
727
el = memchr(c->rbuf, '\n', c->rbytes);
731
if (el - c->rbuf > 1 && *(el - 1) == '\r') {
736
process_command(c, c->rbuf);
738
if (cont - c->rbuf < c->rbytes) { /* more stuff in the buffer */
739
memmove(c->rbuf, cont, c->rbytes - (cont - c->rbuf));
741
c->rbytes -= (cont - c->rbuf);
746
* read from network as much as we can, handle buffer overflow and connection
748
* return 0 if there's nothing to read on the first read.
750
int try_read_network(conn *c) {
754
if (c->rbytes >= c->rsize) {
755
char *new_rbuf = realloc(c->rbuf, c->rsize*2);
757
if (settings.verbose > 0)
758
fprintf(stderr, "Couldn't realloc input buffer\n");
759
c->rbytes = 0; /* ignore what we read */
760
out_string(c, "SERVER_ERROR out of memory");
761
c->write_and_go = conn_closing;
764
c->rbuf = new_rbuf; c->rsize *= 2;
766
res = read(c->sfd, c->rbuf + c->rbytes, c->rsize - c->rbytes);
768
stats.bytes_read += res;
774
/* connection closed */
775
c->state = conn_closing;
779
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
786
int update_event(conn *c, int new_flags) {
787
if (c->ev_flags == new_flags)
789
if (event_del(&c->event) == -1) return 0;
790
event_set(&c->event, c->sfd, new_flags, event_handler, (void *)c);
791
c->ev_flags = new_flags;
792
if (event_add(&c->event, 0) == -1) return 0;
796
void drive_machine(conn *c) {
801
struct sockaddr addr;
806
/* printf("state %d\n", c->state);*/
809
addrlen = sizeof(addr);
810
if ((sfd = accept(c->sfd, &addr, &addrlen)) == -1) {
811
if (errno == EAGAIN || errno == EWOULDBLOCK) {
819
if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 ||
820
fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
821
perror("setting O_NONBLOCK");
825
newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST);
827
if (settings.verbose > 0)
828
fprintf(stderr, "couldn't create new connection\n");
836
if (try_read_command(c)) {
839
if (try_read_network(c)) {
842
/* we have no command line and no data to read from network */
843
if (!update_event(c, EV_READ | EV_PERSIST)) {
844
if (settings.verbose > 0)
845
fprintf(stderr, "Couldn't update event\n");
846
c->state = conn_closing;
853
/* we are reading rlbytes into rcurr; */
854
if (c->rlbytes == 0) {
858
/* first check if we have leftovers in the conn_read buffer */
860
int tocopy = c->rbytes > c->rlbytes ? c->rlbytes : c->rbytes;
861
memcpy(c->rcurr, c->rbuf, tocopy);
863
c->rlbytes -= tocopy;
864
if (c->rbytes > tocopy) {
865
memmove(c->rbuf, c->rbuf+tocopy, c->rbytes - tocopy);
871
/* now try reading from the socket */
872
res = read(c->sfd, c->rcurr, c->rlbytes);
874
stats.bytes_read += res;
879
if (res == 0) { /* end of stream */
880
c->state = conn_closing;
883
if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
884
if (!update_event(c, EV_READ | EV_PERSIST)) {
885
if (settings.verbose > 0)
886
fprintf(stderr, "Couldn't update event\n");
887
c->state = conn_closing;
893
/* otherwise we have a real error, on which we close the connection */
894
if (settings.verbose > 0)
895
fprintf(stderr, "Failed to read, and not due to blocking\n");
896
c->state = conn_closing;
900
/* we are reading sbytes and throwing them away */
901
if (c->sbytes == 0) {
902
c->state = conn_read;
906
/* first check if we have leftovers in the conn_read buffer */
908
int tocopy = c->rbytes > c->sbytes ? c->sbytes : c->rbytes;
910
if (c->rbytes > tocopy) {
911
memmove(c->rbuf, c->rbuf+tocopy, c->rbytes - tocopy);
917
/* now try reading from the socket */
918
res = read(c->sfd, c->rbuf, c->rsize > c->sbytes ? c->sbytes : c->rsize);
920
stats.bytes_read += res;
924
if (res == 0) { /* end of stream */
925
c->state = conn_closing;
928
if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
929
if (!update_event(c, EV_READ | EV_PERSIST)) {
930
if (settings.verbose > 0)
931
fprintf(stderr, "Couldn't update event\n");
932
c->state = conn_closing;
938
/* otherwise we have a real error, on which we close the connection */
939
if (settings.verbose > 0)
940
fprintf(stderr, "Failed to read, and not due to blocking\n");
941
c->state = conn_closing;
945
/* we are writing wbytes bytes starting from wcurr */
946
if (c->wbytes == 0) {
947
if (c->write_and_free) {
948
free(c->write_and_free);
949
c->write_and_free = 0;
951
c->state = c->write_and_go;
952
if (c->state == conn_read)
956
res = write(c->sfd, c->wcurr, c->wbytes);
958
stats.bytes_written += res;
963
if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
964
if (!update_event(c, EV_WRITE | EV_PERSIST)) {
965
if (settings.verbose > 0)
966
fprintf(stderr, "Couldn't update event\n");
967
c->state = conn_closing;
973
/* if res==0 or res==-1 and error is not EAGAIN or EWOULDBLOCK,
974
we have a real error, on which we close the connection */
975
if (settings.verbose > 0)
976
fprintf(stderr, "Failed to write, and not due to blocking\n");
977
c->state = conn_closing;
981
* we're writing ibytes bytes from iptr. iptr alternates between
982
* ibuf, where we build a string "VALUE...", and ITEM_data(it) for the
983
* current item. When we finish a chunk, we choose the next one using
984
* ipart, which has the following semantics: 0 - start the loop, 1 -
985
* we finished ibuf, go to current ITEM_data(it); 2 - we finished ITEM_data(it),
986
* move to the next item and build its ibuf; 3 - we finished all items,
990
res = write(c->sfd, c->iptr, c->ibytes);
992
stats.bytes_written += res;
997
if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
998
if (!update_event(c, EV_WRITE | EV_PERSIST)) {
999
if (settings.verbose > 0)
1000
fprintf(stderr, "Couldn't update event\n");
1001
c->state = conn_closing;
1007
/* if res==0 or res==-1 and error is not EAGAIN or EWOULDBLOCK,
1008
we have a real error, on which we close the connection */
1009
if (settings.verbose > 0)
1010
fprintf(stderr, "Failed to write, and not due to blocking\n");
1011
c->state = conn_closing;
1015
/* we finished a chunk, decide what to do next */
1019
assert((it->it_flags & ITEM_SLABBED) == 0);
1020
c->iptr = ITEM_data(it);
1021
c->ibytes = it->nbytes;
1028
if (c->ileft <= 0) {
1037
assert((it->it_flags & ITEM_SLABBED) == 0);
1038
sprintf(c->ibuf, "VALUE %s %u %u\r\n", ITEM_key(it), it->flags, it->nbytes - 2);
1039
if (settings.verbose > 1)
1040
fprintf(stderr, ">%d sending key %s\n", c->sfd, ITEM_key(it));
1042
c->ibytes = strlen(c->iptr);
1046
out_string(c, "END");
1064
void event_handler(int fd, short which, void *arg) {
1072
if (settings.verbose > 0)
1073
fprintf(stderr, "Catastrophic: event fd doesn't match conn fd!\n");
1078
/* do as much I/O as possible until we block */
1081
/* wait for next event */
1085
int new_socket(void) {
1089
if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
1094
if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 ||
1095
fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
1096
perror("setting O_NONBLOCK");
1103
int server_socket(int port) {
1105
struct linger ling = {0, 0};
1106
struct sockaddr_in addr;
1109
if ((sfd = new_socket()) == -1) {
1113
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
1114
setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags));
1115
setsockopt(sfd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
1116
#if !defined(TCP_NOPUSH)
1117
setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags));
1120
addr.sin_family = AF_INET;
1121
addr.sin_port = htons(port);
1122
addr.sin_addr = settings.interface;
1123
if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
1128
if (listen(sfd, 1024) == -1) {
1136
/* invoke right before gdb is called, on assert */
1139
if(l_socket) close(l_socket);
1140
for (i=3; i<=500; i++) close(i); /* so lame */
1141
kill(getpid(), SIGABRT);
1144
struct event deleteevent;
1146
void delete_handler(int fd, short which, void *arg) {
1149
evtimer_del(&deleteevent);
1150
evtimer_set(&deleteevent, delete_handler, 0);
1151
t.tv_sec = 5; t.tv_usec=0;
1152
evtimer_add(&deleteevent, &t);
1156
time_t now = time(0);
1157
for (i=0; i<delcurr; i++) {
1158
item *it = todelete[i];
1159
if (it->exptime < now) {
1160
assert(it->refcount > 0);
1161
it->it_flags &= ~ITEM_DELETED;
1175
printf(PACKAGE " " VERSION "\n");
1176
printf("-p <num> port number to listen on\n");
1177
printf("-l <ip_addr> interface to listen on, default is INDRR_ANY\n");
1178
printf("-d run as a daemon\n");
1179
printf("-r maximize core file limit\n");
1180
printf("-u <username> assume identity of <username> (only when run as root)\n");
1181
printf("-m <num> max memory to use for items in megabytes, default is 64 MB\n");
1182
printf("-M return error on memory exhausted (rather than removing items)\n");
1183
printf("-c <num> max simultaneous connections, default is 1024\n");
1184
printf("-k lock down all paged memory\n");
1185
printf("-v verbose (print errors/warnings while in event loop)\n");
1186
printf("-vv very verbose (also print client commands/reponses)\n");
1187
printf("-h print this help and exit\n");
1188
printf("-i print memcached and libevent license\n");
1192
void usage_license(void) {
1193
printf(PACKAGE " " VERSION "\n\n");
1195
"Copyright (c) 2003, Danga Interactive, Inc. <http://www.danga.com/>\n"
1196
"All rights reserved.\n"
1198
"Redistribution and use in source and binary forms, with or without\n"
1199
"modification, are permitted provided that the following conditions are\n"
1202
" * Redistributions of source code must retain the above copyright\n"
1203
"notice, this list of conditions and the following disclaimer.\n"
1205
" * Redistributions in binary form must reproduce the above\n"
1206
"copyright notice, this list of conditions and the following disclaimer\n"
1207
"in the documentation and/or other materials provided with the\n"
1210
" * Neither the name of the Danga Interactive nor the names of its\n"
1211
"contributors may be used to endorse or promote products derived from\n"
1212
"this software without specific prior written permission.\n"
1214
"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
1215
"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
1216
"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
1217
"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
1218
"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
1219
"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
1220
"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
1221
"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
1222
"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
1223
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
1224
"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
1227
"This product includes software developed by Niels Provos.\n"
1231
"Copyright 2000-2003 Niels Provos <provos@citi.umich.edu>\n"
1232
"All rights reserved.\n"
1234
"Redistribution and use in source and binary forms, with or without\n"
1235
"modification, are permitted provided that the following conditions\n"
1237
"1. Redistributions of source code must retain the above copyright\n"
1238
" notice, this list of conditions and the following disclaimer.\n"
1239
"2. Redistributions in binary form must reproduce the above copyright\n"
1240
" notice, this list of conditions and the following disclaimer in the\n"
1241
" documentation and/or other materials provided with the distribution.\n"
1242
"3. All advertising materials mentioning features or use of this software\n"
1243
" must display the following acknowledgement:\n"
1244
" This product includes software developed by Niels Provos.\n"
1245
"4. The name of the author may not be used to endorse or promote products\n"
1246
" derived from this software without specific prior written permission.\n"
1248
"THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
1249
"IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"
1250
"OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"
1251
"IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n"
1252
"INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n"
1253
"NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
1254
"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
1255
"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
1256
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n"
1257
"THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
1265
int main (int argc, char **argv) {
1268
struct in_addr addr;
1269
int lock_memory = 0;
1274
struct sigaction sa;
1280
/* process arguments */
1281
while ((c = getopt(argc, argv, "p:m:Mc:khirvdl:u:")) != -1) {
1284
settings.port = atoi(optarg);
1287
settings.maxbytes = atoi(optarg)*1024*1024;
1290
settings.evict_to_free = 0;
1293
settings.maxconns = atoi(optarg);
1308
if (!inet_aton(optarg, &addr)) {
1309
fprintf(stderr, "Illegal address: %s\n", optarg);
1312
settings.interface = addr;
1325
fprintf(stderr, "Illegal argument \"%c\"\n", c);
1331
struct rlimit rlim_new;
1333
* First try raising to infinity; if that fails, try bringing
1334
* the soft limit to the hard.
1336
if (getrlimit(RLIMIT_CORE, &rlim)==0) {
1337
rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
1338
if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) {
1339
/* failed. try raising just to the old max */
1340
rlim_new.rlim_cur = rlim_new.rlim_max =
1342
(void) setrlimit(RLIMIT_CORE, &rlim_new);
1346
* getrlimit again to see what we ended up with. Only fail if
1347
* the soft limit ends up 0, because then no core files will be
1351
if ((getrlimit(RLIMIT_CORE, &rlim)!=0) || rlim.rlim_cur==0) {
1352
fprintf(stderr, "failed to ensure corefile creation\n");
1358
* If needed, increase rlimits to allow as many connections
1362
if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
1363
fprintf(stderr, "failed to getrlimit number of files\n");
1366
int maxfiles = settings.maxconns;
1367
if (rlim.rlim_cur < maxfiles)
1368
rlim.rlim_cur = maxfiles + 3;
1369
if (rlim.rlim_max < rlim.rlim_cur)
1370
rlim.rlim_max = rlim.rlim_cur;
1371
if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
1372
fprintf(stderr, "failed to set rlimit for open files. Try running as root or requesting smaller maxconns value.\n");
1378
* initialization order: first create the listening socket
1379
* (may need root on low ports), then drop root if needed,
1380
* then daemonise if needed, then init libevent (in some cases
1381
* descriptors created by libevent wouldn't survive forking).
1384
/* create the listening socket and bind it */
1385
l_socket = server_socket(settings.port);
1386
if (l_socket == -1) {
1387
fprintf(stderr, "failed to listen\n");
1391
/* lose root privileges if we have them */
1392
if (getuid()== 0 || geteuid()==0) {
1393
if (username==0 || *username=='\0') {
1394
fprintf(stderr, "can't run as root without the -u switch\n");
1397
if ((pw = getpwnam(username)) == 0) {
1398
fprintf(stderr, "can't find the user %s to switch to\n", username);
1401
if (setgid(pw->pw_gid)<0 || setuid(pw->pw_uid)<0) {
1402
fprintf(stderr, "failed to assume identity of user %s\n", username);
1407
/* daemonize if requested */
1408
/* if we want to ensure our ability to dump core, don't chdir to / */
1411
res = daemon(maxcore, settings.verbose);
1413
fprintf(stderr, "failed to daemon() in order to daemonize\n");
1419
/* initialize other stuff */
1425
slabs_init(settings.maxbytes);
1427
/* lock paged memory if needed */
1429
#ifdef HAVE_MLOCKALL
1430
mlockall(MCL_CURRENT | MCL_FUTURE);
1432
fprintf(stderr, "warning: mlockall() not supported on this platform. proceeding without.\n");
1437
* ignore SIGPIPE signals; we can use errno==EPIPE if we
1438
* need that information
1440
sa.sa_handler = SIG_IGN;
1442
if (sigemptyset(&sa.sa_mask) == -1 ||
1443
sigaction(SIGPIPE, &sa, 0) == -1) {
1444
perror("failed to ignore SIGPIPE; sigaction");
1448
/* create the initial listening connection */
1449
if (!(l_conn = conn_new(l_socket, conn_listening, EV_READ | EV_PERSIST))) {
1450
fprintf(stderr, "failed to create listening connection");
1454
/* initialise deletion array and timer event */
1455
deltotal = 200; delcurr = 0;
1456
todelete = malloc(sizeof(item *)*deltotal);
1457
delete_handler(0,0,0); /* sets up the event */
1459
/* enter the loop */