資訊人筆記

Work hard, Have fun, Make history!

使用者工具

網站工具


course:nctu-高等unix程式設計:chapter17

*Advanced IPC

0x00 Outline

  • Introduction
  • UNIX domain socket
  • Passing file descriptors
  • Passing file descriptors and the credentials
  • Examples

0x01 Introduction

前面幾個章節已經討論過了多種 IPC,包括 pipe 和 socket,這個章節則聚焦在 UNIX domain sockets

我們可以在 processes 間傳遞開啟的 file descriptors

server 可以藉由名稱關聯到 file descriptors

clients 也可使用這些名稱去連線到 servers


0x02 UNIX domain socket

UNIX domain socket

  • UNIX domain socket 可以讓跑在同一台機器上的 processes 互相溝通
  • 他只複製 data 不涉及 protocols,基本上效能優於 Internet domain sockets
    • No headers, checksums, sequence numbers, and acknowledgements …
  • UNIX domain socket 也同時提供了 stream 和 datagram interfaces
  • UNIX domain sockets 是可靠傳輸,訊息不會遺失或 delivered out-of-order

Unnamed UNIX domain sockets

#include <sys/socket.h>
 
int socketpair(int domain, int type, int protocol, int sockfd[2]);
/* Returns: zero if success, or -1 on error */
  • domain: Specifies the communications domain in which the sockets are to be created
  • type: Specifies the type of sockets to be created
    • SOCK_STREAM
    • SOCK_DGRAM
    • SOCK_SEQPACKET
  • protocol: Specifies a particular protocol to be used with the sockets
    • 用 0 的話 socketpair() 會依據 requested socket type 決定適當的 protocol
  • sockfd: Specifies a 2-integer array to hold the file descriptors of the created socket pair
  • socketpair() works like a two-way (full-duplex) pipe
  • 有些 BSD 系統使用 socketpair() function 實作 pipe
  • The write end of the first descriptor and the read end of the second descriptor are both closed
int s_pipe(int fd[2])
{
    return(socketpair(AF_UNIX, SOCK_STREAM, 0, fd));
}
  • 使用 UNIX domain socket 建立 full-duplex pipe

Naming UNIX domain sockets

  • socketpair() function 雖然可以建立 socket 連接相關 processes,但這個 socket 並沒有名字,這表示他無法被其他無相關的 processes 定址
/* The sockaddr_un structure (on Linux and Solaris) */
struct sockaddr_un
{
    sa_family_t sun_family;   /* AF_UNIX */
    char sun_path[108];       /* pathname */
};
 
/* The sockaddr_un structure (on BSD and Mac OS X) */
struct sockaddr_un
{
    unsigned char sun_len;    /* length including null */
    sa_family_t sun_family;   /* AF_UNIX */
    char sun_path[108];       /* pathname */
};

Bind a UNIX domain socket

int main(void)
{
    int fd, size;
    struct sockaddr_un un;
 
    un.sun_family = AF_UNIX;
    strcpy(un.sun_path, "foo.socket");
 
    if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
    {
        err_sys("socket failed");
    }
 
    size = offsetof(struct sockaddr_un, sun_path)+ strlen(un.sun_path);
 
    if (bind(fd, (struct sockaddr *)&un, size) < 0)
    {
        err_sys("bind failed");
    }
 
    printf("UNIX domain socket bound\n");
    exit(0);
}

Unique Connections

  • server 可以配置獨立的 UNIX domain 藉由 bind, listen, 和 accept function 跟 clients 建立連線
  • clients 使用 connect function 與 server 聯繫
  • 當 connect request 被 server 接受後,一個 unique connection 就被建立了
  • 過程基本上和 Internet domain sockets 相同

0x03 Passing File Descriptors

course/nctu-高等unix程式設計/chapter17.txt · 上一次變更: 127.0.0.1