annotate src/c/driver.c @ 796:6271f0e3c272

Fix a nasty binding bug in CoreUtil
author Adam Chlipala <adamc@hcoop.net>
date Thu, 14 May 2009 09:11:58 -0400
parents 3e5d1c6ae30c
children 5f49a6b759cb
rev   line source
adamc@737 1 #define _GNU_SOURCE
adamc@737 2
adamc@116 3 #include <stdio.h>
adamc@116 4 #include <string.h>
adamc@502 5 #include <stdlib.h>
adamc@116 6 #include <sys/types.h>
adamc@116 7 #include <sys/socket.h>
adamc@116 8 #include <netinet/in.h>
adamc@472 9 #include <unistd.h>
adamc@502 10 #include <signal.h>
adamc@116 11
adamc@138 12 #include <pthread.h>
adamc@138 13
adamc@734 14 #include <mhash.h>
adamc@734 15
adamc@244 16 #include "urweb.h"
adamc@117 17
adamc@311 18 int uw_backlog = 10;
adamc@311 19 int uw_bufsize = 1024;
adamc@116 20
adamc@138 21 typedef struct node {
adamc@138 22 int fd;
adamc@138 23 struct node *next;
adamc@138 24 } *node;
adamc@138 25
adamc@138 26 static node front = NULL, back = NULL;
adamc@138 27
adamc@138 28 static int empty() {
adamc@138 29 return front == NULL;
adamc@138 30 }
adamc@138 31
adamc@138 32 static void enqueue(int fd) {
adamc@138 33 node n = malloc(sizeof(struct node));
adamc@138 34
adamc@138 35 n->fd = fd;
adamc@138 36 n->next = NULL;
adamc@138 37 if (back)
adamc@138 38 back->next = n;
adamc@138 39 else
adamc@138 40 front = n;
adamc@138 41 back = n;
adamc@138 42 }
adamc@138 43
adamc@138 44 static int dequeue() {
adamc@138 45 int ret = front->fd;
adamc@138 46
adamc@138 47 front = front->next;
adamc@138 48 if (!front)
adamc@138 49 back = NULL;
adamc@138 50
adamc@138 51 return ret;
adamc@138 52 }
adamc@138 53
adamc@138 54 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@138 55 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
adamc@138 56
adamc@167 57 #define MAX_RETRIES 5
adamc@167 58
adamc@424 59 static int try_rollback(uw_context ctx) {
adamc@671 60 int r = uw_rollback(ctx);
adamc@424 61
adamc@424 62 if (r) {
adamc@424 63 printf("Error running SQL ROLLBACK\n");
adamc@424 64 uw_reset(ctx);
adamc@424 65 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@424 66 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@424 67 uw_write(ctx, "Error running SQL ROLLBACK\n");
adamc@424 68 }
adamc@424 69
adamc@424 70 return r;
adamc@424 71 }
adamc@424 72
adamc@698 73 static uw_context new_context() {
adamc@687 74 uw_context ctx = uw_init();
adamc@698 75 int retries_left = MAX_RETRIES;
adamc@698 76
adamc@272 77 while (1) {
adamc@311 78 failure_kind fk = uw_begin_init(ctx);
adamc@272 79
adamc@272 80 if (fk == SUCCESS) {
adamc@272 81 printf("Database connection initialized.\n");
adamc@272 82 break;
adamc@272 83 } else if (fk == BOUNDED_RETRY) {
adamc@272 84 if (retries_left) {
adamc@311 85 printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@272 86 --retries_left;
adamc@272 87 } else {
adamc@311 88 printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
adamc@311 89 uw_free(ctx);
adamc@272 90 return NULL;
adamc@272 91 }
adamc@272 92 } else if (fk == UNLIMITED_RETRY)
adamc@311 93 printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@272 94 else if (fk == FATAL) {
adamc@311 95 printf("Fatal initialization error: %s\n", uw_error_message(ctx));
adamc@311 96 uw_free(ctx);
adamc@272 97 return NULL;
adamc@272 98 } else {
adamc@698 99 printf("Unknown uw_begin_init return code!\n");
adamc@311 100 uw_free(ctx);
adamc@272 101 return NULL;
adamc@272 102 }
adamc@272 103 }
adamc@116 104
adamc@698 105 return ctx;
adamc@698 106 }
adamc@698 107
adamc@734 108 #define KEYSIZE 16
adamc@734 109 #define PASSSIZE 4
adamc@734 110
adamc@734 111 #define HASH_ALGORITHM MHASH_SHA256
adamc@734 112 #define HASH_BLOCKSIZE 32
adamc@734 113 #define KEYGEN_ALGORITHM KEYGEN_MCRYPT
adamc@734 114
adamc@734 115 int uw_hash_blocksize = HASH_BLOCKSIZE;
adamc@734 116
adamc@734 117 static int password[PASSSIZE];
adamc@734 118 static unsigned char private_key[KEYSIZE];
adamc@734 119
adamc@734 120 static void init_crypto() {
adamc@734 121 KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
adamc@734 122 int i;
adamc@734 123
adamc@734 124 assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
adamc@734 125
adamc@734 126 for (i = 0; i < PASSSIZE; ++i)
adamc@734 127 password[i] = rand();
adamc@734 128
adamc@734 129 if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
adamc@734 130 private_key, sizeof(private_key),
adamc@734 131 (unsigned char*)password, sizeof(password)) < 0) {
adamc@734 132 printf("Key generation failed\n");
adamc@734 133 exit(1);
adamc@734 134 }
adamc@734 135 }
adamc@734 136
adamc@734 137 void uw_sign(const char *in, char *out) {
adamc@734 138 MHASH td;
adamc@734 139
adamc@734 140 td = mhash_hmac_init(HASH_ALGORITHM, private_key, sizeof(private_key),
adamc@734 141 mhash_get_hash_pblock(HASH_ALGORITHM));
adamc@734 142
adamc@734 143 mhash(td, in, strlen(in));
adamc@734 144 if (mhash_hmac_deinit(td, out) < 0)
adamc@734 145 printf("Signing failed");
adamc@734 146 }
adamc@734 147
adamc@698 148 static void *worker(void *data) {
adamc@698 149 int me = *(int *)data, retries_left = MAX_RETRIES;
adamc@698 150 uw_context ctx = new_context();
adamc@742 151 size_t buf_size = 2;
adamc@737 152 char *buf = malloc(buf_size);
adamc@793 153 size_t path_copy_size = 0;
adamc@793 154 char *path_copy = malloc(path_copy_size);
adamc@698 155
adamc@116 156 while (1) {
adamc@737 157 char *back = buf, *s, *post;
adamc@667 158 int sock, dont_close = 0;
adamc@116 159
adamc@138 160 pthread_mutex_lock(&queue_mutex);
adamc@138 161 while (empty())
adamc@138 162 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 163 sock = dequeue();
adamc@138 164 pthread_mutex_unlock(&queue_mutex);
adamc@138 165
adamc@138 166 printf("Handling connection with thread #%d.\n", me);
adamc@138 167
adamc@138 168 while (1) {
adamc@167 169 unsigned retries_left = MAX_RETRIES;
adamc@737 170 int r;
adamc@737 171
adamc@742 172 if (back - buf == buf_size - 1) {
adamc@737 173 char *new_buf;
adamc@737 174 buf_size *= 2;
adamc@737 175 new_buf = realloc(buf, buf_size);
adamc@737 176 back = new_buf + (back - buf);
adamc@737 177 buf = new_buf;
adamc@737 178 }
adamc@737 179
adamc@742 180 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@138 181
adamc@138 182 if (r < 0) {
adamc@138 183 fprintf(stderr, "Recv failed\n");
adamc@138 184 break;
adamc@138 185 }
adamc@138 186
adamc@138 187 if (r == 0) {
adamc@138 188 printf("Connection closed.\n");
adamc@138 189 break;
adamc@138 190 }
adamc@138 191
adamc@730 192 //printf("Received %d bytes.\n", r);
adamc@138 193
adamc@138 194 back += r;
adamc@138 195 *back = 0;
adamc@730 196
adamc@138 197 if (s = strstr(buf, "\r\n\r\n")) {
adamc@424 198 failure_kind fk;
adamc@741 199 int is_post = 0, do_normal_send = 1;
adamc@737 200 char *boundary = NULL;
adamc@737 201 size_t boundary_len;
adamc@793 202 char *cmd, *path, *headers, *inputs, *after_headers;
adamc@138 203
adamc@737 204 //printf("All: %s\n", buf);
adamc@737 205
adamc@475 206 s[2] = 0;
adamc@730 207 after_headers = s + 4;
adamc@144 208
adamc@138 209 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 210 fprintf(stderr, "No newline in buf\n");
adamc@138 211 break;
adamc@138 212 }
adamc@138 213
adamc@138 214 *s = 0;
adamc@457 215 headers = s + 2;
adamc@138 216 cmd = s = buf;
adamc@401 217
adamc@737 218 //printf("Read: %s\n", buf);
adamc@138 219
adamc@138 220 if (!strsep(&s, " ")) {
adamc@138 221 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 222 break;
adamc@138 223 }
adamc@138 224
adamc@730 225 uw_set_headers(ctx, headers);
adamc@730 226
adamc@730 227 if (!strcmp(cmd, "POST")) {
adamc@730 228 char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
adamc@730 229 if (!clen_s) {
adamc@737 230 fprintf(stderr, "No Content-length with POST\n");
adamc@730 231 goto done;
adamc@730 232 }
adamc@730 233 int clen = atoi(clen_s);
adamc@730 234 if (clen < 0) {
adamc@737 235 fprintf(stderr, "Negative Content-length with POST\n");
adamc@730 236 goto done;
adamc@730 237 }
adamc@730 238
adamc@730 239 while (back - after_headers < clen) {
adamc@742 240 if (back - buf == buf_size - 1) {
adamc@737 241 char *new_buf;
adamc@737 242 buf_size *= 2;
adamc@737 243 new_buf = realloc(buf, buf_size);
adamc@742 244
adamc@737 245 back = new_buf + (back - buf);
adamc@742 246 headers = new_buf + (headers - buf);
adamc@742 247 uw_headers_moved(ctx, headers);
adamc@742 248 after_headers = new_buf + (after_headers - buf);
adamc@742 249 s = new_buf + (s - buf);
adamc@742 250
adamc@737 251 buf = new_buf;
adamc@737 252 }
adamc@737 253
adamc@742 254 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@730 255
adamc@730 256 if (r < 0) {
adamc@730 257 fprintf(stderr, "Recv failed\n");
adamc@730 258 goto done;
adamc@730 259 }
adamc@730 260
adamc@730 261 if (r == 0) {
adamc@730 262 printf("Connection closed.\n");
adamc@730 263 goto done;
adamc@730 264 }
adamc@730 265
adamc@730 266 back += r;
adamc@730 267 *back = 0;
adamc@730 268 }
adamc@730 269
adamc@730 270 is_post = 1;
adamc@737 271
adamc@737 272 clen_s = uw_Basis_requestHeader(ctx, "Content-type");
adamc@737 273 if (clen_s && !strncasecmp(clen_s, "multipart/form-data", 19)) {
adamc@737 274 if (strncasecmp(clen_s + 19, "; boundary=", 11)) {
adamc@737 275 fprintf(stderr, "Bad multipart boundary spec");
adamc@737 276 break;
adamc@737 277 }
adamc@737 278
adamc@737 279 boundary = clen_s + 28;
adamc@737 280 boundary[0] = '-';
adamc@737 281 boundary[1] = '-';
adamc@737 282 boundary_len = strlen(boundary);
adamc@737 283 }
adamc@730 284 } else if (strcmp(cmd, "GET")) {
adamc@730 285 fprintf(stderr, "Not ready for non-GET/POST command: %s\n", cmd);
adamc@138 286 break;
adamc@138 287 }
adamc@138 288
adamc@138 289 path = s;
adamc@138 290 if (!strsep(&s, " ")) {
adamc@138 291 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 292 break;
adamc@138 293 }
adamc@138 294
adamc@668 295 if (!strcmp(path, "/.msgs")) {
adamc@668 296 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
adamc@668 297 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
adamc@668 298
adamc@668 299 if (id && pass) {
adamc@682 300 unsigned idn = atoi(id);
adamc@668 301 uw_client_connect(idn, atoi(pass), sock);
adamc@668 302 dont_close = 1;
adamc@682 303 fprintf(stderr, "Processed request for messages by client %u\n\n", idn);
adamc@668 304 }
adamc@703 305 else {
adamc@703 306 fprintf(stderr, "Missing fields in .msgs request: %s, %s\n\n", id, pass);
adamc@703 307 }
adamc@667 308 break;
adamc@667 309 }
adamc@667 310
adamc@737 311 if (boundary) {
adamc@737 312 char *part = after_headers, *after_sub_headers, *header, *after_header;
adamc@737 313 size_t part_len;
adamc@144 314
adamc@737 315 part = strstr(part, boundary);
adamc@737 316 if (!part) {
adamc@737 317 fprintf(stderr, "Missing first multipart boundary\n");
adamc@737 318 break;
adamc@737 319 }
adamc@737 320 part += boundary_len;
adamc@145 321
adamc@737 322 while (1) {
adamc@737 323 char *name = NULL, *filename = NULL, *type = NULL;
adamc@737 324
adamc@737 325 if (part[0] == '-' && part[1] == '-')
adamc@737 326 break;
adamc@737 327
adamc@737 328 if (*part != '\r') {
adamc@737 329 fprintf(stderr, "No \\r after multipart boundary\n");
adamc@737 330 goto done;
adamc@144 331 }
adamc@737 332 ++part;
adamc@737 333 if (*part != '\n') {
adamc@737 334 fprintf(stderr, "No \\n after multipart boundary\n");
adamc@737 335 goto done;
adamc@737 336 }
adamc@737 337 ++part;
adamc@737 338
adamc@737 339 if (!(after_sub_headers = strstr(part, "\r\n\r\n"))) {
adamc@737 340 fprintf(stderr, "Missing end of headers after multipart boundary\n");
adamc@737 341 goto done;
adamc@737 342 }
adamc@737 343 after_sub_headers[2] = 0;
adamc@737 344 after_sub_headers += 4;
adamc@737 345
adamc@737 346 for (header = part; after_header = strstr(header, "\r\n"); header = after_header + 2) {
adamc@737 347 char *colon, *after_colon;
adamc@737 348
adamc@737 349 *after_header = 0;
adamc@737 350 if (!(colon = strchr(header, ':'))) {
adamc@737 351 fprintf(stderr, "Missing colon in multipart sub-header\n");
adamc@737 352 goto done;
adamc@737 353 }
adamc@737 354 *colon++ = 0;
adamc@737 355 if (*colon++ != ' ') {
adamc@737 356 fprintf(stderr, "No space after colon in multipart sub-header\n");
adamc@737 357 goto done;
adamc@737 358 }
adamc@737 359
adamc@737 360 if (!strcasecmp(header, "Content-Disposition")) {
adamc@737 361 if (strncmp(colon, "form-data; ", 11)) {
adamc@737 362 fprintf(stderr, "Multipart data is not \"form-data\"\n");
adamc@737 363 goto done;
adamc@737 364 }
adamc@737 365
adamc@737 366 for (colon += 11; after_colon = strchr(colon, '='); colon = after_colon) {
adamc@737 367 char *data;
adamc@737 368 after_colon[0] = 0;
adamc@737 369 if (after_colon[1] != '"') {
adamc@737 370 fprintf(stderr, "Disposition setting is missing initial quote\n");
adamc@737 371 goto done;
adamc@737 372 }
adamc@737 373 data = after_colon+2;
adamc@737 374 if (!(after_colon = strchr(data, '"'))) {
adamc@737 375 fprintf(stderr, "Disposition setting is missing final quote\n");
adamc@737 376 goto done;
adamc@737 377 }
adamc@737 378 after_colon[0] = 0;
adamc@737 379 ++after_colon;
adamc@737 380 if (after_colon[0] == ';' && after_colon[1] == ' ')
adamc@737 381 after_colon += 2;
adamc@737 382
adamc@737 383 if (!strcasecmp(colon, "name"))
adamc@737 384 name = data;
adamc@737 385 else if (!strcasecmp(colon, "filename"))
adamc@737 386 filename = data;
adamc@737 387 }
adamc@737 388 } else if (!strcasecmp(header, "Content-Type")) {
adamc@737 389 type = colon;
adamc@737 390 }
adamc@737 391 }
adamc@737 392
adamc@737 393 part = memmem(after_sub_headers, back - after_sub_headers, boundary, boundary_len);
adamc@737 394 if (!part) {
adamc@737 395 fprintf(stderr, "Missing boundary after multipart payload\n");
adamc@737 396 goto done;
adamc@737 397 }
adamc@737 398 part[-2] = 0;
adamc@737 399 part_len = part - after_sub_headers - 2;
adamc@737 400 part[0] = 0;
adamc@737 401 part += boundary_len;
adamc@737 402
adamc@737 403 if (filename) {
adamc@740 404 uw_Basis_file f = {filename, type, {part_len, after_sub_headers}};
adamc@737 405
adamc@739 406 uw_set_file_input(ctx, name, f);
adamc@737 407 } else
adamc@737 408 uw_set_input(ctx, name, after_sub_headers);
adamc@737 409 }
adamc@737 410 }
adamc@737 411 else {
adamc@737 412 if (is_post)
adamc@737 413 inputs = after_headers;
adamc@737 414 else if (inputs = strchr(path, '?'))
adamc@737 415 *inputs++ = 0;
adamc@737 416
adamc@737 417 if (inputs) {
adamc@737 418 char *name, *value;
adamc@737 419
adamc@737 420 while (*inputs) {
adamc@737 421 name = inputs;
adamc@737 422 if (inputs = strchr(inputs, '&'))
adamc@737 423 *inputs++ = 0;
adamc@737 424 else
adamc@737 425 inputs = strchr(name, 0);
adamc@737 426
adamc@737 427 if (value = strchr(name, '=')) {
adamc@737 428 *value++ = 0;
adamc@737 429 uw_set_input(ctx, name, value);
adamc@737 430 }
adamc@737 431 else
adamc@737 432 uw_set_input(ctx, name, "");
adamc@737 433 }
adamc@144 434 }
adamc@144 435 }
adamc@144 436
adamc@138 437 printf("Serving URI %s....\n", path);
adamc@138 438
adamc@167 439 while (1) {
adamc@793 440 size_t path_len = strlen(path);
adamc@793 441
adamc@462 442 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@167 443
adamc@793 444 if (path_len + 1 > path_copy_size) {
adamc@793 445 path_copy_size = path_len + 1;
adamc@793 446 path_copy = realloc(path_copy, path_copy_size);
adamc@793 447 }
adamc@400 448 strcpy(path_copy, path);
adamc@458 449 fk = uw_begin(ctx, path_copy);
adamc@741 450 if (fk == SUCCESS || fk == RETURN_BLOB) {
adamc@671 451 uw_commit(ctx);
adamc@167 452 break;
adamc@167 453 } else if (fk == BOUNDED_RETRY) {
adamc@167 454 if (retries_left) {
adamc@311 455 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@167 456 --retries_left;
adamc@167 457 }
adamc@167 458 else {
adamc@311 459 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@167 460
adamc@682 461 try_rollback(ctx);
adamc@682 462
adamc@311 463 uw_reset_keep_error_message(ctx);
adamc@464 464 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 465 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 466 uw_write(ctx, "Fatal error (out of retries): ");
adamc@311 467 uw_write(ctx, uw_error_message(ctx));
adamc@311 468 uw_write(ctx, "\n");
adamc@424 469
adamc@424 470 break;
adamc@167 471 }
adamc@167 472 } else if (fk == UNLIMITED_RETRY)
adamc@311 473 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@167 474 else if (fk == FATAL) {
adamc@311 475 printf("Fatal error: %s\n", uw_error_message(ctx));
adamc@167 476
adamc@682 477 try_rollback(ctx);
adamc@682 478
adamc@311 479 uw_reset_keep_error_message(ctx);
adamc@464 480 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@548 481 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@548 482 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@311 483 uw_write(ctx, "Fatal error: ");
adamc@311 484 uw_write(ctx, uw_error_message(ctx));
adamc@548 485 uw_write(ctx, "\n</body></html>");
adamc@167 486
adamc@167 487 break;
adamc@167 488 } else {
adamc@311 489 printf("Unknown uw_handle return code!\n");
adamc@167 490
adamc@682 491 try_rollback(ctx);
adamc@682 492
adamc@311 493 uw_reset_keep_request(ctx);
adamc@464 494 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 495 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 496 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@167 497
adamc@167 498 break;
adamc@167 499 }
adamc@167 500
adamc@424 501 if (try_rollback(ctx))
adamc@424 502 break;
adamc@682 503
adamc@682 504 uw_reset_keep_request(ctx);
adamc@167 505 }
adamc@138 506
adamc@311 507 uw_send(ctx, sock);
adamc@138 508
adamc@138 509 printf("Done with client.\n\n");
adamc@324 510 uw_memstats(ctx);
adamc@138 511 break;
adamc@138 512 }
adamc@116 513 }
adamc@116 514
adamc@730 515 done:
adamc@667 516 if (!dont_close)
adamc@667 517 close(sock);
adamc@311 518 uw_reset(ctx);
adamc@116 519 }
adamc@116 520 }
adamc@102 521
adamc@667 522 static void *client_pruner(void *data) {
adamc@698 523 uw_context ctx = new_context();
adamc@698 524
adamc@698 525 if (!ctx)
adamc@698 526 exit(1);
adamc@683 527
adamc@667 528 while (1) {
adamc@683 529 uw_prune_clients(ctx);
adamc@667 530 sleep(5);
adamc@667 531 }
adamc@667 532 }
adamc@667 533
adamc@477 534 static void help(char *cmd) {
adamc@477 535 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 536 }
adamc@477 537
adamc@502 538 static void sigint(int signum) {
adamc@502 539 printf("Exiting....\n");
adamc@502 540 exit(0);
adamc@502 541 }
adamc@502 542
adamc@687 543 static void initialize() {
adamc@734 544 uw_context ctx;
adamc@700 545 failure_kind fk;
adamc@687 546
adamc@734 547 init_crypto();
adamc@734 548
adamc@734 549 ctx = new_context();
adamc@734 550
adamc@698 551 if (!ctx)
adamc@698 552 exit(1);
adamc@698 553
adamc@700 554 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
adamc@700 555 printf("Unlimited retry during init: %s\n", uw_error_message(ctx));
adamc@700 556 uw_db_rollback(ctx);
adamc@700 557 uw_reset(ctx);
adamc@700 558 }
adamc@700 559
adamc@700 560 if (fk != SUCCESS) {
adamc@700 561 printf("Failed to initialize database! %s\n", uw_error_message(ctx));
adamc@687 562 uw_db_rollback(ctx);
adamc@687 563 exit(1);
adamc@687 564 }
adamc@687 565
adamc@687 566 uw_free(ctx);
adamc@687 567 }
adamc@687 568
adamc@138 569 int main(int argc, char *argv[]) {
adamc@116 570 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 571 int sockfd; // listen on sock_fd
adamc@116 572 struct sockaddr_in my_addr;
adamc@116 573 struct sockaddr_in their_addr; // connector's address information
adamc@116 574 int sin_size, yes = 1;
adamc@472 575 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 576
adamc@502 577 signal(SIGINT, sigint);
adamc@505 578 signal(SIGPIPE, SIG_IGN);
adamc@505 579
adamc@477 580 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 581 switch (opt) {
adamc@472 582 case '?':
adamc@472 583 fprintf(stderr, "Unknown command-line option");
adamc@477 584 help(argv[0]);
adamc@472 585 return 1;
adamc@138 586
adamc@477 587 case 'h':
adamc@477 588 help(argv[0]);
adamc@477 589 return 0;
adamc@477 590
adamc@472 591 case 'p':
adamc@472 592 uw_port = atoi(optarg);
adamc@472 593 if (uw_port <= 0) {
adamc@472 594 fprintf(stderr, "Invalid port number\n");
adamc@477 595 help(argv[0]);
adamc@472 596 return 1;
adamc@472 597 }
adamc@472 598 break;
adamc@472 599
adamc@472 600 case 't':
adamc@472 601 nthreads = atoi(optarg);
adamc@472 602 if (nthreads <= 0) {
adamc@472 603 fprintf(stderr, "Invalid thread count\n");
adamc@477 604 help(argv[0]);
adamc@472 605 return 1;
adamc@472 606 }
adamc@472 607 break;
adamc@472 608
adamc@472 609 default:
adamc@472 610 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 611 return 1;
adamc@472 612 }
adamc@138 613 }
adamc@138 614
adamc@734 615 uw_global_init();
adamc@687 616 initialize();
adamc@687 617
adamc@138 618 names = calloc(nthreads, sizeof(int));
adamc@116 619
adamc@116 620 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 621
adamc@116 622 if (sockfd < 0) {
adamc@116 623 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 624 return 1;
adamc@116 625 }
adamc@116 626
adamc@116 627 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 628 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 629 return 1;
adamc@116 630 }
adamc@116 631
adamc@116 632 my_addr.sin_family = AF_INET; // host byte order
adamc@311 633 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 634 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 635 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 636
adamc@116 637 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 638 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 639 return 1;
adamc@116 640 }
adamc@116 641
adamc@311 642 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 643 fprintf(stderr, "Socket listen failed\n");
adamc@116 644 return 1;
adamc@116 645 }
adamc@116 646
adamc@116 647 sin_size = sizeof their_addr;
adamc@116 648
adamc@311 649 printf("Listening on port %d....\n", uw_port);
adamc@116 650
adamc@667 651 {
adamc@667 652 pthread_t thread;
adamc@667 653 int name;
adamc@667 654
adamc@667 655 if (pthread_create(&thread, NULL, client_pruner, &name)) {
adamc@667 656 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 657 return 1;
adamc@667 658 }
adamc@667 659 }
adamc@667 660
adamc@138 661 for (i = 0; i < nthreads; ++i) {
adamc@138 662 pthread_t thread;
adamc@138 663 names[i] = i;
adamc@138 664 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 665 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 666 return 1;
adamc@138 667 }
adamc@138 668 }
adamc@138 669
adamc@116 670 while (1) {
adamc@138 671 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 672
adamc@116 673 if (new_fd < 0) {
adamc@116 674 fprintf(stderr, "Socket accept failed\n");
adamc@116 675 return 1;
adamc@116 676 }
adamc@116 677
adamc@116 678 printf("Accepted connection.\n");
adamc@138 679
adamc@138 680 pthread_mutex_lock(&queue_mutex);
adamc@138 681 enqueue(new_fd);
adamc@139 682 pthread_cond_broadcast(&queue_cond);
adamc@138 683 pthread_mutex_unlock(&queue_mutex);
adamc@116 684 }
adamc@102 685 }