dig exit status is 0 when the connection is refused, it should be equivalent to "no reply received"
(Sorry about the changes, I wrongly though it was affecting 9.16, seems to be a regression in 9.18)
Summary
Dig returns an exit status of 9 (as the manpage states) when no answer is received. However, if the queried DNS server is not runnign and it receives an unreachable error, exit status is 0. It's a regression from 9.16.
BIND version used
bind 9.18.1 on FreeBSD 13
Steps to reproduce
dig @127.0.0.1 whatever (assuming that you have a DNS server on the same server and it's not running or it's not listening).
$ dig @127.0.0.1 .
;; communications error to 127.0.0.1#53: connection refused
$ echo $?
0
What is the current bug behavior?
dig returns an exit code of 0 despite the fact that it did not receive an actual answer to a DNS query, but an error code.
What is the expected correct behavior?
This failure to receive an actual DNS answer from a server merits an exit code. Either the "No reply from server", which is 9, or a different one. On 9.16 it doesn't exit immediately
Relevant configuration files
(Paste any relevant configuration files - please use code blocks (```)
to format console output. If submitting the contents of your
configuration file in a non-confidential Issue, it is advisable to
obscure key secrets: this can be done automatically by using
named-checkconf -px
.)
Relevant logs and/or screenshots
It seems to be a difference in how sendmsg() is handled.
On 9.16 the error code is ignored.
socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP) = 13 (0xd) setsockopt(13,IPPROTO_IP,IP_TOS,0x7fffdffed5f4,4) = 0 (0x0) setsockopt(13,IPPROTO_IP,IP_RECVTOS,0x7fffdffed5f0,4) = 0 (0x0) bind(13,{ AF_INET 127.0.0.1:0 },16) = 0 (0x0) getsockname(13,{ AF_INET 127.0.0.1:9883 },0x7fffdffed3a8) = 0 (0x0) sendmsg(13,{{ AF_INET 127.0.0.1:9883 },16,[{"\0",1}],1,{{level=IPPROTO_IP,type=3,data={0xb8}}},24,0},0) = 1 (0x1) fcntl(13,F_GETFL,) = 2 (0x2) fcntl(13,F_SETFL,O_RDWR|O_NONBLOCK) = 0 (0x0) recvmsg(13,{{ AF_INET 127.0.0.1:9883 },16,[{"\0",1}],1,{},0,0},0) = 1 (0x1) close(13) = 0 (0x0)
while on 9.18,
` socket(PF_INET,SOCK_DGRAM,0) = 10 (0xa) setsockopt(10,SOL_SOCKET,SO_REUSEPORT,0x7fffdffed74c,4) = 0 (0x0) setsockopt(10,SOL_SOCKET,65536,0x7fffdffed74c,4) = 0 (0x0) setsockopt(10,IPPROTO_IP,IP_DONTFRAG,0x7fffdffed744,4) = 0 (0x0) ioctl(10,FIONBIO,0x7fffdffed6ac) = 0 (0x0) setsockopt(10,SOL_SOCKET,SO_REUSEPORT,0x7fffdffed6ec,4) = 0 (0x0) getpeername(10,0x7fffdffed638,0x7fffdffed5fc) ERR#57 'Socket is not connected' setsockopt(10,SOL_SOCKET,SO_REUSEPORT,0x7fffdffed6dc,4) = 0 (0x0) bind(10,{ AF_INET 0.0.0.0:0 },16) = 0 (0x0) connect(10,{ AF_INET 127.0.0.1:53 },16) = 0 (0x0) write(9,"\0",1) = 1 (0x1) socket(PF_INET,SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK,0) = 11 (0xb) close(11) = 0 (0x0) sendmsg(10,{NULL,0,[{"\M^\M-V^A \0^A\0\0\0\0\0^A"...,52}],1,{},0,0},0) = 52 (0x34) kevent(3,{ 10,EVFILT_READ,EV_ADD,0,0,0x0 },1,{ 8,EVFILT_READ,0x0,0,0x1,0x0 10,EVFILT_READ,0x0,0,0,0x0 },1024,{ 0.998000000 }) = 2 (0x2) read(8,"\0",1024) = 1 (0x1) recvmsg(10,0x7fffdffed510,0) ERR#61 'Connection refused' write(9,"\0",1) = 1 (0x1) kevent(3,{ },0,{ 8,EVFILT_READ,0x0,0,0x1,0x0 },1024,{ 0.000000000 }) = 1 (0x1) read(8,"\0",1024) = 1 (0x1) fstat(1,{ mode=crw--w---- ,inode=103,size=0,blksize=4096 }) = 0 (0x0) ioctl(1,TIOCGETA,0x7fffdffecbf0) = 0 (0x0) ;; communications error to 127.0.0.1#53: connection refused write(1,";; communications error to 127.0"...,60) = 60 (0x3c)
write(1,"\n",1) = 1 (0x1)
`
Possible fixes
(If you can, link to the line of code that might be responsible for the problem.)