UNIX domain socket怎么用(一)?

概述

问题来源: 一个开源项目用这个当消息队列。
说明: 本文根据一个例子,查询Linux手册,介绍相关的概念先扫个盲。下一篇写一个完整的测试代码。

代码示例

#include <sys/socket.h>
#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path)        \
                     + strlen ((ptr)->sun_path))

void * BindUnixDomain(char * path, int type, int max_msg_size){
    char * path;
    struct sockaddr_un n_us;
    int ossock = 0;

    memset(&n_us, 0, sizeof(n_us));
    n_us.sun_family = AF_UNIX;
    strncpy(n_us.sun_path, path, sizeof(n_us.sun_path) - 1);
    
    ossock = socket(PF_UNIX, type, 0);

    bind(ossock, (struct sockaddr *)&n_us, SUN_LEN(&n_us));
    
    chmod(path, 0660);
    
    listen(ossock, 128);

    // Set close-on-exec
    fcntl(ossock, F_SETFD, FD_CLOEXEC);
}

说明

相关函数: socket、bind、listen ...

#include <sys/socket.h>
#include <sys/un.h>
unix_socket = socket(AF_UNIX, type, 0);
error = socketpair(AF_UNIX, type, 0, int *sv);

#include <sys/types.h>         
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

什么是UNIX domain socket?

DESCRIPTION         

       The AF_UNIX (also known as AF_LOCAL) socket family is used to
       communicate between processes on the same machine efficiently.
       Traditionally, UNIX domain sockets can be either unnamed, or bound to
       a filesystem pathname (marked as being of type socket).  Linux also
       supports an abstract namespace which is independent of the
       filesystem.
  • 是一种叫AF_UNIX 的 socket family(区别于AF_INET,具体在另一篇讲)
  • UNIX下的一种高效的IPC(进程间通信)方式;
  • 可以在系统中运行的进程间共享一个文件描述符,还能给这个描述符命名,通过名称来使用它。

为什么参数是SOCK_DGRAM用法跟网络socket一样吗?

DESCRIPTION         

       ...
       Valid socket types in the UNIX domain are: 
              SOCK_STREAM, for a stream-oriented socket; 
              SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and dont reorder datagrams); 
              and (since Linux 2.6.4) SOCK_SEQPACKET, for a sequenced-packet socket that is connection-oriented, preserves message boundaries, and delivers messages in the order that they were sent.
       ...

  • UNIX domain socket跟网络通信中的socket一样可以用数据流(stream)和数据报(datagram)的方式传输数据

为什么AF_UNIX和PF_UNIX有什么区别?

NOTES         top
       POSIX.1 does not require the inclusion of <sys/types.h>, and this
       header file is not required on Linux.  However, some historical (BSD)
       implementations required this header file, and portable applications
       are probably wise to include it.

       The manifest constants used under 4.x BSD for protocol families are
       PF_UNIX, PF_INET, and so on, while AF_UNIX, AF_INET, and so on are
       used for address families.  However, already the BSD man page
       promises: "The protocol family generally is the same as the address
       family", and subsequent standards use AF_* everywhere.

  • PF_UNIX 是为了兼容设置的, 跟AP_UNIX 意思一样

结构体sockaddr_un是什么?

A UNIX domain socket address is represented in the following structure:

   struct sockaddr_un {
       sa_family_t sun_family;               /* AF_UNIX */
       char        sun_path[108];            /* Pathname */
   };
The sun_family field always contains AF_UNIX.  On Linux, sun_path is 108 bytes in size;

  • 用于保存domain socket 地址的结构体
  • 对应AF_INET中的sockaddr_in
  • 主要给bind这种函数用
  • sun_family字段都应该设置成AF_UNIX.
  • sun_path在Linux上是108字节

sun_path字段如何设置?


Three types of address are distinguished in the sockaddr_un structure:

       *  pathname: a UNIX domain socket can be bound to a null-terminated
          filesystem pathname using bind(2).  When the address of a pathname
          socket is returned (by one of the system calls noted above), its
          length is

              offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1

          and sun_path contains the null-terminated pathname.  (On Linux,
          the above offsetof() expression equates to the same value as
          sizeof(sa_family_t), but some other implementations include other
          fields before sun_path, so the offsetof() expression more portably
          describes the size of the address structure.)

       *  unnamed: A stream socket that has not been bound to a pathname
          using bind(2) has no name.  Likewise, the two sockets created by
          socketpair(2) are unnamed.  
          ...

       *  abstract: an abstract socket address is distinguished (from a
          pathname socket) by the fact that sun_path[0] is a null byte
          ('\0').  ...

Pathname sockets
       When binding a socket to a pathname, a few rules should be observed
       for maximum portability and ease of coding:

       *  The pathname in sun_path should be null-terminated.

       *  The length of the pathname, including the terminating null byte,
          should not exceed the size of sun_path.

  • sun_path字段有三种类型: 本文只关心pathname类型,即把sun_path设置为一个文件路径,用于绑定到该文件。
  • pathname 是一个null-teminated字符串
  • 包括null byte在内,pathname长度要小与sun_path(108字节)

SUN_LEN宏是干嘛的?


Pathname sockets
       ...
       *  The addrlen argument that describes the enclosing sockaddr_un
          structure should have a value of at least:

              offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1

          or, more simply, addrlen can be specified as sizeof(struct sock‐
          addr_un).

       There is some variation in how implementations handle UNIX domain
       socket addresses that do not follow the above rules.  For example,
       some (but not all) implementations append a null terminator if none
       is present in the supplied sun_path.

       When coding portable applications, keep in mind that some implementa‐
       tions have sun_path as short as 92 bytes.

       Various system calls (accept(2), recvfrom(2), getsockname(2),
       getpeername(2)) return socket address structures.  When applied to
       UNIX domain sockets, the value-result addrlen argument supplied to
       the call should be initialized as above.  Upon return, the argument
       is set to indicate the actual size of the address structure.  The
       caller should check the value returned in this argument: if the out‐
       put value exceeds the input value, then there is no guarantee that a
       null terminator is present in sun_path.  (See BUGS.)
  • 计算sockaddr_un的长度,在Linux下直接用sizeof(struct sockaddr_un)就可以,这里用是为了兼容性考虑。

绑定的文件能不能用cat等其他进程查看?

If the file already exists when we try to bind the same address,  the bind request will fail.
When we close the socket, this file is not automatically removed,  so we need to make sure that we unlink it before our application exits.
  • 这个文件就像端口一样,不能多次绑定
  • 关闭socket或者进程退出后也不会自动删除
  • 要重启进程需要先unlink。

chmod的作用?

   Pathname socket ownership and permissions
       In the Linux implementation, pathname sockets honor the permissions
       of the directory they are in.  Creation of a new socket fails if the
       process does not have write and search (execute) permission on the
       directory in which the socket is created.

       On Linux, connecting to a stream socket object requires write permis‐
       sion on that socket; sending a datagram to a datagram socket likewise
       requires write permission on that socket.  POSIX does not make any
       statement about the effect of the permissions on a socket file, and
       on some systems (e.g., older BSDs), the socket permissions are
       ignored.  Portable programs should not rely on this feature for secu‐
       rity.

       When creating a new socket, the owner and group of the socket file
       are set according to the usual rules.  The socket file has all per‐
       missions enabled, other than those that are turned off by the process
       umask(2).

       The owner, group, and permissions of a pathname socket can be changed
       (using chown(2) and chmod(2)).

  • 向Pathname socket收发数据,需要通信的进程都拥有对文件的读写权限

其他疑问

为什么用unix domain socket?
什么是socket family?
什么是POSIX, BSD?

参考资料

Linux Programmer's Manual UNIX(7)
Linux Programmer's Manual SOCKET(2)
Advanced Programming in the UNIX Environment 3rd Edition

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,539评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,911评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,337评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,723评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,795评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,762评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,742评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,508评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,954评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,247评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,404评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,104评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,736评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,352评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,557评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,371评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,292评论 2 352

推荐阅读更多精彩内容