/* milter-callback - a sendmail milter to perform variuos sender verifications
 Copyright (C) 2008  Eugene M. Zheganin, milter-callback@norma.perm.ru
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <milter-callback.h>
#include <cache.h>
#include <cpit.h>
#include <commonprocs.h>

extern int logLevel;
extern int mcDebug;

void
getCaches (int sig) {
    char tmpLog[MAXLOGLINE];
    int mtxErr;
    
    /* displaying cache */
    logNow("Got SIGUSR1, displaying both caches.", 0, 0);

    /* displaying cache */
    logNow("Trying to lock the cache.", 3, 0);
    mtxErr = pthread_mutex_lock(mcCacheBusy);
    if (mtxErr == 0) {
	logNow("Lock aquired.", 3, 0);
	getCache(0);
	snprintf(tmpLog, MAXLOGLINE, "Trying to unlock the cache.");
        logNow(&tmpLog[0], 3, 0);
        mtxErr = pthread_mutex_unlock(mcCacheBusy);
        if (mtxErr == 0) {
            logNow("Cache unlocked.", 3, 0);
        } else {
            snprintf(tmpLog, MAXLOGLINE, "Cache cannot be unlocked, pthread_mutex_unlock() signalled %d.", mtxErr);
            logNow(&tmpLog[0], 0, 0);
        }
    } else {
	snprintf(tmpLog, MAXLOGLINE, "Lock cannot be aquired, pthread_mutex_lock() signalled %d.", mtxErr);
	logNow(&tmpLog[0], 0, 0);
    }

    /* displaying pit */
    snprintf(tmpLog, MAXLOGLINE, "Trying to lock the pit cache.");
    logNow(&tmpLog[0], 3, 0);
    mtxErr = pthread_mutex_lock(mcPitBusy);
    if (mtxErr == 0) {
        logNow("Lock aquired.", 3, 0);
	getPit(0, 0);
	/* unflag mutex */
	snprintf(tmpLog, MAXLOGLINE, "Trying to unlock the pit cache.");
        logNow(&tmpLog[0], 3, 0);
        mtxErr = pthread_mutex_unlock(mcPitBusy);
        if (mtxErr == 0) {
            logNow("Pit cache unlocked.", 3, 0);
        } else {
            snprintf(tmpLog, MAXLOGLINE, "Pit cache cannot be unlocked, pthread_mutex_unlock() signalled %d.", mtxErr);
            logNow(&tmpLog[0], 0, 0);
        }
    } else {
        snprintf(tmpLog, MAXLOGLINE, "Lock on the pit cannot be aquired, pthread_mutex_lock() signalled %d.", mtxErr);
        logNow(&tmpLog[0], 0, 0);
    };
};
