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 */
99103506Smike#if __BSD_VISIBLE
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
117177783Skib/* Defined by POSIX Extended API Set Part 2 */
118177783Skib#if __BSD_VISIBLE
119177783Skib#define	O_DIRECTORY	0x00020000	/* Fail if not directory */
120177783Skib#define	O_EXEC		0x00040000	/* Open for execute only */
121177783Skib#endif
122177783Skib#ifdef	_KERNEL
123177783Skib#define	FEXEC		O_EXEC
124177783Skib#endif
125177783Skib
126219999Skib#if __POSIX_VISIBLE >= 200809
127189143Sed/* Defined by POSIX 1003.1-2008; BSD default, but reserve for future use. */
128189143Sed#define	O_TTY_INIT	0x00080000	/* Restore default termios attributes */
129219999Skib
130219999Skib#define	O_CLOEXEC	0x00100000
131189143Sed#endif
132189143Sed
133103506Smike/*
134103506Smike * XXX missing O_DSYNC, O_RSYNC.
135103506Smike */
136103506Smike
13755205Speter#ifdef _KERNEL
1381541Srgrimes/* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */
139254888Sjilles#define	FFLAGS(oflags)	((oflags) & O_EXEC ? (oflags) : (oflags) + 1)
140254888Sjilles#define	OFLAGS(fflags)	((fflags) & O_EXEC ? (fflags) : (fflags) - 1)
1411541Srgrimes
1421541Srgrimes/* bits to save after open */
143177783Skib#define	FMASK	(FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK|O_DIRECT|FEXEC)
1441541Srgrimes/* bits settable by fcntl(F_SETFL, ...) */
145197579Sdelphij#define	FCNTLFLAGS	(FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FRDAHEAD|O_DIRECT)
146175164Sjhb
147175164Sjhb#if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
148175164Sjhb    defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
149175164Sjhb/*
150175164Sjhb * Set by shm_open(3) in older libc's to get automatic MAP_ASYNC
151175164Sjhb * behavior for POSIX shared memory objects (which are otherwise
152175164Sjhb * implemented as plain files).
153175164Sjhb */
154175164Sjhb#define	FPOSIXSHM	O_NOFOLLOW
155175164Sjhb#undef FCNTLFLAGS
156197579Sdelphij#define	FCNTLFLAGS	(FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FPOSIXSHM|FRDAHEAD| \
157197579Sdelphij			 O_DIRECT)
1581541Srgrimes#endif
159175164Sjhb#endif
1601541Srgrimes
1611541Srgrimes/*
1621541Srgrimes * The O_* flags used to have only F* names, which were used in the kernel
16359496Swollman * and by fcntl.  We retain the F* names for the kernel f_flag field
164103506Smike * and for backward compatibility for fcntl.  These flags are deprecated.
1651541Srgrimes */
166103506Smike#if __BSD_VISIBLE
1671541Srgrimes#define	FAPPEND		O_APPEND	/* kernel/compat */
1681541Srgrimes#define	FASYNC		O_ASYNC		/* kernel/compat */
1691541Srgrimes#define	FFSYNC		O_FSYNC		/* kernel */
1701541Srgrimes#define	FNONBLOCK	O_NONBLOCK	/* kernel */
1711541Srgrimes#define	FNDELAY		O_NONBLOCK	/* compat */
1721541Srgrimes#define	O_NDELAY	O_NONBLOCK	/* compat */
1731541Srgrimes#endif
1741541Srgrimes
1751541Srgrimes/*
17659496Swollman * We are out of bits in f_flag (which is a short).  However,
17759496Swollman * the flag bits not set in FMASK are only meaningful in the
17859496Swollman * initial open syscall.  Those bits can thus be given a
17959496Swollman * different meaning for fcntl(2).
18059496Swollman */
181103506Smike#if __BSD_VISIBLE
182197579Sdelphij/* Read ahead */
183197579Sdelphij#define	FRDAHEAD	O_CREAT
18459496Swollman#endif
18559496Swollman
186194618Skib/* Defined by POSIX Extended API Set Part 2 */
187194618Skib#if __BSD_VISIBLE
18859496Swollman/*
189194618Skib * Magic value that specify the use of the current working directory
190194618Skib * to determine the target of relative file paths in the openat() and
191194618Skib * similar syscalls.
192194618Skib */
193194618Skib#define	AT_FDCWD		-100
194194618Skib
195194618Skib/*
196194618Skib * Miscellaneous flags for the *at() syscalls.
197194618Skib */
198194618Skib#define	AT_EACCESS		0x100	/* Check access using effective user and group ID */
199194618Skib#define	AT_SYMLINK_NOFOLLOW	0x200   /* Do not follow symbolic links */
200194618Skib#define	AT_SYMLINK_FOLLOW	0x400	/* Follow symbolic link */
201194618Skib#define	AT_REMOVEDIR		0x800	/* Remove directory instead of file */
202194618Skib#endif
203194618Skib
204194618Skib/*
2051541Srgrimes * Constants used for fcntl(2)
2061541Srgrimes */
2071541Srgrimes
2081541Srgrimes/* command values */
2091541Srgrimes#define	F_DUPFD		0		/* duplicate file descriptor */
2101541Srgrimes#define	F_GETFD		1		/* get file descriptor flags */
2111541Srgrimes#define	F_SETFD		2		/* set file descriptor flags */
2121541Srgrimes#define	F_GETFL		3		/* get file status flags */
2131541Srgrimes#define	F_SETFL		4		/* set file status flags */
214103506Smike#if __BSD_VISIBLE || __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
2151541Srgrimes#define	F_GETOWN	5		/* get SIGIO/SIGURG proc/pgrp */
216238667Skib#define	F_SETOWN	6		/* set SIGIO/SIGURG proc/pgrp */
2171541Srgrimes#endif
218238667Skib#if __BSD_VISIBLE
219177633Sdfr#define	F_OGETLK	7		/* get record locking information */
220177633Sdfr#define	F_OSETLK	8		/* set record locking information */
221177633Sdfr#define	F_OSETLKW	9		/* F_SETLK; wait if blocked */
222176957Santoine#define	F_DUP2FD	10		/* duplicate file descriptor to arg */
223238667Skib#endif
224177633Sdfr#define	F_GETLK		11		/* get record locking information */
225177633Sdfr#define	F_SETLK		12		/* set record locking information */
226177633Sdfr#define	F_SETLKW	13		/* F_SETLK; wait if blocked */
227238667Skib#if __BSD_VISIBLE
228177633Sdfr#define	F_SETLK_REMOTE	14		/* debugging support for remote locks */
229197579Sdelphij#define	F_READAHEAD	15		/* read ahead */
230197579Sdelphij#define	F_RDAHEAD	16		/* Darwin compatible read ahead */
231238667Skib#endif
232238614Skib#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
233238614Skib#define	F_DUPFD_CLOEXEC	17		/* Like F_DUPFD, but FD_CLOEXEC is set */
234238614Skib#endif
235238834Skib#if __BSD_VISIBLE
236238834Skib#define	F_DUP2FD_CLOEXEC 18		/* Like F_DUP2FD, but FD_CLOEXEC is set */
237238834Skib#endif
2381541Srgrimes
2391541Srgrimes/* file descriptor flags (F_GETFD, F_SETFD) */
2401541Srgrimes#define	FD_CLOEXEC	1		/* close-on-exec flag */
2411541Srgrimes
2421541Srgrimes/* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */
2431541Srgrimes#define	F_RDLCK		1		/* shared or read lock */
2441541Srgrimes#define	F_UNLCK		2		/* unlock */
2451541Srgrimes#define	F_WRLCK		3		/* exclusive or write lock */
246238667Skib#if __BSD_VISIBLE
247177633Sdfr#define	F_UNLCKSYS	4		/* purge locks for a given system ID */
248177633Sdfr#define	F_CANCEL	5		/* cancel an async lock request */
249238667Skib#endif
25055205Speter#ifdef _KERNEL
2511541Srgrimes#define	F_WAIT		0x010		/* Wait until lock is granted */
2521541Srgrimes#define	F_FLOCK		0x020	 	/* Use flock(2) semantics for lock */
2531541Srgrimes#define	F_POSIX		0x040	 	/* Use POSIX semantics for lock */
254177633Sdfr#define	F_REMOTE	0x080		/* Lock owner is remote NFS client */
255238667Skib#define	F_NOINTR	0x100		/* Ignore signals when waiting */
2561541Srgrimes#endif
2571541Srgrimes
2581541Srgrimes/*
2591541Srgrimes * Advisory file segment locking data type -
2601541Srgrimes * information passed to system by user
2611541Srgrimes */
2621541Srgrimesstruct flock {
2631541Srgrimes	off_t	l_start;	/* starting offset */
2641541Srgrimes	off_t	l_len;		/* len = 0 means until end of file */
2651541Srgrimes	pid_t	l_pid;		/* lock owner */
2661541Srgrimes	short	l_type;		/* lock type: read/write, etc. */
2671541Srgrimes	short	l_whence;	/* type of l_start */
268177633Sdfr	int	l_sysid;	/* remote system id or zero for local */
2691541Srgrimes};
2701541Srgrimes
271238667Skib#if __BSD_VISIBLE
272177633Sdfr/*
273177633Sdfr * Old advisory file segment locking data type,
274177633Sdfr * before adding l_sysid.
275177633Sdfr */
276238667Skibstruct __oflock {
277177633Sdfr	off_t	l_start;	/* starting offset */
278177633Sdfr	off_t	l_len;		/* len = 0 means until end of file */
279177633Sdfr	pid_t	l_pid;		/* lock owner */
280177633Sdfr	short	l_type;		/* lock type: read/write, etc. */
281177633Sdfr	short	l_whence;	/* type of l_start */
282177633Sdfr};
283238667Skib#endif
2841541Srgrimes
285103506Smike#if __BSD_VISIBLE
2861541Srgrimes/* lock operations for flock(2) */
2871541Srgrimes#define	LOCK_SH		0x01		/* shared file lock */
2881541Srgrimes#define	LOCK_EX		0x02		/* exclusive file lock */
2891541Srgrimes#define	LOCK_NB		0x04		/* don't block when locking */
2901541Srgrimes#define	LOCK_UN		0x08		/* unlock file */
2911541Srgrimes#endif
2921541Srgrimes
293227070Sjhb#if __POSIX_VISIBLE >= 200112
294103506Smike/*
295227070Sjhb * Advice to posix_fadvise
296103506Smike */
297227070Sjhb#define	POSIX_FADV_NORMAL	0	/* no special treatment */
298227070Sjhb#define	POSIX_FADV_RANDOM	1	/* expect random page references */
299227070Sjhb#define	POSIX_FADV_SEQUENTIAL	2	/* expect sequential page references */
300227070Sjhb#define	POSIX_FADV_WILLNEED	3	/* will need these pages */
301227070Sjhb#define	POSIX_FADV_DONTNEED	4	/* dont need these pages */
302227070Sjhb#define	POSIX_FADV_NOREUSE	5	/* access data only once */
303227070Sjhb#endif
3041541Srgrimes
30555205Speter#ifndef _KERNEL
3061541Srgrimes__BEGIN_DECLS
30792719Salfredint	open(const char *, int, ...);
30892719Salfredint	creat(const char *, mode_t);
30992719Salfredint	fcntl(int, int, ...);
310226850Sjhb#if __BSD_VISIBLE
311226850Sjhbint	flock(int, int);
312226850Sjhb#endif
313189353Sdas#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
314177791Skibint	openat(int, const char *, int, ...);
315189353Sdas#endif
316220791Smdf#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112
317227070Sjhbint	posix_fadvise(int, off_t, off_t, int);
318220791Smdfint	posix_fallocate(int, off_t, off_t);
319220791Smdf#endif
3201541Srgrimes__END_DECLS
3211541Srgrimes#endif
3221541Srgrimes
3231541Srgrimes#endif /* !_SYS_FCNTL_H_ */
324