]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/fcntl.h
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / sys / fcntl.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 /*-
25 * Copyright (c) 1983, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)fcntl.h 8.3 (Berkeley) 1/21/94
62 */
63
64
65 #ifndef _SYS_FCNTL_H_
66 #define _SYS_FCNTL_H_
67
68 /*
69 * This file includes the definitions for open and fcntl
70 * described by POSIX for <fcntl.h>; it also includes
71 * related kernel definitions.
72 */
73 #include <sys/_types.h>
74 #include <sys/cdefs.h>
75
76 /* We should not be exporting size_t here. Temporary for gcc bootstrapping. */
77 #ifndef _SIZE_T
78 #define _SIZE_T
79 typedef __darwin_size_t size_t;
80 #endif
81
82 #ifndef _MODE_T
83 typedef __darwin_mode_t mode_t;
84 #define _MODE_T
85 #endif
86
87 #ifndef _OFF_T
88 typedef __darwin_off_t off_t;
89 #define _OFF_T
90 #endif
91
92 #ifndef _PID_T
93 typedef __darwin_pid_t pid_t;
94 #define _PID_T
95 #endif
96
97 /*
98 * File status flags: these are used by open(2), fcntl(2).
99 * They are also used (indirectly) in the kernel file structure f_flags,
100 * which is a superset of the open/fcntl flags. Open flags and f_flags
101 * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags).
102 * Open/fcntl flags begin with O_; kernel-internal flags begin with F.
103 */
104 /* open-only flags */
105 #define O_RDONLY 0x0000 /* open for reading only */
106 #define O_WRONLY 0x0001 /* open for writing only */
107 #define O_RDWR 0x0002 /* open for reading and writing */
108 #define O_ACCMODE 0x0003 /* mask for above modes */
109
110 /*
111 * Kernel encoding of open mode; separate read and write bits that are
112 * independently testable: 1 greater than the above.
113 *
114 * XXX
115 * FREAD and FWRITE are excluded from the #ifdef KERNEL so that TIOCFLUSH,
116 * which was documented to use FREAD/FWRITE, continues to work.
117 */
118 #ifndef _POSIX_C_SOURCE
119 #define FREAD 0x0001
120 #define FWRITE 0x0002
121 #endif
122 #define O_NONBLOCK 0x0004 /* no delay */
123 #define O_APPEND 0x0008 /* set append mode */
124 #define O_SYNC 0x0080 /* synchronous writes */
125 #ifndef _POSIX_C_SOURCE
126 #define O_SHLOCK 0x0010 /* open with shared file lock */
127 #define O_EXLOCK 0x0020 /* open with exclusive file lock */
128 #define O_ASYNC 0x0040 /* signal pgrp when data ready */
129 #define O_FSYNC O_SYNC /* source compatibility: do not use */
130 #define O_NOFOLLOW 0x0100 /* don't follow symlinks */
131 #endif /* _POSIX_C_SOURCE */
132 #define O_CREAT 0x0200 /* create if nonexistant */
133 #define O_TRUNC 0x0400 /* truncate to zero length */
134 #define O_EXCL 0x0800 /* error if already exists */
135 #ifdef KERNEL
136 #define FMARK 0x1000 /* mark during gc() */
137 #define FDEFER 0x2000 /* defer for next gc pass */
138 #define FHASLOCK 0x4000 /* descriptor holds advisory lock */
139 #endif
140 #ifndef _POSIX_C_SOURCE
141 #define O_EVTONLY 0x8000 /* descriptor requested for event notifications only */
142 #endif
143
144 #ifdef KERNEL
145 #define FWASWRITTEN 0x10000 /* descriptor was written */
146 #endif
147
148 /* defined by POSIX 1003.1; BSD default, so no bit required */
149 #define O_NOCTTY 0 /* don't assign controlling terminal */
150 //#define O_SYNC /* ??? POSIX: Write according to synchronized I/O file integrity completion */
151
152 #ifdef KERNEL
153 /* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */
154 #define FFLAGS(oflags) ((oflags) + 1)
155 #define OFLAGS(fflags) ((fflags) - 1)
156
157 /* bits to save after open */
158 #define FMASK (FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK)
159 /* bits settable by fcntl(F_SETFL, ...) */
160 #define FCNTLFLAGS (FAPPEND|FASYNC|FFSYNC|FNONBLOCK)
161 #endif
162
163 /*
164 * The O_* flags used to have only F* names, which were used in the kernel
165 * and by fcntl. We retain the F* names for the kernel f_flags field
166 * and for backward compatibility for fcntl.
167 */
168 #ifndef _POSIX_C_SOURCE
169 #define FAPPEND O_APPEND /* kernel/compat */
170 #define FASYNC O_ASYNC /* kernel/compat */
171 #define FFSYNC O_FSYNC /* kernel */
172 #define FNONBLOCK O_NONBLOCK /* kernel */
173 #define FNDELAY O_NONBLOCK /* compat */
174 #define O_NDELAY O_NONBLOCK /* compat */
175 #endif
176
177 /*
178 * Flags used for copyfile(2)
179 */
180
181 #ifndef _POSIX_C_SOURCE
182 #define CPF_OVERWRITE 1
183 #define CPF_IGNORE_MODE 2
184 #define CPF_MASK (CPF_OVERWRITE|CPF_IGNORE_MODE)
185 #endif
186
187 /*
188 * Constants used for fcntl(2)
189 */
190
191 /* command values */
192 #define F_DUPFD 0 /* duplicate file descriptor */
193 #define F_GETFD 1 /* get file descriptor flags */
194 #define F_SETFD 2 /* set file descriptor flags */
195 #define F_GETFL 3 /* get file status flags */
196 #define F_SETFL 4 /* set file status flags */
197 #define F_GETOWN 5 /* get SIGIO/SIGURG proc/pgrp */
198 #define F_SETOWN 6 /* set SIGIO/SIGURG proc/pgrp */
199 #define F_GETLK 7 /* get record locking information */
200 #define F_SETLK 8 /* set record locking information */
201 #define F_SETLKW 9 /* F_SETLK; wait if blocked */
202 #ifndef _POSIX_C_SOURCE
203 #define F_CHKCLEAN 41 /* Used for regression test */
204 #define F_PREALLOCATE 42 /* Preallocate storage */
205 #define F_SETSIZE 43 /* Truncate a file without zeroing space */
206 #define F_RDADVISE 44 /* Issue an advisory read async with no copy to user */
207 #define F_RDAHEAD 45 /* turn read ahead off/on */
208 #define F_READBOOTSTRAP 46 /* Read bootstrap from disk */
209 #define F_WRITEBOOTSTRAP 47 /* Write bootstrap on disk */
210 #define F_NOCACHE 48 /* turning data caching off/on */
211 #define F_LOG2PHYS 49 /* file offset to device offset */
212 #define F_GETPATH 50 /* return the full path of the fd */
213 #define F_FULLFSYNC 51 /* fsync + ask the drive to flush to the media */
214 #define F_PATHPKG_CHECK 52 /* find which component (if any) is a package */
215 #define F_FREEZE_FS 53 /* "freeze" all fs operations */
216 #define F_THAW_FS 54 /* "thaw" all fs operations */
217 #define F_GLOBAL_NOCACHE 55 /* turn data caching off/on (globally) for this file */
218
219 // FS-specific fcntl()'s numbers begin at 0x00010000 and go up
220 #define FCNTL_FS_SPECIFIC_BASE 0x00010000
221 #endif /* _POSIX_C_SOURCE */
222
223 /* file descriptor flags (F_GETFD, F_SETFD) */
224 #define FD_CLOEXEC 1 /* close-on-exec flag */
225
226 /* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */
227 #define F_RDLCK 1 /* shared or read lock */
228 #define F_UNLCK 2 /* unlock */
229 #define F_WRLCK 3 /* exclusive or write lock */
230 #ifdef KERNEL
231 #define F_WAIT 0x010 /* Wait until lock is granted */
232 #define F_FLOCK 0x020 /* Use flock(2) semantics for lock */
233 #define F_POSIX 0x040 /* Use POSIX semantics for lock */
234 #endif
235
236 /*
237 * [XSI] The values used for l_whence shall be defined as described
238 * in <unistd.h>
239 */
240 #ifndef SEEK_SET
241 #define SEEK_SET 0 /* set file offset to offset */
242 #define SEEK_CUR 1 /* set file offset to current plus offset */
243 #define SEEK_END 2 /* set file offset to EOF plus offset */
244 #endif /* !SEEK_SET */
245
246 /*
247 * [XSI] The symbolic names for file modes for use as values of mode_t
248 * shall be defined as described in <sys/stat.h>
249 */
250 #ifndef S_IFMT
251 /* File type */
252 #define S_IFMT 0170000 /* [XSI] type of file mask */
253 #define S_IFIFO 0010000 /* [XSI] named pipe (fifo) */
254 #define S_IFCHR 0020000 /* [XSI] character special */
255 #define S_IFDIR 0040000 /* [XSI] directory */
256 #define S_IFBLK 0060000 /* [XSI] block special */
257 #define S_IFREG 0100000 /* [XSI] regular */
258 #define S_IFLNK 0120000 /* [XSI] symbolic link */
259 #define S_IFSOCK 0140000 /* [XSI] socket */
260 #ifndef _POSIX_C_SOURCE
261 #define S_IFWHT 0160000 /* whiteout */
262 #define S_IFXATTR 0200000 /* extended attribute */
263 #endif
264
265 /* File mode */
266 /* Read, write, execute/search by owner */
267 #define S_IRWXU 0000700 /* [XSI] RWX mask for owner */
268 #define S_IRUSR 0000400 /* [XSI] R for owner */
269 #define S_IWUSR 0000200 /* [XSI] W for owner */
270 #define S_IXUSR 0000100 /* [XSI] X for owner */
271 /* Read, write, execute/search by group */
272 #define S_IRWXG 0000070 /* [XSI] RWX mask for group */
273 #define S_IRGRP 0000040 /* [XSI] R for group */
274 #define S_IWGRP 0000020 /* [XSI] W for group */
275 #define S_IXGRP 0000010 /* [XSI] X for group */
276 /* Read, write, execute/search by others */
277 #define S_IRWXO 0000007 /* [XSI] RWX mask for other */
278 #define S_IROTH 0000004 /* [XSI] R for other */
279 #define S_IWOTH 0000002 /* [XSI] W for other */
280 #define S_IXOTH 0000001 /* [XSI] X for other */
281
282 #define S_ISUID 0004000 /* [XSI] set user id on execution */
283 #define S_ISGID 0002000 /* [XSI] set group id on execution */
284 #define S_ISVTX 0001000 /* [XSI] directory restrcted delete */
285
286 #ifndef _POSIX_C_SOURCE
287 #define S_ISTXT S_ISVTX /* sticky bit: not supported */
288 #define S_IREAD S_IRUSR /* backward compatability */
289 #define S_IWRITE S_IWUSR /* backward compatability */
290 #define S_IEXEC S_IXUSR /* backward compatability */
291 #endif
292 #endif /* !S_IFMT */
293
294 #ifndef _POSIX_C_SOURCE
295 /* allocate flags (F_PREALLOCATE) */
296
297 #define F_ALLOCATECONTIG 0x00000002 /* allocate contigious space */
298 #define F_ALLOCATEALL 0x00000004 /* allocate all requested space or no space at all */
299
300 /* Position Modes (fst_posmode) for F_PREALLOCATE */
301
302 #define F_PEOFPOSMODE 3 /* Make it past all of the SEEK pos modes so that */
303 /* we can keep them in sync should we desire */
304 #define F_VOLPOSMODE 4 /* specify volume starting postion */
305 #endif /* _POSIX_C_SOURCE */
306
307 /*
308 * Advisory file segment locking data type -
309 * information passed to system by user
310 */
311 struct flock {
312 off_t l_start; /* starting offset */
313 off_t l_len; /* len = 0 means until end of file */
314 pid_t l_pid; /* lock owner */
315 short l_type; /* lock type: read/write, etc. */
316 short l_whence; /* type of l_start */
317 };
318
319
320 #ifndef _POSIX_C_SOURCE
321 /*
322 * advisory file read data type -
323 * information passed by user to system
324 */
325 struct radvisory {
326 off_t ra_offset;
327 int ra_count;
328 };
329
330
331 /* lock operations for flock(2) */
332 #define LOCK_SH 0x01 /* shared file lock */
333 #define LOCK_EX 0x02 /* exclusive file lock */
334 #define LOCK_NB 0x04 /* don't block when locking */
335 #define LOCK_UN 0x08 /* unlock file */
336
337 /* fstore_t type used by F_DEALLOCATE and F_PREALLOCATE commands */
338
339 typedef struct fstore {
340 unsigned int fst_flags; /* IN: flags word */
341 int fst_posmode; /* IN: indicates use of offset field */
342 off_t fst_offset; /* IN: start of the region */
343 off_t fst_length; /* IN: size of the region */
344 off_t fst_bytesalloc; /* OUT: number of bytes allocated */
345 } fstore_t;
346
347 /* fbootstraptransfer_t used by F_READBOOTSTRAP and F_WRITEBOOTSTRAP commands */
348
349 typedef struct fbootstraptransfer {
350 off_t fbt_offset; /* IN: offset to start read/write */
351 size_t fbt_length; /* IN: number of bytes to transfer */
352 void *fbt_buffer; /* IN: buffer to be read/written */
353 } fbootstraptransfer_t;
354
355
356 // LP64todo - should this move?
357 #ifdef KERNEL
358 /* LP64 version of fbootstraptransfer. all pointers
359 * grow when we're dealing with a 64-bit process.
360 * WARNING - keep in sync with fbootstraptransfer
361 */
362
363 #if __DARWIN_ALIGN_NATURAL
364 #pragma options align=natural
365 #endif
366
367 typedef struct user_fbootstraptransfer {
368 off_t fbt_offset; /* IN: offset to start read/write */
369 user_size_t fbt_length; /* IN: number of bytes to transfer */
370 user_addr_t fbt_buffer; /* IN: buffer to be read/written */
371 } user_fbootstraptransfer_t;
372
373 #if __DARWIN_ALIGN_NATURAL
374 #pragma options align=reset
375 #endif
376
377 #endif // KERNEL
378
379 /*
380 * For F_LOG2PHYS this information is passed back to user
381 * Currently only devoffset is returned - that is the VOP_BMAP
382 * result - the disk device address corresponding to the
383 * current file offset (likely set with an lseek).
384 *
385 * The flags could hold an indication of whether the # of
386 * contiguous bytes reflects the true extent length on disk,
387 * or is an advisory value that indicates there is at least that
388 * many bytes contiguous. For some filesystems it might be too
389 * inefficient to provide anything beyond the advisory value.
390 * Flags and contiguous bytes return values are not yet implemented.
391 * For them the fcntl will nedd to switch from using BMAP to CMAP
392 * and a per filesystem type flag will be needed to interpret the
393 * contiguous bytes count result from CMAP.
394 */
395 #if __DARWIN_ALIGN_POWER
396 #pragma options align=power
397 #endif
398
399 struct log2phys {
400 unsigned int l2p_flags; /* unused so far */
401 off_t l2p_contigbytes; /* unused so far */
402 off_t l2p_devoffset; /* bytes into device */
403 };
404
405 #if __DARWIN_ALIGN_POWER
406 #pragma options align=reset
407 #endif
408
409 #define O_POPUP 0x80000000 /* force window to popup on open */
410 #define O_ALERT 0x20000000 /* small, clean popup window */
411 #endif /* _POSIX_C_SOURCE */
412
413 #ifndef KERNEL
414
415 #ifndef _POSIX_C_SOURCE
416 #ifndef _FILESEC_T
417 struct _filesec;
418 typedef struct _filesec *filesec_t;
419 #define _FILESEC_T
420 #endif
421 typedef enum {
422 FILESEC_OWNER = 1,
423 FILESEC_GROUP = 2,
424 FILESEC_UUID = 3,
425 FILESEC_MODE = 4,
426 FILESEC_ACL = 5,
427 FILESEC_GRPUUID = 6,
428
429 /* XXX these are private to the implementation */
430 FILESEC_ACL_RAW = 100,
431 FILESEC_ACL_ALLOCSIZE = 101
432 } filesec_property_t;
433
434 /* XXX backwards compatibility */
435 #define FILESEC_GUID FILESEC_UUID
436 #endif /* _POSIX_C_SOURCE */
437
438 __BEGIN_DECLS
439 int open(const char *, int, ...);
440 int creat(const char *, mode_t);
441 int fcntl(int, int, ...);
442 #ifndef _POSIX_C_SOURCE
443 int openx_np(const char *, int, filesec_t);
444 int flock(int, int);
445 filesec_t filesec_init(void);
446 filesec_t filesec_dup(filesec_t);
447 void filesec_free(filesec_t);
448 int filesec_get_property(filesec_t, filesec_property_t, void *);
449 int filesec_set_property(filesec_t, filesec_property_t, const void *);
450 int filesec_unset_property(filesec_t, filesec_property_t);
451 int filesec_query_property(filesec_t, filesec_property_t, int *);
452 #define _FILESEC_UNSET_PROPERTY ((void *)0)
453 #define _FILESEC_REMOVE_ACL ((void *)1)
454 #endif /* !_POSIX_C_SOURCE */
455 __END_DECLS
456 #endif
457
458 #endif /* !_SYS_FCNTL_H_ */