Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

kernel.c 6.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/******************************************************************************
KERNEL.C     Custom kernel function for TOP.

    Copyright 1993 - 2000 Paul J. Sidorsky

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    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, write to the Free Software
Deucе's avatar
Deucе committed
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

This module contains the custom kernel function for TOP as well as the handler
for time warnings.
******************************************************************************/

#include "top.h"

/* top_kernel() - Custom kernel.
   Parameters:  None.
   Returns:  Nothing.
   Notes:  This function is called by the door kit's own kernel.  The door
           kit's kernel is executed every few seconds and does things like
           subtract the user's time and check for carrier.  TOP hooks a
           custom kernel function to perform some time-sensitive tasks
           specific to TOP.
*/
void top_kernel(void)
{
time_t curtime; /* Current time, in UNIX format. */
XINT kd; /* Counter. */
unsigned char tbyte; /* Temporary byte buffer. */
node_idx_typ tnode; /* Temporary node index data. */
long tcred; /* Temporary RA credit holder. */

/* Get the current time. */
curtime = time(NULL);

/* Support for RA's credit system. */
if (cfg.usecredits != 0)
    {
    /* Execute once a minute. */
    if (curtime >= lastkertime + 60)
        {
        /* Deduct the configured number of credits. */
        tcred = (((curtime - lastkertime) / 60) * (long) abs(cfg.usecredits));
        /* The user is out of time if the value to be deducted exceeds
           the number of credits the user currently has. */
        if (tcred >= od_control.user_credit)
            {
            /* Evict the user. */
            od_control.user_credit = 0;
            top_output(OUT_SCREEN, getlang("OutOfCredits"));
            od_log_write(top_output(OUT_STRING, getlang("LogOutOfCredits")));
            od_exit(9, FALSE);
            }
        /* Decrease the user's credit count. */
        od_control.user_credit -= tcred;
        /* Update the last kernel execution time. */
        lastkertime = curtime;
        }
    }

/* Crash protection.  Only executed while the node is "logged in" to TOP,
   i.e. in the node index. */
if (cfg.crashprotdelay > 0 && isregistered)
    {
    /* Check every 10 seconds.  This is why there is a minimum of 10 on
       the setting for CrashProtDelay. */
    if ((curtime - node->lastaccess) >= (cfg.crashprotdelay - 10))
        {
        /* Keep this node alive by writing the current time to the node
           index. */
        node->lastaccess = time(NULL);
        save_node_data(od_control.od_node, node);

        /* Check each node to make sure it's still alive. */
        // Reading the entire index into a dynamic array of node_idx_typs
        // and then scanning should drastically help the speed here...
        for (kd = 0; kd < MAXNODES; kd++)
            {
            /* Only check if the node is active. */
            if (!get_node_data(kd, &tnode) && tnode.handle[0] &&
                kd != od_control.od_node)
                {
                /* The node is assumed to be dead if the at least
                   CrashProtDelay seconds have elapsed since the node
                   last checked in. */
                if ((curtime - tnode.lastaccess) >= cfg.crashprotdelay)
                    { // Err!!!!!!!!!!
                    /* Delete the node from the index. */
                    lseek(nidx2fil, kd, SEEK_SET);
                    rec_locking(REC_LOCK, nidx2fil, kd, 1);
                    tbyte = 0;
                    write(nidx2fil, &tbyte, 1);
                    rec_locking(REC_UNLOCK, nidx2fil, kd, 1);
                    memset(&tnode, 0, sizeof(node_idx_typ));
                    save_node_data(kd, &tnode);

                    /* Tell all other nodes that we just killed a node
                       with Crash Protection, which will cause the other
                       nodes on this channel to recognize the node's
                       deactivation. */
                    // Should maybe be sent globally...
                    itoa(kd, outnum[0], 10);
                    dispatch_message(MSG_KILLCRASH, outnum[0],
                                     -1, 0, 1);
                    od_log_write(top_output(OUT_STRING,
                                            getlang("LogFoundCrashed"),
                                            outnum[0]));
                    }
                }
            }
        }
    }

/* Check high-level censor warnings. */
if (cfg.censortimerhigh > 0 && censorwarningshigh > 0)
    {
    /* Deduct a warning if enough time has elapsed without an offence. */
    if (curtime - lastcensorwarnhigh > cfg.censortimerhigh)
        {
        censorwarningshigh--;
        lastcensorwarnhigh = time(NULL);
        }
    }
/* Check low-level censor warnings. */
if (cfg.censortimerlow > 0 && censorwarningslow > 0)
    {
    /* Deduct a warning if enough time has elapsed without an offence. */
    if (curtime - lastcensorwarnlow > cfg.censortimerlow)
        {
        censorwarningslow--;
        lastcensorwarnlow = time(NULL);
        }
    }

#ifdef __OS2__
/* Keep us alive if we're using Maximus MCP. */
if (cfg.bbstype == BBS_MAX3 && hpMCP != 0)
    {
    /* Send a ping every minute. */
    if (curtime % 60L == 0L)
        {
        McpSendMsg(hpMCP, PMSG_PING, NULL, 0);
        }
    }
#endif

}

/* sendtimemsgs() - Handles door-kit time warning messages.
   Parameters:  string - Message being sent from the door kit.
   Returns:  Nothing.
   Notes:  Called by the door kit when needed.
*/
void sendtimemsgs(char *instring)
    {
	char string[512];

    /* Strip any extra spaces. */
    trim_string(string, instring, 0);

    /* Time messages are blocked if TOP is in the process_messages()
       function.  Remember that since this function is called by the
       door kit it can be called when any od_ function is called. */
    if (blocktimemsgs)
        {
        /* Just display the message to the screen if blocking is on.  It
           may not look pretty but at least it will get through. */
        top_output(OUT_SCREEN, getlang("TimeMsgForcePrefix"), string);
        return;
        }

    /* Send the message like a normal TOP message, which looks much
       cleaner. */
    dispatch_message(MSG_GENERIC,
                     top_output(OUT_STRINGNF, getlang("TimeMsgPrefix"),
                                string),
                     od_control.od_node, 1, 0);

    }