| 1 | /*	$NetBSD: fcntl.h,v 1.57.2.1 2026/06/16 09:06:50 martin Exp $	*/ |
| 2 | |
| 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 | *	@(#)fcntl.h	8.3 (Berkeley) 1/21/94 |
| 37 | */ |
| 38 | |
| 39 | #ifndef _SYS_FCNTL_H_ |
| 40 | #define	_SYS_FCNTL_H_ |
| 41 | |
| 42 | /* |
| 43 | * This file includes the definitions for open and fcntl |
| 44 | * described by POSIX for <fcntl.h>; it also includes |
| 45 | * related kernel definitions. |
| 46 | */ |
| 47 | |
| 48 | #ifndef _KERNEL |
| 49 | #include <sys/featuretest.h> |
| 50 | #include <sys/types.h> |
| 51 | #if defined(_XOPEN_SOURCE) |
| 52 | #include <sys/stat.h> |
| 53 | #endif /* _XOPEN_SOURCE */ |
| 54 | #endif /* !_KERNEL */ |
| 55 | |
| 56 | /* |
| 57 | * File status flags: these are used by open(2), fcntl(2). |
| 58 | * They are also used (indirectly) in the kernel file structure f_flags, |
| 59 | * which is a superset of the open/fcntl flags. Open flags and f_flags |
| 60 | * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags). |
| 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 | /* |
| 70 | * Kernel encoding of open mode; separate read and write bits that are |
| 71 | * independently testable: 1 greater than the above. |
| 72 | * |
| 73 | * XXX |
| 74 | * FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH, |
| 75 | * which was documented to use FREAD/FWRITE, continues to work. |
| 76 | */ |
| 77 | #if defined(_NETBSD_SOURCE) |
| 78 | #define	FREAD		0x00000001 |
| 79 | #define	FWRITE		0x00000002 |
| 80 | #endif |
| 81 | #define	O_NONBLOCK	0x00000004	/* no delay */ |
| 82 | #define	O_APPEND	0x00000008	/* set append mode */ |
| 83 | #if defined(_NETBSD_SOURCE) |
| 84 | #define	O_SHLOCK	0x00000010	/* open with shared file lock */ |
| 85 | #define	O_EXLOCK	0x00000020	/* open with exclusive file lock */ |
| 86 | #define	O_ASYNC		0x00000040	/* signal pgrp when data ready */ |
| 87 | #endif |
| 88 | #if (_POSIX_C_SOURCE - 0) >= 199309L || \ |
| 89 | (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \ |
| 90 | (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE) |
| 91 | #define	O_SYNC		0x00000080	/* synchronous writes */ |
| 92 | #endif |
| 93 | #if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE) |
| 94 | #define	O_NOFOLLOW	0x00000100	/* don't follow symlinks on the last */ |
| 95 | 					/* path component */ |
| 96 | #endif |
| 97 | #define	O_CREAT		0x00000200	/* create if nonexistent */ |
| 98 | #define	O_TRUNC		0x00000400	/* truncate to zero length */ |
| 99 | #define	O_EXCL		0x00000800	/* error if already exists */ |
| 100 | |
| 101 | /* defined by POSIX 1003.1; BSD default, but required to be bitwise distinct */ |
| 102 | #define	O_NOCTTY	0x00008000	/* don't assign controlling terminal */ |
| 103 | |
| 104 | #if (_POSIX_C_SOURCE - 0) >= 199309L || (_XOPEN_SOURCE - 0) >= 500 || \ |
| 105 | defined(_NETBSD_SOURCE) |
| 106 | #define	O_DSYNC		0x00010000	/* write: I/O data completion */ |
| 107 | #define	O_RSYNC		0x00020000	/* read: I/O completion as for write */ |
| 108 | #endif |
| 109 | |
| 110 | #if defined(_NETBSD_SOURCE) |
| 111 | #define	O_ALT_IO	0x00040000	/* use alternate i/o semantics */ |
| 112 | #define	O_DIRECT	0x00080000	/* direct I/O hint */ |
| 113 | #endif |
| 114 | |
| 115 | #define	O_DIRECTORY	0x00200000	/* fail if not a directory */ |
| 116 | #define	O_CLOEXEC	0x00400000	/* set close on exec */ |
| 117 | #if (_POSIX_C_SOURCE - 0) >= 200809L || (_XOPEN_SOURCE - 0 >= 700) || \ |
| 118 | defined(_NETBSD_SOURCE) |
| 119 | #define	O_SEARCH	0x00800000	/* skip search permission checks */ |
| 120 | #endif |
| 121 | #if defined(_NETBSD_SOURCE) |
| 122 | #define	O_NOSIGPIPE	0x01000000	/* don't deliver sigpipe */ |
| 123 | #define	O_REGULAR	0x02000000	/* fail if not a regular file */ |
| 124 | #endif |
| 125 | #if (_POSIX_C_SOURCE - 0) >= 200809L || (_XOPEN_SOURCE - 0 >= 700) || \ |
| 126 | defined(_NETBSD_SOURCE) |
| 127 | #define	O_EXEC		0x04000000	/* open for executing only */ |
| 128 | #endif |
| 129 | #if (_POSIX_C_SOURCE - 0) >= 202405L || (_XOPEN_SOURCE - 0 >= 800) || \ |
| 130 | 	defined(_NETBSD_SOURCE) |
| 131 | #define	O_CLOFORK	0x08000000	/* set close on fork */ |
| 132 | #endif |
| 133 | |
| 134 | #ifdef _KERNEL |
| 135 | /* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */ |
| 136 | #define	FFLAGS(oflags)	((oflags) + 1) |
| 137 | #define	OFLAGS(fflags)	((fflags) - 1) |
| 138 | |
| 139 | /* all bits settable during open(2) */ |
| 140 | #define	O_MASK		(O_ACCMODE|O_NONBLOCK|O_APPEND|O_SHLOCK|O_EXLOCK|\ |
| 141 | 			 O_ASYNC|O_SYNC|O_CREAT|O_TRUNC|O_EXCL|O_DSYNC|\ |
| 142 | 			 O_RSYNC|O_NOCTTY|O_ALT_IO|O_NOFOLLOW|O_DIRECT|\ |
| 143 | 			 O_DIRECTORY|O_CLOEXEC|O_CLOFORK|O_NOSIGPIPE|\ |
| 144 | 			 O_REGULAR|O_EXEC) |
| 145 | |
| 146 | #define	FEXEC		O_EXEC |
| 147 | #define	FMARK		0x00001000	/* mark during gc() */ |
| 148 | #define	FDEFER		0x00002000	/* defer for next gc pass */ |
| 149 | #define	FHASLOCK	0x00004000	/* descriptor holds advisory lock */ |
| 150 | #define	FSCAN		0x00100000	/* scan during gc passes */ |
| 151 | #define	FSILENT		0x40000000	/* suppress kernel error messages */ |
| 152 | #define	FKIOCTL		0x80000000	/* kernel originated ioctl */ |
| 153 | /* bits settable by fcntl(F_SETFL, ...) */ |
| 154 | #define	FCNTLFLAGS	(FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FDSYNC|FRSYNC|FALTIO|\ |
| 155 | 			 FDIRECT|FNOSIGPIPE) |
| 156 | /* bits to save after open(2) */ |
| 157 | #define	FMASK		(FREAD|FWRITE|FCNTLFLAGS|FEXEC) |
| 158 | #endif /* _KERNEL */ |
| 159 | |
| 160 | /* |
| 161 | * The O_* flags used to have only F* names, which were used in the kernel |
| 162 | * and by fcntl. We retain the F* names for the kernel f_flags field |
| 163 | * and for backward compatibility for fcntl. |
| 164 | */ |
| 165 | #if defined(_NETBSD_SOURCE) |
| 166 | #define	FAPPEND		O_APPEND	/* kernel/compat */ |
| 167 | #define	FASYNC		O_ASYNC		/* kernel/compat */ |
| 168 | #define	O_FSYNC		O_SYNC		/* compat */ |
| 169 | #define	FNDELAY		O_NONBLOCK	/* compat */ |
| 170 | #define	O_NDELAY	O_NONBLOCK	/* compat */ |
| 171 | #endif |
| 172 | #if defined(_KERNEL) |
| 173 | #define	FNOSIGPIPE	O_NOSIGPIPE	/* kernel */ |
| 174 | #define	FNONBLOCK	O_NONBLOCK	/* kernel */ |
| 175 | #define	FFSYNC		O_SYNC		/* kernel */ |
| 176 | #define	FDSYNC		O_DSYNC		/* kernel */ |
| 177 | #define	FRSYNC		O_RSYNC		/* kernel */ |
| 178 | #define	FALTIO		O_ALT_IO	/* kernel */ |
| 179 | #define	FDIRECT		O_DIRECT	/* kernel */ |
| 180 | #endif |
| 181 | |
| 182 | /* |
| 183 | * Constants used for fcntl(2) |
| 184 | */ |
| 185 | |
| 186 | /* command values */ |
| 187 | #define	F_DUPFD		0		/* duplicate file descriptor */ |
| 188 | #define	F_GETFD		1		/* get file descriptor flags */ |
| 189 | #define	F_SETFD		2		/* set file descriptor flags */ |
| 190 | #define	F_GETFL		3		/* get file status flags */ |
| 191 | #define	F_SETFL		4		/* set file status flags */ |
| 192 | #if (_POSIX_C_SOURCE - 0) >= 200112L || (_XOPEN_SOURCE - 0) >= 500 || \ |
| 193 | defined(_NETBSD_SOURCE) |
| 194 | #define	F_GETOWN	5		/* get SIGIO/SIGURG proc/pgrp */ |
| 195 | #define	F_SETOWN	6		/* set SIGIO/SIGURG proc/pgrp */ |
| 196 | #endif |
| 197 | #define	F_GETLK		7		/* get record locking information */ |
| 198 | #define	F_SETLK		8		/* set record locking information */ |
| 199 | #define	F_SETLKW	9		/* F_SETLK; wait if blocked */ |
| 200 | #if defined(_NETBSD_SOURCE) |
| 201 | #define	F_CLOSEM	10		/* close all fds >= to the one given */ |
| 202 | #define	F_MAXFD		11		/* return the max open fd */ |
| 203 | #endif |
| 204 | #if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE) |
| 205 | #define	F_DUPFD_CLOEXEC	12		/* close on exec duplicated fd */ |
| 206 | #endif |
| 207 | #if defined(_NETBSD_SOURCE) |
| 208 | #define	F_GETNOSIGPIPE	13		/* get SIGPIPE disposition */ |
| 209 | #define	F_SETNOSIGPIPE	14		/* set SIGPIPE disposition */ |
| 210 | #define	F_GETPATH	15		/* get pathname associated with fd */ |
| 211 | #define	F_ADD_SEALS	16		/* set seals */ |
| 212 | #define	F_GET_SEALS	17		/* get seals */ |
| 213 | #endif |
| 214 | #if (_POSIX_C_SOURCE - 0) >= 202405L || (_XOPEN_SOURCE - 0 >= 800) || \ |
| 215 | 	defined(_NETBSD_SOURCE) |
| 216 | #define	F_DUPFD_CLOFORK	18		/* close on fork duplicated fd */ |
| 217 | #endif |
| 218 | #if defined(_NETBSD_SOURCE) |
| 219 | #define	F_DUPFD_CLOBOTH	19		/* close on exec/fork duplicated fd */ |
| 220 | #endif |
| 221 | |
| 222 | /* file descriptor flags (F_GETFD, F_SETFD) */ |
| 223 | #define	FD_CLOEXEC	1		/* close-on-exec flag */ |
| 224 | #define	FD_CLOFORK	2		/* close-on-fork 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 | /* types of seals (F_ADD_SEALS, F_GET_SEALS) */ |
| 237 | #if defined(_NETBSD_SOURCE) |
| 238 | #define	F_SEAL_SEAL		0x0001	/* prevent further seals from being set */ |
| 239 | #define	F_SEAL_SHRINK		0x0002	/* prevent file from shrinking */ |
| 240 | #define	F_SEAL_GROW		0x0004	/* prevent file from growing */ |
| 241 | #define	F_SEAL_WRITE		0x0008	/* prevent writes */ |
| 242 | #define	F_SEAL_FUTURE_WRITE	0x0010	/* prevent future writes while mapped */ |
| 243 | #endif |
| 244 | |
| 245 | /* Constants for fcntl's passed to the underlying fs - like ioctl's. */ |
| 246 | #if defined(_NETBSD_SOURCE) |
| 247 | #define	F_PARAM_MASK	0xfff |
| 248 | #define	F_PARAM_LEN(x)	(((x) >> 16) & F_PARAM_MASK) |
| 249 | #define	F_PARAM_MAX	4095 |
| 250 | #define	F_FSCTL		(int)0x80000000	/* This fcntl goes to the fs */ |
| 251 | #define	F_FSVOID	(int)0x40000000	/* no parameters */ |
| 252 | #define	F_FSOUT		(int)0x20000000	/* copy out parameter */ |
| 253 | #define	F_FSIN		(int)0x10000000	/* copy in parameter */ |
| 254 | #define	F_FSINOUT	(F_FSIN | F_FSOUT) |
| 255 | #define	F_FSDIRMASK	(int)0x70000000	/* mask for IN/OUT/VOID */ |
| 256 | #define	F_FSPRIV	(int)0x00008000	/* command is fs-specific */ |
| 257 | |
| 258 | /* |
| 259 | * Define command macros for operations which, if implemented, must be |
| 260 | * the same for all fs's. |
| 261 | */ |
| 262 | #define	_FCN(inout, num, len) \ |
| 263 | 		(F_FSCTL | inout | ((len & F_PARAM_MASK) << 16) | (num)) |
| 264 | #define	_FCNO(c)	_FCN(F_FSVOID,	(c), 0) |
| 265 | #define	_FCNR(c, t)	_FCN(F_FSIN,	(c), (int)sizeof(t)) |
| 266 | #define	_FCNW(c, t)	_FCN(F_FSOUT,	(c), (int)sizeof(t)) |
| 267 | #define	_FCNRW(c, t)	_FCN(F_FSINOUT,	(c), (int)sizeof(t)) |
| 268 | |
| 269 | /* |
| 270 | * Define command macros for fs-specific commands. |
| 271 | */ |
| 272 | #define	_FCN_FSPRIV(inout, fs, num, len) \ |
| 273 | 	(F_FSCTL | F_FSPRIV | inout | ((len & F_PARAM_MASK) << 16) |	\ |
| 274 | 	 (fs) << 8 | (num)) |
| 275 | #define	_FCNO_FSPRIV(f, c)	_FCN_FSPRIV(F_FSVOID, (f), (c), 0) |
| 276 | #define	_FCNR_FSPRIV(f, c, t)	_FCN_FSPRIV(F_FSIN, (f), (c), (int)sizeof(t)) |
| 277 | #define	_FCNW_FSPRIV(f, c, t)	_FCN_FSPRIV(F_FSOUT, (f), (c), (int)sizeof(t)) |
| 278 | #define	_FCNRW_FSPRIV(f, c, t)	_FCN_FSPRIV(F_FSINOUT, (f), (c), (int)sizeof(t)) |
| 279 | |
| 280 | #endif /* _NETBSD_SOURCE */ |
| 281 | |
| 282 | /* |
| 283 | * Advisory file segment locking data type - |
| 284 | * information passed to system by user |
| 285 | */ |
| 286 | struct flock { |
| 287 | 	off_t	l_start;	/* starting offset */ |
| 288 | 	off_t	l_len;		/* len = 0 means until end of file */ |
| 289 | 	pid_t	l_pid;		/* lock owner */ |
| 290 | 	short	l_type;		/* lock type: read/write, etc. */ |
| 291 | 	short	l_whence;	/* type of l_start */ |
| 292 | }; |
| 293 | |
| 294 | |
| 295 | #if defined(_NETBSD_SOURCE) |
| 296 | /* lock operations for flock(2) */ |
| 297 | #define	LOCK_SH		0x01		/* shared file lock */ |
| 298 | #define	LOCK_EX		0x02		/* exclusive file lock */ |
| 299 | #define	LOCK_NB		0x04		/* don't block when locking */ |
| 300 | #define	LOCK_UN		0x08		/* unlock file */ |
| 301 | #endif |
| 302 | |
| 303 | /* Always ensure that these are consistent with <stdio.h> and <unistd.h>! */ |
| 304 | #ifndef	SEEK_SET |
| 305 | #define	SEEK_SET	0	/* set file offset to offset */ |
| 306 | #endif |
| 307 | #ifndef	SEEK_CUR |
| 308 | #define	SEEK_CUR	1	/* set file offset to current plus offset */ |
| 309 | #endif |
| 310 | #ifndef	SEEK_END |
| 311 | #define	SEEK_END	2	/* set file offset to EOF plus offset */ |
| 312 | #endif |
| 313 | |
| 314 | /* |
| 315 | * posix_advise advisories. |
| 316 | */ |
| 317 | |
| 318 | #define	POSIX_FADV_NORMAL	0	/* default advice / no advice */ |
| 319 | #define	POSIX_FADV_RANDOM	1	/* random access */ |
| 320 | #define	POSIX_FADV_SEQUENTIAL	2	/* sequential access(lower to higher) */ |
| 321 | #define	POSIX_FADV_WILLNEED	3	/* be needed in near future */ |
| 322 | #define	POSIX_FADV_DONTNEED	4	/* not be needed in near future */ |
| 323 | #define	POSIX_FADV_NOREUSE	5	/* be accessed once */ |
| 324 | |
| 325 | /* |
| 326 | * Constants for X/Open Extended API set 2 (a.k.a. C063) |
| 327 | */ |
| 328 | #if (_POSIX_C_SOURCE - 0) >= 200809L || (_XOPEN_SOURCE - 0 >= 700) || \ |
| 329 | defined(_NETBSD_SOURCE) |
| 330 | #define	AT_FDCWD		-100	/* Use cwd for relative link target */ |
| 331 | #define	AT_EACCESS		0x100	/* Use euig/egid for access checks */ |
| 332 | #define	AT_SYMLINK_NOFOLLOW	0x200	/* Do not follow symlinks */ |
| 333 | #define	AT_SYMLINK_FOLLOW	0x400	/* Follow symlinks */ |
| 334 | #define	AT_REMOVEDIR		0x800	/* Remove directory only */ |
| 335 | #endif |
| 336 | |
| 337 | |
| 338 | #ifndef _KERNEL |
| 339 | #include <sys/cdefs.h> |
| 340 | |
| 341 | __BEGIN_DECLS |
| 342 | int	open(const char *, int, ...); |
| 343 | int	creat(const char *, mode_t); |
| 344 | int	fcntl(int, int, ...); |
| 345 | #if defined(_NETBSD_SOURCE) |
| 346 | int	flock(int, int); |
| 347 | #endif /* _NETBSD_SOURCE */ |
| 348 | int	posix_fadvise(int, off_t, off_t, int); |
| 349 | |
| 350 | /* |
| 351 | * The Open Group Base Specifications, Issue 6; IEEE Std 1003.1-2001 (POSIX) |
| 352 | */ |
| 353 | #if (_POSIX_C_SOURCE - 0) >= 200112L || (_XOPEN_SOURCE - 0) >= 600 || \ |
| 354 | defined(_NETBSD_SOURCE) |
| 355 | int	 posix_fallocate(int, off_t, off_t); |
| 356 | #endif |
| 357 | |
| 358 | /* |
| 359 | * X/Open Extended API set 2 (a.k.a. C063) |
| 360 | */ |
| 361 | #if (_POSIX_C_SOURCE - 0) >= 200809L || (_XOPEN_SOURCE - 0 >= 700) || \ |
| 362 | defined(_NETBSD_SOURCE) |
| 363 | int	openat(int, const char *, int, ...); |
| 364 | #endif |
| 365 | |
| 366 | __END_DECLS |
| 367 | #endif /* !_KERNEL */ |
| 368 | |
| 369 | #endif /* !_SYS_FCNTL_H_ */ |