{"id":3852,"date":"2025-09-04T10:06:15","date_gmt":"2025-09-04T09:06:15","guid":{"rendered":"https:\/\/workboot.fr\/ciela\/?page_id=3852"},"modified":"2026-06-16T10:54:52","modified_gmt":"2026-06-16T09:54:52","slug":"open-close","status":"publish","type":"page","link":"https:\/\/workboot.fr\/ciela\/open-close\/","title":{"rendered":"open\/close"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Tout est fichiers <\/strong> : dans Unix et Linux, presque tout est consid\u00e9r\u00e9 comme un fichier.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ce n&rsquo;est pas une simple m\u00e9taphore, c&rsquo;est une conception profonde du syst\u00e8me qui le rend extr\u00eamement puissant et simple \u00e0 la fois. Voici ce que cela signifie concr\u00e8tement :<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ces 2 fonctions sont intrins\u00e8quement li\u00e9es<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Si on ouvre un fichier , il faudra forc\u00e9ment le fermer dans votre code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">un fichier est une s\u00e9quence de donn\u00e9es , ici plus particuli\u00e8rement des octets .<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On va utiliser aussi les fonctions write et read , qui veulent dire \u00e9crire et lire <\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary><strong>man open<\/strong><\/summary>\n<p class=\"wp-block-paragraph\">open(2) \u2014 Linux manual page<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#NAME\">NAME<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#LIBRARY\">LIBRARY<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#SYNOPSIS\">SYNOPSIS<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#DESCRIPTION\">DESCRIPTION<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#RETURN_VALUE\">RETURN&nbsp;VALUE<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#ERRORS\">ERRORS<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#VERSIONS\">VERSIONS<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#STANDARDS\">STANDARDS<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#HISTORY\">HISTORY<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#NOTES\">NOTES<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#BUGS\">BUGS<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#SEE_ALSO\">SEE&nbsp;ALSO<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#COLOPHON\">COLOPHON<\/a><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">&nbsp;<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>open<\/em>(2)                    System Calls Manual                    <em>open<\/em>(2)<\/strong><br><\/pre>\n\n\n\n<h2 id=\"name-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#NAME\"><\/a>NAME &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       open, openat, creat - open and possibly create a file<br><\/pre>\n\n\n\n<h2 id=\"library-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#LIBRARY\"><\/a>LIBRARY &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       Standard C library (<em>libc<\/em>, <em>-lc<\/em>)<br><\/pre>\n\n\n\n<h2 id=\"synopsis-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#SYNOPSIS\"><\/a>SYNOPSIS &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       <strong>#include &lt;fcntl.h&gt;<\/strong><br><br>       <strong>int open(const char *<\/strong><em>pathname<\/em><strong>, int <\/strong><em>flags<\/em><strong>, ...<\/strong><br>                  \/* <strong>mode_t <\/strong><em>mode<\/em> *\/ <strong>);<\/strong><br><br>       <strong>int creat(const char *<\/strong><em>pathname<\/em><strong>, mode_t <\/strong><em>mode<\/em><strong>);<\/strong><br><br>       <strong>int openat(int <\/strong><em>dirfd<\/em><strong>, const char *<\/strong><em>pathname<\/em><strong>, int <\/strong><em>flags<\/em><strong>, ...<\/strong><br>                  \/* <strong>mode_t <\/strong><em>mode<\/em> *\/ <strong>);<\/strong><br><br>       \/* Documented separately, in <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat2.2.html\">openat2(2)<\/a>: *\/<br>       <strong>int openat2(int <\/strong><em>dirfd<\/em><strong>, const char *<\/strong><em>pathname<\/em><strong>,<\/strong><br>                  <strong>const struct open_how *<\/strong><em>how<\/em><strong>, size_t <\/strong><em>size<\/em><strong>);<\/strong><br><br>   Feature Test Macro Requirements for glibc (see<br>   <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/feature_test_macros.7.html\">feature_test_macros(7)<\/a>):<br><br>       <strong>openat<\/strong>():<br>           Since glibc 2.10:<br>               _POSIX_C_SOURCE &gt;= 200809L<br>           Before glibc 2.10:<br>               _ATFILE_SOURCE<br><\/pre>\n\n\n\n<h2 id=\"description-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#DESCRIPTION\"><\/a>DESCRIPTION &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       The <strong>open<\/strong>() system call opens the file specified by <em>pathname<\/em>.  If<br>       the specified file does not exist, it may optionally (if <strong>O_CREAT<\/strong><br>       is specified in <em>flags<\/em>) be created by <strong>open<\/strong>().<br><br>       The return value of <strong>open<\/strong>() is a file descriptor, a small,<br>       nonnegative integer that is an index to an entry in the process's<br>       table of open file descriptors.  The file descriptor is used in<br>       subsequent system calls (<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/read.2.html\">read(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/write.2.html\">write(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/lseek.2.html\">lseek(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a>,<br>       etc.)  to refer to the open file.  The file descriptor returned by<br>       a successful call will be the lowest-numbered file descriptor not<br>       currently open for the process.<br><br>       By default, the new file descriptor is set to remain open across<br>       an <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/execve.2.html\">execve(2)<\/a> (i.e., the <strong>FD_CLOEXEC <\/strong>file descriptor flag described<br>       in <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a> is initially disabled); the <strong>O_CLOEXEC <\/strong>flag, described<br>       below, can be used to change this default.  The file offset is set<br>       to the beginning of the file (see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/lseek.2.html\">lseek(2)<\/a>).<br><br>       A call to <strong>open<\/strong>() creates a new <em>open file description<\/em>, an entry in<br>       the system-wide table of open files.  The open file description<br>       records the file offset and the file status flags (see below).  A<br>       file descriptor is a reference to an open file description; this<br>       reference is unaffected if <em>pathname<\/em> is subsequently removed or<br>       modified to refer to a different file.  For further details on<br>       open file descriptions, see NOTES.<br><br>       The argument <em>flags<\/em> must include one of the following <em>access modes<\/em>:<br>       <strong>O_RDONLY<\/strong>, <strong>O_WRONLY<\/strong>, or <strong>O_RDWR<\/strong>.  These request opening the file<br>       read-only, write-only, or read\/write, respectively.<br><br>       In addition, zero or more file creation flags and file status<br>       flags can be bitwise ORed in <em>flags<\/em>.  The <em>file creation flags<\/em> are<br>       <strong>O_CLOEXEC<\/strong>, <strong>O_CREAT<\/strong>, <strong>O_DIRECTORY<\/strong>, <strong>O_EXCL<\/strong>, <strong>O_NOCTTY<\/strong>, <strong>O_NOFOLLOW<\/strong>,<br>       <strong>O_TMPFILE<\/strong>, and <strong>O_TRUNC<\/strong>.  The <em>file status flags<\/em> are all of the<br>       remaining flags listed below.  The distinction between these two<br>       groups of flags is that the file creation flags affect the<br>       semantics of the open operation itself, while the file status<br>       flags affect the semantics of subsequent I\/O operations.  The file<br>       status flags can be retrieved and (in some cases) modified; see<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a> for details.<br><br>       The full list of file creation flags and file status flags is as<br>       follows:<br><br>       <strong>O_APPEND<\/strong><br>              The file is opened in append mode.  Before each <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/write.2.html\">write(2)<\/a>,<br>              the file offset is positioned at the end of the file, as if<br>              with <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/lseek.2.html\">lseek(2)<\/a>.  The modification of the file offset and the<br>              write operation are performed as a single atomic step.<br><br>              <strong>O_APPEND <\/strong>may lead to corrupted files on NFS filesystems if<br>              more than one process appends data to a file at once.  This<br>              is because NFS does not support appending to a file, so the<br>              client kernel has to simulate it, which can't be done<br>              without a race condition.<br><br>       <strong>O_ASYNC<\/strong><br>              Enable signal-driven I\/O: generate a signal (<strong>SIGIO <\/strong>by<br>              default, but this can be changed via <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a>) when input<br>              or output becomes possible on this file descriptor.  This<br>              feature is available only for terminals, pseudoterminals,<br>              sockets, and (since Linux 2.6) pipes and FIFOs.  See<br>              <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a> for further details.  See also BUGS, below.<br><br>       <strong>O_CLOEXEC <\/strong>(since Linux 2.6.23)<br>              Enable the close-on-exec flag for the new file descriptor.<br>              Specifying this flag permits a program to avoid additional<br>              <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a> <strong>F_SETFD <\/strong>operations to set the <strong>FD_CLOEXEC <\/strong>flag.<br><br>              Note that the use of this flag is essential in some<br>              multithreaded programs, because using a separate <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a><br>              <strong>F_SETFD <\/strong>operation to set the <strong>FD_CLOEXEC <\/strong>flag does not<br>              suffice to avoid race conditions where one thread opens a<br>              file descriptor and attempts to set its close-on-exec flag<br>              using <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a> at the same time as another thread does a<br>              <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fork.2.html\">fork(2)<\/a> plus <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/execve.2.html\">execve(2)<\/a>.  Depending on the order of<br>              execution, the race may lead to the file descriptor<br>              returned by <strong>open<\/strong>() being unintentionally leaked to the<br>              program executed by the child process created by <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fork.2.html\">fork(2)<\/a>.<br>              (This kind of race is in principle possible for any system<br>              call that creates a file descriptor whose close-on-exec<br>              flag should be set, and various other Linux system calls<br>              provide an equivalent of the <strong>O_CLOEXEC <\/strong>flag to deal with<br>              this problem.)<br><br>       <strong>O_CREAT<\/strong><br>              If <em>pathname<\/em> does not exist, create it as a regular file.<br><br>              The owner (user ID) of the new file is set to the effective<br>              user ID of the process.<br><br>              The group ownership (group ID) of the new file is set<br>              either to the effective group ID of the process (System V<br>              semantics) or to the group ID of the parent directory (BSD<br>              semantics).  On Linux, the behavior depends on whether the<br>              set-group-ID mode bit is set on the parent directory: if<br>              that bit is set, then BSD semantics apply; otherwise,<br>              System V semantics apply.  For some filesystems, the<br>              behavior also depends on the <em>bsdgroups<\/em> and <em>sysvgroups<\/em> mount<br>              options described in <a href=\"https:\/\/man7.org\/linux\/man-pages\/man8\/mount.8.html\">mount(8)<\/a>.<br><br>              The <em>mode<\/em> argument specifies the file mode bits to be<br>              applied when a new file is created.  If neither <strong>O_CREAT <\/strong>nor<br>              <strong>O_TMPFILE <\/strong>is specified in <em>flags<\/em>, then <em>mode<\/em> is ignored (and<br>              can thus be specified as 0, or simply omitted).  The <em>mode<\/em><br>              argument <strong>must <\/strong>be supplied if <strong>O_CREAT <\/strong>or <strong>O_TMPFILE <\/strong>is<br>              specified in <em>flags<\/em>; if it is not supplied, some arbitrary<br>              bytes from the stack will be applied as the file mode.<br><br>              The effective mode is modified by the process's <em>umask<\/em> in<br>              the usual way: in the absence of a default ACL, the mode of<br>              the created file is <em>(mode &amp; ~umask)<\/em>.<br><br>              Note that <em>mode<\/em> applies only to future accesses of the newly<br>              created file; the <strong>open<\/strong>() call that creates a read-only file<br>              may well return a read\/write file descriptor.<br><br>              The following symbolic constants are provided for <em>mode<\/em>:<br><br>              <strong>S_IRWXU  <\/strong>00700 user (file owner) has read, write, and<br>                       execute permission<br><br>              <strong>S_IRUSR  <\/strong>00400 user has read permission<br><br>              <strong>S_IWUSR  <\/strong>00200 user has write permission<br><br>              <strong>S_IXUSR  <\/strong>00100 user has execute permission<br><br>              <strong>S_IRWXG  <\/strong>00070 group has read, write, and execute<br>                       permission<br><br>              <strong>S_IRGRP  <\/strong>00040 group has read permission<br><br>              <strong>S_IWGRP  <\/strong>00020 group has write permission<br><br>              <strong>S_IXGRP  <\/strong>00010 group has execute permission<br><br>              <strong>S_IRWXO  <\/strong>00007 others have read, write, and execute<br>                       permission<br><br>              <strong>S_IROTH  <\/strong>00004 others have read permission<br><br>              <strong>S_IWOTH  <\/strong>00002 others have write permission<br><br>              <strong>S_IXOTH  <\/strong>00001 others have execute permission<br><br>              According to POSIX, the effect when other bits are set in<br>              <em>mode<\/em> is unspecified.  On Linux, the following bits are also<br>              honored in <em>mode<\/em>:<br><br>              <strong>S_ISUID  <\/strong>0004000 set-user-ID bit<br><br>              <strong>S_ISGID  <\/strong>0002000 set-group-ID bit (see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/inode.7.html\">inode(7)<\/a>).<br><br>              <strong>S_ISVTX  <\/strong>0001000 sticky bit (see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/inode.7.html\">inode(7)<\/a>).<br><br>       <strong>O_DIRECT <\/strong>(since Linux 2.4.10)<br>              Try to minimize cache effects of the I\/O to and from this<br>              file.  In general this will degrade performance, but it is<br>              useful in special situations, such as when applications do<br>              their own caching.  File I\/O is done directly to\/from user-<br>              space buffers.  The <strong>O_DIRECT <\/strong>flag on its own makes an<br>              effort to transfer data synchronously, but does not give<br>              the guarantees of the <strong>O_SYNC <\/strong>flag that data and necessary<br>              metadata are transferred.  To guarantee synchronous I\/O,<br>              <strong>O_SYNC <\/strong>must be used in addition to <strong>O_DIRECT<\/strong>.  See NOTES<br>              below for further discussion.<br><br>              A semantically similar (but deprecated) interface for block<br>              devices is described in <strong>raw<\/strong>(8).<br><br>       <strong>O_DIRECTORY<\/strong><br>              If <em>pathname<\/em> is not a directory, cause the open to fail.<br>              This flag was added in Linux 2.1.126, to avoid denial-of-<br>              service problems if <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/opendir.3.html\">opendir(3)<\/a> is called on a FIFO or tape<br>              device.<br><br>       <strong>O_DSYNC<\/strong><br>              Write operations on the file will complete according to the<br>              requirements of synchronized I\/O <em>data<\/em> integrity completion.<br><br>              By the time <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/write.2.html\">write(2)<\/a> (and similar) return, the output data<br>              has been transferred to the underlying hardware, along with<br>              any file metadata that would be required to retrieve that<br>              data (i.e., as though each <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/write.2.html\">write(2)<\/a> was followed by a call<br>              to <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fdatasync.2.html\">fdatasync(2)<\/a>).  See VERSIONS.<br><br>       <strong>O_EXCL <\/strong>Ensure that this call creates the file: if this flag is<br>              specified in conjunction with <strong>O_CREAT<\/strong>, and <em>pathname<\/em> already<br>              exists, then <strong>open<\/strong>() fails with the error <strong>EEXIST<\/strong>.<br><br>              When these two flags are specified, symbolic links are not<br>              followed: if <em>pathname<\/em> is a symbolic link, then <strong>open<\/strong>() fails<br>              regardless of where the symbolic link points.<br><br>              In general, the behavior of <strong>O_EXCL <\/strong>is undefined if it is<br>              used without <strong>O_CREAT<\/strong>.  There is one exception: on Linux 2.6<br>              and later, <strong>O_EXCL <\/strong>can be used without <strong>O_CREAT <\/strong>if <em>pathname<\/em><br>              refers to a block device.  If the block device is in use by<br>              the system (e.g., mounted), <strong>open<\/strong>() fails with the error<br>              <strong>EBUSY<\/strong>.<br><br>              On NFS, <strong>O_EXCL <\/strong>is supported only when using NFSv3 or later<br>              on kernel 2.6 or later.  In NFS environments where <strong>O_EXCL<\/strong><br>              support is not provided, programs that rely on it for<br>              performing locking tasks will contain a race condition.<br>              Portable programs that want to perform atomic file locking<br>              using a lockfile, and need to avoid reliance on NFS support<br>              for <strong>O_EXCL<\/strong>, can create a unique file on the same filesystem<br>              (e.g., incorporating hostname and PID), and use <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/link.2.html\">link(2)<\/a> to<br>              make a link to the lockfile.  If <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/link.2.html\">link(2)<\/a> returns 0, the<br>              lock is successful.  Otherwise, use <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/stat.2.html\">stat(2)<\/a> on the unique<br>              file to check if its link count has increased to 2, in<br>              which case the lock is also successful.<br><br>       <strong>O_LARGEFILE<\/strong><br>              (LFS) Allow files whose sizes cannot be represented in an<br>              <em>off_t<\/em> (but can be represented in an <em>off64_t<\/em>) to be opened.<br>              The <strong>_LARGEFILE64_SOURCE <\/strong>macro must be defined (before<br>              including <em>any<\/em> header files) in order to obtain this<br>              definition.  Setting the <strong>_FILE_OFFSET_BITS <\/strong>feature test<br>              macro to 64 (rather than using <strong>O_LARGEFILE<\/strong>) is the<br>              preferred method of accessing large files on 32-bit systems<br>              (see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/feature_test_macros.7.html\">feature_test_macros(7)<\/a>).<br><br>       <strong>O_NOATIME <\/strong>(since Linux 2.6.8)<br>              Do not update the file last access time (<em>st_atime<\/em> in the<br>              inode) when the file is <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/read.2.html\">read(2)<\/a>.<br><br>              This flag can be employed only if one of the following<br>              conditions is true:<br><br>              \u2022  The effective UID of the process matches the owner UID<br>                 of the file.<br><br>              \u2022  The calling process has the <strong>CAP_FOWNER <\/strong>capability in its<br>                 user namespace and the owner UID of the file has a<br>                 mapping in the namespace.<br><br>              This flag is intended for use by indexing or backup<br>              programs, where its use can significantly reduce the amount<br>              of disk activity.  This flag may not be effective on all<br>              filesystems.  One example is NFS, where the server<br>              maintains the access time.<br><br>       <strong>O_NOCTTY<\/strong><br>              If <em>pathname<\/em> refers to a terminal device\u2014see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man4\/tty.4.html\">tty(4)<\/a>\u2014it will<br>              not become the process's controlling terminal even if the<br>              process does not have one.<br><br>       <strong>O_NOFOLLOW<\/strong><br>              If the trailing component (i.e., basename) of <em>pathname<\/em> is a<br>              symbolic link, then the open fails, with the error <strong>ELOOP<\/strong>.<br>              Symbolic links in earlier components of the pathname will<br>              still be followed.  (Note that the <strong>ELOOP <\/strong>error that can<br>              occur in this case is indistinguishable from the case where<br>              an open fails because there are too many symbolic links<br>              found while resolving components in the prefix part of the<br>              pathname.)<br><br>              This flag is a FreeBSD extension, which was added in Linux<br>              2.1.126, and has subsequently been standardized in<br>              POSIX.1-2008.<br><br>              See also <strong>O_PATH <\/strong>below.<br><br>       <strong>O_NONBLOCK <\/strong>or <strong>O_NDELAY<\/strong><br>              When possible, the file is opened in nonblocking mode.<br>              Neither the <strong>open<\/strong>() nor any subsequent I\/O operations on the<br>              file descriptor which is returned will cause the calling<br>              process to wait.<br><br>              Note that the setting of this flag has no effect on the<br>              operation of <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/poll.2.html\">poll(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/select.2.html\">select(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/epoll.7.html\">epoll(7)<\/a>, and similar,<br>              since those interfaces merely inform the caller about<br>              whether a file descriptor is \"ready\", meaning that an I\/O<br>              operation performed on the file descriptor with the<br>              <strong>O_NONBLOCK <\/strong>flag <em>clear<\/em> would not block.<br><br>              Note that this flag has no effect for regular files and<br>              block devices; that is, I\/O operations will (briefly) block<br>              when device activity is required, regardless of whether<br>              <strong>O_NONBLOCK <\/strong>is set.  Since <strong>O_NONBLOCK <\/strong>semantics might<br>              eventually be implemented, applications should not depend<br>              upon blocking behavior when specifying this flag for<br>              regular files and block devices.<br><br>              For the handling of FIFOs (named pipes), see also <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/fifo.7.html\">fifo(7)<\/a>.<br>              For a discussion of the effect of <strong>O_NONBLOCK <\/strong>in conjunction<br>              with mandatory file locks and with file leases, see<br>              <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a>.<br><br>       <strong>O_PATH <\/strong>(since Linux 2.6.39)<br>              Obtain a file descriptor that can be used for two purposes:<br>              to indicate a location in the filesystem tree and to<br>              perform operations that act purely at the file descriptor<br>              level.  The file itself is not opened, and other file<br>              operations (e.g., <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/read.2.html\">read(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/write.2.html\">write(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchmod.2.html\">fchmod(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchown.2.html\">fchown(2)<\/a>,<br>              <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fgetxattr.2.html\">fgetxattr(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/ioctl.2.html\">ioctl(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mmap.2.html\">mmap(2)<\/a>) fail with the error <strong>EBADF<\/strong>.<br><br>              The following operations <em>can<\/em> be performed on the resulting<br>              file descriptor:<br><br>              \u2022  <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/close.2.html\">close(2)<\/a>.<br><br>              \u2022  <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchdir.2.html\">fchdir(2)<\/a>, if the file descriptor refers to a directory<br>                 (since Linux 3.5).<br><br>              \u2022  <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fstat.2.html\">fstat(2)<\/a> (since Linux 3.6).<br><br>              \u2022  <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fstatfs.2.html\">fstatfs(2)<\/a> (since Linux 3.12).<br><br>              \u2022  Duplicating the file descriptor (<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/dup.2.html\">dup(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a><br>                 <strong>F_DUPFD<\/strong>, etc.).<br><br>              \u2022  Getting and setting file descriptor flags (<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a><br>                 <strong>F_GETFD <\/strong>and <strong>F_SETFD<\/strong>).<br><br>              \u2022  Retrieving open file status flags using the <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a><br>                 <strong>F_GETFL <\/strong>operation: the returned flags will include the<br>                 bit <strong>O_PATH<\/strong>.<br><br>              \u2022  Passing the file descriptor as the <em>dirfd<\/em> argument of<br>                 <strong>openat<\/strong>() and the other \"*at()\" system calls.  This<br>                 includes <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/linkat.2.html\">linkat(2)<\/a> with <strong>AT_EMPTY_PATH <\/strong>(or via procfs<br>                 using <strong>AT_SYMLINK_FOLLOW<\/strong>) even if the file is not a<br>                 directory.<br><br>              \u2022  Passing the file descriptor to another process via a<br>                 UNIX domain socket (see <strong>SCM_RIGHTS <\/strong>in <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/unix.7.html\">unix(7)<\/a>).<br><br>              When <strong>O_PATH <\/strong>is specified in <em>flags<\/em>, flag bits other than<br>              <strong>O_CLOEXEC<\/strong>, <strong>O_DIRECTORY<\/strong>, and <strong>O_NOFOLLOW <\/strong>are ignored.<br><br>              Opening a file or directory with the <strong>O_PATH <\/strong>flag requires<br>              no permissions on the object itself (but does require<br>              execute permission on the directories in the path prefix).<br>              Depending on the subsequent operation, a check for suitable<br>              file permissions may be performed (e.g., <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchdir.2.html\">fchdir(2)<\/a> requires<br>              execute permission on the directory referred to by its file<br>              descriptor argument).  By contrast, obtaining a reference<br>              to a filesystem object by opening it with the <strong>O_RDONLY <\/strong>flag<br>              requires that the caller have read permission on the<br>              object, even when the subsequent operation (e.g.,<br>              <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchdir.2.html\">fchdir(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fstat.2.html\">fstat(2)<\/a>) does not require read permission on<br>              the object.<br><br>              If <em>pathname<\/em> is a symbolic link and the <strong>O_NOFOLLOW <\/strong>flag is<br>              also specified, then the call returns a file descriptor<br>              referring to the symbolic link.  This file descriptor can<br>              be used as the <em>dirfd<\/em> argument in calls to <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchownat.2.html\">fchownat(2)<\/a>,<br>              <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fstatat.2.html\">fstatat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/linkat.2.html\">linkat(2)<\/a>, and <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/readlinkat.2.html\">readlinkat(2)<\/a> with an empty<br>              pathname to have the calls operate on the symbolic link.<br><br>              If <em>pathname<\/em> refers to an automount point that has not yet<br>              been triggered, so no other filesystem is mounted on it,<br>              then the call returns a file descriptor referring to the<br>              automount directory without triggering a mount.  <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fstatfs.2.html\">fstatfs(2)<\/a><br>              can then be used to determine if it is, in fact, an<br>              untriggered automount point (<strong>.f_type ==<\/strong><br>              <strong>AUTOFS_SUPER_MAGIC<\/strong>).<br><br>              One use of <strong>O_PATH <\/strong>for regular files is to provide the<br>              equivalent of POSIX.1's <strong>O_EXEC <\/strong>functionality.  This permits<br>              us to open a file for which we have execute permission but<br>              not read permission, and then execute that file, with steps<br>              something like the following:<br><br>                  char buf[PATH_MAX];<br>                  fd = open(\"some_prog\", O_PATH);<br>                  snprintf(buf, PATH_MAX, \"\/proc\/self\/fd\/%d\", fd);<br>                  execl(buf, \"some_prog\", (char *) NULL);<br><br>              An <strong>O_PATH <\/strong>file descriptor can also be passed as the<br>              argument of <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/fexecve.3.html\">fexecve(3)<\/a>.<br><br>       <strong>O_SYNC <\/strong>Write operations on the file will complete according to the<br>              requirements of synchronized I\/O <em>file<\/em> integrity completion<br>              (by contrast with the synchronized I\/O <em>data<\/em> integrity<br>              completion provided by <strong>O_DSYNC<\/strong>.)<br><br>              By the time <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/write.2.html\">write(2)<\/a> (or similar) returns, the output data<br>              and associated file metadata have been transferred to the<br>              underlying hardware (i.e., as though each <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/write.2.html\">write(2)<\/a> was<br>              followed by a call to <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fsync.2.html\">fsync(2)<\/a>).  See VERSIONS.<br><br>       <strong>O_TMPFILE <\/strong>(since Linux 3.11)<br>              Create an unnamed temporary regular file.  The <em>pathname<\/em><br>              argument specifies a directory; an unnamed inode will be<br>              created in that directory's filesystem.  Anything written<br>              to the resulting file will be lost when the last file<br>              descriptor is closed, unless the file is given a name.<br><br>              <strong>O_TMPFILE <\/strong>must be specified with one of <strong>O_RDWR <\/strong>or <strong>O_WRONLY<\/strong><br>              and, optionally, <strong>O_EXCL<\/strong>.  If <strong>O_EXCL <\/strong>is not specified, then<br>              <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/linkat.2.html\">linkat(2)<\/a> can be used to link the temporary file into the<br>              filesystem, making it permanent, using code like the<br>              following:<br><br>                  char path[PATH_MAX];<br>                  fd = open(\"\/path\/to\/dir\", O_TMPFILE | O_RDWR,<br>                                          S_IRUSR | S_IWUSR);<br><br>                  \/* File I\/O on 'fd'... *\/<br><br>                  linkat(fd, \"\", AT_FDCWD, \"\/path\/for\/file\", AT_EMPTY_PATH);<br><br>                  \/* If the caller doesn't have the CAP_DAC_READ_SEARCH<br>                     capability (needed to use AT_EMPTY_PATH with linkat(2)),<br>                     and there is a proc(5) filesystem mounted, then the<br>                     linkat(2) call above can be replaced with:<br><br>                  snprintf(path, PATH_MAX,  \"\/proc\/self\/fd\/%d\", fd);<br>                  linkat(AT_FDCWD, path, AT_FDCWD, \"\/path\/for\/file\",<br>                                          AT_SYMLINK_FOLLOW);<br>                  *\/<br><br>              In this case, the <strong>open<\/strong>() <em>mode<\/em> argument determines the file<br>              permission mode, as with <strong>O_CREAT<\/strong>.<br><br>              Specifying <strong>O_EXCL <\/strong>in conjunction with <strong>O_TMPFILE <\/strong>prevents a<br>              temporary file from being linked into the filesystem in the<br>              above manner.  (Note that the meaning of <strong>O_EXCL <\/strong>in this<br>              case is different from the meaning of <strong>O_EXCL <\/strong>otherwise.)<br><br>              There are two main use cases for <strong>O_TMPFILE<\/strong>:<br><br>              \u2022  Improved <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/tmpfile.3.html\">tmpfile(3)<\/a> functionality: race-free creation of<br>                 temporary files that (1) are automatically deleted when<br>                 closed; (2) can never be reached via any pathname; (3)<br>                 are not subject to symlink attacks; and (4) do not<br>                 require the caller to devise unique names.<br><br>              \u2022  Creating a file that is initially invisible, which is<br>                 then populated with data and adjusted to have<br>                 appropriate filesystem attributes (<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchown.2.html\">fchown(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchmod.2.html\">fchmod(2)<\/a>,<br>                 <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fsetxattr.2.html\">fsetxattr(2)<\/a>, etc.)  before being atomically linked into<br>                 the filesystem in a fully formed state (using <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/linkat.2.html\">linkat(2)<\/a><br>                 as described above).<br><br>              <strong>O_TMPFILE <\/strong>requires support by the underlying filesystem;<br>              only a subset of Linux filesystems provide that support.<br>              In the initial implementation, support was provided in the<br>              ext2, ext3, ext4, UDF, Minix, and tmpfs filesystems.<br>              Support for other filesystems has subsequently been added<br>              as follows: XFS (Linux 3.15); Btrfs (Linux 3.16); F2FS<br>              (Linux 3.16); and ubifs (Linux 4.9)<br><br>       <strong>O_TRUNC<\/strong><br>              If the file already exists and is a regular file and the<br>              access mode allows writing (i.e., is <strong>O_RDWR <\/strong>or <strong>O_WRONLY<\/strong>) it<br>              will be truncated to length 0.  If the file is a FIFO or<br>              terminal device file, the <strong>O_TRUNC <\/strong>flag is ignored.<br>              Otherwise, the effect of <strong>O_TRUNC <\/strong>is unspecified.<br><br>   <strong>creat()<\/strong><br>       A call to <strong>creat<\/strong>() is equivalent to calling <strong>open<\/strong>() with <em>flags<\/em> equal<br>       to <strong>O_CREAT|O_WRONLY|O_TRUNC<\/strong>.<br><br>   <strong>openat()<\/strong><br>       The <strong>openat<\/strong>() system call operates in exactly the same way as<br>       <strong>open<\/strong>(), except for the differences described here.<br><br>       The <em>dirfd<\/em> argument is used in conjunction with the <em>pathname<\/em><br>       argument as follows:<br><br>       \u2022  If the pathname given in <em>pathname<\/em> is absolute, then <em>dirfd<\/em> is<br>          ignored.<br><br>       \u2022  If the pathname given in <em>pathname<\/em> is relative and <em>dirfd<\/em> is the<br>          special value <strong>AT_FDCWD<\/strong>, then <em>pathname<\/em> is interpreted relative<br>          to the current working directory of the calling process (like<br>          <strong>open<\/strong>()).<br><br>       \u2022  If the pathname given in <em>pathname<\/em> is relative, then it is<br>          interpreted relative to the directory referred to by the file<br>          descriptor <em>dirfd<\/em> (rather than relative to the current working<br>          directory of the calling process, as is done by <strong>open<\/strong>() for a<br>          relative pathname).  In this case, <em>dirfd<\/em> must be a directory<br>          that was opened for reading (<strong>O_RDONLY<\/strong>) or using the <strong>O_PATH<\/strong><br>          flag.<br><br>       If the pathname given in <em>pathname<\/em> is relative, and <em>dirfd<\/em> is not a<br>       valid file descriptor, an error (<strong>EBADF<\/strong>) results.  (Specifying an<br>       invalid file descriptor number in <em>dirfd<\/em> can be used as a means to<br>       ensure that <em>pathname<\/em> is absolute.)<br><br>   <strong>openat2(2)<\/strong><br>       The <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat2.2.html\">openat2(2)<\/a> system call is an extension of <strong>openat<\/strong>(), and<br>       provides a superset of the features of <strong>openat<\/strong>().  It is documented<br>       separately, in <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat2.2.html\">openat2(2)<\/a>.<br><\/pre>\n\n\n\n<h2 id=\"return-value-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#RETURN_VALUE\"><\/a>RETURN VALUE &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       On success, <strong>open<\/strong>(), <strong>openat<\/strong>(), and <strong>creat<\/strong>() return the new file<br>       descriptor (a nonnegative integer).  On error, -1 is returned and<br>       <em><a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/errno.3.html\">errno<\/a><\/em> is set to indicate the error.<br><\/pre>\n\n\n\n<h2 id=\"errors-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#ERRORS\"><\/a>ERRORS &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       <strong>open<\/strong>(), <strong>openat<\/strong>(), and <strong>creat<\/strong>() can fail with the following errors:<br><br>       <strong>EACCES <\/strong>The requested access to the file is not allowed, or search<br>              permission is denied for one of the directories in the path<br>              prefix of <em>pathname<\/em>, or the file did not exist yet and write<br>              access to the parent directory is not allowed.  (See also<br>              <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/path_resolution.7.html\">path_resolution(7)<\/a>.)<br><br>       <strong>EACCES <\/strong>Where <strong>O_CREAT <\/strong>is specified, the <em>protected_fifos<\/em> or<br>              <em>protected_regular<\/em> sysctl is enabled, the file already<br>              exists and is a FIFO or regular file, the owner of the file<br>              is neither the current user nor the owner of the containing<br>              directory, and the containing directory is both world- or<br>              group-writable and sticky.  For details, see the<br>              descriptions of <em>\/proc\/sys\/fs\/protected_fifos<\/em> and<br>              <em>\/proc\/sys\/fs\/protected_regular<\/em> in <a href=\"https:\/\/man7.org\/linux\/man-pages\/man5\/proc_sys_fs.5.html\">proc_sys_fs(5)<\/a>.<br><br>       <strong>EBADF  <\/strong>(<strong>openat<\/strong>()) <em>pathname<\/em> is relative but <em>dirfd<\/em> is neither<br>              <strong>AT_FDCWD <\/strong>nor a valid file descriptor.<br><br>       <strong>EBUSY  O_EXCL <\/strong>was specified in <em>flags<\/em> and <em>pathname<\/em> refers to a<br>              block device that is in use by the system (e.g., it is<br>              mounted).<br><br>       <strong>EDQUOT <\/strong>Where <strong>O_CREAT <\/strong>is specified, the file does not exist, and<br>              the user's quota of disk blocks or inodes on the filesystem<br>              has been exhausted.<br><br>       <strong>EEXIST <\/strong><em>pathname<\/em> already exists and <strong>O_CREAT <\/strong>and <strong>O_EXCL <\/strong>were used.<br><br>       <strong>EFAULT <\/strong><em>pathname<\/em> points outside your accessible address space.<br><br>       <strong>EFBIG  <\/strong>See <strong>EOVERFLOW<\/strong>.<br><br>       <strong>EINTR  <\/strong>While blocked waiting to complete an open of a slow device<br>              (e.g., a FIFO; see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/fifo.7.html\">fifo(7)<\/a>), the call was interrupted by a<br>              signal handler; see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/signal.7.html\">signal(7)<\/a>.<br><br>       <strong>EINVAL <\/strong>The filesystem does not support the <strong>O_DIRECT <\/strong>flag.  See<br>              <strong>NOTES <\/strong>for more information.<br><br>       <strong>EINVAL <\/strong>Invalid value in <em>flags<\/em>.<br><br>       <strong>EINVAL O_TMPFILE <\/strong>was specified in <em>flags<\/em>, but neither <strong>O_WRONLY <\/strong>nor<br>              <strong>O_RDWR <\/strong>was specified.<br><br>       <strong>EINVAL O_CREAT <\/strong>was specified in <em>flags<\/em> and the final component<br>              (\"basename\") of the new file's <em>pathname<\/em> is invalid (e.g.,<br>              it contains characters not permitted by the underlying<br>              filesystem).<br><br>       <strong>EINVAL <\/strong>The final component (\"basename\") of <em>pathname<\/em> is invalid<br>              (e.g., it contains characters not permitted by the<br>              underlying filesystem).<br><br>       <strong>EISDIR <\/strong><em>pathname<\/em> refers to a directory and the access requested<br>              involved writing (that is, <strong>O_WRONLY <\/strong>or <strong>O_RDWR <\/strong>is set).<br><br>       <strong>EISDIR <\/strong><em>pathname<\/em> refers to an existing directory, <strong>O_TMPFILE <\/strong>and one<br>              of <strong>O_WRONLY <\/strong>or <strong>O_RDWR <\/strong>were specified in <em>flags<\/em>, but this<br>              kernel version does not provide the <strong>O_TMPFILE<\/strong><br>              functionality.<br><br>       <strong>ELOOP  <\/strong>Too many symbolic links were encountered in resolving<br>              <em>pathname<\/em>.<br><br>       <strong>ELOOP  <\/strong><em>pathname<\/em> was a symbolic link, and <em>flags<\/em> specified<br>              <strong>O_NOFOLLOW <\/strong>but not <strong>O_PATH<\/strong>.<br><br>       <strong>EMFILE <\/strong>The per-process limit on the number of open file<br>              descriptors has been reached (see the description of<br>              <strong>RLIMIT_NOFILE <\/strong>in <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/getrlimit.2.html\">getrlimit(2)<\/a>).<br><br>       <strong>ENAMETOOLONG<\/strong><br>              <em>pathname<\/em> was too long.<br><br>       <strong>ENFILE <\/strong>The system-wide limit on the total number of open files has<br>              been reached.<br><br>       <strong>ENODEV <\/strong><em>pathname<\/em> refers to a device special file and no<br>              corresponding device exists.  (This is a Linux kernel bug;<br>              in this situation <strong>ENXIO <\/strong>must be returned.)<br><br>       <strong>ENOENT O_CREAT <\/strong>is not set and the named file does not exist.<br><br>       <strong>ENOENT <\/strong>A directory component in <em>pathname<\/em> does not exist or is a<br>              dangling symbolic link.<br><br>       <strong>ENOENT <\/strong><em>pathname<\/em> refers to a nonexistent directory, <strong>O_TMPFILE <\/strong>and<br>              one of <strong>O_WRONLY <\/strong>or <strong>O_RDWR <\/strong>were specified in <em>flags<\/em>, but this<br>              kernel version does not provide the <strong>O_TMPFILE<\/strong><br>              functionality.<br><br>       <strong>ENOMEM <\/strong>The named file is a FIFO, but memory for the FIFO buffer<br>              can't be allocated because the per-user hard limit on<br>              memory allocation for pipes has been reached and the caller<br>              is not privileged; see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/pipe.7.html\">pipe(7)<\/a>.<br><br>       <strong>ENOMEM <\/strong>Insufficient kernel memory was available.<br><br>       <strong>ENOSPC <\/strong><em>pathname<\/em> was to be created but the device containing<br>              <em>pathname<\/em> has no room for the new file.<br><br>       <strong>ENOTDIR<\/strong><br>              A component used as a directory in <em>pathname<\/em> is not, in<br>              fact, a directory, or <strong>O_DIRECTORY <\/strong>was specified and<br>              <em>pathname<\/em> was not a directory.<br><br>       <strong>ENOTDIR<\/strong><br>              (<strong>openat<\/strong>()) <em>pathname<\/em> is a relative pathname and <em>dirfd<\/em> is a<br>              file descriptor referring to a file other than a directory.<br><br>       <strong>ENXIO  O_NONBLOCK <\/strong>| <strong>O_WRONLY <\/strong>is set, the named file is a FIFO, and<br>              no process has the FIFO open for reading.<br><br>       <strong>ENXIO  <\/strong>The file is a device special file and no corresponding<br>              device exists.<br><br>       <strong>ENXIO  <\/strong>The file is a UNIX domain socket.<br><br>       <strong>EOPNOTSUPP<\/strong><br>              The filesystem containing <em>pathname<\/em> does not support<br>              <strong>O_TMPFILE<\/strong>.<br><br>       <strong>EOVERFLOW<\/strong><br>              <em>pathname<\/em> refers to a regular file that is too large to be<br>              opened.  The usual scenario here is that an application<br>              compiled on a 32-bit platform without<br>              <em>-D_FILE_OFFSET_BITS=64<\/em> tried to open a file whose size<br>              exceeds <em>(1&lt;&lt;31)-1<\/em> bytes; see also <strong>O_LARGEFILE <\/strong>above.  This<br>              is the error specified by POSIX.1; before Linux 2.6.24,<br>              Linux gave the error <strong>EFBIG <\/strong>for this case.<br><br>       <strong>EPERM  <\/strong>The <strong>O_NOATIME <\/strong>flag was specified, but the effective user ID<br>              of the caller did not match the owner of the file and the<br>              caller was not privileged.<br><br>       <strong>EPERM  <\/strong>The operation was prevented by a file seal; see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a>.<br><br>       <strong>EROFS  <\/strong><em>pathname<\/em> refers to a file on a read-only filesystem and<br>              write access was requested.<br><br>       <strong>ETXTBSY<\/strong><br>              <em>pathname<\/em> refers to an executable image which is currently<br>              being executed and write access was requested.<br><br>       <strong>ETXTBSY<\/strong><br>              <em>pathname<\/em> refers to a file that is currently in use as a<br>              swap file, and the <strong>O_TRUNC <\/strong>flag was specified.<br><br>       <strong>ETXTBSY<\/strong><br>              <em>pathname<\/em> refers to a file that is currently being read by<br>              the kernel (e.g., for module\/firmware loading), and write<br>              access was requested.<br><br>       <strong>EWOULDBLOCK<\/strong><br>              The <strong>O_NONBLOCK <\/strong>flag was specified, and an incompatible<br>              lease was held on the file (see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a>).<br><\/pre>\n\n\n\n<h2 id=\"versions-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#VERSIONS\"><\/a>VERSIONS &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       The (undefined) effect of <strong>O_RDONLY | O_TRUNC <\/strong>varies among<br>       implementations.  On many systems the file is actually truncated.<br><br>   <strong>Synchronized I\/O<\/strong><br>       The POSIX.1-2008 \"synchronized I\/O\" option specifies different<br>       variants of synchronized I\/O, and specifies the <strong>open<\/strong>() flags<br>       <strong>O_SYNC<\/strong>, <strong>O_DSYNC<\/strong>, and <strong>O_RSYNC <\/strong>for controlling the behavior.<br>       Regardless of whether an implementation supports this option, it<br>       must at least support the use of <strong>O_SYNC <\/strong>for regular files.<br><br>       Linux implements <strong>O_SYNC <\/strong>and <strong>O_DSYNC<\/strong>, but not <strong>O_RSYNC<\/strong>.  Somewhat<br>       incorrectly, glibc defines <strong>O_RSYNC <\/strong>to have the same value as<br>       <strong>O_SYNC<\/strong>.  (<strong>O_RSYNC <\/strong>is defined in the Linux header file<br>       <em>&lt;asm\/fcntl.h&gt;<\/em> on HP PA-RISC, but it is not used.)<br><br>       <strong>O_SYNC <\/strong>provides synchronized I\/O <em>file<\/em> integrity completion,<br>       meaning write operations will flush data and all associated<br>       metadata to the underlying hardware.  <strong>O_DSYNC <\/strong>provides<br>       synchronized I\/O <em>data<\/em> integrity completion, meaning write<br>       operations will flush data to the underlying hardware, but will<br>       only flush metadata updates that are required to allow a<br>       subsequent read operation to complete successfully.  Data<br>       integrity completion can reduce the number of disk operations that<br>       are required for applications that don't need the guarantees of<br>       file integrity completion.<br><br>       To understand the difference between the two types of completion,<br>       consider two pieces of file metadata: the file last modification<br>       timestamp (<em>st_mtime<\/em>) and the file length.  All write operations<br>       will update the last file modification timestamp, but only writes<br>       that add data to the end of the file will change the file length.<br>       The last modification timestamp is not needed to ensure that a<br>       read completes successfully, but the file length is.  Thus,<br>       <strong>O_DSYNC <\/strong>would only guarantee to flush updates to the file length<br>       metadata (whereas <strong>O_SYNC <\/strong>would also always flush the last<br>       modification timestamp metadata).<br><br>       Before Linux 2.6.33, Linux implemented only the <strong>O_SYNC <\/strong>flag for<br>       <strong>open<\/strong>().  However, when that flag was specified, most filesystems<br>       actually provided the equivalent of synchronized I\/O <em>data<\/em><br>       integrity completion (i.e., <strong>O_SYNC <\/strong>was actually implemented as the<br>       equivalent of <strong>O_DSYNC<\/strong>).<br><br>       Since Linux 2.6.33, proper <strong>O_SYNC <\/strong>support is provided.  However,<br>       to ensure backward binary compatibility, <strong>O_DSYNC <\/strong>was defined with<br>       the same value as the historical <strong>O_SYNC<\/strong>, and <strong>O_SYNC <\/strong>was defined as<br>       a new (two-bit) flag value that includes the <strong>O_DSYNC <\/strong>flag value.<br>       This ensures that applications compiled against new headers get at<br>       least <strong>O_DSYNC <\/strong>semantics before Linux 2.6.33.<br><br>   <strong>C library\/kernel differences<\/strong><br>       Since glibc 2.26, the glibc wrapper function for <strong>open<\/strong>() employs<br>       the <strong>openat<\/strong>() system call, rather than the kernel's <strong>open<\/strong>() system<br>       call.  For certain architectures, this is also true before glibc<br>       2.26.<br><\/pre>\n\n\n\n<h2 id=\"standards-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#STANDARDS\"><\/a>STANDARDS &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       <strong>open<\/strong>()<br>       <strong>creat<\/strong>()<br>       <strong>openat<\/strong>()<br>              POSIX.1-2008.<br><br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat2.2.html\">openat2(2)<\/a> Linux.<br><br>       The <strong>O_DIRECT<\/strong>, <strong>O_NOATIME<\/strong>, <strong>O_PATH<\/strong>, and <strong>O_TMPFILE <\/strong>flags are Linux-<br>       specific.  One must define <strong>_GNU_SOURCE <\/strong>to obtain their<br>       definitions.<br><br>       The <strong>O_CLOEXEC<\/strong>, <strong>O_DIRECTORY<\/strong>, and <strong>O_NOFOLLOW <\/strong>flags are not specified<br>       in POSIX.1-2001, but are specified in POSIX.1-2008.  Since glibc<br>       2.12, one can obtain their definitions by defining either<br>       <strong>_POSIX_C_SOURCE <\/strong>with a value greater than or equal to 200809L or<br>       <strong>_XOPEN_SOURCE <\/strong>with a value greater than or equal to 700.  In glibc<br>       2.11 and earlier, one obtains the definitions by defining<br>       <strong>_GNU_SOURCE<\/strong>.<br><\/pre>\n\n\n\n<h2 id=\"history-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#HISTORY\"><\/a>HISTORY &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       <strong>open<\/strong>()<br>       <strong>creat<\/strong>()<br>              SVr4, 4.3BSD, POSIX.1-2001.<br><br>       <strong>openat<\/strong>()<br>              POSIX.1-2008.  Linux 2.6.16, glibc 2.4.<br><\/pre>\n\n\n\n<h2 id=\"notes-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#NOTES\"><\/a>NOTES &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       Under Linux, the <strong>O_NONBLOCK <\/strong>flag is sometimes used in cases where<br>       one wants to open but does not necessarily have the intention to<br>       read or write.  For example, this may be used to open a device in<br>       order to get a file descriptor for use with <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/ioctl.2.html\">ioctl(2)<\/a>.<br><br>       Note that <strong>open<\/strong>() can open device special files, but <strong>creat<\/strong>() cannot<br>       create them; use <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mknod.2.html\">mknod(2)<\/a> instead.<br><br>       If the file is newly created, its <em>st_atime<\/em>, <em>st_ctime<\/em>, <em>st_mtime<\/em><br>       fields (respectively, time of last access, time of last status<br>       change, and time of last modification; see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/stat.2.html\">stat(2)<\/a>) are set to the<br>       current time, and so are the <em>st_ctime<\/em> and <em>st_mtime<\/em> fields of the<br>       parent directory.  Otherwise, if the file is modified because of<br>       the <strong>O_TRUNC <\/strong>flag, its <em>st_ctime<\/em> and <em>st_mtime<\/em> fields are set to the<br>       current time.<br><br>       The files in the <em>\/proc\/<\/em>pid<em>\/fd<\/em> directory show the open file<br>       descriptors of the process with the PID <em>pid<\/em>.  The files in the<br>       <em>\/proc\/<\/em>pid<em>\/fdinfo<\/em> directory show even more information about these<br>       file descriptors.  See <a href=\"https:\/\/man7.org\/linux\/man-pages\/man5\/proc.5.html\">proc(5)<\/a> for further details of both of<br>       these directories.<br><br>       The Linux header file <strong>&lt;asm\/fcntl.h&gt; <\/strong>doesn't define <strong>O_ASYNC<\/strong>; the<br>       (BSD-derived) <strong>FASYNC <\/strong>synonym is defined instead.<br><br>   <strong>Open file descriptions<\/strong><br>       The term open file description is the one used by POSIX to refer<br>       to the entries in the system-wide table of open files.  In other<br>       contexts, this object is variously also called an \"open file<br>       object\", a \"file handle\", an \"open file table entry\", or\u2014in<br>       kernel-developer parlance\u2014a <em>struct file<\/em>.<br><br>       When a file descriptor is duplicated (using <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/dup.2.html\">dup(2)<\/a> or similar),<br>       the duplicate refers to the same open file description as the<br>       original file descriptor, and the two file descriptors<br>       consequently share the file offset and file status flags.  Such<br>       sharing can also occur between processes: a child process created<br>       via <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fork.2.html\">fork(2)<\/a> inherits duplicates of its parent's file descriptors,<br>       and those duplicates refer to the same open file descriptions.<br><br>       Each <strong>open<\/strong>() of a file creates a new open file description; thus,<br>       there may be multiple open file descriptions corresponding to a<br>       file inode.<br><br>       On Linux, one can use the <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/kcmp.2.html\">kcmp(2)<\/a> <strong>KCMP_FILE <\/strong>operation to test<br>       whether two file descriptors (in the same process or in two<br>       different processes) refer to the same open file description.<br><br>   <strong>NFS<\/strong><br>       There are many infelicities in the protocol underlying NFS,<br>       affecting amongst others <strong>O_SYNC <\/strong>and <strong>O_NDELAY<\/strong>.<br><br>       On NFS filesystems with UID mapping enabled, <strong>open<\/strong>() may return a<br>       file descriptor but, for example, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/read.2.html\">read(2)<\/a> requests are denied with<br>       <strong>EACCES<\/strong>.  This is because the client performs <strong>open<\/strong>() by checking<br>       the permissions, but UID mapping is performed by the server upon<br>       read and write requests.<br><br>   <strong>FIFOs<\/strong><br>       Opening the read or write end of a FIFO blocks until the other end<br>       is also opened (by another process or thread).  See <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/fifo.7.html\">fifo(7)<\/a> for<br>       further details.<br><br>   <strong>File access mode<\/strong><br>       Unlike the other values that can be specified in <em>flags<\/em>, the <em>access<\/em><br>       <em>mode<\/em> values <strong>O_RDONLY<\/strong>, <strong>O_WRONLY<\/strong>, and <strong>O_RDWR <\/strong>do not specify<br>       individual bits.  Rather, they define the low order two bits of<br>       <em>flags<\/em>, and are defined respectively as 0, 1, and 2.  In other<br>       words, the combination <strong>O_RDONLY | O_WRONLY <\/strong>is a logical error, and<br>       certainly does not have the same meaning as <strong>O_RDWR<\/strong>.<br><br>       Linux reserves the special, nonstandard access mode 3 (binary 11)<br>       in <em>flags<\/em> to mean: check for read and write permission on the file<br>       and return a file descriptor that can't be used for reading or<br>       writing.  This nonstandard access mode is used by some Linux<br>       drivers to return a file descriptor that is to be used only for<br>       device-specific <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/ioctl.2.html\">ioctl(2)<\/a> operations.<br><br>   <strong>Rationale for openat() and other directory file descriptor APIs<\/strong><br>       <strong>openat<\/strong>() and the other system calls and library functions that<br>       take a directory file descriptor argument (i.e., <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/execveat.2.html\">execveat(2)<\/a>,<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/faccessat.2.html\">faccessat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fanotify_mark.2.html\">fanotify_mark(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchmodat.2.html\">fchmodat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fchownat.2.html\">fchownat(2)<\/a>,<br>       <strong>fspick<\/strong>(2), <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fstatat.2.html\">fstatat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/futimesat.2.html\">futimesat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/linkat.2.html\">linkat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mkdirat.2.html\">mkdirat(2)<\/a>,<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mknodat.2.html\">mknodat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mount_setattr.2.html\">mount_setattr(2)<\/a>, <strong>move_mount<\/strong>(2), <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/name_to_handle_at.2.html\">name_to_handle_at(2)<\/a>,<br>       <strong>open_tree<\/strong>(2), <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat2.2.html\">openat2(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/readlinkat.2.html\">readlinkat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/renameat.2.html\">renameat(2)<\/a>,<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/renameat2.2.html\">renameat2(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/statx.2.html\">statx(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/symlinkat.2.html\">symlinkat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/unlinkat.2.html\">unlinkat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/utimensat.2.html\">utimensat(2)<\/a>,<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/mkfifoat.3.html\">mkfifoat(3)<\/a>, and <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/scandirat.3.html\">scandirat(3)<\/a>) address two problems with the older<br>       interfaces that preceded them.  Here, the explanation is in terms<br>       of the <strong>openat<\/strong>() call, but the rationale is analogous for the other<br>       interfaces.<br><br>       First, <strong>openat<\/strong>() allows an application to avoid race conditions<br>       that could occur when using <strong>open<\/strong>() to open files in directories<br>       other than the current working directory.  These race conditions<br>       result from the fact that some component of the directory prefix<br>       given to <strong>open<\/strong>() could be changed in parallel with the call to<br>       <strong>open<\/strong>().  Suppose, for example, that we wish to create the file<br>       <em>dir1\/dir2\/xxx.dep<\/em> if the file <em>dir1\/dir2\/xxx<\/em> exists.  The problem<br>       is that between the existence check and the file-creation step,<br>       <em>dir1<\/em> or <em>dir2<\/em> (which might be symbolic links) could be modified to<br>       point to a different location.  Such races can be avoided by<br>       opening a file descriptor for the target directory, and then<br>       specifying that file descriptor as the <em>dirfd<\/em> argument of (say)<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fstatat.2.html\">fstatat(2)<\/a> and <strong>openat<\/strong>().  The use of the <em>dirfd<\/em> file descriptor<br>       also has other benefits:<br><br>       \u2022  the file descriptor is a stable reference to the directory,<br>          even if the directory is renamed; and<br><br>       \u2022  the open file descriptor prevents the underlying filesystem<br>          from being dismounted, just as when a process has a current<br>          working directory on a filesystem.<br><br>       Second, <strong>openat<\/strong>() allows the implementation of a per-thread<br>       \"current working directory\", via file descriptor(s) maintained by<br>       the application.  (This functionality can also be obtained by<br>       tricks based on the use of <em>\/proc\/self\/fd\/<\/em>dirfd, but less<br>       efficiently.)<br><br>       The <em>dirfd<\/em> argument for these APIs can be obtained by using <strong>open<\/strong>()<br>       or <strong>openat<\/strong>() to open a directory (with either the <strong>O_RDONLY <\/strong>or the<br>       <strong>O_PATH <\/strong>flag).  Alternatively, such a file descriptor can be<br>       obtained by applying <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/dirfd.3.html\">dirfd(3)<\/a> to a directory stream created using<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/opendir.3.html\">opendir(3)<\/a>.<br><br>       When these APIs are given a <em>dirfd<\/em> argument of <strong>AT_FDCWD <\/strong>or the<br>       specified pathname is absolute, then they handle their pathname<br>       argument in the same way as the corresponding conventional APIs.<br>       However, in this case, several of the APIs have a <em>flags<\/em> argument<br>       that provides access to functionality that is not available with<br>       the corresponding conventional APIs.<br><br>   <strong>O_DIRECT<\/strong><br>       The <strong>O_DIRECT <\/strong>flag may impose alignment restrictions on the length<br>       and address of user-space buffers and the file offset of I\/Os.  In<br>       Linux alignment restrictions vary by filesystem and kernel version<br>       and might be absent entirely.  The handling of misaligned <strong>O_DIRECT<\/strong><br>       I\/Os also varies; they can either fail with <strong>EINVAL <\/strong>or fall back to<br>       buffered I\/O.<br><br>       Since Linux 6.1, <strong>O_DIRECT <\/strong>support and alignment restrictions for a<br>       file can be queried using <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/statx.2.html\">statx(2)<\/a>, using the <strong>STATX_DIOALIGN <\/strong>flag.<br>       Support for <strong>STATX_DIOALIGN <\/strong>varies by filesystem; see <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/statx.2.html\">statx(2)<\/a>.<br><br>       Some filesystems provide their own interfaces for querying<br>       <strong>O_DIRECT <\/strong>alignment restrictions, for example the <strong>XFS_IOC_DIOINFO<\/strong><br>       operation in <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/xfsctl.3.html\">xfsctl(3)<\/a>.  <strong>STATX_DIOALIGN <\/strong>should be used instead<br>       when it is available.<br><br>       If none of the above is available, then direct I\/O support and<br>       alignment restrictions can only be assumed from known<br>       characteristics of the filesystem, the individual file, the<br>       underlying storage device(s), and the kernel version.  In Linux<br>       2.4, most filesystems based on block devices require that the file<br>       offset and the length and memory address of all I\/O segments be<br>       multiples of the filesystem block size (typically 4096 bytes).  In<br>       Linux 2.6.0, this was relaxed to the logical block size of the<br>       block device (typically 512 bytes).  A block device's logical<br>       block size can be determined using the <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/ioctl.2.html\">ioctl(2)<\/a> <strong>BLKSSZGET<\/strong><br>       operation or from the shell using the command:<br><br>           blockdev --getss<br><br>       <strong>O_DIRECT <\/strong>I\/Os should never be run concurrently with the <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fork.2.html\">fork(2)<\/a><br>       system call, if the memory buffer is a private mapping (i.e., any<br>       mapping created with the <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mmap.2.html\">mmap(2)<\/a> <strong>MAP_PRIVATE <\/strong>flag; this includes<br>       memory allocated on the heap and statically allocated buffers).<br>       Any such I\/Os, whether submitted via an asynchronous I\/O interface<br>       or from another thread in the process, should be completed before<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fork.2.html\">fork(2)<\/a> is called.  Failure to do so can result in data corruption<br>       and undefined behavior in parent and child processes.  This<br>       restriction does not apply when the memory buffer for the <strong>O_DIRECT<\/strong><br>       I\/Os was created using <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/shmat.2.html\">shmat(2)<\/a> or <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mmap.2.html\">mmap(2)<\/a> with the <strong>MAP_SHARED<\/strong><br>       flag.  Nor does this restriction apply when the memory buffer has<br>       been advised as <strong>MADV_DONTFORK <\/strong>with <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/madvise.2.html\">madvise(2)<\/a>, ensuring that it<br>       will not be available to the child after <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fork.2.html\">fork(2)<\/a>.<br><br>       The <strong>O_DIRECT <\/strong>flag was introduced in SGI IRIX, where it has<br>       alignment restrictions similar to those of Linux 2.4.  IRIX has<br>       also a <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a> call to query appropriate alignments, and sizes.<br>       FreeBSD 4.x introduced a flag of the same name, but without<br>       alignment restrictions.<br><br>       <strong>O_DIRECT <\/strong>support was added in Linux 2.4.10.  Older Linux kernels<br>       simply ignore this flag.  Some filesystems may not implement the<br>       flag, in which case <strong>open<\/strong>() fails with the error <strong>EINVAL <\/strong>if it is<br>       used.<br><br>       Applications should avoid mixing <strong>O_DIRECT <\/strong>and normal I\/O to the<br>       same file, and especially to overlapping byte regions in the same<br>       file.  Even when the filesystem correctly handles the coherency<br>       issues in this situation, overall I\/O throughput is likely to be<br>       slower than using either mode alone.  Likewise, applications<br>       should avoid mixing <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mmap.2.html\">mmap(2)<\/a> of files with direct I\/O to the same<br>       files.<br><br>       The behavior of <strong>O_DIRECT <\/strong>with NFS will differ from local<br>       filesystems.  Older kernels, or kernels configured in certain<br>       ways, may not support this combination.  The NFS protocol does not<br>       support passing the flag to the server, so <strong>O_DIRECT <\/strong>I\/O will<br>       bypass the page cache only on the client; the server may still<br>       cache the I\/O.  The client asks the server to make the I\/O<br>       synchronous to preserve the synchronous semantics of <strong>O_DIRECT<\/strong>.<br>       Some servers will perform poorly under these circumstances,<br>       especially if the I\/O size is small.  Some servers may also be<br>       configured to lie to clients about the I\/O having reached stable<br>       storage; this will avoid the performance penalty at some risk to<br>       data integrity in the event of server power failure.  The Linux<br>       NFS client places no alignment restrictions on <strong>O_DIRECT <\/strong>I\/O.<br><br>       In summary, <strong>O_DIRECT <\/strong>is a potentially powerful tool that should be<br>       used with caution.  It is recommended that applications treat use<br>       of <strong>O_DIRECT <\/strong>as a performance option which is disabled by default.<br><\/pre>\n\n\n\n<h2 id=\"bugs-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#BUGS\"><\/a>BUGS &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">       Currently, it is not possible to enable signal-driven I\/O by<br>       specifying <strong>O_ASYNC <\/strong>when calling <strong>open<\/strong>(); use <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a> to enable<br>       this flag.<br><br>       One must check for two different error codes, <strong>EISDIR <\/strong>and <strong>ENOENT<\/strong>,<br>       when trying to determine whether the kernel supports <strong>O_TMPFILE<\/strong><br>       functionality.<br><br>       When both <strong>O_CREAT <\/strong>and <strong>O_DIRECTORY <\/strong>are specified in <em>flags<\/em> and the<br>       file specified by <em>pathname<\/em> does not exist, <strong>open<\/strong>() will create a<br>       regular file (i.e., <strong>O_DIRECTORY <\/strong>is ignored).<br><\/pre>\n\n\n\n<h2 id=\"see-also-top\" class=\"wp-block-heading\"><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#SEE_ALSO\"><\/a>SEE ALSO &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat.2.html#top_of_page\">top<\/a><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/chmod.2.html\">chmod(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/chown.2.html\">chown(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/close.2.html\">close(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/dup.2.html\">dup(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/fcntl.2.html\">fcntl(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/link.2.html\">link(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/lseek.2.html\">lseek(2)<\/a>,<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mknod.2.html\">mknod(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mmap.2.html\">mmap(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/mount.2.html\">mount(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/open_by_handle_at.2.html\">open_by_handle_at(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/openat2.2.html\">openat2(2)<\/a>,<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/read.2.html\">read(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/socket.2.html\">socket(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/stat.2.html\">stat(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/umask.2.html\">umask(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/unlink.2.html\">unlink(2)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/write.2.html\">write(2)<\/a>,<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/fopen.3.html\">fopen(3)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man5\/acl.5.html\">acl(5)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/fifo.7.html\">fifo(7)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/inode.7.html\">inode(7)<\/a>, <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/path_resolution.7.html\">path_resolution(7)<\/a>,<br>       <a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/symlink.7.html\">symlink(7)<\/a><\/p>\n<\/details>\n\n\n\n<h1 id=\"h.uvylikrht2p7_l\" class=\"wp-block-heading\">Ecriture :&nbsp;<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Exemple simple de cr\u00e9ation d&rsquo;un fichier texte en lecture \u00e9criture uniquement par le propri\u00e9taire<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* exemple simple avec write *\/\n\n#include &lt;stdlib.h>\n#include &lt;stdio.h>\n#include &lt;sys\/stat.h>\n#include &lt;fcntl.h>\n#include &lt;unistd.h>\n\nint main (int argc, char ** argv)\n{\nint fd; \/* file desciptor *\/\nfd = open ( \"fichier.txt\",O_CREAT | O_RDWR \n                         ,S_IRUSR | S_IWUSR );  \/* droits Read USER Write USER...*\/\n\/* aller voir man 3 open *\/\nif (fd==-1)     \n              {\n              printf(\"Erreur d'ouverture du fichier \\n\");\n              return EXIT_FAILURE;\n              }\nwrite (fd, \"Bonjour le monde\\n\",17);  \/* 17 etant le nombre de caractere de la chaine *\/\nclose(fd);\nreturn EXIT_SUCCESS;\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ecrire un fichier en compliquant un peu&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">exemple 2 d\u2019\u00e9criture&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* exemple 2 avec une chaine *\/\n#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;termios.h>\n#include &lt;unistd.h>\n#include &lt;sys\/types.h>\n#include &lt;sys\/stat.h>\n#include &lt;fcntl.h>\n\nint main()\n{\n   int fd;\n   char chaine[100]=\"Bonjour le monda !\";\n   fd=open (\"\/tmp\/bonjour.txt\",O_CREAT | O_WRONLY,\n                        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );\n   if (fd==-1)         {\n                       perror(\"Pas possible d'ouvrir ce fichier \");\n                       return EXIT_FAILURE;\n                       }\n   write(fd,chaine,16);\n   printf(\"%s\",chaine);\n   close (fd);\n   return EXIT_SUCCESS;\n}<\/pre>\n\n\n\n<h2 id=\"lire\" class=\"wp-block-heading\">Lire :<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*  Lecture de fichier *\/\n#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;termios.h>\n#include &lt;unistd.h>\n#include &lt;sys\/types.h>\n#include &lt;sys\/stat.h>\n#include &lt;fcntl.h>\n\nint main()\n{\n   int fd;\n   char chaine[100];\n   fd=open (\"fichier.txt\",O_CREAT | O_RDONLY,\n                        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );\n   if (fd==-1)         {\n                       perror(\"Pas possible d'ouvrir ce fichier \");\n                       return EXIT_FAILURE;\n                       }\n   read(fd,chaine,16);\n   printf(\"%s\",chaine);\n   close (fd);\n   printf(\"Hello World!\\n\");\n   return EXIT_SUCCESS;\n}<\/pre>\n\n\n\n<h2 id=\"les-flags\" class=\"wp-block-heading\">Les flags!<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Voici le d\u00e9tail des constantes de droits (issues de&nbsp;<code>&lt;sys\/stat.h&gt;<\/code>) :<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Constante<\/th><th class=\"has-text-align-left\" data-align=\"left\">Valeur octale<\/th><th class=\"has-text-align-left\" data-align=\"left\">Signification<\/th><\/tr><\/thead><tbody><tr><td><strong><code>S_IRUSR<\/code><\/strong><\/td><td>0400<\/td><td>Lecture autoris\u00e9e pour le&nbsp;<strong>propri\u00e9taire<\/strong>&nbsp;(User)<\/td><\/tr><tr><td><strong><code>S_IWUSR<\/code><\/strong><\/td><td>0200<\/td><td>\u00c9criture autoris\u00e9e pour le&nbsp;<strong>propri\u00e9taire<\/strong>&nbsp;(User)<\/td><\/tr><tr><td><strong><code>S_IXUSR<\/code><\/strong><\/td><td>0100<\/td><td>Ex\u00e9cution autoris\u00e9e pour le propri\u00e9taire<\/td><\/tr><tr><td><code>S_IRGRP<\/code><\/td><td>0040<\/td><td>Lecture pour le&nbsp;<strong>groupe<\/strong><\/td><\/tr><tr><td><code>S_IWGRP<\/code><\/td><td>0020<\/td><td>\u00c9criture pour le groupe<\/td><\/tr><tr><td><code>S_IROTH<\/code><\/td><td>0004<\/td><td>Lecture pour les&nbsp;<strong>autres<\/strong><\/td><\/tr><tr><td><code>S_IWOTH<\/code><\/td><td>0002<\/td><td>\u00c9criture pour les autres<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Combinaison courante<\/th><th class=\"has-text-align-left\" data-align=\"left\">Signification<\/th><\/tr><\/thead><tbody><tr><td><code>O_RDONLY<\/code><\/td><td>Lecture seule (fichier doit exister)<\/td><\/tr><tr><td><code>O_WRONLY | O_CREAT | O_TRUNC<\/code><\/td><td>\u00c9criture seule, cr\u00e9e ou \u00e9crase le fichier.<\/td><\/tr><tr><td><code>O_RDWR | O_CREAT | O_APPEND<\/code><\/td><td>Lecture\/\u00c9criture, cr\u00e9e si absent, \u00e9crit \u00e0 la fin.<\/td><\/tr><tr><td><code>O_RDWR | O_CREAT | O_EXCL<\/code><\/td><td>Cr\u00e9ation atomique, \u00e9choue si le fichier existe (utile pour les verrous).<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Tout est fichiers : dans Unix et Linux, presque tout est consid\u00e9r\u00e9 comme un fichier. Ce n&rsquo;est pas une simple m\u00e9taphore, c&rsquo;est une conception profonde du syst\u00e8me qui le rend extr\u00eamement puissant et simple \u00e0 la fois. Voici ce que cela signifie concr\u00e8tement : Ces 2 fonctions sont intrins\u00e8quement li\u00e9es Si on ouvre un fichier [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","footnotes":""},"class_list":["post-3852","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"https:\/\/workboot.fr\/ciela\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Tout est fichiers : dans Unix et Linux, presque tout est consid\u00e9r\u00e9 comme un fichier. Ce n&rsquo;est pas une simple m\u00e9taphore, c&rsquo;est une conception profonde du syst\u00e8me qui le rend extr\u00eamement puissant et simple \u00e0 la fois. Voici ce que cela signifie concr\u00e8tement : Ces 2 fonctions sont intrins\u00e8quement li\u00e9es Si on ouvre un fichier\u2026","_links":{"self":[{"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/pages\/3852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/comments?post=3852"}],"version-history":[{"count":14,"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/pages\/3852\/revisions"}],"predecessor-version":[{"id":8311,"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/pages\/3852\/revisions\/8311"}],"wp:attachment":[{"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/media?parent=3852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}