From: Martin Schwenke Date: Thu, 28 Jun 2018 10:30:32 +0000 (+1000) Subject: ctdb-common: Move ctdb_system_check_iface_exists() to system.[ch] X-Git-Tag: tevent-0.9.37~181 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8fcd12ba29cd1d23e0283cad187ae8d35c8c6fcc;p=thirdparty%2Fsamba.git ctdb-common: Move ctdb_system_check_iface_exists() to system.[ch] The system_.c files contain a lot of duplication, making maintenance difficult. These functions are being merged into system_socket.[ch] and system.[ch]. This function doesn't need ctdb_sock_addr so put it with general system utilities. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- diff --git a/ctdb/common/system.c b/ctdb/common/system.c index 53480dd0d6c..50054f0abc6 100644 --- a/ctdb/common/system.c +++ b/ctdb/common/system.c @@ -163,3 +163,38 @@ void ctdb_wait_for_process_to_exit(pid_t pid) sleep(5); } } + +#ifdef HAVE_AF_PACKET + +bool ctdb_sys_check_iface_exists(const char *iface) +{ + int s; + struct ifreq ifr; + + s = socket(AF_PACKET, SOCK_RAW, 0); + if (s == -1){ + /* We don't know if the interface exists, so assume yes */ + DBG_ERR("Failed to open raw socket\n"); + return true; + } + + strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); + if (ioctl(s, SIOCGIFINDEX, &ifr) < 0 && errno == ENODEV) { + DBG_ERR("Interface '%s' not found\n", iface); + close(s); + return false; + } + close(s); + + return true; +} + +#else /* HAVE_AF_PACKET */ + +bool ctdb_sys_check_iface_exists(const char *iface) +{ + /* Not implemented: Interface always considered present */ + return true; +} + +#endif /* HAVE_AF_PACKET */ diff --git a/ctdb/common/system.h b/ctdb/common/system.h index 9fd8c225e6f..908d29d2794 100644 --- a/ctdb/common/system.h +++ b/ctdb/common/system.h @@ -34,7 +34,6 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, ctdb_sock_addr *src, ctdb_sock_addr *dst, uint32_t *ack_seq, uint32_t *seq, int *rst, uint16_t *window); -bool ctdb_sys_check_iface_exists(const char *iface); int ctdb_get_peer_pid(const int fd, pid_t *peer_pid); /* From system_util.c */ @@ -48,4 +47,6 @@ void mkdir_p_or_die(const char *dir, int mode); void ctdb_wait_for_process_to_exit(pid_t pid); +bool ctdb_sys_check_iface_exists(const char *iface); + #endif /* __CTDB_SYSTEM_H__ */ diff --git a/ctdb/common/system_aix.c b/ctdb/common/system_aix.c index f4c5c700141..f17125cbd39 100644 --- a/ctdb/common/system_aix.c +++ b/ctdb/common/system_aix.c @@ -380,13 +380,6 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } - -bool ctdb_sys_check_iface_exists(const char *iface) -{ - /* FIXME AIX: Interface always considered present */ - return true; -} - int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) { struct peercred_struct cr; diff --git a/ctdb/common/system_freebsd.c b/ctdb/common/system_freebsd.c index 5614e1014d8..de7bf2b3396 100644 --- a/ctdb/common/system_freebsd.c +++ b/ctdb/common/system_freebsd.c @@ -384,12 +384,6 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } -bool ctdb_sys_check_iface_exists(const char *iface) -{ - /* FIXME FreeBSD: Interface always considered present */ - return true; -} - int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) { /* FIXME FreeBSD: get_peer_pid not implemented */ diff --git a/ctdb/common/system_gnu.c b/ctdb/common/system_gnu.c index 27339d18c3e..7f2654e61d7 100644 --- a/ctdb/common/system_gnu.c +++ b/ctdb/common/system_gnu.c @@ -379,12 +379,6 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } -bool ctdb_sys_check_iface_exists(const char *iface) -{ - /* FIXME GNU/Hurd: Interface always considered present */ - return true; -} - int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) { /* FIXME GNU/Hurd: get_peer_pid not implemented */ diff --git a/ctdb/common/system_kfreebsd.c b/ctdb/common/system_kfreebsd.c index 67c2aa06043..ccad294615d 100644 --- a/ctdb/common/system_kfreebsd.c +++ b/ctdb/common/system_kfreebsd.c @@ -379,12 +379,6 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } -bool ctdb_sys_check_iface_exists(const char *iface) -{ - /* FIXME kFreeBSD: Interface always considered present */ - return true; -} - int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) { /* FIXME kFreeBSD: get_peer_pid not implemented */ diff --git a/ctdb/common/system_linux.c b/ctdb/common/system_linux.c index d8f53ea8ea0..3cff3b20c94 100644 --- a/ctdb/common/system_linux.c +++ b/ctdb/common/system_linux.c @@ -604,30 +604,6 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } - -bool ctdb_sys_check_iface_exists(const char *iface) -{ - int s; - struct ifreq ifr; - - s = socket(AF_PACKET, SOCK_RAW, 0); - if (s == -1){ - /* We don't know if the interface exists, so assume yes */ - DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket\n")); - return true; - } - - strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); - if (ioctl(s, SIOCGIFINDEX, &ifr) < 0 && errno == ENODEV) { - DEBUG(DEBUG_CRIT,(__location__ " interface '%s' not found\n", iface)); - close(s); - return false; - } - close(s); - - return true; -} - int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) { struct ucred cr; diff --git a/ctdb/tests/cunit/porting_tests_001.sh b/ctdb/tests/cunit/porting_tests_001.sh index 98a8af2d7f4..ba69fb95c44 100755 --- a/ctdb/tests/cunit/porting_tests_001.sh +++ b/ctdb/tests/cunit/porting_tests_001.sh @@ -11,16 +11,11 @@ remove_socket () test_cleanup remove_socket -result_filter () -{ - sed -e 's|^\(\.\./common/system_linux\.c\):[0-9][0-9]*|\1:LINE|' -} - uid=$(id -u) if [ "$uid" -eq 0 ] ; then - ok "../common/system_linux.c:LINE interface 'fake' not found" + ok "ctdb_sys_check_iface_exists: Interface 'fake' not found" else - ok "../common/system_linux.c:LINE failed to open raw socket" + ok "ctdb_sys_check_iface_exists: Failed to open raw socket" fi unit_test porting_tests --socket=${socket}