-
-
-/*
- * Shut us down the regular way.
- * signum is the signal we want to forward
- */
-pid_t current_child;
-void graceful_shutdown_watcher(int signum) {
- lprintf (1, "bye; shutting down watcher.");
- kill(current_child, signum);
- if (signum != SIGHUP)
- exit(0);
-}
-
-
-
-
-/*
- * Shut us down the regular way.
- * signum is the signal we want to forward
- */
-pid_t current_child;
-void graceful_shutdown(int signum) {
- char wd[SIZ];
- FILE *FD;
- int fd;
- getcwd(wd, SIZ);
- lprintf (1, "bye going down gracefull.[%d][%s]\n", signum, wd);
- fd = msock;
- msock = -1;
- time_to_die = 1;
- FD=fdopen(fd, "a+");
- fflush (FD);
- fclose (FD);
- close(fd);
-}
-
-
-/*
- * Start running as a daemon.
- */
-void start_daemon(char *pid_file)
-{
- int status = 0;
- pid_t child = 0;
- FILE *fp;
- int do_restart = 0;
-
- current_child = 0;
-
- /* Close stdin/stdout/stderr and replace them with /dev/null.
- * We don't just call close() because we don't want these fd's
- * to be reused for other files.
- */
- chdir("/");
-
- signal(SIGHUP, SIG_IGN);
- signal(SIGINT, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
-
- child = fork();
- if (child != 0) {
- exit(0);
- }
-
- setsid();
- umask(0);
- freopen("/dev/null", "r", stdin);
- freopen("/dev/null", "w", stdout);
- freopen("/dev/null", "w", stderr);
- signal(SIGTERM, graceful_shutdown_watcher);
- signal(SIGHUP, graceful_shutdown_watcher);
-
- do {
- current_child = fork();
-
-
- if (current_child < 0) {
- perror("fork");
- ShutDownLibCitadel ();
- exit(errno);
- }
-
- else if (current_child == 0) { /* child process */
- signal(SIGHUP, graceful_shutdown);
-
- return; /* continue starting webcit. */
- }
- else { /* watcher process */
- if (pid_file) {
- fp = fopen(pid_file, "w");
- if (fp != NULL) {
- fprintf(fp, "%d\n", getpid());
- fclose(fp);
- }
- }
- waitpid(current_child, &status, 0);
- }
-
- do_restart = 0;
-
- /* Did the main process exit with an actual exit code? */
- if (WIFEXITED(status)) {
-
- /* Exit code 0 means the watcher should exit */
- if (WEXITSTATUS(status) == 0) {
- do_restart = 0;
- }
-
- /* Exit code 101-109 means the watcher should exit */
- else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
- do_restart = 0;
- }
-
- /* Any other exit code means we should restart. */
- else {
- do_restart = 1;
- }
- }
-
- /* Any other type of termination (signals, etc.) should also restart. */
- else {
- do_restart = 1;
- }
-
- } while (do_restart);
-
- if (pid_file) {
- unlink(pid_file);
- }
- ShutDownLibCitadel ();
- exit(WEXITSTATUS(status));
-}
-
-/*
- * Spawn an additional worker thread into the pool.
- */
-void spawn_another_worker_thread()
-{
- pthread_t SessThread; /* Thread descriptor */
- pthread_attr_t attr; /* Thread attributes */
- int ret;
-
- lprintf(3, "Creating a new thread\n");
-
- /* set attributes for the new thread */
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
- /*
- * Our per-thread stacks need to be bigger than the default size, otherwise
- * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
- * 64-bit Linux.
- */
- if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
- lprintf(1, "pthread_attr_setstacksize: %s\n",
- strerror(ret));
- pthread_attr_destroy(&attr);
- }
-
- /* now create the thread */
- if (pthread_create(&SessThread, &attr,
- (void *(*)(void *)) worker_entry, NULL)
- != 0) {
- lprintf(1, "Can't create thread: %s\n", strerror(errno));
- }
-
- /* free up the attributes */
- pthread_attr_destroy(&attr);
-}
-