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