}
}
-#ifdef HAVE_AF_PACKET
+#ifdef HAVE_IF_NAMEINDEX
bool ctdb_sys_check_iface_exists(const char *iface)
{
- int s;
- struct ifreq ifr;
+ struct if_nameindex *ifnis, *ifni;
+ bool found = false;
- 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;
+ ifnis = if_nameindex();
+ if (ifnis == NULL) {
+ DBG_ERR("Failed to retrieve inteface list\n");
+ return false;
}
- 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;
+ for (ifni = ifnis;
+ ifni->if_index != 0 || ifni->if_name != NULL;
+ ifni++) {
+ int cmp = strcmp(iface, ifni->if_name);
+ if (cmp == 0) {
+ found = true;
+ goto done;
+ }
}
- close(s);
- return true;
+done:
+ if_freenameindex(ifnis);
+
+ return found;
}
-#else /* HAVE_AF_PACKET */
+#else /* HAVE_IF_NAMEINDEX */
bool ctdb_sys_check_iface_exists(const char *iface)
{
return true;
}
-#endif /* HAVE_AF_PACKET */
+#endif /* HAVE_IF_NAMEINDEX */
#ifdef HAVE_PEERCRED
test_cleanup remove_socket
-os=$(uname)
-if [ "$os" = "Linux" ] ; then
- uid=$(id -u)
- if [ "$uid" -eq 0 ] ; then
- ok "ctdb_sys_check_iface_exists: Interface 'fake' not found"
- else
- ok "ctdb_sys_check_iface_exists: Failed to open raw socket"
- fi
-else
- ok_null
-fi
-
-unit_test porting_tests --socket=${socket}
+ok_null
+unit_test porting_tests --socket="$socket"
*/
static int test_ctdb_sys_check_iface_exists(void)
{
- const char *fakename = "fake";
- bool test;
+ bool test1, test2;
+
+ test1 = ctdb_sys_check_iface_exists("unlikely123xyz");
+ assert(!test1);
+
+ /* Linux and others */
+ test1 = ctdb_sys_check_iface_exists("lo");
+ /* FreeBSD */
+ test2 = ctdb_sys_check_iface_exists("lo0");
+ assert(test1 || test2);
- test = ctdb_sys_check_iface_exists(fakename);
- if (geteuid() == 0) {
- assert(test == false);
- } else {
- assert(test == true);
- }
return 0;
}