site  contact  subhomenews

pup_event now detects cable plug/unplug

February 14, 2019 — BarryK

I have reported recent work with the minibase network utilities:

http://bkhome.org/news/201902/basic-ethernet-setup-with-minibase.html

The 'ifmon' daemon detects "aliveness" of an ethernet interface. That is, if a cable is plugged-in, with a router or something at the other end, that is working, then there is a "carrier" on the cable. It doesn't mean that we have Internet connection or whatever, it just shows that the cable is connected and functioning.

The carrier would go down if the router is turned off, back up if turned on -- however, we observed the carrier going up and down a few times, over about 30 seconds, before staying up, which does complicate how we will respond to this information. Discussion here:

https://github.com/arsv/minibase/issues/8

Putting aside minibase for awhile, being able to detect cable plug/unplug and router on/off, is very important to have as a basic feature in EasyOS, so that the network connection can be dynamically brought up/down in response. Rather than the current situation, where the ethernet connection is expected to be active at bootup, and there is no dynamic response afterward.

The utility 'pup_event_frontend_d' is launched at bootup and detects various conditions, such as whether X is up, whether there is a Internet connection. This is part of 'pup_event', described here:

http://bkhome.org/docs/pup_event/pup_event-service-management.htm

'pup_event_frontend-d' is written in BaCon and is in the 'pup-tools' PET. I have enhanced the daemon to detect carrier up/down, and here are the latest binary packages:

http://distro.ibiblio.org/easyos/amd64/packages/compat/oe/pyro/pup-tools-20190215-r0-nocona-64.tar.xz

http://distro.ibiblio.org/easyos/aarch64/packages/compat/oe/pyro/pup-tools-20190215-r0-aarch64.tar.xz

These were compiled in oe-qky-src, my fork of OpenEmbedded:

https://github.com/bkauler/oe-qky-src/tree/master/quirky/meta-quirky/recipes-quirky/pup-tools

The latest source for pup-tools is here;

http://distro.ibiblio.org/easyos/source/oe/pyro/pup-tools-20190215.tar.gz

'pup_event_frontend_d' uses a netlink socket to monitor events from the kernel. Extracting a tiny part of the code, there is a seconf socket added, to wait for 'NETLINK_ROUTE' events:

pfd2.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)

The code that receives the messages is very simple:

 IF pfd2.revents NE 0 THEN
len = recv(pfd2.fd, buf, size_buf, MSG_DONTWAIT)
IF len==-1 THEN END 4
IF netcnt EQ 0 THEN
netcnt=50
'post to ipc clients, if any...
ipc_post_func("netchg_","change")
'also, let us be proactive, call a script directly...
IF FILEEXISTS("/usr/local/pup_event/netchg") THEN
SYSTEM "/usr/local/pup_event/netchg &"
ENDIF
ENDIF
ENDIF

This posts a "netchg_" IPC message to any clients that might be waiting on it. The pup_event IPC mechanism is explained here:

http://bkhome.org/docs/pup_event/pup_event_ipc.htm

Not only does this daemon detect cable carrier up/down, it also detects a USB wifi dongle plugged/unplugged. The 'netcnt' variable is to take care of the rapid carrier up/down that occurs when the router is turned on -- there is an IPC generated on the first change, and any changes over the next 50 seconds are ignored.

I have not yet written a client daemon. Will probably name it something like 'netchgd' and it will start at bootup and await "netchg_" IPC messages. It will then examine /sys to determine exactly what has changed, 

Tags: easy