annotate src/c/driver.c @ 761:16b34dc2e29c

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