previously, transient failures like fd exhaustion or other
resource-related errors were treated the same as non-existence of
these files, leading to fallbacks or false-negative results. in
particular:
- failure to open hosts resulted in fallback to dns, possibly yielding
EAI_NONAME for a hostname that should be defined locally, or an
unwanted result from dns that the hosts file was intended to
replace.
- failure to open services resulted in EAI_SERVICE.
- failure to open resolv.conf resulted in querying localhost rather
than the configured nameservers.
now, only permanent errors trigger the fallback behaviors above; all
other errors are reportable to the caller as EAI_SYSTEM.
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
+#include <errno.h>
#include "lookup.h"
#include "stdio_impl.h"
#include "syscall.h"
int cnt = 0;
unsigned char _buf[1032];
FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
- if (!f) return 0;
+ if (!f) switch (errno) {
+ case ENOENT:
+ case ENOTDIR:
+ case EACCES:
+ return 0;
+ default:
+ return EAI_SYSTEM;
+ }
while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
char *p, *z;
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
+#include <errno.h>
#include "lookup.h"
#include "stdio_impl.h"
unsigned char _buf[1032];
FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
- if (!f) return EAI_SERVICE;
+ if (!f) switch (errno) {
+ case ENOENT:
+ case ENOTDIR:
+ case EACCES:
+ return EAI_SERVICE;
+ default:
+ return EAI_SYSTEM;
+ }
while (fgets(line, sizeof line, f) && cnt < MAXSERVS) {
if ((p=strchr(line, '#'))) *p++='\n', *p=0;
/* Get nameservers from resolv.conf, fallback to localhost */
f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
- if (f) for (nns=0; nns<3 && fgets(line, sizeof line, f); ) {
+ if (!f) switch (errno) {
+ case ENOENT:
+ case ENOTDIR:
+ case EACCES:
+ goto no_resolv_conf;
+ default:
+ return -1;
+ }
+ for (nns=0; nns<3 && fgets(line, sizeof line, f); ) {
if (!strncmp(line, "options", 7) && isspace(line[7])) {
unsigned long x;
char *p, *z;
}
}
}
- if (f) __fclose_ca(f);
+ __fclose_ca(f);
+no_resolv_conf:
if (!nns) {
ns[0].sin.sin_family = AF_INET;
ns[0].sin.sin_port = htons(53);