Changeset 469
- Timestamp:
- 03/06/07 22:11:22 (2 years ago)
- Files:
-
- branches/multithreaded/server/ChangeLog (modified) (2 diffs)
- branches/multithreaded/server/Makefile.am (modified) (1 diff)
- branches/multithreaded/server/assoc.c (modified) (6 diffs)
- branches/multithreaded/server/assoc.h (copied) (copied from trunk/server/assoc.h) (1 diff)
- branches/multithreaded/server/configure.ac (modified) (1 diff)
- branches/multithreaded/server/items.c (modified) (22 diffs)
- branches/multithreaded/server/items.h (copied) (copied from trunk/server/items.h) (2 diffs)
- branches/multithreaded/server/memcached.c (modified) (89 diffs)
- branches/multithreaded/server/memcached.h (modified) (7 diffs)
- branches/multithreaded/server/slabs.c (modified) (16 diffs)
- branches/multithreaded/server/slabs.h (copied) (copied from trunk/server/slabs.h) (2 diffs)
- branches/multithreaded/server/stats.h (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/multithreaded/server/ChangeLog
r467 r469 1 1 2007-03-05 Paul Lindner <lindner@inuus.com> 2 * Fix a number of places where (s)printf calls were using unsigned 3 or signed formats that did not match their arguments. 4 5 * Add support for stdbool.h and stdint.h to use the bool and 6 uint8_t types. 7 8 * Major refactoring - move API calls for assoc/items/slabs to 9 their own individual header files. Add apropriate const and 10 static declarations as appropriate. 11 2 12 * Avoid type-punning. Do a more efficient realloc inside the 3 13 conn_shrink routine. … … 5 15 * Fix overflow bug where uninitialized access to slabclass caused 6 16 size-0 mallocs during slab preallocation. 17 18 * Use EXIT_SUCCESS/EXIT_FAILURE constants. 19 20 * Convert some sprintf calls to snprintf to protect against 21 buffer overflows. 22 23 * Explicitly compare against NULL or zero in many places. 7 24 8 25 2007-03-05 branches/multithreaded/server/Makefile.am
r462 r469 1 1 bin_PROGRAMS = memcached memcached-debug 2 2 3 memcached_SOURCES = memcached.c slabs.c items.c memcached.h assoc.c thread.c stats.c3 memcached_SOURCES = memcached.c slabs.c slabs.h items.c items.h assoc.c assoc.h memcached.h thread.c stats.c stats.h 4 4 memcached_debug_SOURCES = $(memcached_SOURCES) 5 5 memcached_CPPFLAGS = -DNDEBUG branches/multithreaded/server/assoc.c
r442 r469 141 141 const void *key, /* the key to hash */ 142 142 size_t length, /* length of the key */ 143 uint32_t initval) /* initval */143 const uint32_t initval) /* initval */ 144 144 { 145 145 uint32_t a,b,c; /* internal state */ … … 319 319 * big-endian byte ordering. 320 320 */ 321 uint32_t hash( const void *key, size_t length, uint32_t initval)321 uint32_t hash( const void *key, size_t length, const uint32_t initval) 322 322 { 323 323 uint32_t a,b,c; … … 450 450 451 451 /* how many powers of 2's worth of buckets we use */ 452 int hashpower = 16;452 static int hashpower = 16; 453 453 454 454 #define hashsize(n) ((ub4)1<<(n)) … … 486 486 } 487 487 488 item *assoc_find(const char *key, size_t nkey) {488 item *assoc_find(const char *key, const size_t nkey) { 489 489 uint32_t hv = hash(key, nkey, 0); 490 490 item *it; … … 512 512 the item wasn't found */ 513 513 514 static item** _hashitem_before (const char *key, size_t nkey) {514 static item** _hashitem_before (const char *key, const size_t nkey) { 515 515 uint32_t hv = hash(key, nkey, 0); 516 516 item **pos; … … 601 601 } 602 602 603 void assoc_delete(const char *key, size_t nkey) {603 void assoc_delete(const char *key, const size_t nkey) { 604 604 item **before = _hashitem_before(key, nkey); 605 605 branches/multithreaded/server/assoc.h
r468 r469 4 4 int assoc_insert(item *item); 5 5 void assoc_delete(const char *key, const size_t nkey); 6 void assoc_move_next_bucket(void); 6 void do_assoc_move_next_bucket(void); 7 uint32_t hash( const void *key, size_t length, const uint32_t initval); 8 branches/multithreaded/server/configure.ac
r467 r469 99 99 AC_CHECK_FUNC(daemon,AC_DEFINE([HAVE_DAEMON],,[Define this if you have daemon()]),[AC_LIBOBJ(daemon)]) 100 100 101 101 AC_HEADER_STDBOOL 102 AC_C_CONST 102 103 AC_CHECK_HEADER(malloc.h, AC_DEFINE(HAVE_MALLOC_H,,[do we have malloc.h?])) 103 104 AC_CHECK_MEMBER([struct mallinfo.arena], [ branches/multithreaded/server/items.c
r442 r469 15 15 #include <assert.h> 16 16 17 /* Forward Declarations */ 18 static void item_link_q(item *it); 19 static void item_unlink_q(item *it); 20 17 21 /* 18 22 * We only reposition items in the LRU queue if they haven't been repositioned … … 25 29 static item *heads[LARGEST_ID]; 26 30 static item *tails[LARGEST_ID]; 27 unsigned int sizes[LARGEST_ID];31 static unsigned int sizes[LARGEST_ID]; 28 32 29 33 void item_init(void) { … … 60 64 * Returns the total size of the header. 61 65 */ 62 int item_make_header(char *key, uint8_t nkey, int flags, int nbytes, 63 char *suffix, int *nsuffix) { 64 *nsuffix = sprintf(suffix, " %u %u\r\n", flags, nbytes - 2); 66 static size_t item_make_header(char *key, const uint8_t nkey, const int flags, const int nbytes, 67 char *suffix, uint8_t *nsuffix) { 68 /* suffix is defined at 40 chars elsewhere.. */ 69 *nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2); 65 70 return sizeof(item) + nkey + *nsuffix + nbytes; 66 71 } 67 72 68 item *do_item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes) { 69 int nsuffix, ntotal; 73 /*@null@*/ 74 item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes) { 75 uint8_t nsuffix; 76 size_t ntotal; 70 77 item *it; 71 78 unsigned int id; … … 87 94 */ 88 95 89 if ( !settings.evict_to_free) return 0;96 if (settings.evict_to_free == 0) return NULL; 90 97 91 98 /* … … 96 103 */ 97 104 98 if (id > LARGEST_ID) return 0;99 if (tails[id]==0) return 0;105 if (id > LARGEST_ID) return NULL; 106 if (tails[id]==0) return NULL; 100 107 101 108 for (search = tails[id]; tries>0 && search; tries--, search=search->prev) { … … 106 113 } 107 114 it = slabs_alloc(ntotal); 108 if (it==0) return 0;115 if (it==0) return NULL; 109 116 } 110 117 … … 123 130 strcpy(ITEM_key(it), key); 124 131 it->exptime = exptime; 125 memcpy(ITEM_suffix(it), suffix, nsuffix);132 memcpy(ITEM_suffix(it), suffix, (size_t) nsuffix); 126 133 it->nsuffix = nsuffix; 127 134 return it; … … 129 136 130 137 void item_free(item *it) { 131 unsigned int ntotal = ITEM_ntotal(it);138 size_t ntotal = ITEM_ntotal(it); 132 139 assert((it->it_flags & ITEM_LINKED) == 0); 133 140 assert(it != heads[it->slabs_clsid]); … … 146 153 * the maximum for a cache entry.) 147 154 */ 148 int item_size_ok(char *key, size_t nkey, int flags,int nbytes) {155 bool item_size_ok(char *key, const size_t nkey, const int flags, const int nbytes) { 149 156 char prefix[40]; 150 int nsuffix;157 uint8_t nsuffix; 151 158 152 159 return slabs_clsid(item_make_header(key, nkey + 1, flags, nbytes, … … 154 161 } 155 162 156 void item_link_q(item *it) { /* item is the new head */163 static void item_link_q(item *it) { /* item is the new head */ 157 164 item **head, **tail; 158 165 /* always true, warns: assert(it->slabs_clsid <= LARGEST_ID); */ … … 172 179 } 173 180 174 void item_unlink_q(item *it) {181 static void item_unlink_q(item *it) { 175 182 item **head, **tail; 176 183 /* always true, warns: assert(it->slabs_clsid <= LARGEST_ID); */ … … 214 221 215 222 void do_item_unlink(item *it) { 216 if ( it->it_flags & ITEM_LINKED) {223 if ((it->it_flags & ITEM_LINKED) != 0) { 217 224 it->it_flags &= ~ITEM_LINKED; 218 225 STATS_LOCK(); … … 232 239 DEBUG_REFCNT(it, '-'); 233 240 } 234 assert((it->it_flags & ITEM_DELETED) == 0 || it->refcount );241 assert((it->it_flags & ITEM_DELETED) == 0 || it->refcount != 0); 235 242 if (it->refcount == 0 && (it->it_flags & ITEM_LINKED) == 0) { 236 243 item_free(it); … … 257 264 } 258 265 259 char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int *bytes) { 260 266 /*@null@*/ 267 char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes) { 261 268 int memlimit = 2*1024*1024; 262 269 char *buffer; 263 int bufcurr;270 unsigned int bufcurr; 264 271 item *it; 265 272 int len; … … 267 274 char temp[512]; 268 275 269 if (slabs_clsid > LARGEST_ID) return 0;276 if (slabs_clsid > LARGEST_ID) return NULL; 270 277 it = heads[slabs_clsid]; 271 278 272 buffer = malloc( memlimit);273 if (buffer == 0) return 0;279 buffer = malloc((size_t)memlimit); 280 if (buffer == 0) return NULL; 274 281 bufcurr = 0; 275 282 276 while (it && (!limit|| shown < limit)) {277 len = s printf(temp, "ITEM %s [%ub; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started);283 while (it != NULL && (limit==0 || shown < limit)) { 284 len = snprintf(temp, 512, "ITEM %s [%d b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 278 285 if (bufcurr + len + 6 > memlimit) /* 6 is END\r\n\0 */ 279 286 break; … … 291 298 } 292 299 293 void item_stats(char *buffer, int buflen) {300 void item_stats(char *buffer, const int buflen) { 294 301 int i; 295 302 char *bufcurr = buffer; … … 303 310 for (i=0; i<LARGEST_ID; i++) { 304 311 if (tails[i]) 305 bufcurr += s printf(bufcurr, "STAT items:%u:number %u\r\nSTAT items:%u:age %u\r\n",312 bufcurr += snprintf(bufcurr, (size_t)buflen, "STAT items:%d:number %u\r\nSTAT items:%d:age %u\r\n", 306 313 i, sizes[i], i, now - tails[i]->time); 307 314 } … … 311 318 312 319 /* dumps out a list of objects of each size, with granularity of 32 bytes */ 320 /*@null@*/ 313 321 char* item_stats_sizes(int *bytes) { 314 int num_buckets = 32768; /* max 1MB object, divided into 32 bytes size buckets */315 unsigned int *histogram = (unsigned int*) malloc( num_buckets * sizeof(int));322 const int num_buckets = 32768; /* max 1MB object, divided into 32 bytes size buckets */ 323 unsigned int *histogram = (unsigned int*) malloc((size_t)num_buckets * sizeof(int)); 316 324 char *buf = (char*) malloc(1024*1024*2*sizeof(char)); 317 325 int i; … … 320 328 if (histogram) free(histogram); 321 329 if (buf) free(buf); 322 return 0;330 return NULL; 323 331 } 324 332 325 333 /* build the histogram */ 326 memset(histogram, 0, num_buckets * sizeof(int));334 memset(histogram, 0, (size_t) num_buckets * sizeof(int)); 327 335 for (i=0; i<LARGEST_ID; i++) { 328 336 item *iter = heads[i]; … … 330 338 int ntotal = ITEM_ntotal(iter); 331 339 int bucket = ntotal / 32; 332 if ( ntotal % 32) bucket++;340 if ((ntotal % 32) != 0) bucket++; 333 341 if (bucket < num_buckets) histogram[bucket]++; 334 342 iter = iter->next; … … 339 347 *bytes = 0; 340 348 for (i=0; i<num_buckets; i++) { 341 if (histogram[i] ) {342 *bytes += sprintf(&buf[*bytes], "% u%u\r\n", i*32, histogram[i]);349 if (histogram[i] != 0) { 350 *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i*32, histogram[i]); 343 351 } 344 352 } … … 403 411 int i; 404 412 item *iter, *next; 405 if ( ! settings.oldest_live)413 if (settings.oldest_live == 0) 406 414 return; 407 415 for (i = 0; i < LARGEST_ID; i++) { branches/multithreaded/server/items.h
r468 r469 2 2 void item_init(void); 3 3 /*@null@*/ 4 item * item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes);4 item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes); 5 5 void item_free(item *it); 6 6 bool item_size_ok(char *key, const size_t nkey, const int flags, const int nbytes); 7 7 8 int item_link(item *it); /* may fail if transgresses limits */9 void item_unlink(item *it);10 void item_remove(item *it);11 void item_update(item *it); /* update LRU time to current and reposition */12 int item_replace(item *it, item *new_it);8 int do_item_link(item *it); /* may fail if transgresses limits */ 9 void do_item_unlink(item *it); 10 void do_item_remove(item *it); 11 void do_item_update(item *it); /* update LRU time to current and reposition */ 12 int do_item_replace(item *it, item *new_it); 13 13 14 14 /*@null@*/ … … 18 18 /*@null@*/ 19 19 char *item_stats_sizes(int *bytes); 20 void item_flush_expired(void); 20 void do_item_flush_expired(void); 21 item *item_get(char *key, size_t nkey); 22 23 item *do_item_get_notedeleted(char *key, size_t nkey, int *delete_locked); 24 item *do_item_get_nocheck(char *key, size_t nkey); branches/multithreaded/server/memcached.c
r467 r469 61 61 #endif 62 62 63 /* 64 * forward declarations 65 */ 66 static void drive_machine(conn *c); 67 static int new_socket(const bool is_udp); 68 static int server_socket(const int port, const bool is_udp); 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 73 /* stats */ 74 static void stats_reset(void); 75 static void stats_init(void); 76 77 /* defaults */ 78 static void settings_init(void); 79 80 /* event handling, network IO */ 81 static void event_handler(const int fd, const short which, void *arg); 82 static void conn_close(conn *c); 83 static void conn_init(void); 84 static void accept_new_conns(const bool do_accept); 85 static bool update_event(conn *c, const int new_flags); 86 static void complete_nread(conn *c); 87 static void process_command(conn *c, char *command); 88 static int transmit(conn *c); 89 static int ensure_iov_space(conn *c); 90 static int add_iov(conn *c, const void *buf, int len); 91 static int add_msghdr(conn *c); 92 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 void pre_gdb(void); 100 101 /** exported globals **/ 63 102 struct stats stats; 64 103 struct settings settings; 65 104 105 /** file scope variables **/ 66 106 static item **todelete = 0; 67 107 static int delcurr; … … 75 115 #define TRANSMIT_HARD_ERROR 3 76 116 77 int *buckets = 0; /* bucket->generation array for a managed instance */117 static int *buckets = 0; /* bucket->generation array for a managed instance */ 78 118 79 119 void conn_free(conn *c); 80 120 81 121 #define REALTIME_MAXDELTA 60*60*24*30 82 rel_time_t realtime(time_t exptime) { 122 /* 123 * given time value that's either unix time or delta from current unix time, return 124 * unix time. Use the fact that delta can't exceed one month (and real time value can't 125 * be that low). 126 */ 127 static rel_time_t realtime(const time_t exptime) { 83 128 /* no. of seconds in 30 days - largest possible delta exptime */ 84 129 … … 100 145 } 101 146 102 void stats_init(void) {147 static void stats_init(void) { 103 148 stats.curr_items = stats.total_items = stats.curr_conns = stats.total_conns = stats.conn_structs = 0; 104 149 stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = 0; … … 113 158 } 114 159 115 void stats_reset(void) {160 static void stats_reset(void) { 116 161 STATS_LOCK(); 117 162 stats.total_items = stats.total_conns = 0; … … 122 167 } 123 168 124 void settings_init(void) {169 static void settings_init(void) { 125 170 settings.port = 11211; 126 171 settings.udpport = 0; … … 132 177 settings.evict_to_free = 1; /* push old items out of cache when memory runs out */ 133 178 settings.socketpath = NULL; /* by default, not using a unix socket */ 134 settings.managed = 0;179 settings.managed = false; 135 180 settings.factor = 1.25; 136 181 settings.chunk_size = 48; /* space for a modest key and value */ … … 144 189 } 145 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 146 198 /* 147 199 * Adds a message header to a connection. … … 149 201 * Returns 0 on success, -1 on out-of-memory. 150 202 */ 151 int add_msghdr(conn *c)203 static int add_msghdr(conn *c) 152 204 { 153 205 struct msghdr *msg; … … 182 234 } 183 235 236 184 237 /* 185 238 * Free list management for connections. 186 239 */ 187 conn **freeconns; 188 int freetotal; 189 int freecurr; 190 191 void conn_init(void) { 240 241 static conn **freeconns; 242 static int freetotal; 243 static int freecurr; 244 245 246 static void conn_init(void) { 192 247 freetotal = 200; 193 248 freecurr = 0; 194 249 freeconns = (conn **)malloc(sizeof (conn *)*freetotal); 250 /** TODO check for NULL **/ 195 251 return; 196 252 } … … 233 289 } 234 290 235 conn *conn_new( int sfd, int init_state,int event_flags,236 int read_buffer_size, intis_udp, struct event_base *base) {291 conn *conn_new(const int sfd, const int init_state, const int event_flags, 292 const int read_buffer_size, const bool is_udp, struct event_base *base) { 237 293 conn *c = conn_from_freelist(); 238 294 … … 240 296 if (!(c = (conn *)malloc(sizeof(conn)))) { 241 297 perror("malloc()"); 242 return 0;298 return NULL; 243 299 } 244 300 c->rbuf = c->wbuf = 0; … … 255 311 c->hdrsize = 0; 256 312 257 c->rbuf = (char *) malloc( c->rsize);258 c->wbuf = (char *) malloc( c->wsize);313 c->rbuf = (char *) malloc((size_t)c->rsize); 314 c->wbuf = (char *) malloc((size_t)c->wsize); 259 315 c->ilist = (item **) malloc(sizeof(item *) * c->isize); 260 316 c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize); … … 270 326 free(c); 271 327 perror("malloc()"); 272 return 0;328 return NULL; 273 329 } 274 330 … … 315 371 conn_free(c); 316 372 } 317 return 0;373 return NULL; 318 374 } 319 375 … … 326 382 } 327 383 328 void conn_cleanup(conn *c) {384 static void conn_cleanup(conn *c) { 329 385 if (c->item) { 330 386 item_remove(c->item); … … 332 388 } 333 389 334 if (c->ileft ) {390 if (c->ileft != 0) { 335 391 for (; c->ileft > 0; c->ileft--,c->icurr++) { 336 392 item_remove(*(c->icurr)); … … 365 421 } 366 422 367 void conn_close(conn *c) {423 static void conn_close(conn *c) { 368 424 /* delete the event, the socket and the conn */ 369 425 event_del(&c->event); … … 373 429 374 430 close(c->sfd); 375 accept_new_conns( 1);431 accept_new_conns(true); 376 432 conn_cleanup(c); 377 433 … … 403 459 if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { 404 460 if (c->rcurr != c->rbuf) 405 memmove(c->rbuf, c->rcurr, c->rbytes);461 memmove(c->rbuf, c->rcurr, (size_t) c->rbytes); 406 462 407 463 char *newbuf = (char*) realloc((void*)c->rbuf, DATA_BUFFER_SIZE); 464 408 465 if (newbuf) { 409 466 c->rbuf = newbuf; … … 447 504 * happen here. 448 505 */ 449 void conn_set_state(conn *c, int state) {506 static void conn_set_state(conn *c, int state) { 450 507 if (state != c->state) { 451 508 if (state == conn_read) { … … 464 521 * Returns 0 on success, -1 on out-of-memory. 465 522 */ 466 int ensure_iov_space(conn *c) {523 static int ensure_iov_space(conn *c) { 467 524 if (c->iovused >= c->iovsize) { 468 525 int i, iovnum; … … 492 549 */ 493 550 494 int add_iov(conn *c, const void *buf, int len) {551 static int add_iov(conn *c, const void *buf, int len) { 495 552 struct msghdr *m; 496 553 int leftover; 497 intlimit_to_mtu;554 bool limit_to_mtu; 498 555 499 556 do { … … 513 570 } 514 571 515 if (ensure_iov_space(c) )572 if (ensure_iov_space(c) != 0) 516 573 return -1; 517 574 … … 543 600 * Constructs a set of UDP headers and attaches them to the outgoing messages. 544 601 */ 545 int build_udp_headers(conn *c) {602 static int build_udp_headers(conn *c) { 546 603 int i; 547 604 unsigned char *hdr; … … 578 635 579 636 580 void out_string(conn *c,char *str) {637 static void out_string(conn *c, const char *str) { 581 638 int len; 582 639 … … 606 663 */ 607 664 608 void complete_nread(conn *c) {665 static void complete_nread(conn *c) { 609 666 item *it = c->item; 610 667 int comm = c->item_comm; … … 640 697 int stored = 0; 641 698 642 if (old_it && comm == NREAD_ADD) {699 if (old_it != NULL && comm == NREAD_ADD) { 643 700 /* add only adds a nonexistent item, but promote to head of LRU */ 644 701 do_item_update(old_it); … … 655 712 old_it = do_item_get_nocheck(key, it->nkey); 656 713 657 if (old_it )714 if (old_it != NULL) 658 715 do_item_replace(old_it, it); 659 716 else … … 697 754 * } 698 755 */ 699 s ize_t tokenize_command(char* command, token_t* tokens,size_t max_tokens) {756 static size_t tokenize_command(char* command, token_t* tokens, const size_t max_tokens) { 700 757 char* cp; 701 758 char* value = NULL; … … 771 828 } 772 829 773 void process_stat(conn *c, token_t* tokens,size_t ntokens) {830 static void process_stat(conn *c, token_t* tokens, const size_t ntokens) { 774 831 rel_time_t now = current_time; 775 832 char* command; … … 972 1029 } 973 1030 974 inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 1031 /* ntokens is overwritten here... shrug.. */ 1032 static inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 975 1033 char *key; 976 1034 size_t nkey; … … 1026 1084 * " " + flags + " " + data length + "\r\n" + data (with \r\n) 1027 1085 */ 1028 if (add_iov(c, "VALUE ", 6) ||1029 add_iov(c, ITEM_key(it), it->nkey) ||1030 add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) )1086 if (add_iov(c, "VALUE ", 6) != 0 || 1087 add_iov(c, ITEM_key(it), it->nkey) != 0 || 1088 add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) != 0) 1031 1089 { 1032 1090 break; … … 1070 1128 add_iov(c, "END\r\n", 5); 1071 1129 1072 if (c->udp && build_udp_headers(c) ) {1130 if (c->udp && build_udp_headers(c) != 0) { 1073 1131 out_string(c, "SERVER_ERROR out of memory"); 1074 1132 } … … 1080 1138 } 1081 1139 1082 void process_update_command(conn *c, token_t* tokens,size_t ntokens, int comm) {1140 static void process_update_command(conn *c, token_t* tokens, const size_t ntokens, int comm) { 1083 1141 char *key; 1084 1142 size_t nkey; … … 1142 1200 } 1143 1201 1144 void process_arithmetic_command(conn *c, token_t* tokens, size_t ntokens,int incr) {1202 static void process_arithmetic_command(conn *c, token_t* tokens, const size_t ntokens, const int incr) { 1145 1203 char temp[32]; 1146 1204 item *it; … … 1204 1262 1205 1263 ptr = ITEM_data(it); 1206 while ( *ptr&& (*ptr<'0' && *ptr>'9')) ptr++; // BUG: can't be true1264 while ((*ptr != '\0') && (*ptr<'0' && *ptr>'9')) ptr++; // BUG: can't be true 1207 1265 1208 1266 value = strtol(ptr, NULL, 10); … … 1212 1270 } 1213 1271 1214 if (incr )1272 if (incr != 0) 1215 1273 value+=delta; 1216 1274 else { … … 1218 1276 else value-=delta; 1219 1277 } 1220 s printf(buf, "%u", value);1278 snprintf(buf, 32, "%u", value); 1221 1279 res = strlen(buf); 1222 1280 if (res + 2 > it->nbytes) { /* need to realloc */ … … 1238 1296 } 1239 1297 1240 void process_delete_command(conn *c, token_t* tokens,size_t ntokens) {1298 static void process_delete_command(conn *c, token_t* tokens, const size_t ntokens) { 1241 1299 char *key; 1242 1300 size_t nkey; … … 1323 1381 } 1324 1382 1325 void process_command(conn *c, char *command) {1383 static void process_command(conn *c, char *command) { 1326 1384 1327 1385 token_t tokens[MAX_TOKENS]; … … 1340 1398 c->msgused = 0; 1341 1399 c->iovused = 0; 1342 if (add_msghdr(c) ) {1400 if (add_msghdr(c) != 0) { 1343 1401 out_string(c, "SERVER_ERROR out of memory"); 1344 1402 return; … … 1507 1565 * if we have a complete line in the buffer, process it. 1508 1566 */ 1509 int try_read_command(conn *c) {1567 static int try_read_command(conn *c) { 1510 1568 char *el, *cont; 1511 1569 1512 1570 assert(c->rcurr <= c->rbuf + c->rsize); 1513 1571 1514 if ( !c->rbytes)1572 if (c->rbytes == 0) 1515 1573 return 0; 1516 1574 el = memchr(c->rcurr, '\n', c->rbytes); … … 1539 1597 * return 0 if there's nothing to read. 1540 1598 */ 1541 int try_read_udp(conn *c) {1599 static int try_read_udp(conn *c) { 1542 1600 int res; 1543 1601 … … 1578 1636 * return 0 if there's nothing to read on the first read. 1579 1637 */ 1580 int try_read_network(conn *c) {1638 static int try_read_network(conn *c) { 1581 1639 int gotdata = 0; 1582 1640 int res; … … 1634 1692 } 1635 1693 1636 int update_event(conn *c,int new_flags) {1694 static bool update_event(conn *c, const int new_flags) { 1637 1695 struct event_base *base = c->event.ev_base; 1638 1696 if (c->ev_flags == new_flags) 1639 return 1;1640 if (event_del(&c->event) == -1) return 0;1697 return true; 1698 if (event_del(&c->event) == -1) return false; 1641 1699 event_set(&c->event, c->sfd, new_flags, event_handler, (void *)c); 1642 1700 event_base_set(base, &c->event); 1643 1701 c->ev_flags = new_flags; 1644 if (event_add(&c->event, 0) == -1) return 0;1645 return 1;1702 if (event_add(&c->event, 0) == -1) return false; 1703 return true; 1646 1704 } 1647 1705 … … 1649 1707 * Sets whether we are listening for new connections or not. 1650 1708 */ 1651 void accept_new_conns( intdo_accept) {1709 void accept_new_conns(const bool do_accept) { 1652 1710 if (! is_listen_thread()) 1653 1711 return; 1654 1712 if (do_accept) { 1655 1713 update_event(listen_conn, EV_READ | EV_PERSIST); 1656 if (listen(listen_conn->sfd, 1024) ) {1714 if (listen(listen_conn->sfd, 1024) != 0) { 1657 1715 perror("listen"); 1658 1716 } … … 1660 1718 else { 1661 1719 update_event(listen_conn, 0); 1662 if (listen(listen_conn->sfd, 0) ) {1720 if (listen(listen_conn->sfd, 0) != 0) { 1663 1721 perror("listen"); 1664 1722 } … … 1676 1734 * TRANSMIT_HARD_ERROR Can't write (c->state is set to conn_closing) 1677 1735 */ 1678 int transmit(conn *c) {1736 static int transmit(conn *c) { 1679 1737 int res; 1680 1738 … … 1732 1790 } 1733 1791 1734 void drive_machine(conn *c) { 1735 1736 int stop = 0; 1792 static void drive_machine(conn *c) { 1793 bool stop = false; 1737 1794 int sfd, flags = 1; 1738 1795 socklen_t addrlen; … … 1748 1805 if (errno == EAGAIN || errno == EWOULDBLOCK) { 1749 1806 /* these are transient, so don't log anything */ 1750 stop = 1;1807 stop = true; 1751 1808 } else if (errno == EMFILE) { 1752 1809 if (settings.verbose > 0) 1753 1810 fprintf(stderr, "Too many open connections\n"); 1754 accept_new_conns( 0);1811 accept_new_conns(false); 1755 1812 stop = 1; 1756 1813 } else { … … 1767 1824 } 1768 1825 dispatch_conn_new(sfd, conn_read, EV_READ | EV_PERSIST, 1769 DATA_BUFFER_SIZE, 0);1826 DATA_BUFFER_SIZE, false); 1770 1827 break; 1771 1828 1772 1829 case conn_read: 1773 if (try_read_command(c) ) {1830 if (try_read_command(c) != 0) { 1774 1831 continue; 1775 1832 } 1776 if ( c->udp ? try_read_udp(c) : try_read_network(c)) {1833 if ((c->udp ? try_read_udp(c) : try_read_network(c)) != 0) { 1777 1834 continue; 1778 1835 } … … 1784 1841 break; 1785 1842 } 1786 stop = 1;1843 stop = true; 1787 1844 break; 1788 1845 … … 1825 1882 break; 1826 1883 } 1827 stop = 1;1884 stop = true; 1828 1885 break; 1829 1886 } … … 1870 1927 break; 1871 1928 } 1872 stop = 1;1929 stop = true; 1873 1930 break; 1874 1931 } … … 1886 1943 */ 1887 1944 if (c->iovused == 0 || (c->udp && c->iovused == 1)) { 1888 if (add_iov(c, c->wcurr, c->wbytes) ||1889 (c->udp && build_udp_headers(c) )) {1945 if (add_iov(c, c->wcurr, c->wbytes) != 0 || 1946 (c->udp && build_udp_headers(c) != 0)) { 1890 1947 if (settings.verbose > 0) 1891 1948 fprintf(stderr, "Couldn't build response\n"); … … 1927 1984 1928 1985 case TRANSMIT_SOFT_ERROR: 1929 stop = 1;1986 stop = true; 1930 1987 break; 1931 1988 } … … 1937 1994 else 1938 1995 conn_close(c); 1939 stop = 1;1996 stop = true; 1940 1997 break; 1941 1998 } … … 1945 2002 } 1946 2003 1947 void event_handler( int fd,short which, void *arg) {2004 void event_handler(const int fd, const short which, void *arg) { 1948 2005 conn *c; 1949 2006 … … 1965 2022 } 1966 2023 1967 int new_socket(intis_udp) {2024 static int new_socket(const bool is_udp) { 1968 2025 int sfd; 1969 2026 int flags; … … 1987 2044 * Sets a socket's send buffer size to the maximum allowed by the system. 1988 2045 */ 1989 void maximize_sndbuf(int sfd) {2046 static void maximize_sndbuf(const int sfd) { 1990 2047 socklen_t intsize = sizeof(int); 1991 2048 int last_good = 0; … … 1994 2051 1995 2052 /* Start with the default size. */ 1996 if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize) ) {2053 if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize) != 0) { 1997 2054 if (settings.verbose > 0) 1998 2055 perror("getsockopt(SO_SNDBUF)"); … … 2019 2076 2020 2077 2021 int server_socket(int port, intis_udp) {2078 static int server_socket(const int port, const bool is_udp) { 2022 2079 int sfd; 2023 2080 struct linger ling = {0, 0}; … … 2052 2109 return -1; 2053 2110 } 2054 if (! is_udp && listen(sfd, 1024) == -1) {2111 if (!is_udp && listen(sfd, 1024) == -1) { 2055 2112 perror("listen()"); 2056 2113 close(sfd); … … 2060 2117 } 2061 2118 2062 int new_socket_unix(void) {2119 static int new_socket_unix(void) { 2063 2120 int sfd; 2064 2121 int flags; … … 2078 2135 } 2079 2136 2080 int server_socket_unix(char *path) {2137 static int server_socket_unix(char *path) { 2081 2138 int sfd; 2082 2139 struct linger ling = {0, 0}; … … 2096 2153 * Clean up a previous socket file if we left it around 2097 2154 */ 2098 if ( !lstat(path, &tstat)) {2155 if (lstat(path, &tstat) == 0) { 2099 2156 if (S_ISSOCK(tstat.st_mode)) 2100 2157 unlink(path); … … 2126 2183 } 2127 2184 2185 /* listening socket */ 2186 static int l_socket=0; 2187 2188 /* udp socket */ 2189 static int u_socket=-1; 2128 2190 2129 2191 /* invoke right before gdb is called, on assert */ 2130 void pre_gdb () {2192 void pre_gdb(void) { 2131 2193 int i = 0; 2132 if (l_socket) close(l_socket);2133 if (u_socket > -1) close(u_socket);2194 if (l_socket > -1) close(l_socket); 2195 if (u_socket > -1) close(u_socket); 2134 2196 for (i=3; i<=500; i++) close(i); /* so lame */ 2135 2197 kill(getpid(), SIGABRT); … … 2145 2207 */ 2146 2208 volatile rel_time_t curre
