-/*
- * Entry point for worker threads
- */
-void worker_entry(void)
-{
- int ssock;
- int i = 0;
- int fail_this_transaction = 0;
- int ret;
- struct timeval tv;
- fd_set readset, tempset;
- ParsedHttpHdrs Hdr;
-
- memset(&Hdr, 0, sizeof(ParsedHttpHdrs));
- Hdr.HR.eReqType = eGET;
- http_new_modules(&Hdr);
- tv.tv_sec = 0;
- tv.tv_usec = 10000;
- FD_ZERO(&readset);
- FD_SET(msock, &readset);
-
- do {
- /* Only one thread can accept at a time */
- fail_this_transaction = 0;
- ssock = -1;
- errno = EAGAIN;
- do {
- ret = -1; /* just one at once should select... */
- begin_critical_section(S_SELECT);
-
- FD_ZERO(&tempset);
- if (msock > 0) FD_SET(msock, &tempset);
- tv.tv_sec = 0;
- tv.tv_usec = 10000;
- if (msock > 0) ret = select(msock+1, &tempset, NULL, NULL, &tv);
- end_critical_section(S_SELECT);
- if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
- {/* EINTR and EAGAIN are thrown but not of interest. */
- lprintf(2, "accept() failed:%d %s\n",
- errno, strerror(errno));
- }
- else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
- {/* Successfully selected, and still not shutting down? Accept! */
- ssock = accept(msock, NULL, 0);
- }
-
- } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
-
- if ((msock == -1)||(time_to_die))
- {/* ok, we're going down. */
- int shutdown = 0;
-
- /* the first to come here will have to do the cleanup.
- * make shure its realy just one.
- */
- begin_critical_section(S_SHUTDOWN);
- if (msock == -1)
- {
- msock = -2;
- shutdown = 1;
- }
- end_critical_section(S_SHUTDOWN);
- if (shutdown == 1)
- {/* we're the one to cleanup the mess. */
- http_destroy_modules(&Hdr);
- lprintf(2, "I'm master shutdown: tagging sessions to be killed.\n");
- shutdown_sessions();
- lprintf(2, "master shutdown: waiting for others\n");
- sleeeeeeeeeep(1); /* wait so some others might finish... */
- lprintf(2, "master shutdown: cleaning up sessions\n");
- do_housekeeping();
- lprintf(2, "master shutdown: cleaning up libical\n");
-
- ShutDownWebcit();
-
- lprintf(2, "master shutdown exiting!.\n");
- exit(0);
- }
- break;
- }
- if (ssock < 0 ) continue;
-
- if (msock < 0) {
- if (ssock > 0) close (ssock);
- lprintf(2, "inbetween.");
- pthread_exit(NULL);
- } else { /* Got it? do some real work! */
- /* Set the SO_REUSEADDR socket option */
- i = 1;
- setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
- &i, sizeof(i));
-
- /* If we are an HTTPS server, go crypto now. */
-#ifdef HAVE_OPENSSL
- if (is_https) {
- if (starttls(ssock) != 0) {
- fail_this_transaction = 1;
- close(ssock);
- }
- }
- else
- {
- int fdflags;
- fdflags = fcntl(ssock, F_GETFL);
- if (fdflags < 0)
- lprintf(1, "unable to get server socket flags! %s \n",
- strerror(errno));
- fdflags = fdflags | O_NONBLOCK;
- if (fcntl(ssock, F_SETFD, fdflags) < 0)
- lprintf(1, "unable to set server socket nonblocking flags! %s \n",
- strerror(errno));
- }
-#endif
-
- if (fail_this_transaction == 0) {
- Hdr.http_sock = ssock;
-
- /* Perform an HTTP transaction... */
- context_loop(&Hdr);
-
- /* Shut down SSL/TLS if required... */
-#ifdef HAVE_OPENSSL
- if (is_https) {
- endtls();
- }
-#endif
-
- /* ...and close the socket. */
- if (Hdr.http_sock > 0)
- lingering_close(ssock);
- http_detach_modules(&Hdr);
-
- }
-
- }
-
- } while (!time_to_die);
-
- http_destroy_modules(&Hdr);
- lprintf (1, "bye\n");
- pthread_exit(NULL);
-}
-
-/*
- * print log messages
- * logs to stderr if loglevel is lower than the verbosity set at startup
- *
- * loglevel level of the message
- * format the printf like format string
- * ... the strings to put into format
- */
-int lprintf(int loglevel, const char *format, ...)
-{
- va_list ap;
-
- if (loglevel <= verbosity) {
- va_start(ap, format);
- vfprintf(stderr, format, ap);
- va_end(ap);
- fflush(stderr);
- }
- return 1;
-}
-
-
-/*
- * print the actual stack frame.
- */
-void wc_backtrace(void)
-{
-#ifdef HAVE_BACKTRACE
- void *stack_frames[50];
- size_t size, i;
- char **strings;
-
-
- size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
- strings = backtrace_symbols(stack_frames, size);
- for (i = 0; i < size; i++) {
- if (strings != NULL)
- lprintf(1, "%s\n", strings[i]);
- else
- lprintf(1, "%p\n", stack_frames[i]);
- }
- free(strings);
-#endif
-}