aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/daemon.c')
-rw-r--r--src/daemon/daemon.c115
1 files changed, 56 insertions, 59 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 3b8b1111..346abedf 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -225,8 +225,8 @@ MHD_handle_connection (void *data)
225 if (con->socket_fd != -1) 225 if (con->socket_fd != -1)
226 { 226 {
227#if DEBUG_CLOSE 227#if DEBUG_CLOSE
228 MHD_DLOG (con->daemon, 228 MHD_DLOG (con->daemon,
229 "Processing thread terminating, closing connection\n"); 229 "Processing thread terminating, closing connection\n");
230#endif 230#endif
231 SHUTDOWN (con->socket_fd, SHUT_RDWR); 231 SHUTDOWN (con->socket_fd, SHUT_RDWR);
232 CLOSE (con->socket_fd); 232 CLOSE (con->socket_fd);
@@ -259,10 +259,11 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
259 if ((s < 0) || (addrlen <= 0)) 259 if ((s < 0) || (addrlen <= 0))
260 { 260 {
261 MHD_DLOG (daemon, "Error accepting connection: %s\n", STRERROR (errno)); 261 MHD_DLOG (daemon, "Error accepting connection: %s\n", STRERROR (errno));
262 if (s != -1) { 262 if (s != -1)
263 SHUTDOWN (s, SHUT_RDWR); 263 {
264 CLOSE (s); /* just in case */ 264 SHUTDOWN (s, SHUT_RDWR);
265 } 265 CLOSE (s); /* just in case */
266 }
266 return MHD_NO; 267 return MHD_NO;
267 } 268 }
268 if (daemon->max_connections == 0) 269 if (daemon->max_connections == 0)
@@ -278,8 +279,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
278 (MHD_NO == daemon->apc (daemon->apc_cls, addr, addrlen))) 279 (MHD_NO == daemon->apc (daemon->apc_cls, addr, addrlen)))
279 { 280 {
280#if DEBUG_CLOSE 281#if DEBUG_CLOSE
281 MHD_DLOG (daemon, 282 MHD_DLOG (daemon, "Connection rejected, closing connection\n");
282 "Connection rejected, closing connection\n");
283#endif 283#endif
284 SHUTDOWN (s, SHUT_RDWR); 284 SHUTDOWN (s, SHUT_RDWR);
285 CLOSE (s); 285 CLOSE (s);
@@ -357,17 +357,16 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
357 if ((pos->last_activity < timeout) && (pos->socket_fd != -1)) 357 if ((pos->last_activity < timeout) && (pos->socket_fd != -1))
358 { 358 {
359#if DEBUG_CLOSE 359#if DEBUG_CLOSE
360 MHD_DLOG (daemon, 360 MHD_DLOG (daemon, "Connection timed out, closing connection\n");
361 "Connection timed out, closing connection\n");
362#endif 361#endif
363 SHUTDOWN (pos->socket_fd, SHUT_RDWR); 362 SHUTDOWN (pos->socket_fd, SHUT_RDWR);
364 CLOSE (pos->socket_fd); 363 CLOSE (pos->socket_fd);
365 pos->socket_fd = -1; 364 pos->socket_fd = -1;
366 if (pos->daemon->notify_completed != NULL) 365 if (pos->daemon->notify_completed != NULL)
367 pos->daemon->notify_completed(pos->daemon->notify_completed_cls, 366 pos->daemon->notify_completed (pos->daemon->notify_completed_cls,
368 pos, 367 pos,
369 &pos->client_context, 368 &pos->client_context,
370 MHD_REQUEST_TERMINATED_TIMEOUT_REACHED); 369 MHD_REQUEST_TERMINATED_TIMEOUT_REACHED);
371 } 370 }
372 if (pos->socket_fd == -1) 371 if (pos->socket_fd == -1)
373 { 372 {
@@ -472,19 +471,19 @@ MHD_select (struct MHD_Daemon *daemon, int may_block)
472 FD_ZERO (&ws); 471 FD_ZERO (&ws);
473 FD_ZERO (&es); 472 FD_ZERO (&es);
474 max = 0; 473 max = 0;
475 474
476 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 475 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
477 { 476 {
478 /* single-threaded, go over everything */ 477 /* single-threaded, go over everything */
479 if (MHD_NO == MHD_get_fdset (daemon, &rs, &ws, &es, &max)) 478 if (MHD_NO == MHD_get_fdset (daemon, &rs, &ws, &es, &max))
480 return MHD_NO; 479 return MHD_NO;
481 } 480 }
482 else 481 else
483 { 482 {
484 /* accept only, have one thread per connection */ 483 /* accept only, have one thread per connection */
485 max = daemon->socket_fd; 484 max = daemon->socket_fd;
486 if (max == -1) 485 if (max == -1)
487 return MHD_NO; 486 return MHD_NO;
488 FD_SET (max, &rs); 487 FD_SET (max, &rs);
489 } 488 }
490 if (may_block == MHD_NO) 489 if (may_block == MHD_NO)
@@ -496,21 +495,20 @@ MHD_select (struct MHD_Daemon *daemon, int may_block)
496 { 495 {
497 /* ltimeout is in ms */ 496 /* ltimeout is in ms */
498 if (MHD_YES == MHD_get_timeout (daemon, &ltimeout)) 497 if (MHD_YES == MHD_get_timeout (daemon, &ltimeout))
499 { 498 {
500 timeout.tv_usec = (ltimeout % 1000) * 1000; 499 timeout.tv_usec = (ltimeout % 1000) * 1000;
501 timeout.tv_sec = ltimeout / 1000; 500 timeout.tv_sec = ltimeout / 1000;
502 may_block = MHD_NO; 501 may_block = MHD_NO;
503 } 502 }
504 } 503 }
505 num_ready = SELECT (max + 1, 504 num_ready = SELECT (max + 1,
506 &rs, &ws, &es, 505 &rs, &ws, &es, may_block == MHD_NO ? &timeout : NULL);
507 may_block == MHD_NO ? &timeout : NULL);
508 if (daemon->shutdown == MHD_YES) 506 if (daemon->shutdown == MHD_YES)
509 return MHD_NO; 507 return MHD_NO;
510 if (num_ready < 0) 508 if (num_ready < 0)
511 { 509 {
512 if (errno == EINTR) 510 if (errno == EINTR)
513 return MHD_YES; 511 return MHD_YES;
514 MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno)); 512 MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno));
515 return MHD_NO; 513 return MHD_NO;
516 } 514 }
@@ -525,23 +523,23 @@ MHD_select (struct MHD_Daemon *daemon, int may_block)
525 now = time (NULL); 523 now = time (NULL);
526 pos = daemon->connections; 524 pos = daemon->connections;
527 while (pos != NULL) 525 while (pos != NULL)
528 { 526 {
529 ds = pos->socket_fd; 527 ds = pos->socket_fd;
530 if (ds != -1) 528 if (ds != -1)
531 { 529 {
532 if (FD_ISSET (ds, &rs)) 530 if (FD_ISSET (ds, &rs))
533 { 531 {
534 pos->last_activity = now; 532 pos->last_activity = now;
535 MHD_connection_handle_read (pos); 533 MHD_connection_handle_read (pos);
536 } 534 }
537 if (FD_ISSET (ds, &ws)) 535 if (FD_ISSET (ds, &ws))
538 { 536 {
539 pos->last_activity = now; 537 pos->last_activity = now;
540 MHD_connection_handle_write (pos); 538 MHD_connection_handle_write (pos);
541 } 539 }
542 } 540 }
543 pos = pos->next; 541 pos = pos->next;
544 } 542 }
545 } 543 }
546 return MHD_YES; 544 return MHD_YES;
547} 545}
@@ -694,10 +692,11 @@ MHD_start_daemon (unsigned int options,
694 case MHD_OPTION_CONNECTION_TIMEOUT: 692 case MHD_OPTION_CONNECTION_TIMEOUT:
695 retVal->connection_timeout = va_arg (ap, unsigned int); 693 retVal->connection_timeout = va_arg (ap, unsigned int);
696 break; 694 break;
697 case MHD_OPTION_NOTIFY_COMPLETED: 695 case MHD_OPTION_NOTIFY_COMPLETED:
698 retVal->notify_completed = va_arg(ap, MHD_RequestCompletedCallback); 696 retVal->notify_completed =
699 retVal->notify_completed_cls = va_arg(ap, void *); 697 va_arg (ap, MHD_RequestCompletedCallback);
700 break; 698 retVal->notify_completed_cls = va_arg (ap, void *);
699 break;
701 default: 700 default:
702 fprintf (stderr, 701 fprintf (stderr,
703 "Invalid MHD_OPTION argument! (Did you terminate the list with MHD_OPTION_END?)\n"); 702 "Invalid MHD_OPTION argument! (Did you terminate the list with MHD_OPTION_END?)\n");
@@ -733,8 +732,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
733 fd = daemon->socket_fd; 732 fd = daemon->socket_fd;
734 daemon->socket_fd = -1; 733 daemon->socket_fd = -1;
735#if DEBUG_CLOSE 734#if DEBUG_CLOSE
736 MHD_DLOG (daemon, 735 MHD_DLOG (daemon, "MHD shutdown, closing listen socket\n");
737 "MHD shutdown, closing listen socket\n");
738#endif 736#endif
739 CLOSE (fd); 737 CLOSE (fd);
740 if ((0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || 738 if ((0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) ||
@@ -748,16 +746,15 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
748 if (-1 != daemon->connections->socket_fd) 746 if (-1 != daemon->connections->socket_fd)
749 { 747 {
750#if DEBUG_CLOSE 748#if DEBUG_CLOSE
751 MHD_DLOG (daemon, 749 MHD_DLOG (daemon, "MHD shutdown, closing active connections\n");
752 "MHD shutdown, closing active connections\n");
753#endif 750#endif
754 if (daemon->notify_completed != NULL) 751 if (daemon->notify_completed != NULL)
755 daemon->notify_completed(daemon->notify_completed_cls, 752 daemon->notify_completed (daemon->notify_completed_cls,
756 daemon->connections, 753 daemon->connections,
757 &daemon->connections->client_context, 754 &daemon->connections->client_context,
758 MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN); 755 MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN);
759 SHUTDOWN (daemon->connections->socket_fd, SHUT_RDWR); 756 SHUTDOWN (daemon->connections->socket_fd, SHUT_RDWR);
760 CLOSE (daemon->connections->socket_fd); 757 CLOSE (daemon->connections->socket_fd);
761 daemon->connections->socket_fd = -1; 758 daemon->connections->socket_fd = -1;
762 } 759 }
763 MHD_cleanup_connections (daemon); 760 MHD_cleanup_connections (daemon);