# net-detect.pl # Helper functions for choosing the network config backend do 'ifupdown-lib.pl' if (!defined(&ifupdown_get_interface_defs)); # net_has_network_manager_config([connections-directory]) # Returns true if NetworkManager connection profiles exist sub net_has_network_manager_config { my ($dir) = @_; $dir ||= "/etc/NetworkManager/system-connections"; my @files = glob("$dir/*.nmconnection"); return -d $dir && scalar(@files); } # net_has_netplan_config([netplan-directory]) # Returns true if Netplan is installed and its config directory exists sub net_has_netplan_config { my ($dir) = @_; $dir ||= "/etc/netplan"; return &has_command("netplan") && -d $dir; } # net_has_ifupdown_config([interfaces-file]) # Returns true if ifupdown has any non-loopback iface stanzas sub net_has_ifupdown_config { my ($file) = @_; $file ||= "/etc/network/interfaces"; foreach my $iface (&ifupdown_get_interface_defs($file, undef, 1)) { # Loopback alone does not mean ifupdown owns real interfaces. return 1 if ($iface->[0] ne "lo"); } return 0; } # net_has_dhcpcd_config([dhcpcd-conf], [service-active]) # Returns true if dhcpcd appears to own interface startup config sub net_has_dhcpcd_config { my ($file, $service_active) = @_; $file ||= "/etc/dhcpcd.conf"; return 0 if (!-r $file); # Explicit interface blocks are enough proof even if the daemon is down. open(my $fh, "<", $file) || return 0; while(my $line = <$fh>) { $line =~ s/#.*$//; if ($line =~ /^\s*interface\s+\S+/) { close($fh); return 1; } } close($fh); # Default dhcpcd configs often have no interface blocks, so require service # evidence before treating the file as the active startup backend. return defined($service_active) ? $service_active : &net_dhcpcd_service_active(); } # net_dhcpcd_service_active() # Returns true if the dhcpcd service is active or enabled sub net_dhcpcd_service_active { if (&has_command("systemctl")) { foreach my $unit ("dhcpcd.service", "dhcpcd5.service") { my $q = quotemeta($unit); # Active means dhcpcd is managing interfaces right now. my $active = &backquote_command( "systemctl is-active $q 2>/dev/null /dev/null