11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1983, 1990, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * (c) UNIX System Laboratories, Inc. 51541Srgrimes * All or some portions of this file are derived from material licensed 61541Srgrimes * to the University of California by American Telephone and Telegraph 71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81541Srgrimes * the permission of UNIX System Laboratories, Inc. 91541Srgrimes * 101541Srgrimes * Redistribution and use in source and binary forms, with or without 111541Srgrimes * modification, are permitted provided that the following conditions 121541Srgrimes * are met: 131541Srgrimes * 1. Redistributions of source code must retain the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer. 151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer in the 171541Srgrimes * documentation and/or other materials provided with the distribution. 181541Srgrimes * 4. Neither the name of the University nor the names of its contributors 191541Srgrimes * may be used to endorse or promote products derived from this software 201541Srgrimes * without specific prior written permission. 211541Srgrimes * 221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321541Srgrimes * SUCH DAMAGE. 331541Srgrimes * 341541Srgrimes * @(#)fcntl.h 8.3 (Berkeley) 1/21/94 3550477Speter * $FreeBSD$ 361541Srgrimes */ 371541Srgrimes 381541Srgrimes#ifndef _SYS_FCNTL_H_ 391541Srgrimes#define _SYS_FCNTL_H_ 401541Srgrimes 411541Srgrimes/* 421541Srgrimes * This file includes the definitions for open and fcntl 431541Srgrimes * described by POSIX for <fcntl.h>; it also includes 441541Srgrimes * related kernel definitions. 451541Srgrimes */ 461541Srgrimes 47103506Smike#include <sys/cdefs.h> 48103506Smike#include <sys/_types.h> 49103506Smike 50103506Smike#ifndef _MODE_T_DECLARED 51103506Smiketypedef __mode_t mode_t; 52103506Smike#define _MODE_T_DECLARED 531541Srgrimes#endif 541541Srgrimes 55103506Smike#ifndef _OFF_T_DECLARED 56103506Smiketypedef __off_t off_t; 57103506Smike#define _OFF_T_DECLARED 58103506Smike#endif 59103506Smike 60103506Smike#ifndef _PID_T_DECLARED 61103506Smiketypedef __pid_t pid_t; 62103506Smike#define _PID_T_DECLARED 63103506Smike#endif 64103506Smike 651541Srgrimes/* 661541Srgrimes * File status flags: these are used by open(2), fcntl(2). 671541Srgrimes * They are also used (indirectly) in the kernel file structure f_flags, 681541Srgrimes * which is a superset of the open/fcntl flags. Open flags and f_flags 691541Srgrimes * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags). 701541Srgrimes * Open/fcntl flags begin with O_; kernel-internal flags begin with F. 711541Srgrimes */ 721541Srgrimes/* open-only flags */ 731541Srgrimes#define O_RDONLY 0x0000 /* open for reading only */ 741541Srgrimes#define O_WRONLY 0x0001 /* open for writing only */ 751541Srgrimes#define O_RDWR 0x0002 /* open for reading and writing */ 761541Srgrimes#define O_ACCMODE 0x0003 /* mask for above modes */ 771541Srgrimes 781541Srgrimes/* 791541Srgrimes * Kernel encoding of open mode; separate read and write bits that are 801541Srgrimes * independently testable: 1 greater than the above. 811541Srgrimes * 821541Srgrimes * XXX 8355205Speter * FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH, 841541Srgrimes * which was documented to use FREAD/FWRITE, continues to work. 851541Srgrimes */ 86103506Smike#if __BSD_VISIBLE 871541Srgrimes#define FREAD 0x0001 881541Srgrimes#define FWRITE 0x0002 891541Srgrimes#endif 901541Srgrimes#define O_NONBLOCK 0x0004 /* no delay */ 911541Srgrimes#define O_APPEND 0x0008 /* set append mode */ 92103506Smike#if __BSD_VISIBLE 931541Srgrimes#define O_SHLOCK 0x0010 /* open with shared file lock */ 941541Srgrimes#define O_EXLOCK 0x0020 /* open with exclusive file lock */ 951541Srgrimes#define O_ASYNC 0x0040 /* signal pgrp when data ready */ 961541Srgrimes#define O_FSYNC 0x0080 /* synchronous writes */ 97103506Smike#endif 98103506Smike#define O_SYNC 0x0080 /* POSIX synonym for O_FSYNC */ 99273840Sjilles#if __POSIX_VISIBLE >= 200809 10035082Speter#define O_NOFOLLOW 0x0100 /* don't follow symlinks */ 1011541Srgrimes#endif 10213765Smpp#define O_CREAT 0x0200 /* create if nonexistent */ 1031541Srgrimes#define O_TRUNC 0x0400 /* truncate to zero length */ 1041541Srgrimes#define O_EXCL 0x0800 /* error if already exists */ 10555205Speter#ifdef _KERNEL 1061541Srgrimes#define FHASLOCK 0x4000 /* descriptor holds advisory lock */ 1071541Srgrimes#endif 1081541Srgrimes 10920027Sbde/* Defined by POSIX 1003.1; BSD default, but must be distinct from O_RDONLY. */ 11020027Sbde#define O_NOCTTY 0x8000 /* don't assign controlling terminal */ 1111541Srgrimes 112103506Smike#if __BSD_VISIBLE 11377115Sdillon/* Attempt to bypass buffer cache */ 114238667Skib#define O_DIRECT 0x00010000 115103506Smike#endif 11677115Sdillon 117273840Sjilles#if __POSIX_VISIBLE >= 200809 118177783Skib#define O_DIRECTORY 0x00020000 /* Fail if not directory */ 119177783Skib#define O_EXEC 0x00040000 /* Open for execute only */ 120177783Skib#endif 121177783Skib#ifdef _KERNEL 122177783Skib#define FEXEC O_EXEC 123177783Skib#endif 124177783Skib 125219999Skib#if __POSIX_VISIBLE >= 200809 126189143Sed/* Defined by POSIX 1003.1-2008; BSD default, but reserve for future use. */ 127189143Sed#define O_TTY_INIT 0x00080000 /* Restore default termios attributes */ 128219999Skib 129219999Skib#define O_CLOEXEC 0x00100000 130189143Sed#endif 131189143Sed 132103506Smike/* 133103506Smike * XXX missing O_DSYNC, O_RSYNC. 134103506Smike */ 135103506Smike 13655205Speter#ifdef _KERNEL 1371541Srgrimes/* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */ 138254888Sjilles#define FFLAGS(oflags) ((oflags) & O_EXEC ? (oflags) : (oflags) + 1) 139254888Sjilles#define OFLAGS(fflags) ((fflags) & O_EXEC ? (fflags) : (fflags) - 1) 1401541Srgrimes 1411541Srgrimes/* bits to save after open */ 142177783Skib#define FMASK (FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK|O_DIRECT|FEXEC) 1431541Srgrimes/* bits settable by fcntl(F_SETFL, ...) */ 144197579Sdelphij#define FCNTLFLAGS (FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FRDAHEAD|O_DIRECT) 145175164Sjhb 146175164Sjhb#if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \ 147175164Sjhb defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) 148175164Sjhb/* 149175164Sjhb * Set by shm_open(3) in older libc's to get automatic MAP_ASYNC 150175164Sjhb * behavior for POSIX shared memory objects (which are otherwise 151175164Sjhb * implemented as plain files). 152175164Sjhb */ 153175164Sjhb#define FPOSIXSHM O_NOFOLLOW 154175164Sjhb#undef FCNTLFLAGS 155197579Sdelphij#define FCNTLFLAGS (FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FPOSIXSHM|FRDAHEAD| \ 156197579Sdelphij O_DIRECT) 1571541Srgrimes#endif 158175164Sjhb#endif 1591541Srgrimes 1601541Srgrimes/* 1611541Srgrimes * The O_* flags used to have only F* names, which were used in the kernel 16259496Swollman * and by fcntl. We retain the F* names for the kernel f_flag field 163103506Smike * and for backward compatibility for fcntl. These flags are deprecated. 1641541Srgrimes */ 165103506Smike#if __BSD_VISIBLE 1661541Srgrimes#define FAPPEND O_APPEND /* kernel/compat */ 1671541Srgrimes#define FASYNC O_ASYNC /* kernel/compat */ 1681541Srgrimes#define FFSYNC O_FSYNC /* kernel */ 1691541Srgrimes#define FNONBLOCK O_NONBLOCK /* kernel */ 1701541Srgrimes#define FNDELAY O_NONBLOCK /* compat */ 1711541Srgrimes#define O_NDELAY O_NONBLOCK /* compat */ 1721541Srgrimes#endif 1731541Srgrimes 1741541Srgrimes/* 17559496Swollman * We are out of bits in f_flag (which is a short). However, 17659496Swollman * the flag bits not set in FMASK are only meaningful in the 17759496Swollman * initial open syscall. Those bits can thus be given a 17859496Swollman * different meaning for fcntl(2). 17959496Swollman */ 180103506Smike#if __BSD_VISIBLE 181197579Sdelphij/* Read ahead */ 182197579Sdelphij#define FRDAHEAD O_CREAT 18359496Swollman#endif 18459496Swollman 185273840Sjilles#if __POSIX_VISIBLE >= 200809 18659496Swollman/* 187194618Skib * Magic value that specify the use of the current working directory 188194618Skib * to determine the target of relative file paths in the openat() and 189194618Skib * similar syscalls. 190194618Skib */ 191194618Skib#define AT_FDCWD -100 192194618Skib 193194618Skib/* 194194618Skib * Miscellaneous flags for the *at() syscalls. 195194618Skib */ 196194618Skib#define AT_EACCESS 0x100 /* Check access using effective user and group ID */ 197194618Skib#define AT_SYMLINK_NOFOLLOW 0x200 /* Do not follow symbolic links */ 198194618Skib#define AT_SYMLINK_FOLLOW 0x400 /* Follow symbolic link */ 199194618Skib#define AT_REMOVEDIR 0x800 /* Remove directory instead of file */ 200194618Skib#endif 201194618Skib 202194618Skib/* 2031541Srgrimes * Constants used for fcntl(2) 2041541Srgrimes */ 2051541Srgrimes 2061541Srgrimes/* command values */ 2071541Srgrimes#define F_DUPFD 0 /* duplicate file descriptor */ 2081541Srgrimes#define F_GETFD 1 /* get file descriptor flags */ 2091541Srgrimes#define F_SETFD 2 /* set file descriptor flags */ 2101541Srgrimes#define F_GETFL 3 /* get file status flags */ 2111541Srgrimes#define F_SETFL 4 /* set file status flags */ 212273840Sjilles#if __XSI_VISIBLE || __POSIX_VISIBLE >= 200112 2131541Srgrimes#define F_GETOWN 5 /* get SIGIO/SIGURG proc/pgrp */ 214238667Skib#define F_SETOWN 6 /* set SIGIO/SIGURG proc/pgrp */ 2151541Srgrimes#endif 216238667Skib#if __BSD_VISIBLE 217177633Sdfr#define F_OGETLK 7 /* get record locking information */ 218177633Sdfr#define F_OSETLK 8 /* set record locking information */ 219177633Sdfr#define F_OSETLKW 9 /* F_SETLK; wait if blocked */ 220176957Santoine#define F_DUP2FD 10 /* duplicate file descriptor to arg */ 221238667Skib#endif 222177633Sdfr#define F_GETLK 11 /* get record locking information */ 223177633Sdfr#define F_SETLK 12 /* set record locking information */ 224177633Sdfr#define F_SETLKW 13 /* F_SETLK; wait if blocked */ 225238667Skib#if __BSD_VISIBLE 226177633Sdfr#define F_SETLK_REMOTE 14 /* debugging support for remote locks */ 227197579Sdelphij#define F_READAHEAD 15 /* read ahead */ 228197579Sdelphij#define F_RDAHEAD 16 /* Darwin compatible read ahead */ 229238667Skib#endif 230273840Sjilles#if __POSIX_VISIBLE >= 200809 231238614Skib#define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */ 232238614Skib#endif 233238834Skib#if __BSD_VISIBLE 234238834Skib#define F_DUP2FD_CLOEXEC 18 /* Like F_DUP2FD, but FD_CLOEXEC is set */ 235238834Skib#endif 2361541Srgrimes 2371541Srgrimes/* file descriptor flags (F_GETFD, F_SETFD) */ 2381541Srgrimes#define FD_CLOEXEC 1 /* close-on-exec flag */ 2391541Srgrimes 2401541Srgrimes/* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */ 2411541Srgrimes#define F_RDLCK 1 /* shared or read lock */ 2421541Srgrimes#define F_UNLCK 2 /* unlock */ 2431541Srgrimes#define F_WRLCK 3 /* exclusive or write lock */ 244238667Skib#if __BSD_VISIBLE 245177633Sdfr#define F_UNLCKSYS 4 /* purge locks for a given system ID */ 246177633Sdfr#define F_CANCEL 5 /* cancel an async lock request */ 247238667Skib#endif 24855205Speter#ifdef _KERNEL 2491541Srgrimes#define F_WAIT 0x010 /* Wait until lock is granted */ 2501541Srgrimes#define F_FLOCK 0x020 /* Use flock(2) semantics for lock */ 2511541Srgrimes#define F_POSIX 0x040 /* Use POSIX semantics for lock */ 252177633Sdfr#define F_REMOTE 0x080 /* Lock owner is remote NFS client */ 253238667Skib#define F_NOINTR 0x100 /* Ignore signals when waiting */ 2541541Srgrimes#endif 2551541Srgrimes 2561541Srgrimes/* 2571541Srgrimes * Advisory file segment locking data type - 2581541Srgrimes * information passed to system by user 2591541Srgrimes */ 2601541Srgrimesstruct flock { 2611541Srgrimes off_t l_start; /* starting offset */ 2621541Srgrimes off_t l_len; /* len = 0 means until end of file */ 2631541Srgrimes pid_t l_pid; /* lock owner */ 2641541Srgrimes short l_type; /* lock type: read/write, etc. */ 2651541Srgrimes short l_whence; /* type of l_start */ 266177633Sdfr int l_sysid; /* remote system id or zero for local */ 2671541Srgrimes}; 2681541Srgrimes 269238667Skib#if __BSD_VISIBLE 270177633Sdfr/* 271177633Sdfr * Old advisory file segment locking data type, 272177633Sdfr * before adding l_sysid. 273177633Sdfr */ 274238667Skibstruct __oflock { 275177633Sdfr off_t l_start; /* starting offset */ 276177633Sdfr off_t l_len; /* len = 0 means until end of file */ 277177633Sdfr pid_t l_pid; /* lock owner */ 278177633Sdfr short l_type; /* lock type: read/write, etc. */ 279177633Sdfr short l_whence; /* type of l_start */ 280177633Sdfr}; 281238667Skib#endif 2821541Srgrimes 283103506Smike#if __BSD_VISIBLE 2841541Srgrimes/* lock operations for flock(2) */ 2851541Srgrimes#define LOCK_SH 0x01 /* shared file lock */ 2861541Srgrimes#define LOCK_EX 0x02 /* exclusive file lock */ 2871541Srgrimes#define LOCK_NB 0x04 /* don't block when locking */ 2881541Srgrimes#define LOCK_UN 0x08 /* unlock file */ 2891541Srgrimes#endif 2901541Srgrimes 291227070Sjhb#if __POSIX_VISIBLE >= 200112 292103506Smike/* 293227070Sjhb * Advice to posix_fadvise 294103506Smike */ 295227070Sjhb#define POSIX_FADV_NORMAL 0 /* no special treatment */ 296227070Sjhb#define POSIX_FADV_RANDOM 1 /* expect random page references */ 297227070Sjhb#define POSIX_FADV_SEQUENTIAL 2 /* expect sequential page references */ 298227070Sjhb#define POSIX_FADV_WILLNEED 3 /* will need these pages */ 299227070Sjhb#define POSIX_FADV_DONTNEED 4 /* dont need these pages */ 300227070Sjhb#define POSIX_FADV_NOREUSE 5 /* access data only once */ 301227070Sjhb#endif 3021541Srgrimes 30355205Speter#ifndef _KERNEL 3041541Srgrimes__BEGIN_DECLS 30592719Salfredint open(const char *, int, ...); 30692719Salfredint creat(const char *, mode_t); 30792719Salfredint fcntl(int, int, ...); 308226850Sjhb#if __BSD_VISIBLE 309226850Sjhbint flock(int, int); 310226850Sjhb#endif 311273840Sjilles#if __POSIX_VISIBLE >= 200809 312177791Skibint openat(int, const char *, int, ...); 313189353Sdas#endif 314273840Sjilles#if __POSIX_VISIBLE >= 200112 315227070Sjhbint posix_fadvise(int, off_t, off_t, int); 316220791Smdfint posix_fallocate(int, off_t, off_t); 317220791Smdf#endif 3181541Srgrimes__END_DECLS 3191541Srgrimes#endif 3201541Srgrimes 3211541Srgrimes#endif /* !_SYS_FCNTL_H_ */ 322