1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* Functions concerned with retrying unsuccessful deliveries. */
15 /*************************************************
16 * Check the ultimate address timeout *
17 *************************************************/
19 /* This function tests whether a message has been on the queue longer than
20 the maximum retry time for a particular host or address.
23 retry_key the key to look up a retry rule
24 domain the domain to look up a domain retry rule
25 retry_record contains error information for finding rule
28 Returns: TRUE if the ultimate timeout has been reached
32 retry_ultimate_address_timeout(uschar *retry_key, const uschar *domain,
33 dbdata_retry *retry_record, time_t now)
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);
46 retry_find_config(retry_key+2, domain,
47 retry_record->basic_errno, retry_record->more_errno);
49 if (retry != NULL && retry->rules != NULL)
51 retry_rule *last_rule;
52 for (last_rule = retry->rules;
53 last_rule->next != NULL;
54 last_rule = last_rule->next);
56 debug_printf(" received_time=%d diff=%d timeout=%d\n",
57 received_time, (int)(now - received_time), last_rule->timeout);
58 address_timeout = (now - received_time > last_rule->timeout);
63 debug_printf("no retry rule found: assume timed out\n");
64 address_timeout = TRUE;
69 debug_printf("on queue longer than maximum retry for address - "
70 "allowing delivery\n");
72 return address_timeout;
77 /*************************************************
78 * Set status of a host+address item *
79 *************************************************/
81 /* This function is passed a host_item which contains a host name and an
82 IP address string. Its job is to set the status of the address if it is not
83 already set (indicated by hstatus_unknown). The possible values are:
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
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
94 hstatus_unusable_expired as above, but also the retry time has expired
97 The reason a delivery is permitted when a message has been around for a very
98 long time is to allow the ultimate address timeout to operate after a delivery
99 failure. Otherwise some messages may stick around without being tried for too
102 If a host retry record is retrieved from the hints database, the time of last
103 trying is filled into the last_try field of the host block. If a host is
104 generally usable, a check is made to see if there is a retry delay on this
105 specific message at this host.
107 If a non-standard port is being used, it is added to the retry key.
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
120 Returns: TRUE if the host has expired but is usable because
121 its retry time has come
125 retry_check_address(const uschar *domain, host_item *host, uschar *portstring,
126 BOOL include_ip_address, uschar **retry_host_key, uschar **retry_message_key)
129 time_t now = time(NULL);
130 uschar *host_key, *message_key;
134 dbdata_retry *host_retry_record, *message_retry_record;
136 *retry_host_key = *retry_message_key = NULL;
138 DEBUG(D_transport|D_retry) debug_printf("checking status of %s\n", host->name);
140 /* Do nothing if status already set; otherwise initialize status as usable. */
142 if (host->status != hstatus_unknown) return FALSE;
143 host->status = hstatus_usable;
145 /* Generate the host key for the unusable tree and the retry database. Ensure
146 host names are lower cased (that's what %S does). */
148 host_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);
152 /* Generate the message-specific key */
154 message_key = string_sprintf("%s:%s", host_key, message_id);
156 /* Search the tree of unusable IP addresses. This is filled in when deliveries
157 fail, because the retry database itself is not updated until the end of all
158 deliveries (so as to do it all in one go). The tree records addresses that have
159 become unusable during this delivery process (i.e. those that will get put into
160 the retry database when it is updated). */
162 node = tree_search(tree_unusable, host_key);
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;
172 /* Open the retry database, giving up if there isn't one. Otherwise, search for
173 the retry records, and then close the database again. */
175 if ((dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE)) == NULL)
177 DEBUG(D_deliver|D_retry|D_hints_lookup)
178 debug_printf("no retry data available\n");
181 host_retry_record = dbfn_read(dbm_file, host_key);
182 message_retry_record = dbfn_read(dbm_file, message_key);
183 dbfn_close(dbm_file);
185 /* Ignore the data if it is too old - too long since it was written */
187 if (host_retry_record == NULL)
189 DEBUG(D_transport|D_retry) debug_printf("no host retry record\n");
191 else if (now - host_retry_record->time_stamp > retry_data_expire)
193 host_retry_record = NULL;
194 DEBUG(D_transport|D_retry) debug_printf("host retry record too old\n");
197 if (message_retry_record == NULL)
199 DEBUG(D_transport|D_retry) debug_printf("no message retry record\n");
201 else if (now - message_retry_record->time_stamp > retry_data_expire)
203 message_retry_record = NULL;
204 DEBUG(D_transport|D_retry) debug_printf("message retry record too old\n");
207 /* If there's a host-specific retry record, check for reaching the retry
208 time (or forcing). If not, and the host is not expired, check for the message
209 having been around for longer than the maximum retry time for this host or
210 address. Allow the delivery if it has. Otherwise set the appropriate unusable
211 flag and return FALSE. Otherwise arrange to return TRUE if this is an expired
214 if (host_retry_record != NULL)
216 *retry_host_key = host_key;
218 /* We have not reached the next try time. Check for the ultimate address
219 timeout if the host has not expired. */
221 if (now < host_retry_record->next_try && !deliver_force)
223 if (!host_retry_record->expired &&
224 retry_ultimate_address_timeout(host_key, domain,
225 host_retry_record, now))
228 /* We have not hit the ultimate address timeout; host is unusable. */
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;
237 /* Host is usable; set return TRUE if expired. */
239 yield = host_retry_record->expired;
242 /* It's OK to try the host. If there's a message-specific retry record, check
243 for reaching its retry time (or forcing). If not, mark the host unusable,
244 unless the ultimate address timeout has been reached. */
246 if (message_retry_record != NULL)
248 *retry_message_key = message_key;
249 if (now < message_retry_record->next_try && !deliver_force)
251 if (!retry_ultimate_address_timeout(host_key, domain,
252 message_retry_record, now))
254 host->status = hstatus_unusable;
255 host->why = hwhy_retry;
267 /*************************************************
268 * Add a retry item to an address *
269 *************************************************/
271 /* Retry items are chained onto an address when it is deferred either by router
272 or by a transport, or if it succeeds or fails and there was a previous retry
273 item that now needs to be deleted. Sometimes there can be both kinds of item:
274 for example, if routing was deferred but then succeeded, and delivery then
275 deferred. In that case there is a delete item for the routing retry, and an
276 updating item for the delivery.
278 (But note that that is only visible at the outer level, because in remote
279 delivery subprocesses, the address starts "clean", with no retry items carried
282 These items are used at the end of a delivery attempt to update the retry
283 database. The keys start R: for routing delays and T: for transport delays.
286 addr the address block onto which to hang the item
288 flags delete, host, and message flags, copied into the block
294 retry_add_item(address_item *addr, uschar *key, int flags)
296 retry_item *rti = store_get(sizeof(retry_item));
297 host_item * host = addr->host_used;
298 rti->next = addr->retries;
301 rti->basic_errno = addr->basic_errno;
302 rti->more_errno = addr->more_errno;
304 ? string_sprintf("H=%s [%s]: %s", host->name, host->address, addr->message)
308 DEBUG(D_transport|D_retry)
310 int letter = rti->more_errno & 255;
311 debug_printf("added retry item for %s: errno=%d more_errno=", rti->key,
313 if (letter == 'A' || letter == 'M')
314 debug_printf("%d,%c", (rti->more_errno >> 8) & 255, letter);
316 debug_printf("%d", rti->more_errno);
317 debug_printf(" flags=%d\n", flags);
323 /*************************************************
324 * Find retry configuration data *
325 *************************************************/
327 /* Search the in-store retry information for the first retry item that applies
328 to a given destination. If the key contains an @ we are probably handling a
329 local delivery and have a complete address to search for; this happens when
330 retry_use_local_part is set on a router. Otherwise, the key is likely to be a
331 host name for a remote delivery, or a domain name for a local delivery. We
332 prepend *@ on the front of it so that it will match a retry item whose address
333 item pattern is independent of the local part. The alternate key, if set, is
334 always just a domain, so we treat it likewise.
337 key key for which retry info is wanted
338 alternate alternative key, always just a domain
339 basic_errno specific error predicate on the retry rule, or zero
340 more_errno additional data for errno predicate
342 Returns: pointer to retry rule, or NULL
346 retry_find_config(const uschar *key, const uschar *alternate, int basic_errno,
350 const uschar *colon = Ustrchr(key, ':');
353 /* If there's a colon in the key, there are two possibilities:
355 (1) This is a key for a host, ip address, and possibly port, in the format
359 In this case, we copy the host name.
361 (2) This is a key for a pipe, file, or autoreply delivery, in the format
363 pipe-or-file-or-auto:x@y
365 where x@y is the original address that provoked the delivery. The pipe or
366 file or auto will start with | or / or >, whereas a host name will start
367 with a letter or a digit. In this case we want to use the original address
368 to search for a retry rule. */
372 ? string_copyn(key, colon-key) /* the hostname */
373 : Ustrrchr(key, ':') + 1; /* Take from the last colon */
375 /* Sort out the keys */
377 if (!Ustrchr(key, '@')) key = string_sprintf("*@%s", key);
378 if (alternate) alternate = string_sprintf("*@%s", alternate);
380 /* Scan the configured retry items. */
382 for (yield = retries; yield != NULL; yield = yield->next)
384 const uschar *plist = yield->pattern;
385 const uschar *slist = yield->senders;
387 /* If a specific error is set for this item, check that we are handling that
388 specific error, and if so, check any additional error information if
391 if (yield->basic_errno != 0)
393 /* Special code is required for quota errors, as these can either be system
394 quota errors, or Exim's own quota imposition, which has a different error
395 number. Full partitions are also treated in the same way as quota errors.
398 if (yield->basic_errno == ERRNO_EXIMQUOTA)
400 if ((basic_errno != ERRNO_EXIMQUOTA && basic_errno != errno_quota &&
401 basic_errno != ENOSPC) ||
402 (yield->more_errno != 0 && yield->more_errno > more_errno))
406 /* The TLSREQUIRED error also covers TLSFAILURE. These are subtly different
407 errors, but not worth separating at this level. */
409 else if (yield->basic_errno == ERRNO_TLSREQUIRED)
411 if (basic_errno != ERRNO_TLSREQUIRED && basic_errno != ERRNO_TLSFAILURE)
415 /* Handle 4xx responses to MAIL, RCPT, or DATA. The code that was received
416 is in the 2nd least significant byte of more_errno (with 400 subtracted).
417 The required value is coded in the 2nd least significant byte of the
418 yield->more_errno field as follows:
421 >= 100 => the decade must match the value less 100
422 < 100 => the exact value must match
425 else if (yield->basic_errno == ERRNO_MAIL4XX ||
426 yield->basic_errno == ERRNO_RCPT4XX ||
427 yield->basic_errno == ERRNO_DATA4XX)
430 if (basic_errno != yield->basic_errno) continue;
431 wanted = (yield->more_errno >> 8) & 255;
434 int evalue = (more_errno >> 8) & 255;
437 if ((evalue/10)*10 != wanted - 100) continue;
439 else if (evalue != wanted) continue;
443 /* There are some special cases for timeouts */
445 else if (yield->basic_errno == ETIMEDOUT)
447 if (basic_errno != ETIMEDOUT) continue;
449 /* Just RTEF_CTOUT in the rule => don't care about 'A'/'M' addresses */
450 if (yield->more_errno == RTEF_CTOUT)
452 if ((more_errno & RTEF_CTOUT) == 0) continue;
455 else if (yield->more_errno != 0)
457 int cf_errno = more_errno;
458 if ((yield->more_errno & RTEF_CTOUT) == 0) cf_errno &= ~RTEF_CTOUT;
459 if (yield->more_errno != cf_errno) continue;
463 /* Default checks for exact match */
467 if (yield->basic_errno != basic_errno ||
468 (yield->more_errno != 0 && yield->more_errno != more_errno))
473 /* If the "senders" condition is set, check it. Note that sender_address may
474 be null during -brt checking, in which case we do not use this rule. */
476 if (slist != NULL && (sender_address == NULL ||
477 match_address_list(sender_address, TRUE, TRUE, &slist, NULL, -1, 0,
481 /* Check for a match between the address list item at the start of this retry
482 rule and either the main or alternate keys. */
484 if (match_address_list(key, TRUE, TRUE, &plist, NULL, -1, UCHAR_MAX+1,
486 (alternate != NULL &&
487 match_address_list(alternate, TRUE, TRUE, &plist, NULL, -1,
488 UCHAR_MAX+1, NULL) == OK))
498 /*************************************************
499 * Update retry database *
500 *************************************************/
502 /* Update the retry data for any directing/routing/transporting that was
503 deferred, or delete it for those that succeeded after a previous defer. This is
504 done all in one go to minimize opening/closing/locking of the database file.
506 Note that, because SMTP delivery involves a list of destinations to try, there
507 may be defer-type retry information for some of them even when the message was
508 successfully delivered. Likewise if it eventually failed.
510 This function may move addresses from the defer to the failed queue if the
511 ultimate retry time has expired.
514 addr_defer queue of deferred addresses
515 addr_failed queue of failed addresses
516 addr_succeed queue of successful addresses
522 retry_update(address_item **addr_defer, address_item **addr_failed,
523 address_item **addr_succeed)
526 open_db *dbm_file = NULL;
527 time_t now = time(NULL);
530 DEBUG(D_retry) debug_printf("Processing retry items\n");
532 /* Three-times loop to handle succeeded, failed, and deferred addresses.
533 Deferred addresses must be handled after failed ones, because some may be moved
534 to the failed chain if they have timed out. */
536 for (i = 0; i < 3; i++)
538 address_item *endaddr, *addr;
539 address_item *last_first = NULL;
540 address_item **paddr = (i==0)? addr_succeed :
541 (i==1)? addr_failed : addr_defer;
542 address_item **saved_paddr = NULL;
544 DEBUG(D_retry) debug_printf("%s addresses:\n", (i == 0)? "Succeeded" :
545 (i == 1)? "Failed" : "Deferred");
547 /* Loop for each address on the chain. For deferred addresses, the whole
548 address times out unless one of its retry addresses has a retry rule that
549 hasn't yet timed out. Deferred addresses should not be requesting deletion
550 of retry items, but just in case they do by accident, treat that case
553 As well as handling the addresses themselves, we must also process any
554 retry items for any parent addresses - these are typically "delete" items,
555 because the parent must have succeeded in order to generate the child. */
557 while ((endaddr = *paddr) != NULL)
559 BOOL timed_out = FALSE;
562 for (addr = endaddr; addr != NULL; addr = addr->parent)
564 int update_count = 0;
565 int timedout_count = 0;
567 DEBUG(D_retry) debug_printf("%s%s\n", addr->address, (addr->retries == NULL)?
568 ": no retry items" : "");
570 /* Loop for each retry item. */
572 for (rti = addr->retries; rti != NULL; rti = rti->next)
575 int message_length, message_space, failing_interval, next_try;
576 retry_rule *rule, *final_rule;
578 dbdata_retry *retry_record;
580 /* Open the retry database if it is not already open; failure to open
581 the file is logged, but otherwise ignored - deferred addresses will
582 get retried at the next opportunity. Not opening earlier than this saves
583 opening if no addresses have retry items - common when none have yet
584 reached their retry next try time. */
586 if (dbm_file == NULL)
587 dbm_file = dbfn_open(US"retry", O_RDWR, &dbblock, TRUE);
589 if (dbm_file == NULL)
591 DEBUG(D_deliver|D_retry|D_hints_lookup)
592 debug_printf("retry database not available for updating\n");
596 /* If there are no deferred addresses, that is, if this message is
597 completing, and the retry item is for a message-specific SMTP error,
598 force it to be deleted, because there's no point in keeping data for
599 no-longer-existing messages. This situation can occur when a domain has
600 two hosts and a message-specific error occurs for the first of them,
601 but the address gets delivered to the second one. This optimization
602 doesn't succeed in cleaning out all the dead entries, but it helps. */
604 if (*addr_defer == NULL && (rti->flags & rf_message) != 0)
605 rti->flags |= rf_delete;
607 /* Handle the case of a request to delete the retry info for this
610 if ((rti->flags & rf_delete) != 0)
612 (void)dbfn_delete(dbm_file, rti->key);
614 debug_printf("deleted retry information for %s\n", rti->key);
618 /* Count the number of non-delete retry items. This is so that we
619 can compare it to the count of timed_out ones, to check whether
620 all are timed out. */
624 /* Get the retry information for this destination and error code, if
625 any. If this item is for a remote host with ip address, then pass
626 the domain name as an alternative to search for. If no retry
627 information is found, we can't generate a retry time, so there is
628 no point updating the database. This retry item is timed out. */
630 if ((retry = retry_find_config(rti->key + 2,
631 ((rti->flags & rf_host) != 0)? addr->domain : NULL,
632 rti->basic_errno, rti->more_errno)) == NULL)
634 DEBUG(D_retry) debug_printf("No configured retry item for %s%s%s\n",
636 ((rti->flags & rf_host) != 0)? US" or " : US"",
637 ((rti->flags & rf_host) != 0)? addr->domain : US"");
638 if (addr == endaddr) timedout_count++;
644 if ((rti->flags & rf_host) != 0)
645 debug_printf("retry for %s (%s) = %s %d %d\n", rti->key,
646 addr->domain, retry->pattern, retry->basic_errno,
649 debug_printf("retry for %s = %s %d %d\n", rti->key, retry->pattern,
650 retry->basic_errno, retry->more_errno);
653 /* Set up the message for the database retry record. Because DBM
654 records have a maximum data length, we enforce a limit. There isn't
655 much point in keeping a huge message here, anyway. */
657 message = (rti->basic_errno > 0)? US strerror(rti->basic_errno) :
658 (rti->message == NULL)?
659 US"unknown error" : US string_printing(rti->message);
660 message_length = Ustrlen(message);
661 if (message_length > 150) message_length = 150;
663 /* Read a retry record from the database or construct a new one.
664 Ignore an old one if it is too old since it was last updated. */
666 retry_record = dbfn_read(dbm_file, rti->key);
667 if (retry_record != NULL &&
668 now - retry_record->time_stamp > retry_data_expire)
671 if (retry_record == NULL)
673 retry_record = store_get(sizeof(dbdata_retry) + message_length);
674 message_space = message_length;
675 retry_record->first_failed = now;
676 retry_record->last_try = now;
677 retry_record->next_try = now;
678 retry_record->expired = FALSE;
679 retry_record->text[0] = 0; /* just in case */
681 else message_space = Ustrlen(retry_record->text);
683 /* Compute how long this destination has been failing */
685 failing_interval = now - retry_record->first_failed;
686 DEBUG(D_retry) debug_printf("failing_interval=%d message_age=%d\n",
687 failing_interval, message_age);
689 /* For a non-host error, if the message has been on the queue longer
690 than the recorded time of failure, use the message's age instead. This
691 can happen when some messages can be delivered and others cannot; a
692 successful delivery will reset the first_failed time, and this can lead
693 to a failing message being retried too often. */
695 if ((rti->flags & rf_host) == 0 && message_age > failing_interval)
696 failing_interval = message_age;
698 /* Search for the current retry rule. The cutoff time of the
699 last rule is handled differently to the others. The rule continues
700 to operate for ever (the global maximum interval will eventually
701 limit the gaps) but its cutoff time determines when an individual
702 destination times out. If there are no retry rules, the destination
703 always times out, but we can't compute a retry time. */
706 for (rule = retry->rules; rule != NULL; rule = rule->next)
708 if (failing_interval <= rule->timeout) break;
712 /* If there's an un-timed out rule, the destination has not
713 yet timed out, so the address as a whole has not timed out (but we are
714 interested in this only for the end address). Make sure the expired
715 flag is false (can be forced via fixdb from outside, but ensure it is
716 consistent with the rules whenever we go through here). */
720 retry_record->expired = FALSE;
723 /* Otherwise, set the retry timeout expired, and set the final rule
724 as the one from which to compute the next retry time. Subsequent
725 messages will fail immediately until the retry time is reached (unless
726 there are other, still active, retries). */
731 retry_record->expired = TRUE;
732 if (addr == endaddr) timedout_count++;
735 /* There is a special case to consider when some messages get through
736 to a destination and others don't. This can happen locally when a
737 large message pushes a user over quota, and it can happen remotely
738 when a machine is on a dodgy Internet connection. The messages that
739 get through wipe the retry information, causing those that don't to
740 stay on the queue longer than the final retry time. In order to
741 avoid this, we check, using the time of arrival of the message, to
742 see if it has been on the queue for more than the final cutoff time,
743 and if so, cause this retry item to time out, and the retry time to
744 be set to "now" so that any subsequent messages in the same condition
745 also get tried. We search for the last rule onwards from the one that
746 is in use. If there are no retry rules for the item, rule will be null
747 and timedout_count will already have been updated.
749 This implements "timeout this rule if EITHER the host (or routing or
750 directing) has been failing for more than the maximum time, OR if the
751 message has been on the queue for more than the maximum time."
753 February 2006: It is possible that this code is no longer needed
754 following the change to the retry calculation to use the message age if
755 it is larger than the time since first failure. It may be that the
756 expired flag is always set when the other conditions are met. However,
757 this is a small bit of code, and it does no harm to leave it in place,
760 if (received_time <= retry_record->first_failed &&
761 addr == endaddr && !retry_record->expired && rule != NULL)
763 retry_rule *last_rule;
764 for (last_rule = rule;
765 last_rule->next != NULL;
766 last_rule = last_rule->next);
767 if (now - received_time > last_rule->timeout)
769 DEBUG(D_retry) debug_printf("on queue longer than maximum retry\n");
775 /* Compute the next try time from the rule, subject to the global
776 maximum, and update the retry database. If rule == NULL it means
777 there were no rules at all (and the timeout will be set expired),
778 or we have a message that is older than the final timeout. In this
779 case set the next retry time to now, so that one delivery attempt
780 happens for subsequent messages. */
782 if (rule == NULL) next_try = now; else
784 if (rule->rule == 'F') next_try = now + rule->p1;
785 else /* rule = 'G' or 'H' */
787 int last_predicted_gap =
788 retry_record->next_try - retry_record->last_try;
789 int last_actual_gap = now - retry_record->last_try;
790 int lastgap = (last_predicted_gap < last_actual_gap)?
791 last_predicted_gap : last_actual_gap;
792 int next_gap = (lastgap * rule->p2)/1000;
793 if (rule->rule == 'G')
795 next_try = now + ((lastgap < rule->p1)? rule->p1 : next_gap);
797 else /* The 'H' rule */
799 next_try = now + rule->p1;
800 if (next_gap > rule->p1)
801 next_try += random_number(next_gap - rule->p1)/2 +
802 (next_gap - rule->p1)/2;
807 /* Impose a global retry max */
809 if (next_try - now > retry_interval_max)
810 next_try = now + retry_interval_max;
812 /* If the new message length is greater than the previous one, we
813 have to copy the record first. */
815 if (message_length > message_space)
817 dbdata_retry *newr = store_get(sizeof(dbdata_retry) + message_length);
818 memcpy(newr, retry_record, sizeof(dbdata_retry));
822 /* Set up the retry record; message_length may be less than the string
823 length for very long error strings. */
825 retry_record->last_try = now;
826 retry_record->next_try = next_try;
827 retry_record->basic_errno = rti->basic_errno;
828 retry_record->more_errno = rti->more_errno;
829 Ustrncpy(retry_record->text, message, message_length);
830 retry_record->text[message_length] = 0;
834 int letter = retry_record->more_errno & 255;
835 debug_printf("Writing retry data for %s\n", rti->key);
836 debug_printf(" first failed=%d last try=%d next try=%d expired=%d\n",
837 (int)retry_record->first_failed, (int)retry_record->last_try,
838 (int)retry_record->next_try, retry_record->expired);
839 debug_printf(" errno=%d more_errno=", retry_record->basic_errno);
840 if (letter == 'A' || letter == 'M')
841 debug_printf("%d,%c", (retry_record->more_errno >> 8) & 255,
844 debug_printf("%d", retry_record->more_errno);
845 debug_printf(" %s\n", retry_record->text);
848 (void)dbfn_write(dbm_file, rti->key, retry_record,
849 sizeof(dbdata_retry) + message_length);
850 } /* Loop for each retry item */
852 /* If all the non-delete retry items are timed out, the address is
853 timed out, provided that we didn't skip any hosts because their retry
854 time was not reached (or because of hosts_max_try). */
856 if (update_count > 0 && update_count == timedout_count)
858 if (!testflag(endaddr, af_retry_skipped))
860 DEBUG(D_retry) debug_printf("timed out: all retries expired\n");
866 debug_printf("timed out but some hosts were skipped\n");
869 } /* Loop for an address and its parents */
871 /* If this is a deferred address, and retry processing was requested by
872 means of one or more retry items, and they all timed out, move the address
873 to the failed queue, and restart this loop without updating paddr.
875 If there were several addresses batched in the same remote delivery, only
876 the original top one will have host retry items attached to it, but we want
877 to handle all the same. Each will have a pointer back to its "top" address,
878 and they will now precede the item with the retries because addresses are
879 inverted when added to these final queues. We have saved information about
880 them in passing (below) so they can all be cut out at once. */
882 if (i == 2) /* Handling defers */
884 if (endaddr->retries != NULL && timed_out)
886 if (last_first == endaddr) paddr = saved_paddr;
888 *paddr = endaddr->next;
890 endaddr->next = *addr_failed;
893 for (;; addr = addr->next)
895 setflag(addr, af_retry_timedout);
896 addr->message = (addr->message == NULL)? US"retry timeout exceeded" :
897 string_sprintf("%s: retry timeout exceeded", addr->message);
898 addr->user_message = (addr->user_message == NULL)?
899 US"retry timeout exceeded" :
900 string_sprintf("%s: retry timeout exceeded", addr->user_message);
901 log_write(0, LOG_MAIN, "** %s%s%s%s: retry timeout exceeded",
903 (addr->parent == NULL)? US"" : US" <",
904 (addr->parent == NULL)? US"" : addr->parent->address,
905 (addr->parent == NULL)? US"" : US">");
907 if (addr == endaddr) break;
910 continue; /* Restart from changed *paddr */
913 /* This address is to remain on the defer chain. If it has a "first"
914 pointer, save the pointer to it in case we want to fail the set of
915 addresses when we get to the first one. */
917 if (endaddr->first != last_first)
919 last_first = endaddr->first;
924 /* All cases (succeed, fail, defer left on queue) */
926 paddr = &(endaddr->next); /* Advance to next address */
927 } /* Loop for all addresses */
928 } /* Loop for succeed, fail, defer */
930 /* Close and unlock the database */
932 if (dbm_file != NULL) dbfn_close(dbm_file);
934 DEBUG(D_retry) debug_printf("end of retry processing\n");