1 /** @file
2     This file includes the definitions for open and fcntl described by POSIX
3     for <fcntl.h>; it also includes related kernel definitions.
4 
5     Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
6     This program and the accompanying materials are licensed and made
7     available under the terms and conditions of the BSD License which
8     accompanies this distribution.  The full text of the license may be found
9     at http://opensource.org/licenses/bsd-license.
10 
11     THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12     WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14     Copyright (c) 1983, 1990, 1993
15      The Regents of the University of California.  All rights reserved.
16     (c) UNIX System Laboratories, Inc.
17     All or some portions of this file are derived from material licensed
18     to the University of California by American Telephone and Telegraph
19     Co. or Unix System Laboratories, Inc. and are reproduced herein with
20     the permission of UNIX System Laboratories, Inc.
21 
22     Redistribution and use in source and binary forms, with or without
23     modification, are permitted provided that the following conditions
24     are met:
25     1. Redistributions of source code must retain the above copyright
26        notice, this list of conditions and the following disclaimer.
27     2. Redistributions in binary form must reproduce the above copyright
28        notice, this list of conditions and the following disclaimer in the
29        documentation and/or other materials provided with the distribution.
30     3. Neither the name of the University nor the names of its contributors
31        may be used to endorse or promote products derived from this software
32        without specific prior written permission.
33 
34     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37     ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44     SUCH DAMAGE.
45 
46     fcntl.h 8.3 (Berkeley) 1/21/94
47     NetBSD: fcntl.h,v 1.34 2006/10/05 14:48:33 chs Exp
48  */
49 #ifndef _SYS_FCNTL_H_
50 #define _SYS_FCNTL_H_
51 
52 #include <sys/featuretest.h>
53 #include <sys/types.h>
54 
55 #include <sys/stat.h>
56 
57 /** @{
58     File status flags used by open(2), fcntl(2).
59     They are also used (indirectly) in the kernel file structure f_flags,
60     which is a superset of the open/fcntl flags.
61     Open/fcntl flags begin with O_; kernel-internal flags begin with F.
62 **/
63 /* open-only flags */
64 #define O_RDONLY    0x00000000  ///< open for reading only
65 #define O_WRONLY    0x00000001  ///< open for writing only
66 #define O_RDWR      0x00000002  ///< open for reading and writing
67 #define O_ACCMODE   0x00000003  ///< mask for above modes
68 
69 #define O_NONBLOCK  0x00000004  ///< no delay
70 #define O_APPEND    0x00000008  ///< set append mode
71 #define O_CREAT     0x00000200  ///< create if nonexistent
72 #define O_TRUNC     0x00000400  ///< truncate to zero length
73 #define O_EXCL      0x00000800  ///< Grant EXCLusive access, or error if already exists and O_CREAT
74 
75 #define O_DIRECTORY 0x00001000  ///< error if path is not a directory
76 #define O_NOCTTY    0x00002000  ///< Don't make this the controlling TTY
77 #define O_TTY_INIT  0x00004000  ///< Initialize TTY to "sane" values on open
78 
79 /* UEFI-specific open-only flags. */
80 #define O_HIDDEN    0x00010000  ///< Hidden file attribute
81 #define O_SYSTEM    0x00020000  ///< System file attribute
82 #define O_ARCHIVE   0x00040000  ///< Archive file attribute
83 /// @}
84 
85 #define O_SETMASK     0x0000000F  ///< Flags modifiable by F_SETFD (fcntl)
86 
87 /*
88  * Constants used for fcntl(2)
89  */
90 
91 /** @{ command values used for fcntl(2). **/
92 #define F_DUPFD      0  ///< duplicate file descriptor
93 #define F_GETFD      1  ///< get file descriptor flags
94 #define F_SETFD      2  ///< set file descriptor flags
95 #define F_GETFL      3  ///< get file status flags
96 #define F_SETFL      4  ///< set file status flags
97 #define F_GETOWN     5  ///< get SIGIO/SIGURG proc/pgrp
98 #define F_SETOWN     6  ///< set SIGIO/SIGURG proc/pgrp
99 #define F_GETLK      7  ///< get record locking information
100 #define F_SETLK      8  ///< set record locking information
101 #define F_SETLKW     9  ///< F_SETLK; wait if blocked
102 #define F_CLOSEM    10  ///< close all fds >= to the one given
103 #define F_MAXFD     11  ///< return the max open fd
104 /// @}
105 
106 /** file descriptor flags (F_GETFD, F_SETFD). **/
107 #define FD_CLOEXEC  1   ///< close-on-exec flag
108 
109 /** @{ record locking flags (F_GETLK, F_SETLK, F_SETLKW). **/
110 #define F_RDLCK     1   ///< shared or read lock
111 #define F_UNLCK     2   ///< unlock
112 #define F_WRLCK     3   ///< exclusive or write lock
113 /// @}
114 
115 /** @{ Constants for fcntl's passed to the underlying fs - like ioctl's. **/
116 #define F_PARAM_MASK    0xfff
117 #define F_PARAM_LEN(x)  (((x) >> 16) & F_PARAM_MASK)
118 #define F_PARAM_MAX     4095
119 #define F_FSCTL         (int)0x80000000   ///< This fcntl goes to the fs
120 #define F_FSVOID        (int)0x40000000   ///< no parameters
121 #define F_FSOUT         (int)0x20000000   ///< copy out parameter
122 #define F_FSIN          (int)0x10000000   ///< copy in parameter
123 #define F_FSINOUT       (F_FSIN | F_FSOUT)
124 #define F_FSDIRMASK     (int)0x70000000   ///< mask for IN/OUT/VOID
125 #define F_FSPRIV        (int)0x00008000   ///< command is fs-specific
126 /// @}
127 
128 /* Always ensure that these are consistent with <stdio.h> and <unistd.h>! */
129 #ifndef SEEK_SET
130   #define SEEK_SET  0 /* set file offset to offset */
131 #endif
132 #ifndef SEEK_CUR
133   #define SEEK_CUR  1 /* set file offset to current plus offset */
134 #endif
135 #ifndef SEEK_END
136   #define SEEK_END  2 /* set file offset to EOF plus offset */
137 #endif
138 
139 #include  <sys/EfiCdefs.h>
140 
141 __BEGIN_DECLS
142 #ifndef __FCNTL_SYSCALLS_DECLARED
143   #define __FCNTL_SYSCALLS_DECLARED
144 
145   /** The open() function establishes the connection between a file and a file
146       descriptor.  It creates an open file description that refers to a file
147       and a file descriptor that refers to that open file description. The file
148       descriptor is used by other I/O functions to refer to that file.
149 
150       The open() function returns a file descriptor for the named file that is
151       the lowest file descriptor not currently open for that process. The open
152       file description is new, and therefore the file descriptor shall not
153       share it with any other process in the system.
154 
155       The file offset used to mark the current position within the file is set
156       to the beginning of the file.
157 
158       The file status flags and file access modes of the open file description
159       are set according to the value of oflags.
160 
161       Values for oflags are constructed by a bitwise-inclusive OR of flags from
162       the following list, defined in <fcntl.h>. Applications shall specify
163       exactly one of { O_RDONLY, O_RDWR, O_WRONLY } in the value of oflags.
164       Any combination of { O_NONBLOCK, O_APPEND, O_CREAT, O_TRUNC, O_EXCL } may
165       also be specified in oflags.
166 
167       Values for mode specify the access permissions for newly created files.
168 
169       @param[in]    Path      The path argument points to a pathname naming the
170                               object to be opened.
171       @param[in]    oflags    File status flags and file access modes of the
172                               open file description.
173       @param[in]    mode      File access permission bits as defined in
174                               <sys/stat.h>.
175 
176       @return     Upon successful completion, open() opens the file and returns
177                   a non-negative integer representing the lowest numbered
178                   unused file descriptor. Otherwise, open returns -1 and sets
179                   errno to indicate the error. If a negative value is
180                   returned, no files are created or modified.
181 
182       @retval   EMFILE      No file descriptors available -- Max number already open.
183       @retval   EINVAL      Bad value specified for oflags or mode.
184       @retval   ENOMEM      Failure allocating memory for internal buffers.
185       @retval   EEXIST      File exists and open attempted with (O_EXCL | O_CREAT) set.
186       @retval   EIO         UEFI failure.  Check value in EFIerrno.
187   **/
188   int open(const char *Path, int oflags, int mode);
189 
190   /**
191   **/
192   int creat(const char *, mode_t);
193 
194   /**
195   **/
196   int fcntl(int, int, ...);
197 #endif  // __FCNTL_SYSCALLS_DECLARED
198 __END_DECLS
199 
200 #endif /* !_SYS_FCNTL_H_ */
201