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: stable/11/sys/sys/fcntl.h 357706 2020-02-09 22:15:35Z kevans $
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 */
99264628Sjilles#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
117264628Sjilles#if __POSIX_VISIBLE >= 200809
118177783Skib#define	O_DIRECTORY	0x00020000	/* Fail if not directory */
119177783Skib#define	O_EXEC		0x00040000	/* Open for execute only */
120357706Skevans#define	O_SEARCH	O_EXEC
121177783Skib#endif
122177783Skib#ifdef	_KERNEL
123177783Skib#define	FEXEC		O_EXEC
124357706Skevans#define	FSEARCH		O_SEARCH
125177783Skib#endif
126177783Skib
127219999Skib#if __POSIX_VISIBLE >= 200809
128189143Sed/* Defined by POSIX 1003.1-2008; BSD default, but reserve for future use. */
129189143Sed#define	O_TTY_INIT	0x00080000	/* Restore default termios attributes */
130219999Skib
131219999Skib#define	O_CLOEXEC	0x00100000
132189143Sed#endif
133189143Sed
134281845Srodrigc#if __BSD_VISIBLE
135281845Srodrigc#define	O_VERIFY	0x00200000	/* open only after verification */
136281845Srodrigc#endif
137281845Srodrigc
138103506Smike/*
139103506Smike * XXX missing O_DSYNC, O_RSYNC.
140103506Smike */
141103506Smike
14255205Speter#ifdef _KERNEL
143292624Skib
144292624Skib/* Only for devfs d_close() flags. */
145292624Skib#define	FLASTCLOSE	O_DIRECTORY
146292624Skib#define	FREVOKE		O_VERIFY
147294205Skib/* Only for fo_close() from half-succeeded open */
148294205Skib#define	FOPENFAILED	O_TTY_INIT
149292624Skib
1501541Srgrimes/* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */
151254888Sjilles#define	FFLAGS(oflags)	((oflags) & O_EXEC ? (oflags) : (oflags) + 1)
152254888Sjilles#define	OFLAGS(fflags)	((fflags) & O_EXEC ? (fflags) : (fflags) - 1)
1531541Srgrimes
1541541Srgrimes/* bits to save after open */
155177783Skib#define	FMASK	(FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK|O_DIRECT|FEXEC)
1561541Srgrimes/* bits settable by fcntl(F_SETFL, ...) */
157197579Sdelphij#define	FCNTLFLAGS	(FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FRDAHEAD|O_DIRECT)
158175164Sjhb
159175164Sjhb#if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
160175164Sjhb    defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
161175164Sjhb/*
162175164Sjhb * Set by shm_open(3) in older libc's to get automatic MAP_ASYNC
163175164Sjhb * behavior for POSIX shared memory objects (which are otherwise
164175164Sjhb * implemented as plain files).
165175164Sjhb */
166175164Sjhb#define	FPOSIXSHM	O_NOFOLLOW
167175164Sjhb#undef FCNTLFLAGS
168197579Sdelphij#define	FCNTLFLAGS	(FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FPOSIXSHM|FRDAHEAD| \
169197579Sdelphij			 O_DIRECT)
1701541Srgrimes#endif
171175164Sjhb#endif
1721541Srgrimes
1731541Srgrimes/*
1741541Srgrimes * The O_* flags used to have only F* names, which were used in the kernel
17559496Swollman * and by fcntl.  We retain the F* names for the kernel f_flag field
176103506Smike * and for backward compatibility for fcntl.  These flags are deprecated.
1771541Srgrimes */
178103506Smike#if __BSD_VISIBLE
1791541Srgrimes#define	FAPPEND		O_APPEND	/* kernel/compat */
1801541Srgrimes#define	FASYNC		O_ASYNC		/* kernel/compat */
1811541Srgrimes#define	FFSYNC		O_FSYNC		/* kernel */
1821541Srgrimes#define	FNONBLOCK	O_NONBLOCK	/* kernel */
1831541Srgrimes#define	FNDELAY		O_NONBLOCK	/* compat */
1841541Srgrimes#define	O_NDELAY	O_NONBLOCK	/* compat */
1851541Srgrimes#endif
1861541Srgrimes
1871541Srgrimes/*
18859496Swollman * We are out of bits in f_flag (which is a short).  However,
18959496Swollman * the flag bits not set in FMASK are only meaningful in the
19059496Swollman * initial open syscall.  Those bits can thus be given a
19159496Swollman * different meaning for fcntl(2).
19259496Swollman */
193103506Smike#if __BSD_VISIBLE
194197579Sdelphij/* Read ahead */
195197579Sdelphij#define	FRDAHEAD	O_CREAT
19659496Swollman#endif
19759496Swollman
198264628Sjilles#if __POSIX_VISIBLE >= 200809
19959496Swollman/*
200194618Skib * Magic value that specify the use of the current working directory
201194618Skib * to determine the target of relative file paths in the openat() and
202194618Skib * similar syscalls.
203194618Skib */
204194618Skib#define	AT_FDCWD		-100
205194618Skib
206194618Skib/*
207194618Skib * Miscellaneous flags for the *at() syscalls.
208194618Skib */
209194618Skib#define	AT_EACCESS		0x100	/* Check access using effective user and group ID */
210194618Skib#define	AT_SYMLINK_NOFOLLOW	0x200   /* Do not follow symbolic links */
211194618Skib#define	AT_SYMLINK_FOLLOW	0x400	/* Follow symbolic link */
212194618Skib#define	AT_REMOVEDIR		0x800	/* Remove directory instead of file */
213194618Skib#endif
214194618Skib
215194618Skib/*
2161541Srgrimes * Constants used for fcntl(2)
2171541Srgrimes */
2181541Srgrimes
2191541Srgrimes/* command values */
2201541Srgrimes#define	F_DUPFD		0		/* duplicate file descriptor */
2211541Srgrimes#define	F_GETFD		1		/* get file descriptor flags */
2221541Srgrimes#define	F_SETFD		2		/* set file descriptor flags */
2231541Srgrimes#define	F_GETFL		3		/* get file status flags */
2241541Srgrimes#define	F_SETFL		4		/* set file status flags */
225264628Sjilles#if __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
2261541Srgrimes#define	F_GETOWN	5		/* get SIGIO/SIGURG proc/pgrp */
227238667Skib#define	F_SETOWN	6		/* set SIGIO/SIGURG proc/pgrp */
2281541Srgrimes#endif
229238667Skib#if __BSD_VISIBLE
230177633Sdfr#define	F_OGETLK	7		/* get record locking information */
231177633Sdfr#define	F_OSETLK	8		/* set record locking information */
232177633Sdfr#define	F_OSETLKW	9		/* F_SETLK; wait if blocked */
233176957Santoine#define	F_DUP2FD	10		/* duplicate file descriptor to arg */
234238667Skib#endif
235177633Sdfr#define	F_GETLK		11		/* get record locking information */
236177633Sdfr#define	F_SETLK		12		/* set record locking information */
237177633Sdfr#define	F_SETLKW	13		/* F_SETLK; wait if blocked */
238238667Skib#if __BSD_VISIBLE
239177633Sdfr#define	F_SETLK_REMOTE	14		/* debugging support for remote locks */
240197579Sdelphij#define	F_READAHEAD	15		/* read ahead */
241197579Sdelphij#define	F_RDAHEAD	16		/* Darwin compatible read ahead */
242238667Skib#endif
243264628Sjilles#if __POSIX_VISIBLE >= 200809
244238614Skib#define	F_DUPFD_CLOEXEC	17		/* Like F_DUPFD, but FD_CLOEXEC is set */
245238614Skib#endif
246238834Skib#if __BSD_VISIBLE
247238834Skib#define	F_DUP2FD_CLOEXEC 18		/* Like F_DUP2FD, but FD_CLOEXEC is set */
248238834Skib#endif
2491541Srgrimes
2501541Srgrimes/* file descriptor flags (F_GETFD, F_SETFD) */
2511541Srgrimes#define	FD_CLOEXEC	1		/* close-on-exec flag */
2521541Srgrimes
2531541Srgrimes/* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */
2541541Srgrimes#define	F_RDLCK		1		/* shared or read lock */
2551541Srgrimes#define	F_UNLCK		2		/* unlock */
2561541Srgrimes#define	F_WRLCK		3		/* exclusive or write lock */
257238667Skib#if __BSD_VISIBLE
258177633Sdfr#define	F_UNLCKSYS	4		/* purge locks for a given system ID */
259177633Sdfr#define	F_CANCEL	5		/* cancel an async lock request */
260238667Skib#endif
26155205Speter#ifdef _KERNEL
2621541Srgrimes#define	F_WAIT		0x010		/* Wait until lock is granted */
2631541Srgrimes#define	F_FLOCK		0x020	 	/* Use flock(2) semantics for lock */
2641541Srgrimes#define	F_POSIX		0x040	 	/* Use POSIX semantics for lock */
265177633Sdfr#define	F_REMOTE	0x080		/* Lock owner is remote NFS client */
266238667Skib#define	F_NOINTR	0x100		/* Ignore signals when waiting */
2671541Srgrimes#endif
2681541Srgrimes
2691541Srgrimes/*
2701541Srgrimes * Advisory file segment locking data type -
2711541Srgrimes * information passed to system by user
2721541Srgrimes */
2731541Srgrimesstruct flock {
2741541Srgrimes	off_t	l_start;	/* starting offset */
2751541Srgrimes	off_t	l_len;		/* len = 0 means until end of file */
2761541Srgrimes	pid_t	l_pid;		/* lock owner */
2771541Srgrimes	short	l_type;		/* lock type: read/write, etc. */
2781541Srgrimes	short	l_whence;	/* type of l_start */
279177633Sdfr	int	l_sysid;	/* remote system id or zero for local */
2801541Srgrimes};
2811541Srgrimes
282238667Skib#if __BSD_VISIBLE
283177633Sdfr/*
284177633Sdfr * Old advisory file segment locking data type,
285177633Sdfr * before adding l_sysid.
286177633Sdfr */
287238667Skibstruct __oflock {
288177633Sdfr	off_t	l_start;	/* starting offset */
289177633Sdfr	off_t	l_len;		/* len = 0 means until end of file */
290177633Sdfr	pid_t	l_pid;		/* lock owner */
291177633Sdfr	short	l_type;		/* lock type: read/write, etc. */
292177633Sdfr	short	l_whence;	/* type of l_start */
293177633Sdfr};
294238667Skib#endif
2951541Srgrimes
296103506Smike#if __BSD_VISIBLE
2971541Srgrimes/* lock operations for flock(2) */
2981541Srgrimes#define	LOCK_SH		0x01		/* shared file lock */
2991541Srgrimes#define	LOCK_EX		0x02		/* exclusive file lock */
3001541Srgrimes#define	LOCK_NB		0x04		/* don't block when locking */
3011541Srgrimes#define	LOCK_UN		0x08		/* unlock file */
3021541Srgrimes#endif
3031541Srgrimes
304227070Sjhb#if __POSIX_VISIBLE >= 200112
305103506Smike/*
306227070Sjhb * Advice to posix_fadvise
307103506Smike */
308227070Sjhb#define	POSIX_FADV_NORMAL	0	/* no special treatment */
309227070Sjhb#define	POSIX_FADV_RANDOM	1	/* expect random page references */
310227070Sjhb#define	POSIX_FADV_SEQUENTIAL	2	/* expect sequential page references */
311227070Sjhb#define	POSIX_FADV_WILLNEED	3	/* will need these pages */
312227070Sjhb#define	POSIX_FADV_DONTNEED	4	/* dont need these pages */
313227070Sjhb#define	POSIX_FADV_NOREUSE	5	/* access data only once */
314227070Sjhb#endif
3151541Srgrimes
31655205Speter#ifndef _KERNEL
3171541Srgrimes__BEGIN_DECLS
31892719Salfredint	open(const char *, int, ...);
31992719Salfredint	creat(const char *, mode_t);
32092719Salfredint	fcntl(int, int, ...);
321226850Sjhb#if __BSD_VISIBLE
322226850Sjhbint	flock(int, int);
323226850Sjhb#endif
324264628Sjilles#if __POSIX_VISIBLE >= 200809
325177791Skibint	openat(int, const char *, int, ...);
326189353Sdas#endif
327264628Sjilles#if __POSIX_VISIBLE >= 200112
328227070Sjhbint	posix_fadvise(int, off_t, off_t, int);
329220791Smdfint	posix_fallocate(int, off_t, off_t);
330220791Smdf#endif
3311541Srgrimes__END_DECLS
3321541Srgrimes#endif
3331541Srgrimes
3341541Srgrimes#endif /* !_SYS_FCNTL_H_ */
335