dup

DUP(2)                     Linux Programmer's Manual                    DUP(2)



åå
       dup, dup2, dup3 - ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ãè¤è£½ãã

æ¸å¼
       #include <unistd.h>

       int dup(int oldfd);
       int dup2(int oldfd, int newfd);

       #define _GNU_SOURCE             /* feature_test_macros(7) åç§ */
       #include <fcntl.h>              /* å®æ° O_* ã®å®ç¾©ã®åå¾ */
       #include <unistd.h>

       int dup3(int oldfd, int newfd, int flags);

説æ
       ãããã®ã·ã¹ãã ã³ã¼ã«ã¯ããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ oldfd ã®è¤è£½ãä½ãã

       dup()  ã¯æãå°ããçªå·ã®æªä½¿ç¨ã®ãã£ã¹ã¯ãªãã¿ã
       æ°ãããã£ã¹ã¯ãªãã¿ã¨ãã¦ä½¿ç¨ããã

       dup2()  㯠newfd ã oldfd ã®è¤è£½ã¨ãã¦ä½æããã å¿è¦ã§ããã°æåã« newfd ãã¯ã‐
       ã¼ãºããã 以ä¸ã®ç¹ã«æ³¨æãããã¨ã

       *  oldfd ãæå¹ãªãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã§ãªãå ´åããã®å¼ã³åºãã¯å¤±æãã newfd
          ã¯ã¯ãã¼ãºãããªãã

       *  oldfd ãæå¹ãªãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã§ã newfd ã oldfd ã¨åãå¤ã®å ´åã
          dup2()  ã¯ä½ãããã newfd ãè¿ãã

       ãããã®ã·ã¹ãã ã³ã¼ã«ã®ãããããæåãè¿ããå ´åã«ã¯ã
       å¤ããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¨æ°ãããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¯
       äºãã«å¯æãªãã®ã¨ãã¦ä½¿ããã¨ãã§ããã
       2ã¤ã®ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¯åããã¡ã¤ã«è¨è¿° (description)  (open(2) åç§)
       ãåç§ãã¦ããããããã£ã¦ãã¡ã¤ã«ãªãã»ããããã¡ã¤ã«ç¶æãã©ã°ã
       å±æããããä¾ãã°ãä¸æ¹ã®ãã£ã¹ã¯ãªãã¿ã«å¯¾ã㦠lseek(2)
       ã使ã£ã¦ãã¡ã¤ã«ãªãã»ãããå¤æ´ããå ´åãããä¸æ¹ã®ãã£ã¹ã¯ãªãã¿ã® ãªãã»ãããå¤åããã

       2ã¤ã®ãã£ã¹ã¯ãªãã¿ã¯ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã»ãã©ã° (close-on-exec flag)
       ãå±æããªããè¤è£½ããããã£ã¹ã¯ãªãã¿ã® close-on-exec flag (fcntl(2)  åç§) ã¯
       off ã¨ãªãã

       dup3()  㯠dup2()  ã¨åãã ãã以ä¸ã®ç¹ãç°ãªãã

       *  å¼ã³åºãåããæ°ãããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã«å¯¾ã㦠close-on-exec
          ãã©ã°ãå¼·å¶çã«è¨å®ãããã¨ãã§ããã ãããè¡ãã«ã¯ã flags ã« O_CLOEXEC ãæå®ããã
          ãã®ãã©ã°ãå½¹ã«ç«ã¤çç±ã«ã¤ãã¦ã¯ã open(2)  ã® O_CLOEXEC
          ãã©ã°ã®èª¬æãåç§ã®ãã¨ã

       *  oldfd ã newfd ã¨åãå ´åã dup3()  㯠EINVAL ã¨ã©ã¼ã§å¤±æããã

è¿ãå¤
       æåããã¨ããããã®ã·ã¹ãã ã³ã¼ã«ã¯æ°ãããã£ã¹ã¯ãªãã¿ãè¿ãã ã¨ã©ã¼ã®å ´åã-1 ãè¿ãã
       errno ãé©åã«è¨å®ããã

ã¨ã©ã¼
       EBADF  oldfd ããªã¼ãã³ããããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã§ãªããã newfd
              ããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¨ãã¦è¨±ãããç¯å²ããå¤ãã¦ããã

       EBUSY  (Linux ã®ã¿)  open(2)  ã dup()  ã¨ã®ç«¶åç¶æã®å ´åã«ã dup2()  ã
              dup3() ã¯ãã®ã¨ã©ã¼ãè¿ããããããªãã

       EINTR  dup2()  ã dup3()  ã®å¼ã³åºããã·ã°ãã«ã«ããå²ãè¾¼ã¾ããã signal(7)
              åç§ã

       EINVAL (dup3())  flags ã«ç¡å¹ãªå¤ãå¥ã£ã¦ããã ãããã¯ã oldfd ã newfd
              ã¨åãã§ãã£ãã

       EMFILE ããã»ã¹ããã§ã«ãªã¼ãã³ã§ããæ大æ°ã¾ã§ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿
              ãéãã¦ãã¦ãããã«æ°ãããã®ãéããã¨ããã

ãã¼ã¸ã§ã³
       dup3()  ã¯ãã¼ã¸ã§ã³ 2.6.27 㧠Linux ã«è¿½å ãããã glibc
       ã«ãããµãã¼ãã¯ãã¼ã¸ã§ã³ 2.9 以éã§å©ç¨ã§ããã

æºæ
       dup(), dup2(): SVr4, 4.3BSD, POSIX.1-2001.

       dup3()  㯠Linux åºæã§ããã

注æ
       newfd ãç¯å²ãè¶ããæã«è¿ãããã¨ã©ã¼ã¯ã dup2()  㨠fcntl(..., F_DUPFD, ...)
       ã§ã¯ç°ã£ã¦ããã dup2()  ã F_DUPFD ã¨åãããã« EINVAL ãè¿ãã·ã¹ãã ãããã

       newfd ããªã¼ãã³ããã¦ããã¨ã close(2)  ããæã«å ±åãããã¯ãã®ã¨ã©ã¼ã失ããã¦ãã¾ãã
       dup2()  ã dup3()  ã使ãåã«åã newfd ãã¯ãã¼ãºããããã«ããæ¹ãããã ããã

é¢é£é ç®
       close(2), fcntl(2), open(2)

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



Linux                             2012-02-14                            DUP(2)