Re: [DNG] an alternative to poettering's ifplugd

2019-03-19 Thread aitor_czr

Hi Didier,

On 19/3/19 12:39, Didier Kryn wrote:

Le 19/03/2019 à 09:56, aitor_czr a écrit :


Hi Nick,

On 19/3/19 9:21, Dr. Nikolaus Klepp wrote:

...
 for i in /sys/class/net/eth*; do

...
Thanks for your script! Interesting to know... With a short look, i 
seems to read the value content in the /sys/class/eth0/carrier file, 
set to "0" or "1".


    Just a reminder that using symbolic links in /sys isn't robust, as 
expained in 
https://www.kernel.org/doc/html/v4.16/admin-guide/sysfs-rules.html


    Here are two citations from this link. 1st citation:

/It is agreed upon by the kernel developers that the Linux kernel does 
not provide a stable internal API. Therefore, there are aspects of the 
sysfs interface that may not be stable across kernel releases./


/To minimize the risk of breaking users of sysfs, which are in most 
cases low-level userspace applications, with a new kernel release, the 
users of sysfs must follow some rules to use an 
as-abstract-as-possible way to access this filesystem. The current 
udev and HAL programs already implement this and users are encouraged 
to plug, if possible, into the abstractions these programs provide 
instead of accessing sysfs directly./


    I think we all agree to not follow this recommendation of linking 
to libudev. 2nd citation:


/There is no such thing like class-, bus-, physical devices, 
interfaces, and such that you can rely on in userspace. Everything is 
just simply a “device”. Class-, bus-, physical, ... types are just 
kernel implementation details which should not be expected by 
applications that look for devices in sysfs./


    There is at least one thing stable: the property of each devices 
are represented by files in a subdirectory in the tree below  
/sys/devices and this subdirectory is named after the device name, eg


            /sys/devices/pci:00/:00:19.0/net/eth0

    Therefore, the only robust method I have found to programatically 
discover the device properties is to visit systematically all 
directories below /sys/devices until I find the proper subdirectory.


    My 1 cent.

        Didier



Thanks a lot for the info !!

Aitor.


___
Dng mailing list
Dng@lists.dyne.org
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng


Re: [DNG] an alternative to poettering's ifplugd

2019-03-19 Thread Didier Kryn

Le 19/03/2019 à 09:56, aitor_czr a écrit :


Hi Nick,

On 19/3/19 9:21, Dr. Nikolaus Klepp wrote:

...
 for i in /sys/class/net/eth*; do

...
Thanks for your script! Interesting to know... With a short look, i 
seems to read the value content in the /sys/class/eth0/carrier file, 
set to "0" or "1".


    Just a reminder that using symbolic links in /sys isn't robust, as 
expained in 
https://www.kernel.org/doc/html/v4.16/admin-guide/sysfs-rules.html


    Here are two citations from this link. 1st citation:

/It is agreed upon by the kernel developers that the Linux kernel does 
not provide a stable internal API. Therefore, there are aspects of the 
sysfs interface that may not be stable across kernel releases./


/To minimize the risk of breaking users of sysfs, which are in most 
cases low-level userspace applications, with a new kernel release, the 
users of sysfs must follow some rules to use an as-abstract-as-possible 
way to access this filesystem. The current udev and HAL programs already 
implement this and users are encouraged to plug, if possible, into the 
abstractions these programs provide instead of accessing sysfs directly./


    I think we all agree to not follow this recommendation of linking 
to libudev. 2nd citation:


/There is no such thing like class-, bus-, physical devices, interfaces, 
and such that you can rely on in userspace. Everything is just simply a 
“device”. Class-, bus-, physical, ... types are just kernel 
implementation details which should not be expected by applications that 
look for devices in sysfs./


    There is at least one thing stable: the property of each devices 
are represented by files in a subdirectory in the tree below  
/sys/devices and this subdirectory is named after the device name, eg


            /sys/devices/pci:00/:00:19.0/net/eth0

    Therefore, the only robust method I have found to programatically 
discover the device properties is to visit systematically all 
directories below /sys/devices until I find the proper subdirectory.


    My 1 cent.

        Didier




___
Dng mailing list
Dng@lists.dyne.org
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng


Re: [DNG] an alternative to poettering's ifplugd

2019-03-19 Thread aitor_czr

Hi,

On 19/3/19 9:21, Dr. Nikolaus Klepp wrote:

You might call me ignorant, but with bash this can be done a bit simpler (this 
is from my solution to the detect-if-kable-present-while-booting-problem):-)


The best solution might be to run the connection attempt on a secondary 
plane without interrupting the boot process...


Aitor.


___
Dng mailing list
Dng@lists.dyne.org
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng


Re: [DNG] an alternative to poettering's ifplugd

2019-03-19 Thread Dr. Nikolaus Klepp
Hi Aitor!

Anno domini 2019 Tue, 19 Mar 09:56:07 +0100
 aitor_czr scripsit:
> Hi Nick,
> 
> On 19/3/19 9:21, Dr. Nikolaus Klepp wrote:
> > You might call me ignorant, but with bash this can be done a bit simpler 
> > (this is from my solution to the 
> > detect-if-kable-present-while-booting-problem):-)
> >
> > #!/bin/bash
> >
> > while sleep 1; do
> >  for i in /sys/class/net/eth*; do
> >  VAR=$(basename $i)
> >  VAL=$(sed -n 's/0/UN/p' $i/carrier)PLUGGED
> >  if [ "${!VAR}" != "$VAL" ]; then
> >  echo $VAR $VAL
> >  declare $VAR=$VAL
> >  fi
> >  done
> > done
> >
> >
> > Nik
> Thanks for your script! Interesting to know... With a short look, i 
> seems to read the value content in the /sys/class/eth0/carrier file, set 
> to "0" or "1".
> I was thinking about this possibility a couple of days ago, but i'm not 
> pretty sure if its value also depends on the status of the network 
> hotpluggable device. I'm testing it...

Interestingly the "carrier"-column in /proc/net/dev does not correspond to the 
cable beeing plugged in or not, it's always "0" on my computer.

Nik


> On the other hand, here you are the code used in the ethtool-lite.c file 
> of the netcfg udeb package used by debian-installer, doing something 
> similar (in the case of the FreeBSD kernel
> it uses a raw socket instead):
> 
>      int len = strlen(SYSCLASSNET) + strlen(iface) + strlen("/carrier") + 1;
>      char* filename = malloc(len);
>      snprintf(filename, len, SYSCLASSNET "%s/carrier", iface);
>      FILE* fp = fopen(filename, "r");
>      free(filename);
> 
>      char result[2];
>      if (fgets(result, sizeof(result), fp) == NULL) {
>          fclose(fp);
>          if (errno == EINVAL) {
>              di_info("ethtool-lite: %s is down", iface);
>              return DISCONNECTED;
>          }
>          di_error("ethtool-lite: getting carrier failed: %s",
>              strerror(errno));
>          return UNKNOWN;
>      }
>      fclose(fp);
> 
>      switch (result[0]) {
>      case '1':
>          di_info("ethtool-lite: %s: carrier up", iface);
>          return CONNECTED;
>      case '0':
>          di_info("ethtool-lite: %s: carrier down", iface);
>          return DISCONNECTED;
>      }
>      di_info("ethtool-lite: %s: could not determine carrier state; got 
> \"%s\"",
>          iface, result);
>      return UNKNOWN;
> 
> Cheers,
> 
> Aitor.





-- 
Please do not email me anything that you are not comfortable also sharing with 
the NSA, CIA ...
___
Dng mailing list
Dng@lists.dyne.org
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng


Re: [DNG] an alternative to poettering's ifplugd

2019-03-19 Thread aitor_czr


On 19/3/19 9:56, aitor_czr wrote:


Hi Nick,

On 19/3/19 9:21, Dr. Nikolaus Klepp wrote:

You might call me ignorant, but with bash this can be done a bit simpler (this 
is from my solution to the detect-if-kable-present-while-booting-problem):-)

#!/bin/bash

while sleep 1; do
 for i in /sys/class/net/eth*; do
 VAR=$(basename $i)
 VAL=$(sed -n 's/0/UN/p' $i/carrier)PLUGGED
 if [ "${!VAR}" != "$VAL" ]; then
 echo $VAR $VAL
 declare $VAR=$VAL
 fi
 done
done


Nik
Thanks for your script! Interesting to know... With a short look, i 
seems to read the value content in the /sys/class/eth0/carrier file, 
set to "0" or "1".
I was thinking about this possibility a couple of days ago, but i'm 
not pretty sure if its value also depends on the status of the network 
hotpluggable device. I'm testing it...
On the other hand, here you are the code used in the ethtool-lite.c 
file of the netcfg udeb package used by debian-installer, doing 
something similar (in the case of the FreeBSD kernel

it uses a raw socket instead):

    int len = strlen(SYSCLASSNET) + strlen(iface) + strlen("/carrier") 
+ 1;

    char* filename = malloc(len);
    snprintf(filename, len, SYSCLASSNET "%s/carrier", iface);
    FILE* fp = fopen(filename, "r");
    free(filename);

    char result[2];
    if (fgets(result, sizeof(result), fp) == NULL) {
        fclose(fp);
        if (errno == EINVAL) {
            di_info("ethtool-lite: %s is down", iface);
            return DISCONNECTED;
        }
        di_error("ethtool-lite: getting carrier failed: %s",
            strerror(errno));
        return UNKNOWN;
    }
    fclose(fp);

    switch (result[0]) {
    case '1':
        di_info("ethtool-lite: %s: carrier up", iface);
        return CONNECTED;
    case '0':
        di_info("ethtool-lite: %s: carrier down", iface);
        return DISCONNECTED;
    }
    di_info("ethtool-lite: %s: could not determine carrier state; got 
\"%s\"",

        iface, result);
    return UNKNOWN;

Cheers,


#if defined(__linux__)

    /* The above code */

#elif defined(__FreeBSD_kernel__)
    int fd = socket(AF_INET, SOCK_DGRAM, 0);

    if (fd < 0)
    {
        di_warning("ethtool-lite: could not open control socket\n");
        return UNKNOWN;

    }


___
Dng mailing list
Dng@lists.dyne.org
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng


Re: [DNG] an alternative to poettering's ifplugd

2019-03-19 Thread aitor_czr

Hi Nick,

On 19/3/19 9:21, Dr. Nikolaus Klepp wrote:

You might call me ignorant, but with bash this can be done a bit simpler (this 
is from my solution to the detect-if-kable-present-while-booting-problem):-)

#!/bin/bash

while sleep 1; do
 for i in /sys/class/net/eth*; do
 VAR=$(basename $i)
 VAL=$(sed -n 's/0/UN/p' $i/carrier)PLUGGED
 if [ "${!VAR}" != "$VAL" ]; then
 echo $VAR $VAL
 declare $VAR=$VAL
 fi
 done
done


Nik
Thanks for your script! Interesting to know... With a short look, i 
seems to read the value content in the /sys/class/eth0/carrier file, set 
to "0" or "1".
I was thinking about this possibility a couple of days ago, but i'm not 
pretty sure if its value also depends on the status of the network 
hotpluggable device. I'm testing it...
On the other hand, here you are the code used in the ethtool-lite.c file 
of the netcfg udeb package used by debian-installer, doing something 
similar (in the case of the FreeBSD kernel

it uses a raw socket instead):

    int len = strlen(SYSCLASSNET) + strlen(iface) + strlen("/carrier") + 1;
    char* filename = malloc(len);
    snprintf(filename, len, SYSCLASSNET "%s/carrier", iface);
    FILE* fp = fopen(filename, "r");
    free(filename);

    char result[2];
    if (fgets(result, sizeof(result), fp) == NULL) {
        fclose(fp);
        if (errno == EINVAL) {
            di_info("ethtool-lite: %s is down", iface);
            return DISCONNECTED;
        }
        di_error("ethtool-lite: getting carrier failed: %s",
            strerror(errno));
        return UNKNOWN;
    }
    fclose(fp);

    switch (result[0]) {
    case '1':
        di_info("ethtool-lite: %s: carrier up", iface);
        return CONNECTED;
    case '0':
        di_info("ethtool-lite: %s: carrier down", iface);
        return DISCONNECTED;
    }
    di_info("ethtool-lite: %s: could not determine carrier state; got 
\"%s\"",

        iface, result);
    return UNKNOWN;

Cheers,

Aitor.




___
Dng mailing list
Dng@lists.dyne.org
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng


Re: [DNG] an alternative to poettering's ifplugd

2019-03-19 Thread Dr. Nikolaus Klepp
Hi!

Anno domini 2019 Mon, 18 Mar 18:37:37 +0100
 aitor_czr scripsit:
> Hi all,
> 
> On 17/3/19 13:45, aitor_czr wrote:
> >
> > I'm also working on an alternative to poettering's ifplugd for the 
> > automatically wired connect option of simple-netaid.
> >
> > Aitor.
> >
> Here you are the code:
> 
> gnuinos.org/examples/netproc/main.c
> 
> Install the libiw-dev and ethtool packages, and build the file:
> 
> gcc main.c -liw -o main
> 
> and run it in the command line:
> 
> $ ./main
> 
> The output will be:
> 
> The wire is PLUGGED/UNPLUGGED
> 
> The unplug event is detected inmediatly, but the unplug event takes a 
> few seconds. It's working for me.
> 
> I need testers.
> 
> Thanks in advance :)
> 
> Aitor.
> 
> 
> 
> 

You might call me ignorant, but with bash this can be done a bit simpler (this 
is from my solution to the detect-if-kable-present-while-booting-problem) :-)

#!/bin/bash

while sleep 1; do
for i in /sys/class/net/eth*; do 
VAR=$(basename $i)
VAL=$(sed -n 's/0/UN/p' $i/carrier)PLUGGED
if [ "${!VAR}" != "$VAL" ]; then
echo $VAR $VAL
declare $VAR=$VAL
fi
done
done


Nik

-- 
Please do not email me anything that you are not comfortable also sharing with 
the NSA, CIA ...
___
Dng mailing list
Dng@lists.dyne.org
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng


Re: [DNG] an alternative to poettering's ifplugd

2019-03-18 Thread aitor_czr

Hi,

On 18/3/19 18:37, aitor_czr wrote:


Hi all,

On 17/3/19 13:45, aitor_czr wrote:


I'm also working on an alternative to poettering's ifplugd for the 
automatically wired connect option of simple-netaid.


Aitor.


Here you are the code:

gnuinos.org/examples/netproc/main.c

Install the libiw-dev and ethtool packages, and build the file:

gcc main.c -liw -o main

and run it in the command line:

$ ./main

The output will be:

The wire is PLUGGED/UNPLUGGED

The unplug event is detected inmediatly, but the unplug event takes a 
few seconds. It's working for me.


I need testers.

Thanks in advance :)

Aitor.

The ethtool dependency is superfluous, i removed the header and updated 
the sources in the link:


http://www.gnuinos.org/examples/netproc/

Cheers,

Aitor.


___
Dng mailing list
Dng@lists.dyne.org
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng


Re: [DNG] an alternative to poettering's ifplugd

2019-03-18 Thread aitor_czr

Hi all,

On 17/3/19 13:45, aitor_czr wrote:


I'm also working on an alternative to poettering's ifplugd for the 
automatically wired connect option of simple-netaid.


Aitor.


Here you are the code:

gnuinos.org/examples/netproc/main.c

Install the libiw-dev and ethtool packages, and build the file:

gcc main.c -liw -o main

and run it in the command line:

$ ./main

The output will be:

The wire is PLUGGED/UNPLUGGED

The unplug event is detected inmediatly, but the unplug event takes a 
few seconds. It's working for me.


I need testers.

Thanks in advance :)

Aitor.



___
Dng mailing list
Dng@lists.dyne.org
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng