zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] _find_net_interfaces: use /sys/class/net/* for interface names in linux
@ 2015-08-11  0:49 Eric Cook
  2015-08-11  9:47 ` Oliver Kiddle
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Cook @ 2015-08-11  0:49 UTC (permalink / raw)
  To: zsh-workers

ip(8) seems to add a suffix to the interface name for certain interface
types. The ones i've noticed are macvtaps, macvlans and recently
bridges.

% ip link add link eth0 name tap0 address 00:22:33:44:55:66 \
type macvtap mode bridge

or with iproute2 4.0.x:
% ip link add br0 type bridge

% ip link show <tab> # will show @eth0 or @NONE appended to tap0 or br0.

There doesn't seem to be a option to suppress that behavior and @ is a
legal character for interface names. So chopping off suffix with more
parameter expansion doesn't seem like the correct thing to do.

I also wasn't able to find the type of tunnels mentioned in
http://www.zsh.org/mla/workers/2007/msg00111.html that doesn't show up
in /proc/sys/net/ipv4/conf/. Maybe at some point since then, that was
fixed. But to avoid some kind of regression /sys/class/net/ seems usable
for the time being.
---
 Completion/Unix/Type/_find_net_interfaces | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Completion/Unix/Type/_find_net_interfaces b/Completion/Unix/Type/_find_net_interfaces
index 0c70335..f90f310 100644
--- a/Completion/Unix/Type/_find_net_interfaces
+++ b/Completion/Unix/Type/_find_net_interfaces
@@ -23,7 +23,7 @@ case $OSTYPE in
   irix*) net_intf_list=( ${${${(f)"$(/usr/etc/netstat -i)"}%% *}[2,-1]} ) ;;
   *linux*)
     if (( $+commands[ip] )); then
-      net_intf_list=( ${${(m)${(f)"$(ip -o link)"}#*: }%%: *} )
+      net_intf_list=( /sys/class/net/*(N:t)  )
     fi
   ;&
 
-- 
2.5.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] _find_net_interfaces: use /sys/class/net/* for interface names in linux
  2015-08-11  0:49 [PATCH] _find_net_interfaces: use /sys/class/net/* for interface names in linux Eric Cook
@ 2015-08-11  9:47 ` Oliver Kiddle
  2015-08-11 13:27   ` Eric Cook
  2015-08-12  0:25   ` Eric Cook
  0 siblings, 2 replies; 4+ messages in thread
From: Oliver Kiddle @ 2015-08-11  9:47 UTC (permalink / raw)
  To: zsh-workers

Eric Cook wrote:
> I also wasn't able to find the type of tunnels mentioned in
> http://www.zsh.org/mla/workers/2007/msg00111.html that doesn't show up
> in /proc/sys/net/ipv4/conf/. Maybe at some point since then, that was
> fixed. But to avoid some kind of regression /sys/class/net/ seems usable
> for the time being.

One example, is aliases. So if you do something like
  ifconfig eth0:1 192.168.42.37
then ifconfig will show eth0:1 while /sys/class won't.

There may be cases where we don't want to complete those, however.

> diff --git a/Completion/Unix/Type/_find_net_interfaces b/Completion/Unix/Type/_find_net_interfaces
> index 0c70335..f90f310 100644
> --- a/Completion/Unix/Type/_find_net_interfaces
> +++ b/Completion/Unix/Type/_find_net_interfaces
> @@ -23,7 +23,7 @@ case $OSTYPE in
>    irix*) net_intf_list=( ${${${(f)"$(/usr/etc/netstat -i)"}%% *}[2,-1]} ) ;;
>    *linux*)
>      if (( $+commands[ip] )); then
> -      net_intf_list=( ${${(m)${(f)"$(ip -o link)"}#*: }%%: *} )
> +      net_intf_list=( /sys/class/net/*(N:t)  )
>      fi
>    ;&

The (( $+commands[ip] )) isn't really applicable after that change.

Also did you notice that we still have the following further down in the
function:
    if [[ -d /proc/sys/net/ipv4/conf ]]; then
      # On linux we used to use the following as the default.
      # However, we now use ip or ifconfig since it finds additional devices such
      # as tunnels.  So only do this if that didn't work.
      net_intf_list=( /proc/sys/net/ipv4/conf/*~*(all|default)(N:t) )
    fi

I think the /proc path there will work for older Linux installations
than the /sys path you use and it still works with new systems.

Oliver


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] _find_net_interfaces: use /sys/class/net/* for interface names in linux
  2015-08-11  9:47 ` Oliver Kiddle
@ 2015-08-11 13:27   ` Eric Cook
  2015-08-12  0:25   ` Eric Cook
  1 sibling, 0 replies; 4+ messages in thread
From: Eric Cook @ 2015-08-11 13:27 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 2351 bytes --]

On 08/11/2015 05:47 AM, Oliver Kiddle wrote:
> Eric Cook wrote:
>> I also wasn't able to find the type of tunnels mentioned in
>> http://www.zsh.org/mla/workers/2007/msg00111.html that doesn't show up
>> in /proc/sys/net/ipv4/conf/. Maybe at some point since then, that was
>> fixed. But to avoid some kind of regression /sys/class/net/ seems usable
>> for the time being.
>
> One example, is aliases. So if you do something like
>   ifconfig eth0:1 192.168.42.37
> then ifconfig will show eth0:1 while /sys/class won't.
>
> There may be cases where we don't want to complete those, however.

Are there programs that actually work with aliases interface names? The ones I have seen, not many(ifdown/ifup), assumes you are attempting to address
the actual interface.
>> diff --git a/Completion/Unix/Type/_find_net_interfaces b/Completion/Unix/Type/_find_net_interfaces
>> index 0c70335..f90f310 100644
>> --- a/Completion/Unix/Type/_find_net_interfaces
>> +++ b/Completion/Unix/Type/_find_net_interfaces
>> @@ -23,7 +23,7 @@ case $OSTYPE in
>>    irix*) net_intf_list=( ${${${(f)"$(/usr/etc/netstat -i)"}%% *}[2,-1]} ) ;;
>>    *linux*)
>>      if (( $+commands[ip] )); then
>> -      net_intf_list=( ${${(m)${(f)"$(ip -o link)"}#*: }%%: *} )
>> +      net_intf_list=( /sys/class/net/*(N:t)  )
>>      fi
>>    ;&
>
> The (( $+commands[ip] )) isn't really applicable after that change.

Whoops.
> Also did you notice that we still have the following further down in the
> function:
>     if [[ -d /proc/sys/net/ipv4/conf ]]; then
>       # On linux we used to use the following as the default.
>       # However, we now use ip or ifconfig since it finds additional devices such
>       # as tunnels.  So only do this if that didn't work.
>       net_intf_list=( /proc/sys/net/ipv4/conf/*~*(all|default)(N:t) )
>     fi
>
> I think the /proc path there will work for older Linux installations
> than the /sys path you use and it still works with new systems.
>
> Oliver
>
I noticed it and found /sys/class/net on the oldest linux installs i have access to, centos 4.x.
The linux block in the case statement is a fall through. If /sys/class/net
doesn't exist it will fall back to using ifconfig or /proc/sys/net/ipv4/conf/ since the array
net_intf_list is still empty.

I've fixed my original patch, but if you want to solve it another way, go for it :D

[-- Attachment #2: 0001-_find_net_interfaces-use-sys-class-net-for-interface.patch --]
[-- Type: text/x-patch, Size: 584 bytes --]

diff --git a/Completion/Unix/Type/_find_net_interfaces b/Completion/Unix/Type/_find_net_interfaces
index 0c70335..32c920f 100644
--- a/Completion/Unix/Type/_find_net_interfaces
+++ b/Completion/Unix/Type/_find_net_interfaces
@@ -22,9 +22,7 @@ case $OSTYPE in
   darwin*|freebsd*|dragonfly*) net_intf_list=( $(ifconfig -l) ) ;;
   irix*) net_intf_list=( ${${${(f)"$(/usr/etc/netstat -i)"}%% *}[2,-1]} ) ;;
   *linux*)
-    if (( $+commands[ip] )); then
-      net_intf_list=( ${${(m)${(f)"$(ip -o link)"}#*: }%%: *} )
-    fi
+    net_intf_list=( /sys/class/net/*(N:t) )
   ;&
 
   *)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] _find_net_interfaces: use /sys/class/net/* for interface names in linux
  2015-08-11  9:47 ` Oliver Kiddle
  2015-08-11 13:27   ` Eric Cook
@ 2015-08-12  0:25   ` Eric Cook
  1 sibling, 0 replies; 4+ messages in thread
From: Eric Cook @ 2015-08-12  0:25 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 834 bytes --]

On 08/11/2015 05:47 AM, Oliver Kiddle wrote:
> Eric Cook wrote:
>> I also wasn't able to find the type of tunnels mentioned in
>> http://www.zsh.org/mla/workers/2007/msg00111.html that doesn't show up
>> in /proc/sys/net/ipv4/conf/. Maybe at some point since then, that was
>> fixed. But to avoid some kind of regression /sys/class/net/ seems usable
>> for the time being.
> 
> One example, is aliases. So if you do something like
>   ifconfig eth0:1 192.168.42.37
> then ifconfig will show eth0:1 while /sys/class won't.

When i switched to parsing ip(8), those alias interface names were no longer being shown.

So the following patch removes the linux block in the case statement, yet again.
Use ifconfig, but with _call_program. If ifconfig doesn't find anything, fall back to
/proc/sys/net/ipv4/conf/* and now /sys/class/net/*.


[-- Attachment #2: 0001-_find_net_interfaces-use-sys-class-net-for-interface-v3.patch --]
[-- Type: text/x-patch, Size: 1508 bytes --]

diff --git a/Completion/Unix/Type/_find_net_interfaces b/Completion/Unix/Type/_find_net_interfaces
index 0c70335..32ab79b 100644
--- a/Completion/Unix/Type/_find_net_interfaces
+++ b/Completion/Unix/Type/_find_net_interfaces
@@ -21,22 +21,15 @@ case $OSTYPE in
   ;;
   darwin*|freebsd*|dragonfly*) net_intf_list=( $(ifconfig -l) ) ;;
   irix*) net_intf_list=( ${${${(f)"$(/usr/etc/netstat -i)"}%% *}[2,-1]} ) ;;
-  *linux*)
-    if (( $+commands[ip] )); then
-      net_intf_list=( ${${(m)${(f)"$(ip -o link)"}#*: }%%: *} )
-    fi
-  ;&
-
   *)
-  if [[ ${#net_intf_list} -eq 0 ]]; then
     # linux's deprecated ifconfig may truncate long interface names
-    net_intf_list=( $(ifconfig -a 2>/dev/null | sed -n 's/^\([^ 	:]*\).*/\1/p') )
-    if [[ -d /proc/sys/net/ipv4/conf ]]; then
+    net_intf_list=( $(_call_program interfaces "ifconfig -a 2>/dev/null | sed -n 's/^\([^ 	:]*\).*/\1/p'") )
+    if (( ${#net_intf_list} == 0 )) && [[ -d /proc/sys/net/ipv4/conf ]]; then
       # On linux we used to use the following as the default, without /sys/class/net/*.
-      # However, we now use ip or ifconfig since it finds additional devices such
+      # However, we now use ifconfig since it finds additional devices such
       # as tunnels.  So only do this if that didn't work.
-      net_intf_list=( /proc/sys/net/ipv4/conf/*~*(all|default)(N:t) )
+      typeset -gU net_intf_list
+      net_intf_list=( /proc/sys/net/ipv4/conf/*~*(all|default)(N:t) /sys/class/net/*(N:t) )
     fi
-  fi
   ;;
 esac

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-08-12  0:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-11  0:49 [PATCH] _find_net_interfaces: use /sys/class/net/* for interface names in linux Eric Cook
2015-08-11  9:47 ` Oliver Kiddle
2015-08-11 13:27   ` Eric Cook
2015-08-12  0:25   ` Eric Cook

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).