diff options
author | Christian Grothoff <christian@grothoff.org> | 2007-09-09 21:32:12 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2007-09-09 21:32:12 +0000 |
commit | fb36fb2009a35e48be28df422af5a4b42d099cda (patch) | |
tree | 103c9372286a794293b36bcc0e18e091bde49f3d /src/daemon/daemon.c | |
parent | 8ce09efc5e3c19d413727e8b80c5f2eff9406b38 (diff) | |
download | libmicrohttpd-fb36fb2009a35e48be28df422af5a4b42d099cda.tar.gz libmicrohttpd-fb36fb2009a35e48be28df422af5a4b42d099cda.zip |
release
Diffstat (limited to 'src/daemon/daemon.c')
-rw-r--r-- | src/daemon/daemon.c | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 02cdfe21..d35be3a6 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c | |||
@@ -72,7 +72,9 @@ MHD_register_handler (struct MHD_Daemon *daemon, | |||
72 | ah = malloc (sizeof (struct MHD_Access_Handler)); | 72 | ah = malloc (sizeof (struct MHD_Access_Handler)); |
73 | if (ah == NULL) | 73 | if (ah == NULL) |
74 | { | 74 | { |
75 | #if HAVE_MESSAGES | ||
75 | MHD_DLOG (daemon, "Error allocating memory: %s\n", STRERROR (errno)); | 76 | MHD_DLOG (daemon, "Error allocating memory: %s\n", STRERROR (errno)); |
77 | #endif | ||
76 | return MHD_NO; | 78 | return MHD_NO; |
77 | } | 79 | } |
78 | 80 | ||
@@ -204,8 +206,10 @@ MHD_handle_connection (void *data) | |||
204 | { | 206 | { |
205 | if (errno == EINTR) | 207 | if (errno == EINTR) |
206 | continue; | 208 | continue; |
209 | #if HAVE_MESSAGES | ||
207 | MHD_DLOG (con->daemon, "Error during select (%d): `%s'\n", | 210 | MHD_DLOG (con->daemon, "Error during select (%d): `%s'\n", |
208 | max, STRERROR (errno)); | 211 | max, STRERROR (errno)); |
212 | #endif | ||
209 | break; | 213 | break; |
210 | } | 214 | } |
211 | if (((FD_ISSET (con->socket_fd, &rs)) && | 215 | if (((FD_ISSET (con->socket_fd, &rs)) && |
@@ -224,9 +228,11 @@ MHD_handle_connection (void *data) | |||
224 | if (con->socket_fd != -1) | 228 | if (con->socket_fd != -1) |
225 | { | 229 | { |
226 | #if DEBUG_CLOSE | 230 | #if DEBUG_CLOSE |
231 | #if HAVE_MESSAGES | ||
227 | MHD_DLOG (con->daemon, | 232 | MHD_DLOG (con->daemon, |
228 | "Processing thread terminating, closing connection\n"); | 233 | "Processing thread terminating, closing connection\n"); |
229 | #endif | 234 | #endif |
235 | #endif | ||
230 | SHUTDOWN (con->socket_fd, SHUT_RDWR); | 236 | SHUTDOWN (con->socket_fd, SHUT_RDWR); |
231 | CLOSE (con->socket_fd); | 237 | CLOSE (con->socket_fd); |
232 | con->socket_fd = -1; | 238 | con->socket_fd = -1; |
@@ -257,7 +263,9 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
257 | s = ACCEPT (daemon->socket_fd, addr, &addrlen); | 263 | s = ACCEPT (daemon->socket_fd, addr, &addrlen); |
258 | if ((s < 0) || (addrlen <= 0)) | 264 | if ((s < 0) || (addrlen <= 0)) |
259 | { | 265 | { |
266 | #if HAVE_MESSAGES | ||
260 | MHD_DLOG (daemon, "Error accepting connection: %s\n", STRERROR (errno)); | 267 | MHD_DLOG (daemon, "Error accepting connection: %s\n", STRERROR (errno)); |
268 | #endif | ||
261 | if (s != -1) | 269 | if (s != -1) |
262 | { | 270 | { |
263 | SHUTDOWN (s, SHUT_RDWR); | 271 | SHUTDOWN (s, SHUT_RDWR); |
@@ -268,8 +276,10 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
268 | if (daemon->max_connections == 0) | 276 | if (daemon->max_connections == 0) |
269 | { | 277 | { |
270 | /* above connection limit - reject */ | 278 | /* above connection limit - reject */ |
279 | #if HAVE_MESSAGES | ||
271 | MHD_DLOG (daemon, | 280 | MHD_DLOG (daemon, |
272 | "Server reached connection limit (closing inbound connection)\n"); | 281 | "Server reached connection limit (closing inbound connection)\n"); |
282 | #endif | ||
273 | SHUTDOWN (s, SHUT_RDWR); | 283 | SHUTDOWN (s, SHUT_RDWR); |
274 | CLOSE (s); | 284 | CLOSE (s); |
275 | return MHD_NO; | 285 | return MHD_NO; |
@@ -278,8 +288,10 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
278 | (MHD_NO == daemon->apc (daemon->apc_cls, addr, addrlen))) | 288 | (MHD_NO == daemon->apc (daemon->apc_cls, addr, addrlen))) |
279 | { | 289 | { |
280 | #if DEBUG_CLOSE | 290 | #if DEBUG_CLOSE |
291 | #if HAVE_MESSAGES | ||
281 | MHD_DLOG (daemon, "Connection rejected, closing connection\n"); | 292 | MHD_DLOG (daemon, "Connection rejected, closing connection\n"); |
282 | #endif | 293 | #endif |
294 | #endif | ||
283 | SHUTDOWN (s, SHUT_RDWR); | 295 | SHUTDOWN (s, SHUT_RDWR); |
284 | CLOSE (s); | 296 | CLOSE (s); |
285 | return MHD_YES; | 297 | return MHD_YES; |
@@ -287,7 +299,9 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
287 | connection = malloc (sizeof (struct MHD_Connection)); | 299 | connection = malloc (sizeof (struct MHD_Connection)); |
288 | if (connection == NULL) | 300 | if (connection == NULL) |
289 | { | 301 | { |
302 | #if HAVE_MESSAGES | ||
290 | MHD_DLOG (daemon, "Error allocating memory: %s\n", STRERROR (errno)); | 303 | MHD_DLOG (daemon, "Error allocating memory: %s\n", STRERROR (errno)); |
304 | #endif | ||
291 | SHUTDOWN (s, SHUT_RDWR); | 305 | SHUTDOWN (s, SHUT_RDWR); |
292 | CLOSE (s); | 306 | CLOSE (s); |
293 | return MHD_NO; | 307 | return MHD_NO; |
@@ -297,7 +311,9 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
297 | connection->addr = malloc (addrlen); | 311 | connection->addr = malloc (addrlen); |
298 | if (connection->addr == NULL) | 312 | if (connection->addr == NULL) |
299 | { | 313 | { |
314 | #if HAVE_MESSAGES | ||
300 | MHD_DLOG (daemon, "Error allocating memory: %s\n", STRERROR (errno)); | 315 | MHD_DLOG (daemon, "Error allocating memory: %s\n", STRERROR (errno)); |
316 | #endif | ||
301 | SHUTDOWN (s, SHUT_RDWR); | 317 | SHUTDOWN (s, SHUT_RDWR); |
302 | CLOSE (s); | 318 | CLOSE (s); |
303 | free (connection); | 319 | free (connection); |
@@ -311,7 +327,9 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
311 | (0 != pthread_create (&connection->pid, | 327 | (0 != pthread_create (&connection->pid, |
312 | NULL, &MHD_handle_connection, connection))) | 328 | NULL, &MHD_handle_connection, connection))) |
313 | { | 329 | { |
330 | #if HAVE_MESSAGES | ||
314 | MHD_DLOG (daemon, "Failed to create a thread: %s\n", STRERROR (errno)); | 331 | MHD_DLOG (daemon, "Failed to create a thread: %s\n", STRERROR (errno)); |
332 | #endif | ||
315 | SHUTDOWN (s, SHUT_RDWR); | 333 | SHUTDOWN (s, SHUT_RDWR); |
316 | CLOSE (s); | 334 | CLOSE (s); |
317 | free (connection->addr); | 335 | free (connection->addr); |
@@ -356,8 +374,10 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon) | |||
356 | if ((pos->last_activity < timeout) && (pos->socket_fd != -1)) | 374 | if ((pos->last_activity < timeout) && (pos->socket_fd != -1)) |
357 | { | 375 | { |
358 | #if DEBUG_CLOSE | 376 | #if DEBUG_CLOSE |
377 | #if HAVE_MESSAGES | ||
359 | MHD_DLOG (daemon, "Connection timed out, closing connection\n"); | 378 | MHD_DLOG (daemon, "Connection timed out, closing connection\n"); |
360 | #endif | 379 | #endif |
380 | #endif | ||
361 | SHUTDOWN (pos->socket_fd, SHUT_RDWR); | 381 | SHUTDOWN (pos->socket_fd, SHUT_RDWR); |
362 | CLOSE (pos->socket_fd); | 382 | CLOSE (pos->socket_fd); |
363 | pos->socket_fd = -1; | 383 | pos->socket_fd = -1; |
@@ -508,7 +528,9 @@ MHD_select (struct MHD_Daemon *daemon, int may_block) | |||
508 | { | 528 | { |
509 | if (errno == EINTR) | 529 | if (errno == EINTR) |
510 | return MHD_YES; | 530 | return MHD_YES; |
531 | #if HAVE_MESSAGES | ||
511 | MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno)); | 532 | MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno)); |
533 | #endif | ||
512 | return MHD_NO; | 534 | return MHD_NO; |
513 | } | 535 | } |
514 | ds = daemon->socket_fd; | 536 | ds = daemon->socket_fd; |
@@ -622,15 +644,20 @@ MHD_start_daemon (unsigned int options, | |||
622 | socket_fd = SOCKET (PF_INET, SOCK_STREAM, 0); | 644 | socket_fd = SOCKET (PF_INET, SOCK_STREAM, 0); |
623 | if (socket_fd < 0) | 645 | if (socket_fd < 0) |
624 | { | 646 | { |
647 | #if HAVE_MESSAGES | ||
625 | if ((options & MHD_USE_DEBUG) != 0) | 648 | if ((options & MHD_USE_DEBUG) != 0) |
626 | fprintf (stderr, "Call to socket failed: %s\n", STRERROR (errno)); | 649 | fprintf (stderr, "Call to socket failed: %s\n", STRERROR (errno)); |
650 | #endif | ||
627 | return NULL; | 651 | return NULL; |
628 | } | 652 | } |
629 | if ((SETSOCKOPT (socket_fd, | 653 | if ((SETSOCKOPT (socket_fd, |
630 | SOL_SOCKET, | 654 | SOL_SOCKET, |
631 | SO_REUSEADDR, | 655 | SO_REUSEADDR, |
632 | &on, sizeof (on)) < 0) && (options & MHD_USE_DEBUG) != 0) | 656 | &on, sizeof (on)) < 0) && (options & MHD_USE_DEBUG) != 0) { |
657 | #if HAVE_MESSAGES | ||
633 | fprintf (stderr, "setsockopt failed: %s\n", STRERROR (errno)); | 658 | fprintf (stderr, "setsockopt failed: %s\n", STRERROR (errno)); |
659 | #endif | ||
660 | } | ||
634 | if ((options & MHD_USE_IPv6) != 0) | 661 | if ((options & MHD_USE_IPv6) != 0) |
635 | { | 662 | { |
636 | memset (&servaddr6, 0, sizeof (struct sockaddr_in6)); | 663 | memset (&servaddr6, 0, sizeof (struct sockaddr_in6)); |
@@ -649,17 +676,21 @@ MHD_start_daemon (unsigned int options, | |||
649 | } | 676 | } |
650 | if (BIND (socket_fd, servaddr, addrlen) < 0) | 677 | if (BIND (socket_fd, servaddr, addrlen) < 0) |
651 | { | 678 | { |
679 | #if HAVE_MESSAGES | ||
652 | if ((options & MHD_USE_DEBUG) != 0) | 680 | if ((options & MHD_USE_DEBUG) != 0) |
653 | fprintf (stderr, | 681 | fprintf (stderr, |
654 | "Failed to bind to port %u: %s\n", port, STRERROR (errno)); | 682 | "Failed to bind to port %u: %s\n", port, STRERROR (errno)); |
683 | #endif | ||
655 | CLOSE (socket_fd); | 684 | CLOSE (socket_fd); |
656 | return NULL; | 685 | return NULL; |
657 | } | 686 | } |
658 | if (LISTEN (socket_fd, 20) < 0) | 687 | if (LISTEN (socket_fd, 20) < 0) |
659 | { | 688 | { |
689 | #if HAVE_MESSAGES | ||
660 | if ((options & MHD_USE_DEBUG) != 0) | 690 | if ((options & MHD_USE_DEBUG) != 0) |
661 | fprintf (stderr, | 691 | fprintf (stderr, |
662 | "Failed to listen for connections: %s\n", STRERROR (errno)); | 692 | "Failed to listen for connections: %s\n", STRERROR (errno)); |
693 | #endif | ||
663 | CLOSE (socket_fd); | 694 | CLOSE (socket_fd); |
664 | return NULL; | 695 | return NULL; |
665 | } | 696 | } |
@@ -697,8 +728,10 @@ MHD_start_daemon (unsigned int options, | |||
697 | retVal->notify_completed_cls = va_arg (ap, void *); | 728 | retVal->notify_completed_cls = va_arg (ap, void *); |
698 | break; | 729 | break; |
699 | default: | 730 | default: |
731 | #if HAVE_MESSAGES | ||
700 | fprintf (stderr, | 732 | fprintf (stderr, |
701 | "Invalid MHD_OPTION argument! (Did you terminate the list with MHD_OPTION_END?)\n"); | 733 | "Invalid MHD_OPTION argument! (Did you terminate the list with MHD_OPTION_END?)\n"); |
734 | #endif | ||
702 | abort (); | 735 | abort (); |
703 | } | 736 | } |
704 | } | 737 | } |
@@ -707,8 +740,10 @@ MHD_start_daemon (unsigned int options, | |||
707 | (0 != (options & MHD_USE_SELECT_INTERNALLY))) && | 740 | (0 != (options & MHD_USE_SELECT_INTERNALLY))) && |
708 | (0 != pthread_create (&retVal->pid, NULL, &MHD_select_thread, retVal))) | 741 | (0 != pthread_create (&retVal->pid, NULL, &MHD_select_thread, retVal))) |
709 | { | 742 | { |
743 | #if HAVE_MESSAGES | ||
710 | MHD_DLOG (retVal, | 744 | MHD_DLOG (retVal, |
711 | "Failed to create listen thread: %s\n", STRERROR (errno)); | 745 | "Failed to create listen thread: %s\n", STRERROR (errno)); |
746 | #endif | ||
712 | free (retVal); | 747 | free (retVal); |
713 | CLOSE (socket_fd); | 748 | CLOSE (socket_fd); |
714 | return NULL; | 749 | return NULL; |
@@ -731,8 +766,10 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) | |||
731 | fd = daemon->socket_fd; | 766 | fd = daemon->socket_fd; |
732 | daemon->socket_fd = -1; | 767 | daemon->socket_fd = -1; |
733 | #if DEBUG_CLOSE | 768 | #if DEBUG_CLOSE |
769 | #if HAVE_MESSAGES | ||
734 | MHD_DLOG (daemon, "MHD shutdown, closing listen socket\n"); | 770 | MHD_DLOG (daemon, "MHD shutdown, closing listen socket\n"); |
735 | #endif | 771 | #endif |
772 | #endif | ||
736 | CLOSE (fd); | 773 | CLOSE (fd); |
737 | if ((0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || | 774 | if ((0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || |
738 | (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY))) | 775 | (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY))) |
@@ -745,8 +782,10 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) | |||
745 | if (-1 != daemon->connections->socket_fd) | 782 | if (-1 != daemon->connections->socket_fd) |
746 | { | 783 | { |
747 | #if DEBUG_CLOSE | 784 | #if DEBUG_CLOSE |
785 | #if HAVE_MESSAGES | ||
748 | MHD_DLOG (daemon, "MHD shutdown, closing active connections\n"); | 786 | MHD_DLOG (daemon, "MHD shutdown, closing active connections\n"); |
749 | #endif | 787 | #endif |
788 | #endif | ||
750 | if (daemon->notify_completed != NULL) | 789 | if (daemon->notify_completed != NULL) |
751 | daemon->notify_completed (daemon->notify_completed_cls, | 790 | daemon->notify_completed (daemon->notify_completed_cls, |
752 | daemon->connections, | 791 | daemon->connections, |