1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2023 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
10 /* Functions concerned with retrying unsuccessful deliveries. */
17 /*************************************************
18 * Check the ultimate address timeout *
19 *************************************************/
21 /* This function tests whether a message has been on the queue longer than
22 the maximum retry time for a particular host or address.
25 retry_key the key to look up a retry rule
26 domain the domain to look up a domain retry rule
27 retry_record contains error information for finding rule
30 Returns: TRUE if the ultimate timeout has been reached
34 retry_ultimate_address_timeout(const uschar * retry_key, const uschar *domain,
35 dbdata_retry *retry_record, time_t now)
42 debug_printf("retry time not reached: checking ultimate address timeout\n");
43 debug_printf(" now=" TIME_T_FMT " first_failed=" TIME_T_FMT
44 " next_try=" TIME_T_FMT " expired=%c\n",
45 now, retry_record->first_failed,
46 retry_record->next_try, retry_record->expired ? 'T' : 'F');
49 retry = retry_find_config(retry_key+2, domain,
50 retry_record->basic_errno, retry_record->more_errno);
52 if (retry && retry->rules)
54 retry_rule *last_rule;
55 for (last_rule = retry->rules; last_rule->next; last_rule = last_rule->next) ;
57 debug_printf(" received_time=" TIME_T_FMT " diff=%d timeout=%d\n",
58 received_time.tv_sec, (int)(now - received_time.tv_sec), last_rule->timeout);
59 address_timeout = (now - received_time.tv_sec > last_rule->timeout);
64 debug_printf("no retry rule found: assume timed out\n");
65 address_timeout = TRUE;
70 debug_printf("on queue longer than maximum retry for address - "
71 "allowing delivery\n");
73 return address_timeout;
79 retry_host_key_build(const host_item * host, BOOL incl_ip,
80 const uschar * portstring)
82 const uschar * s = host->name;
83 gstring * g = string_is_ip_address(s, NULL)
84 ? string_fmt_append(NULL, "T:[%s]", s) /* wrap a name which is a bare ip */
85 : string_fmt_append(NULL, "T:%s", s);
90 ? string_fmt_append(g, ":[%s]", s) /* wrap an ipv6 */
91 : string_fmt_append(g, ":%s", s);
94 g = string_cat(g, portstring);
96 gstring_release_unused(g);
97 return string_from_gstring(g);
101 /*************************************************
102 * Set status of a host+address item *
103 *************************************************/
105 /* This function is passed a host_item which contains a host name and an
106 IP address string. Its job is to set the status of the address if it is not
107 already set (indicated by hstatus_unknown). The possible values are:
109 hstatus_usable the address is not listed in the unusable tree, and does
110 not have a retry record, OR the time is past the next
111 try time, OR the message has been on the queue for more
112 than the maximum retry time for a failing host
114 hstatus_unusable the address is listed in the unusable tree, or does have
115 a retry record, and the time is not yet at the next retry
118 hstatus_unusable_expired as above, but also the retry time has expired
121 The reason a delivery is permitted when a message has been around for a very
122 long time is to allow the ultimate address timeout to operate after a delivery
123 failure. Otherwise some messages may stick around without being tried for too
126 If a host retry record is retrieved from the hints database, the time of last
127 trying is filled into the last_try field of the host block. If a host is
128 generally usable, a check is made to see if there is a retry delay on this
129 specific message at this host.
131 If a non-standard port is being used, it is added to the retry key.
134 domain the address domain
135 host pointer to a host item
136 portstring "" for standard port, ":xxxx" for a non-standard port
137 include_ip_address TRUE to include the address in the key - this is
138 usual, but sometimes is not wanted
139 retry_host_key where to put a pointer to the key for the host-specific
140 retry record, if one is read and the host is usable
141 retry_message_key where to put a pointer to the key for the message+host
142 retry record, if one is read and the host is usable
144 Returns: TRUE if the host has expired but is usable because
145 its retry time has come
149 retry_check_address(const uschar *domain, host_item *host, uschar *portstring,
150 BOOL include_ip_address,
151 const uschar **retry_host_key, const uschar **retry_message_key)
154 time_t now = time(NULL);
155 const uschar * host_key, * message_key;
156 open_db dbblock, * dbm_file;
158 dbdata_retry * host_retry_record, * message_retry_record;
160 *retry_host_key = *retry_message_key = NULL;
162 DEBUG(D_transport|D_retry) debug_printf("checking retry status of %s\n", host->name);
164 /* Do nothing if status already set; otherwise initialize status as usable. */
166 if (host->status != hstatus_unknown) return FALSE;
167 host->status = hstatus_usable;
169 /* Generate the host key for the unusable tree and the retry database. Ensure
170 host names are lower cased (that's what %S does).
171 Generate the message-specific key too.
172 Be sure to maintain lack-of-spaces in retry keys; exinext depends on it. */
174 host_key = retry_host_key_build(host, include_ip_address, portstring);
175 message_key = string_sprintf("%s:%s", host_key, message_id);
177 /* Search the tree of unusable IP addresses. This is filled in when deliveries
178 fail, because the retry database itself is not updated until the end of all
179 deliveries (so as to do it all in one go). The tree records addresses that have
180 become unusable during this delivery process (i.e. those that will get put into
181 the retry database when it is updated). */
183 if ((node = tree_search(tree_unusable, host_key)))
185 DEBUG(D_transport|D_retry) debug_printf("found in tree of unusables\n");
186 host->status = (node->data.val > 255)?
187 hstatus_unusable_expired : hstatus_unusable;
188 host->why = node->data.val & 255;
192 /* Open the retry database, giving up if there isn't one. Otherwise, search for
193 the retry records, and then close the database again. */
195 if (!(dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE, TRUE)))
197 DEBUG(D_deliver|D_retry|D_hints_lookup)
198 debug_printf("no retry data available\n");
201 host_retry_record = dbfn_read(dbm_file, host_key);
202 message_retry_record = dbfn_read(dbm_file, message_key);
203 dbfn_close(dbm_file);
205 /* Ignore the data if it is too old - too long since it was written */
207 if (!host_retry_record)
209 DEBUG(D_transport|D_retry) debug_printf("no host retry record\n");
211 else if (now - host_retry_record->time_stamp > retry_data_expire)
213 host_retry_record = NULL;
214 DEBUG(D_transport|D_retry) debug_printf("host retry record too old\n");
217 if (!message_retry_record)
219 DEBUG(D_transport|D_retry) debug_printf("no message retry record\n");
221 else if (now - message_retry_record->time_stamp > retry_data_expire)
223 message_retry_record = NULL;
224 DEBUG(D_transport|D_retry) debug_printf("message retry record too old\n");
227 /* If there's a host-specific retry record, check for reaching the retry
228 time (or forcing). If not, and the host is not expired, check for the message
229 having been around for longer than the maximum retry time for this host or
230 address. Allow the delivery if it has. Otherwise set the appropriate unusable
231 flag and return FALSE. Otherwise arrange to return TRUE if this is an expired
234 if (host_retry_record)
236 *retry_host_key = host_key;
238 /* We have not reached the next try time. Check for the ultimate address
239 timeout if the host has not expired. */
241 if (now < host_retry_record->next_try && !f.deliver_force)
243 if (!host_retry_record->expired &&
244 retry_ultimate_address_timeout(host_key, domain,
245 host_retry_record, now))
248 /* We have not hit the ultimate address timeout; host is unusable. */
250 host->status = (host_retry_record->expired)?
251 hstatus_unusable_expired : hstatus_unusable;
252 host->why = hwhy_retry;
253 host->last_try = host_retry_record->last_try;
257 /* Host is usable; set return TRUE if expired. */
259 yield = host_retry_record->expired;
262 /* It's OK to try the host. If there's a message-specific retry record, check
263 for reaching its retry time (or forcing). If not, mark the host unusable,
264 unless the ultimate address timeout has been reached. */
266 if (message_retry_record)
268 *retry_message_key = message_key;
269 if (now < message_retry_record->next_try && !f.deliver_force)
271 if (!retry_ultimate_address_timeout(host_key, domain,
272 message_retry_record, now))
274 host->status = hstatus_unusable;
275 host->why = hwhy_retry;
287 /*************************************************
288 * Add a retry item to an address *
289 *************************************************/
291 /* Retry items are chained onto an address when it is deferred either by router
292 or by a transport, or if it succeeds or fails and there was a previous retry
293 item that now needs to be deleted. Sometimes there can be both kinds of item:
294 for example, if routing was deferred but then succeeded, and delivery then
295 deferred. In that case there is a delete item for the routing retry, and an
296 updating item for the delivery.
298 (But note that that is only visible at the outer level, because in remote
299 delivery subprocesses, the address starts "clean", with no retry items carried
302 These items are used at the end of a delivery attempt to update the retry
303 database. The keys start R: for routing delays and T: for transport delays.
306 addr the address block onto which to hang the item
308 flags delete, host, and message flags, copied into the block
314 retry_add_item(address_item * addr, const uschar * key, int flags)
316 retry_item * rti = store_get(sizeof(retry_item), GET_UNTAINTED);
317 host_item * host = addr->host_used;
319 rti->next = addr->retries;
322 rti->basic_errno = addr->basic_errno;
323 rti->more_errno = addr->more_errno;
325 ? string_sprintf("H=%s [%s]: %s", host->name, host->address, addr->message)
329 DEBUG(D_transport|D_retry)
331 int letter = rti->more_errno & 255;
332 debug_printf("added retry item for %s: errno=%d more_errno=", rti->key,
334 if (letter == 'A' || letter == 'M')
335 debug_printf("%d,%c", (rti->more_errno >> 8) & 255, letter);
337 debug_printf("%d", rti->more_errno);
338 debug_printf(" flags=%d\n", flags);
344 /*************************************************
345 * Find retry configuration data *
346 *************************************************/
348 /* Search the in-store retry information for the first retry item that applies
349 to a given destination. If the key contains an @ we are probably handling a
350 local delivery and have a complete address to search for; this happens when
351 retry_use_local_part is set on a router. Otherwise, the key is likely to be a
352 host name for a remote delivery, or a domain name for a local delivery. We
353 prepend *@ on the front of it so that it will match a retry item whose address
354 item pattern is independent of the local part. The alternate key, if set, is
355 always just a domain, so we treat it likewise.
358 key key for which retry info is wanted
359 alternate alternative key, always just a domain
360 basic_errno specific error predicate on the retry rule, or zero
361 more_errno additional data for errno predicate
363 Returns: pointer to retry rule, or NULL
367 retry_find_config(const uschar * key, const uschar * alternate, int basic_errno,
370 const uschar * colon = Ustrchr(key, ':');
371 retry_config * yield;
373 /* If there's a colon in the key, there are two possibilities:
375 (1) This is a key for a host, ip address, and possibly port, in the format
379 In this case, we copy the host name (which could be an [ip], including
380 being an [ipv6], and we drop the []).
382 (2) This is a key for a pipe, file, or autoreply delivery, in the format
384 pipe-or-file-or-auto:x@y
386 where x@y is the original address that provoked the delivery. The pipe or
387 file or auto will start with | or / or >, whereas a host name will start
388 with a letter or a digit. In this case we want to use the original address
389 to search for a retry rule. */
393 ? string_copyn(key, colon-key) /* the hostname */
395 ? string_copyn(key+1, Ustrchr(key, ']')-1-key) /* the ip */
396 : Ustrrchr(key, ':') + 1; /* Take from the last colon */
398 /* Sort out the keys */
400 if (!Ustrchr(key, '@')) key = string_sprintf("*@%s", key);
401 if (alternate) alternate = string_sprintf("*@%s", alternate);
403 /* Scan the configured retry items. */
405 for (yield = retries; yield; yield = yield->next)
407 const uschar *plist = yield->pattern;
408 const uschar *slist = yield->senders;
410 /* If a specific error is set for this item, check that we are handling that
411 specific error, and if so, check any additional error information if
414 if (yield->basic_errno != 0)
416 /* Special code is required for quota errors, as these can either be system
417 quota errors, or Exim's own quota imposition, which has a different error
418 number. Full partitions are also treated in the same way as quota errors.
421 if (yield->basic_errno == ERRNO_EXIMQUOTA)
423 if ((basic_errno != ERRNO_EXIMQUOTA && basic_errno != errno_quota &&
424 basic_errno != ENOSPC) ||
425 (yield->more_errno != 0 && yield->more_errno > more_errno))
429 /* The TLSREQUIRED error also covers TLSFAILURE. These are subtly different
430 errors, but not worth separating at this level. */
432 else if (yield->basic_errno == ERRNO_TLSREQUIRED)
434 if (basic_errno != ERRNO_TLSREQUIRED && basic_errno != ERRNO_TLSFAILURE)
438 /* Handle 4xx responses to MAIL, RCPT, or DATA. The code that was received
439 is in the 2nd least significant byte of more_errno (with 400 subtracted).
440 The required value is coded in the 2nd least significant byte of the
441 yield->more_errno field as follows:
444 >= 100 => the decade must match the value less 100
445 < 100 => the exact value must match
448 else if (yield->basic_errno == ERRNO_MAIL4XX ||
449 yield->basic_errno == ERRNO_RCPT4XX ||
450 yield->basic_errno == ERRNO_DATA4XX)
453 if (basic_errno != yield->basic_errno) continue;
454 wanted = (yield->more_errno >> 8) & 255;
457 int evalue = (more_errno >> 8) & 255;
460 if ((evalue/10)*10 != wanted - 100) continue;
462 else if (evalue != wanted) continue;
466 /* There are some special cases for timeouts */
468 else if (yield->basic_errno == ETIMEDOUT)
470 if (basic_errno != ETIMEDOUT) continue;
472 /* Just RTEF_CTOUT in the rule => don't care about 'A'/'M' addresses */
473 if (yield->more_errno == RTEF_CTOUT)
475 if ((more_errno & RTEF_CTOUT) == 0) continue;
478 else if (yield->more_errno != 0)
480 int cf_errno = more_errno;
481 if ((yield->more_errno & RTEF_CTOUT) == 0) cf_errno &= ~RTEF_CTOUT;
482 if (yield->more_errno != cf_errno) continue;
486 /* Default checks for exact match */
490 if (yield->basic_errno != basic_errno ||
491 (yield->more_errno != 0 && yield->more_errno != more_errno))
496 /* If the "senders" condition is set, check it. Note that sender_address may
497 be null during -brt checking, in which case we do not use this rule. */
501 || match_address_list_basic(sender_address, &slist, 0) != OK
505 /* Check for a match between the address list item at the start of this retry
506 rule and either the main or alternate keys. */
508 if ( match_address_list_basic(key, &plist, UCHAR_MAX+1) == OK
510 && match_address_list_basic(alternate, &plist, UCHAR_MAX+1) == OK
521 /*************************************************
522 * Update retry database *
523 *************************************************/
525 /* Update the retry data for any directing/routing/transporting that was
526 deferred, or delete it for those that succeeded after a previous defer. This is
527 done all in one go to minimize opening/closing/locking of the database file.
529 Note that, because SMTP delivery involves a list of destinations to try, there
530 may be defer-type retry information for some of them even when the message was
531 successfully delivered. Likewise if it eventually failed.
533 This function may move addresses from the defer to the failed queue if the
534 ultimate retry time has expired.
537 addr_defer queue of deferred addresses
538 addr_failed queue of failed addresses
539 addr_succeed queue of successful addresses
545 retry_update(address_item ** addr_defer, address_item ** addr_failed,
546 address_item ** addr_succeed)
549 open_db *dbm_file = NULL;
550 time_t now = time(NULL);
552 DEBUG(D_retry) debug_printf("Processing retry items\n");
554 /* Three-times loop to handle succeeded, failed, and deferred addresses.
555 Deferred addresses must be handled after failed ones, because some may be moved
556 to the failed chain if they have timed out. */
558 for (int i = 0; i < 3; i++)
560 address_item * endaddr, *addr;
561 address_item * last_first = NULL;
562 address_item ** paddr = i==0 ? addr_succeed : i==1 ? addr_failed : addr_defer;
563 address_item ** saved_paddr = NULL;
565 DEBUG(D_retry) debug_printf("%s addresses:\n",
566 i == 0 ? "Succeeded" : i == 1 ? "Failed" : "Deferred");
568 /* Loop for each address on the chain. For deferred addresses, the whole
569 address times out unless one of its retry addresses has a retry rule that
570 hasn't yet timed out. Deferred addresses should not be requesting deletion
571 of retry items, but just in case they do by accident, treat that case
574 As well as handling the addresses themselves, we must also process any
575 retry items for any parent addresses - these are typically "delete" items,
576 because the parent must have succeeded in order to generate the child. */
578 while ((endaddr = *paddr))
580 BOOL timed_out = FALSE;
582 for (addr = endaddr; addr; addr = addr->parent)
584 int update_count = 0;
585 int timedout_count = 0;
587 DEBUG(D_retry) debug_printf(" %s%s\n", addr->address,
588 addr->retries ? "" : ": no retry items");
590 /* Loop for each retry item. */
592 for (retry_item * rti = addr->retries; rti; rti = rti->next)
595 int message_length, message_space, failing_interval, next_try;
596 retry_rule *rule, *final_rule;
598 dbdata_retry *retry_record;
600 /* Open the retry database if it is not already open; failure to open
601 the file is logged, but otherwise ignored - deferred addresses will
602 get retried at the next opportunity. Not opening earlier than this saves
603 opening if no addresses have retry items - common when none have yet
604 reached their retry next try time. */
607 dbm_file = dbfn_open(US"retry", O_RDWR, &dbblock, TRUE, TRUE);
611 DEBUG(D_deliver|D_retry|D_hints_lookup)
612 debug_printf("retry database not available for updating\n");
616 /* If there are no deferred addresses, that is, if this message is
617 completing, and the retry item is for a message-specific SMTP error,
618 force it to be deleted, because there's no point in keeping data for
619 no-longer-existing messages. This situation can occur when a domain has
620 two hosts and a message-specific error occurs for the first of them,
621 but the address gets delivered to the second one. This optimization
622 doesn't succeed in cleaning out all the dead entries, but it helps. */
624 if (!*addr_defer && rti->flags & rf_message)
625 rti->flags |= rf_delete;
627 /* Handle the case of a request to delete the retry info for this
630 if (rti->flags & rf_delete)
632 (void)dbfn_delete(dbm_file, rti->key);
634 debug_printf("deleted retry information for %s\n", rti->key);
638 /* Count the number of non-delete retry items. This is so that we
639 can compare it to the count of timed_out ones, to check whether
640 all are timed out. */
644 /* Get the retry information for this destination and error code, if
645 any. If this item is for a remote host with ip address, then pass
646 the domain name as an alternative to search for. If no retry
647 information is found, we can't generate a retry time, so there is
648 no point updating the database. This retry item is timed out. */
650 if (!(retry = retry_find_config(rti->key + 2,
651 rti->flags & rf_host ? addr->domain : NULL,
652 rti->basic_errno, rti->more_errno)))
654 DEBUG(D_retry) debug_printf("No configured retry item for %s%s%s\n",
656 rti->flags & rf_host ? US" or " : US"",
657 rti->flags & rf_host ? addr->domain : US"");
658 if (addr == endaddr) timedout_count++;
663 if (rti->flags & rf_host)
664 debug_printf("retry for %s (%s) = %s %d %d\n", rti->key,
665 addr->domain, retry->pattern, retry->basic_errno,
668 debug_printf("retry for %s = %s %d %d\n", rti->key, retry->pattern,
669 retry->basic_errno, retry->more_errno);
671 /* Set up the message for the database retry record. Because DBM
672 records have a maximum data length, we enforce a limit. There isn't
673 much point in keeping a huge message here, anyway. */
675 message = rti->basic_errno > 0
676 ? US strerror(rti->basic_errno)
678 ? US string_printing(rti->message)
680 message_length = Ustrlen(message);
681 if (message_length > EXIM_DB_RLIMIT)
684 debug_printf_indent("truncating message from %u to %u bytes\n",
685 message_length, EXIM_DB_RLIMIT);
686 message_length = EXIM_DB_RLIMIT;
689 /* Read a retry record from the database or construct a new one.
690 Ignore an old one if it is too old since it was last updated. */
692 retry_record = dbfn_read_with_length(dbm_file, rti->key,
695 && now - retry_record->time_stamp > retry_data_expire)
700 retry_record = store_get(sizeof(dbdata_retry) + message_length,
702 message_space = message_length;
703 retry_record->first_failed = now;
704 retry_record->last_try = now;
705 retry_record->next_try = now;
706 retry_record->expired = FALSE;
707 retry_record->text[0] = 0; /* just in case */
709 else message_space -= sizeof(dbdata_retry);
711 /* Compute how long this destination has been failing */
713 failing_interval = now - retry_record->first_failed;
714 DEBUG(D_retry) debug_printf("failing_interval=%d message_age=%d\n",
715 failing_interval, message_age);
717 /* For a non-host error, if the message has been on the queue longer
718 than the recorded time of failure, use the message's age instead. This
719 can happen when some messages can be delivered and others cannot; a
720 successful delivery will reset the first_failed time, and this can lead
721 to a failing message being retried too often. */
723 if (!(rti->flags & rf_host) && message_age > failing_interval)
724 failing_interval = message_age;
726 /* Search for the current retry rule. The cutoff time of the
727 last rule is handled differently to the others. The rule continues
728 to operate for ever (the global maximum interval will eventually
729 limit the gaps) but its cutoff time determines when an individual
730 destination times out. If there are no retry rules, the destination
731 always times out, but we can't compute a retry time. */
734 for (rule = retry->rules; rule; rule = rule->next)
736 if (failing_interval <= rule->timeout) break;
740 /* If there's an un-timed out rule, the destination has not
741 yet timed out, so the address as a whole has not timed out (but we are
742 interested in this only for the end address). Make sure the expired
743 flag is false (can be forced via fixdb from outside, but ensure it is
744 consistent with the rules whenever we go through here). */
747 retry_record->expired = FALSE;
749 /* Otherwise, set the retry timeout expired, and set the final rule
750 as the one from which to compute the next retry time. Subsequent
751 messages will fail immediately until the retry time is reached (unless
752 there are other, still active, retries). */
757 retry_record->expired = TRUE;
758 if (addr == endaddr) timedout_count++;
761 /* There is a special case to consider when some messages get through
762 to a destination and others don't. This can happen locally when a
763 large message pushes a user over quota, and it can happen remotely
764 when a machine is on a dodgy Internet connection. The messages that
765 get through wipe the retry information, causing those that don't to
766 stay on the queue longer than the final retry time. In order to
767 avoid this, we check, using the time of arrival of the message, to
768 see if it has been on the queue for more than the final cutoff time,
769 and if so, cause this retry item to time out, and the retry time to
770 be set to "now" so that any subsequent messages in the same condition
771 also get tried. We search for the last rule onwards from the one that
772 is in use. If there are no retry rules for the item, rule will be null
773 and timedout_count will already have been updated.
775 This implements "timeout this rule if EITHER the host (or routing or
776 directing) has been failing for more than the maximum time, OR if the
777 message has been on the queue for more than the maximum time."
779 February 2006: It is possible that this code is no longer needed
780 following the change to the retry calculation to use the message age if
781 it is larger than the time since first failure. It may be that the
782 expired flag is always set when the other conditions are met. However,
783 this is a small bit of code, and it does no harm to leave it in place,
786 if ( received_time.tv_sec <= retry_record->first_failed
788 && !retry_record->expired
791 retry_rule *last_rule;
792 for (last_rule = rule; last_rule->next; last_rule = last_rule->next)
794 if (now - received_time.tv_sec > last_rule->timeout)
796 DEBUG(D_retry) debug_printf("on queue longer than maximum retry\n");
802 /* Compute the next try time from the rule, subject to the global
803 maximum, and update the retry database. If rule == NULL it means
804 there were no rules at all (and the timeout will be set expired),
805 or we have a message that is older than the final timeout. In this
806 case set the next retry time to now, so that one delivery attempt
807 happens for subsequent messages. */
813 if (rule->rule == 'F')
814 next_try = now + rule->p1;
815 else /* rule = 'G' or 'H' */
817 int last_predicted_gap =
818 retry_record->next_try - retry_record->last_try;
819 int last_actual_gap = now - retry_record->last_try;
820 int lastgap = (last_predicted_gap < last_actual_gap)?
821 last_predicted_gap : last_actual_gap;
822 int next_gap = (lastgap * rule->p2)/1000;
823 if (rule->rule == 'G')
824 next_try = now + ((lastgap < rule->p1)? rule->p1 : next_gap);
825 else /* The 'H' rule */
827 next_try = now + rule->p1;
828 if (next_gap > rule->p1)
829 next_try += random_number(next_gap - rule->p1)/2 +
830 (next_gap - rule->p1)/2;
835 /* Impose a global retry max */
837 if (next_try - now > retry_interval_max)
838 next_try = now + retry_interval_max;
840 /* If the new message length is greater than the previous one, we have
841 to copy the record first. If we're using an old one, the read used
842 tainted memory so we're ok to write into it. */
844 if (message_length > message_space)
846 dbdata_retry * newr =
847 store_get(sizeof(dbdata_retry) + message_length, message);
848 memcpy(newr, retry_record, sizeof(dbdata_retry));
852 /* Set up the retry record; message_length may be less than the string
853 length for very long error strings. */
855 retry_record->last_try = now;
856 retry_record->next_try = next_try;
857 retry_record->basic_errno = rti->basic_errno;
858 retry_record->more_errno = rti->more_errno;
859 Ustrncpy(retry_record->text, message, message_length);
860 retry_record->text[message_length] = 0;
864 int letter = retry_record->more_errno & 255;
865 debug_printf("Writing retry data for %s\n", rti->key);
866 debug_printf(" first failed=%d last try=%d next try=%d expired=%d\n",
867 (int)retry_record->first_failed, (int)retry_record->last_try,
868 (int)retry_record->next_try, retry_record->expired);
869 debug_printf(" errno=%d more_errno=", retry_record->basic_errno);
870 if (letter == 'A' || letter == 'M')
871 debug_printf("%d,%c", (retry_record->more_errno >> 8) & 255,
874 debug_printf("%d", retry_record->more_errno);
875 debug_printf(" %s\n", retry_record->text);
878 (void)dbfn_write(dbm_file, rti->key, retry_record,
879 sizeof(dbdata_retry) + message_length);
880 } /* Loop for each retry item */
882 /* If all the non-delete retry items are timed out, the address is
883 timed out, provided that we didn't skip any hosts because their retry
884 time was not reached (or because of hosts_max_try). */
886 if (update_count > 0 && update_count == timedout_count)
887 if (!testflag(endaddr, af_retry_skipped))
889 DEBUG(D_retry) debug_printf("timed out: all retries expired\n");
894 debug_printf("timed out but some hosts were skipped\n");
895 } /* Loop for an address and its parents */
897 /* If this is a deferred address, and retry processing was requested by
898 means of one or more retry items, and they all timed out, move the address
899 to the failed queue, and restart this loop without updating paddr.
901 If there were several addresses batched in the same remote delivery, only
902 the original top one will have host retry items attached to it, but we want
903 to handle all the same. Each will have a pointer back to its "top" address,
904 and they will now precede the item with the retries because addresses are
905 inverted when added to these final queues. We have saved information about
906 them in passing (below) so they can all be cut out at once. */
908 if (i == 2) /* Handling defers */
910 if (endaddr->retries && timed_out)
912 if (last_first == endaddr) paddr = saved_paddr;
914 *paddr = endaddr->next;
916 endaddr->next = *addr_failed;
919 for (;; addr = addr->next)
921 setflag(addr, af_retry_timedout);
922 addr->message = addr->message
923 ? string_sprintf("%s: retry timeout exceeded", addr->message)
924 : US"retry timeout exceeded";
925 addr->user_message = addr->user_message
926 ? string_sprintf("%s: retry timeout exceeded", addr->user_message)
927 : US"retry timeout exceeded";
928 log_write(0, LOG_MAIN, "** %s%s%s%s: retry timeout exceeded",
930 addr->parent ? US" <" : US"",
931 addr->parent ? addr->parent->address : US"",
932 addr->parent ? US">" : US"");
934 if (addr == endaddr) break;
937 continue; /* Restart from changed *paddr */
940 /* This address is to remain on the defer chain. If it has a "first"
941 pointer, save the pointer to it in case we want to fail the set of
942 addresses when we get to the first one. */
944 if (endaddr->first != last_first)
946 last_first = endaddr->first;
951 /* All cases (succeed, fail, defer left on queue) */
953 paddr = &(endaddr->next); /* Advance to next address */
954 } /* Loop for all addresses */
955 } /* Loop for succeed, fail, defer */
957 /* Close and unlock the database */
959 if (dbm_file) dbfn_close(dbm_file);
961 DEBUG(D_retry) debug_printf("end of retry processing\n");