comparison thrlib/totals.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 /* Distributed Checksum Clearinghouse
2 *
3 * count work
4 *
5 * Copyright (c) 2008 by Rhyolite Software, LLC
6 *
7 * This agreement is not applicable to any entity which sells anti-spam
8 * solutions to others or provides an anti-spam solution as part of a
9 * security solution sold to other entities, or to a private network
10 * which employs the DCC or uses data provided by operation of the DCC
11 * but does not provide corresponding data to other users.
12 *
13 * Permission to use, copy, modify, and distribute this software without
14 * changes for any purpose with or without fee is hereby granted, provided
15 * that the above copyright notice and this permission notice appear in all
16 * copies and any distributed versions or copies are either unchanged
17 * or not called anything similar to "DCC" or "Distributed Checksum
18 * Clearinghouse".
19 *
20 * Parties not eligible to receive a license under this agreement can
21 * obtain a commercial license to use DCC by contacting Rhyolite Software
22 * at sales@rhyolite.com.
23 *
24 * A commercial license would be for Distributed Checksum and Reputation
25 * Clearinghouse software. That software includes additional features. This
26 * free license for Distributed ChecksumClearinghouse Software does not in any
27 * way grant permision to use Distributed Checksum and Reputation Clearinghouse
28 * software
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
31 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
33 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
34 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
35 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
36 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
37 * SOFTWARE.
38 *
39 * Rhyolite Software DCC 1.3.103-1.19 $Revision$
40 */
41
42
43 #include "cmn_defs.h"
44 #include "helper.h"
45 #include <signal.h>
46
47
48 TOTALS totals;
49 static time_t signaled;
50 static pthread_t totals_tid;
51 static u_char stopping;
52
53 static void totals_msg_signal(int);
54 static void *totals_msg_thread(void *);
55
56
57
58 static void
59 totals_msg_signal(int sig UATTRIB)
60 {
61 time_t now;
62
63 signal(SIGUSR1, totals_msg_signal);
64
65 now = time(0);
66 /* ignore signals faster than 60 seconds */
67 if (!signaled || now > signaled+60) {
68 signaled = now;
69 totals.msg_next = signaled;
70 }
71 }
72
73
74
75 static void * NRATTRIB
76 totals_msg_thread(void *ign UATTRIB)
77 {
78 time_t now, next, last_clean;
79 struct tm tm, signaled_tm;
80 int secs;
81
82 clnt_sigs_off(0);
83
84 last_clean = time(0);
85 for (;;) {
86 if (stopping)
87 pthread_exit(0);
88
89 reap_helpers(0);
90
91 now = time(0);
92 next = totals.msg_next;
93 if (now >= next && next != 0) {
94 totals_msg();
95 next = 0;
96 }
97
98 if (!next) {
99 /* make a note to announce totals at midnight
100 * or 24 hours after the signal */
101 dcc_localtime(totals.msg_prev, &tm);
102 if (signaled != 0) {
103 dcc_localtime(signaled, &signaled_tm);
104 tm.tm_hour = signaled_tm.tm_hour;
105 tm.tm_min = signaled_tm.tm_min;
106 } else {
107 tm.tm_hour = 0;
108 tm.tm_min = 0;
109 }
110 tm.tm_sec = 0;
111 ++tm.tm_mday;
112 next = mktime(&tm);
113 if (next == -1) {
114 dcc_error_msg("mktime() failed");
115 next = now + 24*60*60;
116 }
117 totals.msg_next = next;
118
119 }
120
121 /* tell dccifd or dccm to close old sockets to recover
122 * from dictionary attacks */
123 if (last_clean > now || last_clean+5*60 < now) {
124 last_clean = now;
125 work_clean();
126 }
127
128 secs = next - now;
129 if (secs > 0) {
130 /* reap helpers regularly */
131 if (secs > HELPER_AUTO_REAP)
132 secs = HELPER_AUTO_REAP;
133 sleep(secs);
134 }
135 }
136 }
137
138
139
140 void
141 totals_init(void)
142 {
143 int i;
144
145 signal(SIGUSR1, totals_msg_signal);
146 totals.msg_prev = time(0);
147
148 i = pthread_create(&totals_tid, 0, totals_msg_thread, 0);
149 if (i)
150 dcc_logbad(EX_SOFTWARE, "pthread_create(totals msg): %s",
151 ERROR_STR1(i));
152 i = pthread_detach(totals_tid);
153 if (i)
154 dcc_error_msg("pthread_detach(totals msg): %s",
155 ERROR_STR1(i));
156 }
157
158
159
160 void
161 totals_stop(void)
162 {
163 stopping = 1;
164 totals_msg();
165 }
166
167
168
169 /* brag about our accomplishments */
170 void
171 totals_msg(void)
172 {
173 time_t now;
174 char tbuf[20];
175 char gbuf[80];
176 sigset_t sigsold;
177 int error;
178
179 clnt_sigs_off(&sigsold);
180
181 lock_work();
182 now = time(0);
183
184 if (grey_on)
185 snprintf(gbuf, sizeof(gbuf),
186 " greylist embargoed %d messages to %d targets;",
187 totals.msgs_embargoed, totals.tgts_embargoed);
188 else
189 gbuf[0] = '\0';
190
191 dcc_trace_msg(DCC_VERSION
192 "%s detected %d spam, ignored for %d, rejected for %d,"
193 " and discarded for %d targets among"
194 " %d total messages for %d targets since %s",
195 gbuf,
196 totals.msgs_spam, totals.tgts_ignored,
197 totals.tgts_rejected, totals.tgts_discarded,
198 totals.msgs, totals.tgts,
199 dcc_time2str(tbuf, sizeof(tbuf), "%x %X",
200 totals.msg_prev));
201
202 memset(&totals, 0, sizeof(totals));
203 totals.msg_prev = now;
204 unlock_work();
205
206 error = pthread_sigmask(SIG_SETMASK, &sigsold, 0);
207 if (error)
208 dcc_logbad(EX_SOFTWARE, "pthread_sigmask(totals): %s",
209 ERROR_STR1(error));
210 }