4 * This contains a simple multithreaded TCP server manager. It sits around
5 * waiting on the specified port for incoming HTTP connections. When a
6 * connection is established, it calls context_loop() from context_loop.c.
11 #include "webserver.h"
18 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
21 int verbosity = 9; /* Logging level */
22 int msock; /* master listening socket */
23 int is_https = 0; /* Nonzero if I am an HTTPS service */
24 int follow_xff = 0; /* Follow X-Forwarded-For: header */
25 int home_specified = 0; /* did the user specify a homedir? */
26 int time_to_die = 0; /* Nonzero if server is shutting down */
27 extern void *context_loop(int);
28 extern void *housekeeping_loop(void);
29 extern pthread_mutex_t SessionListMutex;
30 extern pthread_key_t MyConKey;
33 char ctdl_key_dir[PATH_MAX]=SSL_DIR;
34 char file_crpt_file_key[PATH_MAX]="";
35 char file_crpt_file_csr[PATH_MAX]="";
36 char file_crpt_file_cer[PATH_MAX]="";
38 char socket_dir[PATH_MAX]; /* where to talk to our citadel server */
39 static const char editor_absolut_dir[PATH_MAX]=EDITORDIR; /* nailed to what configure gives us. */
40 static char static_dir[PATH_MAX]; /* calculated on startup */
41 static char static_local_dir[PATH_MAX]; /* calculated on startup */
42 static char static_icon_dir[PATH_MAX]; /* where should we find our mime icons? */
43 char *static_dirs[]={ /* needs same sort order as the web mapping */
44 (char*)static_dir, /* our templates on disk */
45 (char*)static_local_dir, /* user provided templates disk */
46 (char*)editor_absolut_dir, /* the editor on disk */
47 (char*)static_icon_dir /* our icons... */
51 * Subdirectories from which the client may request static content
53 * (If you add more, remember to increment 'ndirs' below)
55 char *static_content_dirs[] = {
56 "static", /* static templates */
57 "static.local", /* site local static templates */
58 "tiny_mce" /* rich text editor */
64 char *server_cookie = NULL; /* our Cookie connection to the client */
65 int http_port = PORT_NUM; /* Port to listen on */
66 char *ctdlhost = DEFAULT_HOST; /* our name */
67 char *ctdlport = DEFAULT_PORT; /* our Port */
68 int setup_wizard = 0; /* should we run the setup wizard? \todo */
69 char wizard_filename[PATH_MAX]; /* where's the setup wizard? */
70 int running_as_daemon = 0; /* should we deamonize on startup? */
74 * This is a generic function to set up a master socket for listening on
75 * a TCP port. The server shuts down if the bind fails.
77 * ip_addr IP address to bind
78 * port_number port number to bind
79 * queue_len number of incoming connections to allow in the queue
81 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
83 struct sockaddr_in sin;
86 memset(&sin, 0, sizeof(sin));
87 sin.sin_family = AF_INET;
88 if (ip_addr == NULL) {
89 sin.sin_addr.s_addr = INADDR_ANY;
91 sin.sin_addr.s_addr = inet_addr(ip_addr);
94 if (sin.sin_addr.s_addr == INADDR_NONE) {
95 sin.sin_addr.s_addr = INADDR_ANY;
98 if (port_number == 0) {
99 lprintf(1, "Cannot start: no port number specified.\n");
102 sin.sin_port = htons((u_short) port_number);
104 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
106 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
109 /* Set some socket options that make sense. */
111 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
113 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
114 there should be a preceding F_GETFL
115 and a bitwise OR with the previous
118 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
119 lprintf(1, "Can't bind: %s\n", strerror(errno));
122 if (listen(s, queue_len) < 0) {
123 lprintf(1, "Can't listen: %s\n", strerror(errno));
132 * Create a Unix domain socket and listen on it
133 * sockpath - file name of the unix domain socket
134 * queue_len - Number of incoming connections to allow in the queue
136 int ig_uds_server(char *sockpath, int queue_len)
138 struct sockaddr_un addr;
141 int actual_queue_len;
143 actual_queue_len = queue_len;
144 if (actual_queue_len < 5) actual_queue_len = 5;
146 i = unlink(sockpath);
147 if (i != 0) if (errno != ENOENT) {
148 lprintf(1, "webcit: can't unlink %s: %s\n",
149 sockpath, strerror(errno));
153 memset(&addr, 0, sizeof(addr));
154 addr.sun_family = AF_UNIX;
155 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
157 s = socket(AF_UNIX, SOCK_STREAM, 0);
159 lprintf(1, "webcit: Can't create a socket: %s\n",
164 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
165 lprintf(1, "webcit: Can't bind: %s\n",
170 if (listen(s, actual_queue_len) < 0) {
171 lprintf(1, "webcit: Can't listen: %s\n",
176 chmod(sockpath, 0777);
184 * \brief Read data from the client socket.
185 * \param sock socket fd to read from
186 * \param buf buffer to read into
187 * \param bytes number of bytes to read
188 * \param timeout Number of seconds to wait before timing out
189 * \return values are\
190 * 1 Requested number of bytes has been read.\
191 * 0 Request timed out.\
192 * -1 Connection is broken, or other error.
194 int client_read_to(int sock, char *buf, int bytes, int timeout)
204 return (client_read_ssl(buf, bytes, timeout));
209 while (len < bytes) {
215 retval = select((sock) + 1, &rfds, NULL, NULL, &tv);
216 if (FD_ISSET(sock, &rfds) == 0) {
220 rlen = read(sock, &buf[len], bytes - len);
223 lprintf(2, "client_read() failed: %s\n",
231 write(2, "\033[32m", 5);
232 write(2, buf, bytes);
233 write(2, "\033[30m", 5);
239 * \brief write data to the client
240 * \param buf data to write to the client
241 * \param count size of buffer
243 ssize_t client_write(const void *buf, size_t count)
247 size_t bytesWritten = 0;
252 if (WC->burst != NULL) {
253 if ((WC->burst_len + count) >= WC->burst_alloc) {
254 newalloc = (WC->burst_alloc * 2);
255 if ((WC->burst_len + count) >= newalloc) {
258 newptr = realloc(WC->burst, newalloc);
259 if (newptr != NULL) {
261 WC->burst_alloc = newalloc;
264 if ((WC->burst_len + count) < WC->burst_alloc) {
265 memcpy(&WC->burst[WC->burst_len], buf, count);
266 WC->burst_len += count;
275 client_write_ssl((char *) buf, count);
280 write(2, "\033[34m", 5);
281 write(2, buf, count);
282 write(2, "\033[30m", 5);
284 fdflags = fcntl(WC->http_sock, F_GETFL);
286 while (bytesWritten < count) {
287 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
289 FD_SET(WC->http_sock, &wset);
290 if (select(WC->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
291 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
296 if ((res = write(WC->http_sock, (char*)buf + bytesWritten,
297 count - bytesWritten)) == -1) {
298 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
308 * \brief Begin buffering HTTP output so we can transmit it all in one write operation later.
310 void begin_burst(void)
312 if (WC->burst != NULL) {
317 WC->burst_alloc = 32768;
318 WC->burst = malloc(WC->burst_alloc);
323 * \brief uses the same calling syntax as compress2(), but it
324 * creates a stream compatible with HTTP "Content-encoding: gzip"
327 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
328 #define OS_CODE 0x03 /*< unix */
329 int ZEXPORT compress_gzip(Bytef * dest, /*< compressed buffer*/
330 size_t * destLen, /*< length of the compresed data */
331 const Bytef * source, /*< source to encode */
332 uLong sourceLen, /*< length of source to encode */
333 int level) /*< compression level */
335 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
337 /* write gzip header */
338 snprintf((char *) dest, *destLen,
339 "%c%c%c%c%c%c%c%c%c%c",
340 gz_magic[0], gz_magic[1], Z_DEFLATED,
341 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
347 stream.next_in = (Bytef *) source;
348 stream.avail_in = (uInt) sourceLen;
349 stream.next_out = dest + 10L; // after header
350 stream.avail_out = (uInt) * destLen;
351 if ((uLong) stream.avail_out != *destLen)
354 stream.zalloc = (alloc_func) 0;
355 stream.zfree = (free_func) 0;
356 stream.opaque = (voidpf) 0;
358 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
359 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
363 err = deflate(&stream, Z_FINISH);
364 if (err != Z_STREAM_END) {
366 return err == Z_OK ? Z_BUF_ERROR : err;
368 *destLen = stream.total_out + 10L;
370 /* write CRC and Length */
371 uLong crc = crc32(0L, source, sourceLen);
373 for (n = 0; n < 4; ++n, ++*destLen) {
374 dest[*destLen] = (int) (crc & 0xff);
377 uLong len = stream.total_in;
378 for (n = 0; n < 4; ++n, ++*destLen) {
379 dest[*destLen] = (int) (len & 0xff);
382 err = deflateEnd(&stream);
388 * \brief Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
395 if (WC->burst == NULL)
398 the_len = WC->burst_len;
399 the_data = WC->burst;
406 /* Perform gzip compression, if enabled and supported by client */
408 char *compressed_data = NULL;
409 size_t compressed_len;
411 compressed_len = ((the_len * 101) / 100) + 100;
412 compressed_data = malloc(compressed_len);
414 if (compress_gzip((Bytef *) compressed_data,
417 (uLongf) the_len, Z_BEST_SPEED) == Z_OK) {
418 wprintf("Content-encoding: gzip\r\n");
420 the_data = compressed_data;
421 the_len = compressed_len;
423 free(compressed_data);
426 #endif /* HAVE_ZLIB */
428 wprintf("Content-length: %d\r\n\r\n", the_len);
429 client_write(the_data, the_len);
437 * \brief Read data from the client socket with default timeout.
438 * (This is implemented in terms of client_read_to() and could be
439 * justifiably moved out of sysdep.c)
440 * \param sock the socket fd to read from
441 * \param buf the buffer to write to
442 * \param bytes Number of bytes to read
444 int client_read(int sock, char *buf, int bytes)
446 return (client_read_to(sock, buf, bytes, SLEEPING));
451 * \brief Get a LF-terminated line of text from the client.
452 * (This is implemented in terms of client_read() and could be
453 * justifiably moved out of sysdep.c)
454 * \param sock socket fd to get client line from
455 * \param buf buffer to write read data to
456 * \param bufsiz how many bytes to read
457 * \return number of bytes read???
459 int client_getln(int sock, char *buf, int bufsiz)
463 /* Read one character at a time.*/
465 retval = client_read(sock, &buf[i], 1);
466 if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
468 if ( (!isspace(buf[i])) && (!isprint(buf[i])) ) {
469 /* Non printable character recieved from client */
474 /* If we got a long line, discard characters until the newline. */
476 while (buf[i] != '\n' && retval == 1)
477 retval = client_read(sock, &buf[i], 1);
480 * Strip any trailing non-printable characters.
483 while ((i > 0) && (!isprint(buf[i - 1]))) {
490 * \brief shut us down the regular way.
491 * param signum the signal we want to forward
494 void graceful_shutdown_watcher(int signum) {
495 lprintf (1, "bye; shutting down watcher.");
496 kill(current_child, signum);
497 if (signum != SIGHUP)
502 * \brief shut us down the regular way.
503 * param signum the signal we want to forward
506 void graceful_shutdown(int signum) {
507 // kill(current_child, signum);
512 lprintf (1, "bye going down gracefull.[%d][%s]\n", signum, wd);
524 * \brief Start running as a daemon.
526 * param do_close_stdio Only close stdio if set.
530 * Start running as a daemon.
532 void start_daemon(char *pid_file)
541 /* Close stdin/stdout/stderr and replace them with /dev/null.
542 * We don't just call close() because we don't want these fd's
543 * to be reused for other files.
547 signal(SIGHUP, SIG_IGN);
548 signal(SIGINT, SIG_IGN);
549 signal(SIGQUIT, SIG_IGN);
558 freopen("/dev/null", "r", stdin);
559 freopen("/dev/null", "w", stdout);
560 freopen("/dev/null", "w", stderr);
561 signal(SIGTERM, graceful_shutdown_watcher);
562 signal(SIGHUP, graceful_shutdown_watcher);
565 current_child = fork();
568 if (current_child < 0) {
570 ShutDownLibCitadel ();
574 else if (current_child == 0) { // child process
575 // signal(SIGTERM, graceful_shutdown);
576 signal(SIGHUP, graceful_shutdown);
578 return; /* continue starting webcit. */
581 else { // watcher process
582 // signal(SIGTERM, SIG_IGN);
583 // signal(SIGHUP, SIG_IGN);
585 fp = fopen(pid_file, "w");
587 fprintf(fp, "%d\n", getpid());
591 waitpid(current_child, &status, 0);
596 /* Did the main process exit with an actual exit code? */
597 if (WIFEXITED(status)) {
599 /* Exit code 0 means the watcher should exit */
600 if (WEXITSTATUS(status) == 0) {
604 /* Exit code 101-109 means the watcher should exit */
605 else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
609 /* Any other exit code means we should restart. */
615 /* Any other type of termination (signals, etc.) should also restart. */
620 } while (do_restart);
625 ShutDownLibCitadel ();
626 exit(WEXITSTATUS(status));
630 * \brief Spawn an additional worker thread into the pool.
632 void spawn_another_worker_thread()
634 pthread_t SessThread; /*< Thread descriptor */
635 pthread_attr_t attr; /*< Thread attributes */
638 lprintf(3, "Creating a new thread\n");
640 /* set attributes for the new thread */
641 pthread_attr_init(&attr);
642 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
645 * Our per-thread stacks need to be bigger than the default size, otherwise
646 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
649 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
650 lprintf(1, "pthread_attr_setstacksize: %s\n",
652 pthread_attr_destroy(&attr);
655 /* now create the thread */
656 if (pthread_create(&SessThread, &attr,
657 (void *(*)(void *)) worker_entry, NULL)
659 lprintf(1, "Can't create thread: %s\n", strerror(errno));
662 /* free up the attributes */
663 pthread_attr_destroy(&attr);
667 * \brief Here's where it all begins.
668 * \param argc number of commandline args
669 * \param argv the commandline arguments
671 int main(int argc, char **argv)
673 pthread_t SessThread; /*< Thread descriptor */
674 pthread_attr_t attr; /*< Thread attributes */
675 int a, i; /*< General-purpose variables */
676 char tracefile[PATH_MAX];
677 char ip_addr[256]="0.0.0.0";
678 char dirbuffer[PATH_MAX]="";
681 int home_specified=0;
682 char relhome[PATH_MAX]="";
683 char webcitdir[PATH_MAX] = DATADIR;
684 char *pidfile = NULL;
690 #endif /* ENABLE_NLS */
691 char uds_listen_path[PATH_MAX]; /*< listen on a unix domain socket? */
693 HandlerHash = NewHash (1, NULL);
694 /* Ensure that we are linked to the correct version of libcitadel */
695 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
696 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
697 (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
698 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
699 (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
703 strcpy(uds_listen_path, "");
705 /* Parse command line */
707 while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cfs")) != EOF)
709 while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cf")) != EOF)
713 hdir = strdup(optarg);
715 if (!relh) safestrncpy(webcitdir, hdir,
718 safestrncpy(relhome, relhome,
720 /* free(hdir); TODO: SHOULD WE DO THIS? */
725 running_as_daemon = 1;
728 pidfile = strdup(optarg);
729 running_as_daemon = 1;
732 safestrncpy(ip_addr, optarg, sizeof ip_addr);
735 http_port = atoi(optarg);
736 if (http_port == 0) {
737 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
741 safestrncpy(tracefile, optarg, sizeof tracefile);
742 freopen(tracefile, "w", stdout);
743 freopen(tracefile, "w", stderr);
744 freopen(tracefile, "r", stdin);
747 verbosity = atoi(optarg);
753 server_cookie = malloc(256);
754 if (server_cookie != NULL) {
755 safestrncpy(server_cookie,
756 "Set-cookie: wcserver=",
759 (&server_cookie[strlen(server_cookie)],
761 lprintf(2, "gethostname: %s\n",
771 fprintf(stderr, "usage: webcit "
772 "[-i ip_addr] [-p http_port] "
773 "[-t tracefile] [-c] [-f] "
778 "[remotehost [remoteport]]\n");
783 ctdlhost = argv[optind];
785 ctdlport = argv[optind];
788 /* daemonize, if we were asked to */
789 if (running_as_daemon) {
790 start_daemon(pidfile);
793 /// signal(SIGTERM, graceful_shutdown);
794 signal(SIGHUP, graceful_shutdown);
797 /* Tell 'em who's in da house */
798 lprintf(1, PACKAGE_STRING "\n");
799 lprintf(1, "Copyright (C) 1996-2008 by the Citadel development team.\n"
800 "This software is distributed under the terms of the "
801 "GNU General Public License.\n\n"
805 /* initialize the International Bright Young Thing */
807 initialize_locales();
809 locale = setlocale(LC_ALL, "");
811 mo = malloc(strlen(webcitdir) + 20);
812 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
814 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
815 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
820 /* calculate all our path on a central place */
821 /* where to keep our config */
823 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
824 snprintf(SUBDIR,sizeof SUBDIR, "%s%s%s%s%s%s%s", \
825 (home&!relh)?webcitdir:basedir, \
826 ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
827 ((basedir!=webcitdir)&(home&!relh))?"/":"", \
829 (relhome[0]!='\0')?"/":"",\
831 (dirbuffer[0]!='\0')?"/":"");
833 COMPUTE_DIRECTORY(socket_dir);
834 basedir=WWWDIR "/static";
835 COMPUTE_DIRECTORY(static_dir);
836 basedir=WWWDIR "/static/icons";
837 COMPUTE_DIRECTORY(static_icon_dir);
838 basedir=WWWDIR "/static.local";
839 COMPUTE_DIRECTORY(static_local_dir);
841 snprintf(file_crpt_file_key,
842 sizeof file_crpt_file_key,
845 snprintf(file_crpt_file_csr,
846 sizeof file_crpt_file_csr,
849 snprintf(file_crpt_file_cer,
850 sizeof file_crpt_file_cer,
854 /* we should go somewhere we can leave our coredump, if enabled... */
855 lprintf(9, "Changing directory to %s\n", socket_dir);
856 if (chdir(webcitdir) != 0) {
859 LoadIconDir(static_icon_dir);
860 initialize_viewdefs();
864 * Set up a place to put thread-specific data.
865 * We only need a single pointer per thread - it points to the
866 * wcsession struct to which the thread is currently bound.
868 if (pthread_key_create(&MyConKey, NULL) != 0) {
869 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
871 InitialiseSemaphores ();
874 * Set up a place to put thread-specific SSL data.
875 * We don't stick this in the wcsession struct because SSL starts
876 * up before the session is bound, and it gets torn down between
880 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
881 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
886 * Bind the server to our favorite port.
887 * There is no need to check for errors, because ig_tcp_server()
888 * exits if it doesn't succeed.
891 if (!IsEmptyStr(uds_listen_path)) {
892 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
893 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
896 lprintf(2, "Attempting to bind to port %d...\n", http_port);
897 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
900 lprintf(2, "Listening on socket %d\n", msock);
901 signal(SIGPIPE, SIG_IGN);
903 pthread_mutex_init(&SessionListMutex, NULL);
906 * Start up the housekeeping thread
908 pthread_attr_init(&attr);
909 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
910 pthread_create(&SessThread, &attr,
911 (void *(*)(void *)) housekeeping_loop, NULL);
915 * If this is an HTTPS server, fire up SSL
923 /* Start a few initial worker threads */
924 for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
925 spawn_another_worker_thread();
928 /* now the original thread becomes another worker */
930 ShutDownLibCitadel ();
936 * Entry point for worker threads
938 void worker_entry(void)
942 int fail_this_transaction = 0;
945 fd_set readset, tempset;
950 FD_SET(msock, &readset);
953 /* Only one thread can accept at a time */
954 fail_this_transaction = 0;
958 ret = -1; /* just one at once should select... */
959 begin_critical_section(S_SELECT);
962 if (msock > 0) FD_SET(msock, &tempset);
965 if (msock > 0) ret = select(msock+1, &tempset, NULL, NULL, &tv);
966 end_critical_section(S_SELECT);
967 if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
968 {// EINTR and EAGAIN are thrown but not of interest.
969 lprintf(2, "accept() failed:%d %s\n",
970 errno, strerror(errno));
972 else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
973 {// Successfully selected, and still not shutting down? Accept!
974 ssock = accept(msock, NULL, 0);
977 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
979 if ((msock == -1)||(time_to_die))
980 {// ok, we're going down.
983 /* the first to come here will have to do the cleanup.
984 * make shure its realy just one.
986 begin_critical_section(S_SHUTDOWN);
992 end_critical_section(S_SHUTDOWN);
994 {// we're the one to cleanup the mess.
995 lprintf(2, "I'm master shutdown: tagging sessions to be killed.\n");
997 lprintf(2, "master shutdown: waiting for others\n");
998 sleeeeeeeeeep(1); // wait so some others might finish...
999 lprintf(2, "master shutdown: cleaning up sessions\n");
1001 lprintf(2, "master shutdown: cleaning up libical\n");
1002 free_zone_directory ();
1003 icaltimezone_release_zone_tab ();
1004 icalmemory_free_ring ();
1005 ShutDownLibCitadel ();
1006 lprintf(2, "master shutdown exiting!.\n");
1011 if (ssock < 0 ) continue;
1014 if (ssock > 0) close (ssock);
1015 lprintf(2, "inbetween.");
1017 } else { // Got it? do some real work!
1018 /* Set the SO_REUSEADDR socket option */
1020 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
1023 /* If we are an HTTPS server, go crypto now. */
1026 if (starttls(ssock) != 0) {
1027 fail_this_transaction = 1;
1033 if (fail_this_transaction == 0) {
1035 /* Perform an HTTP transaction... */
1036 context_loop(ssock);
1038 /* Shut down SSL/TLS if required... */
1045 /* ...and close the socket. */
1046 lingering_close(ssock);
1051 } while (!time_to_die);
1053 lprintf (1, "bye\n");
1058 * \brief logprintf. log messages
1059 * logs to stderr if loglevel is lower than the verbosity set at startup
1060 * \param loglevel level of the message
1061 * \param format the printf like format string
1062 * \param ... the strings to put into format
1064 int lprintf(int loglevel, const char *format, ...)
1068 if (loglevel <= verbosity) {
1069 va_start(ap, format);
1070 vfprintf(stderr, format, ap);
1079 * \brief print the actual stack frame.
1081 void wc_backtrace(void)
1083 #ifdef HAVE_BACKTRACE
1084 void *stack_frames[50];
1089 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1090 strings = backtrace_symbols(stack_frames, size);
1091 for (i = 0; i < size; i++) {
1092 if (strings != NULL)
1093 lprintf(1, "%s\n", strings[i]);
1095 lprintf(1, "%p\n", stack_frames[i]);