Changeset 468
- Timestamp:
- 03/06/07 16:28:53 (2 years ago)
- Files:
-
- trunk/server/ChangeLog (modified) (2 diffs)
- trunk/server/Makefile.am (modified) (1 diff)
- trunk/server/assoc.c (modified) (6 diffs)
- trunk/server/assoc.h (added)
- trunk/server/configure.ac (modified) (1 diff)
- trunk/server/items.c (modified) (22 diffs)
- trunk/server/items.h (added)
- trunk/server/memcached.c (modified) (93 diffs)
- trunk/server/memcached.h (modified) (6 diffs)
- trunk/server/slabs.c (modified) (17 diffs)
- trunk/server/slabs.h (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/server/ChangeLog
r465 r468 1 1 2007-03-05 Paul Lindner <lindner@inuus.com> 2 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 3 12 * Avoid type-punning. Do a more efficient realloc inside the 4 13 conn_shrink routine. … … 6 15 * Fix overflow bug where uninitialized access to slabclass caused 7 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. 8 24 9 25 2006-12-23 trunk/server/Makefile.am
r379 r468 1 1 bin_PROGRAMS = memcached memcached-debug 2 2 3 memcached_SOURCES = memcached.c slabs.c items.c assoc.c memcached.h3 memcached_SOURCES = memcached.c slabs.c slabs.h items.c items.h assoc.c assoc.h memcached.h 4 4 memcached_debug_SOURCES = $(memcached_SOURCES) 5 5 memcached_CPPFLAGS = -DNDEBUG trunk/server/assoc.c
r450 r468 143 143 144 144 #if HASH_LITTLE_ENDIAN == 1 145 uint32_t hash(145 static uint32_t hash( 146 146 const void *key, /* the key to hash */ 147 147 size_t length, /* length of the key */ 148 uint32_t initval) /* initval */148 const uint32_t initval) /* initval */ 149 149 { 150 150 uint32_t a,b,c; /* internal state */ … … 324 324 * big-endian byte ordering. 325 325 */ 326 uint32_t hash( const void *key, size_t length,uint32_t initval)326 static uint32_t hash( const void *key, size_t length, const uint32_t initval) 327 327 { 328 328 uint32_t a,b,c; … … 455 455 456 456 /* how many powers of 2's worth of buckets we use */ 457 int hashpower = 16;457 static int hashpower = 16; 458 458 459 459 #define hashsize(n) ((ub4)1<<(n)) … … 491 491 } 492 492 493 item *assoc_find(const char *key, size_t nkey) {493 item *assoc_find(const char *key, const size_t nkey) { 494 494 uint32_t hv = hash(key, nkey, 0); 495 495 item *it; … … 517 517 the item wasn't found */ 518 518 519 static item** _hashitem_before (const char *key, size_t nkey) {519 static item** _hashitem_before (const char *key, const size_t nkey) { 520 520 uint32_t hv = hash(key, nkey, 0); 521 521 item **pos; … … 604 604 } 605 605 606 void assoc_delete(const char *key, size_t nkey) {606 void assoc_delete(const char *key, const size_t nkey) { 607 607 item **before = _hashitem_before(key, nkey); 608 608 trunk/server/configure.ac
r453 r468 98 98 AC_CHECK_FUNC(daemon,AC_DEFINE([HAVE_DAEMON],,[Define this if you have daemon()]),[AC_LIBOBJ(daemon)]) 99 99 100 100 AC_HEADER_STDBOOL 101 AC_C_CONST 101 102 AC_CHECK_HEADER(malloc.h, AC_DEFINE(HAVE_MALLOC_H,,[do we have malloc.h?])) 102 103 AC_CHECK_MEMBER([struct mallinfo.arena], [ trunk/server/items.c
r450 r468 20 20 #include "memcached.h" 21 21 22 /* Forward Declarations */ 23 static void item_link_q(item *it); 24 static void item_unlink_q(item *it); 25 22 26 /* 23 27 * We only reposition items in the LRU queue if they haven't been repositioned … … 30 34 static item *heads[LARGEST_ID]; 31 35 static item *tails[LARGEST_ID]; 32 unsigned int sizes[LARGEST_ID];36 static unsigned int sizes[LARGEST_ID]; 33 37 34 38 void item_init(void) { … … 54 58 * Returns the total size of the header. 55 59 */ 56 int item_make_header(char *key, uint8_t nkey, int flags, int nbytes, 57 char *suffix, int *nsuffix) { 58 *nsuffix = sprintf(suffix, " %u %u\r\n", flags, nbytes - 2); 60 static size_t item_make_header(char *key, const uint8_t nkey, const int flags, const int nbytes, 61 char *suffix, uint8_t *nsuffix) { 62 /* suffix is defined at 40 chars elsewhere.. */ 63 *nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2); 59 64 return sizeof(item) + nkey + *nsuffix + nbytes; 60 65 } 61 66 62 item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes) { 63 int nsuffix, ntotal; 67 /*@null@*/ 68 item *item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes) { 69 uint8_t nsuffix; 70 size_t ntotal; 64 71 item *it; 65 72 unsigned int id; … … 81 88 */ 82 89 83 if ( !settings.evict_to_free) return 0;90 if (settings.evict_to_free == 0) return NULL; 84 91 85 92 /* … … 90 97 */ 91 98 92 if (id > LARGEST_ID) return 0;93 if (tails[id]==0) return 0;99 if (id > LARGEST_ID) return NULL; 100 if (tails[id]==0) return NULL; 94 101 95 102 for (search = tails[id]; tries>0 && search; tries--, search=search->prev) { … … 100 107 } 101 108 it = slabs_alloc(ntotal); 102 if (it==0) return 0;109 if (it==0) return NULL; 103 110 } 104 111 … … 116 123 strcpy(ITEM_key(it), key); 117 124 it->exptime = exptime; 118 memcpy(ITEM_suffix(it), suffix, nsuffix);125 memcpy(ITEM_suffix(it), suffix, (size_t) nsuffix); 119 126 it->nsuffix = nsuffix; 120 127 return it; … … 122 129 123 130 void item_free(item *it) { 124 unsigned int ntotal = ITEM_ntotal(it);131 size_t ntotal = ITEM_ntotal(it); 125 132 assert((it->it_flags & ITEM_LINKED) == 0); 126 133 assert(it != heads[it->slabs_clsid]); … … 138 145 * the maximum for a cache entry.) 139 146 */ 140 int item_size_ok(char *key, size_t nkey, int flags,int nbytes) {147 bool item_size_ok(char *key, const size_t nkey, const int flags, const int nbytes) { 141 148 char prefix[40]; 142 int nsuffix;149 uint8_t nsuffix; 143 150 144 151 return slabs_clsid(item_make_header(key, nkey + 1, flags, nbytes, … … 146 153 } 147 154 148 void item_link_q(item *it) { /* item is the new head */155 static void item_link_q(item *it) { /* item is the new head */ 149 156 item **head, **tail; 150 157 /* always true, warns: assert(it->slabs_clsid <= LARGEST_ID); */ … … 164 171 } 165 172 166 void item_unlink_q(item *it) {173 static void item_unlink_q(item *it) { 167 174 item **head, **tail; 168 175 /* always true, warns: assert(it->slabs_clsid <= LARGEST_ID); */ … … 204 211 205 212 void item_unlink(item *it) { 206 if ( it->it_flags & ITEM_LINKED) {213 if ((it->it_flags & ITEM_LINKED) != 0) { 207 214 it->it_flags &= ~ITEM_LINKED; 208 215 stats.curr_bytes -= ITEM_ntotal(it); … … 216 223 void item_remove(item *it) { 217 224 assert((it->it_flags & ITEM_SLABBED) == 0); 218 if (it->refcount ) it->refcount--;219 assert((it->it_flags & ITEM_DELETED) == 0 || it->refcount );225 if (it->refcount != 0) it->refcount--; 226 assert((it->it_flags & ITEM_DELETED) == 0 || it->refcount != 0); 220 227 if (it->refcount == 0 && (it->it_flags & ITEM_LINKED) == 0) { 221 228 item_free(it); … … 240 247 } 241 248 242 char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int *bytes) { 243 249 /*@null@*/ 250 char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes) { 244 251 int memlimit = 2*1024*1024; 245 252 char *buffer; 246 int bufcurr;253 unsigned int bufcurr; 247 254 item *it; 248 255 int len; … … 250 257 char temp[512]; 251 258 252 if (slabs_clsid > LARGEST_ID) return 0;259 if (slabs_clsid > LARGEST_ID) return NULL; 253 260 it = heads[slabs_clsid]; 254 261 255 buffer = malloc( memlimit);256 if (buffer == 0) return 0;262 buffer = malloc((size_t)memlimit); 263 if (buffer == 0) return NULL; 257 264 bufcurr = 0; 258 265 259 while (it && (!limit|| shown < limit)) {260 len = s printf(temp, "ITEM %s [%ub; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started);266 while (it != NULL && (limit==0 || shown < limit)) { 267 len = snprintf(temp, 512, "ITEM %s [%d b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 261 268 if (bufcurr + len + 6 > memlimit) /* 6 is END\r\n\0 */ 262 269 break; … … 274 281 } 275 282 276 void item_stats(char *buffer, int buflen) {283 void item_stats(char *buffer, const int buflen) { 277 284 int i; 278 285 char *bufcurr = buffer; … … 286 293 for (i=0; i<LARGEST_ID; i++) { 287 294 if (tails[i]) 288 bufcurr += s printf(bufcurr, "STAT items:%u:number %u\r\nSTAT items:%u:age %u\r\n",295 bufcurr += snprintf(bufcurr, (size_t)buflen, "STAT items:%d:number %u\r\nSTAT items:%d:age %u\r\n", 289 296 i, sizes[i], i, now - tails[i]->time); 290 297 } … … 294 301 295 302 /* dumps out a list of objects of each size, with granularity of 32 bytes */ 303 /*@null@*/ 296 304 char* item_stats_sizes(int *bytes) { 297 int num_buckets = 32768; /* max 1MB object, divided into 32 bytes size buckets */298 unsigned int *histogram = (unsigned int*) malloc( num_buckets * sizeof(int));305 const int num_buckets = 32768; /* max 1MB object, divided into 32 bytes size buckets */ 306 unsigned int *histogram = (unsigned int*) malloc((size_t)num_buckets * sizeof(int)); 299 307 char *buf = (char*) malloc(1024*1024*2*sizeof(char)); 300 308 int i; … … 303 311 if (histogram) free(histogram); 304 312 if (buf) free(buf); 305 return 0;313 return NULL; 306 314 } 307 315 308 316 /* build the histogram */ 309 memset(histogram, 0, num_buckets * sizeof(int));317 memset(histogram, 0, (size_t) num_buckets * sizeof(int)); 310 318 for (i=0; i<LARGEST_ID; i++) { 311 319 item *iter = heads[i]; … … 313 321 int ntotal = ITEM_ntotal(iter); 314 322 int bucket = ntotal / 32; 315 if ( ntotal % 32) bucket++;323 if ((ntotal % 32) != 0) bucket++; 316 324 if (bucket < num_buckets) histogram[bucket]++; 317 325 iter = iter->next; … … 322 330 *bytes = 0; 323 331 for (i=0; i<num_buckets; i++) { 324 if (histogram[i] ) {325 *bytes += sprintf(&buf[*bytes], "% u%u\r\n", i*32, histogram[i]);332 if (histogram[i] != 0) { 333 *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i*32, histogram[i]); 326 334 } 327 335 } … … 335 343 int i; 336 344 item *iter, *next; 337 if ( ! settings.oldest_live)345 if (settings.oldest_live == 0) 338 346 return; 339 347 for (i = 0; i < LARGEST_ID; i++) { trunk/server/memcached.c
r465 r468 67 67 #include "memcached.h" 68 68 69 /* 70 * forward declarations 71 */ 72 static void drive_machine(conn *c); 73 static int new_socket(const bool is_udp); 74 static int server_socket(const int port, const bool is_udp); 75 static int try_read_command(conn *c); 76 static int try_read_network(conn *c); 77 static int try_read_udp(conn *c); 78 79 /* stats */ 80 static void stats_reset(void); 81 static void stats_init(void); 82 83 /* defaults */ 84 static void settings_init(void); 85 86 /* event handling, network IO */ 87 static void event_handler(const int fd, const short which, void *arg); 88 static conn *conn_new(const int sfd, const int init_state, const int event_flags, const int read_buffer_size, const bool is_udp); 89 static void conn_close(conn *c); 90 static void conn_init(void); 91 static void accept_new_conns(const bool do_accept); 92 static bool update_event(conn *c, const int new_flags); 93 static void complete_nread(conn *c); 94 static void process_command(conn *c, char *command); 95 static int transmit(conn *c); 96 static int ensure_iov_space(conn *c); 97 static int add_iov(conn *c, const void *buf, int len); 98 static int add_msghdr(conn *c); 99 100 101 /* time handling */ 102 static void set_current_time(void); /* update the global variable holding 103 global 32-bit seconds-since-start time 104 (to avoid 64 bit time_t) */ 105 106 void pre_gdb(void); 107 108 /** exported globals **/ 69 109 struct stats stats; 70 110 struct settings settings; 71 111 112 /** file scope variables **/ 72 113 static item **todelete = 0; 73 114 static int delcurr; … … 80 121 #define TRANSMIT_HARD_ERROR 3 81 122 82 int *buckets = 0; /* bucket->generation array for a managed instance */123 static int *buckets = 0; /* bucket->generation array for a managed instance */ 83 124 84 125 #define REALTIME_MAXDELTA 60*60*24*30 85 rel_time_t realtime(time_t exptime) { 126 /* 127 * given time value that's either unix time or delta from current unix time, return 128 * unix time. Use the fact that delta can't exceed one month (and real time value can't 129 * be that low). 130 */ 131 static rel_time_t realtime(const time_t exptime) { 86 132 /* no. of seconds in 30 days - largest possible delta exptime */ 87 133 … … 103 149 } 104 150 105 void stats_init(void) {151 static void stats_init(void) { 106 152 stats.curr_items = stats.total_items = stats.curr_conns = stats.total_conns = stats.conn_structs = 0; 107 153 stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = 0; … … 115 161 } 116 162 117 void stats_reset(void) {163 static void stats_reset(void) { 118 164 stats.total_items = stats.total_conns = 0; 119 165 stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = 0; … … 121 167 } 122 168 123 void settings_init(void) {169 static void settings_init(void) { 124 170 settings.port = 11211; 125 171 settings.udpport = 0; … … 131 177 settings.evict_to_free = 1; /* push old items out of cache when memory runs out */ 132 178 settings.socketpath = NULL; /* by default, not using a unix socket */ 133 settings.managed = 0;179 settings.managed = false; 134 180 settings.factor = 1.25; 135 181 settings.chunk_size = 48; /* space for a modest key and value */ … … 138 184 /* returns true if a deleted item's delete-locked-time is over, and it 139 185 should be removed from the namespace */ 140 intitem_delete_lock_over (item *it) {186 static bool item_delete_lock_over (item *it) { 141 187 assert(it->it_flags & ITEM_DELETED); 142 188 return (current_time >= it->exptime); … … 144 190 145 191 /* wrapper around assoc_find which does the lazy expiration/deletion logic */ 146 item *get_item_notedeleted(char *key,size_t nkey, int *delete_locked) {192 static item *get_item_notedeleted(const char *key, const size_t nkey, int *delete_locked) { 147 193 item *it = assoc_find(key, nkey); 194 148 195 if (delete_locked) *delete_locked = 0; 149 if (it && (it->it_flags & ITEM_DELETED)) { 196 197 if (it != NULL && (it->it_flags & ITEM_DELETED)) { 150 198 /* it's flagged as delete-locked. let's see if that condition 151 199 is past due, and the 5-second delete_timer just hasn't … … 156 204 } 157 205 } 158 if (it && settings.oldest_live&& settings.oldest_live <= current_time &&206 if (it != NULL && settings.oldest_live != 0 && settings.oldest_live <= current_time && 159 207 it->time <= settings.oldest_live) { 160 208 item_unlink(it); 161 209 it = 0; 162 210 } 163 if (it && it->exptime&& it->exptime <= current_time) {211 if (it != NULL && it->exptime != 0 && it->exptime <= current_time) { 164 212 item_unlink(it); 165 213 it = 0; … … 168 216 } 169 217 170 item *get_item(char *key,size_t nkey) {218 static item *get_item(const char *key, const size_t nkey) { 171 219 return get_item_notedeleted(key, nkey, 0); 172 220 } … … 177 225 * Returns 0 on success, -1 on out-of-memory. 178 226 */ 179 int add_msghdr(conn *c)227 static int add_msghdr(conn *c) 180 228 { 181 229 struct msghdr *msg; … … 210 258 } 211 259 212 conn **freeconns;213 int freetotal;214 int freecurr;215 216 void conn_init(void) {260 static conn **freeconns; 261 static int freetotal; 262 static int freecurr; 263 264 static void conn_init(void) { 217 265 freetotal = 200; 218 266 freecurr = 0; 219 267 freeconns = (conn **)malloc(sizeof (conn *)*freetotal); 268 /** TODO check for NULL **/ 220 269 return; 221 270 } 222 271 223 conn *conn_new(int sfd, int init_state, int event_flags, int read_buffer_size, 224 int is_udp) { 272 /*@null@*/ 273 static conn *conn_new(const int sfd, const int init_state, const int event_flags, 274 const int read_buffer_size, const bool is_udp) { 225 275 conn *c; 226 276 … … 231 281 if (!(c = (conn *)malloc(sizeof(conn)))) { 232 282 perror("malloc()"); 233 return 0;283 return NULL; 234 284 } 235 285 c->rbuf = c->wbuf = 0; … … 246 296 c->hdrsize = 0; 247 297 248 c->rbuf = (char *) malloc( c->rsize);249 c->wbuf = (char *) malloc( c->wsize);298 c->rbuf = (char *) malloc((size_t)c->rsize); 299 c->wbuf = (char *) malloc((size_t)c->wsize); 250 300 c->ilist = (item **) malloc(sizeof(item *) * c->isize); 251 301 c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize); … … 261 311 free(c); 262 312 perror("malloc()"); 263 return 0;313 return NULL; 264 314 } 265 315 … … 312 362 free (c); 313 363 } 314 return 0;364 return NULL; 315 365 } 316 366 … … 321 371 } 322 372 323 void conn_cleanup(conn *c) {373 static void conn_cleanup(conn *c) { 324 374 if (c->item) { 325 375 item_free(c->item); … … 327 377 } 328 378 329 if (c->ileft ) {379 if (c->ileft != 0) { 330 380 for (; c->ileft > 0; c->ileft--,c->icurr++) { 331 381 item_remove(*(c->icurr)); … … 360 410 } 361 411 362 void conn_close(conn *c) {412 static void conn_close(conn *c) { 363 413 /* delete the event, the socket and the conn */ 364 414 event_del(&c->event); … … 368 418 369 419 close(c->sfd); 370 accept_new_conns( 1);420 accept_new_conns(true); 371 421 conn_cleanup(c); 372 422 … … 379 429 } else { 380 430 /* try to enlarge free connections array */ 381 conn **new_freeconns = realloc( freeconns, sizeof(conn *)*freetotal*2);431 conn **new_freeconns = realloc((void*)freeconns, sizeof(conn *)*freetotal*2); 382 432 if (new_freeconns) { 383 433 freetotal *= 2; … … 409 459 if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { 410 460 if (c->rcurr != c->rbuf) 411 memmove(c->rbuf, c->rcurr, c->rbytes);461 memmove(c->rbuf, c->rcurr, (size_t) c->rbytes); 412 462 413 463 char *newbuf = (char*) realloc((void*)c->rbuf, DATA_BUFFER_SIZE); 464 414 465 if (newbuf) { 415 466 c->rbuf = newbuf; … … 453 504 * happen here. 454 505 */ 455 void conn_set_state(conn *c, int state) {506 static void conn_set_state(conn *c, int state) { 456 507 if (state != c->state) { 457 508 if (state == conn_read) { … … 470 521 * Returns 0 on success, -1 on out-of-memory. 471 522 */ 472 int ensure_iov_space(conn *c) {523 static int ensure_iov_space(conn *c) { 473 524 if (c->iovused >= c->iovsize) { 474 525 int i, iovnum; … … 498 549 */ 499 550 500 int add_iov(conn *c, const void *buf, int len) {551 static int add_iov(conn *c, const void *buf, int len) { 501 552 struct msghdr *m; 502 553 int leftover; 503 intlimit_to_mtu;554 bool limit_to_mtu; 504 555 505 556 do { … … 519 570 } 520 571 521 if (ensure_iov_space(c) )572 if (ensure_iov_space(c) != 0) 522 573 return -1; 523 574 … … 549 600 * Constructs a set of UDP headers and attaches them to the outgoing messages. 550 601 */ 551 int build_udp_headers(conn *c) {602 static int build_udp_headers(conn *c) { 552 603 int i; 553 604 unsigned char *hdr; … … 584 635 585 636 586 void out_string(conn *c,char *str) {637 static void out_string(conn *c, const char *str) { 587 638 int len; 588 639 … … 612 663 */ 613 664 614 void complete_nread(conn *c) {665 static void complete_nread(conn *c) { 615 666 item *it = c->item; 616 667 int comm = c->item_comm; … … 628 679 old_it = get_item_notedeleted(key, it->nkey, &delete_locked); 629 680 630 if (old_it && comm == NREAD_ADD) {681 if (old_it != NULL && comm == NREAD_ADD) { 631 682 item_update(old_it); /* touches item, promotes to head of LRU */ 632 683 out_string(c, "NOT_STORED"); … … 634 685 } 635 686 636 if ( !old_it&& comm == NREAD_REPLACE) {687 if (old_it == NULL && comm == NREAD_REPLACE) { 637 688 out_string(c, "NOT_STORED"); 638 689 goto err; 639 690 } 640 691 641 if (delete_locked ) {692 if (delete_locked != 0) { 642 693 if (comm == NREAD_REPLACE || comm == NREAD_ADD) { 643 694 out_string(c, "NOT_STORED"); … … 696 747 * } 697 748 */ 698 s ize_t tokenize_command(char* command, token_t* tokens,size_t max_tokens) {749 static size_t tokenize_command(char* command, token_t* tokens, const size_t max_tokens) { 699 750 char* cp; 700 751 char* value = NULL; … … 742 793 } 743 794 744 void process_stat(conn *c, token_t* tokens,size_t ntokens) {795 static void process_stat(conn *c, token_t* tokens, const size_t ntokens) { 745 796 rel_time_t now = current_time; 746 797 char* command; … … 932 983 } 933 984 934 inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 985 /* ntokens is overwritten here... shrug.. */ 986 static inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 935 987 char *key; 936 988 size_t nkey; … … 981 1033 * " " + flags + " " + data length + "\r\n" + data (with \r\n) 982 1034 */ 983 if (add_iov(c, "VALUE ", 6) ||984 add_iov(c, ITEM_key(it), it->nkey) ||985 add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) )1035 if (add_iov(c, "VALUE ", 6) != 0 || 1036 add_iov(c, ITEM_key(it), it->nkey) != 0 || 1037 add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) != 0) 986 1038 { 987 1039 break; … … 1019 1071 add_iov(c, "END\r\n", 5); 1020 1072 1021 if (c->udp && build_udp_headers(c) ) {1073 if (c->udp && build_udp_headers(c) != 0) { 1022 1074 out_string(c, "SERVER_ERROR out of memory"); 1023 1075 } … … 1029 1081 } 1030 1082 1031 void process_update_command(conn *c, token_t* tokens,size_t ntokens, int comm) {1083 static void process_update_command(conn *c, token_t* tokens, const size_t ntokens, int comm) { 1032 1084 char *key; 1033 1085 size_t nkey; … … 1088 1140 } 1089 1141 1090 void process_arithmetic_command(conn *c, token_t* tokens, size_t ntokens,int incr) {1142 static void process_arithmetic_command(conn *c, token_t* tokens, const size_t ntokens, const int incr) { 1091 1143 char temp[32]; 1092 1144 unsigned int value; … … 1133 1185 1134 1186 ptr = ITEM_data(it); 1135 while ( *ptr&& (*ptr<'0' && *ptr>'9')) ptr++; // BUG: can't be true1187 while ((*ptr != '\0') && (*ptr<'0' && *ptr>'9')) ptr++; // BUG: can't be true 1136 1188 1137 1189 value = strtol(ptr, NULL, 10); … … 1142 1194 } 1143 1195 1144 if (incr )1196 if (incr != 0) 1145 1197 value+=delta; 1146 1198 else { … … 1148 1200 else value-=delta; 1149 1201 } 1150 s printf(temp, "%u", value);1202 snprintf(temp, 32, "%u", value); 1151 1203 res = strlen(temp); 1152 1204 if (res + 2 > it->nbytes) { /* need to realloc */ … … 1168 1220 } 1169 1221 1170 void process_delete_command(conn *c, token_t* tokens,size_t ntokens) {1222 static void process_delete_command(conn *c, token_t* tokens, const size_t ntokens) { 1171 1223 char *key; 1172 1224 size_t nkey; … … 1239 1291 } 1240 1292 1241 void process_command(conn *c, char *command) {1293 static void process_command(conn *c, char *command) { 1242 1294 1243 1295 token_t tokens[MAX_TOKENS]; … … 1256 1308 c->msgused = 0; 1257 1309 c->iovused = 0; 1258 if (add_msghdr(c) ) {1310 if (add_msghdr(c) != 0) { 1259 1311 out_string(c, "SERVER_ERROR out of memory"); 1260 1312 return; … … 1423 1475 * if we have a complete line in the buffer, process it. 1424 1476 */ 1425 int try_read_command(conn *c) {1477 static int try_read_command(conn *c) { 1426 1478 char *el, *cont; 1427 1479 1428 1480 assert(c->rcurr <= c->rbuf + c->rsize); 1429 1481 1430 if ( !c->rbytes)1482 if (c->rbytes == 0) 1431 1483 return 0; 1432 1484 el = memchr(c->rcurr, '\n', c->rbytes); … … 1455 1507 * return 0 if there's nothing to read. 1456 1508 */ 1457 int try_read_udp(conn *c) {1509 static int try_read_udp(conn *c) { 1458 1510 int res; 1459 1511 … … 1492 1544 * return 0 if there's nothing to read on the first read. 1493 1545 */ 1494 int try_read_network(conn *c) {1546 static int try_read_network(conn *c) { 1495 1547 int gotdata = 0; 1496 1548 int res; … … 1546 1598 } 1547 1599 1548 int update_event(conn *c,int new_flags) {1600 static bool update_event(conn *c, const int new_flags) { 1549 1601 if (c->ev_flags == new_flags) 1550 return 1;1551 if (event_del(&c->event) == -1) return 0;1602 return true; 1603 if (event_del(&c->event) == -1) return false; 1552 1604 event_set(&c->event, c->sfd, new_flags, event_handler, (void *)c); 1553 1605 c->ev_flags = new_flags; 1554 if (event_add(&c->event, 0) == -1) return 0;1555 return 1;1606 if (event_add(&c->event, 0) == -1) return false; 1607 return true; 1556 1608 } 1557 1609 … … 1559 1611 * Sets whether we are listening for new connections or not. 1560 1612 */ 1561 void accept_new_conns( intdo_accept) {1613 void accept_new_conns(const bool do_accept) { 1562 1614 if (do_accept) { 1563 1615 update_event(listen_conn, EV_READ | EV_PERSIST); 1564 if (listen(listen_conn->sfd, 1024) ) {1616 if (listen(listen_conn->sfd, 1024) != 0) { 1565 1617 perror("listen"); 1566 1618 } … … 1568 1620 else { 1569 1621 update_event(listen_conn, 0); 1570 if (listen(listen_conn->sfd, 0) ) {1622 if (listen(listen_conn->sfd, 0) != 0) { 1571 1623 perror("listen"); 1572 1624 } … … 1584 1636 * TRANSMIT_HARD_ERROR Can't write (c->state is set to conn_closing) 1585 1637 */ 1586 int transmit(conn *c) {1638 static int transmit(conn *c) { 1587 1639 int res; 1588 1640 … … 1638 1690 } 1639 1691 1640 void drive_machine(conn *c) { 1641 1642 int stop = 0; 1692 static void drive_machine(conn *c) { 1693 bool stop = false; 1643 1694 int sfd, flags = 1; 1644 1695 socklen_t addrlen; … … 1653 1704 if ((sfd = accept(c->sfd, &addr, &addrlen)) == -1) { 1654 1705 if (errno == EAGAIN || errno == EWOULDBLOCK) { 1655 stop = 1;1706 stop = true; 1656 1707 break; 1657 1708 } else if (errno == EMFILE) { 1658 1709 if (settings.verbose > 0) 1659 1710 fprintf(stderr, "Too many open connections\n"); 1660 accept_new_conns( 0);1711 accept_new_conns(false); 1661 1712 } else { 1662 1713 perror("accept()"); … … 1671 1722 } 1672 1723 newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST, 1673 DATA_BUFFER_SIZE, 0);1724 DATA_BUFFER_SIZE, false); 1674 1725 if (!newc) { 1675 1726 if (settings.verbose > 0) … … 1682 1733 1683 1734 case conn_read: 1684 if (try_read_command(c) ) {1735 if (try_read_command(c) != 0) { 1685 1736 continue; 1686 1737 } 1687 if ( c->udp ? try_read_udp(c) : try_read_network(c)) {1738 if ((c->udp ? try_read_udp(c) : try_read_network(c)) != 0) { 1688 1739 continue; 1689 1740 } … … 1695 1746 break; 1696 1747 } 1697 stop = 1;1748 stop = true; 1698 1749 break; 1699 1750 … … 1734 1785 break; 1735 1786 } 1736 stop = 1;1787 stop = true; 1737 1788 break; 1738 1789 } … … 1777 1828 break; 1778 1829 } 1779 stop = 1;1830 stop = true; 1780 1831 break; 1781 1832 } … … 1793 1844 */ 1794 1845 if (c->iovused == 0 || (c->udp && c->iovused == 1)) { 1795 if (add_iov(c, c->wcurr, c->wbytes) ||1796 (c->udp && build_udp_headers(c) )) {1846 if (add_iov(c, c->wcurr, c->wbytes) != 0 || 1847 (c->udp && build_udp_headers(c) != 0)) { 1797 1848 if (settings.verbose > 0) 1798 1849 fprintf(stderr, "Couldn't build response\n"); … … 1834 1885 1835 1886 case TRANSMIT_SOFT_ERROR: 1836 stop = 1;1887 stop = true; 1837 1888 break; 1838 1889 } … … 1844 1895 else 1845 1896 conn_close(c); 1846 stop = 1;1897 stop = true; 1847 1898 break; 1848 1899 } … … 1853 1904 } 1854 1905 1855 void event_handler( int fd,short which, void *arg) {1906 void event_handler(const int fd, const short which, void *arg) { 1856 1907 conn *c; 1857 1908 … … 1874 1925 } 1875 1926 1876 int new_socket(intis_udp) {1927 static int new_socket(const bool is_udp) { 1877 1928 int sfd; 1878 1929 int flags; … … 1896 1947 * Sets a socket's send buffer size to the maximum allowed by the system. 1897 1948 */ 1898 void maximize_sndbuf(int sfd) {1949 static void maximize_sndbuf(const int sfd) { 1899 1950 socklen_t intsize = sizeof(int); 1900 1951 int last_good = 0; … … 1903 1954 1904 1955 /* Start with the default size. */ 1905 if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize) ) {1956 if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize) != 0) { 1906 1957 if (settings.verbose > 0) 1907 1958 perror("getsockopt(SO_SNDBUF)"); … … 1928 1979 1929 1980 1930 int server_socket(int port, intis_udp) {1981 static int server_socket(const int port, const bool is_udp) { 1931 1982 int sfd; 1932 1983 struct linger ling = {0, 0}; … … 1961 2012 return -1; 1962 2013 } 1963 if (! is_udp && listen(sfd, 1024) == -1) {2014 if (!is_udp && listen(sfd, 1024) == -1) { 1964 2015 perror("listen()"); 1965 2016 close(sfd); … … 1969 2020 } 1970 2021 1971 int new_socket_unix(void) {2022 static int new_socket_unix(void) { 1972 2023 int sfd; 1973 2024 int flags; … … 1987 2038 } 1988 2039 1989 int server_socket_unix(char *path) {2040 static int server_socket_unix(char *path) { 1990 2041 int sfd; 1991 2042 struct linger ling = {0, 0}; … … 2005 2056 * Clean up a previous socket file if we left it around 2006 2057 */ 2007 if ( !lstat(path, &tstat)) {2058 if (lstat(path, &tstat) == 0) { 2008 2059 if (S_ISSOCK(tstat.st_mode)) 2009 2060 unlink(path); … … 2035 2086 } 2036 2087 2088 /* listening socket */ 2089 static int l_socket=0; 2090 2091 /* udp socket */ 2092 static int u_socket=-1; 2037 2093 2038 2094 /* invoke right before gdb is called, on assert */ 2039 void pre_gdb () {2095 void pre_gdb(void) { 2040 2096 int i = 0; 2041 if (l_socket) close(l_socket);2042 if (u_socket > -1) close(u_socket);2097 if (l_socket > -1) close(l_socket); 2098 if (u_socket > -1) close(u_socket); 2043 2099 for (i=3; i<=500; i++) close(i); /* so lame */ 2044 2100 kill(getpid(), SIGABRT); … … 2054 2110 */ 2055 2111 volatile rel_time_t current_time; 2056 st ruct event clockevent;2112 static struct event clockevent; 2057 2113 2058 2114 /* time-sensitive callers can call it by hand with this, outside the normal ever-1-second timer */ 2059 void set_current_time () {2115 static void set_current_time(void) { 2060 2116 current_time = (rel_time_t) (time(0) - stats.started); 2061 2117 } 2062 2118 2063 void clock_handler(int fd,short which, void *arg) {2064 struct timeval t ;2065 static int initialized = 0;2119 static void clock_handler(const int fd, const short which, void *arg) { 2120 struct timeval t = {.tv_sec = 1, .tv_usec = 0};
