sl@0
|
1 |
/** @file ../include/sys/fcntl.h
|
sl@0
|
2 |
@internalComponent
|
sl@0
|
3 |
*/
|
sl@0
|
4 |
|
sl@0
|
5 |
/** @fn open(const char *file, int flags, ...)
|
sl@0
|
6 |
@param file
|
sl@0
|
7 |
@param flags
|
sl@0
|
8 |
@param ...
|
sl@0
|
9 |
@return If successful, open returns a non-negative integer, termed a file descriptor. It returns
|
sl@0
|
10 |
-1 on failure and sets errno to indicate the error.
|
sl@0
|
11 |
|
sl@0
|
12 |
The file name specified by file is opened for reading and/or writing, as specified by the argument flags , and the file descriptor returned to the calling process. The flags argument may indicate that the file is to be created if it does
|
sl@0
|
13 |
not exist (by specifying the O_CREAT flag). In this case open requires a third argument, mode_t mode , and the file is created with mode as described in chmod and modified by the process' umask
|
sl@0
|
14 |
value (see umask )
|
sl@0
|
15 |
|
sl@0
|
16 |
|
sl@0
|
17 |
|
sl@0
|
18 |
The flags specified are formed by OR'ing the following values
|
sl@0
|
19 |
|
sl@0
|
20 |
@code
|
sl@0
|
21 |
|
sl@0
|
22 |
O_RDONLYopen for reading only
|
sl@0
|
23 |
O_WRONLYopen for writing only
|
sl@0
|
24 |
O_RDWRopen for reading and writing
|
sl@0
|
25 |
O_NONBLOCKdo not block on open
|
sl@0
|
26 |
O_APPENDappend on each write
|
sl@0
|
27 |
O_CREATcreate file if it does not exist
|
sl@0
|
28 |
O_TRUNCtruncate size to 0
|
sl@0
|
29 |
O_EXCLerror if create and file exists
|
sl@0
|
30 |
O_SHLOCKatomically obtain a shared lock
|
sl@0
|
31 |
O_EXLOCKatomically obtain an exclusive lock
|
sl@0
|
32 |
O_DIRECTeliminate or reduce cache effects
|
sl@0
|
33 |
O_FSYNCsynchronous writes
|
sl@0
|
34 |
O_NOFOLLOWdo not follow symlinks
|
sl@0
|
35 |
Following options are currently not supported :
|
sl@0
|
36 |
O_NONBLOCK, O_SHLOCK, O_EXLOCK, O_DIRECT, O_FSYNC, O_NOFOLLOW.
|
sl@0
|
37 |
@endcode
|
sl@0
|
38 |
O_LARGEFILE
|
sl@0
|
39 |
This flag, if passed into the open() API, enables it to open a large file (files with 64 bit file sizes)
|
sl@0
|
40 |
|
sl@0
|
41 |
|
sl@0
|
42 |
Opening a file with O_APPEND set causes each write on the file
|
sl@0
|
43 |
to be appended to the end.
|
sl@0
|
44 |
|
sl@0
|
45 |
If O_TRUNC is specified, and the file exists, the file is truncated to
|
sl@0
|
46 |
zero length.
|
sl@0
|
47 |
|
sl@0
|
48 |
If O_EXCL is set with O_CREAT and the file already
|
sl@0
|
49 |
exists, open returns an error.
|
sl@0
|
50 |
This may be used to
|
sl@0
|
51 |
implement a simple exclusive access locking mechanism.
|
sl@0
|
52 |
|
sl@0
|
53 |
If O_EXCL is set and the last component of the pathname is
|
sl@0
|
54 |
a symbolic link, open will fail even if the symbolic
|
sl@0
|
55 |
link points to a non-existent name.
|
sl@0
|
56 |
|
sl@0
|
57 |
If the O_NONBLOCK flag is specified and the open system call would result in the process being blocked for some
|
sl@0
|
58 |
reason (e.g., waiting for carrier on a dialup line), open returns immediately. The descriptor remains in non-blocking mode
|
sl@0
|
59 |
for subsequent operations. This mode need not have any effect on files other
|
sl@0
|
60 |
than FIFOs.
|
sl@0
|
61 |
|
sl@0
|
62 |
If O_FSYNC is used in the mask all writes will immediately be written
|
sl@0
|
63 |
to disk, the kernel will not cache written data and all writes on the descriptor
|
sl@0
|
64 |
will not return until the data to be written completes.
|
sl@0
|
65 |
|
sl@0
|
66 |
If O_NOFOLLOW is used in the mask and the target file passed to open is a symbolic link the open will fail.
|
sl@0
|
67 |
|
sl@0
|
68 |
When opening a file, a lock with flock semantics can be obtained by setting O_SHLOCK for a shared lock or O_EXLOCK for an exclusive lock. If creating a file with O_CREAT, the request for the lock will never fail (provided that the
|
sl@0
|
69 |
underlying file system supports locking).
|
sl@0
|
70 |
|
sl@0
|
71 |
O_DIRECT may be used to minimize or eliminate the cache effects of
|
sl@0
|
72 |
reading and writing. The system will attempt to avoid caching the data being
|
sl@0
|
73 |
read or written. If it cannot avoid caching the data, it will minimize the impact
|
sl@0
|
74 |
the data has on the cache. Use of this flag can drastically reduce performance
|
sl@0
|
75 |
if not used with care.
|
sl@0
|
76 |
|
sl@0
|
77 |
If successful, open returns a non-negative integer termed a file descriptor. It returns
|
sl@0
|
78 |
-1 on failure. The file pointer used to mark the current position within the
|
sl@0
|
79 |
file is set to the beginning of the file.
|
sl@0
|
80 |
|
sl@0
|
81 |
When a new file is created it is given the group of the directory
|
sl@0
|
82 |
which contains it.
|
sl@0
|
83 |
|
sl@0
|
84 |
The new descriptor is set to remain open across execve system calls.; See close and fcntl .
|
sl@0
|
85 |
|
sl@0
|
86 |
The system imposes a limit on the number of file descriptors
|
sl@0
|
87 |
open simultaneously by one process.
|
sl@0
|
88 |
The getdtablesize system call returns the current system limit.
|
sl@0
|
89 |
|
sl@0
|
90 |
|
sl@0
|
91 |
|
sl@0
|
92 |
Notes:
|
sl@0
|
93 |
|
sl@0
|
94 |
1) Mode values for group and others are ignored
|
sl@0
|
95 |
|
sl@0
|
96 |
2) Execute bit and setuid on exec bit are ignored.
|
sl@0
|
97 |
|
sl@0
|
98 |
3) The default working directory of a process is initalized to C: \\private\\UID
|
sl@0
|
99 |
(UID of the calling application) and any data written into this directory persists
|
sl@0
|
100 |
between phone resets.
|
sl@0
|
101 |
|
sl@0
|
102 |
4) If the specified file is a symbolic link and the file it is pointing to
|
sl@0
|
103 |
is invalid, the symbolic link file will be automatically removed.
|
sl@0
|
104 |
|
sl@0
|
105 |
5) A file in cannot be created with write-only permissions. Attempting to create
|
sl@0
|
106 |
a file with write-only permissions will result in a file with read-write permission.
|
sl@0
|
107 |
|
sl@0
|
108 |
6) Creating a new file with the O_CREAT flag does not alter the time stamp
|
sl@0
|
109 |
of the parent directory.
|
sl@0
|
110 |
|
sl@0
|
111 |
7) A file has only two time stamps: access and modification. Creation time
|
sl@0
|
112 |
stamp of the file is not supported and access time stamp is initially set equal
|
sl@0
|
113 |
to modification time stamp.
|
sl@0
|
114 |
|
sl@0
|
115 |
8) Users should not use O_DIRECT flag as the underlying implementation makes explicit
|
sl@0
|
116 |
use of O_DIRECT by default. Instead if the users want to use read/write
|
sl@0
|
117 |
buffering they can use O_BUFFERED.
|
sl@0
|
118 |
|
sl@0
|
119 |
9) Users should not use O_BINARY flag as the underlying implementation opens a file
|
sl@0
|
120 |
in binary mode by default. Instead if the users want to open the file in text mode
|
sl@0
|
121 |
they can use O_TEXT.
|
sl@0
|
122 |
|
sl@0
|
123 |
Examples:
|
sl@0
|
124 |
@code
|
sl@0
|
125 |
/* This example creates a file in the current working directory and
|
sl@0
|
126 |
* opens it in read-write mode. */
|
sl@0
|
127 |
#include <sys/types.h>
|
sl@0
|
128 |
#include <sys/stat.h>
|
sl@0
|
129 |
#include <fcntl.h>
|
sl@0
|
130 |
int main()
|
sl@0
|
131 |
{
|
sl@0
|
132 |
int fd = 0;
|
sl@0
|
133 |
fd = open("Example.txt" , O_CREAT | O_EXCL , 0666);
|
sl@0
|
134 |
if(fd < 0 )
|
sl@0
|
135 |
{
|
sl@0
|
136 |
printf("Failed to create and open file in current working directory
|
sl@0
|
137 |
");
|
sl@0
|
138 |
return -1;
|
sl@0
|
139 |
}
|
sl@0
|
140 |
printf("File created and opened in current working directory
|
sl@0
|
141 |
" );
|
sl@0
|
142 |
return 0;
|
sl@0
|
143 |
}
|
sl@0
|
144 |
|
sl@0
|
145 |
@endcode
|
sl@0
|
146 |
Output
|
sl@0
|
147 |
@code
|
sl@0
|
148 |
File created and opened in current working directory
|
sl@0
|
149 |
|
sl@0
|
150 |
@endcode
|
sl@0
|
151 |
@see chmod()
|
sl@0
|
152 |
@see close()
|
sl@0
|
153 |
@see dup()
|
sl@0
|
154 |
@see getdtablesize()
|
sl@0
|
155 |
@see lseek()
|
sl@0
|
156 |
@see read()
|
sl@0
|
157 |
@see umask()
|
sl@0
|
158 |
@see write()
|
sl@0
|
159 |
@see fopen()
|
sl@0
|
160 |
|
sl@0
|
161 |
Limitations:
|
sl@0
|
162 |
|
sl@0
|
163 |
KErrNotReady of Symbian error code is mapped to ENOENT, which typically means drive
|
sl@0
|
164 |
not found or filesystem not mounted on the drive.
|
sl@0
|
165 |
|
sl@0
|
166 |
@capability Deferred @ref RFs::Entry(const TDesC16&, TEntry&) const
|
sl@0
|
167 |
|
sl@0
|
168 |
@publishedAll
|
sl@0
|
169 |
@externallyDefinedApi
|
sl@0
|
170 |
*/
|
sl@0
|
171 |
|
sl@0
|
172 |
|
sl@0
|
173 |
/** @fn open64(const char *file, int flags, ...)
|
sl@0
|
174 |
@param file
|
sl@0
|
175 |
@param flags
|
sl@0
|
176 |
@param ...
|
sl@0
|
177 |
@return If successful, open64() returns a non-negative integer, termed a file descriptor. It returns
|
sl@0
|
178 |
-1 on failure and sets errno to indicate the error.
|
sl@0
|
179 |
|
sl@0
|
180 |
|
sl@0
|
181 |
For full documentation see: http://www.unix.org/version2/whatsnew/lfs20mar.html#3.0
|
sl@0
|
182 |
|
sl@0
|
183 |
@see open()
|
sl@0
|
184 |
|
sl@0
|
185 |
@publishedAll
|
sl@0
|
186 |
@externallyDefinedApi
|
sl@0
|
187 |
*/
|
sl@0
|
188 |
|
sl@0
|
189 |
/**@typedef typedef __off_t off64_t;
|
sl@0
|
190 |
|
sl@0
|
191 |
Large file offsets.
|
sl@0
|
192 |
|
sl@0
|
193 |
@publishedAll
|
sl@0
|
194 |
@externallyDefinedApi
|
sl@0
|
195 |
*/
|
sl@0
|
196 |
|
sl@0
|
197 |
/** @fn creat(const char *file, mode_t mode)
|
sl@0
|
198 |
@param file
|
sl@0
|
199 |
@param mode
|
sl@0
|
200 |
@return open and creat return the new file descriptor, or -1 if an error occurred (in
|
sl@0
|
201 |
which case errno is set appropriately).
|
sl@0
|
202 |
|
sl@0
|
203 |
This interface is made obsolete by: open
|
sl@0
|
204 |
|
sl@0
|
205 |
The creat function
|
sl@0
|
206 |
is the same as: open(path, O_CREAT | O_TRUNC | O_WRONLY, mode); Limitation :Creating a new file doesn't alter the time stamp of parent directory, created entry has only two time stamps access and modification timestamps.
|
sl@0
|
207 |
Creation time stamp of the file is not supported, here accesstime stamp is equal to modification time stamp.
|
sl@0
|
208 |
|
sl@0
|
209 |
Examples:
|
sl@0
|
210 |
@code
|
sl@0
|
211 |
/* Detailed description : This test code demonstrates creat system call usage, it creates a
|
sl@0
|
212 |
* in current working directory(if file exists then it is truncated. Preconditions : None */
|
sl@0
|
213 |
#include <sys/types.h>
|
sl@0
|
214 |
#include <sys/stat.h>
|
sl@0
|
215 |
#include <fcntl.h>
|
sl@0
|
216 |
int main()
|
sl@0
|
217 |
{
|
sl@0
|
218 |
int fd = 0;
|
sl@0
|
219 |
fd = creat("Example.txt" , 0666);
|
sl@0
|
220 |
if(fd < 0 )
|
sl@0
|
221 |
{
|
sl@0
|
222 |
printf("File creation failed
|
sl@0
|
223 |
");
|
sl@0
|
224 |
return -1;
|
sl@0
|
225 |
}
|
sl@0
|
226 |
printf("Example.txt file created
|
sl@0
|
227 |
");
|
sl@0
|
228 |
return 0;
|
sl@0
|
229 |
}
|
sl@0
|
230 |
|
sl@0
|
231 |
@endcode
|
sl@0
|
232 |
Output
|
sl@0
|
233 |
@code
|
sl@0
|
234 |
Example.txt file created
|
sl@0
|
235 |
|
sl@0
|
236 |
@endcode
|
sl@0
|
237 |
@see open()
|
sl@0
|
238 |
|
sl@0
|
239 |
Limitations:
|
sl@0
|
240 |
|
sl@0
|
241 |
KErrNotReady of Symbian error code is mapped to ENOENT, which typically means drive
|
sl@0
|
242 |
not found or filesystem not mounted on the drive.
|
sl@0
|
243 |
|
sl@0
|
244 |
@capability Deferred @ref RFs::Entry(const TDesC16&, TEntry&) const
|
sl@0
|
245 |
|
sl@0
|
246 |
@publishedAll
|
sl@0
|
247 |
@externallyDefinedApi
|
sl@0
|
248 |
*/
|
sl@0
|
249 |
|
sl@0
|
250 |
/** @fn creat64(const char *file, mode_t mode)
|
sl@0
|
251 |
@param file
|
sl@0
|
252 |
@param mode
|
sl@0
|
253 |
@return creat64() returns the new file descriptor, or -1 if an error occurred (in
|
sl@0
|
254 |
which case errno is set appropriately).
|
sl@0
|
255 |
|
sl@0
|
256 |
For full documentation see: http://www.unix.org/version2/whatsnew/lfs20mar.html#3.0
|
sl@0
|
257 |
|
sl@0
|
258 |
@see creat()
|
sl@0
|
259 |
|
sl@0
|
260 |
@publishedAll
|
sl@0
|
261 |
@externallyDefinedApi
|
sl@0
|
262 |
*/
|
sl@0
|
263 |
|
sl@0
|
264 |
/** @fn fcntl(int aFid, int aCmd, ...)
|
sl@0
|
265 |
@param aFid
|
sl@0
|
266 |
@param aCmd
|
sl@0
|
267 |
@param ...
|
sl@0
|
268 |
@return Upon successful completion, the value returned depends on cmd as follows: F_DUPFD A new file descriptor. F_GETFD Value of flag (only the low-order bit is defined). F_GETFL Value of flags. F_GETOWN Value of file descriptor owner (Not supported). other Value other than -1. Otherwise, a value of -1 is returned and errno is set to indicate the error.
|
sl@0
|
269 |
|
sl@0
|
270 |
@code
|
sl@0
|
271 |
The fcntl system call provides for control over descriptors. The argument fd is a descriptor to be operated on by cmd as described below. Depending on the value of cmd, fcntl can take an additional third argument int arg.
|
sl@0
|
272 |
|
sl@0
|
273 |
F_DUPFD Return a new descriptor as follows: Lowest numbered available descriptor greater than or equal to arg. Same object references as the original descriptor. New descriptor shares the same file offset if the object
|
sl@0
|
274 |
was a file. Same access mode (read, write or read/write). Same file status flags (i.e., both file descriptors
|
sl@0
|
275 |
share the same file status flags). The close-on-exec flag associated with the new file descriptor
|
sl@0
|
276 |
is set to remain open across execve
|
sl@0
|
277 |
system calls.
|
sl@0
|
278 |
|
sl@0
|
279 |
Limitations:
|
sl@0
|
280 |
|
sl@0
|
281 |
The difference between two file descriptors passed must not
|
sl@0
|
282 |
be greater than 8. If the difference is greater than 8 then behaviour of
|
sl@0
|
283 |
fcntl system call is undefined.F_SELKW flag is not supported because of underlying platform limitations.
|
sl@0
|
284 |
F_RDLCK is not supported because the native platform only supports exclusive locks and not shared locks.
|
sl@0
|
285 |
|
sl@0
|
286 |
|
sl@0
|
287 |
@code
|
sl@0
|
288 |
|
sl@0
|
289 |
O_NONBLOCK Non-blocking I/O; if no data is available to a read system call, or if a write operation would block, the read or
|
sl@0
|
290 |
write call returns -1 with the error EAGAIN. (This flag is Not supported for files)
|
sl@0
|
291 |
O_APPEND Force each write to append at the end of file;
|
sl@0
|
292 |
corresponds to the O_APPEND flag of open .
|
sl@0
|
293 |
(Setting this flag in fcntl currently has no effect on subsequent write system calls)
|
sl@0
|
294 |
O_DIRECT Minimize or eliminate the cache effects of reading and writing. The system
|
sl@0
|
295 |
will attempt to avoid caching data during read or write. If it cannot avoid
|
sl@0
|
296 |
caching the data it will minimize the impact the data has on the cache.
|
sl@0
|
297 |
Use of this flag can drastically reduce performance if not used with care
|
sl@0
|
298 |
(Not supported).
|
sl@0
|
299 |
O_ASYNC Enable the SIGIO signal to be sent to the process group when I/O is possible,
|
sl@0
|
300 |
e.g. upon availability of data to be read (Not supported).
|
sl@0
|
301 |
|
sl@0
|
302 |
F_GETLK Get the first lock that blocks the lock description pointed to by the
|
sl@0
|
303 |
third argument, arg, taken as a pointer to a struct flock (see above).
|
sl@0
|
304 |
The information retrieved overwrites the information passed to fcntl in the flock structure.
|
sl@0
|
305 |
If no lock is found that would prevent this lock from being created,
|
sl@0
|
306 |
the structure is left unchanged by this system call except for the
|
sl@0
|
307 |
lock type which is set to F_UNLCK .
|
sl@0
|
308 |
F_SETLK Set or clear a file segment lock according to the lock description
|
sl@0
|
309 |
pointed to by the third argument, arg, taken as a pointer to a struct flock (see above).
|
sl@0
|
310 |
F_SETLK is used to establish shared (or read) locks ( F_RDLCK )( "F_RDLCK" is not supported)
|
sl@0
|
311 |
or exclusive (or write) locks, ( F_WRLCK, ) as well as remove either type of lock ( F_UNLCK. )
|
sl@0
|
312 |
If a shared or exclusive lock cannot be set, fcntl returns immediately with EAGAIN. (Not supported)
|
sl@0
|
313 |
|
sl@0
|
314 |
F_SETLKW This command is the same as F_SETLK except that if a shared or exclusive lock is blocked by other locks,
|
sl@0
|
315 |
the process waits until the request can be satisfied.(Not Supported)
|
sl@0
|
316 |
If a signal that is to be caught is received while fcntl is waiting for a region, the fcntl will be interrupted if the signal handler has not specified the SA_RESTART . (Not supported)
|
sl@0
|
317 |
|
sl@0
|
318 |
@endcode
|
sl@0
|
319 |
|
sl@0
|
320 |
The fcntl system call provides for control over descriptors.
|
sl@0
|
321 |
The argument fd is a descriptor to be operated on by cmd as described below.
|
sl@0
|
322 |
Depending on the value of cmd, fcntl can take an additional third argument int arg . F_DUPFD Return a new descriptor as follows:
|
sl@0
|
323 |
|
sl@0
|
324 |
Lowest numbered available descriptor greater than or equal to arg. Same object references as the original descriptor. New descriptor shares the same file offset if the object
|
sl@0
|
325 |
was a file. Same access mode (read, write or read/write). Same file status flags (i.e., both file descriptors
|
sl@0
|
326 |
share the same file status flags). The close-on-exec flag associated with the new file descriptor
|
sl@0
|
327 |
is set to remain open across execve
|
sl@0
|
328 |
system calls. Limitation: The difference between two file descriptors passed must not
|
sl@0
|
329 |
be greater than 8. If the difference is greater than 8 then behaviour of
|
sl@0
|
330 |
fcntl system call is undefined. F_GETFD Get the close-on-exec flag associated with the file descriptor fd as FD_CLOEXEC. If the returned value ANDed with FD_CLOEXEC is 0 the file will remain open across exec, otherwise the file will be closed upon execution of exec (arg is ignored). F_SETFD Set the close-on-exec flag associated with fd to arg, where arg is either 0 or FD_CLOEXEC, as described above. F_GETFL Get descriptor status flags, as described below (arg is ignored). F_SETFL Set descriptor status flags to arg . F_GETOWN Get the process ID or process group currently receiving SIGIO and SIGURG signals. Process groups are returned as negative values (arg is ignored)(Not supported). F_SETOWN Set the process or process group to receive SIGIO and SIGURG signals. Process groups are specified by supplying arg as negative, otherwise arg is interpreted as a process ID (Not supported).
|
sl@0
|
331 |
|
sl@0
|
332 |
The flags for the F_GETFL and F_SETFL flags are as follows: O_NONBLOCK No n-blocking I/O; if no data is available to a read system call, or if a write operation would block, the read or
|
sl@0
|
333 |
write call returns -1 with the error EAGAIN. (This flag is Not supported for files) O_APPEND Force each write to append at the end of file;
|
sl@0
|
334 |
corresponds to the O_APPEND flag of open .
|
sl@0
|
335 |
(Setting this flag in fcntl currently has no effect on subsequent write system calls) O_DIRECT Minimize or eliminate the cache effects of reading and writing. The system
|
sl@0
|
336 |
will attempt to avoid caching data during read or write. If it cannot avoid
|
sl@0
|
337 |
caching the data it will minimize the impact the data has on the cache.
|
sl@0
|
338 |
Use of this flag can drastically reduce performance if not used with care
|
sl@0
|
339 |
(Not supported). O_ASYNC Enable the SIGIO signal to be sent to the process group when I/O is possible,
|
sl@0
|
340 |
e.g. upon availability of data to be read (Not supported).
|
sl@0
|
341 |
|
sl@0
|
342 |
Several commands are available for doing advisory file locking;
|
sl@0
|
343 |
they all operate on the following structure:
|
sl@0
|
344 |
|
sl@0
|
345 |
@code
|
sl@0
|
346 |
|
sl@0
|
347 |
struct flock {
|
sl@0
|
348 |
off_tl_start;/* starting offset */
|
sl@0
|
349 |
off_tl_len;/* len = 0 means until end of file */
|
sl@0
|
350 |
pid_tl_pid;/* lock owner */
|
sl@0
|
351 |
shortl_type;/* lock type: read/write, etc. */
|
sl@0
|
352 |
shortl_whence;/* type of l_start */
|
sl@0
|
353 |
};
|
sl@0
|
354 |
|
sl@0
|
355 |
@endcode
|
sl@0
|
356 |
|
sl@0
|
357 |
The commands available for advisory record locking are as follows:
|
sl@0
|
358 |
|
sl@0
|
359 |
F_GETLK Get the first lock that blocks the lock description pointed to by the
|
sl@0
|
360 |
third argument, arg, taken as a pointer to a struct flock (see above).
|
sl@0
|
361 |
The information retrieved overwrites the information passed to fcntl in the flock structure.
|
sl@0
|
362 |
If no lock is found that would prevent this lock from being created,
|
sl@0
|
363 |
the structure is left unchanged by this system call except for the
|
sl@0
|
364 |
lock type which is set to F_UNLCK .
|
sl@0
|
365 |
|
sl@0
|
366 |
F_SETLK Set or clear a file segment lock according to the lock description
|
sl@0
|
367 |
pointed to by the third argument, arg, taken as a pointer to a struct flock (see above).
|
sl@0
|
368 |
F_SETLK is used to establish shared (or read) locks ( F_RDLCK ) . Note "F_RDLCK" is not supported)
|
sl@0
|
369 |
or exclusive (or write) locks, ( F_WRLCK, ) as well as remove either type of lock ( F_UNLCK. )
|
sl@0
|
370 |
If a shared or exclusive lock cannot be set, fcntl returns immediately with EAGAIN. (Not supported)
|
sl@0
|
371 |
|
sl@0
|
372 |
F_SETLKW This command is the same as F_SETLK except that if a shared or exclusive lock is blocked by other locks,
|
sl@0
|
373 |
the process waits until the request can be satisfied.
|
sl@0
|
374 |
If a signal that is to be caught is received while fcntl is waiting for a region, the fcntl will be interrupted if the signal handler has not specified the SA_RESTART . (Not supported)
|
sl@0
|
375 |
|
sl@0
|
376 |
When a shared lock has been set on a segment of a file,other processes can set shared locks on that segment
|
sl@0
|
377 |
or a portion of it.A shared lock prevents any other process from setting an exclusive lock on any portion of the protected area.
|
sl@0
|
378 |
A request for a shared lock fails if the file descriptor was not opened with read access.
|
sl@0
|
379 |
|
sl@0
|
380 |
An exclusive lock prevents any other process from setting a shared lock or an exclusive lock on any portion of the protected area.
|
sl@0
|
381 |
A request for an exclusive lock fails if the file was not opened with write access.
|
sl@0
|
382 |
|
sl@0
|
383 |
The value of l_whence is SEEK_SET, SEEK_CUR, or SEEK_END to indicate that the relative offset, l_start bytes, will be measured from the start of the file,
|
sl@0
|
384 |
current position, or end of the file, respectively.
|
sl@0
|
385 |
The value of l_len is the number of consecutive bytes to be locked.
|
sl@0
|
386 |
If l_len is negative, l_start means end edge of the region.
|
sl@0
|
387 |
The l_pid field is only used with F_GETLK to return the process ID of the process holding a blocking lock .( Not Supported )
|
sl@0
|
388 |
After a successful F_GETLK request, the value of l_whence is SEEK_SET.
|
sl@0
|
389 |
|
sl@0
|
390 |
Locks may start and extend beyond the current end of a file,
|
sl@0
|
391 |
but may not start or extend before the beginning of the file.
|
sl@0
|
392 |
A lock is set to extend to the largest possible value of the
|
sl@0
|
393 |
file offset for that file if l_len is set to zero.
|
sl@0
|
394 |
If l_whence and l_start point to the beginning of the file, and l_len is zero, the entire file is locked.
|
sl@0
|
395 |
If an application wishes only to do entire file locking, the flock system call is much more efficient.
|
sl@0
|
396 |
|
sl@0
|
397 |
There is at most one type of lock set for each byte in the file.
|
sl@0
|
398 |
Before a successful return from an F_SETLK or an F_SETLKW ( Not Supported ) request when the calling process has previously existing locks on bytes in the region specified by the request,
|
sl@0
|
399 |
the previous lock type for each byte in the specified
|
sl@0
|
400 |
region is replaced by the new lock type.
|
sl@0
|
401 |
As specified above under the descriptions
|
sl@0
|
402 |
of shared locks and exclusive locks, an F_SETLK or an F_SETLKW request fails or blocks respectively when another process has existing
|
sl@0
|
403 |
locks on bytes in the specified region and the type of any of those
|
sl@0
|
404 |
locks conflicts with the type specified in the request.
|
sl@0
|
405 |
|
sl@0
|
406 |
This interface follows the completely stupid [sic] semantics of System
|
sl@0
|
407 |
V and -p1003.1-88 that require that all locks associated with a file for a
|
sl@0
|
408 |
given process are removed when any file descriptor for that file is closed by that process. This semantic
|
sl@0
|
409 |
means that applications must be aware of any files that a subroutine library
|
sl@0
|
410 |
may access. For example if an application for updating the password file locks
|
sl@0
|
411 |
the password file database while making the update, and then calls getpwnam to retrieve a record, the lock will
|
sl@0
|
412 |
be lost because getpwnam opens, reads, and closes the password
|
sl@0
|
413 |
database. The database close will release all locks that the process has associated
|
sl@0
|
414 |
with the database, even if the library routine never requested a lock on the
|
sl@0
|
415 |
database. Another minor semantic problem with this interface is that locks are
|
sl@0
|
416 |
not inherited by a child process created using the fork system
|
sl@0
|
417 |
call. The flock interface has much more rational last close semantics and allows locks to be
|
sl@0
|
418 |
inherited by child processes. The flock system call is recommended for applications that want to ensure the integrity
|
sl@0
|
419 |
of their locks when using library routines or wish to pass locks to their children.
|
sl@0
|
420 |
|
sl@0
|
421 |
The fcntl, flock ,
|
sl@0
|
422 |
and lockf locks are compatible.
|
sl@0
|
423 |
Processes using different locking interfaces can cooperate
|
sl@0
|
424 |
over the same file safely.
|
sl@0
|
425 |
However, only one of such interfaces should be used within
|
sl@0
|
426 |
the same process.
|
sl@0
|
427 |
If a file is locked by a process through flock ,
|
sl@0
|
428 |
any record within the file will be seen as locked
|
sl@0
|
429 |
from the viewpoint of another process using fcntl or lockf ,
|
sl@0
|
430 |
and vice versa.
|
sl@0
|
431 |
Note that fcntl (F_GETLK); returns -1 in l_pid if the process holding a blocking lock previously locked the
|
sl@0
|
432 |
file descriptor by flock .
|
sl@0
|
433 |
|
sl@0
|
434 |
All locks associated with a file for a given process are
|
sl@0
|
435 |
removed when the process terminates.
|
sl@0
|
436 |
|
sl@0
|
437 |
All locks obtained before a call to execve remain in effect until the new program releases them.
|
sl@0
|
438 |
If the new program does not know about the locks, they will not be
|
sl@0
|
439 |
released until the program exits.
|
sl@0
|
440 |
|
sl@0
|
441 |
A potential for deadlock occurs if a process controlling a locked region
|
sl@0
|
442 |
is put to sleep by attempting to lock the locked region of another process.
|
sl@0
|
443 |
This implementation detects that sleeping until a locked region is unlocked
|
sl@0
|
444 |
would cause a deadlock and fails with an EDEADLK error.
|
sl@0
|
445 |
|
sl@0
|
446 |
If 'aFid' corresponds to a shared memory object the only values of cmd that are supported are
|
sl@0
|
447 |
F_DUPFD, F_GETFD, F_SETFD, F_SETFL, F_GETFL.
|
sl@0
|
448 |
|
sl@0
|
449 |
Examples:
|
sl@0
|
450 |
@code
|
sl@0
|
451 |
/* Detailed description : Sample usafe of fcntl system call */
|
sl@0
|
452 |
#include <stdio.h>
|
sl@0
|
453 |
#include <fcntl.h>
|
sl@0
|
454 |
int main()
|
sl@0
|
455 |
{
|
sl@0
|
456 |
int fd = 0;
|
sl@0
|
457 |
int flags = 0;
|
sl@0
|
458 |
fd = open("Example.txt " , O_CREAT | O_RDWR , 0666);
|
sl@0
|
459 |
if(fd < 0 )
|
sl@0
|
460 |
{
|
sl@0
|
461 |
printf("Failed to open file Example.txt
|
sl@0
|
462 |
");
|
sl@0
|
463 |
return -1;
|
sl@0
|
464 |
}
|
sl@0
|
465 |
if( (flags = fcntl(fd , F_GETFL) ) < 0 )
|
sl@0
|
466 |
{
|
sl@0
|
467 |
printf("Fcntl system call failed
|
sl@0
|
468 |
");
|
sl@0
|
469 |
return -1;
|
sl@0
|
470 |
}
|
sl@0
|
471 |
printf("Flags of the file %o
|
sl@0
|
472 |
" , flags);
|
sl@0
|
473 |
return 0;
|
sl@0
|
474 |
}
|
sl@0
|
475 |
|
sl@0
|
476 |
@endcode
|
sl@0
|
477 |
Output
|
sl@0
|
478 |
@code
|
sl@0
|
479 |
Flags of the file 2
|
sl@0
|
480 |
|
sl@0
|
481 |
@endcode
|
sl@0
|
482 |
@see close()
|
sl@0
|
483 |
@see flock()
|
sl@0
|
484 |
@see getdtablesize()
|
sl@0
|
485 |
@see open()
|
sl@0
|
486 |
|
sl@0
|
487 |
|
sl@0
|
488 |
|
sl@0
|
489 |
|
sl@0
|
490 |
@publishedAll
|
sl@0
|
491 |
@externallyDefinedApi
|
sl@0
|
492 |
*/
|
sl@0
|
493 |
|
sl@0
|
494 |
|
sl@0
|
495 |
/** @def O_RDONLY
|
sl@0
|
496 |
|
sl@0
|
497 |
open for reading only
|
sl@0
|
498 |
|
sl@0
|
499 |
@publishedAll
|
sl@0
|
500 |
@externallyDefinedApi
|
sl@0
|
501 |
*/
|
sl@0
|
502 |
|
sl@0
|
503 |
/** @def O_WRONLY
|
sl@0
|
504 |
|
sl@0
|
505 |
open for writing only
|
sl@0
|
506 |
|
sl@0
|
507 |
@publishedAll
|
sl@0
|
508 |
@externallyDefinedApi
|
sl@0
|
509 |
*/
|
sl@0
|
510 |
|
sl@0
|
511 |
/** @def O_RDWR
|
sl@0
|
512 |
|
sl@0
|
513 |
open for reading and writing
|
sl@0
|
514 |
|
sl@0
|
515 |
@publishedAll
|
sl@0
|
516 |
@externallyDefinedApi
|
sl@0
|
517 |
*/
|
sl@0
|
518 |
|
sl@0
|
519 |
|
sl@0
|
520 |
/** @def O_ACCMODE
|
sl@0
|
521 |
|
sl@0
|
522 |
mask for above modes
|
sl@0
|
523 |
|
sl@0
|
524 |
@publishedAll
|
sl@0
|
525 |
@externallyDefinedApi
|
sl@0
|
526 |
*/
|
sl@0
|
527 |
|
sl@0
|
528 |
/** @def O_NONBLOCK
|
sl@0
|
529 |
|
sl@0
|
530 |
no delay
|
sl@0
|
531 |
|
sl@0
|
532 |
@publishedAll
|
sl@0
|
533 |
@externallyDefinedApi
|
sl@0
|
534 |
*/
|
sl@0
|
535 |
|
sl@0
|
536 |
/** @def O_APPEND
|
sl@0
|
537 |
|
sl@0
|
538 |
set append mode
|
sl@0
|
539 |
|
sl@0
|
540 |
@publishedAll
|
sl@0
|
541 |
@externallyDefinedApi
|
sl@0
|
542 |
*/
|
sl@0
|
543 |
|
sl@0
|
544 |
/** @def O_CREAT
|
sl@0
|
545 |
|
sl@0
|
546 |
create if nonexistent
|
sl@0
|
547 |
|
sl@0
|
548 |
@publishedAll
|
sl@0
|
549 |
@externallyDefinedApi
|
sl@0
|
550 |
*/
|
sl@0
|
551 |
|
sl@0
|
552 |
/** @def O_TRUNC
|
sl@0
|
553 |
|
sl@0
|
554 |
truncate to zero length
|
sl@0
|
555 |
|
sl@0
|
556 |
@publishedAll
|
sl@0
|
557 |
@externallyDefinedApi
|
sl@0
|
558 |
*/
|
sl@0
|
559 |
|
sl@0
|
560 |
|
sl@0
|
561 |
/** @def O_EXCL
|
sl@0
|
562 |
|
sl@0
|
563 |
error if already exists
|
sl@0
|
564 |
|
sl@0
|
565 |
@publishedAll
|
sl@0
|
566 |
@externallyDefinedApi
|
sl@0
|
567 |
*/
|
sl@0
|
568 |
|
sl@0
|
569 |
/** @def O_NOCTTY
|
sl@0
|
570 |
|
sl@0
|
571 |
don't assign controlling terminal
|
sl@0
|
572 |
|
sl@0
|
573 |
@publishedAll
|
sl@0
|
574 |
@externallyDefinedApi
|
sl@0
|
575 |
*/
|
sl@0
|
576 |
|
sl@0
|
577 |
/** @def F_DUPFD
|
sl@0
|
578 |
|
sl@0
|
579 |
Duplicate file descriptor.
|
sl@0
|
580 |
|
sl@0
|
581 |
@publishedAll
|
sl@0
|
582 |
@externallyDefinedApi
|
sl@0
|
583 |
*/
|
sl@0
|
584 |
|
sl@0
|
585 |
|
sl@0
|
586 |
/** @def F_GETFD
|
sl@0
|
587 |
|
sl@0
|
588 |
Get file descriptor flags.
|
sl@0
|
589 |
|
sl@0
|
590 |
@publishedAll
|
sl@0
|
591 |
@externallyDefinedApi
|
sl@0
|
592 |
*/
|
sl@0
|
593 |
|
sl@0
|
594 |
/** @def F_SETFD
|
sl@0
|
595 |
|
sl@0
|
596 |
set file descriptor flags
|
sl@0
|
597 |
|
sl@0
|
598 |
@publishedAll
|
sl@0
|
599 |
@externallyDefinedApi
|
sl@0
|
600 |
*/
|
sl@0
|
601 |
|
sl@0
|
602 |
/** @def F_GETFL
|
sl@0
|
603 |
|
sl@0
|
604 |
get file status flags
|
sl@0
|
605 |
|
sl@0
|
606 |
@publishedAll
|
sl@0
|
607 |
@externallyDefinedApi
|
sl@0
|
608 |
*/
|
sl@0
|
609 |
|
sl@0
|
610 |
/** @def F_SETFL
|
sl@0
|
611 |
|
sl@0
|
612 |
set file status flags
|
sl@0
|
613 |
|
sl@0
|
614 |
@publishedAll
|
sl@0
|
615 |
@externallyDefinedApi
|
sl@0
|
616 |
*/
|
sl@0
|
617 |
|
sl@0
|
618 |
/** @def F_GETOWN
|
sl@0
|
619 |
|
sl@0
|
620 |
get SIGIO/SIGURG proc/pgrp
|
sl@0
|
621 |
|
sl@0
|
622 |
@publishedAll
|
sl@0
|
623 |
@externallyDefinedApi
|
sl@0
|
624 |
*/
|
sl@0
|
625 |
|
sl@0
|
626 |
/** @def F_SETOWN
|
sl@0
|
627 |
|
sl@0
|
628 |
set SIGIO/SIGURG proc/pgrp
|
sl@0
|
629 |
|
sl@0
|
630 |
@publishedAll
|
sl@0
|
631 |
@externallyDefinedApi
|
sl@0
|
632 |
*/
|
sl@0
|
633 |
|
sl@0
|
634 |
/** @def F_GETLK
|
sl@0
|
635 |
|
sl@0
|
636 |
get record locking information
|
sl@0
|
637 |
|
sl@0
|
638 |
@publishedAll
|
sl@0
|
639 |
@externallyDefinedApi
|
sl@0
|
640 |
*/
|
sl@0
|
641 |
|
sl@0
|
642 |
|
sl@0
|
643 |
/** @def F_SETLK
|
sl@0
|
644 |
|
sl@0
|
645 |
set record locking information
|
sl@0
|
646 |
|
sl@0
|
647 |
@publishedAll
|
sl@0
|
648 |
@externallyDefinedApi
|
sl@0
|
649 |
*/
|
sl@0
|
650 |
|
sl@0
|
651 |
/** @def F_SETLKW
|
sl@0
|
652 |
|
sl@0
|
653 |
F_SETLK; wait if blocked
|
sl@0
|
654 |
|
sl@0
|
655 |
@publishedAll
|
sl@0
|
656 |
@externallyDefinedApi
|
sl@0
|
657 |
*/
|
sl@0
|
658 |
|
sl@0
|
659 |
/** @def F_GETLK64
|
sl@0
|
660 |
|
sl@0
|
661 |
get record locking information of large file
|
sl@0
|
662 |
|
sl@0
|
663 |
@publishedAll
|
sl@0
|
664 |
@externallyDefinedApi
|
sl@0
|
665 |
*/
|
sl@0
|
666 |
|
sl@0
|
667 |
|
sl@0
|
668 |
/** @def F_SETLK64
|
sl@0
|
669 |
|
sl@0
|
670 |
set record locking information to a large file
|
sl@0
|
671 |
|
sl@0
|
672 |
@publishedAll
|
sl@0
|
673 |
@externallyDefinedApi
|
sl@0
|
674 |
*/
|
sl@0
|
675 |
|
sl@0
|
676 |
/** @def F_SETLKW64
|
sl@0
|
677 |
|
sl@0
|
678 |
F_SETLK; wait if blocked in a large file
|
sl@0
|
679 |
|
sl@0
|
680 |
@publishedAll
|
sl@0
|
681 |
@externallyDefinedApi
|
sl@0
|
682 |
*/
|
sl@0
|
683 |
|
sl@0
|
684 |
/** @def F_RDLCK
|
sl@0
|
685 |
|
sl@0
|
686 |
shared or read lock
|
sl@0
|
687 |
|
sl@0
|
688 |
@publishedAll
|
sl@0
|
689 |
@externallyDefinedApi
|
sl@0
|
690 |
*/
|
sl@0
|
691 |
|
sl@0
|
692 |
/** @def F_UNLCK
|
sl@0
|
693 |
|
sl@0
|
694 |
unlock
|
sl@0
|
695 |
|
sl@0
|
696 |
@publishedAll
|
sl@0
|
697 |
@externallyDefinedApi
|
sl@0
|
698 |
*/
|
sl@0
|
699 |
|
sl@0
|
700 |
/** @def F_WRLCK
|
sl@0
|
701 |
|
sl@0
|
702 |
exclusive or write lock
|
sl@0
|
703 |
|
sl@0
|
704 |
@publishedAll
|
sl@0
|
705 |
@externallyDefinedApi
|
sl@0
|
706 |
*/
|
sl@0
|
707 |
|
sl@0
|
708 |
/** @def O_SYNC
|
sl@0
|
709 |
|
sl@0
|
710 |
POSIX synonym for O_FSYNC
|
sl@0
|
711 |
|
sl@0
|
712 |
@publishedAll
|
sl@0
|
713 |
@externallyDefinedApi
|
sl@0
|
714 |
*/
|
sl@0
|
715 |
|
sl@0
|
716 |
/** @def FD_CLOEXEC
|
sl@0
|
717 |
|
sl@0
|
718 |
close-on-exec flag
|
sl@0
|
719 |
|
sl@0
|
720 |
@publishedAll
|
sl@0
|
721 |
@externallyDefinedApi
|
sl@0
|
722 |
*/
|
sl@0
|
723 |
|
sl@0
|
724 |
|
sl@0
|
725 |
/** @def FREAD
|
sl@0
|
726 |
|
sl@0
|
727 |
Kernel encoding of open mode; separate read and write bits that are independently testable: 1 greater than the above.
|
sl@0
|
728 |
FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH, which was documented to use FREAD/FWRITE, continues to work.
|
sl@0
|
729 |
|
sl@0
|
730 |
@publishedAll
|
sl@0
|
731 |
@externallyDefinedApi
|
sl@0
|
732 |
*/
|
sl@0
|
733 |
|
sl@0
|
734 |
|
sl@0
|
735 |
/** @def FWRITE
|
sl@0
|
736 |
|
sl@0
|
737 |
Kernel encoding of open mode; separate read and write bits that are independently testable: 1 greater than the above.
|
sl@0
|
738 |
FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH, which was documented to use FREAD/FWRITE, continues to work.
|
sl@0
|
739 |
|
sl@0
|
740 |
@publishedAll
|
sl@0
|
741 |
@externallyDefinedApi
|
sl@0
|
742 |
*/
|
sl@0
|
743 |
|
sl@0
|
744 |
|
sl@0
|
745 |
|
sl@0
|
746 |
|
sl@0
|
747 |
|
sl@0
|
748 |
|
sl@0
|
749 |
|
sl@0
|
750 |
|
sl@0
|
751 |
|
sl@0
|
752 |
|
sl@0
|
753 |
|
sl@0
|
754 |
|