xref: /freebsd/sys/sys/fcntl.h (revision 971f738679e2e57114366c0f69af99e07150f1ff)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef _SYS_FCNTL_H_
38 #define	_SYS_FCNTL_H_
39 
40 /*
41  * This file includes the definitions for open and fcntl
42  * described by POSIX for <fcntl.h>; it also includes
43  * related kernel definitions.
44  */
45 
46 #include <sys/cdefs.h>
47 #include <sys/_types.h>
48 
49 #ifndef _MODE_T_DECLARED
50 typedef	__mode_t	mode_t;
51 #define	_MODE_T_DECLARED
52 #endif
53 
54 #ifndef _OFF_T_DECLARED
55 typedef	__off_t		off_t;
56 #define	_OFF_T_DECLARED
57 #endif
58 
59 #ifndef _PID_T_DECLARED
60 typedef	__pid_t		pid_t;
61 #define	_PID_T_DECLARED
62 #endif
63 
64 /*
65  * File status flags: these are used by open(2), fcntl(2).
66  * They are also used (indirectly) in the kernel file structure f_flags,
67  * which is a superset of the open/fcntl flags.  Open flags and f_flags
68  * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags).
69  * Open/fcntl flags begin with O_; kernel-internal flags begin with F.
70  */
71 /* open-only flags */
72 #define	O_RDONLY	0x0000		/* open for reading only */
73 #define	O_WRONLY	0x0001		/* open for writing only */
74 #define	O_RDWR		0x0002		/* open for reading and writing */
75 #define	O_ACCMODE	0x0003		/* mask for above modes */
76 
77 /*
78  * Kernel encoding of open mode; separate read and write bits that are
79  * independently testable: 1 greater than the above.
80  *
81  * XXX
82  * FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH,
83  * which was documented to use FREAD/FWRITE, continues to work.
84  */
85 #if __BSD_VISIBLE
86 #define	FREAD		0x0001
87 #define	FWRITE		0x0002
88 #endif
89 #define	O_NONBLOCK	0x0004		/* no delay */
90 #define	O_APPEND	0x0008		/* set append mode */
91 #if __BSD_VISIBLE
92 #define	O_SHLOCK	0x0010		/* open with shared file lock */
93 #define	O_EXLOCK	0x0020		/* open with exclusive file lock */
94 #define	O_ASYNC		0x0040		/* signal pgrp when data ready */
95 #define	O_FSYNC		0x0080		/* synchronous writes */
96 #endif
97 #define	O_SYNC		0x0080		/* POSIX synonym for O_FSYNC */
98 #if __POSIX_VISIBLE >= 200809
99 #define	O_NOFOLLOW	0x0100		/* don't follow symlinks */
100 #endif
101 #define	O_CREAT		0x0200		/* create if nonexistent */
102 #define	O_TRUNC		0x0400		/* truncate to zero length */
103 #define	O_EXCL		0x0800		/* error if already exists */
104 #ifdef _KERNEL
105 #define	FHASLOCK	0x4000		/* descriptor holds advisory lock */
106 #endif
107 
108 /* Defined by POSIX 1003.1; BSD default, but must be distinct from O_RDONLY. */
109 #define	O_NOCTTY	0x8000		/* don't assign controlling terminal */
110 
111 #if __BSD_VISIBLE
112 /* Attempt to bypass buffer cache */
113 #define	O_DIRECT	0x00010000
114 #endif
115 
116 #if __POSIX_VISIBLE >= 200809
117 #define	O_DIRECTORY	0x00020000	/* Fail if not directory */
118 #define	O_EXEC		0x00040000	/* Open for execute only */
119 #define	O_SEARCH	O_EXEC
120 #endif
121 #ifdef	_KERNEL
122 #define	FEXEC		O_EXEC
123 #define	FSEARCH		O_SEARCH
124 #endif
125 
126 #if __POSIX_VISIBLE >= 200809
127 /* Defined by POSIX 1003.1-2008; BSD default, but reserve for future use. */
128 #define	O_TTY_INIT	0x00080000	/* Restore default termios attributes */
129 
130 #define	O_CLOEXEC	0x00100000
131 #endif
132 
133 #if __BSD_VISIBLE
134 #define	O_VERIFY	0x00200000	/* open only after verification */
135 #define O_PATH		0x00400000	/* fd is only a path */
136 #define	O_RESOLVE_BENEATH 0x00800000	/* Do not allow name resolution to walk
137 					   out of cwd */
138 #endif
139 
140 #define	O_DSYNC		0x01000000	/* POSIX data sync */
141 #if __BSD_VISIBLE
142 #define	O_EMPTY_PATH	0x02000000
143 #define	O_NAMEDATTR	0x04000000	/* NFSv4 named attributes */
144 #define	O_XATTR		O_NAMEDATTR	/* Solaris compatibility */
145 #endif
146 
147 #if __POSIX_VISIBLE >= 202405
148 #define	O_CLOFORK	0x08000000
149 #endif
150 
151 /*
152  * !!! DANGER !!!
153  *
154  * There are very few bits left for O_* flags.  Every bit we consume for
155  * local features is one bit we can't use for future source compatibility
156  * with other operating systems.
157  *
158  * All additions should be coordinated with srcmgr@.
159  */
160 
161 /*
162  * XXX missing O_RSYNC.
163  */
164 
165 #ifdef _KERNEL
166 
167 /* Only for devfs d_close() flags. */
168 #define	FLASTCLOSE	O_DIRECTORY
169 #define	FREVOKE		O_VERIFY
170 /* Only for fo_close() from half-succeeded open */
171 #define	FOPENFAILED	O_TTY_INIT
172 /* Only for O_PATH files which passed ACCESS FREAD check on open */
173 #define	FKQALLOWED	O_RESOLVE_BENEATH
174 
175 /* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */
176 #define	FFLAGS(oflags)	((oflags) & O_EXEC ? (oflags) : (oflags) + 1)
177 #define	OFLAGS(fflags)	\
178     (((fflags) & (O_EXEC | O_PATH)) != 0 ? (fflags) : (fflags) - 1)
179 
180 /* bits to save after open */
181 #define	FMASK	(FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FDSYNC|FNONBLOCK| \
182 		 O_DIRECT|FEXEC|O_PATH)
183 /* bits settable by fcntl(F_SETFL, ...) */
184 #define	FCNTLFLAGS	(FAPPEND|FASYNC|FFSYNC|FDSYNC|FNONBLOCK|FRDAHEAD|O_DIRECT)
185 
186 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
187     defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
188 /*
189  * Set by shm_open(3) in older libc's to get automatic MAP_ASYNC
190  * behavior for POSIX shared memory objects (which are otherwise
191  * implemented as plain files).
192  */
193 #define	FPOSIXSHM	O_NOFOLLOW
194 #undef FCNTLFLAGS
195 #define	FCNTLFLAGS	(FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FPOSIXSHM|FRDAHEAD| \
196 			 O_DIRECT)
197 #endif
198 #endif
199 
200 /*
201  * The O_* flags used to have only F* names, which were used in the kernel
202  * and by fcntl.  We retain the F* names for the kernel f_flag field
203  * and for backward compatibility for fcntl.  These flags are deprecated.
204  */
205 #if __BSD_VISIBLE
206 #define	FAPPEND		O_APPEND	/* kernel/compat */
207 #define	FASYNC		O_ASYNC		/* kernel/compat */
208 #define	FFSYNC		O_FSYNC		/* kernel */
209 #define	FDSYNC		O_DSYNC		/* kernel */
210 #define	FNONBLOCK	O_NONBLOCK	/* kernel */
211 #define	FNDELAY		O_NONBLOCK	/* compat */
212 #define	O_NDELAY	O_NONBLOCK	/* compat */
213 #endif
214 
215 /*
216  * Historically, we ran out of bits in f_flag (which was once a short).
217  * However, the flag bits not set in FMASK are only meaningful in the
218  * initial open syscall.  Those bits were thus given a
219  * different meaning for fcntl(2).
220  */
221 #if __BSD_VISIBLE
222 /* Read ahead */
223 #define	FRDAHEAD	O_CREAT
224 #endif
225 
226 #if __POSIX_VISIBLE >= 200809
227 /*
228  * Magic value that specify the use of the current working directory
229  * to determine the target of relative file paths in the openat() and
230  * similar syscalls.
231  */
232 #define	AT_FDCWD		-100
233 
234 /*
235  * Miscellaneous flags for the *at() syscalls.
236  */
237 #define	AT_EACCESS		0x0100	/* Check access using effective user
238 					   and group ID */
239 #define	AT_SYMLINK_NOFOLLOW	0x0200	/* Do not follow symbolic links */
240 #define	AT_SYMLINK_FOLLOW	0x0400	/* Follow symbolic link */
241 #define	AT_REMOVEDIR		0x0800	/* Remove directory instead of file */
242 #endif	/* __POSIX_VISIBLE >= 200809 */
243 #if __BSD_VISIBLE
244 /* #define AT_UNUSED1		0x1000 *//* Was AT_BENEATH */
245 #define	AT_RESOLVE_BENEATH	0x2000	/* Do not allow name resolution
246 					   to walk out of dirfd */
247 #define	AT_EMPTY_PATH		0x4000	/* Operate on dirfd if path is empty */
248 #endif	/* __BSD_VISIBLE */
249 
250 /*
251  * Constants used for fcntl(2)
252  */
253 
254 /* command values */
255 #define	F_DUPFD		0		/* duplicate file descriptor */
256 #define	F_GETFD		1		/* get file descriptor flags */
257 #define	F_SETFD		2		/* set file descriptor flags */
258 #define	F_GETFL		3		/* get file status flags */
259 #define	F_SETFL		4		/* set file status flags */
260 #if __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
261 #define	F_GETOWN	5		/* get SIGIO/SIGURG proc/pgrp */
262 #define	F_SETOWN	6		/* set SIGIO/SIGURG proc/pgrp */
263 #endif
264 #if __BSD_VISIBLE
265 #define	F_OGETLK	7		/* get record locking information */
266 #define	F_OSETLK	8		/* set record locking information */
267 #define	F_OSETLKW	9		/* F_SETLK; wait if blocked */
268 #define	F_DUP2FD	10		/* duplicate file descriptor to arg */
269 #endif
270 #define	F_GETLK		11		/* get record locking information */
271 #define	F_SETLK		12		/* set record locking information */
272 #define	F_SETLKW	13		/* F_SETLK; wait if blocked */
273 #if __BSD_VISIBLE
274 #define	F_SETLK_REMOTE	14		/* debugging support for remote locks */
275 #define	F_READAHEAD	15		/* read ahead */
276 #define	F_RDAHEAD	16		/* Darwin compatible read ahead */
277 #endif
278 #if __POSIX_VISIBLE >= 200809
279 #define	F_DUPFD_CLOEXEC	17		/* Like F_DUPFD, but FD_CLOEXEC is set */
280 #endif
281 #if __BSD_VISIBLE
282 #define	F_DUP2FD_CLOEXEC 18		/* Like F_DUP2FD, but FD_CLOEXEC is set */
283 #define	F_ADD_SEALS	19
284 #define	F_GET_SEALS	20
285 #define	F_ISUNIONSTACK	21		/* Kludge for libc, don't use it. */
286 #define	F_KINFO		22		/* Return kinfo_file for this fd */
287 #endif	/* __BSD_VISIBLE */
288 
289 #if __POSIX_VISIBLE >= 202405
290 #define	F_DUPFD_CLOFORK	23		/* Like F_DUPFD, but FD_CLOFORK is set */
291 #endif
292 
293 #if __BSD_VISIBLE
294 #define F_DUP3FD	24		/* Used with dup3() */
295 
296 #define F_DUP3FD_SHIFT	16		/* Shift used for F_DUP3FD */
297 
298 /* Seals (F_ADD_SEALS, F_GET_SEALS). */
299 #define	F_SEAL_SEAL	0x0001		/* Prevent adding sealings */
300 #define	F_SEAL_SHRINK	0x0002		/* May not shrink */
301 #define	F_SEAL_GROW	0x0004		/* May not grow */
302 #define	F_SEAL_WRITE	0x0008		/* May not write */
303 #endif	/* __BSD_VISIBLE */
304 
305 /* file descriptor flags (F_GETFD, F_SETFD) */
306 #define	FD_CLOEXEC	1		/* close-on-exec flag */
307 #define	FD_RESOLVE_BENEATH 2		/* all lookups relative to fd have
308 					   O_RESOLVE_BENEATH semantics */
309 #if __POSIX_VISIBLE >= 202405
310 #define	FD_CLOFORK	4		/* close-on-fork flag */
311 #endif
312 
313 /* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */
314 #define	F_RDLCK		1		/* shared or read lock */
315 #define	F_UNLCK		2		/* unlock */
316 #define	F_WRLCK		3		/* exclusive or write lock */
317 #if __BSD_VISIBLE
318 #define	F_UNLCKSYS	4		/* purge locks for a given system ID */
319 #define	F_CANCEL	5		/* cancel an async lock request */
320 #endif
321 #ifdef _KERNEL
322 #define	F_WAIT		0x010		/* Wait until lock is granted */
323 #define	F_FLOCK		0x020	 	/* Use flock(2) semantics for lock */
324 #define	F_POSIX		0x040	 	/* Use POSIX semantics for lock */
325 #define	F_REMOTE	0x080		/* Lock owner is remote NFS client */
326 #define	F_NOINTR	0x100		/* Ignore signals when waiting */
327 #define	F_FIRSTOPEN	0x200		/* First right to advlock file */
328 #endif
329 
330 /*
331  * Advisory file segment locking data type -
332  * information passed to system by user
333  */
334 struct flock {
335 	off_t	l_start;	/* starting offset */
336 	off_t	l_len;		/* len = 0 means until end of file */
337 	pid_t	l_pid;		/* lock owner */
338 	short	l_type;		/* lock type: read/write, etc. */
339 	short	l_whence;	/* type of l_start */
340 	int	l_sysid;	/* remote system id or zero for local */
341 };
342 
343 #if __BSD_VISIBLE
344 /*
345  * Old advisory file segment locking data type,
346  * before adding l_sysid.
347  */
348 struct __oflock {
349 	off_t	l_start;	/* starting offset */
350 	off_t	l_len;		/* len = 0 means until end of file */
351 	pid_t	l_pid;		/* lock owner */
352 	short	l_type;		/* lock type: read/write, etc. */
353 	short	l_whence;	/* type of l_start */
354 };
355 
356 /*
357  * Space control offset/length description
358  */
359 struct spacectl_range {
360 	off_t	r_offset;	/* starting offset */
361 	off_t	r_len;	/* length */
362 };
363 #endif
364 
365 #if __BSD_VISIBLE
366 /* lock operations for flock(2) */
367 #define	LOCK_SH		0x01		/* shared file lock */
368 #define	LOCK_EX		0x02		/* exclusive file lock */
369 #define	LOCK_NB		0x04		/* don't block when locking */
370 #define	LOCK_UN		0x08		/* unlock file */
371 #endif
372 
373 #if __POSIX_VISIBLE >= 200112
374 /*
375  * Advice to posix_fadvise
376  */
377 #define	POSIX_FADV_NORMAL	0	/* no special treatment */
378 #define	POSIX_FADV_RANDOM	1	/* expect random page references */
379 #define	POSIX_FADV_SEQUENTIAL	2	/* expect sequential page references */
380 #define	POSIX_FADV_WILLNEED	3	/* will need these pages */
381 #define	POSIX_FADV_DONTNEED	4	/* dont need these pages */
382 #define	POSIX_FADV_NOREUSE	5	/* access data only once */
383 #endif
384 
385 #ifdef __BSD_VISIBLE
386 /*
387  * Magic value that specify that corresponding file descriptor to filename
388  * is unknown and sanitary check should be omitted in the funlinkat() and
389  * similar syscalls.
390  */
391 #define	FD_NONE			-200
392 
393 /*
394  * Commands for fspacectl(2)
395  */
396 #define SPACECTL_DEALLOC	1	/* deallocate space */
397 
398 /*
399  * fspacectl(2) flags
400  */
401 #define SPACECTL_F_SUPPORTED	0
402 #endif
403 
404 #ifndef _KERNEL
405 __BEGIN_DECLS
406 int	open(const char *, int, ...);
407 int	creat(const char *, mode_t);
408 int	fcntl(int, int, ...);
409 #if __BSD_VISIBLE
410 int	flock(int, int);
411 int	fspacectl(int, int, const struct spacectl_range *, int,
412 	    struct spacectl_range *);
413 #endif
414 #if __POSIX_VISIBLE >= 200809
415 int	openat(int, const char *, int, ...);
416 #endif
417 #if __POSIX_VISIBLE >= 200112
418 int	posix_fadvise(int, off_t, off_t, int);
419 int	posix_fallocate(int, off_t, off_t);
420 #endif
421 __END_DECLS
422 #endif
423 
424 #endif /* !_SYS_FCNTL_H_ */
425