:: Re: [DNG] an alternative to poetter…
Top Page
Delete this message
Reply to this message
Author: aitor_czr
Date:  
To: dng
Subject: Re: [DNG] an alternative to poettering's ifplugd
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.