unix

UNIX(7)                    Linux Programmer's Manual                   UNIX(7)



åå
       unix - ãã¼ã«ã«ãª ããã»ã¹ééä¿¡ç¨ã®ã½ã±ãã

æ¸å¼
       #include <sys/socket.h>
       #include <sys/un.h>

       unix_socket = socket(AF_UNIX, type, 0);
       error = socketpair(AF_UNIX, type, 0, int *sv);

説æ
       AF_UNIX (AF_LOCAL ã¨ãè¨ããã) ã½ã±ãããã¡ããªã¼ã¯ãåããã·ã³ä¸ã§ ããã»ã¹å士ã
       å¹ççã«éä¿¡ããããã«ç¨ãããããä¼çµ±çã«ãUNIX ãã¡ã¤ã³ ã½ã±ããã¯ãååãªãã«ãã§ãããã
       (ã½ã±ããåã§ããã¨å°ã®ã¤ãã) ãã¡ã¤ã« ã·ã¹ãã ã®ãã¹åã« çµã³ä»ãããã¨ãã§ãããããã«
       Linux ã§ã¯ããã¡ã¤ã« ã·ã¹ãã ã«ä¾åããªãæ½è±¡åå空é (abstract namespace)
       ããµãã¼ããã¦ããã

       æå¹ãªã¿ã¤ãã以ä¸ã«ç¤ºãã SOCK_STREAM ã¯ã¹ããªã¼ã æåã®ã½ã±ããã§ããã
       SOCK_DGRAM ã¯ã¡ãã»ã¼ã¸å¢çãä¿åãããã¼ã¿ã°ã©ã æåã®ã½ã±ããã§ãã (ã»ã¨ãã©ã®
       UNIX ã®å®è£ã§ã¯ãUNIX ãã¡ã¤ã³ã»ãã¼ã¿ã°ã©ã ã»ã½ã±ãã㯠常ã«
       ä¿¡é ¼ã§ãããã¼ã¿ã°ã©ã ã®ä¸¦ã³æ¿ãã¯è¡ããªã)ã SOCK_SEQPACKET
       ã¯ã¡ãã»ã¼ã¸å¢çãä¿åããéä¿¡ãããé åºã§ã¡ãã»ã¼ã¸ã å±ããæ¥ç¶æåã½ã±ãã ã§ãã
       (Linux 2.6.4 以éã§å©ç¨ã§ãã)ã

       UNIX ãã¡ã¤ã³ã½ã±ããã§ã¯ãè£å©ãã¼ã¿ã使ã£ã¦ ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã ãã‐
       ã»ã¹ã®ä¿¡ä»»ç¶ (credential) ã éåä¿¡ãããã¨ãã§ããã

   ã¢ãã¬ã¹ã®ãã©ã¼ããã
       UNIX ãã¡ã¤ã³ã½ã±ããã®ã¢ãã¬ã¹ã¯ä»¥ä¸ã®æ§é ä½ã§è¡¨ç¾ãããã

           #define UNIX_PATH_MAX    108

           struct sockaddr_un {
               sa_family_t sun_family;               /* AF_UNIX */
               char        sun_path[UNIX_PATH_MAX];  /* pathname */
           };

       sun_family ã«ã¯å¿ã AF_UNIX ãå¥ã£ã¦ããã

       ãã®æ§é ä½ã§ã¯ 3 種é¡ã®ã¢ãã¬ã¹ãåºå¥ãããã

       *  pathname (ãã¹å): bind(2) ã使ã£ã¦ãUNIX ãã¡ã¤ã³ã½ã±ããã NULL çµç«¯
          ããããã¡ã¤ã«ã·ã¹ãã ä¸ã® ãã¹åã«çµã³ä»ãããã¨ãã§ããã getsockname(2),
          getpeername(2), accept(2) ãã½ã±ããã®ã¢ãã¬ã¹ã è¿ãéã«ã¯ããã®é·ãã¯
          offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1 ã§ããã
          sun_path ã« NULL çµç«¯ããããã¹åãæ ¼ç´ãããã

       *  unnamed (ååãªã): bind(2)
          ã使ã£ã¦ãã¹åã«çµã³ä»ãããã¨ãã§ããªãã¹ããªã¼ã åã®ã½ã±ãã㯠ååãæããªããåæ§ã«ã
          socketpair(2)  ã§ä½æããã 2 ã¤ã®ã½ã±ãããååãæããªãã getsockname(2),
          getpeername(2), accept(2)  ãååãªãã®ã½ã±ããã®ã¢ãã¬ã¹ãè¿ãéã«ã¯ã
          ãã®é·ã㯠sizeof(sa_family_t) ã§ããã sun_path ã¯æ¤æ»ãã¹ãã§ã¯ãªãã

       *  abstract (æ½è±¡): æ½è±¡ã½ã±ããã¢ãã¬ã¹ã¯ã sun_path[0] ã NULL ãã¤ã
          ('\0') ã§ãããã¨ã§åºå¥ãããããã®åå空éã«ãããã½ã±ããã®ã¢ãã¬ã¹ ã¯ã sun_path
          ã®æ®ãã®ãã¤ãã®ãã¢ãã¬ã¹æ§é ä½ã®æå®ãããé·ãã®ç¯å²ã§è¡¨ã ãã (ååä¸ã® NULL
          ãã¤ãã«ã¯ç¹å¥ãªæå³ã¯ãªã)ããã®ååã¯ãã¡ã¤ã«ã·ã¹ãã ã®
          ãã¹åã¨ã¯ä½ã®é¢ä¿ããªãã getsockname(2), getpeername(2), accept(2)
          ãæ½è±¡ã½ã±ããã®ã¢ãã¬ã¹ãè¿ãéã«ã¯ãè¿ããã addrlen 㯠sizeof(sa_family_t)
          ãã大ãã (ã¤ã¾ã 2 ãã大ãã)ãã½ã±ããã®åå㯠sun_path ã®æåã® (addrlen -
          sizeof(sa_family_t)) ãã¤ãã«æ ¼ç´ãããã ã½ã±ããã®æ½è±¡åå空é㯠Linux
          ã«ããæ¡å¼µã§ããã移æ¤æ§ã¯ãªãã

   ã½ã±ãããªãã·ã§ã³
       æ´å²çãªçç±ã«ããããããã®ãªãã·ã§ã³ã¯ ãã¨ã AF_UNIX åºæã®ãªãã·ã§ã³ã§ãã£ã¦ã
       SOL_SOCKET åã§æå®ããã ã½ã±ãããã¡ããªã¼ã¨ã㦠SOL_SOCKET ãæå®ããã¨ã
       setsockopt(2)  ã§ãªãã·ã§ã³ãè¨å®ã§ãã getsockopt(2)  ã§åå¾ãã§ããã

       SO_PASSCRED
              éä¿¡ããã»ã¹ã®è£å©ã¡ãã»ã¼ã¸ã§ä¿¡ä»»ç¶ãåä¿¡ã§ããããã«ããããã®ãªãã·ã§ã³ã
              ã»ããããã¦ãã¦ãã¾ã ã½ã±ãããæ¥ç¶ããã¦ããªãã¨ãæ½è±¡åå空éã«ä»ã¨éãªã
              ãªãååãèªåçã«çæãããããã¼ã«æ´æ°å¤ã®ãã©ã°ãåãã

   èªåãã¤ã³ã (autobind) æ©è½
       bind(2) å¼ã³åºã㧠sizeof(sa_family_t) ã¨ã㦠addrlen ãæå®ãããã
       ã¢ãã¬ã¹ã«æ示çã«ãã¤ã³ãããã¦ããªãã½ã±ããã«å¯¾ã㦠SO_PASSCRED
       ã½ã±ãããªãã·ã§ã³ãæå®ããã¦ããå ´åã
       ãã®ã½ã±ããã¯æ½è±¡ã¢ãã¬ã¹ã«èªåçã«ãã¤ã³ããããã ãã®ã¢ãã¬ã¹ã¯ã1 åã® NULL
       ãã¤ãã®å¾ã«ãæåéå [0-9a-f] ã®ãã¤ãã 5
       åç¶ãå½¢å¼ã§ããããããã£ã¦ãèªåçã«ãã¤ã³ããããã¢ãã¬ã¹æ°ã«ã¯ 2^20 åã¨ããä¸éãåå¨ããã
       (Linux 2.1.15 以éã§ãèªåãã¤ã³ãæ©è½ã追å ãããã¨ãã«ã¯ã 8
       ãã¤ãã使ããã¦ãããèªåãã¤ã³ãã¢ãã¬ã¹æ°ã®ä¸é㯠2^32 ã§ãã£ãã Linux 2.3.15 㧠5
       ãã¤ãã«å¤æ´ãããã)

   ã½ã±ãã API
       ãã®ç¯ã§ã¯ãLinux ã® UNIX ãã¡ã¤ã³ã½ã±ããã§ã®ããã¡ã¤ã³åºæã®è©³ç´°ä»æ§ã¨
       ã½ã±ãã API ã§ãµãã¼ãããã¦ããªãæ©è½ã«ã¤ãã¦èª¬æããã

       UNIX ãã¡ã¤ã³ã½ã±ããã§ã¯ã帯åå¤ãã¼ã¿ (out-of-band data) ã® éä¿¡ (send(2)
       㨠recv(2) ã® MSG_OOB ãã©ã°) ã¯ãµãã¼ãããã¦ããªãã

       send(2) MSG_MORE ãã©ã°ã¯ UNIX ãã¡ã¤ã³ã½ã±ããã§ã¯ãµãã¼ãããã¦ããªãã

       recv(2) ã® flags å¼ãæ°ã§ã® MSG_TRUNC ã®ä½¿ç¨ã¯ UNIX ãã¡ã¤ã³
       ã½ã±ããã§ã¯ãµãã¼ãããã¦ããªãã

       SO_SNDBUF ã½ã±ãããªãã·ã§ã³ã¯ UNIX ãã¡ã¤ã³ã½ã±ããã§å¹æãæã¤ãã SO_RCVBUF
       ã¯å¹æããªãã ãã¼ã¿ã°ã©ã ã»ã½ã±ããã§ã¯ã SO_SNDBUF ã®å¤ã
       åºåãã¼ã¿ã°ã©ã ã®ä¸éãµã¤ãºã¨ãªãã å®éã®ä¸éå¤ã¯ã SO_SNDBUF ãªãã·ã§ã³
       ã¨ãã¦è¨å®ãããå¤ã® 2å (socket(7) åç§) ãããªã¼ããããã¨ãã¦ä½¿ç¨ããã 32
       ãã¤ããå¼ããå¤ã¨ãªãã

   è£å©ã¡ãã»ã¼ã¸
       è£å©ãã¼ã¿ãéåããã«ã¯ã sendmsg(2)  ã recvmsg(2)  ã使ç¨ããã æ‐
       ´å²çãªçç±ã«ããã以ä¸ã«ç¤ºãè£å©ã¡ãã»ã¼ã¸ã®å㯠ãã¨ã AF_UNIX
       åºæã®ãã®ã§ãã£ã¦ã SOL_SOCKET åã§æå®ããã ããããéãã«ã¯ãæ§é ä½ cmsghdr ã®
       cmsg_level ãã£ã¼ã«ãã« SOL_SOCKET ãã»ãããã cmsg_type
       ãã£ã¼ã«ãã«ã¿ã¤ããã»ããããã 詳細㯠cmsg(3)  ãè¦ãã

       SCM_RIGHTS
              ä»ã®ããã»ã¹ã§ãªã¼ãã³ããããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã®ã»ãããéåä¿¡ããã
              ãã¼ã¿é¨åã«ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã®æ´æ°éåãå¥ã£ã¦ããã
              渡ããããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã¯ããããã dup(2)  ã§çæããããã®ããã«æ¯ãèãã

       SCM_CREDENTIALS
              UNIX ä¿¡ä»»ç¶ãéåä¿¡ãããããã¯èªè¨¼ã«ç¨ãããã¨ãã§ããã ä¿¡ä»»ç¶ã¯ struct
              ucred ã®è£å©ã¡ãã»ã¼ã¸ã¨ãã¦æ¸¡ãããã ãã®æ§é ä½ã¯ <sys/socket.h>
              ã§ä»¥ä¸ã®ããã«å®ç¾©ããã¦ããã

                  struct ucred {
                      pid_t pid;    /* process ID of the sending process */
                      uid_t uid;    /* user ID of the sending process */
                      gid_t gid;    /* group ID of the sending process */
                  };

              glibc 2.8 以éã§ã¯ããã®æ§é ä½ã®å®ç¾©ãå¾ãããã«ã¯
              (ã©ã®ããããã¡ã¤ã«ãã¤ã³ã¯ã«ã¼ããããããåã«) æ©è½æ¤æ»ãã¯ã _GNU_SOURCE
              ãå®ç¾©ããªããã°ãªããªãã

              éä¿¡å´ãæå®ããä¿¡ä»»ç¶ã¯ãã«ã¼ãã«ããã§ãã¯ããã å®å¹ã¦ã¼ã¶ã¼ ID ã 0
              ã®ããã»ã¹ã«ã¯ã èªåèªèº«ä»¥å¤ã®å¤ãæå®ããäºã許ãããã éä¿¡å´ã¯ä»¥ä¸ã®
              3 ã¤ãæå®ããªããã°ãªããªãã 1) èªåèªèº«ã®ããã»ã¹ ID (CAP_SYS_ADMIN
              権éãæã£ã¦ããªãå ´å)ã 2) èªåèªèº«ã®ã¦ã¼ã¶ã¼ ID ãããã¯å®å¹ã¦ã¼ã¶ã¼
              ID ãä¿å set-user-ID (CAP_SETUID 権éãæã£ã¦ããªãå ´å)ã 3)
              èªåèªèº«ã®ã°ã«ã¼ã ID ãããã¯å®è¡ã°ã«ã¼ã ID ãä¿å set-group-ID
              (CAP_SETGID ãæã£ã¦ããªãå ´å)ã struct ucred
              ã¡ãã»ã¼ã¸ãåä¿¡ããããã«ã¯ãã½ã±ããã«å¯¾ã SO_PASSCRED
              ãªãã·ã§ã³ãæå¹ã«ããªãã¦ã¯ãªããªãã

   ioctl
       以ä¸ã® ioctl(2) å¼ã³åºã㯠value ã«æå ±ãå¥ãã¦è¿ãã æ£ããæ¸å¼ã¯ä»¥ä¸ã®éãã

              int value;
              error = ioctl(unix_socket, ioctl_type, &value);

       ioctl_type ã«ã¯ä»¥ä¸ãæå®ã§ãã:

       SIOCINQ
              åä¿¡ãããã¡ã®ãã¥ã¼ã«ãããã¾ã èªãã§ããªããã¼ã¿ã®éãè¿ããã½ã±ãã㯠LISTEN
              ç¶æã«ãã£ã¦ã¯ãªããããããªãã¨ã¨ã©ã¼ (EINVAL) ãè¿ãã SIOCINQ ã¯
              <linux/sockios.h> ã§å®ç¾©ããã¦ããã 代ããã«ã<sys/ioctl.h>
              ã§å®ç¾©ããã¦ãããå義èªã® FIONREAD ã使ããã¨ãã§ããã

ã¨ã©ã¼
       EADDRINUSE
              æå®ãããã¼ã«ã«ã¢ãã¬ã¹ãæ¢ã«ä½¿ç¨ããã¦ãããããã¡ã¤ã«ã·ã¹ãã ã®
              ã½ã±ãããªãã¸ã§ã¯ããæ¢ã«åå¨ãã¦ããã

       ECONNREFUSED
              connect(2) ã«ããæå®ããããªã¢ã¼ãã¢ãã¬ã¹ãæ¥ç¶å¾ã¡ã½ã±ããã§ã¯ãªãã£ãã
              ã¿ã¼ã²ããã¢ãã¬ã¹ãã½ã±ããã§ã¯ãªãå ´åã«ããã®ã¨ã©ã¼ãçºçããã

       ECONNRESET
              ãªã¢ã¼ãã½ã±ãããäºæããªãããã¡ã§ã¯ãã¼ãºãããã

       EFAULT ã¦ã¼ã¶ã¼ã¡ã¢ãªã¢ãã¬ã¹ãä¸æ£ã

       EINVAL 渡ããå¼æ°ãä¸æ£ãããããåå ã¨ãã¦ã¯ã渡ããã¢ãã¬ã¹ã® sun_type ãã£ã¼ã«
              ãã« AF_UNIX ãæå®ããã¦ããªãã£ããè¡ããã¨ããæä½ã«å¯¾ãã¦ã½ã±ãããæ
              å¹ãªç¶æã§ã¯ãªãã£ãããªã©ã

       EISCONN
              æ¢ã«æ¥ç¶ããã¦ããã½ã±ããã«å¯¾ã㦠connect(2)
              ãå¼ã°ãããã¾ãã¯ãæå®ããã¿ã¼ã²ããã¢ãã¬ã¹ã æ¢ã«æ¥ç¶æ¸ã¿ã®ã½ã±ããã ã£ãã

       ENOENT connect(2) ã«æå®ããããªã¢ã¼ãã¢ãã¬ã¹ã®ãã¹åãåå¨ããªãã£ãã

       ENOMEM ã¡ã¢ãªã足ããªãã

       ENOTCONN
              ã½ã±ããæä½ã«ã¿ã¼ã²ããã¢ãã¬ã¹ãå¿è¦ã ãã ãã®ã½ã±ããã¯æ¥ç¶ããã¦ããªãã

       EOPNOTSUPP
              ã¹ããªã¼ã æåã§ãªãã½ã±ããã«å¯¾ãã¦ã¹ããªã¼ã æä½ãå¼ã³åºãããã
              ã¾ãã¯å¸¯åå¤ãã¼ã¿ãªãã·ã§ã³ãç¨ãããã¨ããã

       EPERM  éä¿¡èã struct ucred ã«ä¸æ£ãªä¿¡ä»»ç¶ã渡ããã

       EPIPE  ãªã¢ã¼ãã½ã±ãããã¹ããªã¼ã ã½ã±ããä¸ã§ã¯ãã¼ãºãããã å¯è½ãªå ´åã¯
              SIGPIPE ãåæã«éãããããããé¿ããã«ã¯ MSG_NOSIGNAL ãã©ã°ã sendmsg(2)  ã
              recvmsg(2)  ã«æ¸¡ãã

       EPROTONOSUPPORT
              渡ããããããã³ã«ã AF_UNIX ã§ãªãã

       EPROTOTYPE
              ãªã¢ã¼ãã½ã±ããã¨ãã¼ã«ã«ã½ã±ããã®ã¿ã¤ããä¸è´ãã¦ããªãã£ã (SOCK_DGRAM
              㨠SOCK_STREAMESOCKTNOSUPPORT
              æªç¥ã®ã½ã±ããã¿ã¤ãã

       ä»ã«ãæ±ç¨ã®ã½ã±ãã層ã§ã¨ã©ã¼ãèµ·ãã£ããã
       ãã¡ã¤ã«ã·ã¹ãã ä¸ã«ã½ã±ãããªãã¸ã§ã¯ããä½ããã¨ããå ´åã«
       ãã¡ã¤ã«ã·ã¹ãã ã®ã¨ã©ã¼ãèµ·ãããã¨ãããã ããããã®è©³ç´°ã¯é©å㪠man
       ãã¼ã¸ãåç§ãããã¨ã

ãã¼ã¸ã§ã³
       SCM_CREDENTIALS ã¨æ½è±¡åå空éã¯ãLinux 2.2 ã§å°å¥ãããã 移æ¤æ§ãå¿è¦ãªãã‐
       ã°ã©ã ã§ã¯ä½¿ãã¹ãã§ã¯ãªãã (BSD ç±æ¥ã®ã·ã¹ãã ã®ä¸‐
       ã«ãä¿¡ä»»ç¶ã®éåä¿¡ããµãã¼ããã¦ãããã®ããããã
       ãã®å®è£ã®è©³ç´°ã¯ã·ã¹ãã ã«ãã£ã¦ç°ãªã)

注æ
       Linux ã®å®è£ã§ã¯ããã¡ã¤ã«ã·ã¹ãã ä¸ããè¦ããã½ã±ããã¯ãããããç½®ããã¦ã
       ããã£ã¬ã¯ããªã®ãã¼ããã·ã§ã³ã«å¾ããã½ã±ããã®ææèãã°ã«ã¼ãããã¼ããã·ã§
       ã³ã¯å¤æ´ã§ãããæ°ããã½ã±ãããä½ãã¨ããä½ããã¨ãããã£ã¬ã¯ããªã«å¯¾ã㦠ã ã‐
       ã»ã¹ãæ¸ãè¾¼ã¿ã¨æ¤ç´¢ (å®è¡) 権éãæã£ã¦ããªããã°ãä½æã«å¤±æãããã½ã±ã
       ããªãã¸ã§ã¯ãã«æ¥ç¶ããã«ã¯ã read/write 権éãå¿è¦ã§ããããã®åä½ã¯ãå¤ã ã® BSD
       ç±æ¥ã®ã·ã¹ãã ã¨ã¯ç°ãªã£ã¦ãã (BSD ã§ã¯ UNIX ãã¡ã¤ã³ã½ã±ããã«å¯¾ãã¦
       ã¯ãã¼ããã·ã§ã³ãç¡è¦ãã)ã 移æ¤æ§ã®å¿è¦ãªããã°ã©ã ã§ã¯ãã»ãã¥ãªãã£ãã
       ã®ä»æ§ã«ä¾åãã¦ã¯ãªããªãã

       ãã¡ã¤ã«åãæå®ãã¦ã½ã±ããã«ãã¤ã³ãããã¨ããã¡ã¤ã«ã·ã¹ãã ã«ã½ã±ããã
       çæããããããã¯å¿è¦ãªããªã£ãã¨ãã«å¼ã³ã ããã¦ã¼ã¶ã¼ãåé¤ããªããã° ãªããªã
       (unlink(2) ãç¨ãã)ã UNIX ã§é常使ããããèå¾ã§éããæ¹å¼ã
       ãé©ç¨ããããã½ã±ããã¯ãã¤ã§ã unlink ãããã¨ãã§ããæå¾ã®åç§ã ã¯ã‐
       ã¼ãºãããã¨ãã«ãã¡ã¤ã«ã·ã¹ãã ããåé¤ãããã

       SOCK_STREAM ä¸ã§ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãä¿¡ä»»ç¶ã渡ãããã«ã¯ãåã sendmsg(2)
       ã recvmsg(2) ã³ã¼ã«ã§è£å©ãã¼ã¿ä»¥å¤ã®ãã¼ã¿ãå°ãªãã¨ã 1
       ãã¤ãéä¿¡/åä¿¡ããå¿è¦ãããã

       UNIX ãã¡ã¤ã³ã®ã¹ããªã¼ã ã»ã½ã±ããã§ã¯ã 帯åå¤ãã¼ã¿ã®æ¦å¿µã¯ãµãã¼ããããªãã

ä¾
       bind(2)  åç§ã

       SCM_RIGHTS ã®ä½¿ç¨ä¾ã«ã¤ãã¦ã¯ cmsg(3) ãåç§ã

é¢é£é ç®
       recvmsg(2), sendmsg(2), socket(2), socketpair(2), cmsg(3),
       capabilities(7), credentials(7), socket(7)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2012-05-10                           UNIX(7)