00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "dhcpd.h"
00030
00031 #include <sys/time.h>
00032
00033 struct timeout *timeouts;
00034 static struct timeout *free_timeouts;
00035
00036 void set_time(TIME t)
00037 {
00038
00039 if (cur_tv . tv_sec != t) {
00040 cur_tv . tv_sec = t;
00041 cur_tv . tv_usec = 0;
00042 process_outstanding_timeouts ((struct timeval *)0);
00043 }
00044 }
00045
00046 struct timeval *process_outstanding_timeouts (struct timeval *tvp)
00047 {
00048
00049
00050
00051 another:
00052 if (timeouts) {
00053 struct timeout *t;
00054 if ((timeouts -> when . tv_sec < cur_tv . tv_sec) ||
00055 ((timeouts -> when . tv_sec == cur_tv . tv_sec) &&
00056 (timeouts -> when . tv_usec <= cur_tv . tv_usec))) {
00057 t = timeouts;
00058 timeouts = timeouts -> next;
00059 (*(t -> func)) (t -> what);
00060 if (t -> unref)
00061 (*t -> unref) (&t -> what, MDL);
00062 t -> next = free_timeouts;
00063 free_timeouts = t;
00064 goto another;
00065 }
00066 if (tvp) {
00067 tvp -> tv_sec = timeouts -> when . tv_sec;
00068 tvp -> tv_usec = timeouts -> when . tv_usec;
00069 }
00070 return tvp;
00071 } else
00072 return (struct timeval *)0;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 void
00109 dispatch(void)
00110 {
00111 isc_result_t status;
00112
00113 do {
00114 status = isc_app_ctxrun(dhcp_gbl_ctx.actx);
00115
00116
00117
00118
00119
00120
00121
00122 if (status == ISC_R_RELOAD) {
00123
00124
00125
00126
00127 status = dhcp_set_control_state(server_shutdown,
00128 server_shutdown);
00129 if (status == ISC_R_SUCCESS)
00130 status = ISC_R_RELOAD;
00131 }
00132 } while (status == ISC_R_RELOAD);
00133
00134 log_fatal ("Dispatch routine failed: %s -- exiting",
00135 isc_result_totext (status));
00136 }
00137
00138 void
00139 isclib_timer_callback(isc_task_t *taskp,
00140 isc_event_t *eventp)
00141 {
00142 struct timeout *t = (struct timeout *)eventp->ev_arg;
00143 struct timeout *q, *r;
00144
00145
00146 gettimeofday (&cur_tv, (struct timezone *)0);
00147
00148
00149
00150
00151
00152
00153 r = NULL;
00154 for (q = timeouts; q; q = q->next) {
00155 if (q == t) {
00156 if (r)
00157 r->next = q->next;
00158 else
00159 timeouts = q->next;
00160 break;
00161 }
00162 r = q;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172 if (q != NULL) {
00173
00174 (*(q->func)) (q->what);
00175 if (q->unref) {
00176 (*q->unref) (&q->what, MDL);
00177 }
00178 q->next = free_timeouts;
00179 isc_timer_detach(&q->isc_timeout);
00180 free_timeouts = q;
00181 } else {
00182
00183
00184
00185
00186
00187 log_error("Error finding timer structure");
00188 }
00189
00190 isc_event_free(&eventp);
00191 return;
00192 }
00193
00194
00195 #define USEC_MAX 1000000
00196 #define DHCP_SEC_MAX 0xFFFFFFFF
00197
00198 void add_timeout (when, where, what, ref, unref)
00199 struct timeval *when;
00200 void (*where) (void *);
00201 void *what;
00202 tvref_t ref;
00203 tvunref_t unref;
00204 {
00205 struct timeout *t, *q;
00206 int usereset = 0;
00207 isc_result_t status;
00208 int64_t sec;
00209 int usec;
00210 isc_interval_t interval;
00211 isc_time_t expires;
00212
00213 if (when == NULL) {
00214 return;
00215 }
00216
00217
00218 t = (struct timeout *)0;
00219 for (q = timeouts; q; q = q->next) {
00220 if ((where == NULL || q->func == where) &&
00221 q->what == what) {
00222 if (t)
00223 t->next = q->next;
00224 else
00225 timeouts = q->next;
00226 usereset = 1;
00227 break;
00228 }
00229 t = q;
00230 }
00231
00232
00233
00234 if (!q) {
00235 if (free_timeouts) {
00236 q = free_timeouts;
00237 free_timeouts = q->next;
00238 } else {
00239 q = ((struct timeout *)
00240 dmalloc(sizeof(struct timeout), MDL));
00241 if (!q) {
00242 log_fatal("add_timeout: no memory!");
00243 }
00244 }
00245 memset(q, 0, sizeof *q);
00246 q->func = where;
00247 q->ref = ref;
00248 q->unref = unref;
00249 if (q->ref)
00250 (*q->ref)(&q->what, what, MDL);
00251 else
00252 q->what = what;
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 sec = when->tv_sec - cur_tv.tv_sec;
00269 usec = when->tv_usec - cur_tv.tv_usec;
00270
00271 if ((when->tv_usec != 0) && (usec < 0)) {
00272 sec--;
00273 usec += USEC_MAX;
00274 }
00275
00276 if (sec < 0) {
00277 sec = 0;
00278 usec = 0;
00279 } else if (sec > DHCP_SEC_MAX) {
00280 log_error("Timeout requested too large "
00281 "reducing to 2^^32-1");
00282 sec = DHCP_SEC_MAX;
00283 usec = 0;
00284 } else if (usec < 0) {
00285 usec = 0;
00286 } else if (usec >= USEC_MAX) {
00287 usec = USEC_MAX - 1;
00288 }
00289
00290
00291
00292
00293
00294
00295 q->when.tv_sec = cur_tv.tv_sec + (sec & DHCP_SEC_MAX);
00296 q->when.tv_usec = usec;
00297
00298 #if defined (TRACING)
00299 if (trace_playback()) {
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 if (!timeouts || (timeouts->when.tv_sec > q-> when.tv_sec) ||
00313 ((timeouts->when.tv_sec == q->when.tv_sec) &&
00314 (timeouts->when.tv_usec > q->when.tv_usec))) {
00315 q->next = timeouts;
00316 timeouts = q;
00317 return;
00318 }
00319
00320
00321 for (t = timeouts; t->next; t = t->next) {
00322 if ((t->next->when.tv_sec > q->when.tv_sec) ||
00323 ((t->next->when.tv_sec == q->when.tv_sec) &&
00324 (t->next->when.tv_usec > q->when.tv_usec))) {
00325 q->next = t->next;
00326 t->next = q;
00327 return;
00328 }
00329 }
00330
00331
00332 t->next = q;
00333 q->next = (struct timeout *)0;
00334 return;
00335 }
00336 #endif
00337
00338
00339
00340
00341
00342
00343 q->next = timeouts;
00344 timeouts = q;
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 if (sec > TIME_MAX)
00357 sec = TIME_MAX - 9;
00358
00359 isc_interval_set(&interval, sec, usec * 1000);
00360 status = isc_time_nowplusinterval(&expires, &interval);
00361 if (status != ISC_R_SUCCESS) {
00362
00363
00364
00365
00366 log_fatal("Unable to set up timer: %s",
00367 isc_result_totext(status));
00368 }
00369
00370 if (usereset == 0) {
00371 status = isc_timer_create(dhcp_gbl_ctx.timermgr,
00372 isc_timertype_once, &expires,
00373 NULL, dhcp_gbl_ctx.task,
00374 isclib_timer_callback,
00375 (void *)q, &q->isc_timeout);
00376 } else {
00377 status = isc_timer_reset(q->isc_timeout,
00378 isc_timertype_once, &expires,
00379 NULL, 0);
00380 }
00381
00382
00383 if (status != ISC_R_SUCCESS) {
00384 log_fatal("Unable to add timeout to isclib\n");
00385 }
00386
00387 return;
00388 }
00389
00390 void cancel_timeout (where, what)
00391 void (*where) (void *);
00392 void *what;
00393 {
00394 struct timeout *t, *q;
00395
00396
00397 t = (struct timeout *)0;
00398 for (q = timeouts; q; q = q -> next) {
00399 if (q->func == where && q->what == what) {
00400 if (t)
00401 t->next = q->next;
00402 else
00403 timeouts = q->next;
00404 break;
00405 }
00406 t = q;
00407 }
00408
00409
00410
00411
00412
00413
00414 if (q) {
00415 #if defined (TRACING)
00416 if (!trace_playback()) {
00417 #endif
00418 isc_timer_detach(&q->isc_timeout);
00419 #if defined (TRACING)
00420 }
00421 #endif
00422
00423 if (q->unref)
00424 (*q->unref) (&q->what, MDL);
00425 q->next = free_timeouts;
00426 free_timeouts = q;
00427 }
00428 }
00429
00430 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
00431 void cancel_all_timeouts ()
00432 {
00433 struct timeout *t, *n;
00434 for (t = timeouts; t; t = n) {
00435 n = t->next;
00436 isc_timer_detach(&t->isc_timeout);
00437 if (t->unref && t->what)
00438 (*t->unref) (&t->what, MDL);
00439 t->next = free_timeouts;
00440 free_timeouts = t;
00441 }
00442 }
00443
00444 void relinquish_timeouts ()
00445 {
00446 struct timeout *t, *n;
00447 for (t = free_timeouts; t; t = n) {
00448 n = t->next;
00449 dfree(t, MDL);
00450 }
00451 }
00452 #endif