Update contrib.
1 /** @file ../include/sys/fcntl.h
5 /** @fn open(const char *file, int flags, ...)
9 @return If successful, open returns a non-negative integer, termed a file descriptor. It returns
10 -1 on failure and sets errno to indicate the error.
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
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
18 The flags specified are formed by OR'ing the following values
22 O_RDONLYopen for reading only
23 O_WRONLYopen for writing only
24 O_RDWRopen for reading and writing
25 O_NONBLOCKdo not block on open
26 O_APPENDappend on each write
27 O_CREATcreate file if it does not exist
28 O_TRUNCtruncate size to 0
29 O_EXCLerror if create and file exists
30 O_SHLOCKatomically obtain a shared lock
31 O_EXLOCKatomically obtain an exclusive lock
32 O_DIRECTeliminate or reduce cache effects
33 O_FSYNCsynchronous writes
34 O_NOFOLLOWdo not follow symlinks
35 Following options are currently not supported :
36 O_NONBLOCK, O_SHLOCK, O_EXLOCK, O_DIRECT, O_FSYNC, O_NOFOLLOW.
39 This flag, if passed into the open() API, enables it to open a large file (files with 64 bit file sizes)
42 Opening a file with O_APPEND set causes each write on the file
43 to be appended to the end.
45 If O_TRUNC is specified, and the file exists, the file is truncated to
48 If O_EXCL is set with O_CREAT and the file already
49 exists, open returns an error.
51 implement a simple exclusive access locking mechanism.
53 If O_EXCL is set and the last component of the pathname is
54 a symbolic link, open will fail even if the symbolic
55 link points to a non-existent name.
57 If the O_NONBLOCK flag is specified and the open system call would result in the process being blocked for some
58 reason (e.g., waiting for carrier on a dialup line), open returns immediately. The descriptor remains in non-blocking mode
59 for subsequent operations. This mode need not have any effect on files other
62 If O_FSYNC is used in the mask all writes will immediately be written
63 to disk, the kernel will not cache written data and all writes on the descriptor
64 will not return until the data to be written completes.
66 If O_NOFOLLOW is used in the mask and the target file passed to open is a symbolic link the open will fail.
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
69 underlying file system supports locking).
71 O_DIRECT may be used to minimize or eliminate the cache effects of
72 reading and writing. The system will attempt to avoid caching the data being
73 read or written. If it cannot avoid caching the data, it will minimize the impact
74 the data has on the cache. Use of this flag can drastically reduce performance
75 if not used with care.
77 If successful, open returns a non-negative integer termed a file descriptor. It returns
78 -1 on failure. The file pointer used to mark the current position within the
79 file is set to the beginning of the file.
81 When a new file is created it is given the group of the directory
84 The new descriptor is set to remain open across execve system calls.; See close and fcntl .
86 The system imposes a limit on the number of file descriptors
87 open simultaneously by one process.
88 The getdtablesize system call returns the current system limit.
94 1) Mode values for group and others are ignored
96 2) Execute bit and setuid on exec bit are ignored.
98 3) The default working directory of a process is initalized to C: \\private\\UID
99 (UID of the calling application) and any data written into this directory persists
100 between phone resets.
102 4) If the specified file is a symbolic link and the file it is pointing to
103 is invalid, the symbolic link file will be automatically removed.
105 5) A file in cannot be created with write-only permissions. Attempting to create
106 a file with write-only permissions will result in a file with read-write permission.
108 6) Creating a new file with the O_CREAT flag does not alter the time stamp
109 of the parent directory.
111 7) A file has only two time stamps: access and modification. Creation time
112 stamp of the file is not supported and access time stamp is initially set equal
113 to modification time stamp.
115 8) Users should not use O_DIRECT flag as the underlying implementation makes explicit
116 use of O_DIRECT by default. Instead if the users want to use read/write
117 buffering they can use O_BUFFERED.
119 9) Users should not use O_BINARY flag as the underlying implementation opens a file
120 in binary mode by default. Instead if the users want to open the file in text mode
125 /* This example creates a file in the current working directory and
126 * opens it in read-write mode. */
127 #include <sys/types.h>
128 #include <sys/stat.h>
133 fd = open("Example.txt" , O_CREAT | O_EXCL , 0666);
136 printf("Failed to create and open file in current working directory
140 printf("File created and opened in current working directory
148 File created and opened in current working directory
163 KErrNotReady of Symbian error code is mapped to ENOENT, which typically means drive
164 not found or filesystem not mounted on the drive.
166 @capability Deferred @ref RFs::Entry(const TDesC16&, TEntry&) const
169 @externallyDefinedApi
173 /** @fn open64(const char *file, int flags, ...)
177 @return If successful, open64() returns a non-negative integer, termed a file descriptor. It returns
178 -1 on failure and sets errno to indicate the error.
181 For full documentation see: http://www.unix.org/version2/whatsnew/lfs20mar.html#3.0
186 @externallyDefinedApi
189 /**@typedef typedef __off_t off64_t;
194 @externallyDefinedApi
197 /** @fn creat(const char *file, mode_t mode)
200 @return open and creat return the new file descriptor, or -1 if an error occurred (in
201 which case errno is set appropriately).
203 This interface is made obsolete by: open
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.
207 Creation time stamp of the file is not supported, here accesstime stamp is equal to modification time stamp.
211 /* Detailed description : This test code demonstrates creat system call usage, it creates a
212 * in current working directory(if file exists then it is truncated. Preconditions : None */
213 #include <sys/types.h>
214 #include <sys/stat.h>
219 fd = creat("Example.txt" , 0666);
222 printf("File creation failed
226 printf("Example.txt file created
234 Example.txt file created
241 KErrNotReady of Symbian error code is mapped to ENOENT, which typically means drive
242 not found or filesystem not mounted on the drive.
244 @capability Deferred @ref RFs::Entry(const TDesC16&, TEntry&) const
247 @externallyDefinedApi
250 /** @fn creat64(const char *file, mode_t mode)
253 @return creat64() returns the new file descriptor, or -1 if an error occurred (in
254 which case errno is set appropriately).
256 For full documentation see: http://www.unix.org/version2/whatsnew/lfs20mar.html#3.0
261 @externallyDefinedApi
264 /** @fn fcntl(int aFid, int aCmd, ...)
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.
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.
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
274 was a file. Same access mode (read, write or read/write). Same file status flags (i.e., both file descriptors
275 share the same file status flags). The close-on-exec flag associated with the new file descriptor
276 is set to remain open across execve
281 The difference between two file descriptors passed must not
282 be greater than 8. If the difference is greater than 8 then behaviour of
283 fcntl system call is undefined.F_SELKW flag is not supported because of underlying platform limitations.
284 F_RDLCK is not supported because the native platform only supports exclusive locks and not shared locks.
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
290 write call returns -1 with the error EAGAIN. (This flag is Not supported for files)
291 O_APPEND Force each write to append at the end of file;
292 corresponds to the O_APPEND flag of open .
293 (Setting this flag in fcntl currently has no effect on subsequent write system calls)
294 O_DIRECT Minimize or eliminate the cache effects of reading and writing. The system
295 will attempt to avoid caching data during read or write. If it cannot avoid
296 caching the data it will minimize the impact the data has on the cache.
297 Use of this flag can drastically reduce performance if not used with care
299 O_ASYNC Enable the SIGIO signal to be sent to the process group when I/O is possible,
300 e.g. upon availability of data to be read (Not supported).
302 F_GETLK Get the first lock that blocks the lock description pointed to by the
303 third argument, arg, taken as a pointer to a struct flock (see above).
304 The information retrieved overwrites the information passed to fcntl in the flock structure.
305 If no lock is found that would prevent this lock from being created,
306 the structure is left unchanged by this system call except for the
307 lock type which is set to F_UNLCK .
308 F_SETLK Set or clear a file segment lock according to the lock description
309 pointed to by the third argument, arg, taken as a pointer to a struct flock (see above).
310 F_SETLK is used to establish shared (or read) locks ( F_RDLCK )( "F_RDLCK" is not supported)
311 or exclusive (or write) locks, ( F_WRLCK, ) as well as remove either type of lock ( F_UNLCK. )
312 If a shared or exclusive lock cannot be set, fcntl returns immediately with EAGAIN. (Not supported)
314 F_SETLKW This command is the same as F_SETLK except that if a shared or exclusive lock is blocked by other locks,
315 the process waits until the request can be satisfied.(Not Supported)
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)
320 The fcntl system call provides for control over descriptors.
321 The argument fd is a descriptor to be operated on by cmd as described below.
322 Depending on the value of cmd, fcntl can take an additional third argument int arg . F_DUPFD Return a new descriptor as follows:
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
325 was a file. Same access mode (read, write or read/write). Same file status flags (i.e., both file descriptors
326 share the same file status flags). The close-on-exec flag associated with the new file descriptor
327 is set to remain open across execve
328 system calls. Limitation: The difference between two file descriptors passed must not
329 be greater than 8. If the difference is greater than 8 then behaviour of
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).
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
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;
334 corresponds to the O_APPEND flag of open .
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
336 will attempt to avoid caching data during read or write. If it cannot avoid
337 caching the data it will minimize the impact the data has on the cache.
338 Use of this flag can drastically reduce performance if not used with care
339 (Not supported). O_ASYNC Enable the SIGIO signal to be sent to the process group when I/O is possible,
340 e.g. upon availability of data to be read (Not supported).
342 Several commands are available for doing advisory file locking;
343 they all operate on the following structure:
348 off_tl_start;/* starting offset */
349 off_tl_len;/* len = 0 means until end of file */
350 pid_tl_pid;/* lock owner */
351 shortl_type;/* lock type: read/write, etc. */
352 shortl_whence;/* type of l_start */
357 The commands available for advisory record locking are as follows:
359 F_GETLK Get the first lock that blocks the lock description pointed to by the
360 third argument, arg, taken as a pointer to a struct flock (see above).
361 The information retrieved overwrites the information passed to fcntl in the flock structure.
362 If no lock is found that would prevent this lock from being created,
363 the structure is left unchanged by this system call except for the
364 lock type which is set to F_UNLCK .
366 F_SETLK Set or clear a file segment lock according to the lock description
367 pointed to by the third argument, arg, taken as a pointer to a struct flock (see above).
368 F_SETLK is used to establish shared (or read) locks ( F_RDLCK ) . Note "F_RDLCK" is not supported)
369 or exclusive (or write) locks, ( F_WRLCK, ) as well as remove either type of lock ( F_UNLCK. )
370 If a shared or exclusive lock cannot be set, fcntl returns immediately with EAGAIN. (Not supported)
372 F_SETLKW This command is the same as F_SETLK except that if a shared or exclusive lock is blocked by other locks,
373 the process waits until the request can be satisfied.
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)
376 When a shared lock has been set on a segment of a file,other processes can set shared locks on that segment
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.
378 A request for a shared lock fails if the file descriptor was not opened with read access.
380 An exclusive lock prevents any other process from setting a shared lock or an exclusive lock on any portion of the protected area.
381 A request for an exclusive lock fails if the file was not opened with write access.
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,
384 current position, or end of the file, respectively.
385 The value of l_len is the number of consecutive bytes to be locked.
386 If l_len is negative, l_start means end edge of the region.
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 )
388 After a successful F_GETLK request, the value of l_whence is SEEK_SET.
390 Locks may start and extend beyond the current end of a file,
391 but may not start or extend before the beginning of the file.
392 A lock is set to extend to the largest possible value of the
393 file offset for that file if l_len is set to zero.
394 If l_whence and l_start point to the beginning of the file, and l_len is zero, the entire file is locked.
395 If an application wishes only to do entire file locking, the flock system call is much more efficient.
397 There is at most one type of lock set for each byte in the file.
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,
399 the previous lock type for each byte in the specified
400 region is replaced by the new lock type.
401 As specified above under the descriptions
402 of shared locks and exclusive locks, an F_SETLK or an F_SETLKW request fails or blocks respectively when another process has existing
403 locks on bytes in the specified region and the type of any of those
404 locks conflicts with the type specified in the request.
406 This interface follows the completely stupid [sic] semantics of System
407 V and -p1003.1-88 that require that all locks associated with a file for a
408 given process are removed when any file descriptor for that file is closed by that process. This semantic
409 means that applications must be aware of any files that a subroutine library
410 may access. For example if an application for updating the password file locks
411 the password file database while making the update, and then calls getpwnam to retrieve a record, the lock will
412 be lost because getpwnam opens, reads, and closes the password
413 database. The database close will release all locks that the process has associated
414 with the database, even if the library routine never requested a lock on the
415 database. Another minor semantic problem with this interface is that locks are
416 not inherited by a child process created using the fork system
417 call. The flock interface has much more rational last close semantics and allows locks to be
418 inherited by child processes. The flock system call is recommended for applications that want to ensure the integrity
419 of their locks when using library routines or wish to pass locks to their children.
422 and lockf locks are compatible.
423 Processes using different locking interfaces can cooperate
424 over the same file safely.
425 However, only one of such interfaces should be used within
427 If a file is locked by a process through flock ,
428 any record within the file will be seen as locked
429 from the viewpoint of another process using fcntl or lockf ,
431 Note that fcntl (F_GETLK); returns -1 in l_pid if the process holding a blocking lock previously locked the
432 file descriptor by flock .
434 All locks associated with a file for a given process are
435 removed when the process terminates.
437 All locks obtained before a call to execve remain in effect until the new program releases them.
438 If the new program does not know about the locks, they will not be
439 released until the program exits.
441 A potential for deadlock occurs if a process controlling a locked region
442 is put to sleep by attempting to lock the locked region of another process.
443 This implementation detects that sleeping until a locked region is unlocked
444 would cause a deadlock and fails with an EDEADLK error.
446 If 'aFid' corresponds to a shared memory object the only values of cmd that are supported are
447 F_DUPFD, F_GETFD, F_SETFD, F_SETFL, F_GETFL.
451 /* Detailed description : Sample usafe of fcntl system call */
458 fd = open("Example.txt " , O_CREAT | O_RDWR , 0666);
461 printf("Failed to open file Example.txt
465 if( (flags = fcntl(fd , F_GETFL) ) < 0 )
467 printf("Fcntl system call failed
471 printf("Flags of the file %o
491 @externallyDefinedApi
497 open for reading only
500 @externallyDefinedApi
505 open for writing only
508 @externallyDefinedApi
513 open for reading and writing
516 @externallyDefinedApi
525 @externallyDefinedApi
533 @externallyDefinedApi
541 @externallyDefinedApi
546 create if nonexistent
549 @externallyDefinedApi
554 truncate to zero length
557 @externallyDefinedApi
563 error if already exists
566 @externallyDefinedApi
571 don't assign controlling terminal
574 @externallyDefinedApi
579 Duplicate file descriptor.
582 @externallyDefinedApi
588 Get file descriptor flags.
591 @externallyDefinedApi
596 set file descriptor flags
599 @externallyDefinedApi
604 get file status flags
607 @externallyDefinedApi
612 set file status flags
615 @externallyDefinedApi
620 get SIGIO/SIGURG proc/pgrp
623 @externallyDefinedApi
628 set SIGIO/SIGURG proc/pgrp
631 @externallyDefinedApi
636 get record locking information
639 @externallyDefinedApi
645 set record locking information
648 @externallyDefinedApi
653 F_SETLK; wait if blocked
656 @externallyDefinedApi
661 get record locking information of large file
664 @externallyDefinedApi
670 set record locking information to a large file
673 @externallyDefinedApi
678 F_SETLK; wait if blocked in a large file
681 @externallyDefinedApi
689 @externallyDefinedApi
697 @externallyDefinedApi
702 exclusive or write lock
705 @externallyDefinedApi
710 POSIX synonym for O_FSYNC
713 @externallyDefinedApi
721 @externallyDefinedApi
727 Kernel encoding of open mode; separate read and write bits that are independently testable: 1 greater than the above.
728 FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH, which was documented to use FREAD/FWRITE, continues to work.
731 @externallyDefinedApi
737 Kernel encoding of open mode; separate read and write bits that are independently testable: 1 greater than the above.
738 FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH, which was documented to use FREAD/FWRITE, continues to work.
741 @externallyDefinedApi