Skip to content

Commit

Permalink
Change the code according to @tixxdz advice
Browse files Browse the repository at this point in the history
* Check for IFF_LOOPBACK instead of matching the iface name 'lo'
* Use proper return values
  • Loading branch information
mattthias committed Oct 15, 2017
1 parent 098fa9f commit a55d849
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion slurm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "specified device does not exist or cannot "
"be monitored!\n\nIf you think this is an error please report "
"it to https://github.com/mattthias/slurm/issues . Thanks!\n");
exit(1);
exit(1);
}

/* Initialize some info variables */
Expand Down
32 changes: 16 additions & 16 deletions src/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ int get_default_interface(IfData * ifdata)

struct ifreq ifr;
unsigned int i;
unsigned int index;
unsigned int iface_up = 0;
char iface_up_name[IFNAMSIZ];

int ret;

/* Create a socket to ioctl on */
int sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Expand All @@ -130,29 +130,29 @@ int get_default_interface(IfData * ifdata)
/* iterate over the array and .. */
for (i = 0; ifs[i].if_index; i++) {

/* skip local loopback */
if (!strcmp(ifs[i].if_name, "lo")) {
continue;
}

/* write the name of the iface we want to check into the ifr struct and .. */
/* ..write the name of the iface we want to check into the ifr struct and .. */
strncpy(ifr.ifr_name, ifs[i].if_name, IFNAMSIZ);

/* .. perform an ioctl SIOCGIFFLAGS to get the iface flags */
ioctl(sk, SIOCGIFFLAGS, &ifr);
ret = ioctl(sk, SIOCGIFFLAGS, &ifr);
if (ret < 0)
return ret;

/* check if the iface is up (IFF_UP is set) */
if (ifr.ifr_flags & IFF_UP) {
if (ifr.ifr_flags & IFF_LOOPBACK) { /* skip local loopback */
continue;
} else if (ifr.ifr_flags & IFF_UP) { /* check if the iface is up (IFF_UP is set) */
iface_up++;
strncpy(iface_up_name, ifr.ifr_name, IFNAMSIZ);
index = i; /* save the index to get the iface name later */
}
}

if (iface_up == 1) {
snprintf((char *) ifdata->if_name,
(size_t) sizeof(ifdata->if_name), "%s", iface_up_name);
return (0);
strncpy(ifdata->if_name, ifs[index].if_name,
sizeof(ifdata->if_name));
ret = 0;
} else {
ret = -1;
}

return (2);
return ret;
}

0 comments on commit a55d849

Please sign in to comment.