1.\" $OpenBSD: ratecheck.9,v 1.5 2001/08/03 15:21:17 mpech Exp $ 2.\" $NetBSD: ratecheck.9,v 1.1.2.1 2000/02/18 20:26:43 he Exp $ 3.\" 4.\" Copyright (c) 2000 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by Christopher G. Demetriou. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. All advertising materials mentioning features or use of this software 19.\" must display the following acknowledgement: 20.\" This product includes software developed by the NetBSD 21.\" Foundation, Inc. and its contributors. 22.\" 4. Neither the name of The NetBSD Foundation nor the names of its 23.\" contributors may be used to endorse or promote products derived 24.\" from this software without specific prior written permission. 25.\" 26.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36.\" POSSIBILITY OF SUCH DAMAGE. 37.\" 38.Dd February 2, 2000 39.Dt RATECHECK 9 40.Os 41.Sh NAME 42.Nm ratecheck 43.Nd function to help implement rate-limited actions 44.Sh SYNOPSIS 45.Fd #include <sys/time.h> 46.Ft int 47.Fn ratecheck "struct timeval *lasttime" "const struct timeval *mininterval" 48.Sh DESCRIPTION 49The 50.Fn ratecheck 51function provides a simple time interval check which can be used 52when implementing time-based rate-limited actions. 53If the difference between the current monotonically-increasing 54system time 55.Pq Va mono_time 56and 57.Fa lasttime 58is less than the value given by the 59.Fa mininterval 60argument, zero is returned. 61Otherwise, 62.Fa lasttime 63is set to the current time and a non-zero value is returned. 64.Pp 65The motivation for implementing 66.Fn ratecheck 67was to provide a mechanism that could be used to add rate limiting to 68diagnostic message output. 69If printed too often, diagnostic messages can 70keep the system from doing useful work. 71If the repeated messages can be caused by deliberate user action or 72network events, they can be exploited to cause denial of system service. 73.Pp 74Note that using a very short time interval (less than a second) 75for 76.Fa mininterval 77defeats the purpose of this function. 78(It doesn't 79take much to flood a 9600 baud serial console with output, for instance.) 80.Sh EXAMPLES 81Here is a simple example of use of the 82.Fn ratecheck 83function: 84.Bd -literal 85/* 86 * The following variables could be global, in a device softc, etc., 87 * depending on the exact usage. 88 */ 89struct timeval drv_lasterr1time; /* time of last err1 message */ 90long drv_err1count; /* # of err1 errs since last msg */ 91struct timeval drv_lasterr2time; /* time of last err2 message */ 92long drv_err2count; /* # of err2 errs since last msg */ 93 94/* 95 * The following variable will often be global or shared by all 96 * instances of a driver. It should be initialized, so it can be 97 * patched. Allowing it to be set via an option might be nice, 98 * but could lead to an insane proliferation of options. 99 */ 100struct timeval drv_errintvl = { 5, 0 }; /* 5 seconds */ 101 102/* error handling/reporting function */ 103void 104drv_errhandler(int err1, int err2) 105{ 106 107 /* 108 * Note that you should NOT use the same last-event 109 * time variable for dissimilar messages! 110 */ 111 if (err1) { 112 /* handle err1 condition */ 113 ... 114 115 drv_err1count++; 116 if (ratecheck(&drv_lasterr1notice, 117 &drv_errinterval)) { 118 printf("drv: %ld err1 errors occurred", 119 drv_err1count); 120 drv_err1count = 0; 121 } 122 } 123 if (err2) { 124 /* handle err2 condition */ 125 ... 126 127 drv_err2count++; 128 if (ratecheck(&drv_lasterr2notice, 129 &drv_errinterval)) { 130 printf("drv: %ld err2 errors occurred", 131 drv_err2count); 132 drv_err2count = 0; 133 } 134 } 135} 136.Ed 137.Sh SEE ALSO 138.Xr log 9 , 139.Xr printf 9 , 140.Xr time 9 141.Sh HISTORY 142The 143.Fn ratecheck 144function first appeared in 145.Nx 1.5 . 146