2 * WebCit "system dependent" code.
4 * Copyright (c) 1996-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
22 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <sys/syslog.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
41 #include <sys/resource.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
58 #ifdef HAVE_SYS_SELECT_H
59 #include <sys/select.h>
62 #include "webserver.h"
63 #include "modules_init.h"
68 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
69 pthread_key_t MyConKey; /* TSD key for MyContext() */
70 pthread_key_t MyReq; /* TSD key for MyReq() */
71 int msock; /* master listening socket */
72 int time_to_die = 0; /* Nonzero if server is shutting down */
74 extern void *context_loop(ParsedHttpHdrs *Hdr);
75 extern void *housekeeping_loop(void);
76 extern void do_housekeeping(void);
78 char ctdl_key_dir[PATH_MAX]=SSL_DIR;
79 char file_crpt_file_key[PATH_MAX]="";
80 char file_crpt_file_csr[PATH_MAX]="";
81 char file_crpt_file_cer[PATH_MAX]="";
83 const char editor_absolut_dir[PATH_MAX]=EDITORDIR; /* nailed to what configure gives us. */
84 char static_dir[PATH_MAX]; /* calculated on startup */
85 char static_local_dir[PATH_MAX]; /* calculated on startup */
86 char static_icon_dir[PATH_MAX]; /* where should we find our mime icons? */
87 char *static_dirs[]={ /* needs same sort order as the web mapping */
88 (char*)static_dir, /* our templates on disk */
89 (char*)static_local_dir, /* user provided templates disk */
90 (char*)editor_absolut_dir, /* the editor on disk */
91 (char*)static_icon_dir /* our icons... */
96 void InitialiseSemaphores(void)
100 /* Set up a bunch of semaphores to be used for critical sections */
101 for (i=0; i<MAX_SEMAPHORES; ++i) {
102 pthread_mutex_init(&Critters[i], NULL);
107 syslog(LOG_WARNING, "Failed to open exit pipe: %d [%s]\n",
116 * Obtain a semaphore lock to begin a critical section.
118 void begin_critical_section(int which_one)
120 pthread_mutex_lock(&Critters[which_one]);
124 * Release a semaphore lock to end a critical section.
126 void end_critical_section(int which_one)
128 pthread_mutex_unlock(&Critters[which_one]);
132 void ShutDownWebcit(void)
134 free_zone_directory ();
135 icaltimezone_release_zone_tab ();
136 icalmemory_free_ring ();
137 ShutDownLibCitadel ();
147 * Entry point for worker threads
149 void worker_entry(void)
153 int fail_this_transaction = 0;
156 memset(&Hdr, 0, sizeof(ParsedHttpHdrs));
157 Hdr.HR.eReqType = eGET;
158 http_new_modules(&Hdr);
161 /* Each worker thread blocks on accept() while waiting for something to do. */
162 fail_this_transaction = 0;
167 --num_threads_executing;
169 FD_SET(msock, &wset);
170 FD_SET(ExitPipe[1], &wset);
172 select(msock + 1, NULL, &wset, NULL, NULL);
176 ssock = accept(msock, NULL, 0);
177 ++num_threads_executing;
178 if (ssock < 0) fail_this_transaction = 1;
179 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
181 if ((msock == -1)||(time_to_die))
182 {/* ok, we're going down. */
185 /* The first thread to get here will have to do the cleanup.
186 * Make sure it's really just one.
188 begin_critical_section(S_SHUTDOWN);
194 end_critical_section(S_SHUTDOWN);
196 {/* we're the one to cleanup the mess. */
197 http_destroy_modules(&Hdr);
198 syslog(LOG_DEBUG, "I'm master shutdown: tagging sessions to be killed.\n");
200 syslog(LOG_DEBUG, "master shutdown: waiting for others\n");
201 sleeeeeeeeeep(1); /* wait so some others might finish... */
202 syslog(LOG_DEBUG, "master shutdown: cleaning up sessions\n");
204 syslog(LOG_DEBUG, "master shutdown: cleaning up libical\n");
208 syslog(LOG_DEBUG, "master shutdown exiting.\n");
213 if (ssock < 0 ) continue;
215 check_thread_pool_size();
217 /* Now do something. */
219 if (ssock > 0) close (ssock);
220 syslog(LOG_DEBUG, "in between.");
223 /* Got it? do some real work! */
224 /* Set the SO_REUSEADDR socket option */
226 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
228 /* If we are an HTTPS server, go crypto now. */
231 if (starttls(ssock) != 0) {
232 fail_this_transaction = 1;
240 fdflags = fcntl(ssock, F_GETFL);
242 syslog(LOG_WARNING, "unable to get server socket flags! %s \n",
244 fdflags = fdflags | O_NONBLOCK;
245 if (fcntl(ssock, F_SETFL, fdflags) < 0)
246 syslog(LOG_WARNING, "unable to set server socket nonblocking flags! %s \n",
250 if (fail_this_transaction == 0) {
251 Hdr.http_sock = ssock;
253 /* Perform an HTTP transaction... */
256 /* Shut down SSL/TLS if required... */
263 /* ...and close the socket. */
264 if (Hdr.http_sock > 0) {
265 lingering_close(ssock);
267 http_detach_modules(&Hdr);
273 } while (!time_to_die);
275 http_destroy_modules(&Hdr);
276 syslog(LOG_DEBUG, "Thread exiting.\n");
282 * Shut us down the regular way.
283 * signum is the signal we want to forward
286 void graceful_shutdown_watcher(int signum) {
287 syslog(LOG_INFO, "Watcher thread exiting.\n");
288 write(ExitPipe[0], HKEY(" "));
289 kill(current_child, signum);
290 if (signum != SIGHUP)
296 * Shut us down the regular way.
297 * signum is the signal we want to forward
300 void graceful_shutdown(int signum) {
304 syslog(LOG_INFO, "WebCit is being shut down on signal %d.\n", signum);
312 write(ExitPipe[0], HKEY(" "));
317 * Start running as a daemon.
319 void start_daemon(char *pid_file)
328 /* Close stdin/stdout/stderr and replace them with /dev/null.
329 * We don't just call close() because we don't want these fd's
330 * to be reused for other files.
334 signal(SIGHUP, SIG_IGN);
335 signal(SIGINT, SIG_IGN);
336 signal(SIGQUIT, SIG_IGN);
345 freopen("/dev/null", "r", stdin);
346 freopen("/dev/null", "w", stdout);
347 freopen("/dev/null", "w", stderr);
348 signal(SIGTERM, graceful_shutdown_watcher);
349 signal(SIGHUP, graceful_shutdown_watcher);
352 current_child = fork();
355 if (current_child < 0) {
357 ShutDownLibCitadel ();
361 else if (current_child == 0) { /* child process */
362 signal(SIGHUP, graceful_shutdown);
364 return; /* continue starting webcit. */
366 else { /* watcher process */
368 fp = fopen(pid_file, "w");
370 fprintf(fp, "%d\n", getpid());
374 waitpid(current_child, &status, 0);
379 /* Did the main process exit with an actual exit code? */
380 if (WIFEXITED(status)) {
382 /* Exit code 0 means the watcher should exit */
383 if (WEXITSTATUS(status) == 0) {
387 /* Exit code 101-109 means the watcher should exit */
388 else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
392 /* Any other exit code means we should restart. */
398 /* Any other type of termination (signals, etc.) should also restart. */
403 } while (do_restart);
408 ShutDownLibCitadel ();
409 exit(WEXITSTATUS(status));
414 * Spawn an additional worker thread into the pool.
416 void spawn_another_worker_thread()
418 pthread_t SessThread; /* Thread descriptor */
419 pthread_attr_t attr; /* Thread attributes */
422 ++num_threads_existing;
423 ++num_threads_executing;
425 /* set attributes for the new thread */
426 pthread_attr_init(&attr);
427 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
430 * Our per-thread stacks need to be bigger than the default size,
431 * otherwise the MIME parser crashes on FreeBSD.
433 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
434 syslog(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(ret));
435 pthread_attr_destroy(&attr);
438 /* now create the thread */
439 if (pthread_create(&SessThread, &attr, (void *(*)(void *)) worker_entry, NULL) != 0) {
440 syslog(LOG_WARNING, "Can't create thread: %s\n", strerror(errno));
443 /* free up the attributes */
444 pthread_attr_destroy(&attr);
449 webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome)
451 char dirbuffer[PATH_MAX]="";
452 /* calculate all our path on a central place */
453 /* where to keep our config */
455 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
456 snprintf(SUBDIR,sizeof SUBDIR, "%s%s%s%s%s%s%s", \
457 (home&!relh)?webcitdir:basedir, \
458 ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
459 ((basedir!=webcitdir)&(home&!relh))?"/":"", \
461 (relhome[0]!='\0')?"/":"",\
463 (dirbuffer[0]!='\0')?"/":"");
465 COMPUTE_DIRECTORY(socket_dir);
466 basedir=WWWDIR "/static";
467 COMPUTE_DIRECTORY(static_dir);
468 basedir=WWWDIR "/static/icons";
469 COMPUTE_DIRECTORY(static_icon_dir);
470 basedir=WWWDIR "/static.local";
471 COMPUTE_DIRECTORY(static_local_dir);
472 StripSlashes(static_dir, 1);
473 StripSlashes(static_icon_dir, 1);
474 StripSlashes(static_local_dir, 1);
476 snprintf(file_crpt_file_key,
477 sizeof file_crpt_file_key,
480 snprintf(file_crpt_file_csr,
481 sizeof file_crpt_file_csr,
484 snprintf(file_crpt_file_cer,
485 sizeof file_crpt_file_cer,
489 /* we should go somewhere we can leave our coredump, if enabled... */
490 syslog(LOG_INFO, "Changing directory to %s\n", socket_dir);
491 if (chdir(webcitdir) != 0) {
496 void drop_root(uid_t UID)
498 struct passwd pw, *pwp = NULL;
501 * Now that we've bound the sockets, change to the Citadel user id and its
502 * corresponding group ids
506 #ifdef HAVE_GETPWUID_R
507 #ifdef SOLARIS_GETPWUID
508 pwp = getpwuid_r(UID, &pw, pwbuf, sizeof(pwbuf));
509 #else /* SOLARIS_GETPWUID */
510 getpwuid_r(UID, &pw, pwbuf, sizeof(pwbuf), &pwp);
511 #endif /* SOLARIS_GETPWUID */
512 #else /* HAVE_GETPWUID_R */
514 #endif /* HAVE_GETPWUID_R */
517 syslog(LOG_CRIT, "WARNING: getpwuid(%d): %s\n"
518 "Group IDs will be incorrect.\n", UID,
521 initgroups(pw.pw_name, pw.pw_gid);
522 if (setgid(pw.pw_gid))
523 syslog(LOG_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
526 syslog(LOG_INFO, "Changing uid to %ld\n", (long)UID);
527 if (setuid(UID) != 0) {
528 syslog(LOG_CRIT, "setuid() failed: %s\n", strerror(errno));
530 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
531 prctl(PR_SET_DUMPABLE, 1);
538 * print the actual stack frame.
540 void wc_backtrace(long LogLevel)
542 #ifdef HAVE_BACKTRACE
543 void *stack_frames[50];
548 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
549 strings = backtrace_symbols(stack_frames, size);
550 for (i = 0; i < size; i++) {
552 syslog(LogLevel, "%s\n", strings[i]);
554 syslog(LogLevel, "%p\n", stack_frames[i]);