open

OPEN(2)                          System calls                          OPEN(2)



åå
       open, creat - ãã¡ã¤ã«ã¾ãã¯ããã¤ã¹ã®ãªã¼ãã³ãä½æãè¡ãªãã

æ¸å¼
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>

       int open(const char *pathname, int flags);
       int open(const char *pathname, int flags, mode_t mode);
       int creat(const char *pathname, mode_t mode);

説æ
       open ã¯ãã¡ã¤ã«ã®ãªã¼ãã³(open)ã®è©¦ã¿ããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¼( read,
       write, ãªã©ã§ä½¿ç¨ããå°ããªéè² ã®æ´æ°)ãè¿ãã

       flags 㯠O_RDONLY, O_WRONLY, O_RDWR ã®ã©ããä¸ã¤ã§ãããããèª‐
       ã¿è¾¼ã¿å°ç¨ãæ¸ãè¾¼ã¿å°ç¨ãèªã¿æ¸ãç¨ãè¦æ±ããã

       flags ã¯ããã«ä»¥ä¸ã®ä¸ã¤ä»¥ä¸ã®ãããOR(bitwise-or)ã§ãããã

       O_CREAT
              ãã¡ã¤ã«ãåå¨ããªãã£ãå ´åã¯ä½æ(create)ããã

       O_EXCL O_CREAT ã¨ä¸ç·ã«ä½¿ç¨ãããå ´åã ãã¡ã¤ã«ãæ¢ã«å‐
              å¨ããå ´åã¯ã¨ã©ã¼ã«ãªãã open ã¯å¤±æããã O_EXCL 㯠NFS
              ãã¡ã¤ã«ã»ã·ã¹ãã ã§ã¯æ©è½ããªãã ã‐
              ãã¯(lock)æ©è½ãªã©ã«ä½¿ç¨ããå ´åã«ã¯ç«¶åç¶æã«ãªãã ããã ã‐
              ãã¯ãã¡ã¤ã«ã使ç¨ããå解ä¸è½(atomic)ã®ããã¯æ©è½ã®è§£æ±ºã«ã¯
              åããã¡ã¤ã«ã»ã·ã¹ãã ã«ä»ã¨éãªããªããã¡ã¤ã«(ä¾ãã°ãã¹ãå㨠pid ã®
              çµã¿åãã)ãä½æãã link(2) ã使ç¨ãã¦ããã¯ãã¡ã¤ã«ãä½æãããã®ãã¡ã¤ã«ã«
              stat(2) ã使ç¨ãã¦ãªã³ã¯æ°(link count)ã 2 ã«ä¸æãã¦ãããããã§ãã¯ããã
              link() ã³ã¼ã«ã®è¿ãå¤ã使ç¨ãã¦ã¯ãããªãã

       O_NOCTTY
              pathname ã¯ç«¯æ«(terminal)ããã¤ã¹ãåç§ãã¦ãã¦ã — tty(4) ãåç§ããã㨠—
              ããã»ã¹ãå¶å¾¡ç«¯æ«ãæããªãå ´åã¯ããããå¶å¾¡ç«¯æ«ã«ãªãã

       O_TRUNC
              ãã¡ã¤ã«ãæ¢ã«åå¨ããå ´åãããã¯åãè©°ã(truncate)ãããã

       O_APPEND
              ãã¡ã¤ã«ã追å (append)ã¢ã¼ãã§ãªã¼ãã³ãããæåã¨ãããããã® write
              ã®åã«ãã¡ã¤ã«ã»ãã¤ã³ã¿ã¼ããã¡ã¤ã«ã®æå¾ã«ç§»åããã NFS
              ãã¡ã¤ã«ã»ã·ã¹ãã ã§ã¯ã O_APPEND ã§è¤æ°ã®ããã»ã¹ãåããã¡ã¤ã«ã«åæã«
              æ¸ãè¾¼ããã¨ããå ´åã¯å容ãç ´å£ãããããã㯠NFS ã追å ã¢ã¼ãã
              ãµãã¼ããããã¯ã©ã¤ã¢ã³ãã®ã«ã¼ãã«(kernel)ããããã·ãã¥ã¬ã¼ã
              ãã¦ããã®ã§ã競åç¶æã§ã¯ä½¿ç¨ã§ããªãããã§ããã

       O_NONBLOCK ã¾ã㯠O_NDELAY
              ãã¡ã¤ã«ã¯éåæ¢(non-blocking)ã¢ã¼ãã§ãªã¼ãã³ãããã open
              ãä»ã®å¨ã¦ã®ãã®ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¼ã«å¯¾ããæä½ã å¼ã³åºãããã‐
              ã»ã¹ãå¾ããããã¨ãªãè¿ãããã

       O_SYNC ãã¡ã¤ã«ã¯åæ(synchronous) I/O ã¢ã¼ãã§ãªã¼ãã³ãããã
              ãã®ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¼ã«å¯¾ããå¨ã¦ã® write
              ã¯å¯¾å¿ãããã¼ãã¦ã§ã¢ã«ç©ççã«æ¸ãè¾¼ã¾ããã¾ã§ã¯å¼ã³åºãã ãã‐
              ã»ã¹ã«æ»ããªãã 以ä¸ã®ãå¶éããè¦ãã

       ãããã®é¸æãã©ã°ã®ããã¤ãã¯ãã¡ã¤ã«ããªã¼ãã³ããå¾ã§ã fcntl
       ã使ç¨ãã¦å¤æ´ãããã¨ãã§ããã

       mode ã¯æ°ãããã¡ã¤ã«ãä½æããå ´åã«ä½¿ç¨ãã許å¯(permission)ãæå®ããã ããã¯ãã‐
       ã»ã¹ã® umask ã§é常éãä¿®æ£ãããä½æããããã¡ã¤ã«ã®è¨±å¯ã¯ (mode & ~umask)
       ã¨ãªãã

       mode ã®ããã«ä»¥ä¸ã®ã·ã³ãã«å®æ°ãæä¾ããã¦ãã :

       S_IRWXU
              00700 ã¦ã¼ã¶ã¼(ãã¡ã¤ã«ã®ææè) ã¯èªã¿è¾¼ã¿ãæ¸ãè¾¼ã¿ã
              å®è¡ã®è¨±å¯ãæã¤ã

       S_IRUSR (S_IREAD)
              00400 ã¦ã¼ã¶ã¼ã¯èªã¿è¾¼ã¿ã®è¨±å¯ãæã¤ã

       S_IWUSR (S_IWRITE)
              00200 ã¦ã¼ã¶ã¼ã¯æ¸ãè¾¼ã¿ã®è¨±å¯ãæã¤ã

       S_IXUSR (S_IEXEC)
              00100 ã¦ã¼ã¶ã¼ã¯å®è¡ã®è¨±å¯ãæã¤ã


       S_IRWXG
              00070 ã°ã«ã¼ãã¯èªã¿è¾¼ã¿ãæ¸ãè¾¼ã¿ãå®è¡ã®è¨±å¯ãæã¤ã

       S_IRGRP
              00040 ã°ã«ã¼ãã¯èªã¿è¾¼ã¿ã®è¨±å¯ãæã¤ã

       S_IWGRP
              00020 ã°ã«ã¼ãã¯æ¸ãè¾¼ã¿ã®è¨±å¯ãæã¤ã

       S_IXGRP
              00010 ã°ã«ã¼ãã¯å®è¡ã®è¨±å¯ãæã¤ã

       S_IRWXO
              00007 ä»äºº(others)ã¯èªã¿è¾¼ã¿ãæ¸ãè¾¼ã¿ãå®è¡ã®è¨±å¯ãæã¤ã

       S_IROTH
              00004 ä»äººã¯èªã¿è¾¼ã¿ã®è¨±å¯ãæã¤ã

       S_IWOTH
              00002 ä»äººã¯æ¸ãè¾¼ã¿ã®è¨±å¯ãæã¤ã

       S_IXOTH
              00001 ä»äººã¯å®è¡ã®è¨±å¯ãæã¤ã

       mode 㯠flags ã« O_CREAT ãæå®ããã¦ããå ´åã¯å¸¸ã«
       æå®ããå¿è¦ãããããã以å¤ã®å ´åã¯ç¡è¦ãããã

       creat 㯠flags ã« O_CREAT|O_WRONLY|O_TRUNC ãæå®ã㦠open ãè¡ãªãã®ã¨ç‐
       価ã§ããã


è¿ãå¤
       open 㨠creat ã¯æ°ãããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¼ãè¿ãã ã¨ã©ã¼ãçºçããå ´å㯠-1
       ãè¿ãã(ãã®å ´å㯠errno ãé©åã«è¨å®ãã)ã

       open ã§ç¹æ®ãã¡ã¤ã«ããªã¼ãã³ãããã¨ã¯ã§ãããã creat
       ã§ç¹æ®ãã¡ã¤ã«ãä½æãããã¨ã¯ã§ããªããã¨ã«æ³¨æãããã¨ã- 代ãã« mknod(2)
       ã使ç¨ããã

       UIDãããã³ã°ã使ç¨ãã¦ãã NFS ãã¡ã¤ã«ã»ã·ã¹ãã ã§ã¯ã open
       ããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¼ãè¿ããå ´åã§ã read(2) ã EACCES ã§æå¦ãããå ´åãããã
       ãã㯠open ã®è¨±å¯ã®ãã§ãã¯ã¯ã¯ã©ã¤ã¢ã³ãã§è¡ãªããã¦ãããã èª‐
       ã¿è¾¼ã¿ãæ¸ãè¾¼ã¿ã®éã® UID ãããã³ã°ã¯ãµã¼ãã¼ã§è¡ãªããã¦ããããã§ããã

ã¨ã©ã¼
       EEXIST pathname ã¯æ¢ã«åå¨ãã O_CREAT 㨠O_EXCL ã使ç¨ãããã

       EISDIR pathname ã¯ãã£ã¬ã¯ããªãåç§ãã¦ãããæ¸ãè¾¼ã¿è¦æ±ãå«ã¾ãã¦ããã

       ETXTBSY
              pathname ãç¾å¨å®è¡ä¸ã®å®è¡ã¤ã¡ã¼ã¸ãåç§ãã¦ãããæ¸ãè¾¼ã¿ãè¦æ±ããã

       EFAULT pathname ãã¢ã¯ã»ã¹å¯è½ãªã¢ãã¬ã¹ç©ºéã®å¤é¨ãå·®ãã¦ãã.

       EACCES ãã¡ã¤ã«ã«å¯¾ããè¦æ±ãããã¢ã¯ã»ã¹ã許ããã¦ããªããã pathname
              ã®ãã£ã¬ã¯ããªé¨åã®ã©ããã«æ¤ç´¢(å®è¡)許å¯ããªãã£ãã

       ENAMETOOLONG
              pathname ãé·éããã

       ENOENT pathname ã®ãã£ã¬ã¯ããªé¨åãå‐
              å¨ããªããå£ãã(dangling)ã·ã³ããªãã¯ã»ãªã³ã¯ã§ããã

       ENOTDIR
              pathname ã«å«ã¾ãããã£ã¬ã¯ããªé¨åã®ã©ãããå®éã«ã¯ãã£ã¬ã¯ããªã§ãªãã

       EMFILE ããã»ã¹ã¯æ¢ã«å¯è½ãªæ大æ°ã®ãã¡ã¤ã«ããªã¼ãã³ãã¦ããã

       ENFILE ã·ã¹ãã å¨ä½ã§ãªã¼ãã³å¯è½ãªãã¡ã¤ã«ã®éçã«éã£ãã¦ããã

       ENOMEM ã«ã¼ãã«ã«ååãªã¡ã¢ãªãåå¨ããªãã

       EROFS  pathname ãèªã¿è¾¼ã¿å°ç¨ã®ãã¡ã¤ã«ã·ã¹ãã ä¸ã®ãã¡ã¤ã«ãåç§ãã¦ããã
              æ¸ãè¾¼ã¿ã¢ã¯ã»ã¹ãè¦æ±ããã

       ELOOP  pathname ã解決ããéã«ééããã·ã³ããªãã¯ã»ãªã³ã¯ãå¤éããã

       ENOSPC pathname ãä½æããå¿è¦ããããã pathname
              ãå«ãã§ããããã¤ã¹ã«æ°ãããã¡ã¤ã«ã®ããã®ç©ºã容éããªãã

æºæ
       SVr4, SVID, POSIX, X/OPEN, BSD 4.3

å¶é
       NFS ãå®ç¾ãã¦ãããããã³ã«ã«ã¯å¤ãã®ä¸åããããç¹ã« O_SYNC 㨠O_NDELAY ã«å½±é¿ããã

é¢é£é ç®
       read(2), write(2), fcntl(2), close(2), unlink(2), mknod(2), stat(2),
       umask(2), mount(2), socket(2), socket(2), fopen(3), link(2).



                               December 20, 1996                       OPEN(2)