How to Build a Simple USB Network Card Monitor Daemon (ethcheck) on Linux
This guide explains how to create a lightweight daemon called ethcheck that continuously monitors the presence of a USB Ethernet adapter on Linux, checks its IP configuration, and automatically adjusts the interface using low‑level system calls and scripts.
1. Implementing USB Network Card Monitoring
The article starts by noting that the open‑source tool ifplugd can monitor network interface status, but it only works when the interface is already registered in the system (visible via ifconfig -a). USB Ethernet adapters are registered only after being plugged in, and they may be unplugged at any time, so a custom solution is needed.
2. Design of the ethcheck Program
2.1 Program Flowchart
2.2 Daemonizing the Process
Because the program must run in the background and stay resident, it is turned into a daemon. The essential steps are fork, exit the parent, create a new session with setsid(), change the working directory to /tmp, reset the file‑creation mask, and close all open file descriptors.
void init_daemon(void) {
int pid;
int i;
if (pid = fork())
exit(0); // parent exits
else if (pid < 0)
exit(1); // fork failed
setsid(); // become session leader
chdir("/tmp");
umask(0);
for (i = 0; i < NOFILE; ++i) close(i);
return;
}2.3 Checking Whether a Specific Interface Exists
The program reads /proc/net/dev to see if the target interface name appears in the list.
cat /proc/net/dev
Inter-| Receive ...
lo: 26163 ...
eth0: 285444708 ...The following C functions parse that file, extract the interface name, and return 1 if the interface is present, 0 otherwise.
static char *interface_name_cut(char *buf, char **name) {
while (*buf == ' ') buf++;
*name = buf;
char *stat = strrchr(buf, ':');
*stat++ = '\0';
return stat;
}
int check_interface_fromproc(char *interface) {
FILE *fp = fopen(_PATH_PROC_NET_DEV, "r");
if (fp == NULL) { printf("open proc file error
"); return -1; }
char buf[PROCBUFSIZ];
fgets(buf, PROCBUFSIZ, fp); // skip header lines
fgets(buf, PROCBUFSIZ, fp);
while (fgets(buf, PROCBUFSIZ, fp) != NULL) {
char *name;
interface_name_cut(buf, &name);
if (strcmp(interface, name) == 0) return 1;
}
fclose(fp);
return 0;
}2.4 Handling the Case When the Interface Is Missing
If the interface is not found, the daemon sleeps for a short interval and then re‑checks /proc/net/dev again.
2.5 When the Interface Exists – Verify Its IP Address
When the interface is present, the program obtains its IP address using the ioctl() system call with the SIOCGIFADDR request.
int getLocalIp(const char *eth, char *ip) {
struct ifreq ifr;
struct sockaddr_in sin;
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) return -1;
strcpy(ifr.ifr_name, eth);
if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) { close(fd); return -1; }
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
snprintf(ip, IP_SIZE, "%s", inet_ntoa(sin.sin_addr));
close(fd);
return 0;
}The retrieved address is stored in the buffer passed as ip.
2.6 Comparing the Obtained IP with the Desired One
If the current IP matches the target IP, the daemon simply sleeps for a configurable period before the next check.
2.7 Changing the IP When It Differs
If the IP does not match, the daemon invokes a pre‑written shell script if.sh to reconfigure the interface.
#!/bin/bash
IPADDR=192.168.40.8
ETHPORT=eth1
echo "ethcheck set" $ETHPORT $IPADDR
if [ $# -eq 2 ]; then
if [ $1 = $ETHPORT ]; then
if [ $2 = "up" ]; then
ifconfig $ETHPORT $IPADDR
sleep 1
ip rule add from all lookup main pref 9000
sleep 1
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -F
echo "set $ETHPORT done"
elif [ $2 = "down" ]; then
echo "down"
elif [ $2 = "disable" ]; then
echo "disable"
elif [ $2 = "error" ]; then
echo "error"
fi
fi
fiThis script sets the interface IP, updates routing rules, enables IP forwarding, and flushes iptables as needed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
