Back to index

citadel  8.12
housekeeping.c
Go to the documentation of this file.
00001 /*
00002  * This file contains miscellaneous housekeeping tasks.
00003  *
00004  * Copyright (c) 1987-2011 by the citadel.org team
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019  */
00020 
00021 #include "sysdep.h"
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 #include <stdio.h>
00025 #include <fcntl.h>
00026 
00027 #if TIME_WITH_SYS_TIME
00028 # include <sys/time.h>
00029 # include <time.h>
00030 #else
00031 # if HAVE_SYS_TIME_H
00032 #  include <sys/time.h>
00033 # else
00034 #  include <time.h>
00035 # endif
00036 #endif
00037 
00038 #include <ctype.h>
00039 #include <string.h>
00040 #include <errno.h>
00041 #include <limits.h>
00042 #include <sys/types.h>
00043 #ifdef HAVE_SYS_SELECT_H
00044 #include <sys/select.h>
00045 #endif
00046 #include <syslog.h>
00047 #include <libcitadel.h>
00048 #include "citadel.h"
00049 #include "server.h"
00050 #include "serv_extensions.h"
00051 #include "citserver.h"
00052 #include "config.h"
00053 #include "housekeeping.h"
00054 #include "sysdep_decls.h"
00055 #include "room_ops.h"
00056 #include "database.h"
00057 #include "msgbase.h"
00058 #include "journaling.h"
00059 
00060 #include "ctdl_module.h"
00061 #include "threads.h"
00062 
00063 void check_sched_shutdown(void) {
00064        if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
00065               syslog(LOG_NOTICE, "Scheduled shutdown initiating.\n");
00066               server_shutting_down = 1;
00067        }
00068 }
00069 
00070 
00071 
00072 /*
00073  * Check (and fix) floor reference counts.  This doesn't need to be done
00074  * very often, since the counts should remain correct during normal operation.
00075  */
00076 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
00077 
00078        int *new_refcounts;
00079 
00080        new_refcounts = (int *) data;
00081 
00082        ++new_refcounts[(int)qrbuf->QRfloor];
00083 }
00084 
00085 void check_ref_counts(void) {
00086        struct floor flbuf;
00087        int a;
00088 
00089        int new_refcounts[MAXFLOORS];
00090 
00091        syslog(LOG_DEBUG, "Checking floor reference counts\n");
00092        for (a=0; a<MAXFLOORS; ++a) {
00093               new_refcounts[a] = 0;
00094        }
00095 
00096        cdb_begin_transaction();
00097        CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
00098        cdb_end_transaction();
00099 
00100        for (a=0; a<MAXFLOORS; ++a) {
00101               lgetfloor(&flbuf, a);
00102               flbuf.f_ref_count = new_refcounts[a];
00103               if (new_refcounts[a] > 0) {
00104                      flbuf.f_flags = flbuf.f_flags | QR_INUSE;
00105               }
00106               else {
00107                      flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
00108               }
00109               lputfloor(&flbuf, a);
00110               syslog(LOG_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
00111        }
00112 }      
00113 
00114 /*
00115  * This is the housekeeping loop.  Worker threads come through here after
00116  * processing client requests but before jumping back into the pool.  We
00117  * only allow housekeeping to execute once per minute, and we only allow one
00118  * instance to run at a time.
00119  */
00120 static int housekeeping_in_progress = 0;
00121 static time_t last_timer = 0L;
00122 void do_housekeeping(void) {
00123        int do_housekeeping_now = 0;
00124        int do_perminute_housekeeping_now = 0;
00125        time_t now;
00126 
00127        /*
00128         * We do it this way instead of wrapping the whole loop in an
00129         * S_HOUSEKEEPING critical section because it eliminates the need to
00130         * potentially have multiple concurrent mutexes in progress.
00131         */
00132        begin_critical_section(S_HOUSEKEEPING);
00133        if (housekeeping_in_progress == 0) {
00134               do_housekeeping_now = 1;
00135               housekeeping_in_progress = 1;
00136        }
00137        end_critical_section(S_HOUSEKEEPING);
00138 
00139        if (do_housekeeping_now == 0) {
00140               return;
00141        }
00142 
00143        /*
00144         * Ok, at this point we've made the decision to run the housekeeping
00145         * loop.  Everything below this point is real work.
00146         */
00147 
00148        now = time(NULL);
00149        if ( (now - last_timer) > (time_t)60 ) {
00150               do_perminute_housekeeping_now = 1;
00151               last_timer = time(NULL);
00152        }
00153 
00154        /* First, do the "as often as needed" stuff... */
00155        JournalRunQueue();
00156        PerformSessionHooks(EVT_HOUSE);
00157 
00158        /* Then, do the "once per minute" stuff... */
00159        if (do_perminute_housekeeping_now) {
00160               cdb_check_handles();               /* suggested by Justin Case */
00161               PerformSessionHooks(EVT_TIMER);           /* Run any timer hooks */
00162        }
00163 
00164        /*
00165         * All done.
00166         */
00167        begin_critical_section(S_HOUSEKEEPING);
00168        housekeeping_in_progress = 0;
00169        end_critical_section(S_HOUSEKEEPING);
00170 }