afb40ef9078101284f3f65f53eeb3874c7b5a6e9
[exim.git] / src / src / retry.c
1 /* $Cambridge: exim/src/src/retry.c,v 1.10 2006/04/20 10:57:46 ph10 Exp $ */
2
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
6
7 /* Copyright (c) University of Cambridge 1995 - 2006 */
8 /* See the file NOTICE for conditions of use and distribution. */
9
10 /* Functions concerned with retrying unsuccessful deliveries. */
11
12
13 #include "exim.h"
14
15
16
17 /*************************************************
18 * Check the ultimate address timeout *
19 *************************************************/
20
21 /* This function tests whether a message has been on the queue longer than
22 the maximum retry time for a particular host.
23
24 Arguments:
25 host_key the key to look up a host retry rule
26 domain the domain to look up a domain retry rule
27 basic_errno a specific error number, or zero if none
28 more_errno additional data for the error
29 now the time
30
31 Returns: TRUE if the ultimate timeout has been reached
32 */
33
34 static BOOL
35 ultimate_address_timeout(uschar *host_key, uschar *domain, int basic_errno,
36 int more_errno, time_t now)
37 {
38 BOOL address_timeout = TRUE; /* no rule => timed out */
39
40 retry_config *retry =
41 retry_find_config(host_key+2, domain, basic_errno, more_errno);
42
43 if (retry != NULL && retry->rules != NULL)
44 {
45 retry_rule *last_rule;
46 for (last_rule = retry->rules;
47 last_rule->next != NULL;
48 last_rule = last_rule->next);
49 DEBUG(D_transport|D_retry)
50 debug_printf("now=%d received_time=%d diff=%d timeout=%d\n",
51 (int)now, received_time, (int)(now - received_time),
52 last_rule->timeout);
53 address_timeout = (now - received_time > last_rule->timeout);
54 }
55 else
56 {
57 DEBUG(D_transport|D_retry)
58 debug_printf("no retry rule found: assume timed out\n");
59 }
60
61 return address_timeout;
62 }
63
64
65
66 /*************************************************
67 * Set status of a host+address item *
68 *************************************************/
69
70 /* This function is passed a host_item which contains a host name and an
71 IP address string. Its job is to set the status of the address if it is not
72 already set (indicated by hstatus_unknown). The possible values are:
73
74 hstatus_usable the address is not listed in the unusable tree, and does
75 not have a retry record, OR the time is past the next
76 try time, OR the message has been on the queue for more
77 than the maximum retry time for a failing host
78
79 hstatus_unusable the address is listed in the unusable tree, or does have
80 a retry record, and the time is not yet at the next retry
81 time.
82
83 hstatus_unusable_expired as above, but also the retry time has expired
84 for this address.
85
86 The reason a delivery is permitted when a message has been around for a very
87 long time is to allow the ultimate address timeout to operate after a delivery
88 failure. Otherwise some messages may stick around without being tried for too
89 long.
90
91 If a host retry record is retrieved from the hints database, the time of last
92 trying is filled into the last_try field of the host block. If a host is
93 generally usable, a check is made to see if there is a retry delay on this
94 specific message at this host.
95
96 If a non-standard port is being used, it is added to the retry key.
97
98 Arguments:
99 domain the address domain
100 host pointer to a host item
101 portstring "" for standard port, ":xxxx" for a non-standard port
102 include_ip_address TRUE to include the address in the key - this is
103 usual, but sometimes is not wanted
104 retry_host_key where to put a pointer to the key for the host-specific
105 retry record, if one is read and the host is usable
106 retry_message_key where to put a pointer to the key for the message+host
107 retry record, if one is read and the host is usable
108
109 Returns: TRUE if the host has expired but is usable because
110 its retry time has come
111 */
112
113 BOOL
114 retry_check_address(uschar *domain, host_item *host, uschar *portstring,
115 BOOL include_ip_address, uschar **retry_host_key, uschar **retry_message_key)
116 {
117 BOOL yield = FALSE;
118 time_t now = time(NULL);
119 uschar *host_key, *message_key;
120 open_db dbblock;
121 open_db *dbm_file;
122 tree_node *node;
123 dbdata_retry *host_retry_record, *message_retry_record;
124
125 *retry_host_key = *retry_message_key = NULL;
126
127 DEBUG(D_transport|D_retry) debug_printf("checking status of %s\n", host->name);
128
129 /* Do nothing if status already set; otherwise initialize status as usable. */
130
131 if (host->status != hstatus_unknown) return FALSE;
132 host->status = hstatus_usable;
133
134 /* Generate the host key for the unusable tree and the retry database. Ensure
135 host names are lower cased (that's what %S does). */
136
137 host_key = include_ip_address?
138 string_sprintf("T:%S:%s%s", host->name, host->address, portstring) :
139 string_sprintf("T:%S%s", host->name, portstring);
140
141 /* Generate the message-specific key */
142
143 message_key = string_sprintf("%s:%s", host_key, message_id);
144
145 /* Search the tree of unusable IP addresses. This is filled in when deliveries
146 fail, because the retry database itself is not updated until the end of all
147 deliveries (so as to do it all in one go). The tree records addresses that have
148 become unusable during this delivery process (i.e. those that will get put into
149 the retry database when it is updated). */
150
151 node = tree_search(tree_unusable, host_key);
152 if (node != NULL)
153 {
154 DEBUG(D_transport|D_retry) debug_printf("found in tree of unusables\n");
155 host->status = (node->data.val > 255)?
156 hstatus_unusable_expired : hstatus_unusable;
157 host->why = node->data.val & 255;
158 return FALSE;
159 }
160
161 /* Open the retry database, giving up if there isn't one. Otherwise, search for
162 the retry records, and then close the database again. */
163
164 if ((dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE)) == NULL)
165 {
166 DEBUG(D_deliver|D_retry|D_hints_lookup)
167 debug_printf("no retry data available\n");
168 return FALSE;
169 }
170 host_retry_record = dbfn_read(dbm_file, host_key);
171 message_retry_record = dbfn_read(dbm_file, message_key);
172 dbfn_close(dbm_file);
173
174 /* Ignore the data if it is too old - too long since it was written */
175
176 if (host_retry_record == NULL)
177 {
178 DEBUG(D_transport|D_retry) debug_printf("no host retry record\n");
179 }
180 else if (now - host_retry_record->time_stamp > retry_data_expire)
181 {
182 host_retry_record = NULL;
183 DEBUG(D_transport|D_retry) debug_printf("host retry record too old\n");
184 }
185
186 if (message_retry_record == NULL)
187 {
188 DEBUG(D_transport|D_retry) debug_printf("no message retry record\n");
189 }
190 else if (now - message_retry_record->time_stamp > retry_data_expire)
191 {
192 message_retry_record = NULL;
193 DEBUG(D_transport|D_retry) debug_printf("message retry record too old\n");
194 }
195
196 /* If there's a host-specific retry record, check for reaching the retry
197 time (or forcing). If not, and the host is not expired, check for the message
198 having been around for longer than the maximum retry time for this host or
199 address. Allow the delivery if it has. Otherwise set the appropriate unusable
200 flag and return FALSE. Otherwise arrange to return TRUE if this is an expired
201 host. */
202
203 if (host_retry_record != NULL)
204 {
205 *retry_host_key = host_key;
206
207 /* We have not reached the next try time. Check for the ultimate address
208 timeout if the host has not expired. */
209
210 if (now < host_retry_record->next_try && !deliver_force)
211 {
212 DEBUG(D_transport|D_retry)
213 debug_printf("host retry time not reached: checking ultimate address "
214 "timeout\n");
215
216 if (!host_retry_record->expired &&
217 ultimate_address_timeout(host_key, domain,
218 host_retry_record->basic_errno, host_retry_record->more_errno, now))
219 {
220 DEBUG(D_transport|D_retry)
221 debug_printf("on queue longer than maximum retry for "
222 "address - allowing delivery\n");
223 return FALSE;
224 }
225
226 /* We have not hit the ultimate address timeout; host is unusable. */
227
228 host->status = (host_retry_record->expired)?
229 hstatus_unusable_expired : hstatus_unusable;
230 host->why = hwhy_retry;
231 host->last_try = host_retry_record->last_try;
232 return FALSE;
233 }
234
235 /* Host is usable; set return TRUE if expired. */
236
237 yield = host_retry_record->expired;
238 }
239
240 /* It's OK to try the host. If there's a message-specific retry record, check
241 for reaching its retry time (or forcing). If not, mark the host unusable,
242 unless the ultimate address timeout has been reached. */
243
244 if (message_retry_record != NULL)
245 {
246 *retry_message_key = message_key;
247 if (now < message_retry_record->next_try && !deliver_force)
248 {
249 DEBUG(D_transport|D_retry)
250 debug_printf("host+message retry time not reached: checking ultimate "
251 "address timeout\n");
252 if (!ultimate_address_timeout(host_key, domain, 0, 0, now))
253 {
254 host->status = hstatus_unusable;
255 host->why = hwhy_retry;
256 }
257 else
258 {
259 DEBUG(D_transport|D_retry)
260 debug_printf("on queue longer than maximum retry for "
261 "address - allowing delivery\n");
262 }
263 return FALSE;
264 }
265 }
266
267 return yield;
268 }
269
270
271
272
273 /*************************************************
274 * Add a retry item to an address *
275 *************************************************/
276
277 /* Retry items are chained onto an address when it is deferred either by router
278 or by a transport, or if it succeeds or fails and there was a previous retry
279 item that now needs to be deleted. Sometimes there can be both kinds of item:
280 for example, if routing was deferred but then succeeded, and delivery then
281 deferred. In that case there is a delete item for the routing retry, and an
282 updating item for the delivery.
283
284 (But note that that is only visible at the outer level, because in remote
285 delivery subprocesses, the address starts "clean", with no retry items carried
286 in.)
287
288 These items are used at the end of a delivery attempt to update the retry
289 database. The keys start R: for routing delays and T: for transport delays.
290
291 Arguments:
292 addr the address block onto which to hang the item
293 key the retry key
294 flags delete, host, and message flags, copied into the block
295
296 Returns: nothing
297 */
298
299 void
300 retry_add_item(address_item *addr, uschar *key, int flags)
301 {
302 retry_item *rti = store_get(sizeof(retry_item));
303 rti->next = addr->retries;
304 addr->retries = rti;
305 rti->key = key;
306 rti->basic_errno = addr->basic_errno;
307 rti->more_errno = addr->more_errno;
308 rti->message = addr->message;
309 rti->flags = flags;
310
311 DEBUG(D_transport|D_retry)
312 {
313 int letter = rti->more_errno & 255;
314 debug_printf("added retry item for %s: errno=%d more_errno=", rti->key,
315 rti->basic_errno);
316 if (letter == 'A' || letter == 'M')
317 debug_printf("%d,%c", (rti->more_errno >> 8) & 255, letter);
318 else
319 debug_printf("%d", rti->more_errno);
320 debug_printf(" flags=%d\n", flags);
321 }
322 }
323
324
325
326 /*************************************************
327 * Find retry configuration data *
328 *************************************************/
329
330 /* Search the in-store retry information for the first retry item that applies
331 to a given destination. If the key contains an @ we are probably handling a
332 local delivery and have a complete address to search for; this happens when
333 retry_use_local_part is set on a router. Otherwise, the key is likely to be a
334 host name for a remote delivery, or a domain name for a local delivery. We
335 prepend *@ on the front of it so that it will match a retry item whose address
336 item pattern is independent of the local part. The alternate key, if set, is
337 always just a domain, so we treat it likewise.
338
339 Arguments:
340 key key for which retry info is wanted
341 alternate alternative key, always just a domain
342 basic_errno specific error predicate on the retry rule, or zero
343 more_errno additional data for errno predicate
344
345 Returns: pointer to retry rule, or NULL
346 */
347
348 retry_config *
349 retry_find_config(uschar *key, uschar *alternate, int basic_errno,
350 int more_errno)
351 {
352 int replace = 0;
353 uschar *use_key, *use_alternate;
354 uschar *colon = Ustrchr(key, ':');
355 retry_config *yield;
356
357 /* If there's a colon in the key, there are two possibilities:
358
359 (1) This is a key for a host, ip address, and possibly port, in the format
360
361 hostname:ip+port
362
363 In this case, we temporarily replace the colon with a zero, to terminate
364 the string after the host name.
365
366 (2) This is a key for a pipe, file, or autoreply delivery, in the format
367
368 pipe-or-file-or-auto:x@y
369
370 where x@y is the original address that provoked the delivery. The pipe or
371 file or auto will start with | or / or >, whereas a host name will start
372 with a letter or a digit. In this case we want to use the original address
373 to search for a retry rule. */
374
375 if (colon != NULL)
376 {
377 if (isalnum(*key))
378 replace = ':';
379 else
380 key = Ustrrchr(key, ':') + 1; /* Take from the last colon */
381 }
382
383 if (replace == 0) colon = key + Ustrlen(key);
384 *colon = 0;
385
386 /* Sort out the keys */
387
388 use_key = (Ustrchr(key, '@') != NULL)? key : string_sprintf("*@%s", key);
389 use_alternate = (alternate == NULL)? NULL : string_sprintf("*@%s", alternate);
390
391 /* Scan the configured retry items. */
392
393 for (yield = retries; yield != NULL; yield = yield->next)
394 {
395 uschar *plist = yield->pattern;
396 uschar *slist = yield->senders;
397
398 /* If a specific error is set for this item, check that we are handling that
399 specific error, and if so, check any additional error information if
400 required. */
401
402 if (yield->basic_errno != 0)
403 {
404 /* Special code is required for quota errors, as these can either be system
405 quota errors, or Exim's own quota imposition, which has a different error
406 number. Full partitions are also treated in the same way as quota errors.
407 */
408
409 if (yield->basic_errno == ERRNO_EXIMQUOTA)
410 {
411 if ((basic_errno != ERRNO_EXIMQUOTA && basic_errno != errno_quota &&
412 basic_errno != ENOSPC) ||
413 (yield->more_errno != 0 && yield->more_errno > more_errno))
414 continue;
415 }
416
417 /* The TLSREQUIRED error also covers TLSFAILURE. These are subtly different
418 errors, but not worth separating at this level. */
419
420 else if (yield->basic_errno == ERRNO_TLSREQUIRED)
421 {
422 if (basic_errno != ERRNO_TLSREQUIRED && basic_errno != ERRNO_TLSFAILURE)
423 continue;
424 }
425
426 /* Handle 4xx responses to MAIL, RCPT, or DATA. The code that was received
427 is in the 2nd least significant byte of more_errno (with 400 subtracted).
428 The required value is coded in the 2nd least significant byte of the
429 yield->more_errno field as follows:
430
431 255 => any 4xx code
432 >= 100 => the decade must match the value less 100
433 < 100 => the exact value must match
434 */
435
436 else if (yield->basic_errno == ERRNO_MAIL4XX ||
437 yield->basic_errno == ERRNO_RCPT4XX ||
438 yield->basic_errno == ERRNO_DATA4XX)
439 {
440 int wanted;
441 if (basic_errno != yield->basic_errno) continue;
442 wanted = (yield->more_errno >> 8) & 255;
443 if (wanted != 255)
444 {
445 int evalue = (more_errno >> 8) & 255;
446 if (wanted >= 100)
447 {
448 if ((evalue/10)*10 != wanted - 100) continue;
449 }
450 else if (evalue != wanted) continue;
451 }
452 }
453
454 /* There are some special cases for timeouts */
455
456 else if (yield->basic_errno == ETIMEDOUT)
457 {
458 if (basic_errno != ETIMEDOUT) continue;
459
460 /* Just RTEF_CTOUT in the rule => don't care about 'A'/'M' addresses */
461 if (yield->more_errno == RTEF_CTOUT)
462 {
463 if ((more_errno & RTEF_CTOUT) == 0) continue;
464 }
465
466 else if (yield->more_errno != 0)
467 {
468 int cf_errno = more_errno;
469 if ((yield->more_errno & RTEF_CTOUT) == 0) cf_errno &= ~RTEF_CTOUT;
470 if (yield->more_errno != cf_errno) continue;
471 }
472 }
473
474 /* Default checks for exact match */
475
476 else
477 {
478 if (yield->basic_errno != basic_errno ||
479 (yield->more_errno != 0 && yield->more_errno != more_errno))
480 continue;
481 }
482 }
483
484 /* If the "senders" condition is set, check it. Note that sender_address may
485 be null during -brt checking, in which case we do not use this rule. */
486
487 if (slist != NULL && (sender_address == NULL ||
488 match_address_list(sender_address, TRUE, TRUE, &slist, NULL, -1, 0,
489 NULL) != OK))
490 continue;
491
492 /* Check for a match between the address list item at the start of this retry
493 rule and either the main or alternate keys. */
494
495 if (match_address_list(use_key, TRUE, TRUE, &plist, NULL, -1, UCHAR_MAX+1,
496 NULL) == OK ||
497 (use_alternate != NULL &&
498 match_address_list(use_alternate, TRUE, TRUE, &plist, NULL, -1,
499 UCHAR_MAX+1, NULL) == OK))
500 break;
501 }
502
503 *colon = replace;
504 return yield;
505 }
506
507
508
509
510 /*************************************************
511 * Update retry database *
512 *************************************************/
513
514 /* Update the retry data for any directing/routing/transporting that was
515 deferred, or delete it for those that succeeded after a previous defer. This is
516 done all in one go to minimize opening/closing/locking of the database file.
517
518 Note that, because SMTP delivery involves a list of destinations to try, there
519 may be defer-type retry information for some of them even when the message was
520 successfully delivered. Likewise if it eventually failed.
521
522 This function may move addresses from the defer to the failed queue if the
523 ultimate retry time has expired.
524
525 Arguments:
526 addr_defer queue of deferred addresses
527 addr_failed queue of failed addresses
528 addr_succeed queue of successful addresses
529
530 Returns: nothing
531 */
532
533 void
534 retry_update(address_item **addr_defer, address_item **addr_failed,
535 address_item **addr_succeed)
536 {
537 open_db dbblock;
538 open_db *dbm_file = NULL;
539 time_t now = time(NULL);
540 int i;
541
542 DEBUG(D_retry) debug_printf("Processing retry items\n");
543
544 /* Three-times loop to handle succeeded, failed, and deferred addresses.
545 Deferred addresses must be handled after failed ones, because some may be moved
546 to the failed chain if they have timed out. */
547
548 for (i = 0; i < 3; i++)
549 {
550 address_item *endaddr, *addr;
551 address_item *last_first = NULL;
552 address_item **paddr = (i==0)? addr_succeed :
553 (i==1)? addr_failed : addr_defer;
554 address_item **saved_paddr = NULL;
555
556 DEBUG(D_retry) debug_printf("%s addresses:\n", (i == 0)? "Succeeded" :
557 (i == 1)? "Failed" : "Deferred");
558
559 /* Loop for each address on the chain. For deferred addresses, the whole
560 address times out unless one of its retry addresses has a retry rule that
561 hasn't yet timed out. Deferred addresses should not be requesting deletion
562 of retry items, but just in case they do by accident, treat that case
563 as "not timed out".
564
565 As well as handling the addresses themselves, we must also process any
566 retry items for any parent addresses - these are typically "delete" items,
567 because the parent must have succeeded in order to generate the child. */
568
569 while ((endaddr = *paddr) != NULL)
570 {
571 BOOL timed_out = FALSE;
572 retry_item *rti;
573
574 for (addr = endaddr; addr != NULL; addr = addr->parent)
575 {
576 int update_count = 0;
577 int timedout_count = 0;
578
579 DEBUG(D_retry) debug_printf("%s%s\n", addr->address, (addr->retries == NULL)?
580 ": no retry items" : "");
581
582 /* Loop for each retry item. */
583
584 for (rti = addr->retries; rti != NULL; rti = rti->next)
585 {
586 uschar *message;
587 int message_length, message_space, failing_interval, next_try;
588 retry_rule *rule, *final_rule;
589 retry_config *retry;
590 dbdata_retry *retry_record;
591
592 /* Open the retry database if it is not already open; failure to open
593 the file is logged, but otherwise ignored - deferred addresses will
594 get retried at the next opportunity. Not opening earlier than this saves
595 opening if no addresses have retry items - common when none have yet
596 reached their retry next try time. */
597
598 if (dbm_file == NULL)
599 dbm_file = dbfn_open(US"retry", O_RDWR, &dbblock, TRUE);
600
601 if (dbm_file == NULL)
602 {
603 DEBUG(D_deliver|D_retry|D_hints_lookup)
604 debug_printf("retry database not available for updating\n");
605 return;
606 }
607
608 /* If there are no deferred addresses, that is, if this message is
609 completing, and the retry item is for a message-specific SMTP error,
610 force it to be deleted, because there's no point in keeping data for
611 no-longer-existing messages. This situation can occur when a domain has
612 two hosts and a message-specific error occurs for the first of them,
613 but the address gets delivered to the second one. This optimization
614 doesn't succeed in cleaning out all the dead entries, but it helps. */
615
616 if (*addr_defer == NULL && (rti->flags & rf_message) != 0)
617 rti->flags |= rf_delete;
618
619 /* Handle the case of a request to delete the retry info for this
620 destination. */
621
622 if ((rti->flags & rf_delete) != 0)
623 {
624 (void)dbfn_delete(dbm_file, rti->key);
625 DEBUG(D_retry)
626 debug_printf("deleted retry information for %s\n", rti->key);
627 continue;
628 }
629
630 /* Count the number of non-delete retry items. This is so that we
631 can compare it to the count of timed_out ones, to check whether
632 all are timed out. */
633
634 update_count++;
635
636 /* Get the retry information for this destination and error code, if
637 any. If this item is for a remote host with ip address, then pass
638 the domain name as an alternative to search for. If no retry
639 information is found, we can't generate a retry time, so there is
640 no point updating the database. This retry item is timed out. */
641
642 if ((retry = retry_find_config(rti->key + 2,
643 ((rti->flags & rf_host) != 0)? addr->domain : NULL,
644 rti->basic_errno, rti->more_errno)) == NULL)
645 {
646 DEBUG(D_retry) debug_printf("No configured retry item for %s%s%s\n",
647 rti->key,
648 ((rti->flags & rf_host) != 0)? US" or " : US"",
649 ((rti->flags & rf_host) != 0)? addr->domain : US"");
650 if (addr == endaddr) timedout_count++;
651 continue;
652 }
653
654 DEBUG(D_retry)
655 {
656 if ((rti->flags & rf_host) != 0)
657 debug_printf("retry for %s (%s) = %s %d %d\n", rti->key,
658 addr->domain, retry->pattern, retry->basic_errno,
659 retry->more_errno);
660 else
661 debug_printf("retry for %s = %s %d %d\n", rti->key, retry->pattern,
662 retry->basic_errno, retry->more_errno);
663 }
664
665 /* Set up the message for the database retry record. Because DBM
666 records have a maximum data length, we enforce a limit. There isn't
667 much point in keeping a huge message here, anyway. */
668
669 message = (rti->basic_errno > 0)? US strerror(rti->basic_errno) :
670 (rti->message == NULL)?
671 US"unknown error" : string_printing(rti->message);
672 message_length = Ustrlen(message);
673 if (message_length > 150) message_length = 150;
674
675 /* Read a retry record from the database or construct a new one.
676 Ignore an old one if it is too old since it was last updated. */
677
678 retry_record = dbfn_read(dbm_file, rti->key);
679 if (retry_record != NULL &&
680 now - retry_record->time_stamp > retry_data_expire)
681 retry_record = NULL;
682
683 if (retry_record == NULL)
684 {
685 retry_record = store_get(sizeof(dbdata_retry) + message_length);
686 message_space = message_length;
687 retry_record->first_failed = now;
688 retry_record->last_try = now;
689 retry_record->next_try = now;
690 retry_record->expired = FALSE;
691 retry_record->text[0] = 0; /* just in case */
692 }
693 else message_space = Ustrlen(retry_record->text);
694
695 /* Compute how long this destination has been failing */
696
697 failing_interval = now - retry_record->first_failed;
698 DEBUG(D_retry) debug_printf("failing_interval=%d message_age=%d\n",
699 failing_interval, message_age);
700
701 /* For a non-host error, if the message has been on the queue longer
702 than the recorded time of failure, use the message's age instead. This
703 can happen when some messages can be delivered and others cannot; a
704 successful delivery will reset the first_failed time, and this can lead
705 to a failing message being retried too often. */
706
707 if ((rti->flags & rf_host) == 0 && message_age > failing_interval)
708 failing_interval = message_age;
709
710 /* Search for the current retry rule. The cutoff time of the
711 last rule is handled differently to the others. The rule continues
712 to operate for ever (the global maximum interval will eventually
713 limit the gaps) but its cutoff time determines when an individual
714 destination times out. If there are no retry rules, the destination
715 always times out, but we can't compute a retry time. */
716
717 final_rule = NULL;
718 for (rule = retry->rules; rule != NULL; rule = rule->next)
719 {
720 if (failing_interval <= rule->timeout) break;
721 final_rule = rule;
722 }
723
724 /* If there's an un-timed out rule, the destination has not
725 yet timed out, so the address as a whole has not timed out (but we are
726 interested in this only for the end address). Make sure the expired
727 flag is false (can be forced via fixdb from outside, but ensure it is
728 consistent with the rules whenever we go through here). */
729
730 if (rule != NULL)
731 {
732 retry_record->expired = FALSE;
733 }
734
735 /* Otherwise, set the retry timeout expired, and set the final rule
736 as the one from which to compute the next retry time. Subsequent
737 messages will fail immediately until the retry time is reached (unless
738 there are other, still active, retries). */
739
740 else
741 {
742 rule = final_rule;
743 retry_record->expired = TRUE;
744 if (addr == endaddr) timedout_count++;
745 }
746
747 /* There is a special case to consider when some messages get through
748 to a destination and others don't. This can happen locally when a
749 large message pushes a user over quota, and it can happen remotely
750 when a machine is on a dodgy Internet connection. The messages that
751 get through wipe the retry information, causing those that don't to
752 stay on the queue longer than the final retry time. In order to
753 avoid this, we check, using the time of arrival of the message, to
754 see if it has been on the queue for more than the final cutoff time,
755 and if so, cause this retry item to time out, and the retry time to
756 be set to "now" so that any subsequent messages in the same condition
757 also get tried. We search for the last rule onwards from the one that
758 is in use. If there are no retry rules for the item, rule will be null
759 and timedout_count will already have been updated.
760
761 This implements "timeout this rule if EITHER the host (or routing or
762 directing) has been failing for more than the maximum time, OR if the
763 message has been on the queue for more than the maximum time."
764
765 February 2006: It is possible that this code is no longer needed
766 following the change to the retry calculation to use the message age if
767 it is larger than the time since first failure. It may be that the
768 expired flag is always set when the other conditions are met. However,
769 this is a small bit of code, and it does no harm to leave it in place,
770 just in case. */
771
772 if (received_time <= retry_record->first_failed &&
773 addr == endaddr && !retry_record->expired && rule != NULL)
774 {
775 retry_rule *last_rule;
776 for (last_rule = rule;
777 last_rule->next != NULL;
778 last_rule = last_rule->next);
779 if (now - received_time > last_rule->timeout)
780 {
781 DEBUG(D_retry) debug_printf("on queue longer than maximum retry\n");
782 timedout_count++;
783 rule = NULL;
784 }
785 }
786
787 /* Compute the next try time from the rule, subject to the global
788 maximum, and update the retry database. If rule == NULL it means
789 there were no rules at all (and the timeout will be set expired),
790 or we have a message that is older than the final timeout. In this
791 case set the next retry time to now, so that one delivery attempt
792 happens for subsequent messages. */
793
794 if (rule == NULL) next_try = now; else
795 {
796 if (rule->rule == 'F') next_try = now + rule->p1;
797 else /* rule = 'G' or 'H' */
798 {
799 int last_predicted_gap =
800 retry_record->next_try - retry_record->last_try;
801 int last_actual_gap = now - retry_record->last_try;
802 int lastgap = (last_predicted_gap < last_actual_gap)?
803 last_predicted_gap : last_actual_gap;
804 int next_gap = (lastgap * rule->p2)/1000;
805 if (rule->rule == 'G')
806 {
807 next_try = now + ((lastgap < rule->p1)? rule->p1 : next_gap);
808 }
809 else /* The 'H' rule */
810 {
811 next_try = now + rule->p1;
812 if (next_gap > rule->p1)
813 next_try += random_number(next_gap - rule->p1)/2 +
814 (next_gap - rule->p1)/2;
815 }
816 }
817 }
818
819 /* Impose a global retry max */
820
821 if (next_try - now > retry_interval_max)
822 next_try = now + retry_interval_max;
823
824 /* If the new message length is greater than the previous one, we
825 have to copy the record first. */
826
827 if (message_length > message_space)
828 {
829 dbdata_retry *newr = store_get(sizeof(dbdata_retry) + message_length);
830 memcpy(newr, retry_record, sizeof(dbdata_retry));
831 retry_record = newr;
832 }
833
834 /* Set up the retry record; message_length may be less than the string
835 length for very long error strings. */
836
837 retry_record->last_try = now;
838 retry_record->next_try = next_try;
839 retry_record->basic_errno = rti->basic_errno;
840 retry_record->more_errno = rti->more_errno;
841 Ustrncpy(retry_record->text, message, message_length);
842 retry_record->text[message_length] = 0;
843
844 DEBUG(D_retry)
845 {
846 int letter = retry_record->more_errno & 255;
847 debug_printf("Writing retry data for %s\n", rti->key);
848 debug_printf(" first failed=%d last try=%d next try=%d expired=%d\n",
849 (int)retry_record->first_failed, (int)retry_record->last_try,
850 (int)retry_record->next_try, retry_record->expired);
851 debug_printf(" errno=%d more_errno=", retry_record->basic_errno);
852 if (letter == 'A' || letter == 'M')
853 debug_printf("%d,%c", (retry_record->more_errno >> 8) & 255,
854 letter);
855 else
856 debug_printf("%d", retry_record->more_errno);
857 debug_printf(" %s\n", retry_record->text);
858 }
859
860 (void)dbfn_write(dbm_file, rti->key, retry_record,
861 sizeof(dbdata_retry) + message_length);
862 } /* Loop for each retry item */
863
864 /* If all the non-delete retry items are timed out, the address is
865 timed out, provided that we didn't skip any hosts because their retry
866 time was not reached (or because of hosts_max_try). */
867
868 if (update_count > 0 && update_count == timedout_count)
869 {
870 if (!testflag(endaddr, af_retry_skipped))
871 {
872 DEBUG(D_retry) debug_printf("timed out: all retries expired\n");
873 timed_out = TRUE;
874 }
875 else
876 {
877 DEBUG(D_retry)
878 debug_printf("timed out but some hosts were skipped\n");
879 }
880 }
881 } /* Loop for an address and its parents */
882
883 /* If this is a deferred address, and retry processing was requested by
884 means of one or more retry items, and they all timed out, move the address
885 to the failed queue, and restart this loop without updating paddr.
886
887 If there were several addresses batched in the same remote delivery, only
888 the original top one will have host retry items attached to it, but we want
889 to handle all the same. Each will have a pointer back to its "top" address,
890 and they will now precede the item with the retries because addresses are
891 inverted when added to these final queues. We have saved information about
892 them in passing (below) so they can all be cut out at once. */
893
894 if (i == 2) /* Handling defers */
895 {
896 if (endaddr->retries != NULL && timed_out)
897 {
898 if (last_first == endaddr) paddr = saved_paddr;
899 addr = *paddr;
900 *paddr = endaddr->next;
901
902 endaddr->next = *addr_failed;
903 *addr_failed = addr;
904
905 for (;; addr = addr->next)
906 {
907 setflag(addr, af_retry_timedout);
908 addr->message = (addr->message == NULL)? US"retry timeout exceeded" :
909 string_sprintf("%s: retry timeout exceeded", addr->message);
910 addr->user_message = (addr->user_message == NULL)?
911 US"retry timeout exceeded" :
912 string_sprintf("%s: retry timeout exceeded", addr->user_message);
913 log_write(0, LOG_MAIN, "** %s%s%s%s: retry timeout exceeded",
914 addr->address,
915 (addr->parent == NULL)? US"" : US" <",
916 (addr->parent == NULL)? US"" : addr->parent->address,
917 (addr->parent == NULL)? US"" : US">");
918
919 if (addr == endaddr) break;
920 }
921
922 continue; /* Restart from changed *paddr */
923 }
924
925 /* This address is to remain on the defer chain. If it has a "first"
926 pointer, save the pointer to it in case we want to fail the set of
927 addresses when we get to the first one. */
928
929 if (endaddr->first != last_first)
930 {
931 last_first = endaddr->first;
932 saved_paddr = paddr;
933 }
934 }
935
936 /* All cases (succeed, fail, defer left on queue) */
937
938 paddr = &(endaddr->next); /* Advance to next address */
939 } /* Loop for all addresses */
940 } /* Loop for succeed, fail, defer */
941
942 /* Close and unlock the database */
943
944 if (dbm_file != NULL) dbfn_close(dbm_file);
945
946 DEBUG(D_retry) debug_printf("end of retry processing\n");
947 }
948
949 /* End of retry.c */