annotate src/c/driver.c @ 700:db6ab16cd8f3

Initialize contexts a little more vigorously
author Adam Chlipala <adamc@hcoop.net>
date Sun, 05 Apr 2009 12:05:19 -0400
parents 9b29ce0babb8
children a5d8b470d7ca
rev   line source
adamc@116 1 #include <stdio.h>
adamc@116 2
adamc@116 3 #include <string.h>
adamc@502 4 #include <stdlib.h>
adamc@116 5 #include <sys/types.h>
adamc@116 6 #include <sys/socket.h>
adamc@116 7 #include <netinet/in.h>
adamc@472 8 #include <unistd.h>
adamc@502 9 #include <signal.h>
adamc@116 10
adamc@138 11 #include <pthread.h>
adamc@138 12
adamc@244 13 #include "urweb.h"
adamc@117 14
adamc@311 15 int uw_backlog = 10;
adamc@311 16 int uw_bufsize = 1024;
adamc@116 17
adamc@138 18 typedef struct node {
adamc@138 19 int fd;
adamc@138 20 struct node *next;
adamc@138 21 } *node;
adamc@138 22
adamc@138 23 static node front = NULL, back = NULL;
adamc@138 24
adamc@138 25 static int empty() {
adamc@138 26 return front == NULL;
adamc@138 27 }
adamc@138 28
adamc@138 29 static void enqueue(int fd) {
adamc@138 30 node n = malloc(sizeof(struct node));
adamc@138 31
adamc@138 32 n->fd = fd;
adamc@138 33 n->next = NULL;
adamc@138 34 if (back)
adamc@138 35 back->next = n;
adamc@138 36 else
adamc@138 37 front = n;
adamc@138 38 back = n;
adamc@138 39 }
adamc@138 40
adamc@138 41 static int dequeue() {
adamc@138 42 int ret = front->fd;
adamc@138 43
adamc@138 44 front = front->next;
adamc@138 45 if (!front)
adamc@138 46 back = NULL;
adamc@138 47
adamc@138 48 return ret;
adamc@138 49 }
adamc@138 50
adamc@138 51 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@138 52 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
adamc@138 53
adamc@167 54 #define MAX_RETRIES 5
adamc@167 55
adamc@424 56 static int try_rollback(uw_context ctx) {
adamc@671 57 int r = uw_rollback(ctx);
adamc@424 58
adamc@424 59 if (r) {
adamc@424 60 printf("Error running SQL ROLLBACK\n");
adamc@424 61 uw_reset(ctx);
adamc@424 62 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@424 63 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@424 64 uw_write(ctx, "Error running SQL ROLLBACK\n");
adamc@424 65 }
adamc@424 66
adamc@424 67 return r;
adamc@424 68 }
adamc@424 69
adamc@698 70 static uw_context new_context() {
adamc@687 71 uw_context ctx = uw_init();
adamc@698 72 int retries_left = MAX_RETRIES;
adamc@698 73
adamc@272 74 while (1) {
adamc@311 75 failure_kind fk = uw_begin_init(ctx);
adamc@272 76
adamc@272 77 if (fk == SUCCESS) {
adamc@272 78 printf("Database connection initialized.\n");
adamc@272 79 break;
adamc@272 80 } else if (fk == BOUNDED_RETRY) {
adamc@272 81 if (retries_left) {
adamc@311 82 printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@272 83 --retries_left;
adamc@272 84 } else {
adamc@311 85 printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
adamc@311 86 uw_free(ctx);
adamc@272 87 return NULL;
adamc@272 88 }
adamc@272 89 } else if (fk == UNLIMITED_RETRY)
adamc@311 90 printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@272 91 else if (fk == FATAL) {
adamc@311 92 printf("Fatal initialization error: %s\n", uw_error_message(ctx));
adamc@311 93 uw_free(ctx);
adamc@272 94 return NULL;
adamc@272 95 } else {
adamc@698 96 printf("Unknown uw_begin_init return code!\n");
adamc@311 97 uw_free(ctx);
adamc@272 98 return NULL;
adamc@272 99 }
adamc@272 100 }
adamc@116 101
adamc@698 102 return ctx;
adamc@698 103 }
adamc@698 104
adamc@698 105 static void *worker(void *data) {
adamc@698 106 int me = *(int *)data, retries_left = MAX_RETRIES;
adamc@698 107 uw_context ctx = new_context();
adamc@698 108
adamc@116 109 while (1) {
adamc@311 110 char buf[uw_bufsize+1], *back = buf, *s;
adamc@667 111 int sock, dont_close = 0;
adamc@116 112
adamc@138 113 pthread_mutex_lock(&queue_mutex);
adamc@138 114 while (empty())
adamc@138 115 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 116 sock = dequeue();
adamc@138 117 pthread_mutex_unlock(&queue_mutex);
adamc@138 118
adamc@138 119 printf("Handling connection with thread #%d.\n", me);
adamc@138 120
adamc@138 121 while (1) {
adamc@167 122 unsigned retries_left = MAX_RETRIES;
adamc@311 123 int r = recv(sock, back, uw_bufsize - (back - buf), 0);
adamc@138 124
adamc@138 125 if (r < 0) {
adamc@138 126 fprintf(stderr, "Recv failed\n");
adamc@138 127 break;
adamc@138 128 }
adamc@138 129
adamc@138 130 if (r == 0) {
adamc@138 131 printf("Connection closed.\n");
adamc@138 132 break;
adamc@138 133 }
adamc@138 134
adamc@138 135 printf("Received %d bytes.\n", r);
adamc@138 136
adamc@138 137 back += r;
adamc@138 138 *back = 0;
adamc@138 139
adamc@138 140 if (s = strstr(buf, "\r\n\r\n")) {
adamc@424 141 failure_kind fk;
adamc@457 142 char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs;
adamc@138 143
adamc@475 144 s[2] = 0;
adamc@144 145
adamc@138 146 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 147 fprintf(stderr, "No newline in buf\n");
adamc@138 148 break;
adamc@138 149 }
adamc@138 150
adamc@138 151 *s = 0;
adamc@457 152 headers = s + 2;
adamc@138 153 cmd = s = buf;
adamc@401 154
adamc@401 155 printf("Read: %s\n", buf);
adamc@138 156
adamc@138 157 if (!strsep(&s, " ")) {
adamc@138 158 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 159 break;
adamc@138 160 }
adamc@138 161
adamc@138 162 if (strcmp(cmd, "GET")) {
adamc@138 163 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
adamc@138 164 break;
adamc@138 165 }
adamc@138 166
adamc@138 167 path = s;
adamc@138 168 if (!strsep(&s, " ")) {
adamc@138 169 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 170 break;
adamc@138 171 }
adamc@138 172
adamc@668 173 uw_set_headers(ctx, headers);
adamc@668 174
adamc@668 175 if (!strcmp(path, "/.msgs")) {
adamc@668 176 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
adamc@668 177 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
adamc@668 178
adamc@668 179 if (id && pass) {
adamc@682 180 unsigned idn = atoi(id);
adamc@668 181 uw_client_connect(idn, atoi(pass), sock);
adamc@668 182 dont_close = 1;
adamc@682 183 fprintf(stderr, "Processed request for messages by client %u\n\n", idn);
adamc@668 184 }
adamc@667 185 break;
adamc@667 186 }
adamc@667 187
adamc@144 188 if (inputs = strchr(path, '?')) {
adamc@144 189 char *name, *value;
adamc@144 190 *inputs++ = 0;
adamc@144 191
adamc@144 192 while (*inputs) {
adamc@144 193 name = inputs;
adamc@145 194 if (inputs = strchr(inputs, '&'))
adamc@145 195 *inputs++ = 0;
adamc@145 196 else
adamc@145 197 inputs = strchr(name, 0);
adamc@145 198
adamc@145 199 if (value = strchr(name, '=')) {
adamc@144 200 *value++ = 0;
adamc@311 201 uw_set_input(ctx, name, value);
adamc@144 202 }
adamc@145 203 else
adamc@311 204 uw_set_input(ctx, name, "");
adamc@144 205 }
adamc@144 206 }
adamc@144 207
adamc@138 208 printf("Serving URI %s....\n", path);
adamc@138 209
adamc@167 210 while (1) {
adamc@462 211 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@167 212
adamc@400 213 strcpy(path_copy, path);
adamc@458 214 fk = uw_begin(ctx, path_copy);
adamc@167 215 if (fk == SUCCESS) {
adamc@671 216 uw_commit(ctx);
adamc@167 217 break;
adamc@167 218 } else if (fk == BOUNDED_RETRY) {
adamc@167 219 if (retries_left) {
adamc@311 220 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@167 221 --retries_left;
adamc@167 222 }
adamc@167 223 else {
adamc@311 224 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@167 225
adamc@682 226 try_rollback(ctx);
adamc@682 227
adamc@311 228 uw_reset_keep_error_message(ctx);
adamc@464 229 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 230 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 231 uw_write(ctx, "Fatal error (out of retries): ");
adamc@311 232 uw_write(ctx, uw_error_message(ctx));
adamc@311 233 uw_write(ctx, "\n");
adamc@424 234
adamc@424 235 break;
adamc@167 236 }
adamc@167 237 } else if (fk == UNLIMITED_RETRY)
adamc@311 238 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@167 239 else if (fk == FATAL) {
adamc@311 240 printf("Fatal error: %s\n", uw_error_message(ctx));
adamc@167 241
adamc@682 242 try_rollback(ctx);
adamc@682 243
adamc@311 244 uw_reset_keep_error_message(ctx);
adamc@464 245 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@548 246 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@548 247 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@311 248 uw_write(ctx, "Fatal error: ");
adamc@311 249 uw_write(ctx, uw_error_message(ctx));
adamc@548 250 uw_write(ctx, "\n</body></html>");
adamc@167 251
adamc@167 252 break;
adamc@167 253 } else {
adamc@311 254 printf("Unknown uw_handle return code!\n");
adamc@167 255
adamc@682 256 try_rollback(ctx);
adamc@682 257
adamc@311 258 uw_reset_keep_request(ctx);
adamc@464 259 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 260 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 261 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@167 262
adamc@167 263 break;
adamc@167 264 }
adamc@167 265
adamc@424 266 if (try_rollback(ctx))
adamc@424 267 break;
adamc@682 268
adamc@682 269 uw_reset_keep_request(ctx);
adamc@167 270 }
adamc@138 271
adamc@311 272 uw_send(ctx, sock);
adamc@138 273
adamc@138 274 printf("Done with client.\n\n");
adamc@324 275 uw_memstats(ctx);
adamc@138 276 break;
adamc@138 277 }
adamc@116 278 }
adamc@116 279
adamc@667 280 if (!dont_close)
adamc@667 281 close(sock);
adamc@311 282 uw_reset(ctx);
adamc@116 283 }
adamc@116 284 }
adamc@102 285
adamc@667 286 static void *client_pruner(void *data) {
adamc@698 287 uw_context ctx = new_context();
adamc@698 288
adamc@698 289 if (!ctx)
adamc@698 290 exit(1);
adamc@683 291
adamc@667 292 while (1) {
adamc@683 293 uw_prune_clients(ctx);
adamc@667 294 sleep(5);
adamc@667 295 }
adamc@667 296 }
adamc@667 297
adamc@477 298 static void help(char *cmd) {
adamc@477 299 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 300 }
adamc@477 301
adamc@502 302 static void sigint(int signum) {
adamc@502 303 printf("Exiting....\n");
adamc@502 304 exit(0);
adamc@502 305 }
adamc@502 306
adamc@687 307 static void initialize() {
adamc@698 308 uw_context ctx = new_context();
adamc@700 309 failure_kind fk;
adamc@687 310
adamc@698 311 if (!ctx)
adamc@698 312 exit(1);
adamc@698 313
adamc@700 314 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
adamc@700 315 printf("Unlimited retry during init: %s\n", uw_error_message(ctx));
adamc@700 316 uw_db_rollback(ctx);
adamc@700 317 uw_reset(ctx);
adamc@700 318 }
adamc@700 319
adamc@700 320 if (fk != SUCCESS) {
adamc@700 321 printf("Failed to initialize database! %s\n", uw_error_message(ctx));
adamc@687 322 uw_db_rollback(ctx);
adamc@687 323 exit(1);
adamc@687 324 }
adamc@687 325
adamc@687 326 uw_free(ctx);
adamc@687 327 }
adamc@687 328
adamc@138 329 int main(int argc, char *argv[]) {
adamc@116 330 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 331 int sockfd; // listen on sock_fd
adamc@116 332 struct sockaddr_in my_addr;
adamc@116 333 struct sockaddr_in their_addr; // connector's address information
adamc@116 334 int sin_size, yes = 1;
adamc@472 335 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 336
adamc@502 337 signal(SIGINT, sigint);
adamc@505 338 signal(SIGPIPE, SIG_IGN);
adamc@505 339
adamc@477 340 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 341 switch (opt) {
adamc@472 342 case '?':
adamc@472 343 fprintf(stderr, "Unknown command-line option");
adamc@477 344 help(argv[0]);
adamc@472 345 return 1;
adamc@138 346
adamc@477 347 case 'h':
adamc@477 348 help(argv[0]);
adamc@477 349 return 0;
adamc@477 350
adamc@472 351 case 'p':
adamc@472 352 uw_port = atoi(optarg);
adamc@472 353 if (uw_port <= 0) {
adamc@472 354 fprintf(stderr, "Invalid port number\n");
adamc@477 355 help(argv[0]);
adamc@472 356 return 1;
adamc@472 357 }
adamc@472 358 break;
adamc@472 359
adamc@472 360 case 't':
adamc@472 361 nthreads = atoi(optarg);
adamc@472 362 if (nthreads <= 0) {
adamc@472 363 fprintf(stderr, "Invalid thread count\n");
adamc@477 364 help(argv[0]);
adamc@472 365 return 1;
adamc@472 366 }
adamc@472 367 break;
adamc@472 368
adamc@472 369 default:
adamc@472 370 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 371 return 1;
adamc@472 372 }
adamc@138 373 }
adamc@138 374
adamc@687 375 initialize();
adamc@687 376
adamc@138 377 names = calloc(nthreads, sizeof(int));
adamc@116 378
adamc@116 379 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 380
adamc@116 381 if (sockfd < 0) {
adamc@116 382 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 383 return 1;
adamc@116 384 }
adamc@116 385
adamc@116 386 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 387 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 388 return 1;
adamc@116 389 }
adamc@116 390
adamc@116 391 my_addr.sin_family = AF_INET; // host byte order
adamc@311 392 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 393 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 394 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 395
adamc@116 396 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 397 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 398 return 1;
adamc@116 399 }
adamc@116 400
adamc@311 401 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 402 fprintf(stderr, "Socket listen failed\n");
adamc@116 403 return 1;
adamc@116 404 }
adamc@116 405
adamc@116 406 sin_size = sizeof their_addr;
adamc@116 407
adamc@667 408 uw_global_init();
adamc@667 409
adamc@311 410 printf("Listening on port %d....\n", uw_port);
adamc@116 411
adamc@667 412 {
adamc@667 413 pthread_t thread;
adamc@667 414 int name;
adamc@667 415
adamc@667 416 if (pthread_create(&thread, NULL, client_pruner, &name)) {
adamc@667 417 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 418 return 1;
adamc@667 419 }
adamc@667 420 }
adamc@667 421
adamc@138 422 for (i = 0; i < nthreads; ++i) {
adamc@138 423 pthread_t thread;
adamc@138 424 names[i] = i;
adamc@138 425 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 426 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 427 return 1;
adamc@138 428 }
adamc@138 429 }
adamc@138 430
adamc@116 431 while (1) {
adamc@138 432 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 433
adamc@116 434 if (new_fd < 0) {
adamc@116 435 fprintf(stderr, "Socket accept failed\n");
adamc@116 436 return 1;
adamc@116 437 }
adamc@116 438
adamc@116 439 printf("Accepted connection.\n");
adamc@138 440
adamc@138 441 pthread_mutex_lock(&queue_mutex);
adamc@138 442 enqueue(new_fd);
adamc@139 443 pthread_cond_broadcast(&queue_cond);
adamc@138 444 pthread_mutex_unlock(&queue_mutex);
adamc@116 445 }
adamc@102 446 }