X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=src%2FPVE%2FProcFSTools.pm;h=05fd744367a9780fa80a719751bb897eb4296ecd;hb=0c0304c0d9a9a753b5d93a6c7b2239059a26d945;hp=516c0e8b939791a64771df41c36df362e65c7423;hpb=c1073fdc483bad57e0d71999f83b6797f294cf47;p=pve-common.git diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm index 516c0e8..05fd744 100644 --- a/src/PVE/ProcFSTools.pm +++ b/src/PVE/ProcFSTools.pm @@ -8,6 +8,12 @@ use IO::File; use PVE::Tools; use Cwd qw(); +use Socket qw(PF_INET PF_INET6 SOCK_DGRAM IPPROTO_IP); + +use constant IFF_UP => 1; +use constant IFNAMSIZ => 16; +use constant SIOCGIFFLAGS => 0x8913; + my $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK); my $cpuinfo; @@ -323,6 +329,8 @@ sub is_mounted { $mountpoint = Cwd::realpath($mountpoint); + return 0 if !defined($mountpoint); # path does not exist + my $mounts = parse_proc_mounts(); return (grep { $_->[1] eq $mountpoint } @$mounts) ? 1 : 0; } @@ -372,4 +380,36 @@ sub upid_wait { } } +# struct ifreq { // FOR SIOCGIFFLAGS: +# char ifrn_name[IFNAMSIZ] +# short ifru_flags +# }; +my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ . 's1'; +sub get_active_network_interfaces { + # Use the interface name list from /proc/net/dev + open my $fh, '<', '/proc/net/dev' + or die "failed to open /proc/net/dev: $!\n"; + # And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl: + my $sock; + socket($sock, PF_INET6, SOCK_DGRAM, &IPPROTO_IP) + or socket($sock, PF_INET, SOCK_DGRAM, &IPPROTO_IP) + or return []; + + my $ifaces = []; + while(defined(my $line = <$fh>)) { + next if $line !~ /^\s*([^:\s]+):/; + my $ifname = $1; + my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0); + if (!defined(ioctl($sock, SIOCGIFFLAGS, $ifreq))) { + warn "failed to get interface flags for: $ifname\n"; + next; + } + my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq); + push @$ifaces, $ifname if ($flags & IFF_UP); + } + close $fh; + close $sock; + return $ifaces; +} + 1;