Day54 - UNIX Domain Socket Advanced IPC¶
Overview¶
This chapter extends the previous AF_UNIX middleware daemon implementation and explores advanced Linux local IPC concepts.
Topics include:
- Abstract namespace UNIX sockets
- Linux credential passing (
SO_PEERCRED) - Permission control based on UID/GID
SOCK_STREAMvsSOCK_DGRAM- Datagram request/reply model
- Datagram truncation behavior
Abstract Namespace UNIX Socket¶
Traditional UNIX domain sockets are pathname-based:
These sockets exist inside the filesystem and require:
before bind().
Linux additionally supports:
which does not create filesystem entries.
Example:
Characteristics:
- Linux-specific extension
- No filesystem inode
- No
unlink() - Automatically removed when process exits
Check active abstract sockets:
Example output:
sockaddr_un Length Calculation¶
Abstract sockets are not normal C strings.
Correct address length:
Where:
+1represents the leading'\0'- remaining bytes are the abstract socket name
Using sizeof(struct sockaddr_un) is not recommended because
unused bytes may become part of the socket name.
SO_PEERCRED¶
Linux supports retrieving peer credentials from AF_UNIX sockets.
Example:
Returned information:
- PID
- UID
- GID
Example:
This enables:
- Local IPC authentication
- Permission control
- Process tracing
Credential-Based Authorization¶
The daemon can restrict commands based on UID.
Example:
Example authorization:
This model is commonly used in:
- systemd
- dbus-daemon
- bluetoothd
- Android services
SOCK_STREAM vs SOCK_DGRAM¶
SOCK_STREAM¶
Characteristics:
- Connection-oriented
- Ordered byte stream
- Requires framing
- Supports partial send/recv
- Requires TX queue handling
Example issues:
- Sticky packets
- Partial reads
- Backpressure
Typical APIs:
SOCK_DGRAM¶
Characteristics:
- Packet-oriented
- Message boundary preserved
- No connection lifecycle
- No partial packet delivery
- One send = one receive
Typical APIs:
DGRAM Request / Reply Flow¶
Client:
Server:
No accept() or dedicated client fd is required.
Sender information is included inside each datagram packet.
Message Boundary Preservation¶
Example:
Client sends:
Server receives:
Packets are never merged.
This differs from SOCK_STREAM, where multiple messages
may be combined into a single read.
Datagram Truncation¶
If a datagram packet exceeds the RX buffer size:
- The packet is truncated
- Remaining bytes are discarded
- Next
recvfrom()receives the next packet
Unlike stream sockets:
Example detection:
Typical handling:
- Log warning
- Drop packet
- Continue server operation
Key Learning Points¶
- Linux abstract namespace sockets
- AF_UNIX credential passing
- Local IPC authorization model
- Packet-oriented communication
- Datagram truncation semantics
- STREAM vs DGRAM architecture differences