18fb65cergrimes/*- 24736ccfpfg * SPDX-License-Identifier: BSD-3-Clause 34736ccfpfg * 48fb65cergrimes * Copyright (c) 1983, 1990, 1993 58fb65cergrimes * The Regents of the University of California. All rights reserved. 68fb65cergrimes * (c) UNIX System Laboratories, Inc. 78fb65cergrimes * All or some portions of this file are derived from material licensed 88fb65cergrimes * to the University of California by American Telephone and Telegraph 98fb65cergrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 108fb65cergrimes * the permission of UNIX System Laboratories, Inc. 118fb65cergrimes * 128fb65cergrimes * Redistribution and use in source and binary forms, with or without 138fb65cergrimes * modification, are permitted provided that the following conditions 148fb65cergrimes * are met: 158fb65cergrimes * 1. Redistributions of source code must retain the above copyright 168fb65cergrimes * notice, this list of conditions and the following disclaimer. 178fb65cergrimes * 2. Redistributions in binary form must reproduce the above copyright 188fb65cergrimes * notice, this list of conditions and the following disclaimer in the 198fb65cergrimes * documentation and/or other materials provided with the distribution. 207e6cabdimp * 3. Neither the name of the University nor the names of its contributors 218fb65cergrimes * may be used to endorse or promote products derived from this software 228fb65cergrimes * without specific prior written permission. 238fb65cergrimes * 248fb65cergrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 258fb65cergrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 268fb65cergrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 278fb65cergrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 288fb65cergrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 298fb65cergrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 308fb65cergrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 318fb65cergrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 328fb65cergrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 338fb65cergrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 348fb65cergrimes * SUCH DAMAGE. 358fb65cergrimes * 368fb65cergrimes * @(#)fcntl.h 8.3 (Berkeley) 1/21/94 373b842d3peter * $FreeBSD$ 388fb65cergrimes */ 398fb65cergrimes 408fb65cergrimes#ifndef _SYS_FCNTL_H_ 418fb65cergrimes#define _SYS_FCNTL_H_ 428fb65cergrimes 438fb65cergrimes/* 448fb65cergrimes * This file includes the definitions for open and fcntl 458fb65cergrimes * described by POSIX for <fcntl.h>; it also includes 468fb65cergrimes * related kernel definitions. 478fb65cergrimes */ 488fb65cergrimes 4965b2813mike#include <sys/cdefs.h> 5065b2813mike#include <sys/_types.h> 5165b2813mike 5265b2813mike#ifndef _MODE_T_DECLARED 5365b2813miketypedef __mode_t mode_t; 5465b2813mike#define _MODE_T_DECLARED 5565b2813mike#endif 5665b2813mike 5765b2813mike#ifndef _OFF_T_DECLARED 5865b2813miketypedef __off_t off_t; 5965b2813mike#define _OFF_T_DECLARED 6065b2813mike#endif 6165b2813mike 6265b2813mike#ifndef _PID_T_DECLARED 6365b2813miketypedef __pid_t pid_t; 6465b2813mike#define _PID_T_DECLARED 658fb65cergrimes#endif 668fb65cergrimes 678fb65cergrimes/* 688fb65cergrimes * File status flags: these are used by open(2), fcntl(2). 698fb65cergrimes * They are also used (indirectly) in the kernel file structure f_flags, 708fb65cergrimes * which is a superset of the open/fcntl flags. Open flags and f_flags 718fb65cergrimes * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags). 728fb65cergrimes * Open/fcntl flags begin with O_; kernel-internal flags begin with F. 738fb65cergrimes */ 748fb65cergrimes/* open-only flags */ 758fb65cergrimes#define O_RDONLY 0x0000 /* open for reading only */ 768fb65cergrimes#define O_WRONLY 0x0001 /* open for writing only */ 778fb65cergrimes#define O_RDWR 0x0002 /* open for reading and writing */ 788fb65cergrimes#define O_ACCMODE 0x0003 /* mask for above modes */ 798fb65cergrimes 808fb65cergrimes/* 818fb65cergrimes * Kernel encoding of open mode; separate read and write bits that are 828fb65cergrimes * independently testable: 1 greater than the above. 838fb65cergrimes * 848fb65cergrimes * XXX 8515b9bcbpeter * FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH, 868fb65cergrimes * which was documented to use FREAD/FWRITE, continues to work. 878fb65cergrimes */ 8865b2813mike#if __BSD_VISIBLE 898fb65cergrimes#define FREAD 0x0001 908fb65cergrimes#define FWRITE 0x0002 918fb65cergrimes#endif 928fb65cergrimes#define O_NONBLOCK 0x0004 /* no delay */ 938fb65cergrimes#define O_APPEND 0x0008 /* set append mode */ 9465b2813mike#if __BSD_VISIBLE 958fb65cergrimes#define O_SHLOCK 0x0010 /* open with shared file lock */ 968fb65cergrimes#define O_EXLOCK 0x0020 /* open with exclusive file lock */ 978fb65cergrimes#define O_ASYNC 0x0040 /* signal pgrp when data ready */ 988fb65cergrimes#define O_FSYNC 0x0080 /* synchronous writes */ 9965b2813mike#endif 10065b2813mike#define O_SYNC 0x0080 /* POSIX synonym for O_FSYNC */ 1011cb4e21jilles#if __POSIX_VISIBLE >= 200809 102d5ab1c3peter#define O_NOFOLLOW 0x0100 /* don't follow symlinks */ 1038fb65cergrimes#endif 104f3dd75ampp#define O_CREAT 0x0200 /* create if nonexistent */ 1058fb65cergrimes#define O_TRUNC 0x0400 /* truncate to zero length */ 1068fb65cergrimes#define O_EXCL 0x0800 /* error if already exists */ 10715b9bcbpeter#ifdef _KERNEL 1088fb65cergrimes#define FHASLOCK 0x4000 /* descriptor holds advisory lock */ 1098fb65cergrimes#endif 1108fb65cergrimes 111623ea90bde/* Defined by POSIX 1003.1; BSD default, but must be distinct from O_RDONLY. */ 112623ea90bde#define O_NOCTTY 0x8000 /* don't assign controlling terminal */ 1138fb65cergrimes 11465b2813mike#if __BSD_VISIBLE 115a179ee0dillon/* Attempt to bypass buffer cache */ 116e04825ckib#define O_DIRECT 0x00010000 11765b2813mike#endif 11865b2813mike 1191cb4e21jilles#if __POSIX_VISIBLE >= 200809 1209ab7de8kib#define O_DIRECTORY 0x00020000 /* Fail if not directory */ 1219ab7de8kib#define O_EXEC 0x00040000 /* Open for execute only */ 12211e74d9kevans#define O_SEARCH O_EXEC 1239ab7de8kib#endif 1249ab7de8kib#ifdef _KERNEL 1259ab7de8kib#define FEXEC O_EXEC 12611e74d9kevans#define FSEARCH O_SEARCH 1279ab7de8kib#endif 1289ab7de8kib 129f056985ed#if __POSIX_VISIBLE >= 200809 130fc2bd01kib/* Defined by POSIX 1003.1-2008; BSD default, but reserve for future use. */ 131f056985ed#define O_TTY_INIT 0x00080000 /* Restore default termios attributes */ 132fc2bd01kib 133fc2bd01kib#define O_CLOEXEC 0x00100000 134f056985ed#endif 135b5fb244rodrigc 136b5fb244rodrigc#if __BSD_VISIBLE 137b5fb244rodrigc#define O_VERIFY 0x00200000 /* open only after verification */ 138de9d57ckib#define O_BENEATH 0x00400000 /* Fail if not under cwd */ 139e511050kib#define O_RESOLVE_BENEATH 0x00800000 /* As O_BENEATH, but do not allow 140e511050kib resolve to walk out of cwd even to 141e511050kib return back */ 142b5fb244rodrigc#endif 143f056985ed 14465b2813mike/* 14565b2813mike * XXX missing O_DSYNC, O_RSYNC. 14665b2813mike */ 147a179ee0dillon 14815b9bcbpeter#ifdef _KERNEL 14976abdf8kib 15076abdf8kib/* Only for devfs d_close() flags. */ 15176abdf8kib#define FLASTCLOSE O_DIRECTORY 15276abdf8kib#define FREVOKE O_VERIFY 1537d0828ckib/* Only for fo_close() from half-succeeded open */ 1547d0828ckib#define FOPENFAILED O_TTY_INIT 15576abdf8kib 1568fb65cergrimes/* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */ 15703a89a5jilles#define FFLAGS(oflags) ((oflags) & O_EXEC ? (oflags) : (oflags) + 1) 15803a89a5jilles#define OFLAGS(fflags) ((fflags) & O_EXEC ? (fflags) : (fflags) - 1) 1598fb65cergrimes 1608fb65cergrimes/* bits to save after open */ 1619ab7de8kib#define FMASK (FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK|O_DIRECT|FEXEC) 1628fb65cergrimes/* bits settable by fcntl(F_SETFL, ...) */ 16379f2f8cdelphij#define FCNTLFLAGS (FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FRDAHEAD|O_DIRECT) 1648cd9437jhb 1658cd9437jhb#if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \ 1668cd9437jhb defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) 1678cd9437jhb/* 1688cd9437jhb * Set by shm_open(3) in older libc's to get automatic MAP_ASYNC 1698cd9437jhb * behavior for POSIX shared memory objects (which are otherwise 1708cd9437jhb * implemented as plain files). 1718cd9437jhb */ 1728cd9437jhb#define FPOSIXSHM O_NOFOLLOW 1738cd9437jhb#undef FCNTLFLAGS 17479f2f8cdelphij#define FCNTLFLAGS (FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FPOSIXSHM|FRDAHEAD| \ 17579f2f8cdelphij O_DIRECT) 1768fb65cergrimes#endif 1778cd9437jhb#endif 1788fb65cergrimes 1798fb65cergrimes/* 1808fb65cergrimes * The O_* flags used to have only F* names, which were used in the kernel 18116604abwollman * and by fcntl. We retain the F* names for the kernel f_flag field 18265b2813mike * and for backward compatibility for fcntl. These flags are deprecated. 1838fb65cergrimes */ 18465b2813mike#if __BSD_VISIBLE 1858fb65cergrimes#define FAPPEND O_APPEND /* kernel/compat */ 1868fb65cergrimes#define FASYNC O_ASYNC /* kernel/compat */ 1878fb65cergrimes#define FFSYNC O_FSYNC /* kernel */ 1888fb65cergrimes#define FNONBLOCK O_NONBLOCK /* kernel */ 1898fb65cergrimes#define FNDELAY O_NONBLOCK /* compat */ 1908fb65cergrimes#define O_NDELAY O_NONBLOCK /* compat */ 1918fb65cergrimes#endif 1928fb65cergrimes 1938fb65cergrimes/* 19416604abwollman * We are out of bits in f_flag (which is a short). However, 19516604abwollman * the flag bits not set in FMASK are only meaningful in the 19616604abwollman * initial open syscall. Those bits can thus be given a 19716604abwollman * different meaning for fcntl(2). 19816604abwollman */ 19965b2813mike#if __BSD_VISIBLE 20079f2f8cdelphij/* Read ahead */ 20179f2f8cdelphij#define FRDAHEAD O_CREAT 20216604abwollman#endif 20316604abwollman 2041cb4e21jilles#if __POSIX_VISIBLE >= 200809 2051a759a3kib/* 2061a759a3kib * Magic value that specify the use of the current working directory 2071a759a3kib * to determine the target of relative file paths in the openat() and 2081a759a3kib * similar syscalls. 2091a759a3kib */ 2101a759a3kib#define AT_FDCWD -100 2111a759a3kib 2121a759a3kib/* 2131a759a3kib * Miscellaneous flags for the *at() syscalls. 2141a759a3kib */ 215de9d57ckib#define AT_EACCESS 0x0100 /* Check access using effective user 216de9d57ckib and group ID */ 217de9d57ckib#define AT_SYMLINK_NOFOLLOW 0x0200 /* Do not follow symbolic links */ 218de9d57ckib#define AT_SYMLINK_FOLLOW 0x0400 /* Follow symbolic link */ 219de9d57ckib#define AT_REMOVEDIR 0x0800 /* Remove directory instead of file */ 220de9d57ckib#define AT_BENEATH 0x1000 /* Fail if not under dirfd */ 221e511050kib#define AT_RESOLVE_BENEATH 0x2000 /* As AT_BENEATH, but do not allow 222e511050kib resolve to walk out of dirfd even 223e511050kib to return back */ 2241a759a3kib#endif 2251a759a3kib 22616604abwollman/* 2278fb65cergrimes * Constants used for fcntl(2) 2288fb65cergrimes */ 2298fb65cergrimes 2308fb65cergrimes/* command values */ 2318fb65cergrimes#define F_DUPFD 0 /* duplicate file descriptor */ 2328fb65cergrimes#define F_GETFD 1 /* get file descriptor flags */ 2338fb65cergrimes#define F_SETFD 2 /* set file descriptor flags */ 2348fb65cergrimes#define F_GETFL 3 /* get file status flags */ 2358fb65cergrimes#define F_SETFL 4 /* set file status flags */ 2361cb4e21jilles#if __XSI_VISIBLE || __POSIX_VISIBLE >= 200112 2378fb65cergrimes#define F_GETOWN 5 /* get SIGIO/SIGURG proc/pgrp */ 238e04825ckib#define F_SETOWN 6 /* set SIGIO/SIGURG proc/pgrp */ 2398fb65cergrimes#endif 240e04825ckib#if __BSD_VISIBLE 24179d2dfddfr#define F_OGETLK 7 /* get record locking information */ 24279d2dfddfr#define F_OSETLK 8 /* set record locking information */ 24379d2dfddfr#define F_OSETLKW 9 /* F_SETLK; wait if blocked */ 244514f31fantoine#define F_DUP2FD 10 /* duplicate file descriptor to arg */ 245e04825ckib#endif 24679d2dfddfr#define F_GETLK 11 /* get record locking information */ 24779d2dfddfr#define F_SETLK 12 /* set record locking information */ 24879d2dfddfr#define F_SETLKW 13 /* F_SETLK; wait if blocked */ 249e04825ckib#if __BSD_VISIBLE 25079d2dfddfr#define F_SETLK_REMOTE 14 /* debugging support for remote locks */ 25179f2f8cdelphij#define F_READAHEAD 15 /* read ahead */ 25279f2f8cdelphij#define F_RDAHEAD 16 /* Darwin compatible read ahead */ 253e04825ckib#endif 2541cb4e21jilles#if __POSIX_VISIBLE >= 200809 255fb0ee76kib#define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */ 256fb0ee76kib#endif 2575a2e169kib#if __BSD_VISIBLE 2585a2e169kib#define F_DUP2FD_CLOEXEC 18 /* Like F_DUP2FD, but FD_CLOEXEC is set */ 25913d4dfekevans#define F_ADD_SEALS 19 26013d4dfekevans#define F_GET_SEALS 20 261fa60d0fmjg#define F_ISUNIONSTACK 21 /* Kludge for libc, don't use it. */ 26213d4dfekevans 26313d4dfekevans/* Seals (F_ADD_SEALS, F_GET_SEALS). */ 26413d4dfekevans#define F_SEAL_SEAL 0x0001 /* Prevent adding sealings */ 26513d4dfekevans#define F_SEAL_SHRINK 0x0002 /* May not shrink */ 26613d4dfekevans#define F_SEAL_GROW 0x0004 /* May not grow */ 26713d4dfekevans#define F_SEAL_WRITE 0x0008 /* May not write */ 26813d4dfekevans#endif /* __BSD_VISIBLE */ 2698fb65cergrimes 2708fb65cergrimes/* file descriptor flags (F_GETFD, F_SETFD) */ 2718fb65cergrimes#define FD_CLOEXEC 1 /* close-on-exec flag */ 2728fb65cergrimes 2738fb65cergrimes/* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */ 2748fb65cergrimes#define F_RDLCK 1 /* shared or read lock */ 2758fb65cergrimes#define F_UNLCK 2 /* unlock */ 2768fb65cergrimes#define F_WRLCK 3 /* exclusive or write lock */ 277e04825ckib#if __BSD_VISIBLE 27879d2dfddfr#define F_UNLCKSYS 4 /* purge locks for a given system ID */ 27979d2dfddfr#define F_CANCEL 5 /* cancel an async lock request */ 280e04825ckib#endif 28115b9bcbpeter#ifdef _KERNEL 2828fb65cergrimes#define F_WAIT 0x010 /* Wait until lock is granted */ 2838fb65cergrimes#define F_FLOCK 0x020 /* Use flock(2) semantics for lock */ 2848fb65cergrimes#define F_POSIX 0x040 /* Use POSIX semantics for lock */ 28579d2dfddfr#define F_REMOTE 0x080 /* Lock owner is remote NFS client */ 286e04825ckib#define F_NOINTR 0x100 /* Ignore signals when waiting */ 2878fb65cergrimes#endif 2888fb65cergrimes 2898fb65cergrimes/* 2908fb65cergrimes * Advisory file segment locking data type - 2918fb65cergrimes * information passed to system by user 2928fb65cergrimes */ 2938fb65cergrimesstruct flock { 2948fb65cergrimes off_t l_start; /* starting offset */ 2958fb65cergrimes off_t l_len; /* len = 0 means until end of file */ 2968fb65cergrimes pid_t l_pid; /* lock owner */ 2978fb65cergrimes short l_type; /* lock type: read/write, etc. */ 2988fb65cergrimes short l_whence; /* type of l_start */ 29979d2dfddfr int l_sysid; /* remote system id or zero for local */ 30079d2dfddfr}; 30179d2dfddfr 302e04825ckib#if __BSD_VISIBLE 30379d2dfddfr/* 30479d2dfddfr * Old advisory file segment locking data type, 30579d2dfddfr * before adding l_sysid. 30679d2dfddfr */ 307e04825ckibstruct __oflock { 30879d2dfddfr off_t l_start; /* starting offset */ 30979d2dfddfr off_t l_len; /* len = 0 means until end of file */ 31079d2dfddfr pid_t l_pid; /* lock owner */ 31179d2dfddfr short l_type; /* lock type: read/write, etc. */ 31279d2dfddfr short l_whence; /* type of l_start */ 3138fb65cergrimes}; 314e04825ckib#endif 3158fb65cergrimes 31665b2813mike#if __BSD_VISIBLE 3178fb65cergrimes/* lock operations for flock(2) */ 3188fb65cergrimes#define LOCK_SH 0x01 /* shared file lock */ 3198fb65cergrimes#define LOCK_EX 0x02 /* exclusive file lock */ 3208fb65cergrimes#define LOCK_NB 0x04 /* don't block when locking */ 3218fb65cergrimes#define LOCK_UN 0x08 /* unlock file */ 3228fb65cergrimes#endif 3238fb65cergrimes 32478c0751jhb#if __POSIX_VISIBLE >= 200112 32565b2813mike/* 32678c0751jhb * Advice to posix_fadvise 32765b2813mike */ 32878c0751jhb#define POSIX_FADV_NORMAL 0 /* no special treatment */ 32978c0751jhb#define POSIX_FADV_RANDOM 1 /* expect random page references */ 33078c0751jhb#define POSIX_FADV_SEQUENTIAL 2 /* expect sequential page references */ 33178c0751jhb#define POSIX_FADV_WILLNEED 3 /* will need these pages */ 33278c0751jhb#define POSIX_FADV_DONTNEED 4 /* dont need these pages */ 33378c0751jhb#define POSIX_FADV_NOREUSE 5 /* access data only once */ 33478c0751jhb#endif 3358fb65cergrimes 33620d273boshogbo#ifdef __BSD_VISIBLE 33720d273boshogbo/* 33820d273boshogbo * Magic value that specify that corresponding file descriptor to filename 33920d273boshogbo * is unknown and sanitary check should be omitted in the funlinkat() and 34020d273boshogbo * similar syscalls. 34120d273boshogbo */ 34220d273boshogbo#define FD_NONE -200 34320d273boshogbo#endif 34420d273boshogbo 34515b9bcbpeter#ifndef _KERNEL 3468fb65cergrimes__BEGIN_DECLS 3473b2d03balfredint open(const char *, int, ...); 3483b2d03balfredint creat(const char *, mode_t); 3493b2d03balfredint fcntl(int, int, ...); 350a6c5138jhb#if __BSD_VISIBLE 351a6c5138jhbint flock(int, int); 352a6c5138jhb#endif 3531cb4e21jilles#if __POSIX_VISIBLE >= 200809 3542ad0eb2kibint openat(int, const char *, int, ...); 35540cb57edas#endif 3561cb4e21jilles#if __POSIX_VISIBLE >= 200112 35778c0751jhbint posix_fadvise(int, off_t, off_t, int); 3589c9a32dmdfint posix_fallocate(int, off_t, off_t); 3599c9a32dmdf#endif 3608fb65cergrimes__END_DECLS 3618fb65cergrimes#endif 3628fb65cergrimes 3638fb65cergrimes#endif /* !_SYS_FCNTL_H_ */ 364