]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_expire.c
* Add specific error codes for every command on the wire protocol, so that
[citadel.git] / citadel / serv_expire.c
index 149ad0e6cfaf9b378daa16dd78ca4baf6ef0d719..f3de34e25b1b7d8299e3cd61b36d4bb6328ebe89 100644 (file)
@@ -14,8 +14,8 @@
  * then the second stage deletes all listed objects from the database.
  *
  * At first glance this may seem cumbersome and unnecessary.  The reason it is
- * implemented in this way is because GDBM (and perhaps some other backends we
- * may hook into in the future) explicitly do _not_ support the deletion of
+ * implemented in this way is because Berkeley DB, and possibly other backends
+ * we may hook into in the future, explicitly do _not_ support the deletion of
  * records from a file while the file is being traversed.  The delete operation
  * will succeed, but the traversal is not guaranteed to visit every object if
  * this is done.  Therefore we utilize the two-stage purge.
 #include <limits.h>
 #include "citadel.h"
 #include "server.h"
-#include <syslog.h>
 #include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "dynloader.h"
+#include "serv_extensions.h"
 #include "room_ops.h"
 #include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "user_ops.h"
 #include "control.h"
+#include "serv_network.h"
 #include "tools.h"
 
 
@@ -90,11 +90,16 @@ struct ValidUser {
 };
 
 
-struct roomref {
-       struct roomref *next;
+struct ctdlroomref {
+       struct ctdlroomref *next;
        long msgnum;
 };
 
+struct UPurgeList {
+       struct UPurgeList *next;
+       char up_key[SIZ];
+};
+
 
 struct PurgeList *UserPurgeList = NULL;
 struct PurgeList *RoomPurgeList = NULL;
@@ -102,7 +107,7 @@ struct ValidRoom *ValidRoomList = NULL;
 struct ValidUser *ValidUserList = NULL;
 int messages_purged;
 
-struct roomref *rr = NULL;
+struct ctdlroomref *rr = NULL;
 
 extern struct CitContext *ContextList;
 
@@ -111,7 +116,7 @@ extern struct CitContext *ContextList;
  * First phase of message purge -- gather the locations of messages which
  * qualify for purging and write them to a temp file.
  */
-void GatherPurgeMessages(struct quickroom *qrbuf, void *data) {
+void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
        struct ExpirePolicy epbuf;
        long delnum;
        time_t xtime, now;
@@ -227,7 +232,7 @@ void PurgeMessages(void) {
 }
 
 
-void AddValidUser(struct usersupp *usbuf, void *data) {
+void AddValidUser(struct ctdluser *usbuf, void *data) {
        struct ValidUser *vuptr;
 
        vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
@@ -236,7 +241,7 @@ void AddValidUser(struct usersupp *usbuf, void *data) {
        ValidUserList = vuptr;
 }
 
-void AddValidRoom(struct quickroom *qrbuf, void *data) {
+void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
        struct ValidRoom *vrptr;
 
        vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
@@ -246,7 +251,7 @@ void AddValidRoom(struct quickroom *qrbuf, void *data) {
        ValidRoomList = vrptr;
 }
 
-void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
+void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
        time_t age, purge_secs;
        struct PurgeList *pptr;
        struct ValidUser *vuptr;
@@ -283,7 +288,7 @@ void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
                age = time(NULL) - (qrbuf->QRmtime);
                purge_secs = (time_t)config.c_roompurge * (time_t)86400;
                if (purge_secs <= (time_t)0) return;
-               lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
+               lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
                if (age > purge_secs) do_purge = 1;
        } /* !QR_MAILBOX */
 
@@ -301,7 +306,7 @@ void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
 int PurgeRooms(void) {
        struct PurgeList *pptr;
        int num_rooms_purged = 0;
-       struct quickroom qrbuf;
+       struct ctdlroom qrbuf;
        struct ValidUser *vuptr;
        char *transcript = NULL;
 
@@ -329,7 +334,7 @@ int PurgeRooms(void) {
        while (RoomPurgeList != NULL) {
                if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
                        transcript=reallok(transcript, strlen(transcript)+SIZ);
-                       sprintf(&transcript[strlen(transcript)], " %s\n",
+                       snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
                                qrbuf.QRname);
                        delete_room(&qrbuf);
                }
@@ -347,7 +352,7 @@ int PurgeRooms(void) {
 }
 
 
-void do_user_purge(struct usersupp *us, void *data) {
+void do_user_purge(struct ctdluser *us, void *data) {
        int purge;
        time_t now;
        time_t purge_time;
@@ -355,10 +360,10 @@ void do_user_purge(struct usersupp *us, void *data) {
 
        /* stupid recovery routine to re-create missing mailboxen.
         * don't enable this.
-       struct quickroom qrbuf;
+       struct ctdlroom qrbuf;
        char mailboxname[ROOMNAMELEN];
        MailboxName(mailboxname, us, MAILROOM);
-       create_room(mailboxname, 4, "", 0, 1);
+       create_room(mailboxname, 4, "", 0, 1, 1);
        if (getroom(&qrbuf, mailboxname) != 0) return;
        lprintf(9, "Got %s\n", qrbuf.QRname);
         */
@@ -427,7 +432,7 @@ int PurgeUsers(void) {
 
        while (UserPurgeList != NULL) {
                transcript=reallok(transcript, strlen(transcript)+SIZ);
-               sprintf(&transcript[strlen(transcript)], " %s\n",
+               snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
                        UserPurgeList->name);
                purge_user(UserPurgeList->name);
                pptr = UserPurgeList->next;
@@ -475,7 +480,6 @@ int PurgeVisits(void) {
        ForEachUser(AddValidUser, NULL);
 
        /* Now traverse through the visits, purging irrelevant records... */
-       cdb_begin_transaction();
        cdb_rewind(CDB_VISIT);
        while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
                memset(&vbuf, 0, sizeof(struct visit));
@@ -513,8 +517,6 @@ int PurgeVisits(void) {
 
        }
 
-       cdb_end_transaction();
-
        /* Free the valid room/gen combination list */
        while (ValidRoomList != NULL) {
                vrptr = ValidRoomList->next;
@@ -545,60 +547,106 @@ int PurgeVisits(void) {
        return(purged);
 }
 
+/*
+ * Purge the use table of old entries.
+ *
+ */
+int PurgeUseTable(void) {
+       int purged = 0;
+       struct cdbdata *cdbut;
+       struct UseTable ut;
+       struct UPurgeList *ul = NULL;
+       struct UPurgeList *uptr; 
+
+       /* Phase 1: traverse through the table, discovering old records... */
+       lprintf(9, "Purge use table: phase 1\n");
+       cdb_rewind(CDB_USETABLE);
+       while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
+
+                memcpy(&ut, cdbut->ptr,
+                       ((cdbut->len > sizeof(struct UseTable)) ?
+                        sizeof(struct UseTable) : cdbut->len));
+                cdb_free(cdbut);
+
+               if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
+                       uptr = (struct UPurgeList *) mallok(sizeof(struct UPurgeList));
+                       if (uptr != NULL) {
+                               uptr->next = ul;
+                               safestrncpy(uptr->up_key, ut.ut_msgid, SIZ);
+                               ul = uptr;
+                       }
+                       ++purged;
+               }
+
+       }
+
+       /* Phase 2: delete the records */
+       lprintf(9, "Purge use table: phase 2\n");
+       while (ul != NULL) {
+               cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
+               uptr = ul->next;
+               phree(ul);
+               ul = uptr;
+       }
+
+       lprintf(9, "Purge use table: finished (purged %d records)\n", purged);
+       return(purged);
+}
+
 
-void cmd_expi(char *argbuf) {
-       char cmd[SIZ];
+void purge_databases(void) {
        int retval;
+       static time_t last_purge = 0;
+       time_t now;
+       struct tm tm;
 
-       if (CtdlAccessCheck(ac_aide)) return;
+       /* Do the auto-purge if the current hour equals the purge hour,
+        * but not if the operation has already been performed in the
+        * last twelve hours.  This is usually enough granularity.
+        */
+       now = time(NULL);
+       memcpy(&tm, localtime(&now), sizeof(struct tm));
+       if (tm.tm_hour != config.c_purge_hour) return;
+       if ((now - last_purge) < 43200) return;
 
-       extract(cmd, argbuf, 0);
-       if (!strcasecmp(cmd, "users")) {
-               retval = PurgeUsers();
-               cprintf("%d Purged %d users.\n", OK, retval);
-               return;
-       }
-       else if (!strcasecmp(cmd, "messages")) {
-               PurgeMessages();
-               cprintf("%d Expired %d messages.\n", OK, messages_purged);
-               return;
-       }
-       else if (!strcasecmp(cmd, "rooms")) {
-               retval = PurgeRooms();
-               cprintf("%d Expired %d rooms.\n", OK, retval);
-               return;
-       }
-       else if (!strcasecmp(cmd, "visits")) {
-               retval = PurgeVisits();
-               cprintf("%d Purged %d visits.\n", OK, retval);
-       }
-       else if (!strcasecmp(cmd, "defrag")) {
-               defrag_databases();
-               cprintf("%d Defragmented the databases.\n", OK);
-       }
-       else {
-               cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
-               return;
-       }
+       lprintf(3, "Auto-purger: starting.\n");
+
+       retval = PurgeUsers();
+       lprintf(3, "Purged %d users.\n", retval);
+
+       PurgeMessages();
+       lprintf(3, "Expired %d messages.\n", messages_purged);
+
+       retval = PurgeRooms();
+       lprintf(3, "Expired %d rooms.\n", retval);
+
+       retval = PurgeVisits();
+       lprintf(3, "Purged %d visits.\n", retval);
+
+       retval = PurgeUseTable();
+       lprintf(3, "Purged %d entries from the use table.\n", retval);
+
+       lprintf(3, "Auto-purger: finished.\n");
+
+       last_purge = now;       /* So we don't do it again soon */
 }
 
 /*****************************************************************************/
 
 
 void do_fsck_msg(long msgnum, void *userdata) {
-       struct roomref *ptr;
+       struct ctdlroomref *ptr;
 
-       ptr = (struct roomref *)mallok(sizeof(struct roomref));
+       ptr = (struct ctdlroomref *)mallok(sizeof(struct ctdlroomref));
        ptr->next = rr;
        ptr->msgnum = msgnum;
        rr = ptr;
 }
 
-void do_fsck_room(struct quickroom *qrbuf, void *data)
+void do_fsck_room(struct ctdlroom *qrbuf, void *data)
 {
-       getroom(&CC->quickroom, qrbuf->QRname);
-       CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL,
-               do_fsck_msg, NULL);
+       getroom(&CC->room, qrbuf->QRname);
+       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, do_fsck_msg, NULL);
 }
 
 /*
@@ -607,15 +655,15 @@ void do_fsck_room(struct quickroom *qrbuf, void *data)
 void cmd_fsck(char *argbuf) {
        long msgnum;
        struct cdbdata *cdbmsg;
-       struct SuppMsgInfo smi;
-       struct roomref *ptr;
+       struct MetaData smi;
+       struct ctdlroomref *ptr;
        int realcount;
 
        if (CtdlAccessCheck(ac_aide)) return;
 
        /* Lame way of checking whether anyone else is doing this now */
        if (rr != NULL) {
-               cprintf("%d Another FSCK is already running.\n", ERROR);
+               cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
                return;
        }
 
@@ -634,8 +682,8 @@ void cmd_fsck(char *argbuf) {
                        cdb_free(cdbmsg);
                        cprintf("Message %7ld    ", msgnum);
 
-                       GetSuppMsgInfo(&smi, msgnum);
-                       cprintf("refcount=%-2d   ", smi.smi_refcount);
+                       GetMetaData(&smi, msgnum);
+                       cprintf("refcount=%-2d   ", smi.meta_refcount);
 
                        realcount = 0;
                        for (ptr = rr; ptr != NULL; ptr = ptr->next) {
@@ -643,10 +691,10 @@ void cmd_fsck(char *argbuf) {
                        }
                        cprintf("realcount=%-2d\n", realcount);
 
-                       if ( (smi.smi_refcount != realcount)
+                       if ( (smi.meta_refcount != realcount)
                           || (realcount == 0) ) {
-                               smi.smi_refcount = realcount;
-                               PutSuppMsgInfo(&smi);
+                               smi.meta_refcount = realcount;
+                               PutMetaData(&smi);
                                AdjRefCount(msgnum, 0); /* deletes if needed */
                        }
 
@@ -671,9 +719,9 @@ void cmd_fsck(char *argbuf) {
 
 /*****************************************************************************/
 
-char *Dynamic_Module_Init(void)
+char *serv_expire_init(void)
 {
-       CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
+       CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
        CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
        return "$Id$";
 }