| 1 | /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
|---|
| 2 | /* |
|---|
| 3 | Â *Â memcached - memory caching daemon |
|---|
| 4 | Â * |
|---|
| 5 | Â *Â Â Â Â http://www.danga.com/memcached/ |
|---|
| 6 | Â * |
|---|
| 7 |  * Copyright 2003 Danga Interactive, Inc. All rights reserved. |
|---|
| 8 | Â * |
|---|
| 9 |  * Use and distribution licensed under the BSD license. See |
|---|
| 10 | Â *Â the LICENSE file for full text. |
|---|
| 11 | Â * |
|---|
| 12 | Â *Â Authors: |
|---|
| 13 | Â *Â Â Â Anatoly Vorobey <mellon@pobox.com> |
|---|
| 14 | Â *Â Â Â Brad Fitzpatrick <brad@danga.com> |
|---|
| 15 | std * |
|---|
| 16 | Â *Â $Id$ |
|---|
| 17 | Â */ |
|---|
| 18 | #include "memcached.h" |
|---|
| 19 | #include <sys/stat.h> |
|---|
| 20 | #include <sys/socket.h> |
|---|
| 21 | #include <sys/un.h> |
|---|
| 22 | #include <signal.h> |
|---|
| 23 | #include <sys/resource.h> |
|---|
| 24 | #include <sys/uio.h> |
|---|
| 25 | #include <ctype.h> |
|---|
| 26 | |
|---|
| 27 | /* some POSIX systems need the following definition |
|---|
| 28 |  * to get mlockall flags out of sys/mman.h. */ |
|---|
| 29 | #ifndef _P1003_1B_VISIBLE |
|---|
| 30 | #define _P1003_1B_VISIBLE |
|---|
| 31 | #endif |
|---|
| 32 | /* need this to get IOV_MAX on some platforms. */ |
|---|
| 33 | #ifndef __need_IOV_MAX |
|---|
| 34 | #define __need_IOV_MAX |
|---|
| 35 | #endif |
|---|
| 36 | #include <pwd.h> |
|---|
| 37 | #include <sys/mman.h> |
|---|
| 38 | #include <fcntl.h> |
|---|
| 39 | #include <netinet/tcp.h> |
|---|
| 40 | #include <arpa/inet.h> |
|---|
| 41 | #include <errno.h> |
|---|
| 42 | #include <stdlib.h> |
|---|
| 43 | #include <stdio.h> |
|---|
| 44 | #include <string.h> |
|---|
| 45 | #include <time.h> |
|---|
| 46 | #include <assert.h> |
|---|
| 47 | #include <limits.h> |
|---|
| 48 | |
|---|
| 49 | #ifdef HAVE_MALLOC_H |
|---|
| 50 | /* OpenBSD has a malloc.h, but warns to use stdlib.h instead */ |
|---|
| 51 | #ifndef __OpenBSD__ |
|---|
| 52 | #include <malloc.h> |
|---|
| 53 | #endif |
|---|
| 54 | #endif |
|---|
| 55 | |
|---|
| 56 | /* FreeBSD 4.x doesn't have IOV_MAX exposed. */ |
|---|
| 57 | #ifndef IOV_MAX |
|---|
| 58 | #if defined(__FreeBSD__) || defined(__APPLE__) |
|---|
| 59 | # define IOV_MAX 1024 |
|---|
| 60 | #endif |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | /* |
|---|
| 64 | Â * forward declarations |
|---|
| 65 | Â */ |
|---|
| 66 | static void drive_machine(conn *c); |
|---|
| 67 | static int new_socket(struct addrinfo *ai); |
|---|
| 68 | static int server_socket(const int port, const int prot); |
|---|
| 69 | static int try_read_command(conn *c); |
|---|
| 70 | static int try_read_network(conn *c); |
|---|
| 71 | static int try_read_udp(conn *c); |
|---|
| 72 | static void conn_set_state(conn *, int); |
|---|
| 73 | |
|---|
| 74 | /* stats */ |
|---|
| 75 | static void stats_reset(void); |
|---|
| 76 | static void stats_init(void); |
|---|
| 77 | |
|---|
| 78 | /* defaults */ |
|---|
| 79 | static void settings_init(void); |
|---|
| 80 | |
|---|
| 81 | /* event handling, network IO */ |
|---|
| 82 | static void event_handler(const int fd, const short which, void *arg); |
|---|
| 83 | static void conn_close(conn *c); |
|---|
| 84 | static void conn_init(void); |
|---|
| 85 | static void accept_new_conns(const bool do_accept); |
|---|
| 86 | static bool update_event(conn *c, const int new_flags); |
|---|
| 87 | static void complete_nread(conn *c); |
|---|
| 88 | static void process_command(conn *c, char *command); |
|---|
| 89 | static int transmit(conn *c); |
|---|
| 90 | static int ensure_iov_space(conn *c); |
|---|
| 91 | static int add_iov(conn *c, const void *buf, int len); |
|---|
| 92 | static int add_msghdr(conn *c); |
|---|
| 93 | |
|---|
| 94 | /* time handling */ |
|---|
| 95 | static void set_current_time(void); /* update the global variable holding |
|---|
| 96 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â global 32-bit seconds-since-start time |
|---|
| 97 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (to avoid 64 bit time_t) */ |
|---|
| 98 | |
|---|
| 99 | static void conn_free(conn *c); |
|---|
| 100 | |
|---|
| 101 | /** exported globals **/ |
|---|
| 102 | struct stats stats; |
|---|
| 103 | struct settings settings; |
|---|
| 104 | |
|---|
| 105 | /** file scope variables **/ |
|---|
| 106 | static item **todelete = NULL; |
|---|
| 107 | static int delcurr; |
|---|
| 108 | static int deltotal; |
|---|
| 109 | static conn *listen_conn = NULL; |
|---|
| 110 | static struct event_base *main_base; |
|---|
| 111 | |
|---|
| 112 | #define TRANSMIT_COMPLETEÂ Â 0 |
|---|
| 113 | #define TRANSMIT_INCOMPLETE 1 |
|---|
| 114 | #define TRANSMIT_SOFT_ERROR 2 |
|---|
| 115 | #define TRANSMIT_HARD_ERROR 3 |
|---|
| 116 | |
|---|
| 117 | static int *buckets = 0; /* bucket->generation array for a managed instance */ |
|---|
| 118 | |
|---|
| 119 | #define REALTIME_MAXDELTA 60*60*24*30 |
|---|
| 120 | /* |
|---|
| 121 | Â * given time value that's either unix time or delta from current unix time, return |
|---|
| 122 | Â * unix time. Use the fact that delta can't exceed one month (and real time value can't |
|---|
| 123 | Â * be that low). |
|---|
| 124 | Â */ |
|---|
| 125 | static rel_time_t realtime(const time_t exptime) { |
|---|
| 126 | Â Â /* no. of seconds in 30 days - largest possible delta exptime */ |
|---|
| 127 | |
|---|
| 128 |   if (exptime == 0) return 0; /* 0 means never expire */ |
|---|
| 129 | |
|---|
| 130 |   if (exptime > REALTIME_MAXDELTA) { |
|---|
| 131 | Â Â Â Â /* if item expiration is at/before the server started, give it an |
|---|
| 132 | Â Â Â Â Â Â expiration time of 1 second after the server started. |
|---|
| 133 |       (because 0 means don't expire). without this, we'd |
|---|
| 134 | Â Â Â Â Â Â underflow and wrap around to some large value way in the |
|---|
| 135 | Â Â Â Â Â Â future, effectively making items expiring in the past |
|---|
| 136 | Â Â Â Â Â Â really expiring never */ |
|---|
| 137 |     if (exptime <= stats.started) |
|---|
| 138 |       return (rel_time_t)1; |
|---|
| 139 |     return (rel_time_t)(exptime - stats.started); |
|---|
| 140 |   } else { |
|---|
| 141 |     return (rel_time_t)(exptime + current_time); |
|---|
| 142 | Â Â } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | static void stats_init(void) { |
|---|
| 146 | Â Â stats.curr_items =Â stats.total_items =Â stats.curr_conns =Â stats.total_conns =Â stats.conn_structs =Â 0; |
|---|
| 147 | Â Â stats.get_cmds =Â stats.set_cmds =Â stats.get_hits =Â stats.get_misses =Â stats.evictions =Â 0; |
|---|
| 148 | Â Â stats.curr_bytes =Â stats.bytes_read =Â stats.bytes_written =Â 0; |
|---|
| 149 | |
|---|
| 150 | Â Â /* make the time we started always be 2 seconds before we really |
|---|
| 151 |     did, so time(0) - time.started is never zero. if so, things |
|---|
| 152 | Â Â Â Â like 'settings.oldest_live' which act as booleans as well as |
|---|
| 153 | Â Â Â Â values are now false in boolean context... */ |
|---|
| 154 | Â Â stats.started =Â time(0)Â -Â 2; |
|---|
| 155 | Â Â stats_prefix_init(); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | static void stats_reset(void) { |
|---|
| 159 | Â Â STATS_LOCK(); |
|---|
| 160 | Â Â stats.total_items =Â stats.total_conns =Â 0; |
|---|
| 161 | Â Â stats.get_cmds =Â stats.set_cmds =Â stats.get_hits =Â stats.get_misses =Â stats.evictions =Â 0; |
|---|
| 162 | Â Â stats.bytes_read =Â stats.bytes_written =Â 0; |
|---|
| 163 | Â Â stats_prefix_clear(); |
|---|
| 164 | Â Â STATS_UNLOCK(); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | static void settings_init(void) { |
|---|
| 168 | Â Â settings.access=0700; |
|---|
| 169 | Â Â settings.port =Â 11211; |
|---|
| 170 | Â Â settings.udpport =Â 0; |
|---|
| 171 | Â Â /* By default this string should be NULL for getaddrinfo() */ |
|---|
| 172 | Â Â settings.inter =Â NULL; |
|---|
| 173 | Â Â settings.maxbytes =Â 64Â *Â 1024Â *Â 1024;Â /* default is 64MB */ |
|---|
| 174 | Â Â settings.maxconns =Â 1024;Â Â Â Â Â /* to limit connections-related memory to about 5MB */ |
|---|
| 175 | Â Â settings.verbose =Â 0; |
|---|
| 176 | Â Â settings.oldest_live =Â 0; |
|---|
| 177 | Â Â settings.evict_to_free =Â 1;Â Â Â Â /* push old items out of cache when memory runs out */ |
|---|
| 178 | Â Â settings.socketpath =Â NULL;Â Â Â Â /* by default, not using a unix socket */ |
|---|
| 179 | Â Â settings.managed =Â false; |
|---|
| 180 | Â Â settings.factor =Â 1.25; |
|---|
| 181 | Â Â settings.chunk_size =Â 48;Â Â Â Â Â /* space for a modest key and value */ |
|---|
| 182 | #ifdef USE_THREADS |
|---|
| 183 | Â Â settings.num_threads =Â 4; |
|---|
| 184 | #else |
|---|
| 185 | Â Â settings.num_threads =Â 1; |
|---|
| 186 | #endif |
|---|
| 187 | Â Â settings.prefix_delimiter =Â ':'; |
|---|
| 188 | Â Â settings.detail_enabled =Â 0; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | /* returns true if a deleted item's delete-locked-time is over, and it |
|---|
| 192 | Â Â should be removed from the namespace */ |
|---|
| 193 | static bool item_delete_lock_over (item *it) { |
|---|
| 194 | Â Â assert(it->it_flags &Â ITEM_DELETED); |
|---|
| 195 |   return (current_time >= it->exptime); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | /* |
|---|
| 199 | Â * Adds a message header to a connection. |
|---|
| 200 | Â * |
|---|
| 201 | Â * Returns 0 on success, -1 on out-of-memory. |
|---|
| 202 | Â */ |
|---|
| 203 | static int add_msghdr(conn *c) |
|---|
| 204 | { |
|---|
| 205 |   struct msghdr *msg; |
|---|
| 206 | |
|---|
| 207 | Â Â assert(c !=Â NULL); |
|---|
| 208 | |
|---|
| 209 |   if (c->msgsize == c->msgused) { |
|---|
| 210 |     msg = realloc(c->msglist, c->msgsize * 2 * sizeof(struct msghdr)); |
|---|
| 211 |     if (! msg) |
|---|
| 212 |       return -1; |
|---|
| 213 | Â Â Â Â c->msglist =Â msg; |
|---|
| 214 | Â Â Â Â c->msgsize *=Â 2; |
|---|
| 215 | Â Â } |
|---|
| 216 | |
|---|
| 217 | Â Â msg =Â c->msglist +Â c->msgused; |
|---|
| 218 | |
|---|
| 219 | Â Â /* this wipes msg_iovlen, msg_control, msg_controllen, and |
|---|
| 220 | Â Â Â Â msg_flags, the last 3 of which aren't defined on solaris: */ |
|---|
| 221 |   memset(msg, 0, sizeof(struct msghdr)); |
|---|
| 222 | |
|---|
| 223 | Â Â msg->msg_iov =Â &c->iov[c->iovused]; |
|---|
| 224 | |
|---|
| 225 |   if (c->request_addr_size > 0) { |
|---|
| 226 | Â Â Â Â msg->msg_name =Â &c->request_addr; |
|---|
| 227 | Â Â Â Â msg->msg_namelen =Â c->request_addr_size; |
|---|
| 228 | Â Â } |
|---|
| 229 | |
|---|
| 230 | Â Â c->msgbytes =Â 0; |
|---|
| 231 | Â Â c->msgused++; |
|---|
| 232 | |
|---|
| 233 |   if (IS_UDP(c->protocol)) { |
|---|
| 234 | Â Â Â Â /* Leave room for the UDP header, which we'll fill in later. */ |
|---|
| 235 |     return add_iov(c, NULL, UDP_HEADER_SIZE); |
|---|
| 236 | Â Â } |
|---|
| 237 | |
|---|
| 238 |   return 0; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | /* |
|---|
| 243 | Â * Free list management for connections. |
|---|
| 244 | Â */ |
|---|
| 245 | |
|---|
| 246 | static conn **freeconns; |
|---|
| 247 | static int freetotal; |
|---|
| 248 | static int freecurr; |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | static void conn_init(void) { |
|---|
| 252 | Â Â freetotal =Â 200; |
|---|
| 253 | Â Â freecurr =Â 0; |
|---|
| 254 |   if ((freeconns = (conn **)malloc(sizeof(conn *) * freetotal)) == NULL) { |
|---|
| 255 |     fprintf(stderr, "malloc()\n"); |
|---|
| 256 | Â Â } |
|---|
| 257 | Â Â return; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | /* |
|---|
| 261 | Â * Returns a connection from the freelist, if any. Should call this using |
|---|
| 262 | Â * conn_from_freelist() for thread safety. |
|---|
| 263 | Â */ |
|---|
| 264 | conn *do_conn_from_freelist()Â { |
|---|
| 265 | Â Â conn *c; |
|---|
| 266 | |
|---|
| 267 |   if (freecurr > 0) { |
|---|
| 268 | Â Â Â Â c =Â freeconns[--freecurr]; |
|---|
| 269 |   } else { |
|---|
| 270 | Â Â Â Â c =Â NULL; |
|---|
| 271 | Â Â } |
|---|
| 272 | |
|---|
| 273 |   return c; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | /* |
|---|
| 277 | Â * Adds a connection to the freelist. 0 = success. Should call this using |
|---|
| 278 | Â * conn_add_to_freelist() for thread safety. |
|---|
| 279 | Â */ |
|---|
| 280 | bool do_conn_add_to_freelist(conn *c)Â { |
|---|
| 281 |   if (freecurr < freetotal) { |
|---|
| 282 | Â Â Â Â freeconns[freecurr++]Â =Â c; |
|---|
| 283 |     return false; |
|---|
| 284 |   } else { |
|---|
| 285 | Â Â Â Â /* try to enlarge free connections array */ |
|---|
| 286 |     conn **new_freeconns = realloc(freeconns, sizeof(conn *) * freetotal * 2); |
|---|
| 287 |     if (new_freeconns) { |
|---|
| 288 | Â Â Â Â Â Â freetotal *=Â 2; |
|---|
| 289 | Â Â Â Â Â Â freeconns =Â new_freeconns; |
|---|
| 290 | Â Â Â Â Â Â freeconns[freecurr++]Â =Â c; |
|---|
| 291 |       return false; |
|---|
| 292 | Â Â Â Â } |
|---|
| 293 | Â Â } |
|---|
| 294 |   return true; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | static char *prot_text(const int prot) { |
|---|
| 298 |   char *rv="unknown"; |
|---|
| 299 | Â Â switch(prot)Â { |
|---|
| 300 |     case ascii_prot: |
|---|
| 301 | Â Â Â Â Â Â rv="ascii"; |
|---|
| 302 | Â Â Â Â Â Â break; |
|---|
| 303 |     case binary_prot: |
|---|
| 304 | Â Â Â Â Â Â rv="binary"; |
|---|
| 305 | Â Â Â Â Â Â break; |
|---|
| 306 |     case ascii_udp_prot: |
|---|
| 307 | Â Â Â Â Â Â rv="ascii-udp"; |
|---|
| 308 | Â Â Â Â Â Â break; |
|---|
| 309 |     case negotiating_prot: |
|---|
| 310 | Â Â Â Â Â Â rv="auto-negotiate"; |
|---|
| 311 | Â Â Â Â Â Â break; |
|---|
| 312 | Â Â } |
|---|
| 313 |   return rv; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | conn *conn_new(const int sfd, const int init_state, const int event_flags, |
|---|
| 317 |         const int read_buffer_size, const int prot, |
|---|
| 318 |         struct event_base *base) { |
|---|
| 319 | Â Â conn *c =Â conn_from_freelist(); |
|---|
| 320 | |
|---|
| 321 |   if (NULL == c) { |
|---|
| 322 |     if (!(c = (conn *)malloc(sizeof(conn)))) { |
|---|
| 323 |       fprintf(stderr, "malloc()\n"); |
|---|
| 324 |       return NULL; |
|---|
| 325 | Â Â Â Â } |
|---|
| 326 | Â Â Â Â c->rbuf =Â c->wbuf =Â 0; |
|---|
| 327 | Â Â Â Â c->ilist =Â 0; |
|---|
| 328 | Â Â Â Â c->suffixlist =Â 0; |
|---|
| 329 | Â Â Â Â c->iov =Â 0; |
|---|
| 330 | Â Â Â Â c->msglist =Â 0; |
|---|
| 331 | Â Â Â Â c->hdrbuf =Â 0; |
|---|
| 332 | |
|---|
| 333 | Â Â Â Â c->rsize =Â read_buffer_size; |
|---|
| 334 | Â Â Â Â c->wsize =Â DATA_BUFFER_SIZE; |
|---|
| 335 | Â Â Â Â c->isize =Â ITEM_LIST_INITIAL; |
|---|
| 336 | Â Â Â Â c->suffixsize =Â SUFFIX_LIST_INITIAL; |
|---|
| 337 | Â Â Â Â c->iovsize =Â IOV_LIST_INITIAL; |
|---|
| 338 | Â Â Â Â c->msgsize =Â MSG_LIST_INITIAL; |
|---|
| 339 | Â Â Â Â c->hdrsize =Â 0; |
|---|
| 340 | |
|---|
| 341 |     c->rbuf = (char *)malloc((size_t)c->rsize); |
|---|
| 342 |     c->wbuf = (char *)malloc((size_t)c->wsize); |
|---|
| 343 | Â Â Â Â c->ilist =Â (item **)malloc(sizeof(item *)Â *Â c->isize); |
|---|
| 344 |     c->suffixlist = (char **)malloc(sizeof(char *) * c->suffixsize); |
|---|
| 345 |     c->iov = (struct iovec *)malloc(sizeof(struct iovec) * c->iovsize); |
|---|
| 346 |     c->msglist = (struct msghdr *)malloc(sizeof(struct msghdr) * c->msgsize); |
|---|
| 347 | |
|---|
| 348 |     if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0 || c->iov == 0 || |
|---|
| 349 | Â Â Â Â Â Â Â Â c->msglist ==Â 0Â ||Â c->suffixlist ==Â 0)Â { |
|---|
| 350 |       if (c->rbuf != 0) free(c->rbuf); |
|---|
| 351 |       if (c->wbuf != 0) free(c->wbuf); |
|---|
| 352 |       if (c->ilist !=0) free(c->ilist); |
|---|
| 353 |       if (c->suffixlist != 0) free(c->suffixlist); |
|---|
| 354 |       if (c->iov != 0) free(c->iov); |
|---|
| 355 |       if (c->msglist != 0) free(c->msglist); |
|---|
| 356 | Â Â Â Â Â Â free(c); |
|---|
| 357 |       fprintf(stderr, "malloc()\n"); |
|---|
| 358 |       return NULL; |
|---|
| 359 | Â Â Â Â } |
|---|
| 360 | |
|---|
| 361 | Â Â Â Â STATS_LOCK(); |
|---|
| 362 | Â Â Â Â stats.conn_structs++; |
|---|
| 363 | Â Â Â Â STATS_UNLOCK(); |
|---|
| 364 | Â Â } |
|---|
| 365 | |
|---|
| 366 |   /* unix socket mode doesn't need this, so zeroed out. but why |
|---|
| 367 |    * is this done for every command? presumably for UDP |
|---|
| 368 |    * mode. */ |
|---|
| 369 |   if (!settings.socketpath) { |
|---|
| 370 | Â Â Â Â c->request_addr_size =Â sizeof(c->request_addr); |
|---|
| 371 |   } else { |
|---|
| 372 | Â Â Â Â c->request_addr_size =Â 0; |
|---|
| 373 | Â Â } |
|---|
| 374 | |
|---|
| 375 |   if (settings.verbose > 1) { |
|---|
| 376 |     if (init_state == conn_listening) { |
|---|
| 377 |       fprintf(stderr, "<%d server listening (%s)\n", sfd, |
|---|
| 378 | Â Â Â Â Â Â Â Â prot_text(prot)); |
|---|
| 379 |     } else if (IS_UDP(prot)) { |
|---|
| 380 |       fprintf(stderr, "<%d server listening (udp)\n", sfd); |
|---|
| 381 |     } else if (prot == binary_prot) { |
|---|
| 382 |       fprintf(stderr, "<%d new binary client connection\n", sfd); |
|---|
| 383 |     } else if (prot == ascii_prot) { |
|---|
| 384 |       fprintf(stderr, "<%d new ascii client connection\n", sfd); |
|---|
| 385 |     } else if (prot == negotiating_prot) { |
|---|
| 386 |       fprintf(stderr, "<%d new auto-negotiating client connection\n", sfd); |
|---|
| 387 |     } else { |
|---|
| 388 |       fprintf(stderr, "<%d new unknown (%d) client connection\n", |
|---|
| 389 |         sfd, prot); |
|---|
| 390 | Â Â Â Â Â Â abort(); |
|---|
| 391 | Â Â Â Â } |
|---|
| 392 | Â Â } |
|---|
| 393 | |
|---|
| 394 | Â Â c->sfd =Â sfd; |
|---|
| 395 | Â Â c->protocol =Â prot; |
|---|
| 396 | Â Â c->state =Â init_state; |
|---|
| 397 | Â Â c->rlbytes =Â 0; |
|---|
| 398 | Â Â c->cmd =Â -1; |
|---|
| 399 | Â Â c->rbytes =Â c->wbytes =Â 0; |
|---|
| 400 | Â Â c->wcurr =Â c->wbuf; |
|---|
| 401 | Â Â c->rcurr =Â c->rbuf; |
|---|
| 402 | Â Â c->ritem =Â 0; |
|---|
| 403 | Â Â c->icurr =Â c->ilist; |
|---|
| 404 | Â Â c->suffixcurr =Â c->suffixlist; |
|---|
| 405 | Â Â c->ileft =Â 0; |
|---|
| 406 | Â Â c->suffixleft =Â 0; |
|---|
| 407 | Â Â c->iovused =Â 0; |
|---|
| 408 | Â Â c->msgcurr =Â 0; |
|---|
| 409 | Â Â c->msgused =Â 0; |
|---|
| 410 | |
|---|
| 411 | Â Â c->write_and_go =Â init_state; |
|---|
| 412 | Â Â c->write_and_free =Â 0; |
|---|
| 413 | Â Â c->item =Â 0; |
|---|
| 414 | Â Â c->bucket =Â -1; |
|---|
| 415 | Â Â c->gen =Â 0; |
|---|
| 416 | |
|---|
| 417 | Â Â c->noreply =Â false; |
|---|
| 418 | |
|---|
| 419 |   event_set(&c->event, sfd, event_flags, event_handler, (void *)c); |
|---|
| 420 |   event_base_set(base, &c->event); |
|---|
| 421 | Â Â c->ev_flags =Â event_flags; |
|---|
| 422 | |
|---|
| 423 |   if (event_add(&c->event, 0) == -1) { |
|---|
| 424 |     if (conn_add_to_freelist(c)) { |
|---|
| 425 | Â Â Â Â Â Â conn_free(c); |
|---|
| 426 | Â Â Â Â } |
|---|
| 427 | Â Â Â Â perror("event_add"); |
|---|
| 428 |     return NULL; |
|---|
| 429 | Â Â } |
|---|
| 430 | |
|---|
| 431 | Â Â STATS_LOCK(); |
|---|
| 432 | Â Â stats.curr_conns++; |
|---|
| 433 | Â Â stats.total_conns++; |
|---|
| 434 | Â Â STATS_UNLOCK(); |
|---|
| 435 | |
|---|
| 436 |   return c; |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | static void conn_cleanup(conn *c) { |
|---|
| 440 | Â Â assert(c !=Â NULL); |
|---|
| 441 | |
|---|
| 442 |   if (c->item) { |
|---|
| 443 | Â Â Â Â item_remove(c->item); |
|---|
| 444 | Â Â Â Â c->item =Â 0; |
|---|
| 445 | Â Â } |
|---|
| 446 | |
|---|
| 447 |   if (c->ileft != 0) { |
|---|
| 448 |     for (; c->ileft > 0; c->ileft--,c->icurr++) { |
|---|
| 449 | Â Â Â Â Â Â item_remove(*(c->icurr)); |
|---|
| 450 | Â Â Â Â } |
|---|
| 451 | Â Â } |
|---|
| 452 | |
|---|
| 453 |   if (c->suffixleft != 0) { |
|---|
| 454 |     for (; c->suffixleft > 0; c->suffixleft--, c->suffixcurr++) { |
|---|
| 455 | Â Â Â Â Â Â if(suffix_add_to_freelist(*(c->suffixcurr)))Â { |
|---|
| 456 | Â Â Â Â Â Â Â Â free(*(c->suffixcurr)); |
|---|
| 457 | Â Â Â Â Â Â } |
|---|
| 458 | Â Â Â Â } |
|---|
| 459 | Â Â } |
|---|
| 460 | |
|---|
| 461 |   if (c->write_and_free) { |
|---|
| 462 | Â Â Â Â free(c->write_and_free); |
|---|
| 463 | Â Â Â Â c->write_and_free =Â 0; |
|---|
| 464 | Â Â } |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | /* |
|---|
| 468 | Â * Frees a connection. |
|---|
| 469 | Â */ |
|---|
| 470 | void conn_free(conn *c) { |
|---|
| 471 |   if (c) { |
|---|
| 472 |     if (c->hdrbuf) |
|---|
| 473 | Â Â Â Â Â Â free(c->hdrbuf); |
|---|
| 474 |     if (c->msglist) |
|---|
| 475 | Â Â Â Â Â Â free(c->msglist); |
|---|
| 476 |     if (c->rbuf) |
|---|
| 477 | Â Â Â Â Â Â free(c->rbuf); |
|---|
| 478 |     if (c->wbuf) |
|---|
| 479 | Â Â Â Â Â Â free(c->wbuf); |
|---|
| 480 |     if (c->ilist) |
|---|
| 481 | Â Â Â Â Â Â free(c->ilist); |
|---|
| 482 |     if (c->suffixlist) |
|---|
| 483 | Â Â Â Â Â Â free(c->suffixlist); |
|---|
| 484 |     if (c->iov) |
|---|
| 485 | Â Â Â Â Â Â free(c->iov); |
|---|
| 486 | Â Â Â Â free(c); |
|---|
| 487 | Â Â } |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | static void conn_close(conn *c) { |
|---|
| 491 | Â Â assert(c !=Â NULL); |
|---|
| 492 | |
|---|
| 493 | Â Â /* delete the event, the socket and the conn */ |
|---|
| 494 | Â Â event_del(&c->event); |
|---|
| 495 | |
|---|
| 496 |   if (settings.verbose > 1) |
|---|
| 497 |     fprintf(stderr, "<%d connection closed.\n", c->sfd); |
|---|
| 498 | |
|---|
| 499 | Â Â close(c->sfd); |
|---|
| 500 | Â Â accept_new_conns(true); |
|---|
| 501 | Â Â conn_cleanup(c); |
|---|
| 502 | |
|---|
| 503 | Â Â /* if the connection has big buffers, just free it */ |
|---|
| 504 |   if (c->rsize > READ_BUFFER_HIGHWAT || conn_add_to_freelist(c)) { |
|---|
| 505 | Â Â Â Â conn_free(c); |
|---|
| 506 | Â Â } |
|---|
| 507 | |
|---|
| 508 | Â Â STATS_LOCK(); |
|---|
| 509 | Â Â stats.curr_conns--; |
|---|
| 510 | Â Â STATS_UNLOCK(); |
|---|
| 511 | |
|---|
| 512 | Â Â return; |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | static int get_init_state(conn *c) { |
|---|
| 516 |   int rv=0; |
|---|
| 517 | Â Â assert(c !=Â NULL); |
|---|
| 518 | |
|---|
| 519 | Â Â switch(c->protocol)Â { |
|---|
| 520 |     case binary_prot: |
|---|
| 521 | Â Â Â Â Â Â rv=conn_bin_init; |
|---|
| 522 | Â Â Â Â Â Â break; |
|---|
| 523 |     case negotiating_prot: |
|---|
| 524 | Â Â Â Â Â Â rv=conn_negotiate; |
|---|
| 525 | Â Â Â Â Â Â break; |
|---|
| 526 | Â Â Â Â default: |
|---|
| 527 | Â Â Â Â Â Â rv=conn_read; |
|---|
| 528 | Â Â } |
|---|
| 529 |   return rv; |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | /* Set the given connection to its initial state. The initial state will vary |
|---|
| 533 | Â * base don protocol type. */ |
|---|
| 534 | static void conn_set_init_state(conn *c) { |
|---|
| 535 | Â Â assert(c !=Â NULL); |
|---|
| 536 | |
|---|
| 537 |   conn_set_state(c, get_init_state(c)); |
|---|
| 538 | } |
|---|
| 539 | |
|---|
| 540 | /* |
|---|
| 541 |  * Shrinks a connection's buffers if they're too big. This prevents |
|---|
| 542 | Â * periodic large "get" requests from permanently chewing lots of server |
|---|
| 543 | Â * memory. |
|---|
| 544 | Â * |
|---|
| 545 | Â * This should only be called in between requests since it can wipe output |
|---|
| 546 | Â * buffers! |
|---|
| 547 | Â */ |
|---|
| 548 | static void conn_shrink(conn *c) { |
|---|
| 549 | Â Â assert(c !=Â NULL); |
|---|
| 550 | |
|---|
| 551 |   if (IS_UDP(c->protocol)) |
|---|
| 552 | Â Â Â Â return; |
|---|
| 553 | |
|---|
| 554 |   if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { |
|---|
| 555 |     char *newbuf; |
|---|
| 556 | |
|---|
| 557 |     if (c->rcurr != c->rbuf) |
|---|
| 558 |       memmove(c->rbuf, c->rcurr, (size_t)c->rbytes); |
|---|
| 559 | |
|---|
| 560 |     newbuf = (char *)realloc((void *)c->rbuf, DATA_BUFFER_SIZE); |
|---|
| 561 | |
|---|
| 562 |     if (newbuf) { |
|---|
| 563 | Â Â Â Â Â Â c->rbuf =Â newbuf; |
|---|
| 564 | Â Â Â Â Â Â c->rsize =Â DATA_BUFFER_SIZE; |
|---|
| 565 | Â Â Â Â } |
|---|
| 566 | Â Â Â Â /* TODO check other branch... */ |
|---|
| 567 | Â Â Â Â c->rcurr =Â c->rbuf; |
|---|
| 568 | Â Â } |
|---|
| 569 | |
|---|
| 570 |   if (c->isize > ITEM_LIST_HIGHWAT) { |
|---|
| 571 |     item **newbuf = (item**) realloc((void *)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0])); |
|---|
| 572 |     if (newbuf) { |
|---|
| 573 | Â Â Â Â Â Â c->ilist =Â newbuf; |
|---|
| 574 | Â Â Â Â Â Â c->isize =Â ITEM_LIST_INITIAL; |
|---|
| 575 | Â Â Â Â } |
|---|
| 576 | Â Â /* TODO check error condition? */ |
|---|
| 577 | Â Â } |
|---|
| 578 | |
|---|
| 579 |   if (c->msgsize > MSG_LIST_HIGHWAT) { |
|---|
| 580 |     struct msghdr *newbuf = (struct msghdr *) realloc((void *)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0])); |
|---|
| 581 |     if (newbuf) { |
|---|
| 582 | Â Â Â Â Â Â c->msglist =Â newbuf; |
|---|
| 583 | Â Â Â Â Â Â c->msgsize =Â MSG_LIST_INITIAL; |
|---|
| 584 | Â Â Â Â } |
|---|
| 585 | Â Â /* TODO check error condition? */ |
|---|
| 586 | Â Â } |
|---|
| 587 | |
|---|
| 588 |   if (c->iovsize > IOV_LIST_HIGHWAT) { |
|---|
| 589 |     struct iovec *newbuf = (struct iovec *) realloc((void *)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0])); |
|---|
| 590 |     if (newbuf) { |
|---|
| 591 | Â Â Â Â Â Â c->iov =Â newbuf; |
|---|
| 592 | Â Â Â Â Â Â c->iovsize =Â IOV_LIST_INITIAL; |
|---|
| 593 | Â Â Â Â } |
|---|
| 594 | Â Â /* TODO check return value */ |
|---|
| 595 | Â Â } |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | /* |
|---|
| 599 | Â * Sets a connection's current state in the state machine. Any special |
|---|
| 600 | Â * processing that needs to happen on certain state transitions can |
|---|
| 601 | Â * happen here. |
|---|
| 602 | Â */ |
|---|
| 603 | static void conn_set_state(conn *c, int state) { |
|---|
| 604 | Â Â assert(c !=Â NULL); |
|---|
| 605 | |
|---|
| 606 |   if (state != c->state) { |
|---|
| 607 |     if (state == conn_read) { |
|---|
| 608 | Â Â Â Â Â Â conn_shrink(c); |
|---|
| 609 | Â Â Â Â Â Â assoc_move_next_bucket(); |
|---|
| 610 | Â Â Â Â } |
|---|
| 611 | Â Â Â Â c->state =Â state; |
|---|
| 612 | Â Â } |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | /* |
|---|
| 616 | Â * Free list management for suffix buffers. |
|---|
| 617 | Â */ |
|---|
| 618 | |
|---|
| 619 | static char **freesuffix; |
|---|
| 620 | static int freesuffixtotal; |
|---|
| 621 | static int freesuffixcurr; |
|---|
| 622 | |
|---|
| 623 | static void suffix_init(void) { |
|---|
| 624 | Â Â freesuffixtotal =Â 500; |
|---|
| 625 |   freesuffixcurr = 0; |
|---|
| 626 | |
|---|
| 627 |   freesuffix = (char **)malloc( sizeof(char *) * freesuffixtotal ); |
|---|
| 628 |   if (freesuffix == NULL) { |
|---|
| 629 |     fprintf(stderr, "malloc()\n"); |
|---|
| 630 | Â Â } |
|---|
| 631 | Â Â return; |
|---|
| 632 | } |
|---|
| 633 | |
|---|
| 634 | /* |
|---|
| 635 | Â * Returns a suffix buffer from the freelist, if any. Should call this using |
|---|
| 636 | Â * suffix_from_freelist() for thread safety. |
|---|
| 637 | Â */ |
|---|
| 638 | char *do_suffix_from_freelist() { |
|---|
| 639 |   char *s; |
|---|
| 640 | |
|---|
| 641 |   if (freesuffixcurr > 0) { |
|---|
| 642 | Â Â Â Â s =Â freesuffix[--freesuffixcurr]; |
|---|
| 643 |   } else { |
|---|
| 644 | Â Â Â Â /* If malloc fails, let the logic fall through without spamming |
|---|
| 645 | Â Â Â Â Â * STDERR on the server. */ |
|---|
| 646 | Â Â Â Â s =Â malloc(Â SUFFIX_SIZE ); |
|---|
| 647 | Â Â } |
|---|
| 648 | |
|---|
| 649 |   return s; |
|---|
| 650 | } |
|---|
| 651 | |
|---|
| 652 | /* |
|---|
| 653 | Â * Adds a connection to the freelist. 0 = success. Should call this using |
|---|
| 654 | Â * conn_add_to_freelist() for thread safety. |
|---|
| 655 | Â */ |
|---|
| 656 | bool do_suffix_add_to_freelist(char *s) { |
|---|
| 657 |   if (freesuffixcurr < freesuffixtotal) { |
|---|
| 658 | Â Â Â Â freesuffix[freesuffixcurr++]Â =Â s; |
|---|
| 659 |     return false; |
|---|
| 660 |   } else { |
|---|
| 661 | Â Â Â Â /* try to enlarge free connections array */ |
|---|
| 662 |     char **new_freesuffix = realloc(freesuffix, freesuffixtotal * 2); |
|---|
| 663 |     if (new_freesuffix) { |
|---|
| 664 | Â Â Â Â Â Â freesuffixtotal *=Â 2; |
|---|
| 665 | Â Â Â Â Â Â freesuffix =Â new_freesuffix; |
|---|
| 666 | Â Â Â Â Â Â freesuffix[freesuffixcurr++]Â =Â s; |
|---|
| 667 |       return false; |
|---|
| 668 | Â Â Â Â } |
|---|
| 669 | Â Â } |
|---|
| 670 |   return true; |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | /* |
|---|
| 674 | Â * Ensures that there is room for another struct iovec in a connection's |
|---|
| 675 | Â * iov list. |
|---|
| 676 | Â * |
|---|
| 677 | Â * Returns 0 on success, -1 on out-of-memory. |
|---|
| 678 | Â */ |
|---|
| 679 | static int ensure_iov_space(conn *c) { |
|---|
| 680 | Â Â assert(c !=Â NULL); |
|---|
| 681 | |
|---|
| 682 |   if (c->iovused >= c->iovsize) { |
|---|
| 683 |     int i, iovnum; |
|---|
| 684 |     struct iovec *new_iov = (struct iovec *)realloc(c->iov, |
|---|
| 685 |                 (c->iovsize * 2) * sizeof(struct iovec)); |
|---|
| 686 |     if (! new_iov) |
|---|
| 687 |       return -1; |
|---|
| 688 | Â Â Â Â c->iov =Â new_iov; |
|---|
| 689 | Â Â Â Â c->iovsize *=Â 2; |
|---|
| 690 | |
|---|
| 691 | Â Â Â Â /* Point all the msghdr structures at the new list. */ |
|---|
| 692 |     for (i = 0, iovnum = 0; i < c->msgused; i++) { |
|---|
| 693 | Â Â Â Â Â Â c->msglist[i].msg_iov =Â &c->iov[iovnum]; |
|---|
| 694 | Â Â Â Â Â Â iovnum +=Â c->msglist[i].msg_iovlen; |
|---|
| 695 | Â Â Â Â } |
|---|
| 696 | Â Â } |
|---|
| 697 | |
|---|
| 698 |   return 0; |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | /* |
|---|
| 703 | Â * Adds data to the list of pending data that will be written out to a |
|---|
| 704 | Â * connection. |
|---|
| 705 | Â * |
|---|
| 706 | Â * Returns 0 on success, -1 on out-of-memory. |
|---|
| 707 | Â */ |
|---|
| 708 | |
|---|
| 709 | static int add_iov(conn *c, const void *buf, int len) { |
|---|
| 710 |   struct msghdr *m; |
|---|
| 711 |   int leftover; |
|---|
| 712 | Â Â bool limit_to_mtu; |
|---|
| 713 | |
|---|
| 714 | Â Â assert(c !=Â NULL); |
|---|
| 715 | |
|---|
| 716 |   do { |
|---|
| 717 | Â Â Â Â m =Â &c->msglist[c->msgused -Â 1]; |
|---|
| 718 | |
|---|
| 719 | Â Â Â Â /* |
|---|
| 720 | Â Â Â Â Â * Limit UDP packets, and the first payloads of TCP replies, to |
|---|
| 721 | Â Â Â Â Â * UDP_MAX_PAYLOAD_SIZE bytes. |
|---|
| 722 | Â Â Â Â Â */ |
|---|
| 723 | Â Â Â Â limit_to_mtu =Â IS_UDP(c->protocol)Â ||Â (1Â ==Â c->msgused); |
|---|
| 724 | |
|---|
| 725 | Â Â Â Â /* We may need to start a new msghdr if this one is full. */ |
|---|
| 726 |     if (m->msg_iovlen == IOV_MAX || |
|---|
| 727 | Â Â Â Â Â Â (limit_to_mtu &&Â c->msgbytes >=Â UDP_MAX_PAYLOAD_SIZE))Â { |
|---|
| 728 | Â Â Â Â Â Â add_msghdr(c); |
|---|
| 729 | Â Â Â Â Â Â m =Â &c->msglist[c->msgused -Â 1]; |
|---|
| 730 | Â Â Â Â } |
|---|
| 731 | |
|---|
| 732 |     if (ensure_iov_space(c) != 0) |
|---|
| 733 |       return -1; |
|---|
| 734 | |
|---|
| 735 | Â Â Â Â /* If the fragment is too big to fit in the datagram, split it up */ |
|---|
| 736 |     if (limit_to_mtu && len + c->msgbytes > UDP_MAX_PAYLOAD_SIZE) { |
|---|
| 737 | Â Â Â Â Â Â leftover =Â len +Â c->msgbytes -Â UDP_MAX_PAYLOAD_SIZE; |
|---|
| 738 | Â Â Â Â Â Â len -=Â leftover; |
|---|
| 739 |     } else { |
|---|
| 740 | Â Â Â Â Â Â leftover =Â 0; |
|---|
| 741 | Â Â Â Â } |
|---|
| 742 | |
|---|
| 743 | Â Â Â Â m =Â &c->msglist[c->msgused -Â 1]; |
|---|
| 744 |     m->msg_iov[m->msg_iovlen].iov_base = (void *)buf; |
|---|
| 745 | Â Â Â Â m->msg_iov[m->msg_iovlen].iov_len =Â len; |
|---|
| 746 | |
|---|
| 747 | Â Â Â Â c->msgbytes +=Â len; |
|---|
| 748 | Â Â Â Â c->iovused++; |
|---|
| 749 | Â Â Â Â m->msg_iovlen++; |
|---|
| 750 | |
|---|
| 751 |     buf = ((char *)buf) + len; |
|---|
| 752 | Â Â Â Â len =Â leftover; |
|---|
| 753 |   } while (leftover > 0); |
|---|
| 754 | |
|---|
| 755 |   return 0; |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | /* |
|---|
| 760 | Â * Constructs a set of UDP headers and attaches them to the outgoing messages. |
|---|
| 761 | Â */ |
|---|
| 762 | static int build_udp_headers(conn *c) { |
|---|
| 763 |   int i; |
|---|
| 764 |   unsigned char *hdr; |
|---|
| 765 | |
|---|
| 766 | Â Â assert(c !=Â NULL); |
|---|
| 767 | |
|---|
| 768 |   if (c->msgused > c->hdrsize) { |
|---|
| 769 |     void *new_hdrbuf; |
|---|
| 770 |     if (c->hdrbuf) |
|---|
| 771 |       new_hdrbuf = realloc(c->hdrbuf, c->msgused * 2 * UDP_HEADER_SIZE); |
|---|
| 772 | Â Â Â Â else |
|---|
| 773 | Â Â Â Â Â Â new_hdrbuf =Â malloc(c->msgused *Â 2Â *Â UDP_HEADER_SIZE); |
|---|
| 774 |     if (! new_hdrbuf) |
|---|
| 775 |       return -1; |
|---|
| 776 |     c->hdrbuf = (unsigned char *)new_hdrbuf; |
|---|
| 777 | Â Â Â Â c->hdrsize =Â c->msgused *Â 2; |
|---|
| 778 | Â Â } |
|---|
| 779 | |
|---|
| 780 | Â Â hdr =Â c->hdrbuf; |
|---|
| 781 |   for (i = 0; i < c->msgused; i++) { |
|---|
| 782 | Â Â Â Â c->msglist[i].msg_iov[0].iov_base =Â hdr; |
|---|
| 783 | Â Â Â Â c->msglist[i].msg_iov[0].iov_len =Â UDP_HEADER_SIZE; |
|---|
| 784 | Â Â Â Â *hdr++Â =Â c->request_id /Â 256; |
|---|
| 785 | Â Â Â Â *hdr++Â =Â c->request_id %Â 256; |
|---|
| 786 | Â Â Â Â *hdr++Â =Â i /Â 256; |
|---|
| 787 | Â Â Â Â *hdr++Â =Â i %Â 256; |
|---|
| 788 | Â Â Â Â *hdr++Â =Â c->msgused /Â 256; |
|---|
| 789 | Â Â Â Â *hdr++Â =Â c->msgused %Â 256; |
|---|
| 790 | Â Â Â Â *hdr++Â =Â 0; |
|---|
| 791 | Â Â Â Â *hdr++Â =Â 0; |
|---|
| 792 |     assert((void *) hdr == (void *)c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE); |
|---|
| 793 | Â Â } |
|---|
| 794 | |
|---|
| 795 |   return 0; |
|---|
| 796 | } |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | static void out_string(conn *c, const char *str) { |
|---|
| 800 |   size_t len; |
|---|
| 801 | |
|---|
| 802 | Â Â assert(c !=Â NULL); |
|---|
| 803 | |
|---|
| 804 |   if (c->noreply) { |
|---|
| 805 |     if (settings.verbose > 1) |
|---|
| 806 |       fprintf(stderr, ">%d NOREPLY %s\n", c->sfd, str); |
|---|
| 807 | Â Â Â Â c->noreply =Â false; |
|---|
| 808 |     conn_set_state(c, conn_read); |
|---|
| 809 | Â Â Â Â return; |
|---|
| 810 | Â Â } |
|---|
| 811 | |
|---|
| 812 |   if (settings.verbose > 1) |
|---|
| 813 |     fprintf(stderr, ">%d %s\n", c->sfd, str); |
|---|
| 814 | |
|---|
| 815 | Â Â len =Â strlen(str); |
|---|
| 816 |   if ((len + 2) > c->wsize) { |
|---|
| 817 | Â Â Â Â /* ought to be always enough. just fail for simplicity */ |
|---|
| 818 | Â Â Â Â str =Â "SERVER_ERROR output line too long"; |
|---|
| 819 | Â Â Â Â len =Â strlen(str); |
|---|
| 820 | Â Â } |
|---|
| 821 | |
|---|
| 822 |   memcpy(c->wbuf, str, len); |
|---|
| 823 |   memcpy(c->wbuf + len, "\r\n", 3); |
|---|
| 824 | Â Â c->wbytes =Â len +Â 2; |
|---|
| 825 | Â Â c->wcurr =Â c->wbuf; |
|---|
| 826 | |
|---|
| 827 |   conn_set_state(c, conn_write); |
|---|
| 828 | Â Â c->write_and_go =Â get_init_state(c); |
|---|
| 829 | Â Â return; |
|---|
| 830 | } |
|---|
| 831 | |
|---|
| 832 | /* |
|---|
| 833 | Â * we get here after reading the value in set/add/replace commands. The command |
|---|
| 834 | Â * has been stored in c->item_comm, and the item is ready in c->item. |
|---|
| 835 | Â */ |
|---|
| 836 | static void complete_nread_ascii(conn *c) { |
|---|
| 837 | Â Â assert(c !=Â NULL); |
|---|
| 838 | |
|---|
| 839 | Â Â item *it =Â c->item; |
|---|
| 840 |   int comm = c->item_comm; |
|---|
| 841 |   int ret; |
|---|
| 842 | |
|---|
| 843 | Â Â STATS_LOCK(); |
|---|
| 844 | Â Â stats.set_cmds++; |
|---|
| 845 | Â Â STATS_UNLOCK(); |
|---|
| 846 | |
|---|
| 847 |   if (strncmp(ITEM_data(it) + it->nbytes - 2, "\r\n", 2) != 0) { |
|---|
| 848 |     out_string(c, "CLIENT_ERROR bad data chunk"); |
|---|
| 849 |   } else { |
|---|
| 850 |    ret = store_item(it, comm); |
|---|
| 851 |    if (ret == 1) |
|---|
| 852 |      out_string(c, "STORED"); |
|---|
| 853 |    else if(ret == 2) |
|---|
| 854 |      out_string(c, "EXISTS"); |
|---|
| 855 |    else if(ret == 3) |
|---|
| 856 |      out_string(c, "NOT_FOUND"); |
|---|
| 857 | Â Â Â else |
|---|
| 858 |      out_string(c, "NOT_STORED"); |
|---|
| 859 | Â Â } |
|---|
| 860 | |
|---|
| 861 | Â Â item_remove(c->item);Â Â Â Â /* release the c->item reference */ |
|---|
| 862 | Â Â c->item =Â 0; |
|---|
| 863 | } |
|---|
| 864 | |
|---|
| 865 | static void add_bin_header(conn *c, int err, int hdr_len, int body_len) { |
|---|
| 866 |   int i=0; |
|---|
| 867 |   uint32_t res_header[BIN_PKT_HDR_WORDS]; |
|---|
| 868 | |
|---|
| 869 | Â Â assert(c); |
|---|
| 870 | Â Â assert(body_len >=Â 0); |
|---|
| 871 | |
|---|
| 872 | Â Â c->msgcurr =Â 0; |
|---|
| 873 | Â Â c->msgused =Â 0; |
|---|
| 874 | Â Â c->iovused =Â 0; |
|---|
| 875 |   if (add_msghdr(c) != 0) { |
|---|
| 876 | Â Â Â Â /* XXX:Â out_string is inappropriate here */ |
|---|
| 877 |     out_string(c, "SERVER_ERROR out of memory"); |
|---|
| 878 | Â Â Â Â return; |
|---|
| 879 | Â Â } |
|---|
| 880 | |
|---|
| 881 | Â Â res_header[0]Â =Â BIN_RES_MAGIC <<Â 24; |
|---|
| 882 |   res_header[0] |= ((0xff & c->cmd) << 16); |
|---|
| 883 | Â Â res_header[0]Â |=Â err &Â 0xffff; |
|---|
| 884 | |
|---|
| 885 | Â Â res_header[1]Â =Â hdr_len <<Â 24; |
|---|
| 886 | Â Â /* TODO:Â Support datatype */ |
|---|
| 887 | Â Â res_header[2]Â =Â body_len; |
|---|
| 888 | Â Â res_header[3]Â =Â c->opaque; |
|---|
| 889 | |
|---|
| 890 | Â Â if(settings.verbose >Â 1)Â { |
|---|
| 891 |     fprintf(stderr, "Writing bin response: %08x %08x %08x %08x\n", |
|---|
| 892 |       res_header[0], res_header[1], res_header[2], res_header[3]); |
|---|
| 893 | Â Â } |
|---|
| 894 | |
|---|
| 895 | Â Â for(i=0;Â i<BIN_PKT_HDR_WORDS;Â i++)Â { |
|---|
| 896 | Â Â Â Â res_header[i]Â =Â htonl(res_header[i]); |
|---|
| 897 | Â Â } |
|---|
| 898 | |
|---|
| 899 | Â Â assert(c->wsize >=Â MIN_BIN_PKT_LENGTH); |
|---|
| 900 |   memcpy(c->wbuf, &res_header, MIN_BIN_PKT_LENGTH); |
|---|
| 901 |   add_iov(c, c->wbuf, MIN_BIN_PKT_LENGTH); |
|---|
| 902 | } |
|---|
| 903 | |
|---|
| 904 | static void write_bin_error(conn *c, int err, int swallow) { |
|---|
| 905 |   char *errstr="Unknown error"; |
|---|
| 906 | Â Â switch(err)Â { |
|---|
| 907 |     case ERR_UNKNOWN_CMD: |
|---|
| 908 | Â Â Â Â Â Â errstr="Unknown command"; |
|---|
| 909 | Â Â Â Â Â Â break; |
|---|
| 910 |     case ERR_NOT_FOUND: |
|---|
| 911 | Â Â Â Â Â Â errstr="Not found"; |
|---|
| 912 | Â Â Â Â Â Â break; |
|---|
| 913 |     case ERR_INVALID_ARGUMENTS: |
|---|
| 914 | Â Â Â Â Â Â errstr="Invalid arguments"; |
|---|
| 915 | Â Â Â Â Â Â break; |
|---|
| 916 |     case ERR_EXISTS: |
|---|
| 917 | Â Â Â Â Â Â errstr="Data exists for key."; |
|---|
| 918 | Â Â Â Â Â Â break; |
|---|
| 919 |     case ERR_TOO_LARGE: |
|---|
| 920 | Â Â Â Â Â Â errstr="Too large."; |
|---|
| 921 | Â Â Â Â Â Â break; |
|---|
| 922 |     case ERR_NOT_STORED: |
|---|
| 923 | Â Â Â Â Â Â errstr="Not stored."; |
|---|
| 924 | Â Â Â Â Â Â break; |
|---|
| 925 | Â Â Â Â default: |
|---|
| 926 | Â Â Â Â Â Â errstr="UNHANDLED ERROR"; |
|---|
| 927 |       fprintf(stderr, "UNHANDLED ERROR: %d\n", err); |
|---|
| 928 | Â Â } |
|---|
| 929 | Â Â if(settings.verbose >Â 0)Â { |
|---|
| 930 |     fprintf(stderr, "Writing an error: %s\n", errstr); |
|---|
| 931 | Â Â } |
|---|
| 932 |   add_bin_header(c, err, 0, strlen(errstr)); |
|---|
| 933 |   add_iov(c, errstr, strlen(errstr)); |
|---|
| 934 | |
|---|
| 935 |   conn_set_state(c, conn_mwrite); |
|---|
| 936 | Â Â if(swallow >Â 0)Â { |
|---|
| 937 | Â Â Â Â c->sbytes=swallow; |
|---|
| 938 | Â Â Â Â c->write_and_go =Â conn_swallow; |
|---|
| 939 |   } else { |
|---|
| 940 | Â Â Â Â c->write_and_go =Â conn_bin_init; |
|---|
| 941 | Â Â } |
|---|
| 942 | } |
|---|
| 943 | |
|---|
| 944 | /* Form and send a response to a command over the binary protocol */ |
|---|
| 945 | static void write_bin_response(conn *c, void *d, int hlen, int dlen) { |
|---|
| 946 |   add_bin_header(c, 0, hlen, dlen); |
|---|
| 947 | Â Â if(dlen >Â 0)Â { |
|---|
| 948 |     add_iov(c, d, dlen); |
|---|
| 949 | Â Â } |
|---|
| 950 |   conn_set_state(c, conn_mwrite); |
|---|
| 951 | Â Â c->write_and_go =Â conn_bin_init; |
|---|
| 952 | } |
|---|
| 953 | |
|---|
| 954 | /* Byte swap a 64-bit number */ |
|---|
| 955 | static int64_t swap64(int64_t in) { |
|---|
| 956 | #ifdef ENDIAN_LITTLE |
|---|
| 957 | Â Â /* Little endian, flip the bytes around until someone makes a faster/better |
|---|
| 958 | Â Â * way to do this. */ |
|---|
| 959 |   int64_t rv=0; |
|---|
| 960 |   int i=0; |
|---|
| 961 | Â Â Â for(i=0;Â i<8;Â i++)Â { |
|---|
| 962 | Â Â Â Â rv =Â (rv <<Â 8)Â |Â (in &Â 0xff); |
|---|
| 963 | Â Â Â Â in >>=Â 8; |
|---|
| 964 | Â Â Â } |
|---|
| 965 |   return rv; |
|---|
| 966 | #else |
|---|
| 967 | Â Â /* big-endian machines don't need byte swapping */ |
|---|
| 968 |   return in; |
|---|
| 969 | #endif |
|---|
| 970 | } |
|---|
| 971 | |
|---|
| 972 | static void complete_incr_bin(conn *c) { |
|---|
| 973 | Â Â item *it; |
|---|
| 974 |   int64_t delta; |
|---|
| 975 |   uint64_t initial; |
|---|
| 976 |   int32_t exptime; |
|---|
| 977 |   char *key; |
|---|
| 978 |   size_t nkey; |
|---|
| 979 |   int i; |
|---|
| 980 |   uint64_t *responseBuf = (uint64_t*) c->wbuf + BIN_INCR_HDR_LEN; |
|---|
| 981 | |
|---|
| 982 | Â Â assert(c !=Â NULL); |
|---|
| 983 | |
|---|
| 984 | Â Â key=c->rbuf +Â BIN_INCR_HDR_LEN; |
|---|
| 985 | Â Â nkey=c->keylen; |
|---|
| 986 | Â Â key[nkey]=0x00; |
|---|
| 987 | |
|---|
| 988 | Â Â delta =Â swap64(*((int64_t*)(c->rbuf))); |
|---|
| 989 | Â Â initial =Â (uint64_t)swap64(*((int64_t*)(c->rbuf +Â 8))); |
|---|
| 990 | Â Â exptime =Â ntohl(*((int*)(c->rbuf +Â 16))); |
|---|
| 991 | |
|---|
| 992 | Â Â if(settings.verbose)Â { |
|---|
| 993 |     fprintf(stderr, "incr "); |
|---|
| 994 | Â Â Â Â for(i=0;Â i<nkey;Â i++)Â { |
|---|
| 995 |       fprintf(stderr, "%c", key[i]); |
|---|
| 996 | Â Â Â Â } |
|---|
| 997 |     fprintf(stderr, " %lld, %llu, %d\n", delta, initial, exptime); |
|---|
| 998 | Â Â } |
|---|
| 999 | |
|---|
| 1000 | Â Â /* XXX:Â Not sure what to do with these yet |
|---|
| 1001 | Â Â if (settings.managed) { |
|---|
| 1002 | Â Â Â Â int bucket = c->bucket; |
|---|
| 1003 | Â Â Â Â if (bucket == -1) { |
|---|
| 1004 | Â Â Â Â Â Â out_string(c, "CLIENT_ERROR no BG data in managed mode"); |
|---|
| 1005 | Â Â Â Â Â Â return; |
|---|
| 1006 | Â Â Â Â } |
|---|
| 1007 | Â Â Â Â c->bucket = -1; |
|---|
| 1008 | Â Â Â Â if (buckets[bucket] != c->gen) { |
|---|
| 1009 | Â Â Â Â Â Â out_string(c, "ERROR_NOT_OWNER"); |
|---|
| 1010 | Â Â Â Â Â Â return; |
|---|
| 1011 | Â Â Â Â } |
|---|
| 1012 | Â Â } |
|---|
| 1013 | Â Â */ |
|---|
| 1014 | |
|---|
| 1015 |   it = item_get(key, nkey); |
|---|
| 1016 |   if (it) { |
|---|
| 1017 | Â Â Â Â /* Weird magic in add_delta forces me to pad here */ |
|---|
| 1018 |     char tmpbuf[INCR_MAX_STORAGE_LEN]; |
|---|
| 1019 |     uint64_t l=0; |
|---|
| 1020 |     memset(tmpbuf, ' ', INCR_MAX_STORAGE_LEN); |
|---|
| 1021 | Â Â Â Â tmpbuf[INCR_MAX_STORAGE_LEN]=0x00; |
|---|
| 1022 |     add_delta(it, c->cmd == CMD_INCR, delta, tmpbuf); |
|---|
| 1023 |     *responseBuf=swap64(strtoull(tmpbuf, NULL, 10)); |
|---|
| 1024 | |
|---|
| 1025 |     write_bin_response(c, responseBuf, BIN_INCR_HDR_LEN, INCR_RES_LEN); |
|---|
| 1026 | Â Â Â Â item_remove(it);Â Â Â Â Â /* release our reference */ |
|---|
| 1027 |   } else { |
|---|
| 1028 | Â Â Â Â if(exptime >=Â 0)Â { |
|---|
| 1029 | Â Â Â Â Â Â /* Save some room for the response */ |
|---|
| 1030 | Â Â Â Â Â Â assert(c->wsize >Â BIN_INCR_HDR_LEN +Â BIN_DEL_HDR_LEN); |
|---|
| 1031 | Â Â Â Â Â Â *responseBuf=swap64(initial); |
|---|
| 1032 |       it = item_alloc(key, nkey, 0, realtime(exptime), |
|---|
| 1033 | Â Â Â Â Â Â Â Â INCR_MAX_STORAGE_LEN); |
|---|
| 1034 |       snprintf(ITEM_data(it), INCR_MAX_STORAGE_LEN, "%llu", initial); |
|---|
| 1035 | |
|---|
| 1036 |       if(store_item(it, NREAD_SET)) { |
|---|
| 1037 |         write_bin_response(c, responseBuf, BIN_INCR_HDR_LEN, |
|---|
| 1038 | Â Â Â Â Â Â Â Â Â Â INCR_RES_LEN); |
|---|
| 1039 |       } else { |
|---|
| 1040 |         write_bin_error(c, ERR_NOT_STORED, 0); |
|---|
| 1041 | Â Â Â Â Â Â } |
|---|
| 1042 | Â Â Â Â Â Â item_remove(it);Â Â Â Â Â /* release our reference */ |
|---|
| 1043 |     } else { |
|---|
| 1044 |       write_bin_error(c, ERR_NOT_FOUND, 0); |
|---|
| 1045 | Â Â Â Â } |
|---|
| 1046 | Â Â } |
|---|
| 1047 | } |
|---|
| 1048 | |
|---|
| 1049 | static void complete_update_bin(conn *c) { |
|---|
| 1050 |   int eno=-1, ret=0; |
|---|
| 1051 | Â Â assert(c !=Â NULL); |
|---|
| 1052 | |
|---|
| 1053 | Â Â item *it =Â c->item; |
|---|
| 1054 | |
|---|
| 1055 | Â Â STATS_LOCK(); |
|---|
| 1056 | Â Â stats.set_cmds++; |
|---|
| 1057 | Â Â STATS_UNLOCK(); |
|---|
| 1058 | |
|---|
| 1059 | Â Â /* We don't actually receive the trailing two characters in the bin |
|---|
| 1060 | Â Â Â * protocol, so we're going to just set them here */ |
|---|
| 1061 | Â Â *(ITEM_data(it)Â +Â it->nbytes -Â 2)Â =Â '\r'; |
|---|
| 1062 | Â Â *(ITEM_data(it)Â +Â it->nbytes -Â 1)Â =Â '\n'; |
|---|
| 1063 | |
|---|
| 1064 |   switch (store_item(it, c->item_comm)) { |
|---|
| 1065 |     case 1: |
|---|
| 1066 | Â Â Â Â Â Â /* Stored */ |
|---|
| 1067 |       write_bin_response(c, NULL, BIN_SET_HDR_LEN, 0); |
|---|
| 1068 | Â Â Â Â Â Â break; |
|---|
| 1069 |     case 2: |
|---|
| 1070 |       write_bin_error(c, ERR_EXISTS, 0); |
|---|
| 1071 | Â Â Â Â Â Â break; |
|---|
| 1072 |     case 3: |
|---|
| 1073 |       write_bin_error(c, ERR_NOT_FOUND, 0); |
|---|
| 1074 | Â Â Â Â Â Â break; |
|---|
| 1075 | Â Â Â Â default: |
|---|
| 1076 | Â Â Â Â Â Â if(c->item_comm ==Â NREAD_ADD)Â { |
|---|
| 1077 | Â Â Â Â Â Â Â Â eno=ERR_EXISTS; |
|---|
| 1078 |       } else if(c->item_comm == NREAD_REPLACE) { |
|---|
| 1079 | Â Â Â Â Â Â Â Â eno=ERR_NOT_FOUND; |
|---|
| 1080 |       } else { |
|---|
| 1081 | Â Â Â Â Â Â Â Â eno=ERR_NOT_STORED; |
|---|
| 1082 | Â Â Â Â Â Â } |
|---|
| 1083 |       write_bin_error(c, eno, 0); |
|---|
| 1084 | Â Â } |
|---|
| 1085 | |
|---|
| 1086 | Â Â item_remove(c->item);Â Â Â Â /* release the c->item reference */ |
|---|
| 1087 | Â Â c->item =Â 0; |
|---|
| 1088 | } |
|---|
| 1089 | |
|---|
| 1090 | static void process_bin_get(conn *c) { |
|---|
| 1091 | Â Â item *it; |
|---|
| 1092 | |
|---|
| 1093 |   it = item_get(c->rbuf, c->keylen); |
|---|
| 1094 |   if (it) { |
|---|
| 1095 |     int *flags; |
|---|
| 1096 | Â Â Â Â uint64_t*Â identifier; |
|---|
| 1097 | |
|---|
| 1098 | Â Â Â Â assert(c->rsize >=Â MIN_BIN_PKT_LENGTH +Â 4); |
|---|
| 1099 | |
|---|
| 1100 |     /* This is a bit of magic. I'm using wbuf as the header, so I'll place |
|---|
| 1101 | Â Â Â Â this is int in far enough to cover the header */ |
|---|
| 1102 | Â Â Â Â flags=(int*)(c->wbuf +Â MIN_BIN_PKT_LENGTH); |
|---|
| 1103 |     *flags=htonl(strtoul(ITEM_suffix(it), NULL, 10)); |
|---|
| 1104 | |
|---|
| 1105 | Â Â Â Â /* the length has two unnecessary bytes, and then we write four more */ |
|---|
| 1106 |     add_bin_header(c, 0, GET_RES_HDR_LEN, it->nbytes - 2 + GET_RES_HDR_LEN); |
|---|
| 1107 | Â Â Â Â /* Flags */ |
|---|
| 1108 |     add_iov(c, flags, 4); |
|---|
| 1109 | Â Â Â Â identifier=(uint64_t*)(c->wbuf +Â MIN_BIN_PKT_LENGTH +Â 4); |
|---|
| 1110 | Â Â Â Â *identifier=swap64((uint32_t)it->cas_id); |
|---|
| 1111 |     add_iov(c, identifier, 8); |
|---|
| 1112 | Â Â Â Â /* bytes minus the CRLF */ |
|---|
| 1113 |     add_iov(c, ITEM_data(it), it->nbytes - 2); |
|---|
| 1114 |     conn_set_state(c, conn_mwrite); |
|---|
| 1115 |   } else { |
|---|
| 1116 | Â Â Â Â if(c->cmd ==Â CMD_GETQ)Â { |
|---|
| 1117 |       conn_set_state(c, conn_bin_init); |
|---|
| 1118 |     } else { |
|---|
| 1119 |       write_bin_error(c, ERR_NOT_FOUND, 0); |
|---|
| 1120 | Â Â Â Â } |
|---|
| 1121 | Â Â } |
|---|
| 1122 | } |
|---|
| 1123 | |
|---|
| 1124 | static void bin_read_key(conn *c, int next_substate, int extra) { |
|---|
| 1125 | Â Â assert(c); |
|---|
| 1126 | Â Â c->substate =Â next_substate; |
|---|
| 1127 | Â Â c->rlbytes =Â c->keylen +Â extra; |
|---|
| 1128 | Â Â assert(c->rsize >=Â c->rlbytes); |
|---|
| 1129 | Â Â c->ritem =Â c->rbuf; |
|---|
| 1130 |   conn_set_state(c, conn_nread); |
|---|
| 1131 | } |
|---|
| 1132 | |
|---|
| 1133 | static void dispatch_bin_command(conn *c) { |
|---|
| 1134 |   time_t exptime = 0; |
|---|
| 1135 | Â Â switch(c->cmd)Â { |
|---|
| 1136 |     case CMD_VERSION: |
|---|
| 1137 |       write_bin_response(c, VERSION, 0, strlen(VERSION)); |
|---|
| 1138 | Â Â Â Â Â Â break; |
|---|
| 1139 |     case CMD_FLUSH: |
|---|
| 1140 | Â Â Â Â Â Â set_current_time(); |
|---|
| 1141 | |
|---|
| 1142 | Â Â Â Â Â Â settings.oldest_live =Â current_time -Â 1; |
|---|
| 1143 | Â Â Â Â Â Â item_flush_expired(); |
|---|
| 1144 |       write_bin_response(c, NULL, 0, 0); |
|---|
| 1145 | Â Â Â Â Â Â break; |
|---|
| 1146 |     case CMD_NOOP: |
|---|
| 1147 |       write_bin_response(c, NULL, 0, 0); |
|---|
| 1148 | Â Â Â Â Â Â break; |
|---|
| 1149 |     case CMD_SET: |
|---|
| 1150 | Â Â Â Â Â Â /* Fallthrough */ |
|---|
| 1151 |     case CMD_ADD: |
|---|
| 1152 | Â Â Â Â Â Â /* Fallthrough */ |
|---|
| 1153 |     case CMD_REPLACE: |
|---|
| 1154 |       bin_read_key(c, bin_reading_set_header, BIN_SET_HDR_LEN); |
|---|
| 1155 | Â Â Â Â Â Â break; |
|---|
| 1156 |     case CMD_GETQ: |
|---|
| 1157 |     case CMD_GET: |
|---|
| 1158 |       bin_read_key(c, bin_reading_get_key, 0); |
|---|
| 1159 | Â Â Â Â Â Â break; |
|---|
| 1160 |     case CMD_DELETE: |
|---|
| 1161 |       bin_read_key(c, bin_reading_del_header, BIN_DEL_HDR_LEN); |
|---|
| 1162 | Â Â Â Â Â Â break; |
|---|
| 1163 |     case CMD_INCR: |
|---|
| 1164 |     case CMD_DECR: |
|---|
| 1165 |       bin_read_key(c, bin_reading_incr_header, BIN_INCR_HDR_LEN); |
|---|
| 1166 | Â Â Â Â Â Â break; |
|---|
| 1167 | Â Â Â Â default: |
|---|
| 1168 |       write_bin_error(c, ERR_UNKNOWN_CMD, c->bin_header[2]); |
|---|
| 1169 | Â Â } |
|---|
| 1170 | } |
|---|
| 1171 | |
|---|
| 1172 | static void process_bin_update(conn *c) { |
|---|
| 1173 |   char *key; |
|---|
| 1174 |   int nkey; |
|---|
| 1175 |   int vlen; |
|---|
| 1176 |   int flags; |
|---|
| 1177 |   int exptime; |
|---|
| 1178 | Â Â item *it; |
|---|
| 1179 |   int comm; |
|---|
| 1180 |   int hdrlen=BIN_SET_HDR_LEN; |
|---|
| 1181 | |
|---|
| 1182 | Â Â assert(c !=Â NULL); |
|---|
| 1183 | |
|---|
| 1184 | Â Â key=c->rbuf +Â hdrlen; |
|---|
| 1185 | Â Â nkey=c->keylen; |
|---|
| 1186 | Â Â key[nkey]=0x00; |
|---|
| 1187 | |
|---|
| 1188 | Â Â flags =Â ntohl(*((int*)(c->rbuf))); |
|---|
| 1189 | Â Â exptime =Â ntohl(*((int*)(c->rbuf +Â 4))); |
|---|
| 1190 | Â Â vlen =Â c->bin_header[2]Â -Â (nkey +Â hdrlen); |
|---|
| 1191 | |
|---|
| 1192 | Â Â if(settings.verbose)Â { |
|---|
| 1193 |     fprintf(stderr, "Value len is %d\n", vlen); |
|---|
| 1194 | Â Â } |
|---|
| 1195 | |
|---|
| 1196 |   if (settings.detail_enabled) { |
|---|
| 1197 | Â Â Â Â stats_prefix_record_set(key); |
|---|
| 1198 | Â Â } |
|---|
| 1199 | |
|---|
| 1200 | Â Â /* Not sure what to do with this. |
|---|
| 1201 | Â Â if (settings.managed) { |
|---|
| 1202 | Â Â Â Â int bucket = c->bucket; |
|---|
| 1203 | Â Â Â Â if (bucket == -1) { |
|---|
| 1204 | Â Â Â Â Â Â out_string(c, "CLIENT_ERROR no BG data in managed mode"); |
|---|
| 1205 | Â Â Â Â Â Â return; |
|---|
| 1206 | Â Â Â Â } |
|---|
| 1207 | Â Â Â Â c->bucket = -1; |
|---|
| 1208 | Â Â Â Â if (buckets[bucket] != c->gen) { |
|---|
| 1209 | Â Â Â Â Â Â out_string(c, "ERROR_NOT_OWNER"); |
|---|
| 1210 | Â Â Â Â Â Â return; |
|---|
| 1211 | Â Â Â Â } |
|---|
| 1212 | Â Â } |
|---|
| 1213 | Â Â */ |
|---|
| 1214 | |
|---|
| 1215 |   it = item_alloc(key, nkey, flags, realtime(exptime), vlen+2); |
|---|
| 1216 | |
|---|
| 1217 |   if (it == 0) { |
|---|
| 1218 |     if (! item_size_ok(nkey, flags, vlen + 2)) { |
|---|
| 1219 |       write_bin_error(c, ERR_TOO_LARGE, vlen); |
|---|
| 1220 |     } else { |
|---|
| 1221 |       write_bin_error(c, ERR_OUT_OF_MEMORY, vlen); |
|---|
| 1222 | Â Â Â Â } |
|---|
| 1223 | Â Â Â Â /* swallow the data line */ |
|---|
| 1224 | Â Â Â Â c->write_and_go =Â conn_swallow; |
|---|
| 1225 | Â Â Â Â return; |
|---|
| 1226 | Â Â } |
|---|
| 1227 | |
|---|
| 1228 | Â Â it->cas_id =Â (uint64_t)swap64(*((int64_t*)(c->rbuf +Â 8))); |
|---|
| 1229 | |
|---|
| 1230 | Â Â switch(c->cmd)Â { |
|---|
| 1231 |     case CMD_ADD: |
|---|
| 1232 | Â Â Â Â Â Â c->item_comm =Â NREAD_ADD; |
|---|
| 1233 | Â Â Â Â Â Â break; |
|---|
| 1234 |     case CMD_SET: |
|---|
| 1235 | Â Â Â Â Â Â c->item_comm =Â NREAD_SET; |
|---|
| 1236 | Â Â Â Â Â Â break; |
|---|
| 1237 |     case CMD_REPLACE: |
|---|
| 1238 | Â Â Â Â Â Â c->item_comm =Â NREAD_REPLACE; |
|---|
| 1239 | Â Â Â Â Â Â break; |
|---|
| 1240 | Â Â Â Â default: |
|---|
| 1241 | Â Â Â Â Â Â assert(0); |
|---|
| 1242 | Â Â } |
|---|
| 1243 | |
|---|
| 1244 | Â Â if(it->cas_id !=Â 0)Â { |
|---|
| 1245 | Â Â Â Â c->item_comm =Â NREAD_CAS; |
|---|
| 1246 | Â Â } |
|---|
| 1247 | |
|---|
| 1248 | Â Â c->item =Â it; |
|---|
| 1249 | Â Â c->ritem =Â ITEM_data(it); |
|---|
| 1250 | Â Â c->rlbytes =Â vlen; |
|---|
| 1251 |   conn_set_state(c, conn_nread); |
|---|
| 1252 | Â Â c->substate =Â bin_read_set_value; |
|---|
| 1253 | } |
|---|
| 1254 | |
|---|
| 1255 | static void process_bin_delete(conn *c) { |
|---|
| 1256 |   char *key; |
|---|
| 1257 |   size_t nkey; |
|---|
| 1258 | Â Â item *it; |
|---|
| 1259 |   time_t exptime = 0; |
|---|
| 1260 | |
|---|
| 1261 | Â Â assert(c !=Â NULL); |
|---|
| 1262 | |
|---|
| 1263 | Â Â /* XXX:Â I don't know what to do with this yet |
|---|
| 1264 | Â Â if (settings.managed) { |
|---|
| 1265 | Â Â Â Â int bucket = c->bucket; |
|---|
| 1266 | Â Â Â Â if (bucket == -1) { |
|---|
| 1267 | Â Â Â Â Â Â out_string(c, "CLIENT_ERROR no BG data in managed mode"); |
|---|
| 1268 | Â Â Â Â Â Â return; |
|---|
| 1269 | Â Â Â Â } |
|---|
| 1270 | Â Â Â Â c->bucket = -1; |
|---|
| 1271 | Â Â Â Â if (buckets[bucket] != c->gen) { |
|---|
| 1272 | Â Â Â Â Â Â out_string(c, "ERROR_NOT_OWNER"); |
|---|
| 1273 | Â Â Â Â Â Â return; |
|---|
| 1274 | Â Â Â Â } |
|---|
| 1275 | Â Â } |
|---|
| 1276 | Â Â */ |
|---|
| 1277 | |
|---|
| 1278 | Â Â exptime =Â ntohl(*((int*)(c->rbuf))); |
|---|
| 1279 | Â Â key =Â c->rbuf +Â 4; |
|---|
| 1280 | Â Â nkey =Â c->keylen; |
|---|
| 1281 | Â Â key[nkey]=0x00; |
|---|
| 1282 | |
|---|
| 1283 | Â Â if(settings.verbose)Â { |
|---|
| 1284 |     fprintf(stderr, "Deleting %s with a timeout of %d\n", key, exptime); |
|---|
| 1285 | Â Â } |
|---|
| 1286 | |
|---|
| 1287 |   if (settings.detail_enabled) { |
|---|
| 1288 | Â Â Â Â stats_prefix_record_delete(key); |
|---|
| 1289 | Â Â } |
|---|
| 1290 | |
|---|
| 1291 |   it = item_get(key, nkey); |
|---|
| 1292 |   if (it) { |
|---|
| 1293 |     if (exptime == 0) { |
|---|
| 1294 | Â Â Â Â Â Â item_unlink(it); |
|---|
| 1295 | Â Â Â Â Â Â item_remove(it);Â Â Â /* release our reference */ |
|---|
| 1296 |       write_bin_response(c, NULL, 0, 0); |
|---|
| 1297 |     } else { |
|---|
| 1298 | Â Â Â Â Â Â /* XXX:Â This is really lame, but defer_delete returns a string */ |
|---|
| 1299 |       char *res=defer_delete(it, exptime); |
|---|
| 1300 | Â Â Â Â Â Â if(res[0]Â ==Â 'D')Â { |
|---|
| 1301 |         write_bin_response(c, NULL, 0, 0); |
|---|
| 1302 |       } else { |
|---|
| 1303 |         write_bin_error(c, ERR_OUT_OF_MEMORY, 0); |
|---|
| 1304 | Â Â Â Â Â Â } |
|---|
| 1305 | Â Â Â Â } |
|---|
| 1306 |   } else { |
|---|
| 1307 |     write_bin_error(c, ERR_NOT_FOUND, 0); |
|---|
| 1308 | Â Â } |
|---|
| 1309 | } |
|---|
| 1310 | |
|---|
| 1311 | static void complete_nread_binary(conn *c) { |
|---|
| 1312 | Â Â assert(c !=Â NULL); |
|---|
| 1313 | |
|---|
| 1314 | Â Â if(c->cmd <Â 0)Â { |
|---|
| 1315 |     /* No command defined. Figure out what they're trying to say. */ |
|---|
| 1316 |     int i=0; |
|---|
| 1317 | Â Â Â Â /* I did a bit of hard-coding around the packet sizes */ |
|---|
| 1318 | Â Â Â Â assert(BIN_PKT_HDR_WORDS ==Â 3); |
|---|
| 1319 | Â Â Â Â for(i=0;Â i<BIN_PKT_HDR_WORDS;Â i++)Â { |
|---|
| 1320 | Â Â Â Â Â Â c->bin_header[i]Â =Â ntohl(c->bin_header[i]); |
|---|
| 1321 | Â Â Â Â } |
|---|
| 1322 | Â Â Â Â if(settings.verbose)Â { |
|---|
| 1323 |       fprintf(stderr, "Read binary protocol data: %08x %08x %08x %08x\n", |
|---|
| 1324 |         c->bin_header[0], c->bin_header[1], c->bin_header[2], |
|---|
| 1325 | Â Â Â Â Â Â Â Â c->bin_header[3]); |
|---|
| 1326 | Â Â Â Â } |
|---|
| 1327 | Â Â Â Â if((c->bin_header[0]Â >>Â 24)Â !=Â BIN_REQ_MAGIC)Â { |
|---|
| 1328 | Â Â Â Â Â Â if(settings.verbose)Â { |
|---|
| 1329 |         fprintf(stderr, "Invalid magic: %x\n", c->bin_header[0] >> 24); |
|---|
| 1330 | Â Â Â Â Â Â } |
|---|
| 1331 |       conn_set_state(c, conn_closing); |
|---|
| 1332 | Â Â Â Â Â Â return; |
|---|
| 1333 | Â Â Â Â } |
|---|
| 1334 | Â Â |
|---|
| 1335 | Â Â Â Â c->msgcurr =Â 0; |
|---|
| 1336 | Â Â Â Â c->msgused =Â 0; |
|---|
| 1337 | Â Â Â Â c->iovused =Â 0; |
|---|
| 1338 |     if (add_msghdr(c) != 0) { |
|---|
| 1339 |       out_string(c, "SERVER_ERROR out of memory"); |
|---|
| 1340 | Â Â Â Â Â Â return; |
|---|
| 1341 | Â Â Â Â } |
|---|
| 1342 | Â Â |
|---|
| 1343 | Â Â Â Â c->cmd =Â (c->bin_header[0]Â >>Â 16)Â &Â 0xff; |
|---|
| 1344 | Â Â Â Â c->keylen =Â c->bin_header[0]Â &Â 0xffff; |
|---|
| 1345 | Â Â Â Â c->opaque =Â c->bin_header[3]; |
|---|
| 1346 | Â Â Â Â if(settings.verbose >Â 1)Â { |
|---|
| 1347 | Â Â Â Â Â Â fprintf(stderr, |
|---|
| 1348 |         "Command: %d, opaque=%08x, keylen=%d, total_len=%d\n", c->cmd, |
|---|
| 1349 |         c->opaque, c->keylen, c->bin_header[2]); |
|---|
| 1350 | Â Â Â Â } |
|---|
| 1351 | Â Â Â Â dispatch_bin_command(c); |
|---|
| 1352 |   } else { |
|---|
| 1353 | Â Â Â Â switch(c->substate)Â { |
|---|
| 1354 |       case bin_reading_set_header: |
|---|
| 1355 | Â Â Â Â Â Â Â Â process_bin_update(c); |
|---|
| 1356 | Â Â Â Â Â Â Â Â break; |
|---|
| 1357 |       case bin_read_set_value: |
|---|
| 1358 | Â Â Â Â Â Â Â Â complete_update_bin(c); |
|---|
| 1359 | Â Â Â Â Â Â Â Â break; |
|---|
| 1360 |       case bin_reading_get_key: |
|---|
| 1361 | Â Â Â Â Â Â Â Â process_bin_get(c); |
|---|
| 1362 | Â Â Â Â Â Â Â Â break; |
|---|
| 1363 |       case bin_reading_del_header: |
|---|
| 1364 | Â Â Â Â Â Â Â Â process_bin_delete(c); |
|---|
| 1365 | Â Â Â Â Â Â Â Â break; |
|---|
| 1366 |       case bin_reading_incr_header: |
|---|
| 1367 | Â Â Â Â Â Â Â Â complete_incr_bin(c); |
|---|
| 1368 | Â Â Â Â Â Â Â Â break; |
|---|
| 1369 | Â Â Â Â Â Â default: |
|---|
| 1370 |         fprintf(stderr, "Not handling substate %d\n", c->substate); |
|---|
| 1371 | Â Â Â Â Â Â Â Â assert(0); |
|---|
| 1372 | Â Â Â Â } |
|---|
| 1373 | Â Â } |
|---|
| 1374 | } |
|---|
| 1375 | |
|---|
| 1376 | static void reinit_bin_connection(conn *c) { |
|---|
| 1377 |   if (settings.verbose > 0) |
|---|
| 1378 |     fprintf(stderr, "*** Reinitializing binary connection.\n"); |
|---|
| 1379 | Â Â c->rlbytes =Â MIN_BIN_PKT_LENGTH; |
|---|
| 1380 | Â Â c->write_and_go =Â conn_bin_init; |
|---|
| 1381 | Â Â c->cmd =Â -1; |
|---|
| 1382 | Â Â c->substate =Â bin_no_state; |
|---|
| 1383 | Â Â c->rbytes =Â c->wbytes =Â 0; |
|---|
| 1384 | Â Â c->ritem =Â (char*)c->bin_header; |
|---|
| 1385 | Â Â c->rcurr =Â c->rbuf; |
|---|
| 1386 | Â Â c->wcurr =Â c->wbuf; |
|---|
| 1387 | Â Â conn_shrink(c); |
|---|
| 1388 |   conn_set_state(c, conn_nread); |
|---|
| 1389 | } |
|---|
| 1390 | |
|---|
| 1391 | /* These do the initial protocol switch. At this point, we should've read |
|---|
| 1392 | Â * exactly one byte, and must treat that byte as the beginning of a command. */ |
|---|
| 1393 | static void setup_bin_protocol(conn *c) { |
|---|
| 1394 |   char *loc = (char*)c->bin_header; |
|---|
| 1395 |   if (settings.verbose > 1) |
|---|
| 1396 |     fprintf(stderr, "Negotiated protocol as binary.\n"); |
|---|
| 1397 | |
|---|
| 1398 | Â Â c->protocol =Â binary_prot; |
|---|
| 1399 | Â Â reinit_bin_connection(c); |
|---|
| 1400 | Â Â /* Emulate a read of the first byte */ |
|---|
| 1401 | Â Â c->ritem[0]Â =Â c->rbuf[0]; |
|---|
| 1402 | Â Â c->ritem++; |
|---|
| 1403 | Â Â c->rlbytes--; |
|---|
| 1404 | } |
|---|
| 1405 | |
|---|
| 1406 | static void setup_ascii_protocol(conn *c) { |
|---|
| 1407 |   if (settings.verbose > 1) |
|---|
| 1408 |     fprintf(stderr, "Negotiated protocol as ascii.\n"); |
|---|
| 1409 | Â Â c->protocol =Â ascii_prot; |
|---|
| 1410 | |
|---|
| 1411 | Â Â /* We've already got the first letter of the command, so pretend like we |
|---|
| 1412 | Â Â Â * Did a single byte read from try_read_command */ |
|---|
| 1413 | Â Â c->rcurr=c->rbuf; |
|---|
| 1414 | Â Â c->rbytes=1; |
|---|
| 1415 |   conn_set_state(c, conn_read); |
|---|
| 1416 | } |
|---|
| 1417 | |
|---|
| 1418 | static void complete_nread(conn *c) { |
|---|
| 1419 | Â Â assert(c !=Â NULL); |
|---|
| 1420 | |
|---|
| 1421 | Â Â if(c->protocol ==Â ascii_prot)Â { |
|---|
| 1422 | Â Â Â Â complete_nread_ascii(c); |
|---|
| 1423 |   } else if(c->protocol == binary_prot) { |
|---|
| 1424 | Â Â Â Â complete_nread_binary(c); |
|---|
| 1425 |   } else if(c->protocol == negotiating_prot) { |
|---|
| 1426 | Â Â Â Â /* The first byte is either BIN_REQ_MAGIC, or we're speaking ascii */ |
|---|
| 1427 |     if ((c->rbuf[0] & 0xff) == BIN_REQ_MAGIC) |
|---|
| 1428 | Â Â Â Â Â Â setup_bin_protocol(c); |
|---|
| 1429 | Â Â Â Â else |
|---|
| 1430 | Â Â Â Â Â Â setup_ascii_protocol(c); |
|---|
| 1431 |   } else { |
|---|
| 1432 |     assert(0); /* XXX: Invalid case. Should probably do more here. */ |
|---|
| 1433 | Â Â } |
|---|
| 1434 | } |
|---|
| 1435 | |
|---|
| 1436 | /* |
|---|
| 1437 | Â * Stores an item in the cache according to the semantics of one of the set |
|---|
| 1438 | Â * commands. In threaded mode, this is protected by the cache lock. |
|---|
| 1439 | Â * |
|---|
| 1440 | Â * Returns true if the item was stored. |
|---|
| 1441 | Â */ |
|---|
| 1442 | int do_store_item(item *it, int comm) { |
|---|
| 1443 |   char *key = ITEM_key(it); |
|---|
| 1444 | Â Â bool delete_locked =Â false; |
|---|
| 1445 |   item *old_it = do_item_get_notedeleted(key, it->nkey, &delete_locked); |
|---|
| 1446 |   int stored = 0; |
|---|
| 1447 | |
|---|
| 1448 | Â Â item *new_it =Â NULL; |
|---|
| 1449 |   int flags; |
|---|
| 1450 | |
|---|
| 1451 |   if (old_it != NULL && comm == NREAD_ADD) { |
|---|
| 1452 | Â Â Â Â /* add only adds a nonexistent item, but promote to head of LRU */ |
|---|
| 1453 | Â Â Â Â do_item_update(old_it); |
|---|
| 1454 |   } else if (!old_it && (comm == NREAD_REPLACE |
|---|
| 1455 | Â Â Â Â ||Â comm ==Â NREAD_APPEND ||Â comm ==Â NREAD_PREPEND)) |
|---|
| 1456 | Â Â { |
|---|
| 1457 | Â Â Â Â /* replace only replaces an existing value; don't store */ |
|---|
| 1458 |   } else if (delete_locked && (comm == NREAD_REPLACE || comm == NREAD_ADD |
|---|
| 1459 | Â Â Â Â ||Â comm ==Â NREAD_APPEND ||Â comm ==Â NREAD_PREPEND)) |
|---|
| 1460 | Â Â { |
|---|
| 1461 | Â Â Â Â /* replace and add can't override delete locks; don't store */ |
|---|
| 1462 |   } else if (comm == NREAD_CAS) { |
|---|
| 1463 | Â Â Â Â /* validate cas operation */ |
|---|
| 1464 |     if (delete_locked) |
|---|
| 1465 |       old_it = do_item_get_nocheck(key, it->nkey); |
|---|
| 1466 | |
|---|
| 1467 | Â Â Â Â if(old_it ==Â NULL)Â { |
|---|
| 1468 | Â Â Â Â Â // LRU expired |
|---|
| 1469 | Â Â Â Â Â stored =Â 3; |
|---|
| 1470 | Â Â Â Â } |
|---|
| 1471 |     else if(it->cas_id == old_it->cas_id) { |
|---|
| 1472 | Â Â Â Â Â // cas validates |
|---|
| 1473 |      do_item_replace(old_it, it); |
|---|
| 1474 | Â Â Â Â Â stored =Â 1; |
|---|
| 1475 |     } else { |
|---|
| 1476 | Â Â Â Â Â if(settings.verbose >Â 1)Â { |
|---|
| 1477 |       fprintf(stderr, "CAS: failure: expected %llu, got %llu\n", |
|---|
| 1478 |         old_it->cas_id, it->cas_id); |
|---|
| 1479 | Â Â Â Â Â } |
|---|
| 1480 | Â Â Â Â Â stored =Â 2; |
|---|
| 1481 | Â Â Â Â } |
|---|
| 1482 |   } else { |
|---|
| 1483 | Â Â Â Â /* |
|---|
| 1484 | Â Â Â Â Â * Append - combine new and old record into single one. Here it's |
|---|
| 1485 | Â Â Â Â Â * atomic and thread-safe. |
|---|
| 1486 | Â Â Â Â Â */ |
|---|
| 1487 | |
|---|
| 1488 |     if (comm == NREAD_APPEND || comm == NREAD_PREPEND) { |
|---|
| 1489 | |
|---|
| 1490 | Â Â Â Â Â Â /* we have it and old_it here - alloc memory to hold both */ |
|---|
| 1491 | Â Â Â Â Â Â /* flags was already lost - so recover them from ITEM_suffix(it) */ |
|---|
| 1492 | |
|---|
| 1493 |       flags = (int) strtol(ITEM_suffix(old_it), (char **) NULL, 10); |
|---|
| 1494 | |
|---|
| 1495 |       new_it = do_item_alloc(key, it->nkey, flags, old_it->exptime, it->nbytes + old_it->nbytes - 2 /* CRLF */); |
|---|
| 1496 | |
|---|
| 1497 |       if (new_it == NULL) { |
|---|
| 1498 | Â Â Â Â Â Â Â Â /* SERVER_ERROR out of memory */ |
|---|
| 1499 |         return 0; |
|---|
| 1500 | Â Â Â Â Â Â } |
|---|
| 1501 | |
|---|
| 1502 | Â Â Â Â Â Â /* copy data from it and old_it to new_it */ |
|---|
| 1503 | |
|---|
| 1504 |       if (comm == NREAD_APPEND) { |
|---|
| 1505 |         memcpy(ITEM_data(new_it), ITEM_data(old_it), old_it->nbytes); |
|---|
| 1506 |         memcpy(ITEM_data(new_it) + old_it->nbytes - 2 /* CRLF */, ITEM_data(it), it->nbytes); |
|---|
| 1507 |       } else { |
|---|
| 1508 | Â Â Â Â Â Â Â Â /* NREAD_PREPEND */ |
|---|
| 1509 |         memcpy(ITEM_data(new_it), ITEM_data(it), it->nbytes); |
|---|
| 1510 |         memcpy(ITEM_data(new_it) + it->nbytes - 2 /* CRLF */, ITEM_data(old_it), old_it->nbytes); |
|---|
| 1511 | Â Â Â Â Â Â } |
|---|
| 1512 | |
|---|
| 1513 | Â Â Â Â Â Â it =Â new_it; |
|---|
| 1514 | Â Â Â Â } |
|---|
| 1515 | |
|---|
| 1516 | Â Â Â Â /* "set" commands can override the delete lock |
|---|
| 1517 | Â Â Â Â Â Â window... in which case we have to find the old hidden item |
|---|
| 1518 | Â Â Â Â Â Â that's in the namespace/LRU but wasn't returned by |
|---|
| 1519 | Â Â Â Â Â Â item_get.... because we need to replace it */ |
|---|
| 1520 |     if (delete_locked) |
|---|
| 1521 |       old_it = do_item_get_nocheck(key, it->nkey); |
|---|
| 1522 | |
|---|
| 1523 |     if (old_it != NULL) |
|---|
| 1524 |       do_item_replace(old_it, it); |
|---|
| 1525 | Â Â Â Â else |
|---|
| 1526 | Â Â Â Â Â Â do_item_link(it); |
|---|
| 1527 | |
|---|
| 1528 | Â Â Â Â stored =Â 1; |
|---|
| 1529 | Â Â } |
|---|
| 1530 | |
|---|
| 1531 |   if (old_it != NULL) |
|---|
| 1532 | Â Â Â Â do_item_remove(old_it);Â Â Â Â Â /* release our reference */ |
|---|
| 1533 |   if (new_it != NULL) |
|---|
| 1534 | Â Â Â Â do_item_remove(new_it); |
|---|
| 1535 | |
|---|
| 1536 |   return stored; |
|---|
| 1537 | } |
|---|
| 1538 | |
|---|
| 1539 | typedef struct token_s { |
|---|
| 1540 |   char *value; |
|---|
| 1541 |   size_t length; |
|---|
| 1542 | }Â token_t; |
|---|
| 1543 | |
|---|
| 1544 | #define COMMAND_TOKEN 0 |
|---|
| 1545 | #define SUBCOMMAND_TOKEN 1 |
|---|
| 1546 | #define KEY_TOKEN 1 |
|---|
| 1547 | #define KEY_MAX_LENGTH 250 |
|---|
| 1548 | |
|---|
| 1549 | #define MAX_TOKENS 8 |
|---|
| 1550 | |
|---|
| 1551 | /* |
|---|
| 1552 | Â * Tokenize the command string by replacing whitespace with '\0' and update |
|---|
| 1553 | Â * the token array tokens with pointer to start of each token and length. |
|---|
| 1554 |  * Returns total number of tokens. The last valid token is the terminal |
|---|
| 1555 | Â * token (value points to the first unprocessed character of the string and |
|---|
| 1556 | Â * length zero). |
|---|
| 1557 | Â * |
|---|
| 1558 | Â * Usage example: |
|---|
| 1559 | Â * |
|---|
| 1560 | Â *Â while(tokenize_command(command, ncommand, tokens, max_tokens) > 0) { |
|---|
| 1561 | Â *Â Â Â for(int ix = 0; tokens[ix].length != 0; ix++) { |
|---|
| 1562 | Â *Â Â Â Â Â ... |
|---|
| 1563 | Â *Â Â Â } |
|---|
| 1564 | Â *Â Â Â ncommand = tokens[ix].value - command; |
|---|
| 1565 |  *   command = tokens[ix].value; |
|---|
| 1566 | Â *Â Â } |
|---|
| 1567 | Â */ |
|---|
| 1568 | static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) { |
|---|
| 1569 |   char *s, *e; |
|---|
| 1570 |   size_t ntokens = 0; |
|---|
| 1571 | |
|---|
| 1572 | Â Â assert(command !=Â NULLÂ &&Â tokens !=Â NULLÂ &&Â max_tokens >Â 1); |
|---|
| 1573 | |
|---|
| 1574 |   for (s = e = command; ntokens < max_tokens - 1; ++e) { |
|---|
| 1575 |     if (*e == ' ') { |
|---|
| 1576 |       if (s != e) { |
|---|
| 1577 | Â Â Â Â Â Â Â Â tokens[ntokens].value =Â s; |
|---|
| 1578 | Â Â Â Â Â Â Â Â tokens[ntokens].length =Â e -Â s; |
|---|
| 1579 | Â Â Â Â Â Â Â Â ntokens++; |
|---|
| 1580 | Â Â Â Â Â Â Â Â *e =Â '\0'; |
|---|
| 1581 | Â Â Â Â Â Â } |
|---|
| 1582 | Â Â Â Â Â Â s =Â e +Â 1; |
|---|
| 1583 | Â Â Â Â } |
|---|
| 1584 |     else if (*e == '\0') { |
|---|
| 1585 |       if (s != e) { |
|---|
| 1586 | Â Â Â Â Â Â Â Â tokens[ntokens].value =Â s; |
|---|
| 1587 | Â Â Â Â Â Â Â Â tokens[ntokens].length =Â e -Â s; |
|---|
| 1588 | Â Â Â Â Â Â Â Â ntokens++; |
|---|
| 1589 | Â Â Â Â Â Â } |
|---|
| 1590 | |
|---|
| 1591 | Â Â Â Â Â Â break;Â /* string end */ |
|---|
| 1592 | Â Â Â Â } |
|---|
| 1593 | Â Â } |
|---|
| 1594 | |
|---|
| 1595 | Â Â /* |
|---|
| 1596 | Â Â Â * If we scanned the whole string, the terminal value pointer is null, |
|---|
| 1597 | Â Â Â * otherwise it is the first unprocessed character. |
|---|
| 1598 | Â Â Â */ |
|---|
| 1599 |   tokens[ntokens].value = *e == '\0' ? NULL : e; |
|---|
| 1600 | Â Â tokens[ntokens].length =Â 0; |
|---|
| 1601 | Â Â ntokens++; |
|---|
| 1602 | |
|---|
| 1603 |   return ntokens; |
|---|
| 1604 | } |
|---|
| 1605 | |
|---|
| 1606 | /* set up a connection to write a buffer then free it, used for stats */ |
|---|
| 1607 | static void write_and_free(conn *c, char *buf, int bytes) { |
|---|
| 1608 |   if (buf) { |
|---|
| 1609 | Â Â Â Â c->write_and_free =Â buf; |
|---|
| 1610 | Â Â Â Â c->wcurr =Â buf; |
|---|
| 1611 | Â Â Â Â c->wbytes =Â bytes; |
|---|
| 1612 |     conn_set_state(c, conn_write); |
|---|
| 1613 | Â Â Â Â c->write_and_go =Â get_init_state(c); |
|---|
| 1614 |   } else { |
|---|
| 1615 |     out_string(c, "SERVER_ERROR out of memory writing stats"); |
|---|
| 1616 | Â Â } |
|---|
| 1617 | } |
|---|
| 1618 | |
|---|
| 1619 | static inline void set_noreply_maybe(conn *c, token_t *tokens, size_t ntokens) |
|---|
| 1620 | { |
|---|
| 1621 |   int noreply_index = ntokens - 2; |
|---|
| 1622 | |
|---|
| 1623 | Â Â /* |
|---|
| 1624 | Â Â Â NOTE: this function is not the first place where we are going to |
|---|
| 1625 |    send the reply. We could send it instead from process_command() |
|---|
| 1626 |    if the request line has wrong number of tokens. However parsing |
|---|
| 1627 | Â Â Â malformed line for "noreply" option is not reliable anyway, so |
|---|
| 1628 | Â Â Â it can't be helped. |
|---|
| 1629 | Â Â */ |
|---|
| 1630 |   if (tokens[noreply_index].value |
|---|
| 1631 |     && strcmp(tokens[noreply_index].value, "noreply") == 0) { |
|---|
| 1632 | Â Â Â Â c->noreply =Â true; |
|---|
| 1633 | Â Â } |
|---|
| 1634 | } |
|---|
| 1635 | |
|---|
| 1636 | inline static void process_stats_detail(conn *c, const char *command) { |
|---|
| 1637 | Â Â assert(c !=Â NULL); |
|---|
| 1638 | |
|---|
| 1639 |   if (strcmp(command, "on") == 0) { |
|---|
| 1640 | Â Â Â Â settings.detail_enabled =Â 1; |
|---|
| 1641 |     out_string(c, "OK"); |
|---|
| 1642 | Â Â } |
|---|
| 1643 |   else if (strcmp(command, "off") == 0) { |
|---|
| 1644 | Â Â Â Â settings.detail_enabled =Â 0; |
|---|
| 1645 |     out_string(c, "OK"); |
|---|
| 1646 | Â Â } |
|---|
| 1647 |   else if (strcmp(command, "dump") == 0) { |
|---|
| 1648 |     int len; |
|---|
| 1649 |     char *stats = stats_prefix_dump(&len); |
|---|
| 1650 |     write_and_free(c, stats, len); |
|---|
| 1651 | Â Â } |
|---|
| 1652 |   else { |
|---|
| 1653 |     out_string(c, "CLIENT_ERROR usage: stats detail on|off|dump"); |
|---|
| 1654 | Â Â } |
|---|
| 1655 | } |
|---|
| 1656 | |
|---|
| 1657 | static void process_stat(conn *c, token_t *tokens, const size_t ntokens) { |
|---|
| 1658 | Â Â rel_time_t now =Â current_time; |
|---|
| 1659 |   char *command; |
|---|
| 1660 |   char *subcommand; |
|---|
| 1661 | |
|---|
| 1662 | Â Â assert(c !=Â NULL); |
|---|
| 1663 | |
|---|
| 1664 | Â Â if(ntokens <Â 2)Â { |
|---|
| 1665 |     out_string(c, "CLIENT_ERROR bad command line"); |
|---|
| 1666 | Â Â Â Â return; |
|---|
| 1667 | Â Â } |
|---|
| 1668 | |
|---|
| 1669 | Â Â command =Â tokens[COMMAND_TOKEN].value; |
|---|
| 1670 | |
|---|
| 1671 |   if (ntokens == 2 && strcmp(command, "stats") == 0) { |
|---|
| 1672 |     char temp[1024]; |
|---|
| 1673 | Â Â Â Â pid_t pid =Â getpid(); |
|---|
| 1674 |     char *pos = temp; |
|---|
| 1675 | |
|---|
| 1676 | #ifndef WIN32 |
|---|
| 1677 |     struct rusage usage; |
|---|
| 1678 |     getrusage(RUSAGE_SELF, &usage); |
|---|
| 1679 | #endif /* !WIN32 */ |
|---|
| 1680 | |
|---|
| 1681 | Â Â Â Â STATS_LOCK(); |
|---|
| 1682 |     pos += sprintf(pos, "STAT pid %u\r\n", pid); |
|---|
| 1683 |     pos += sprintf(pos, "STAT uptime %u\r\n", now); |
|---|
| 1684 |     pos += sprintf(pos, "STAT time %ld\r\n", now + stats.started); |
|---|
| 1685 |     pos += sprintf(pos, "STAT version " VERSION "\r\n"); |
|---|
| 1686 |     pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void *)); |
|---|
| 1687 | #ifndef WIN32 |
|---|
| 1688 |     pos += sprintf(pos, "STAT rusage_user %ld.%06ld\r\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec); |
|---|
| 1689 |     pos += sprintf(pos, "STAT rusage_system %ld.%06ld\r\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec); |
|---|
| 1690 | #endif /* !WIN32 */ |
|---|
| 1691 |     pos += sprintf(pos, "STAT curr_items %u\r\n", stats.curr_items); |
|---|
| 1692 |     pos += sprintf(pos, "STAT total_items %u\r\n", stats.total_items); |
|---|
| 1693 |     pos += sprintf(pos, "STAT bytes %llu\r\n", stats.curr_bytes); |
|---|
| 1694 |     pos += sprintf(pos, "STAT curr_connections %u\r\n", stats.curr_conns - 1); /* ignore listening conn */ |
|---|
| 1695 |     pos += sprintf(pos, "STAT total_connections %u\r\n", stats.total_conns); |
|---|
| 1696 |     pos += sprintf(pos, "STAT connection_structures %u\r\n", stats.conn_structs); |
|---|
| 1697 |     pos += sprintf(pos, "STAT cmd_get %llu\r\n", stats.get_cmds); |
|---|
| 1698 |     pos += sprintf(pos, "STAT cmd_set %llu\r\n", stats.set_cmds); |
|---|
| 1699 |     pos += sprintf(pos, "STAT get_hits %llu\r\n", stats.get_hits); |
|---|
| 1700 |     pos += sprintf(pos, "STAT get_misses %llu\r\n", stats.get_misses); |
|---|
| 1701 |     pos += sprintf(pos, "STAT evictions %llu\r\n", stats.evictions); |
|---|
| 1702 |     pos += sprintf(pos, "STAT bytes_read %llu\r\n", stats.bytes_read); |
|---|
| 1703 |     pos += sprintf(pos, "STAT bytes_written %llu\r\n", stats.bytes_written); |
|---|
| 1704 |     pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (uint64_t) settings.maxbytes); |
|---|
| 1705 |     pos += sprintf(pos, "STAT threads %u\r\n", settings.num_threads); |
|---|
| 1706 |     pos += sprintf(pos, "END"); |
|---|
| 1707 | Â Â Â Â STATS_UNLOCK(); |
|---|
| 1708 |     out_string(c, temp); |
|---|
| 1709 | Â Â Â Â return; |
|---|
| 1710 | Â Â } |
|---|
| 1711 | |
|---|
| 1712 | Â Â subcommand =Â tokens[SUBCOMMAND_TOKEN].value; |
|---|
| 1713 | |
|---|
| 1714 |   if (strcmp(subcommand, "reset") == 0) { |
|---|
| 1715 | Â Â Â Â stats_reset(); |
|---|
| 1716 |     out_string(c, "RESET"); |
|---|
| 1717 | Â Â Â Â return; |
|---|
| 1718 | Â Â } |
|---|
| 1719 | |
|---|
| 1720 | #ifdef HAVE_MALLOC_H |
|---|
| 1721 | #ifdef HAVE_STRUCT_MALLINFO |
|---|
| 1722 |   if (strcmp(subcommand, "malloc") == 0) { |
|---|
| 1723 |     char temp[512]; |
|---|
| 1724 |     struct mallinfo info; |
|---|
| 1725 |     char *pos = temp; |
|---|
| 1726 | |
|---|
| 1727 | Â Â Â Â info =Â mallinfo(); |
|---|
| 1728 |     pos += sprintf(pos, "STAT arena_size %d\r\n", info.arena); |
|---|
| 1729 |     pos += sprintf(pos, "STAT free_chunks %d\r\n", info.ordblks); |
|---|
| 1730 |     pos += sprintf(pos, "STAT fastbin_blocks %d\r\n", info.smblks); |
|---|
| 1731 |     pos += sprintf(pos, "STAT mmapped_regions %d\r\n", info.hblks); |
|---|
| 1732 |     pos += sprintf(pos, "STAT mmapped_space %d\r\n", info.hblkhd); |
|---|
| 1733 |     pos += sprintf(pos, "STAT max_total_alloc %d\r\n", info.usmblks); |
|---|
| 1734 |     pos += sprintf(pos, "STAT fastbin_space %d\r\n", info.fsmblks); |
|---|
| 1735 |     pos += sprintf(pos, "STAT total_alloc %d\r\n", info.uordblks); |
|---|
| 1736 |     pos += sprintf(pos, "STAT total_free %d\r\n", info.fordblks); |
|---|
| 1737 |     pos += sprintf(pos, "STAT releasable_space %d\r\nEND", info.keepcost); |
|---|
| 1738 |     out_string(c, temp); |
|---|
| 1739 | Â Â Â Â return; |
|---|
| 1740 | Â Â } |
|---|
| 1741 | #endif /* HAVE_STRUCT_MALLINFO */ |
|---|
| 1742 | #endif /* HAVE_MALLOC_H */ |
|---|
| 1743 | |
|---|
| 1744 | #if !defined(WIN32) || !defined(__APPLE__) |
|---|
| 1745 |   if (strcmp(subcommand, "maps") == 0) { |
|---|
| 1746 |     char *wbuf; |
|---|
| 1747 |     int wsize = 8192; /* should be enough */ |
|---|
| 1748 |     int fd; |
|---|
| 1749 |     int res; |
|---|
| 1750 | |
|---|
| 1751 |     if ((wbuf = (char *)malloc(wsize)) == NULL) { |
|---|
| 1752 |       out_string(c, "SERVER_ERROR out of memory writing stats maps"); |
|---|
| 1753 | Â Â Â Â Â Â return; |
|---|
| 1754 | Â Â Â Â } |
|---|
| 1755 | |
|---|
| 1756 |     fd = open("/proc/self/maps", O_RDONLY); |
|---|
| 1757 |     if (fd == -1) { |
|---|
| 1758 |       out_string(c, "SERVER_ERROR cannot open the maps file"); |
|---|
| 1759 | Â Â Â Â Â Â free(wbuf); |
|---|
| 1760 | Â Â Â Â Â Â return; |
|---|
| 1761 | Â Â Â Â } |
|---|
| 1762 | |
|---|
| 1763 |     res = read(fd, wbuf, wsize - 6); /* 6 = END\r\n\0 */ |
|---|
| 1764 |     if (res == wsize - 6) { |
|---|
| 1765 |       out_string(c, "SERVER_ERROR buffer overflow"); |
|---|
| 1766 | Â Â Â Â Â Â free(wbuf);Â close(fd); |
|---|
| 1767 | Â Â Â Â Â Â return; |
|---|
| 1768 | Â Â Â Â } |
|---|
| 1769 |     if (res == 0 || res == -1) { |
|---|
| 1770 |       out_string(c, "SERVER_ERROR can't read the maps file"); |
|---|
| 1771 | Â Â Â Â Â Â free(wbuf);Â close(fd); |
|---|
| 1772 | Â Â Â Â Â Â return; |
|---|
| 1773 | Â Â Â Â } |
|---|
| 1774 |     memcpy(wbuf + res, "END\r\n", 5); |
|---|
| 1775 |     write_and_free(c, wbuf, res + 5); |
|---|
| 1776 | Â Â Â Â close(fd); |
|---|
| 1777 | Â Â Â Â return; |
|---|
| 1778 | Â Â } |
|---|
| 1779 | #endif |
|---|
| 1780 | |
|---|
| 1781 |   if (strcmp(subcommand, "cachedump") == 0) { |
|---|
| 1782 | |
|---|
| 1783 |     char *buf; |
|---|
| 1784 |     unsigned int bytes, id, limit = 0; |
|---|
| 1785 | |
|---|
| 1786 | Â Â Â Â if(ntokens <Â 5)Â { |
|---|
| 1787 |       out_string(c, "CLIENT_ERROR bad command line"); |
|---|
| 1788 | Â Â Â Â Â Â return; |
|---|
| 1789 | Â Â Â Â } |
|---|
| 1790 | |
|---|
| 1791 |     id = strtoul(tokens[2].value, NULL, 10); |
|---|
| 1792 |     limit = strtoul(tokens[3].value, NULL, 10); |
|---|
| 1793 | |
|---|
| 1794 | Â Â Â Â if(errno ==Â ERANGE)Â { |
|---|
| 1795 |       out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 1796 | Â Â Â Â Â Â return; |
|---|
| 1797 | Â Â Â Â } |
|---|
| 1798 | |
|---|
| 1799 |     buf = item_cachedump(id, limit, &bytes); |
|---|
| 1800 |     write_and_free(c, buf, bytes); |
|---|
| 1801 | Â Â Â Â return; |
|---|
| 1802 | Â Â } |
|---|
| 1803 | |
|---|
| 1804 |   if (strcmp(subcommand, "slabs") == 0) { |
|---|
| 1805 |     int bytes = 0; |
|---|
| 1806 |     char *buf = slabs_stats(&bytes); |
|---|
| 1807 |     write_and_free(c, buf, bytes); |
|---|
| 1808 | Â Â Â Â return; |
|---|
| 1809 | Â Â } |
|---|
| 1810 | |
|---|
| 1811 |   if (strcmp(subcommand, "items") == 0) { |
|---|
| 1812 |     int bytes = 0; |
|---|
| 1813 |     char *buf = item_stats(&bytes); |
|---|
| 1814 |     write_and_free(c, buf, bytes); |
|---|
| 1815 | Â Â Â Â return; |
|---|
| 1816 | Â Â } |
|---|
| 1817 | |
|---|
| 1818 |   if (strcmp(subcommand, "detail") == 0) { |
|---|
| 1819 |     if (ntokens < 4) |
|---|
| 1820 |       process_stats_detail(c, ""); /* outputs the error message */ |
|---|
| 1821 | Â Â Â Â else |
|---|
| 1822 |       process_stats_detail(c, tokens[2].value); |
|---|
| 1823 | Â Â Â Â return; |
|---|
| 1824 | Â Â } |
|---|
| 1825 | |
|---|
| 1826 |   if (strcmp(subcommand, "sizes") == 0) { |
|---|
| 1827 |     int bytes = 0; |
|---|
| 1828 |     char *buf = item_stats_sizes(&bytes); |
|---|
| 1829 |     write_and_free(c, buf, bytes); |
|---|
| 1830 | Â Â Â Â return; |
|---|
| 1831 | Â Â } |
|---|
| 1832 | |
|---|
| 1833 |   out_string(c, "ERROR"); |
|---|
| 1834 | } |
|---|
| 1835 | |
|---|
| 1836 | /* ntokens is overwritten here... shrug.. */ |
|---|
| 1837 | static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens, bool return_cas) { |
|---|
| 1838 |   char *key; |
|---|
| 1839 |   size_t nkey; |
|---|
| 1840 |   int i = 0; |
|---|
| 1841 | Â Â item *it; |
|---|
| 1842 | Â Â token_t *key_token =Â &tokens[KEY_TOKEN]; |
|---|
| 1843 |   char *suffix; |
|---|
| 1844 |   int stats_get_cmds  = 0; |
|---|
| 1845 |   int stats_get_hits  = 0; |
|---|
| 1846 |   int stats_get_misses = 0; |
|---|
| 1847 | Â Â assert(c !=Â NULL); |
|---|
| 1848 | |
|---|
| 1849 |   if (settings.managed) { |
|---|
| 1850 |     int bucket = c->bucket; |
|---|
| 1851 |     if (bucket == -1) { |
|---|
| 1852 |       out_string(c, "CLIENT_ERROR no BG data in managed mode"); |
|---|
| 1853 | Â Â Â Â Â Â return; |
|---|
| 1854 | Â Â Â Â } |
|---|
| 1855 | Â Â Â Â c->bucket =Â -1; |
|---|
| 1856 |     if (buckets[bucket] != c->gen) { |
|---|
| 1857 |       out_string(c, "ERROR_NOT_OWNER"); |
|---|
| 1858 | Â Â Â Â Â Â return; |
|---|
| 1859 | Â Â Â Â } |
|---|
| 1860 | Â Â } |
|---|
| 1861 | |
|---|
| 1862 |   do { |
|---|
| 1863 | Â Â Â Â while(key_token->length !=Â 0)Â { |
|---|
| 1864 | |
|---|
| 1865 | Â Â Â Â Â Â key =Â key_token->value; |
|---|
| 1866 | Â Â Â Â Â Â nkey =Â key_token->length; |
|---|
| 1867 | |
|---|
| 1868 | Â Â Â Â Â Â if(nkey >Â KEY_MAX_LENGTH)Â { |
|---|
| 1869 | Â Â Â Â Â Â Â Â STATS_LOCK(); |
|---|
| 1870 |         stats.get_cmds  += stats_get_cmds; |
|---|
| 1871 |         stats.get_hits  += stats_get_hits; |
|---|
| 1872 | Â Â Â Â Â Â Â Â stats.get_misses +=Â stats_get_misses; |
|---|
| 1873 | Â Â Â Â Â Â Â Â STATS_UNLOCK(); |
|---|
| 1874 |         out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 1875 | Â Â Â Â Â Â Â Â return; |
|---|
| 1876 | Â Â Â Â Â Â } |
|---|
| 1877 | |
|---|
| 1878 | Â Â Â Â Â Â stats_get_cmds++; |
|---|
| 1879 |       it = item_get(key, nkey); |
|---|
| 1880 |       if (settings.detail_enabled) { |
|---|
| 1881 |         stats_prefix_record_get(key, NULL != it); |
|---|
| 1882 | Â Â Â Â Â Â } |
|---|
| 1883 |       if (it) { |
|---|
| 1884 |         if (i >= c->isize) { |
|---|
| 1885 |           item **new_list = realloc(c->ilist, sizeof(item *) * c->isize * 2); |
|---|
| 1886 |           if (new_list) { |
|---|
| 1887 | Â Â Â Â Â Â Â Â Â Â Â Â c->isize *=Â 2; |
|---|
| 1888 | Â Â Â Â Â Â Â Â Â Â Â Â c->ilist =Â new_list; |
|---|
| 1889 |           } else break; |
|---|
| 1890 | Â Â Â Â Â Â Â Â } |
|---|
| 1891 | |
|---|
| 1892 | Â Â Â Â Â Â Â Â /* |
|---|
| 1893 | Â Â Â Â Â Â Â Â Â * Construct the response. Each hit adds three elements to the |
|---|
| 1894 | Â Â Â Â Â Â Â Â Â * outgoing data list: |
|---|
| 1895 | Â Â Â Â Â Â Â Â Â *Â Â "VALUE " |
|---|
| 1896 | Â Â Â Â Â Â Â Â Â *Â Â key |
|---|
| 1897 | Â Â Â Â Â Â Â Â Â *Â Â " " + flags + " " + data length + "\r\n" + data (with \r\n) |
|---|
| 1898 | Â Â Â Â Â Â Â Â Â */ |
|---|
| 1899 | |
|---|
| 1900 | Â Â Â Â Â Â Â Â if(return_cas ==Â true) |
|---|
| 1901 | Â Â Â Â Â Â Â Â { |
|---|
| 1902 | Â Â Â Â Â Â Â Â Â /* Goofy mid-flight realloc. */ |
|---|
| 1903 |          if (i >= c->suffixsize) { |
|---|
| 1904 |           char **new_suffix_list = realloc(c->suffixlist, |
|---|
| 1905 |                       sizeof(char *) * c->suffixsize * 2); |
|---|
| 1906 |           if (new_suffix_list) { |
|---|
| 1907 | Â Â Â Â Â Â Â Â Â Â Â c->suffixsize *=Â 2; |
|---|
| 1908 |            c->suffixlist = new_suffix_list; |
|---|
| 1909 |           } else break; |
|---|
| 1910 | Â Â Â Â Â Â Â Â Â } |
|---|
| 1911 | |
|---|
| 1912 | Â Â Â Â Â Â Â Â Â suffix =Â suffix_from_freelist(); |
|---|
| 1913 |          if (suffix == NULL) { |
|---|
| 1914 | Â Â Â Â Â Â Â Â Â Â STATS_LOCK(); |
|---|
| 1915 |           stats.get_cmds  += stats_get_cmds; |
|---|
| 1916 |           stats.get_hits  += stats_get_hits; |
|---|
| 1917 | Â Â Â Â Â Â Â Â Â Â stats.get_misses +=Â stats_get_misses; |
|---|
| 1918 | Â Â Â Â Â Â Â Â Â Â STATS_UNLOCK(); |
|---|
| 1919 |           out_string(c, "SERVER_ERROR out of memory making CAS suffix"); |
|---|
| 1920 | Â Â Â Â Â Â Â Â Â Â return; |
|---|
| 1921 | Â Â Â Â Â Â Â Â Â } |
|---|
| 1922 | Â Â Â Â Â Â Â Â Â *(c->suffixlist +Â i)Â =Â suffix; |
|---|
| 1923 |          sprintf(suffix, " %llu\r\n", it->cas_id); |
|---|
| 1924 |          if (add_iov(c, "VALUE ", 6) != 0 || |
|---|
| 1925 |            add_iov(c, ITEM_key(it), it->nkey) != 0 || |
|---|
| 1926 |            add_iov(c, ITEM_suffix(it), it->nsuffix - 2) != 0 || |
|---|
| 1927 |            add_iov(c, suffix, strlen(suffix)) != 0 || |
|---|
| 1928 |            add_iov(c, ITEM_data(it), it->nbytes) != 0) |
|---|
| 1929 | Â Â Â Â Â Â Â Â Â Â Â { |
|---|
| 1930 | Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 1931 | Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 1932 | Â Â Â Â Â Â Â Â } |
|---|
| 1933 | Â Â Â Â Â Â Â Â else |
|---|
| 1934 | Â Â Â Â Â Â Â Â { |
|---|
| 1935 |          if (add_iov(c, "VALUE ", 6) != 0 || |
|---|
| 1936 |            add_iov(c, ITEM_key(it), it->nkey) != 0 || |
|---|
| 1937 |            add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) != 0) |
|---|
| 1938 | Â Â Â Â Â Â Â Â Â Â Â { |
|---|
| 1939 | Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 1940 | Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 1941 | Â Â Â Â Â Â Â Â } |
|---|
| 1942 | |
|---|
| 1943 | |
|---|
| 1944 |         if (settings.verbose > 1) |
|---|
| 1945 |           fprintf(stderr, ">%d sending key %s\n", c->sfd, ITEM_key(it)); |
|---|
| 1946 | |
|---|
| 1947 | Â Â Â Â Â Â Â Â /* item_get() has incremented it->refcount for us */ |
|---|
| 1948 | Â Â Â Â Â Â Â Â stats_get_hits++; |
|---|
| 1949 | Â Â Â Â Â Â Â Â item_update(it); |
|---|
| 1950 | Â Â Â Â Â Â Â Â *(c->ilist +Â i)Â =Â it; |
|---|
| 1951 | Â Â Â Â Â Â Â Â i++; |
|---|
| 1952 | |
|---|
| 1953 |       } else { |
|---|
| 1954 | Â Â Â Â Â Â Â Â stats_get_misses++; |
|---|
| 1955 | Â Â Â Â Â Â } |
|---|
| 1956 | |
|---|
| 1957 | Â Â Â Â Â Â key_token++; |
|---|
| 1958 | Â Â Â Â } |
|---|
| 1959 | |
|---|
| 1960 | Â Â Â Â /* |
|---|
| 1961 | Â Â Â Â Â * If the command string hasn't been fully processed, get the next set |
|---|
| 1962 | Â Â Â Â Â * of tokens. |
|---|
| 1963 | Â Â Â Â Â */ |
|---|
| 1964 | Â Â Â Â if(key_token->value !=Â NULL)Â { |
|---|
| 1965 |       ntokens = tokenize_command(key_token->value, tokens, MAX_TOKENS); |
|---|
| 1966 | Â Â Â Â Â Â key_token =Â tokens; |
|---|
| 1967 | Â Â Â Â } |
|---|
| 1968 | |
|---|
| 1969 | Â Â }Â while(key_token->value !=Â NULL); |
|---|
| 1970 | |
|---|
| 1971 | Â Â c->icurr =Â c->ilist; |
|---|
| 1972 | Â Â c->ileft =Â i; |
|---|
| 1973 |   if (return_cas) { |
|---|
| 1974 | Â Â Â Â c->suffixcurr =Â c->suffixlist; |
|---|
| 1975 | Â Â Â Â c->suffixleft =Â i; |
|---|
| 1976 | Â Â } |
|---|
| 1977 | |
|---|
| 1978 |   if (settings.verbose > 1) |
|---|
| 1979 |     fprintf(stderr, ">%d END\n", c->sfd); |
|---|
| 1980 | |
|---|
| 1981 | Â Â /* |
|---|
| 1982 | Â Â Â Â If the loop was terminated because of out-of-memory, it is not |
|---|
| 1983 | Â Â Â Â reliable to add END\r\n to the buffer, because it might not end |
|---|
| 1984 | Â Â Â Â in \r\n. So we send SERVER_ERROR instead. |
|---|
| 1985 | Â Â */ |
|---|
| 1986 |   if (key_token->value != NULL || add_iov(c, "END\r\n", 5) != 0 |
|---|
| 1987 | Â Â Â Â ||Â (IS_UDP(c->protocol)Â &&Â build_udp_headers(c)Â !=Â 0))Â { |
|---|
| 1988 |     out_string(c, "SERVER_ERROR out of memory writing get response"); |
|---|
| 1989 | Â Â } |
|---|
| 1990 |   else { |
|---|
| 1991 |     conn_set_state(c, conn_mwrite); |
|---|
| 1992 | Â Â Â Â c->msgcurr =Â 0; |
|---|
| 1993 | Â Â } |
|---|
| 1994 | |
|---|
| 1995 | Â Â STATS_LOCK(); |
|---|
| 1996 |   stats.get_cmds  += stats_get_cmds; |
|---|
| 1997 |   stats.get_hits  += stats_get_hits; |
|---|
| 1998 | Â Â stats.get_misses +=Â stats_get_misses; |
|---|
| 1999 | Â Â STATS_UNLOCK(); |
|---|
| 2000 | |
|---|
| 2001 | Â Â return; |
|---|
| 2002 | } |
|---|
| 2003 | |
|---|
| 2004 | static void process_update_command(conn *c, token_t *tokens, const size_t ntokens, int comm, bool handle_cas) { |
|---|
| 2005 |   char *key; |
|---|
| 2006 |   size_t nkey; |
|---|
| 2007 |   int flags; |
|---|
| 2008 |   time_t exptime; |
|---|
| 2009 |   int vlen, old_vlen; |
|---|
| 2010 |   uint64_t req_cas_id; |
|---|
| 2011 |   item *it, *old_it; |
|---|
| 2012 | |
|---|
| 2013 | Â Â assert(c !=Â NULL); |
|---|
| 2014 | |
|---|
| 2015 |   set_noreply_maybe(c, tokens, ntokens); |
|---|
| 2016 | |
|---|
| 2017 |   if (tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) { |
|---|
| 2018 |     out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 2019 | Â Â Â Â return; |
|---|
| 2020 | Â Â } |
|---|
| 2021 | |
|---|
| 2022 | Â Â key =Â tokens[KEY_TOKEN].value; |
|---|
| 2023 | Â Â nkey =Â tokens[KEY_TOKEN].length; |
|---|
| 2024 | |
|---|
| 2025 |   flags = strtoul(tokens[2].value, NULL, 10); |
|---|
| 2026 |   exptime = strtol(tokens[3].value, NULL, 10); |
|---|
| 2027 |   vlen = strtol(tokens[4].value, NULL, 10); |
|---|
| 2028 | |
|---|
| 2029 | Â Â // does cas value exist? |
|---|
| 2030 | Â Â if(handle_cas) |
|---|
| 2031 | Â Â { |
|---|
| 2032 |    req_cas_id = strtoull(tokens[5].value, NULL, 10); |
|---|
| 2033 | Â Â } |
|---|
| 2034 | |
|---|
| 2035 | Â Â if(errno ==Â ERANGE ||Â ((flags ==Â 0Â ||Â exptime ==Â 0)Â &&Â errno ==Â EINVAL))Â { |
|---|
| 2036 |     out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 2037 | Â Â Â Â return; |
|---|
| 2038 | Â Â } |
|---|
| 2039 | |
|---|
| 2040 |   if (settings.detail_enabled) { |
|---|
| 2041 | Â Â Â Â stats_prefix_record_set(key); |
|---|
| 2042 | Â Â } |
|---|
| 2043 | |
|---|
| 2044 |   if (settings.managed) { |
|---|
| 2045 |     int bucket = c->bucket; |
|---|
| 2046 |     if (bucket == -1) { |
|---|
| 2047 |       out_string(c, "CLIENT_ERROR no BG data in managed mode"); |
|---|
| 2048 | Â Â Â Â Â Â return; |
|---|
| 2049 | Â Â Â Â } |
|---|
| 2050 | Â Â Â Â c->bucket =Â -1; |
|---|
| 2051 |     if (buckets[bucket] != c->gen) { |
|---|
| 2052 |       out_string(c, "ERROR_NOT_OWNER"); |
|---|
| 2053 | Â Â Â Â Â Â return; |
|---|
| 2054 | Â Â Â Â } |
|---|
| 2055 | Â Â } |
|---|
| 2056 | |
|---|
| 2057 |   it = item_alloc(key, nkey, flags, realtime(exptime), vlen+2); |
|---|
| 2058 | |
|---|
| 2059 |   if (it == 0) { |
|---|
| 2060 |     if (! item_size_ok(nkey, flags, vlen + 2)) |
|---|
| 2061 |       out_string(c, "SERVER_ERROR object too large for cache"); |
|---|
| 2062 | Â Â Â Â else |
|---|
| 2063 |       out_string(c, "SERVER_ERROR out of memory storing object"); |
|---|
| 2064 | Â Â Â Â /* swallow the data line */ |
|---|
| 2065 | Â Â Â Â c->write_and_go =Â conn_swallow; |
|---|
| 2066 | Â Â Â Â c->sbytes =Â vlen +Â 2; |
|---|
| 2067 | Â Â Â Â return; |
|---|
| 2068 | Â Â } |
|---|
| 2069 | Â Â if(handle_cas) |
|---|
| 2070 | Â Â Â it->cas_id =Â req_cas_id; |
|---|
| 2071 | |
|---|
| 2072 | Â Â c->item =Â it; |
|---|
| 2073 | Â Â c->ritem =Â ITEM_data(it); |
|---|
| 2074 | Â Â c->rlbytes =Â it->nbytes; |
|---|
| 2075 | Â Â c->item_comm =Â comm; |
|---|
| 2076 |   conn_set_state(c, conn_nread); |
|---|
| 2077 | } |
|---|
| 2078 | |
|---|
| 2079 | static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const bool incr) { |
|---|
| 2080 |   char temp[sizeof("18446744073709551615")]; |
|---|
| 2081 | Â Â item *it; |
|---|
| 2082 |   int64_t delta; |
|---|
| 2083 |   char *key; |
|---|
| 2084 |   size_t nkey; |
|---|
| 2085 | |
|---|
| 2086 | Â Â assert(c !=Â NULL); |
|---|
| 2087 | |
|---|
| 2088 |   set_noreply_maybe(c, tokens, ntokens); |
|---|
| 2089 | |
|---|
| 2090 | Â Â if(tokens[KEY_TOKEN].length >Â KEY_MAX_LENGTH)Â { |
|---|
| 2091 |     out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 2092 | Â Â Â Â return; |
|---|
| 2093 | Â Â } |
|---|
| 2094 | |
|---|
| 2095 | Â Â key =Â tokens[KEY_TOKEN].value; |
|---|
| 2096 | Â Â nkey =Â tokens[KEY_TOKEN].length; |
|---|
| 2097 | |
|---|
| 2098 |   if (settings.managed) { |
|---|
| 2099 |     int bucket = c->bucket; |
|---|
| 2100 |     if (bucket == -1) { |
|---|
| 2101 |       out_string(c, "CLIENT_ERROR no BG data in managed mode"); |
|---|
| 2102 | Â Â Â Â Â Â return; |
|---|
| 2103 | Â Â Â Â } |
|---|
| 2104 | Â Â Â Â c->bucket =Â -1; |
|---|
| 2105 |     if (buckets[bucket] != c->gen) { |
|---|
| 2106 |       out_string(c, "ERROR_NOT_OWNER"); |
|---|
| 2107 | Â Â Â Â Â Â return; |
|---|
| 2108 | Â Â Â Â } |
|---|
| 2109 | Â Â } |
|---|
| 2110 | |
|---|
| 2111 |   delta = strtoll(tokens[2].value, NULL, 10); |
|---|
| 2112 | |
|---|
| 2113 | Â Â if(errno ==Â ERANGE)Â { |
|---|
| 2114 |     out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 2115 | Â Â Â Â return; |
|---|
| 2116 | Â Â } |
|---|
| 2117 | |
|---|
| 2118 |   it = item_get(key, nkey); |
|---|
| 2119 |   if (!it) { |
|---|
| 2120 |     out_string(c, "NOT_FOUND"); |
|---|
| 2121 | Â Â Â Â return; |
|---|
| 2122 | Â Â } |
|---|
| 2123 | |
|---|
| 2124 |   out_string(c, add_delta(it, incr, delta, temp)); |
|---|
| 2125 | Â Â item_remove(it);Â Â Â Â Â /* release our reference */ |
|---|
| 2126 | } |
|---|
| 2127 | |
|---|
| 2128 | /* |
|---|
| 2129 | Â * adds a delta value to a numeric item. |
|---|
| 2130 | Â * |
|---|
| 2131 |  * it  item to adjust |
|---|
| 2132 |  * incr true to increment value, false to decrement |
|---|
| 2133 | Â * delta amount to adjust value by |
|---|
| 2134 |  * buf  buffer for response string |
|---|
| 2135 | Â * |
|---|
| 2136 | Â * returns a response string to send back to the client. |
|---|
| 2137 | Â */ |
|---|
| 2138 | char *do_add_delta(item *it, const bool incr, const int64_t delta, char *buf) { |
|---|
| 2139 |   char *ptr; |
|---|
| 2140 |   int64_t value; |
|---|
| 2141 |   int res; |
|---|
| 2142 | |
|---|
| 2143 | Â Â ptr =Â ITEM_data(it); |
|---|
| 2144 |   while ((*ptr != '\0') && (*ptr < '0' && *ptr > '9')) ptr++;  // BUG: can't be true |
|---|
| 2145 | |
|---|
| 2146 |   value = strtoull(ptr, NULL, 10); |
|---|
| 2147 | |
|---|
| 2148 | Â Â if(errno ==Â ERANGE)Â { |
|---|
| 2149 |     return "CLIENT_ERROR cannot increment or decrement non-numeric value"; |
|---|
| 2150 | Â Â } |
|---|
| 2151 | |
|---|
| 2152 |   if (incr) |
|---|
| 2153 | Â Â Â Â value +=Â delta; |
|---|
| 2154 |   else { |
|---|
| 2155 | Â Â Â Â value -=Â delta; |
|---|
| 2156 | Â Â } |
|---|
| 2157 | Â Â if(value <Â 0)Â { |
|---|
| 2158 | Â Â Â Â value=0; |
|---|
| 2159 | Â Â } |
|---|
| 2160 |   sprintf(buf, "%llu", value); |
|---|
| 2161 | Â Â res =Â strlen(buf); |
|---|
| 2162 |   if (res + 2 > it->nbytes) { /* need to realloc */ |
|---|
| 2163 | Â Â Â Â item *new_it; |
|---|
| 2164 |     new_it = do_item_alloc(ITEM_key(it), it->nkey, atoi(ITEM_suffix(it) + 1), it->exptime, res + 2 ); |
|---|
| 2165 |     if (new_it == 0) { |
|---|
| 2166 |       return "SERVER_ERROR out of memory in incr/decr"; |
|---|
| 2167 | Â Â Â Â } |
|---|
| 2168 |     memcpy(ITEM_data(new_it), buf, res); |
|---|
| 2169 |     memcpy(ITEM_data(new_it) + res, "\r\n", 3); |
|---|
| 2170 |     do_item_replace(it, new_it); |
|---|
| 2171 | Â Â Â Â do_item_remove(new_it);Â Â Â Â /* release our reference */ |
|---|
| 2172 |   } else { /* replace in-place */ |
|---|
| 2173 |     memcpy(ITEM_data(it), buf, res); |
|---|
| 2174 |     memset(ITEM_data(it) + res, ' ', it->nbytes - res - 2); |
|---|
| 2175 | Â Â } |
|---|
| 2176 | |
|---|
| 2177 |   return buf; |
|---|
| 2178 | } |
|---|
| 2179 | |
|---|
| 2180 | static void process_delete_command(conn *c, token_t *tokens, const size_t ntokens) { |
|---|
| 2181 |   char *key; |
|---|
| 2182 |   size_t nkey; |
|---|
| 2183 | Â Â item *it; |
|---|
| 2184 |   time_t exptime = 0; |
|---|
| 2185 | |
|---|
| 2186 | Â Â assert(c !=Â NULL); |
|---|
| 2187 | |
|---|
| 2188 |   set_noreply_maybe(c, tokens, ntokens); |
|---|
| 2189 | |
|---|
| 2190 |   if (settings.managed) { |
|---|
| 2191 |     int bucket = c->bucket; |
|---|
| 2192 |     if (bucket == -1) { |
|---|
| 2193 |       out_string(c, "CLIENT_ERROR no BG data in managed mode"); |
|---|
| 2194 | Â Â Â Â Â Â return; |
|---|
| 2195 | Â Â Â Â } |
|---|
| 2196 | Â Â Â Â c->bucket =Â -1; |
|---|
| 2197 |     if (buckets[bucket] != c->gen) { |
|---|
| 2198 |       out_string(c, "ERROR_NOT_OWNER"); |
|---|
| 2199 | Â Â Â Â Â Â return; |
|---|
| 2200 | Â Â Â Â } |
|---|
| 2201 | Â Â } |
|---|
| 2202 | |
|---|
| 2203 | Â Â key =Â tokens[KEY_TOKEN].value; |
|---|
| 2204 | Â Â nkey =Â tokens[KEY_TOKEN].length; |
|---|
| 2205 | |
|---|
| 2206 | Â Â if(nkey >Â KEY_MAX_LENGTH)Â { |
|---|
| 2207 |     out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 2208 | Â Â Â Â return; |
|---|
| 2209 | Â Â } |
|---|
| 2210 | |
|---|
| 2211 |   if(ntokens == (c->noreply ? 5 : 4)) { |
|---|
| 2212 |     exptime = strtol(tokens[2].value, NULL, 10); |
|---|
| 2213 | |
|---|
| 2214 | Â Â Â Â if(errno ==Â ERANGE)Â { |
|---|
| 2215 |       out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 2216 | Â Â Â Â Â Â return; |
|---|
| 2217 | Â Â Â Â } |
|---|
| 2218 | Â Â } |
|---|
| 2219 | |
|---|
| 2220 |   if (settings.detail_enabled) { |
|---|
| 2221 | Â Â Â Â stats_prefix_record_delete(key); |
|---|
| 2222 | Â Â } |
|---|
| 2223 | |
|---|
| 2224 |   it = item_get(key, nkey); |
|---|
| 2225 |   if (it) { |
|---|
| 2226 |     if (exptime == 0) { |
|---|
| 2227 | Â Â Â Â Â Â item_unlink(it); |
|---|
| 2228 | Â Â Â Â Â Â item_remove(it);Â Â Â /* release our reference */ |
|---|
| 2229 |       out_string(c, "DELETED"); |
|---|
| 2230 |     } else { |
|---|
| 2231 | Â Â Â Â Â Â /* our reference will be transfered to the delete queue */ |
|---|
| 2232 |       out_string(c, defer_delete(it, exptime)); |
|---|
| 2233 | Â Â Â Â } |
|---|
| 2234 |   } else { |
|---|
| 2235 |     out_string(c, "NOT_FOUND"); |
|---|
| 2236 | Â Â } |
|---|
| 2237 | } |
|---|
| 2238 | |
|---|
| 2239 | /* |
|---|
| 2240 | Â * Adds an item to the deferred-delete list so it can be reaped later. |
|---|
| 2241 | Â * |
|---|
| 2242 | Â * Returns the result to send to the client. |
|---|
| 2243 | Â */ |
|---|
| 2244 | char *do_defer_delete(item *it, time_t exptime) |
|---|
| 2245 | { |
|---|
| 2246 |   if (delcurr >= deltotal) { |
|---|
| 2247 |     item **new_delete = realloc(todelete, sizeof(item *) * deltotal * 2); |
|---|
| 2248 |     if (new_delete) { |
|---|
| 2249 | Â Â Â Â Â Â todelete =Â new_delete; |
|---|
| 2250 | Â Â Â Â Â Â deltotal *=Â 2; |
|---|
| 2251 |     } else { |
|---|
| 2252 | Â Â Â Â Â Â /* |
|---|
| 2253 | Â Â Â Â Â Â Â * can't delete it immediately, user wants a delay, |
|---|
| 2254 | Â Â Â Â Â Â Â * but we ran out of memory for the delete queue |
|---|
| 2255 | Â Â Â Â Â Â Â */ |
|---|
| 2256 | Â Â Â Â Â Â item_remove(it);Â Â /* release reference */ |
|---|
| 2257 |       return "SERVER_ERROR out of memory expanding delete queue"; |
|---|
| 2258 | Â Â Â Â } |
|---|
| 2259 | Â Â } |
|---|
| 2260 | |
|---|
| 2261 | Â Â /* use its expiration time as its deletion time now */ |
|---|
| 2262 | Â Â it->exptime =Â realtime(exptime); |
|---|
| 2263 | Â Â it->it_flags |=Â ITEM_DELETED; |
|---|
| 2264 | Â Â todelete[delcurr++]Â =Â it; |
|---|
| 2265 | |
|---|
| 2266 |   return "DELETED"; |
|---|
| 2267 | } |
|---|
| 2268 | |
|---|
| 2269 | static void process_verbosity_command(conn *c, token_t *tokens, const size_t ntokens) { |
|---|
| 2270 |   unsigned int level; |
|---|
| 2271 | |
|---|
| 2272 | Â Â assert(c !=Â NULL); |
|---|
| 2273 | |
|---|
| 2274 |   set_noreply_maybe(c, tokens, ntokens); |
|---|
| 2275 | |
|---|
| 2276 |   level = strtoul(tokens[1].value, NULL, 10); |
|---|
| 2277 |   settings.verbose = level > MAX_VERBOSITY_LEVEL ? MAX_VERBOSITY_LEVEL : level; |
|---|
| 2278 |   out_string(c, "OK"); |
|---|
| 2279 | Â Â return; |
|---|
| 2280 | } |
|---|
| 2281 | |
|---|
| 2282 | static void process_command(conn *c, char *command) { |
|---|
| 2283 | |
|---|
| 2284 | Â Â token_t tokens[MAX_TOKENS]; |
|---|
| 2285 |   size_t ntokens; |
|---|
| 2286 |   int comm; |
|---|
| 2287 | |
|---|
| 2288 | Â Â assert(c !=Â NULL); |
|---|
| 2289 | |
|---|
| 2290 |   if (settings.verbose > 1) |
|---|
| 2291 |     fprintf(stderr, "<%d %s\n", c->sfd, command); |
|---|
| 2292 | |
|---|
| 2293 | Â Â /* |
|---|
| 2294 | Â Â Â * for commands set/add/replace, we build an item and read the data |
|---|
| 2295 | Â Â Â * directly into it, then continue in nread_complete(). |
|---|
| 2296 | Â Â Â */ |
|---|
| 2297 | |
|---|
| 2298 | Â Â c->msgcurr =Â 0; |
|---|
| 2299 | Â Â c->msgused =Â 0; |
|---|
| 2300 | Â Â c->iovused =Â 0; |
|---|
| 2301 |   if (add_msghdr(c) != 0) { |
|---|
| 2302 |     out_string(c, "SERVER_ERROR out of memory preparing response"); |
|---|
| 2303 | Â Â Â Â return; |
|---|
| 2304 | Â Â } |
|---|
| 2305 | |
|---|
| 2306 |   ntokens = tokenize_command(command, tokens, MAX_TOKENS); |
|---|
| 2307 |   if (ntokens >= 3 && |
|---|
| 2308 |     ((strcmp(tokens[COMMAND_TOKEN].value, "get") == 0) || |
|---|
| 2309 |      (strcmp(tokens[COMMAND_TOKEN].value, "bget") == 0))) { |
|---|
| 2310 | |
|---|
| 2311 |     process_get_command(c, tokens, ntokens, false); |
|---|
| 2312 | |
|---|
| 2313 |   } else if ((ntokens == 6 || ntokens == 7) && |
|---|
| 2314 |         ((strcmp(tokens[COMMAND_TOKEN].value, "add") == 0 && (comm = NREAD_ADD)) || |
|---|
| 2315 |         (strcmp(tokens[COMMAND_TOKEN].value, "set") == 0 && (comm = NREAD_SET)) || |
|---|
| 2316 |         (strcmp(tokens[COMMAND_TOKEN].value, "replace") == 0 && (comm = NREAD_REPLACE)) || |
|---|
| 2317 |         (strcmp(tokens[COMMAND_TOKEN].value, "prepend") == 0 && (comm = NREAD_PREPEND)) || |
|---|
| 2318 |         (strcmp(tokens[COMMAND_TOKEN].value, "append") == 0 && (comm = NREAD_APPEND)) )) { |
|---|
| 2319 | |
|---|
| 2320 |     process_update_command(c, tokens, ntokens, comm, false); |
|---|
| 2321 | |
|---|
| 2322 |   } else if ((ntokens == 7 || ntokens == 8) && (strcmp(tokens[COMMAND_TOKEN].value, "cas") == 0 && (comm = NREAD_CAS))) { |
|---|
| 2323 | |
|---|
| 2324 |     process_update_command(c, tokens, ntokens, comm, true); |
|---|
| 2325 | |
|---|
| 2326 |   } else if ((ntokens == 4 || ntokens == 5) && (strcmp(tokens[COMMAND_TOKEN].value, "incr") == 0)) { |
|---|
| 2327 | |
|---|
| 2328 |     process_arithmetic_command(c, tokens, ntokens, 1); |
|---|
| 2329 | |
|---|
| 2330 |   } else if (ntokens >= 3 && (strcmp(tokens[COMMAND_TOKEN].value, "gets") == 0)) { |
|---|
| 2331 | |
|---|
| 2332 |     process_get_command(c, tokens, ntokens, true); |
|---|
| 2333 | |
|---|
| 2334 |   } else if ((ntokens == 4 || ntokens == 5) && (strcmp(tokens[COMMAND_TOKEN].value, "decr") == 0)) { |
|---|
| 2335 | |
|---|
| 2336 |     process_arithmetic_command(c, tokens, ntokens, 0); |
|---|
| 2337 | |
|---|
| 2338 |   } else if (ntokens >= 3 && ntokens <= 5 && (strcmp(tokens[COMMAND_TOKEN].value, "delete") == 0)) { |
|---|
| 2339 | |
|---|
| 2340 |     process_delete_command(c, tokens, ntokens); |
|---|
| 2341 | |
|---|
| 2342 |   } else if (ntokens == 3 && strcmp(tokens[COMMAND_TOKEN].value, "own") == 0) { |
|---|
| 2343 |     unsigned int bucket, gen; |
|---|
| 2344 |     if (!settings.managed) { |
|---|
| 2345 |       out_string(c, "CLIENT_ERROR not a managed instance"); |
|---|
| 2346 | Â Â Â Â Â Â return; |
|---|
| 2347 | Â Â Â Â } |
|---|
| 2348 | |
|---|
| 2349 |     if (sscanf(tokens[1].value, "%u:%u", &bucket,&gen) == 2) { |
|---|
| 2350 |       if ((bucket < 0) || (bucket >= MAX_BUCKETS)) { |
|---|
| 2351 |         out_string(c, "CLIENT_ERROR bucket number out of range"); |
|---|
| 2352 | Â Â Â Â Â Â Â Â return; |
|---|
| 2353 | Â Â Â Â Â Â } |
|---|
| 2354 | Â Â Â Â Â Â buckets[bucket]Â =Â gen; |
|---|
| 2355 |       out_string(c, "OWNED"); |
|---|
| 2356 | Â Â Â Â Â Â return; |
|---|
| 2357 |     } else { |
|---|
| 2358 |       out_string(c, "CLIENT_ERROR bad format"); |
|---|
| 2359 | Â Â Â Â Â Â return; |
|---|
| 2360 | Â Â Â Â } |
|---|
| 2361 | |
|---|
| 2362 |   } else if (ntokens == 3 && (strcmp(tokens[COMMAND_TOKEN].value, "disown")) == 0) { |
|---|
| 2363 | |
|---|
| 2364 |     int bucket; |
|---|
| 2365 |     if (!settings.managed) { |
|---|
| 2366 |       out_string(c, "CLIENT_ERROR not a managed instance"); |
|---|
| 2367 | Â Â Â Â Â Â return; |
|---|
| 2368 | Â Â Â Â } |
|---|
| 2369 |     if (sscanf(tokens[1].value, "%u", &bucket) == 1) { |
|---|
| 2370 |       if ((bucket < 0) || (bucket >= MAX_BUCKETS)) { |
|---|
| 2371 |         out_string(c, "CLIENT_ERROR bucket number out of range"); |
|---|
| 2372 | Â Â Â Â Â Â Â Â return; |
|---|
| 2373 | Â Â Â Â Â Â } |
|---|
| 2374 | Â Â Â Â Â Â buckets[bucket]Â =Â 0; |
|---|
| 2375 |       out_string(c, "DISOWNED"); |
|---|
| 2376 | Â Â Â Â Â Â return; |
|---|
| 2377 |     } else { |
|---|
| 2378 |       out_string(c, "CLIENT_ERROR bad format"); |
|---|
| 2379 | Â Â Â Â Â Â return; |
|---|
| 2380 | Â Â Â Â } |
|---|
| 2381 | |
|---|
| 2382 |   } else if (ntokens == 3 && (strcmp(tokens[COMMAND_TOKEN].value, "bg")) == 0) { |
|---|
| 2383 |     int bucket, gen; |
|---|
| 2384 |     if (!settings.managed) { |
|---|
| 2385 |       out_string(c, "CLIENT_ERROR not a managed instance"); |
|---|
| 2386 | Â Â Â Â Â Â return; |
|---|
| 2387 | Â Â Â Â } |
|---|
| 2388 |     if (sscanf(tokens[1].value, "%u:%u", &bucket, &gen) == 2) { |
|---|
| 2389 | Â Â Â Â Â Â /* we never write anything back, even if input's wrong */ |
|---|
| 2390 |       if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen <= 0)) { |
|---|
| 2391 | Â Â Â Â Â Â Â Â /* do nothing, bad input */ |
|---|
| 2392 |       } else { |
|---|
| 2393 | Â Â Â Â Â Â Â Â c->bucket =Â bucket; |
|---|
| 2394 | Â Â Â Â Â Â Â Â c->gen =Â gen; |
|---|
| 2395 | Â Â Â Â Â Â } |
|---|
| 2396 | Â Â Â Â Â Â conn_set_init_state(c); |
|---|
| 2397 | Â Â Â Â Â Â return; |
|---|
| 2398 |     } else { |
|---|
| 2399 |       out_string(c, "CLIENT_ERROR bad format"); |
|---|
| 2400 | Â Â Â Â Â Â return; |
|---|
| 2401 | Â Â Â Â } |
|---|
| 2402 | |
|---|
| 2403 |   } else if (ntokens >= 2 && (strcmp(tokens[COMMAND_TOKEN].value, "stats") == 0)) { |
|---|
| 2404 | |
|---|
| 2405 |     process_stat(c, tokens, ntokens); |
|---|
| 2406 | |
|---|
| 2407 |   } else if (ntokens >= 2 && ntokens <= 4 && (strcmp(tokens[COMMAND_TOKEN].value, "flush_all") == 0)) { |
|---|
| 2408 |     time_t exptime = 0; |
|---|
| 2409 | Â Â Â Â set_current_time(); |
|---|
| 2410 | |
|---|
| 2411 |     set_noreply_maybe(c, tokens, ntokens); |
|---|
| 2412 | |
|---|
| 2413 |     if(ntokens == (c->noreply ? 3 : 2)) { |
|---|
| 2414 | Â Â Â Â Â Â settings.oldest_live =Â current_time -Â 1; |
|---|
| 2415 | Â Â Â Â Â Â item_flush_expired(); |
|---|
| 2416 |       out_string(c, "OK"); |
|---|
| 2417 | Â Â Â Â Â Â return; |
|---|
| 2418 | Â Â Â Â } |
|---|
| 2419 | |
|---|
| 2420 |     exptime = strtol(tokens[1].value, NULL, 10); |
|---|
| 2421 | Â Â Â Â if(errno ==Â ERANGE)Â { |
|---|
| 2422 |       out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 2423 | Â Â Â Â Â Â return; |
|---|
| 2424 | Â Â Â Â } |
|---|
| 2425 | |
|---|
| 2426 | Â Â Â Â /* |
|---|
| 2427 | Â Â Â Â Â If exptime is zero realtime() would return zero too, and |
|---|
| 2428 | Â Â Â Â Â realtime(exptime) - 1 would overflow to the max unsigned |
|---|
| 2429 |      value. So we process exptime == 0 the same way we do when |
|---|
| 2430 | Â Â Â Â Â no delay is given at all. |
|---|
| 2431 | Â Â Â Â */ |
|---|
| 2432 |     if (exptime > 0) |
|---|
| 2433 | Â Â Â Â Â Â settings.oldest_live =Â realtime(exptime)Â -Â 1; |
|---|
| 2434 |     else /* exptime == 0 */ |
|---|
| 2435 | Â Â Â Â Â Â settings.oldest_live =Â current_time -Â 1; |
|---|
| 2436 | Â Â Â Â item_flush_expired(); |
|---|
| 2437 |     out_string(c, "OK"); |
|---|
| 2438 | Â Â Â Â return; |
|---|
| 2439 | |
|---|
| 2440 |   } else if (ntokens == 2 && (strcmp(tokens[COMMAND_TOKEN].value, "version") == 0)) { |
|---|
| 2441 | |
|---|
| 2442 |     out_string(c, "VERSION " VERSION); |
|---|
| 2443 | |
|---|
| 2444 |   } else if (ntokens == 2 && (strcmp(tokens[COMMAND_TOKEN].value, "quit") == 0)) { |
|---|
| 2445 | |
|---|
| 2446 |     conn_set_state(c, conn_closing); |
|---|
| 2447 | |
|---|
| 2448 |   } else if (ntokens == 5 && (strcmp(tokens[COMMAND_TOKEN].value, "slabs") == 0 && |
|---|
| 2449 |                 strcmp(tokens[COMMAND_TOKEN + 1].value, "reassign") == 0)) { |
|---|
| 2450 | #ifdef ALLOW_SLABS_REASSIGN |
|---|
| 2451 | |
|---|
| 2452 |     int src, dst, rv; |
|---|
| 2453 | |
|---|
| 2454 |     src = strtol(tokens[2].value, NULL, 10); |
|---|
| 2455 |     dst = strtol(tokens[3].value, NULL, 10); |
|---|
| 2456 | |
|---|
| 2457 | Â Â Â Â if(errno ==Â ERANGE)Â { |
|---|
| 2458 |       out_string(c, "CLIENT_ERROR bad command line format"); |
|---|
| 2459 | Â Â Â Â Â Â return; |
|---|
| 2460 | Â Â Â Â } |
|---|
| 2461 | |
|---|
| 2462 |     rv = slabs_reassign(src, dst); |
|---|
| 2463 |     if (rv == 1) { |
|---|
| 2464 |       out_string(c, "DONE"); |
|---|
| 2465 | Â Â Â Â Â Â return; |
|---|
| 2466 | Â Â Â Â } |
|---|
| 2467 |     if (rv == 0) { |
|---|
| 2468 |       out_string(c, "CANT"); |
|---|
| 2469 | Â Â Â Â Â Â return; |
|---|
| 2470 | Â Â Â Â } |
|---|
| 2471 |     if (rv == -1) { |
|---|
| 2472 |       out_string(c, "BUSY"); |
|---|
| 2473 | Â Â Â Â Â Â return; |
|---|
| 2474 | Â Â Â Â } |
|---|
| 2475 | #else |
|---|
| 2476 |     out_string(c, "CLIENT_ERROR Slab reassignment not supported"); |
|---|
| 2477 | #endif |
|---|
| 2478 |   } else if ((ntokens == 3 || ntokens == 4) && (strcmp(tokens[COMMAND_TOKEN].value, "verbosity") == 0)) { |
|---|
| 2479 |     process_verbosity_command(c, tokens, ntokens); |
|---|
| 2480 |   } else { |
|---|
| 2481 |     out_string(c, "ERROR"); |
|---|
| 2482 | Â Â } |
|---|
| 2483 | Â Â return; |
|---|
| 2484 | } |
|---|
| 2485 | |
|---|
| 2486 | /* |
|---|
| 2487 | Â * if we have a complete line in the buffer, process it. |
|---|
| 2488 | Â */ |
|---|
| 2489 | static int try_read_command(conn *c) { |
|---|
| 2490 |   char *el, *cont; |
|---|
| 2491 | |
|---|
| 2492 | Â Â assert(c !=Â NULL); |
|---|
| 2493 | Â Â assert(c->rcurr <=Â (c->rbuf +Â c->rsize)); |
|---|
| 2494 | |
|---|
| 2495 |   if (c->rbytes == 0) |
|---|
| 2496 |     return 0; |
|---|
| 2497 |   el = memchr(c->rcurr, '\n', c->rbytes); |
|---|
| 2498 |   if (!el) |
|---|
| 2499 |     return 0; |
|---|
| 2500 | Â Â cont =Â el +Â 1; |
|---|
| 2501 |   if ((el - c->rcurr) > 1 && *(el - 1) == '\r') { |
|---|
| 2502 | Â Â Â Â el--; |
|---|
| 2503 | Â Â } |
|---|
| 2504 | Â Â *el =Â '\0'; |
|---|
| 2505 | |
|---|
| 2506 | Â Â assert(cont <=Â (c->rcurr +Â c->rbytes)); |
|---|
| 2507 | |
|---|
| 2508 |   process_command(c, c->rcurr); |
|---|
| 2509 | |
|---|
| 2510 | Â Â c->rbytes -=Â (cont -Â c->rcurr); |
|---|
| 2511 | Â Â c->rcurr =Â cont; |
|---|
| 2512 | |
|---|
| 2513 | Â Â assert(c->rcurr <=Â (c->rbuf +Â c->rsize)); |
|---|
| 2514 | |
|---|
| 2515 |   return 1; |
|---|
| 2516 | } |
|---|
| 2517 | |
|---|
| 2518 | /* |
|---|
| 2519 | Â * read a UDP request. |
|---|
| 2520 | Â * return 0 if there's nothing to read. |
|---|
| 2521 | Â */ |
|---|
| 2522 | static int try_read_udp(conn *c) { |
|---|
| 2523 |   int res; |
|---|
| 2524 | |
|---|
| 2525 | Â Â assert(c !=Â NULL); |
|---|
| 2526 | |
|---|
| 2527 | Â Â c->request_addr_size =Â sizeof(c->request_addr); |
|---|
| 2528 |   res = recvfrom(c->sfd, c->rbuf, c->rsize, |
|---|
| 2529 |           0, &c->request_addr, &c->request_addr_size); |
|---|
| 2530 |   if (res > 8) { |
|---|
| 2531 |     unsigned char *buf = (unsigned char *)c->rbuf; |
|---|
| 2532 | Â Â Â Â STATS_LOCK(); |
|---|
| 2533 | Â Â Â Â stats.bytes_read +=Â res; |
|---|
| 2534 | Â Â Â Â STATS_UNLOCK(); |
|---|
| 2535 | |
|---|
| 2536 | Â Â Â Â /* Beginning of UDP packet is the request ID; save it. */ |
|---|
| 2537 | Â Â Â Â c->request_id =Â buf[0]Â *Â 256Â +Â buf[1]; |
|---|
| 2538 | |
|---|
| 2539 | Â Â Â Â /* If this is a multi-packet request, drop it. */ |
|---|
| 2540 |     if (buf[4] != 0 || buf[5] != 1) { |
|---|
| 2541 |       out_string(c, "SERVER_ERROR multi-packet request not supported"); |
|---|
| 2542 |       return 0; |
|---|
| 2543 | Â Â Â Â } |
|---|
| 2544 | |
|---|
| 2545 | Â Â Â Â /* Don't care about any of the rest of the header. */ |
|---|
| 2546 | Â Â Â Â res -=Â 8; |
|---|
| 2547 |     memmove(c->rbuf, c->rbuf + 8, res); |
|---|
| 2548 | |
|---|
| 2549 | Â Â Â Â c->rbytes +=Â res; |
|---|
| 2550 | Â Â Â Â c->rcurr =Â c->rbuf; |
|---|
| 2551 |     return 1; |
|---|
| 2552 | Â Â } |
|---|
| 2553 |   return 0; |
|---|
| 2554 | } |
|---|
| 2555 | |
|---|
| 2556 | /* |
|---|
| 2557 | Â * read from network as much as we can, handle buffer overflow and connection |
|---|
| 2558 | Â * close. |
|---|
| 2559 | Â * before reading, move the remaining incomplete fragment of a command |
|---|
| 2560 | Â * (if any) to the beginning of the buffer. |
|---|
| 2561 | Â * return 0 if there's nothing to read on the first read. |
|---|
| 2562 | Â */ |
|---|
| 2563 | static int try_read_network(conn *c) { |
|---|
| 2564 |   int gotdata = 0; |
|---|
| 2565 |   int res; |
|---|
| 2566 | |
|---|
| 2567 | Â Â assert(c !=Â NULL); |
|---|
| 2568 | |
|---|
| 2569 |   if (c->rcurr != c->rbuf) { |
|---|
| 2570 |     if (c->rbytes != 0) /* otherwise there's nothing to copy */ |
|---|
| 2571 |       memmove(c->rbuf, c->rcurr, c->rbytes); |
|---|
| 2572 | Â Â Â Â c->rcurr =Â c->rbuf; |
|---|
| 2573 | Â Â } |
|---|
| 2574 | |
|---|
| 2575 |   while (1) { |
|---|
| 2576 |     if (c->rbytes >= c->rsize) { |
|---|
| 2577 |       char *new_rbuf = realloc(c->rbuf, c->rsize * 2); |
|---|
| 2578 |       if (!new_rbuf) { |
|---|
| 2579 |         if (settings.verbose > 0) |
|---|
| 2580 |           fprintf(stderr, "Couldn't realloc input buffer\n"); |
|---|
| 2581 | Â Â Â Â Â Â Â Â c->rbytes =Â 0;Â /* ignore what we read */ |
|---|
| 2582 |         out_string(c, "SERVER_ERROR out of memory reading request"); |
|---|
| 2583 | Â Â Â Â Â Â Â Â c->write_and_go =Â conn_closing; |
|---|
| 2584 |         return 1; |
|---|
| 2585 | Â Â Â Â Â Â } |
|---|
| 2586 | Â Â Â Â Â Â c->rcurr =Â c->rbuf =Â new_rbuf; |
|---|
| 2587 | Â Â Â Â Â Â c->rsize *=Â 2; |
|---|
| 2588 | Â Â Â Â } |
|---|
| 2589 | |
|---|
| 2590 |     int avail = c->rsize - c->rbytes; |
|---|
| 2591 |     res = read(c->sfd, c->rbuf + c->rbytes, avail); |
|---|
| 2592 |     if (res > 0) { |
|---|
| 2593 | Â Â Â Â Â Â STATS_LOCK(); |
|---|
| 2594 | Â Â Â Â Â Â stats.bytes_read +=Â res; |
|---|
| 2595 | Â Â Â Â Â Â STATS_UNLOCK(); |
|---|
| 2596 | Â Â Â Â Â Â gotdata =Â 1; |
|---|
| 2597 | Â Â Â Â Â Â c->rbytes +=Â res; |
|---|
| 2598 |       if (res == avail) { |
|---|
| 2599 | Â Â Â Â Â Â Â Â continue; |
|---|
| 2600 |       } else { |
|---|
| 2601 | Â Â Â Â Â Â Â Â break; |
|---|
| 2602 | Â Â Â Â Â Â } |
|---|
| 2603 | Â Â Â Â } |
|---|
| 2604 |     if (res == 0) { |
|---|
| 2605 | Â Â Â Â Â Â /* connection closed */ |
|---|
| 2606 |       conn_set_state(c, conn_closing); |
|---|
| 2607 |       return 1; |
|---|
| 2608 | Â Â Â Â } |
|---|
| 2609 |     if (res == -1) { |
|---|
| 2610 |       if (errno == EAGAIN || errno == EWOULDBLOCK) break; |
|---|
| 2611 | Â Â Â Â Â Â /* Should close on unhandled errors. */ |
|---|
| 2612 |       conn_set_state(c, conn_closing); |
|---|
| 2613 |       return 1; |
|---|
| 2614 | Â Â Â Â } |
|---|
| 2615 | Â Â } |
|---|
| 2616 |   return gotdata; |
|---|
| 2617 | } |
|---|
| 2618 | |
|---|
| 2619 | static bool update_event(conn *c, const int new_flags) { |
|---|
| 2620 | Â Â assert(c !=Â NULL); |
|---|
| 2621 | |
|---|
| 2622 |   struct event_base *base = c->event.ev_base; |
|---|
| 2623 |   if (c->ev_flags == new_flags) |
|---|
| 2624 |     return true; |
|---|
| 2625 |   if (event_del(&c->event) == -1) return false; |
|---|
| 2626 |   event_set(&c->event, c->sfd, new_flags, event_handler, (void *)c); |
|---|
| 2627 |   event_base_set(base, &c->event); |
|---|
| 2628 | Â Â c->ev_flags =Â new_flags; |
|---|
| 2629 |   if (event_add(&c->event, 0) == -1) return false; |
|---|
| 2630 |   return true; |
|---|
| 2631 | } |
|---|
| 2632 | |
|---|
| 2633 | /* |
|---|
| 2634 | Â * Sets whether we are listening for new connections or not. |
|---|
| 2635 | Â */ |
|---|
| 2636 | void accept_new_conns(const bool do_accept) { |
|---|
| 2637 | Â Â conn *next; |
|---|
| 2638 | |
|---|
| 2639 |   if (! is_listen_thread()) |
|---|
| 2640 | Â Â Â Â return; |
|---|
| 2641 | |
|---|
| 2642 |   for (next = listen_conn; next; next = next->next) { |
|---|
| 2643 |     if (do_accept) { |
|---|
| 2644 |       update_event(next, EV_READ | EV_PERSIST); |
|---|
| 2645 |       if (listen(next->sfd, 1024) != 0) { |
|---|
| 2646 | Â Â Â Â Â Â Â Â perror("listen"); |
|---|
| 2647 | Â Â Â Â Â Â } |
|---|
| 2648 | Â Â Â Â } |
|---|
| 2649 |     else { |
|---|
| 2650 |       update_event(next, 0); |
|---|
| 2651 |       if (listen(next->sfd, 0) != 0) { |
|---|
| 2652 | Â Â Â Â Â Â Â Â perror("listen"); |
|---|
| 2653 | Â Â Â Â Â Â } |
|---|
| 2654 | Â Â Â Â } |
|---|
| 2655 | Â } |
|---|
| 2656 | } |
|---|
| 2657 | |
|---|
| 2658 | /* |
|---|
| 2659 | Â * Transmit the next chunk of data from our list of msgbuf structures. |
|---|
| 2660 | Â * |
|---|
| 2661 | Â * Returns: |
|---|
| 2662 | Â *Â Â TRANSMIT_COMPLETEÂ Â All done writing. |
|---|
| 2663 | Â *Â Â TRANSMIT_INCOMPLETE More data remaining to write. |
|---|
| 2664 | Â *Â Â TRANSMIT_SOFT_ERROR Can't write any more right now. |
|---|
| 2665 | Â *Â Â TRANSMIT_HARD_ERROR Can't write (c->state is set to conn_closing) |
|---|
| 2666 | Â */ |
|---|
| 2667 | static int transmit(conn *c) { |
|---|
| 2668 | Â Â assert(c !=Â NULL); |
|---|
| 2669 | |
|---|
| 2670 |   if (c->msgcurr < c->msgused && |
|---|
| 2671 | Â Â Â Â Â Â c->msglist[c->msgcurr].msg_iovlen ==Â 0)Â { |
|---|
| 2672 | Â Â Â Â /* Finished writing the current msg; advance to the next. */ |
|---|
| 2673 | Â Â Â Â c->msgcurr++; |
|---|
| 2674 | Â Â } |
|---|
| 2675 |   if (c->msgcurr < c->msgused) { |
|---|
| 2676 |     ssize_t res; |
|---|
| 2677 |     struct msghdr *m = &c->msglist[c->msgcurr]; |
|---|
| 2678 | |
|---|
| 2679 |     res = sendmsg(c->sfd, m, 0); |
|---|
| 2680 |     if (res > 0) { |
|---|
| 2681 | Â Â Â Â Â Â STATS_LOCK(); |
|---|
| 2682 | Â Â Â Â Â Â stats.bytes_written +=Â res; |
|---|
| 2683 | Â Â Â Â Â Â STATS_UNLOCK(); |
|---|
| 2684 | |
|---|
| 2685 | Â Â Â Â Â Â /* We've written some of the data. Remove the completed |
|---|
| 2686 | Â Â Â Â Â Â Â Â iovec entries from the list of pending writes. */ |
|---|
| 2687 |       while (m->msg_iovlen > 0 && res >= m->msg_iov->iov_len) { |
|---|
| 2688 | Â Â Â Â Â Â Â Â res -=Â m->msg_iov->iov_len; |
|---|
| 2689 | Â Â Â Â Â Â Â Â m->msg_iovlen--; |
|---|
| 2690 | Â Â Â Â Â Â Â Â m->msg_iov++; |
|---|
| 2691 | Â Â Â Â Â Â } |
|---|
| 2692 | |
|---|
| 2693 | Â Â Â Â Â Â /* Might have written just part of the last iovec entry; |
|---|
| 2694 | Â Â Â Â Â Â Â Â adjust it so the next write will do the rest. */ |
|---|
| 2695 |       if (res > 0) { |
|---|
| 2696 | Â Â Â Â Â Â Â Â m->msg_iov->iov_base +=Â res; |
|---|
| 2697 | Â Â Â Â Â Â Â Â m->msg_iov->iov_len -=Â res; |
|---|
| 2698 | Â Â Â Â Â Â } |
|---|
| 2699 |       return TRANSMIT_INCOMPLETE; |
|---|
| 2700 | Â Â Â Â } |
|---|
| 2701 |     if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
|---|
| 2702 |       if (!update_event(c, EV_WRITE | EV_PERSIST)) { |
|---|
| 2703 |         if (settings.verbose > 0) |
|---|
| 2704 |           fprintf(stderr, "Couldn't update event\n"); |
|---|
| 2705 |         conn_set_state(c, conn_closing); |
|---|
| 2706 |         return TRANSMIT_HARD_ERROR; |
|---|
| 2707 | Â Â Â Â Â Â } |
|---|
| 2708 |       return TRANSMIT_SOFT_ERROR; |
|---|
| 2709 | Â Â Â Â } |
|---|
| 2710 | Â Â Â Â /* if res==0 or res==-1 and error is not EAGAIN or EWOULDBLOCK, |
|---|
| 2711 | Â Â Â Â Â Â we have a real error, on which we close the connection */ |
|---|
| 2712 |     if (settings.verbose > 0) |
|---|
| 2713 | Â Â Â Â Â Â perror("Failed to write, and not due to blocking"); |
|---|
| 2714 | |
|---|
| 2715 |     if (IS_UDP(c->protocol)) |
|---|
| 2716 |       conn_set_state(c, conn_read); |
|---|
| 2717 | Â Â Â Â else |
|---|
| 2718 |       conn_set_state(c, conn_closing); |
|---|
| 2719 |     return TRANSMIT_HARD_ERROR; |
|---|
| 2720 |   } else { |
|---|
| 2721 |     return TRANSMIT_COMPLETE; |
|---|
| 2722 | Â Â } |
|---|
| 2723 | } |
|---|
| 2724 | |
|---|
| 2725 | static void drive_machine(conn *c) { |
|---|
| 2726 | Â Â bool stop =Â false; |
|---|
| 2727 |   int sfd, flags = 1; |
|---|
| 2728 |   int init_state; /* initial state for a new connection */ |
|---|
| 2729 | Â Â socklen_t addrlen; |
|---|
| 2730 |   struct sockaddr_storage addr; |
|---|
| 2731 |   int res; |
|---|
| 2732 | |
|---|
| 2733 | Â Â assert(c !=Â NULL); |
|---|
| 2734 | |
|---|
| 2735 |   while (!stop) { |
|---|
| 2736 | |
|---|
| 2737 | Â Â Â Â switch(c->state)Â { |
|---|
| 2738 |     case conn_listening: |
|---|
| 2739 | Â Â Â Â Â Â addrlen =Â sizeof(addr); |
|---|
| 2740 |       if ((sfd = accept(c->sfd, (struct sockaddr *)&addr, &addrlen)) == -1) { |
|---|
| 2741 |         if (errno == EAGAIN || errno == EWOULDBLOCK) { |
|---|
| 2742 | Â Â Â Â Â Â Â Â Â Â /* these are transient, so don't log anything */ |
|---|
| 2743 | Â Â Â Â Â Â Â Â Â Â stop =Â true; |
|---|
| 2744 |         } else if (errno == EMFILE) { |
|---|
| 2745 |           if (settings.verbose > 0) |
|---|
| 2746 |             fprintf(stderr, "Too many open connections\n"); |
|---|
| 2747 | Â Â Â Â Â Â Â Â Â Â accept_new_conns(false); |
|---|
| 2748 | Â Â Â Â Â Â Â Â Â Â stop =Â true; |
|---|
| 2749 |         } else { |
|---|
| 2750 | Â Â Â Â Â Â Â Â Â Â perror("accept()"); |
|---|
| 2751 | Â Â Â Â Â Â Â Â Â Â stop =Â true; |
|---|
| 2752 | Â Â Â Â Â Â Â Â } |
|---|
| 2753 | Â Â Â Â Â Â Â Â break; |
|---|
| 2754 | Â Â Â Â Â Â } |
|---|
| 2755 |       if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || |
|---|
| 2756 |         fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) { |
|---|
| 2757 | Â Â Â Â Â Â Â Â perror("setting O_NONBLOCK"); |
|---|
| 2758 | Â Â Â Â Â Â Â Â close(sfd); |
|---|
| 2759 | Â Â Â Â Â Â Â Â break; |
|---|
| 2760 | Â Â Â Â Â Â } |
|---|
| 2761 | Â Â Â Â Â Â init_state =Â get_init_state(c); |
|---|
| 2762 | |
|---|
| 2763 |       dispatch_conn_new(sfd, init_state, EV_READ | EV_PERSIST, |
|---|
| 2764 |                    DATA_BUFFER_SIZE, c->protocol); |
|---|
| 2765 | |
|---|
| 2766 | Â Â Â Â Â Â break; |
|---|
| 2767 | |
|---|
| 2768 |     case conn_negotiate: |
|---|
| 2769 |       if (settings.verbose > 1) |
|---|
| 2770 |         fprintf(stderr, "Negotiating protocol for a new connection\n"); |
|---|
| 2771 | Â Â Â Â Â Â c->rlbytes =Â 1; |
|---|
| 2772 | Â Â Â Â Â Â c->ritem =Â c->rbuf; |
|---|
| 2773 | Â Â Â Â Â Â c->rcurr =Â c->rbuf; |
|---|
| 2774 | Â Â Â Â Â Â c->wcurr =Â c->wbuf; |
|---|
| 2775 |       conn_set_state(c, conn_nread); |
|---|
| 2776 | Â Â Â Â Â Â break; |
|---|
| 2777 | |
|---|
| 2778 |     case conn_read: |
|---|
| 2779 |       if (try_read_command(c) != 0) { |
|---|
| 2780 | Â Â Â Â Â Â Â Â continue; |
|---|
| 2781 | Â Â Â Â Â Â } |
|---|
| 2782 |       if ((IS_UDP(c->protocol) ? try_read_udp(c) : try_read_network(c)) != 0) { |
|---|
| 2783 | Â Â Â Â Â Â Â Â continue; |
|---|
| 2784 | Â Â Â Â Â Â } |
|---|
| 2785 | Â Â Â Â Â Â /* we have no command line and no data to read from network */ |
|---|
| 2786 |       if (!update_event(c, EV_READ | EV_PERSIST)) { |
|---|
| 2787 |         if (settings.verbose > 0) |
|---|
| 2788 |           fprintf(stderr, "Couldn't update event\n"); |
|---|
| 2789 |         conn_set_state(c, conn_closing); |
|---|
| 2790 | Â Â Â Â Â Â Â Â break; |
|---|
| 2791 | Â Â Â Â Â Â } |
|---|
| 2792 | Â Â Â Â Â Â stop =Â true; |
|---|
| 2793 | Â Â Â Â Â Â break; |
|---|
| 2794 | |
|---|
| 2795 |     case conn_bin_init: /* Reinitialize a binary connection */ |
|---|
| 2796 | Â Â Â Â Â Â reinit_bin_connection(c); |
|---|
| 2797 | Â Â Â Â Â Â break; |
|---|
| 2798 | |
|---|
| 2799 |     case conn_nread: |
|---|
| 2800 |       if (c->rlbytes == 0) { |
|---|
| 2801 | Â Â Â Â Â Â Â Â complete_nread(c); |
|---|
| 2802 | Â Â Â Â Â Â Â Â break; |
|---|
| 2803 | Â Â Â Â Â Â } |
|---|
| 2804 | Â Â Â Â Â Â /* first check if we have leftovers in the conn_read buffer */ |
|---|
| 2805 |       if (c->rbytes > 0) { |
|---|
| 2806 |         int tocopy = c->rbytes > c->rlbytes ? c->rlbytes : c->rbytes; |
|---|
| 2807 |         memcpy(c->ritem, c->rcurr, tocopy); |
|---|
| 2808 | Â Â Â Â Â Â Â Â c->ritem +=Â tocopy; |
|---|
| 2809 | Â Â Â Â Â Â Â Â c->rlbytes -=Â tocopy; |
|---|
| 2810 | Â Â Â Â Â Â Â Â c->rcurr +=Â tocopy; |
|---|
| 2811 | Â Â Â Â Â Â Â Â c->rbytes -=Â tocopy; |
|---|
| 2812 | Â Â Â Â Â Â Â Â break; |
|---|
| 2813 | Â Â Â Â Â Â } |
|---|
| 2814 | |
|---|
| 2815 | Â Â Â Â Â Â /*Â now try reading from the socket */ |
|---|
| 2816 |       res = read(c->sfd, c->ritem, c->rlbytes); |
|---|
| 2817 |       if (res > 0) { |
|---|
| 2818 | Â Â Â Â Â Â Â Â STATS_LOCK(); |
|---|
| 2819 | Â Â Â Â Â Â Â Â stats.bytes_read +=Â res; |
|---|
| 2820 | Â Â Â Â Â Â Â Â STATS_UNLOCK(); |
|---|
| 2821 | Â Â Â Â Â Â Â Â c->ritem +=Â res; |
|---|
| 2822 | Â Â Â Â Â Â Â Â c->rlbytes -=Â res; |
|---|
| 2823 | Â Â Â Â Â Â Â Â break; |
|---|
| 2824 | Â Â Â Â Â Â } |
|---|
| 2825 |       if (res == 0) { /* end of stream */ |
|---|
| 2826 |         conn_set_state(c, conn_closing); |
|---|
| 2827 | Â Â Â Â Â Â Â Â break; |
|---|
| 2828 | Â Â Â Â Â Â } |
|---|
| 2829 |       if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
|---|
| 2830 |         if (!update_event(c, EV_READ | EV_PERSIST)) { |
|---|
| 2831 |           if (settings.verbose > 0) |
|---|
| 2832 |             fprintf(stderr, "Couldn't update event\n"); |
|---|
| 2833 |           conn_set_state(c, conn_closing); |
|---|
| 2834 | Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 2835 | Â Â Â Â Â Â Â Â } |
|---|
| 2836 | Â Â Â Â Â Â Â Â stop =Â true; |
|---|
| 2837 | Â Â Â Â Â Â Â Â break; |
|---|
| 2838 | Â Â Â Â Â Â } |
|---|
| 2839 | Â Â Â Â Â Â /* otherwise we have a real error, on which we close the connection */ |
|---|
| 2840 |       if (settings.verbose > 0) |
|---|
| 2841 |         fprintf(stderr, "Failed to read, and not due to blocking\n"); |
|---|
| 2842 |       conn_set_state(c, conn_closing); |
|---|
| 2843 | Â Â Â Â Â Â break; |
|---|
| 2844 | |
|---|
| 2845 |     case conn_swallow: |
|---|
| 2846 | Â Â Â Â Â Â /* we are reading sbytes and throwing them away */ |
|---|
| 2847 |       if (c->sbytes == 0) { |
|---|
| 2848 | Â Â Â Â Â Â Â Â conn_set_init_state(c); |
|---|
| 2849 | Â Â Â Â Â Â Â Â break; |
|---|
| 2850 | Â Â Â Â Â Â } |
|---|
| 2851 | |
|---|
| 2852 | Â Â Â Â Â Â /* first check if we have leftovers in the conn_read buffer */ |
|---|
| 2853 |       if (c->rbytes > 0) { |
|---|
| 2854 |         int tocopy = c->rbytes > c->sbytes ? c->sbytes : c->rbytes; |
|---|
| 2855 | Â Â Â Â Â Â Â Â c->sbytes -=Â tocopy; |
|---|
| 2856 | Â Â Â Â Â Â Â Â c->rcurr +=Â tocopy; |
|---|
| 2857 | Â Â Â Â Â Â Â Â c->rbytes -=Â tocopy; |
|---|
| 2858 | Â Â Â Â Â Â Â Â break; |
|---|
| 2859 | Â Â Â Â Â Â } |
|---|
| 2860 | |
|---|
| 2861 | Â Â Â Â Â Â /*Â now try reading from the socket */ |
|---|
| 2862 |       res = read(c->sfd, c->rbuf, c->rsize > c->sbytes ? c->sbytes : c->rsize); |
|---|
| 2863 |       if (res > 0) { |
|---|
| 2864 | Â Â Â Â Â Â Â Â STATS_LOCK(); |
|---|
| 2865 | Â Â Â Â Â Â Â Â stats.bytes_read +=Â res; |
|---|
| 2866 | Â Â Â Â Â Â Â Â STATS_UNLOCK(); |
|---|
| 2867 | Â Â Â Â Â Â Â Â c->sbytes -=Â res; |
|---|
| 2868 | Â Â Â Â Â Â Â Â break; |
|---|
| 2869 | Â Â Â Â Â Â } |
|---|
| 2870 |       if (res == 0) { /* end of stream */ |
|---|
| 2871 |         conn_set_state(c, conn_closing); |
|---|
| 2872 | Â Â Â Â Â Â Â Â break; |
|---|
| 2873 | Â Â Â Â Â Â } |
|---|
| 2874 |       if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
|---|
| 2875 |         if (!update_event(c, EV_READ | EV_PERSIST)) { |
|---|
| 2876 |           if (settings.verbose > 0) |
|---|
| 2877 |             fprintf(stderr, "Couldn't update event\n"); |
|---|
| 2878 |           conn_set_state(c, conn_closing); |
|---|
| 2879 | Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 2880 | Â Â Â Â Â Â Â Â } |
|---|
| 2881 | Â Â Â Â Â Â Â Â stop =Â true; |
|---|
| 2882 | Â Â Â Â Â Â Â Â break; |
|---|
| 2883 | Â Â Â Â Â Â } |
|---|
| 2884 | Â Â Â Â Â Â /* otherwise we have a real error, on which we close the connection */ |
|---|
| 2885 |       if (settings.verbose > 0) |
|---|
| 2886 |         fprintf(stderr, "Failed to read, and not due to blocking\n"); |
|---|
| 2887 |       conn_set_state(c, conn_closing); |
|---|
| 2888 | Â Â Â Â Â Â break; |
|---|
| 2889 | |
|---|
| 2890 |     case conn_write: |
|---|
| 2891 | Â Â Â Â Â Â /* |
|---|
| 2892 | Â Â Â Â Â Â Â * We want to write out a simple response. If we haven't already, |
|---|
| 2893 | Â Â Â Â Â Â Â * assemble it into a msgbuf list (this will be a single-entry |
|---|
| 2894 | Â Â Â Â Â Â Â * list for TCP or a two-entry list for UDP). |
|---|
| 2895 | Â Â Â Â Â Â Â */ |
|---|
| 2896 |       if (c->iovused == 0 || (IS_UDP(c->protocol) && c->iovused == 1)) { |
|---|
| 2897 |         if (add_iov(c, c->wcurr, c->wbytes) != 0 || |
|---|
| 2898 | Â Â Â Â Â Â Â Â Â Â (IS_UDP(c->protocol)Â &&Â build_udp_headers(c)Â !=Â 0))Â { |
|---|
| 2899 |           if (settings.verbose > 0) |
|---|
| 2900 |             fprintf(stderr, "Couldn't build response\n"); |
|---|
| 2901 |           conn_set_state(c, conn_closing); |
|---|
| 2902 | Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 2903 | Â Â Â Â Â Â Â Â } |
|---|
| 2904 | Â Â Â Â Â Â } |
|---|
| 2905 | |
|---|
| 2906 | Â Â Â Â Â Â /* fall through... */ |
|---|
| 2907 | |
|---|
| 2908 |     case conn_mwrite: |
|---|
| 2909 |       switch (transmit(c)) { |
|---|
| 2910 |       case TRANSMIT_COMPLETE: |
|---|
| 2911 |         if (c->state == conn_mwrite) { |
|---|
| 2912 |           while (c->ileft > 0) { |
|---|
| 2913 | Â Â Â Â Â Â Â Â Â Â Â Â item *it =Â *(c->icurr); |
|---|
| 2914 | Â Â Â Â Â Â Â Â Â Â Â Â assert((it->it_flags &Â ITEM_SLABBED)Â ==Â 0); |
|---|
| 2915 | Â Â Â Â Â Â Â Â Â Â Â Â item_remove(it); |
|---|
| 2916 | Â Â Â Â Â Â Â Â Â Â Â Â c->icurr++; |
|---|
| 2917 | Â Â Â Â Â Â Â Â Â Â Â Â c->ileft--; |
|---|
| 2918 | Â Â Â Â Â Â Â Â Â Â } |
|---|
| 2919 |           while (c->suffixleft > 0) { |
|---|
| 2920 |             char *suffix = *(c->suffixcurr); |
|---|
| 2921 | Â Â Â Â Â Â Â Â Â Â Â Â if(suffix_add_to_freelist(suffix))Â { |
|---|
| 2922 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* Failed to add to freelist, don't leak */ |
|---|
| 2923 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â free(suffix); |
|---|
| 2924 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 2925 | Â Â Â Â Â Â Â Â Â Â Â Â c->suffixcurr++; |
|---|
| 2926 | Â Â Â Â Â Â Â Â Â Â Â Â c->suffixleft--; |
|---|
| 2927 | Â Â Â Â Â Â Â Â Â Â } |
|---|
| 2928 | Â Â Â Â Â Â Â Â Â Â /* XXX:Â I don't know why this wasn't the general case */ |
|---|
| 2929 | Â Â Â Â Â Â Â Â Â Â if(c->protocol ==Â binary_prot)Â { |
|---|
| 2930 |             conn_set_state(c, c->write_and_go); |
|---|
| 2931 |           } else { |
|---|
| 2932 | Â Â Â Â Â Â Â Â Â Â Â Â conn_set_init_state(c); |
|---|
| 2933 | Â Â Â Â Â Â Â Â Â Â } |
|---|
| 2934 |         } else if (c->state == conn_write) { |
|---|
| 2935 |           if (c->write_and_free) { |
|---|
| 2936 | Â Â Â Â Â Â Â Â Â Â Â Â free(c->write_and_free); |
|---|
| 2937 | Â Â Â Â Â Â Â Â Â Â Â Â c->write_and_free =Â 0; |
|---|
| 2938 | Â Â Â Â Â Â Â Â Â Â } |
|---|
| 2939 |           conn_set_state(c, c->write_and_go); |
|---|
| 2940 |         } else { |
|---|
| 2941 |           if (settings.verbose > 0) |
|---|
| 2942 |             fprintf(stderr, "Unexpected state %d\n", c->state); |
|---|
| 2943 |           conn_set_state(c, conn_closing); |
|---|
| 2944 | Â Â Â Â Â Â Â Â } |
|---|
| 2945 | Â Â Â Â Â Â Â Â break; |
|---|
| 2946 | |
|---|
| 2947 |       case TRANSMIT_INCOMPLETE: |
|---|
| 2948 |       case TRANSMIT_HARD_ERROR: |
|---|
| 2949 | Â Â Â Â Â Â Â Â break;Â Â Â Â Â Â Â Â Â Â /* Continue in state machine. */ |
|---|
| 2950 | |
|---|
| 2951 |       case TRANSMIT_SOFT_ERROR: |
|---|
| 2952 | Â Â Â Â Â Â Â Â stop =Â true; |
|---|
| 2953 | Â Â Â Â Â Â Â Â break; |
|---|
| 2954 | Â Â Â Â Â Â } |
|---|
| 2955 | Â Â Â Â Â Â break; |
|---|
| 2956 | |
|---|
| 2957 |     case conn_closing: |
|---|
| 2958 |       if (IS_UDP(c->protocol)) |
|---|
| 2959 | Â Â Â Â Â Â Â Â conn_cleanup(c); |
|---|
| 2960 | Â Â Â Â Â Â else |
|---|
| 2961 | Â Â Â Â Â Â Â Â conn_close(c); |
|---|
| 2962 | Â Â Â Â Â Â stop =Â true; |
|---|
| 2963 | Â Â Â Â Â Â break; |
|---|
| 2964 | Â Â Â Â } |
|---|
| 2965 | Â Â } |
|---|
| 2966 | |
|---|
| 2967 | Â Â return; |
|---|
| 2968 | } |
|---|
| 2969 | |
|---|
| 2970 | void event_handler(const int fd, const short which, void |
|---|