Coverage Report

Created: 2025-06-22 06:46

/src/hpn-ssh/log.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: log.c,v 1.64 2024/12/07 10:05:36 djm Exp $ */
2
/*
3
 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4
 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5
 *                    All rights reserved
6
 *
7
 * As far as I am concerned, the code I have written for this software
8
 * can be used freely for any purpose.  Any derived versions of this
9
 * software must be clearly marked as such, and if the derived work is
10
 * incompatible with the protocol description in the RFC file, it must be
11
 * called by a name other than "ssh" or "Secure Shell".
12
 */
13
/*
14
 * Copyright (c) 2000 Markus Friedl.  All rights reserved.
15
 *
16
 * Redistribution and use in source and binary forms, with or without
17
 * modification, are permitted provided that the following conditions
18
 * are met:
19
 * 1. Redistributions of source code must retain the above copyright
20
 *    notice, this list of conditions and the following disclaimer.
21
 * 2. Redistributions in binary form must reproduce the above copyright
22
 *    notice, this list of conditions and the following disclaimer in the
23
 *    documentation and/or other materials provided with the distribution.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
 */
36
37
#include "includes.h"
38
39
#include <sys/types.h>
40
41
#include <errno.h>
42
#include <fcntl.h>
43
#include <limits.h>
44
#include <stdarg.h>
45
#include <stdio.h>
46
#include <stdlib.h>
47
#include <string.h>
48
#include <syslog.h>
49
#include <time.h>
50
#include <unistd.h>
51
#include <errno.h>
52
#include "packet.h" /* needed for host and port look ups */
53
#ifdef HAVE_SYS_TIME_H
54
# include <sys/time.h> /* to get current time */
55
#endif
56
#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
57
# include <vis.h>
58
#endif
59
60
#include "log.h"
61
#include "match.h"
62
63
static LogLevel log_level = SYSLOG_LEVEL_INFO;
64
static int log_on_stderr = 1;
65
static int log_stderr_fd = STDERR_FILENO;
66
static int log_facility = LOG_AUTH;
67
static const char *argv0;
68
static log_handler_fn *log_handler;
69
static void *log_handler_ctx;
70
static char **log_verbose;
71
static size_t nlog_verbose;
72
73
extern char *__progname;
74
75
extern struct ssh *active_state;
76
77
0
#define LOG_SYSLOG_VIS  (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
78
1.06M
#define LOG_STDERR_VIS  (VIS_SAFE|VIS_OCTAL)
79
80
/* textual representation of log-facilities/levels */
81
82
static struct {
83
  const char *name;
84
  SyslogFacility val;
85
} log_facilities[] = {
86
  { "DAEMON", SYSLOG_FACILITY_DAEMON },
87
  { "USER", SYSLOG_FACILITY_USER },
88
  { "AUTH", SYSLOG_FACILITY_AUTH },
89
#ifdef LOG_AUTHPRIV
90
  { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV },
91
#endif
92
  { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
93
  { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
94
  { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
95
  { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
96
  { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
97
  { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
98
  { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
99
  { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
100
  { NULL,   SYSLOG_FACILITY_NOT_SET }
101
};
102
103
static struct {
104
  const char *name;
105
  LogLevel val;
106
} log_levels[] =
107
{
108
  { "QUIET",  SYSLOG_LEVEL_QUIET },
109
  { "FATAL",  SYSLOG_LEVEL_FATAL },
110
  { "ERROR",  SYSLOG_LEVEL_ERROR },
111
  { "INFO", SYSLOG_LEVEL_INFO },
112
  { "VERBOSE",  SYSLOG_LEVEL_VERBOSE },
113
  { "DEBUG",  SYSLOG_LEVEL_DEBUG1 },
114
  { "DEBUG1", SYSLOG_LEVEL_DEBUG1 },
115
  { "DEBUG2", SYSLOG_LEVEL_DEBUG2 },
116
  { "DEBUG3", SYSLOG_LEVEL_DEBUG3 },
117
  { NULL,   SYSLOG_LEVEL_NOT_SET }
118
};
119
120
LogLevel
121
log_level_get(void)
122
0
{
123
0
  return log_level;
124
0
}
125
126
SyslogFacility
127
log_facility_number(char *name)
128
0
{
129
0
  int i;
130
131
0
  if (name != NULL)
132
0
    for (i = 0; log_facilities[i].name; i++)
133
0
      if (strcasecmp(log_facilities[i].name, name) == 0)
134
0
        return log_facilities[i].val;
135
0
  return SYSLOG_FACILITY_NOT_SET;
136
0
}
137
138
const char *
139
log_facility_name(SyslogFacility facility)
140
0
{
141
0
  u_int i;
142
143
0
  for (i = 0;  log_facilities[i].name; i++)
144
0
    if (log_facilities[i].val == facility)
145
0
      return log_facilities[i].name;
146
0
  return NULL;
147
0
}
148
149
LogLevel
150
log_level_number(char *name)
151
0
{
152
0
  int i;
153
154
0
  if (name != NULL)
155
0
    for (i = 0; log_levels[i].name; i++)
156
0
      if (strcasecmp(log_levels[i].name, name) == 0)
157
0
        return log_levels[i].val;
158
0
  return SYSLOG_LEVEL_NOT_SET;
159
0
}
160
161
const char *
162
log_level_name(LogLevel level)
163
0
{
164
0
  u_int i;
165
166
0
  for (i = 0; log_levels[i].name != NULL; i++)
167
0
    if (log_levels[i].val == level)
168
0
      return log_levels[i].name;
169
0
  return NULL;
170
0
}
171
172
void
173
log_verbose_add(const char *s)
174
0
{
175
0
  char **tmp;
176
177
  /* Ignore failures here */
178
0
  if ((tmp = recallocarray(log_verbose, nlog_verbose, nlog_verbose + 1,
179
0
      sizeof(*log_verbose))) != NULL) {
180
0
    log_verbose = tmp;
181
0
    if ((log_verbose[nlog_verbose] = strdup(s)) != NULL)
182
0
      nlog_verbose++;
183
0
  }
184
0
}
185
186
void
187
log_verbose_reset(void)
188
0
{
189
0
  size_t i;
190
191
0
  for (i = 0; i < nlog_verbose; i++)
192
0
    free(log_verbose[i]);
193
0
  free(log_verbose);
194
0
  log_verbose = NULL;
195
0
  nlog_verbose = 0;
196
0
}
197
198
/*
199
 * Initialize the log.
200
 */
201
202
void
203
log_init(const char *av0, LogLevel level, SyslogFacility facility,
204
    int on_stderr)
205
1
{
206
#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
207
  struct syslog_data sdata = SYSLOG_DATA_INIT;
208
#endif
209
210
1
  argv0 = av0;
211
212
1
  if (log_change_level(level) != 0) {
213
0
    fprintf(stderr, "Unrecognized internal syslog level code %d\n",
214
0
        (int) level);
215
0
    exit(1);
216
0
  }
217
218
1
  log_handler = NULL;
219
1
  log_handler_ctx = NULL;
220
221
1
  log_on_stderr = on_stderr;
222
1
  if (on_stderr)
223
1
    return;
224
225
0
  switch (facility) {
226
0
  case SYSLOG_FACILITY_DAEMON:
227
0
    log_facility = LOG_DAEMON;
228
0
    break;
229
0
  case SYSLOG_FACILITY_USER:
230
0
    log_facility = LOG_USER;
231
0
    break;
232
0
  case SYSLOG_FACILITY_AUTH:
233
0
    log_facility = LOG_AUTH;
234
0
    break;
235
0
#ifdef LOG_AUTHPRIV
236
0
  case SYSLOG_FACILITY_AUTHPRIV:
237
0
    log_facility = LOG_AUTHPRIV;
238
0
    break;
239
0
#endif
240
0
  case SYSLOG_FACILITY_LOCAL0:
241
0
    log_facility = LOG_LOCAL0;
242
0
    break;
243
0
  case SYSLOG_FACILITY_LOCAL1:
244
0
    log_facility = LOG_LOCAL1;
245
0
    break;
246
0
  case SYSLOG_FACILITY_LOCAL2:
247
0
    log_facility = LOG_LOCAL2;
248
0
    break;
249
0
  case SYSLOG_FACILITY_LOCAL3:
250
0
    log_facility = LOG_LOCAL3;
251
0
    break;
252
0
  case SYSLOG_FACILITY_LOCAL4:
253
0
    log_facility = LOG_LOCAL4;
254
0
    break;
255
0
  case SYSLOG_FACILITY_LOCAL5:
256
0
    log_facility = LOG_LOCAL5;
257
0
    break;
258
0
  case SYSLOG_FACILITY_LOCAL6:
259
0
    log_facility = LOG_LOCAL6;
260
0
    break;
261
0
  case SYSLOG_FACILITY_LOCAL7:
262
0
    log_facility = LOG_LOCAL7;
263
0
    break;
264
0
  default:
265
0
    fprintf(stderr,
266
0
        "Unrecognized internal syslog facility code %d\n",
267
0
        (int) facility);
268
0
    exit(1);
269
0
  }
270
271
  /*
272
   * If an external library (eg libwrap) attempts to use syslog
273
   * immediately after reexec, syslog may be pointing to the wrong
274
   * facility, so we force an open/close of syslog here.
275
   */
276
#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
277
  openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
278
  closelog_r(&sdata);
279
#else
280
0
  openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
281
0
  closelog();
282
0
#endif
283
0
}
284
285
int
286
log_change_level(LogLevel new_log_level)
287
1
{
288
  /* no-op if log_init has not been called */
289
1
  if (argv0 == NULL)
290
0
    return 0;
291
292
1
  switch (new_log_level) {
293
0
  case SYSLOG_LEVEL_QUIET:
294
0
  case SYSLOG_LEVEL_FATAL:
295
0
  case SYSLOG_LEVEL_ERROR:
296
1
  case SYSLOG_LEVEL_INFO:
297
1
  case SYSLOG_LEVEL_VERBOSE:
298
1
  case SYSLOG_LEVEL_DEBUG1:
299
1
  case SYSLOG_LEVEL_DEBUG2:
300
1
  case SYSLOG_LEVEL_DEBUG3:
301
1
    log_level = new_log_level;
302
1
    return 0;
303
0
  default:
304
0
    return -1;
305
1
  }
306
1
}
307
308
int
309
log_is_on_stderr(void)
310
0
{
311
0
  return log_on_stderr && log_stderr_fd == STDERR_FILENO;
312
0
}
313
314
/* redirect what would usually get written to stderr to specified file */
315
void
316
log_redirect_stderr_to(const char *logfile)
317
0
{
318
0
  int fd;
319
320
0
  if (logfile == NULL) {
321
0
    if (log_stderr_fd != STDERR_FILENO) {
322
0
      close(log_stderr_fd);
323
0
      log_stderr_fd = STDERR_FILENO;
324
0
    }
325
0
    return;
326
0
  }
327
328
0
  if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600)) == -1) {
329
0
    fprintf(stderr, "Couldn't open logfile %s: %s\n", logfile,
330
0
        strerror(errno));
331
0
    exit(1);
332
0
  }
333
0
  log_stderr_fd = fd;
334
0
}
335
336
#define MSGBUFSIZ 1024
337
338
void
339
set_log_handler(log_handler_fn *handler, void *ctx)
340
0
{
341
0
  log_handler = handler;
342
0
  log_handler_ctx = ctx;
343
0
}
344
345
static void
346
do_log(LogLevel level, int force, const char *suffix, const char *fmt,
347
    va_list args)
348
1.06M
{
349
#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
350
  struct syslog_data sdata = SYSLOG_DATA_INIT;
351
#endif
352
1.06M
  char msgbuf[MSGBUFSIZ];
353
1.06M
  char fmtbuf[MSGBUFSIZ];
354
1.06M
  char *txt = NULL;
355
1.06M
  int pri = LOG_INFO;
356
1.06M
  int saved_errno = errno;
357
1.06M
  log_handler_fn *tmp_handler;
358
1.06M
  const char *progname = argv0 != NULL ? argv0 : __progname;
359
360
1.06M
  if (!force && level > log_level)
361
0
    return;
362
363
1.06M
  switch (level) {
364
0
  case SYSLOG_LEVEL_FATAL:
365
0
    if (!log_on_stderr)
366
0
      txt = "fatal";
367
0
    pri = LOG_CRIT;
368
0
    break;
369
1.06M
  case SYSLOG_LEVEL_ERROR:
370
1.06M
    if (!log_on_stderr)
371
0
      txt = "error";
372
1.06M
    pri = LOG_ERR;
373
1.06M
    break;
374
7.81k
  case SYSLOG_LEVEL_INFO:
375
7.81k
    pri = LOG_INFO;
376
7.81k
    break;
377
0
  case SYSLOG_LEVEL_VERBOSE:
378
0
    pri = LOG_INFO;
379
0
    break;
380
0
  case SYSLOG_LEVEL_DEBUG1:
381
0
    txt = "debug1";
382
0
    pri = LOG_DEBUG;
383
0
    break;
384
0
  case SYSLOG_LEVEL_DEBUG2:
385
0
    txt = "debug2";
386
0
    pri = LOG_DEBUG;
387
0
    break;
388
0
  case SYSLOG_LEVEL_DEBUG3:
389
0
    txt = "debug3";
390
0
    pri = LOG_DEBUG;
391
0
    break;
392
0
  default:
393
0
    txt = "internal error";
394
0
    pri = LOG_ERR;
395
0
    break;
396
1.06M
  }
397
1.06M
  if (txt != NULL && log_handler == NULL) {
398
0
    snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
399
0
    vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
400
1.06M
  } else {
401
1.06M
    vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
402
1.06M
  }
403
1.06M
  if (suffix != NULL) {
404
33.3k
    snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", msgbuf, suffix);
405
33.3k
    strlcpy(msgbuf, fmtbuf, sizeof(msgbuf));
406
33.3k
  }
407
1.06M
  strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
408
1.06M
      log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
409
1.06M
  if (log_handler != NULL) {
410
    /* Avoid recursion */
411
0
    tmp_handler = log_handler;
412
0
    log_handler = NULL;
413
0
    tmp_handler(level, force, fmtbuf, log_handler_ctx);
414
0
    log_handler = tmp_handler;
415
1.06M
  } else if (log_on_stderr) {
416
1.06M
    snprintf(msgbuf, sizeof msgbuf, "%s%s%.*s\r\n",
417
1.06M
        (log_on_stderr > 1) ? progname : "",
418
1.06M
        (log_on_stderr > 1) ? ": " : "",
419
1.06M
        (int)sizeof msgbuf - 3, fmtbuf);
420
1.06M
    (void)write(log_stderr_fd, msgbuf, strlen(msgbuf));
421
1.06M
  } else {
422
#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
423
    openlog_r(progname, LOG_PID, log_facility, &sdata);
424
    syslog_r(pri, &sdata, "%.500s", fmtbuf);
425
    closelog_r(&sdata);
426
#else
427
0
    openlog(progname, LOG_PID, log_facility);
428
0
    syslog(pri, "%.500s", fmtbuf);
429
0
    closelog();
430
0
#endif
431
0
  }
432
1.06M
  errno = saved_errno;
433
1.06M
}
434
435
void
436
sshlog(const char *file, const char *func, int line, int showfunc,
437
    LogLevel level, const char *suffix, const char *fmt, ...)
438
5.43M
{
439
5.43M
  va_list args;
440
441
5.43M
  va_start(args, fmt);
442
5.43M
  sshlogv(file, func, line, showfunc, level, suffix, fmt, args);
443
5.43M
  va_end(args);
444
5.43M
}
445
446
void
447
sshlogdie(const char *file, const char *func, int line, int showfunc,
448
    LogLevel level, const char *suffix, const char *fmt, ...)
449
0
{
450
0
  va_list args;
451
452
0
  va_start(args, fmt);
453
0
  sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_INFO,
454
0
      suffix, fmt, args);
455
0
  va_end(args);
456
0
  cleanup_exit(255);
457
0
}
458
459
void
460
sshlogv(const char *file, const char *func, int line, int showfunc,
461
    LogLevel level, const char *suffix, const char *fmt, va_list args)
462
5.43M
{
463
5.43M
  char tag[128], fmt2[MSGBUFSIZ + 128];
464
5.43M
  int forced = 0;
465
5.43M
  const char *cp;
466
5.43M
  size_t i;
467
468
  /* short circuit processing early if we're not going to log anything */
469
5.43M
  if (nlog_verbose == 0 && level > log_level)
470
4.36M
    return;
471
472
1.06M
  snprintf(tag, sizeof(tag), "%.48s:%.48s():%d (bin=%s, pid=%ld)",
473
1.06M
      (cp = strrchr(file, '/')) == NULL ? file : cp + 1, func, line,
474
1.06M
      argv0 == NULL ? "UNKNOWN" : argv0, (long)getpid());
475
1.06M
  for (i = 0; i < nlog_verbose; i++) {
476
0
    if (match_pattern_list(tag, log_verbose[i], 0) == 1) {
477
0
      forced = 1;
478
0
      break;
479
0
    }
480
0
  }
481
482
1.06M
  if (forced)
483
0
    snprintf(fmt2, sizeof(fmt2), "%s: %s", tag, fmt);
484
1.06M
  else if (showfunc)
485
1.06M
    snprintf(fmt2, sizeof(fmt2), "%s: %s", func, fmt);
486
7.81k
  else
487
7.81k
    strlcpy(fmt2, fmt, sizeof(fmt2));
488
489
1.06M
  do_log(level, forced, suffix, fmt2, args);
490
1.06M
}
491
492
void
493
sshlogdirect(LogLevel level, int forced, const char *fmt, ...)
494
0
{
495
0
  va_list args;
496
497
0
  va_start(args, fmt);
498
0
  do_log(level, forced, NULL, fmt, args);
499
0
  va_end(args);
500
0
}
501
502
503
/*
504
 * A simple system for ratelimiting aperiodic events such as logs, without
505
 * needing to be hooked into a mainloop/timer. A running total of events is
506
 * maintained and when it exceeds a threshold further events are dropped
507
 * until the rate falls back below that threshold.
508
 *
509
 * To prevent flipping in and out of rate-limiting, there is a hysteresis
510
 * timer that delays leaving the rate-limited state.
511
 *
512
 * While in the rate-limited state, events can be periodically allowed though
513
 * and the number of dropped events since the last log obtained.
514
 *
515
 * XXX a moving average rate of events might be a better approach here rather
516
 *     than linear decay, which can suppress events for a while after large
517
 *     bursts.
518
 */
519
520
/* #define RATELIMIT_DEBUG 1 */
521
522
#ifdef RATELIMIT_DEBUG
523
# define RLDBG(x) do { \
524
    printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
525
    printf x; \
526
    printf("\n"); \
527
    fflush(stdout); \
528
  } while (0)
529
#else
530
# define RLDBG(x)
531
#endif
532
533
/* set up a ratelimit */
534
void
535
log_ratelimit_init(struct log_ratelimit_ctx *rl, u_int threshold,
536
    u_int max_accum, u_int hysteresis, u_int log_every)
537
0
{
538
0
  memset(rl, 0, sizeof(*rl));
539
0
  rl->threshold = threshold;
540
0
  rl->max_accum = max_accum;
541
0
  rl->hysteresis = hysteresis;
542
0
  rl->log_every = log_every;
543
0
  RLDBG(("called: rl=%p thresh=%u max=%u hys=%u log_every=%u",
544
0
      rl, rl->threshold, rl->max_accum, rl->hysteresis, rl->log_every));
545
0
}
546
547
/*
548
 * check whether a log event should be dropped because of rate-limiting.
549
 * returns non-zero if the event should be dropped. If events_since_last
550
 * is supplied then, for periodic logs, it will be set to the number of
551
 * dropped events since the last message.
552
 */
553
int
554
log_ratelimit(struct log_ratelimit_ctx *rl, time_t now, int *active,
555
    u_int *events_dropped)
556
0
{
557
0
  time_t olast_event;
558
559
0
  RLDBG(("called: rl=%p thresh=%u max=%u hys=%u log_every=%u "
560
0
      "accum=%u since=%ld since_last=%u", rl, rl->threshold,
561
0
      rl->max_accum, rl->hysteresis,
562
0
      rl->log_every, rl->accumulated_events,
563
0
      rl->last_event == 0 ? -1 : (long)(now - rl->last_event),
564
0
      rl->ratelimited_events));
565
566
0
  if (now < 0)
567
0
    return 0;
568
0
  if (events_dropped != NULL)
569
0
    *events_dropped = 0;
570
0
  if (active != NULL)
571
0
    *active = rl->ratelimit_active;
572
573
  /* First, decay accumulated events */
574
0
  if (rl->last_event <= 0)
575
0
    rl->last_event = now;
576
0
  if (now > rl->last_event) {
577
0
    uint64_t n = now - rl->last_event;
578
579
0
    if (n > UINT_MAX)
580
0
      n = UINT_MAX;
581
0
    if (rl->accumulated_events < (u_int)n)
582
0
      rl->accumulated_events = 0;
583
0
    else
584
0
      rl->accumulated_events -= (u_int)n;
585
0
    RLDBG(("decay: accum=%u", rl->accumulated_events));
586
0
  }
587
0
  rl->accumulated_events++; /* add this event */
588
0
  if (rl->accumulated_events > rl->max_accum)
589
0
    rl->accumulated_events = rl->max_accum;
590
0
  olast_event = rl->last_event;
591
0
  rl->last_event = now;
592
0
  RLDBG(("check threshold: accum=%u vs thresh=%u",
593
0
      rl->accumulated_events, rl->threshold));
594
595
  /* Are we under threshold? */
596
0
  if (rl->accumulated_events < rl->threshold) {
597
0
    if (!rl->ratelimit_active)
598
0
      return 0;
599
0
    RLDBG(("under threshold: hys=%u since_hys=%ld since_last=%ld",
600
0
        rl->hysteresis, rl->hysteresis_start == 0 ? -1 :
601
0
        (long)(now - rl->hysteresis_start),
602
0
        olast_event == 0 ? -1 : (long)(now - olast_event)));
603
0
    if (rl->hysteresis_start == 0) {
604
      /* active, but under threshold; hysteresis */
605
0
      if (olast_event + rl->hysteresis < now) {
606
        /* hysteresis expired before this event */
607
0
        RLDBG(("hysteresis preexpired"));
608
0
        goto inactive;
609
0
      }
610
0
      RLDBG(("start hysteresis"));
611
0
      rl->hysteresis_start = now;
612
0
    } else if (rl->hysteresis_start + rl->hysteresis < now) {
613
      /* Hysteresis period expired, transition to inactive */
614
0
      RLDBG(("complete hysteresis"));
615
0
 inactive:
616
0
      if (events_dropped != NULL)
617
0
        *events_dropped = rl->ratelimited_events;
618
0
      if (active != NULL)
619
0
        *active = 0;
620
0
      rl->ratelimit_active = 0;
621
0
      rl->ratelimit_start = 0;
622
0
      rl->last_log = 0;
623
0
      rl->hysteresis_start = 0;
624
0
      rl->ratelimited_events = 0;
625
0
      return 0;
626
0
    }
627
    /* ratelimiting active, but in hysteresis period */
628
0
  } else if (!rl->ratelimit_active) {
629
    /* Transition to rate-limiting */
630
0
    RLDBG(("start ratelimit"));
631
0
    rl->ratelimit_active = 1;
632
0
    rl->ratelimit_start = now;
633
0
    rl->last_log = now;
634
0
    rl->hysteresis_start = 0;
635
0
    rl->ratelimited_events = 1;
636
0
    if (active != NULL)
637
0
      *active = 1;
638
0
    return 1;
639
0
  } else if (rl->hysteresis_start != 0) {
640
    /* active and over threshold; reset hysteresis timer */
641
0
    RLDBG(("clear hysteresis"));
642
0
    rl->hysteresis_start = 0;
643
0
  }
644
645
  /* over threshold or in hysteresis period; log periodically */
646
0
  if (active != NULL)
647
0
    *active = 1;
648
0
  RLDBG(("log_every=%u since_log=%ld", rl->log_every,
649
0
      (long)(now - rl->last_log)));
650
0
  if (rl->log_every > 0 && now >= rl->last_log + rl->log_every) {
651
0
    RLDBG(("periodic: since_last=%u", rl->ratelimited_events));
652
0
    rl->last_log = now;
653
0
    if (events_dropped != NULL) {
654
0
      *events_dropped = rl->ratelimited_events;
655
0
      rl->ratelimited_events = 0;
656
0
    }
657
0
    return 0;
658
0
  }
659
660
  /* drop event */
661
0
  rl->ratelimited_events++;
662
0
  RLDBG(("drop: ratelimited_events=%u", rl->ratelimited_events));
663
0
  return 1;
664
0
}