Line data Source code
1 : #include "fd_netdb.h" 2 : #include "fd_lookup.h" 3 : #include <errno.h> 4 : #include <fcntl.h> 5 : #include <unistd.h> 6 : #include "../../util/log/fd_log.h" 7 : #include "../../util/io/fd_io.h" 8 : 9 : FD_TL int fd_etc_hosts_fd = -1; 10 : FD_TL int fd_etc_resolv_conf_fd = -1; 11 : 12 : fd_netdb_fds_t * 13 0 : fd_netdb_open_fds( fd_netdb_fds_t * fds ) { 14 0 : if( FD_UNLIKELY( fd_etc_hosts_fd>=0 || fd_etc_resolv_conf_fd>=0 ) ) { 15 0 : return NULL; 16 0 : } 17 : 18 0 : int f = open( "/etc/resolv.conf", O_RDONLY ); 19 0 : if( FD_UNLIKELY( f<0 ) ) { 20 0 : FD_LOG_ERR(( "open(/etc/resolv.conf) failed (%i-%s)", errno, fd_io_strerror( errno ) )); 21 0 : } 22 0 : fd_etc_resolv_conf_fd = f; 23 : 24 0 : f = open( "/etc/hosts", O_RDONLY ); 25 0 : if( FD_UNLIKELY( f<0 ) ) { 26 0 : FD_LOG_WARNING(( "open(/etc/hosts) failed (%i-%s)", errno, fd_io_strerror( errno ) )); 27 0 : } else { 28 0 : fd_etc_hosts_fd = f; 29 0 : } 30 : 31 0 : if( fds ) { 32 0 : *fds = (fd_netdb_fds_t) { 33 0 : .etc_resolv_conf = fd_etc_resolv_conf_fd, 34 0 : .etc_hosts = fd_etc_hosts_fd 35 0 : }; 36 0 : } 37 0 : return fds; 38 0 : } 39 : 40 : void 41 0 : fd_netdb_close_fds( void ) { 42 0 : if( FD_LIKELY( fd_etc_resolv_conf_fd>=0 ) ) { 43 0 : if( FD_UNLIKELY( -1==close( fd_etc_resolv_conf_fd ) ) ) FD_LOG_ERR(( "close(/etc/resolv.conf) failed (%i-%s)", errno, fd_io_strerror( errno ) )); 44 0 : fd_etc_resolv_conf_fd = -1; 45 0 : } 46 0 : if( FD_LIKELY( fd_etc_hosts_fd>=0 ) ) { 47 0 : if( FD_UNLIKELY( -1==close( fd_etc_hosts_fd ) ) ) FD_LOG_ERR(( "close(/etc/hosts) failed (%i-%s)", errno, fd_io_strerror( errno ) )); 48 0 : fd_etc_hosts_fd = -1; 49 0 : } 50 0 : }