How to Identify Socket-Owning Processes in BusyBox When Netstat Lacks -p Option


13 views

Working with legacy embedded systems like DreamBox running ancient BusyBox versions (v1.01 in this case) presents unique challenges. The built-in netstat implementation lacks the crucial -p flag that shows process ownership of sockets - a feature we often take for granted in modern Linux distributions.

Here are several workarounds I've found effective when debugging network connections on such constrained systems:

Method 1: Using /proc/net/tcp

Parse the raw TCP connection information:

cat /proc/net/tcp | while read line; do
    inode=$(echo $line | awk '{print $10}')
    [ -z "$inode" ] && continue
    find /proc -name "fd" 2>/dev/null | while read proc; do
        ls -l $proc 2>/dev/null | grep -q "socket:\[$inode\]" && 
        echo "Found socket owner: $(echo $proc | cut -d'/' -f3)"
    done
done

Method 2: lsof Alternative

If you can install additional packages, try this compact lsof implementation:

for pid in /proc/[0-9]*; do
    pid=${pid#/proc/}
    for fd in /proc/$pid/fd/*; do
        link=$(readlink $fd)
        [[ $link == socket:* ]] && 
        echo "PID $pid owns socket $(echo $link | cut -d'[' -f2 | cut -d']' -f1)"
    done
done

Method 3: BusyBox ss Alternative

Some newer BusyBox builds include ss from iproute2:

ss -tulnp

Though this likely won't be available on your v1.01 system.

Let's say you want to identify what's handling HTTP connections on port 80:

grep -a ":0050" /proc/net/tcp | awk '{print $10}' | while read inode; do
    find /proc -name "fd" 2>/dev/null | xargs ls -l 2>/dev/null | 
    grep "socket:\[$inode\]" | awk -F'/' '{print $3}'
done

Note: :0050 is port 80 in hex (0x50).

  • The /proc filesystem must be mounted
  • Some methods require root privileges
  • These scripts may need adjustment based on your exact BusyBox features
  • For UDP connections, check /proc/net/udp instead

When working with embedded systems like DreamBox running ancient BusyBox versions (in this case v1.01 from 2008), you'll quickly discover that the netstat implementation lacks the -p flag for process identification. This creates a debugging nightmare when trying to correlate network connections with their owning processes.

Here are several approaches I've successfully used in production environments:


1. /proc/net/tcp Analysis:
# cat /proc/net/tcp | grep -i "0100007F:0016"  # Find local port 22
# ls -l /proc/*/fd/* 2>/dev/null | grep socket:\[$(grep -oP "socket:$$(\d+)$$" | cut -d: -f2 | tr -d ']')\]

2. lsof Alternative:
# find /proc -name "fd" -exec ls -l {} \; 2>/dev/null | grep -E "socket:$$[0-9]+$$" 

3. Manual Socket Inspection:
# for pid in $(ls /proc | grep '^[0-9]\+$'); do 
    ss -p | grep "pid=$pid" >/dev/null && echo "PID $pid has open sockets"; 
  done

When the above methods prove insufficient, consider this Python script that maps sockets to processes:


#!/usr/bin/env python
import os, re

def get_socket_mappings():
    socket_map = {}
    for pid in [d for d in os.listdir('/proc') if d.isdigit()]:
        try:
            fd_path = f"/proc/{pid}/fd"
            for fd in os.listdir(fd_path):
                link = os.readlink(f"{fd_path}/{fd}")
                if 'socket:' in link:
                    inode = re.search(r'socket:\[(\d+)\]', link).group(1)
                    socket_map[inode] = pid
        except (FileNotFoundError, PermissionError):
            continue
    return socket_map

if __name__ == "__main__":
    print("Socket to PID mapping:")
    print(get_socket_mappings())

For production systems requiring continuous monitoring, consider these approaches:

  • Implement an inotify watcher on /proc/net/tcp changes
  • Hook into the kernel's netlink interface via custom module
  • Cross-reference connection states with process open files

Remember that frequent /proc filesystem scanning impacts system performance. On resource-constrained devices:

  • Cache results for non-volatile connections
  • Limit scans to specific ports/protocols
  • Implement rate-limited monitoring