File: timer.c

package info (click to toggle)
dancer-services 1.8.0.6.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,600 kB
  • ctags: 1,623
  • sloc: ansic: 34,690; sh: 2,850; makefile: 269
file content (423 lines) | stat: -rw-r--r-- 9,620 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * HybServ TS Services, Copyright (C) 1998-1999 Patrick Alken
 * This program comes with absolutely NO WARRANTY
 *
 * Should you choose to use and/or modify this source code, please
 * do so under the terms of the GNU General Public License under which
 * this program is distributed.
 *
 * $Id: timer.c,v 1.3 2001/11/12 09:50:55 asuffield Exp $
 */

#include "defs.h"

#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#endif
#ifndef HAVE_CYGWIN
#include <signal.h>
#else
#include <sys/signal.h>
#endif /* HAVE_CYGWIN */

#include "chanserv.h"
#include "config.h"
#include "data.h"
#include "dcc.h"
#include "flood.h"
#include "gline.h"
#include "hybdefs.h"
#include "init.h"
#include "log.h"
#include "memoserv.h"
#include "nickserv.h"
#include "operserv.h"
#include "settings.h"
#include "sock.h"
#include "statserv.h"
#include "timer.h"
#include "jupe.h"

#ifdef HAVE_SOLARIS_THREADS
#include <thread.h>
#include <synch.h>
#else
#ifdef HAVE_PTHREADS
#include <pthread.h>
#endif
#endif

/* Global timer that will reduce local usage of time() -kre */
time_t current_ts;

#if defined HAVE_PTHREADS || defined HAVE_SOLARIS_THREADS

/*
 * 1 if ok to call DoTimer()
 */
int               GoodTimer = 1;

/*
 * p_CheckSignals()
 *
 * This thread will continually wait for various signals, and call
 * ProcessSignal() appropriately
*/

void *p_CheckSignals()
{
  sigset_t set;
  int caught;

  sigemptyset(&set);
/*  sigaddset(&set, SIGINT); */
  sigaddset(&set, SIGHUP);
  sigaddset(&set, SIGSEGV);
  sigaddset(&set, SIGBUS);
  sigaddset(&set, SIGTERM);
  sigaddset(&set, SIGCHLD);
  sigaddset(&set, SIGPIPE);

#ifdef HAVE_SOLARIS_THREADS
  thr_sigsetmask(SIG_BLOCK, &set, NULL);
#else
  pthread_sigmask(SIG_BLOCK, &set, NULL);
#endif

  while (1)
  {
#ifdef HAVE_SOLARIS_THREADS
    caught=sigwait(&set);
#else
    sigwait(&set, &caught); /* wait until we get a signal */
#endif
    ProcessSignal(caught); /* we got a signal, process it */
  }

  return (NULL);
} /* p_CheckSignals() */

/*
 * p_CheckTime()
 *
 * This function will be called from a new thread to enter an infinite
 * loop and call DoTimer() once every second
*/
void *p_CheckTime()
{
  current_ts = time(NULL);

  while (1)
  {
    sleep(1);
    if (GoodTimer)
      DoTimer(current_ts);

    /*
     * Every five minutes, reset current_ts to time() in case a DoTimer()
     * call took over a second, in which case current_ts will be a little
     * off
     */
    if ((current_ts % 300) == 0)
      current_ts = time(NULL);
    else
      current_ts++;
  }

  return (NULL);
} /* p_CheckTime() */

#endif /* HAVE_PTHREADS */

/*
 * DoTimer()
 *
 * Execute various commands every second - such as checking for expired
 * temp glines etc.
 */
void DoTimer(time_t unixtime)
{
#ifdef HIGHTRAFFIC_MODE

  if (!HTM)
  {
    float currload;

    /*
     * Calculate the current K/s the hub is sending us - if it
     * exceeds ReceiveLoad, switch to High Traffic Mode
     */
    currload = ((float) (Network->RecvB - Network->CheckRecvB) / 
                (float) 1024) / (float) HTM_INTERVAL;

    if (currload >= (float) ReceiveLoad)
    {
      HTM = 1;
      HTM_ts = unixtime;
      putlog(LOG1, "Entering high-traffic mode (%0.2f K/s)",
        currload);
      SendUmode(OPERUMODE_Y,
        "*** Entering high-traffic mode (%0.2f K/s)",
        currload);
      return;
    }
  }

  if ((unixtime % HTM_INTERVAL) == 0)
  {
    if (!Network->LastRecvB)
      Network->LastRecvB = Network->RecvB;
    else
    {
      Network->CheckRecvB = Network->LastRecvB;
      Network->LastRecvB = Network->RecvB;
    }
  }

  /*
   * While we are in high-traffic mode, do not execute any
   * of the regular timer functions
   */
  if (HTM)
    return;

#endif /* HIGHTRAFFIC_MODE */

  if ((HubSock == NOSOCKET) && ((unixtime % ConnectFrequency) == 0))
    CycleServers();

  if (BindAttemptFreq && ((unixtime % BindAttemptFreq) == 0))
    DoBinds();

  /* exec this every 5 seconds */
  if ((unixtime % 5) == 0)
  {
    /*
     * Check if any dcc sockets have recently EOF'd - if so,
     * terminate the connection
     */
    CheckEOF();

    /*
     * Check if any ident requests have exceeded IdentTimeout
     * seconds
     */
    ExpireIdent(unixtime);

    /*
     * Check if any telnet clients haven't entered their password
     * within TelnetTimeout seconds
     */
    ExpireTelnet(unixtime);

    /*
     * Check for expired ignores
     */
    ExpireIgnores(unixtime);
  } /* if ((unixtime % 5) == 0) */

  if ((unixtime % 60) == 0)
  {
  #ifdef ALLOW_GLINES

    /*
     * Check for expired temp. glines every 1 minute
     */
    ExpireGlines(unixtime);

  #endif

  #ifdef JUPEVOTES
    /* expire server jupe votes */
    ExpireVotes(unixtime);
  #endif

  #ifdef NICKSERVICES

   /*
    * Check if there are any nick stealers to kill
    */
    CollisionCheck(unixtime);

  #endif /* NICKSERVICES */

#if defined AUTO_ROUTING && defined SPLIT_INFO
    /* Check for splitted servers and optionally reconnect them -kre */
    ReconnectCheck(unixtime);
#endif

  } /* if ((unixtime % 60) == 0) */

#if !defined(HYBRID_ONLY) && defined(NICKSERVICES) && defined(CHANNELSERVICES)

  if (InhabitTimeout && ((unixtime % InhabitTimeout) == 0))
  {
    /*
     * Check for any empty channels that ChanServ is monitoring -
     * part the channel if ChanServ is the only one in there
     */
    CheckEmptyChans();
  }

#endif /* !defined(HYBRID_ONLY) && defined(NICKSERVICES) && defined(CHANNELSERVICES) */

  if ((unixtime % 3600) == 0)
  {
    /*
     * Check for expired nicknames, channels, and memos every hour
     */

  #ifdef NICKSERVICES

    ExpireNicknames(unixtime);

    #ifdef CHANNELSERVICES
      ExpireChannels(unixtime);
    #endif

    #ifdef MEMOSERVICES
      ExpireMemos(unixtime);
    #endif

  #endif /* NICKSERVICES */

  #ifdef STATSERVICES
    /*
     * Check for any HostHash entries which have had 0 clients
     * for the past StatExpire days
     */
    ExpireStats(unixtime);
  #endif

  } /* if ((unixtime % 3600) == 0) */

#if defined(NICKSERVICES) && defined(MEMOSERVICES)

  if (MemoPurgeFreq && ((unixtime % MemoPurgeFreq) == 0))
  {
    SendUmode(OPERUMODE_D,
      "%s: Purging memos marked for deletion",
      n_MemoServ);

    /*
     * Go through memo list and delete any marked for
     * deletion
     */
    PurgeCheck();
  }

#endif /* defined(NICKSERVICES) && defined(MEMOSERVICES) */

  if (AutoLinkFreq && (unixtime % AutoLinkFreq) == 0)
  {
    /*
     * Attempt to link all tcm bots who are not already linked
     */
    LinkBots();
  } /* if (AutoLinkFreq && (unixtime % AutoLinkFreq) == 0) */

  /*
   * we have to add gmt_offset here or it won't be true at
   * midnight unless we are in the GMT timezone - for instance,
   * if we are in the EST timezone, we would be 5 hours earlier
   * than GMT, and thus ((unixtime % 86400) == 0) would only
   * be true at 19:00, so if we add gmt_offset (which would
   * be negative) to unixtime (at midnight), the value would
   * be the TS of 19:00, which would return true
   */
  if (((unixtime + gmt_offset) % 86400) == 0)
  {
  #ifdef STATSERVICES
    /*
     * every day at midnight reset the daily max
     * user/oper/server/channel count
     */
    Network->MaxUsersT = Network->TotalUsers;
    Network->MaxOperatorsT = Network->TotalOperators;
    Network->MaxServersT = Network->TotalServers;
    Network->MaxChannelsT = Network->TotalChannels;

    Network->MaxUsersT_ts = unixtime;
    Network->MaxOperatorsT_ts = unixtime;
    Network->MaxServersT_ts = unixtime;
    Network->MaxChannelsT_ts = unixtime;

    Network->OperKillsT = 0;
    Network->ServKillsT = 0;

    putlog(LOG2, "%s: Reset daily counts",
      n_StatServ);
  #endif /* STATSERVICES */

    /*
     * every day at midnight backup the log file
     * to the format: logfile.YYYYMMDD and start
     * a new one.
     */
    CheckLogs(unixtime);
  } /* if (((unixtime + gmt_offset) % 86400) == 0) */

  if (BackupFreq && (((unixtime + gmt_offset) % BackupFreq) == 0))
  {
    /*
     * Backup database files
     */
    SendUmode(OPERUMODE_Y,
      "*** Backing up databases");

    BackupDatabases(unixtime);
  }

  if ((unixtime % DatabaseSync) == 0)
  {
    SendUmode(OPERUMODE_D,
      "*** Saving databases");

    WriteDatabases();
  } /* if ((unixtime % DatabaseSync) == 0) */

  /*
   * If SendUmode() detected a "function-calling" flood,
   * check if 20 seconds have passed; if so, remove flood
   * protection
   */
  if (ActiveFlood && ((unixtime - ActiveFlood) % 20 == 0))
  {
    ActiveFlood = 0;
    SendUmode(OPERUMODE_Y,
      "*** Flood time limit expired, broadcasting resumed");
  }

#ifdef STATSERVICES

  /*
   * Every minute ping all servers to update lag information
   */
  if (LagDetect && ((unixtime % 60) == 0))
  {
    DoPings();
  }

#endif /* STATSERVICES */

  if (!SafeConnect && currenthub)
  {
    /*
     * currenthub->connect_ts + ConnectBurst is the TS
     * of when we can assume we are safely connected to
     * the network - that is, there is no more initial
     * connect burst information coming. So, we can now
     * broadcast clone/client connections to +y users
     * without fearing they will get flooded with hundreds
     * of them from an initial burst.
     * Make sure currenthub->realname is not null, so we
     * can tell if we are actually connected to a hub :-)
     */
    if (currenthub->realname &&
        (unixtime >= (currenthub->connect_ts + ConnectBurst)))
      SafeConnect = 1;
  } /* if (!SafeConnect && currenthub) */
} /* DoTimer() */