Skip to content

Day53 - UNIX Domain Socket IPC

Objective

Build a local IPC middleware daemon using:

  • AF_UNIX socket
  • epoll
  • multi-client dispatcher
  • /dev/mypoll
  • kernel event broadcast

Environment

Platform

  • Raspberry Pi 5
  • Debian Linux
  • UNIX domain socket

Directory Structure

day53-unix-ipc/
├── Makefile
├── server.c
├── client.c
├── log.h
└── mypoll_timer.c

Build Userspace Applications

Native build

make

Cross compile for Raspberry Pi 5

make CROSS=1

Start Server

./server

Expected:

[INFO] Server listening on /tmp/mypoll.sock

Connect Using Client

./client client-1

Expected:

[INFO] Connected to /tmp/mypoll.sock

Connect Using netcat

nc -U /tmp/mypoll.sock

Test commands:

echo hello
trigger
start_timer
stop_timer

Multi-Client Test

Open multiple clients

for i in 1 2 3 4 5; do
    ./client client-$i &
done

wait

Expected:

  • multiple client connections
  • independent event handling
  • no dispatcher blocking

Broadcast Test

Terminal 1

nc -U /tmp/mypoll.sock

Terminal 2

nc -U /tmp/mypoll.sock

Terminal 3

echo "trigger" | nc -U /tmp/mypoll.sock

Expected:

  • all connected clients receive event

Backpressure Test

Modify client to continuously send commands:

while (1) {
    client_send_message(...);
}

Expected:

  • TX queue full
  • EPOLLIN disabled temporarily
  • dispatcher remains alive
  • no server crash

Verify /dev/mypoll Integration

Start timer

echo "start_timer" | nc -U /tmp/mypoll.sock

Expected:

OK start timer

Then periodic events should appear.


Event Flow Verification

Expected architecture:

client command
server parser
write(/dev/mypoll)
kernel event
epoll dispatcher
broadcast
all clients

Cleanup

Remove socket file:

rm -f /tmp/mypoll.sock