xref: /netbsd/sys/sys/fcntl.h (revision bf9ec67e)
1 /*	$NetBSD: fcntl.h,v 1.20 2001/12/07 07:09:30 jdolecek 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. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)fcntl.h	8.3 (Berkeley) 1/21/94
41  */
42 
43 #ifndef _SYS_FCNTL_H_
44 #define	_SYS_FCNTL_H_
45 
46 /*
47  * This file includes the definitions for open and fcntl
48  * described by POSIX for <fcntl.h>; it also includes
49  * related kernel definitions.
50  */
51 
52 #ifndef _KERNEL
53 #include <sys/featuretest.h>
54 #include <sys/types.h>
55 #if !defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE)
56 #include <sys/stat.h>
57 #endif /* !_POSIX_C_SOURCE */
58 #endif /* !_KERNEL */
59 
60 /*
61  * File status flags: these are used by open(2), fcntl(2).
62  * They are also used (indirectly) in the kernel file structure f_flags,
63  * which is a superset of the open/fcntl flags.  Open flags and f_flags
64  * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags).
65  * Open/fcntl flags begin with O_; kernel-internal flags begin with F.
66  */
67 /* open-only flags */
68 #define	O_RDONLY	0x00000000	/* open for reading only */
69 #define	O_WRONLY	0x00000001	/* open for writing only */
70 #define	O_RDWR		0x00000002	/* open for reading and writing */
71 #define	O_ACCMODE	0x00000003	/* mask for above modes */
72 
73 /*
74  * Kernel encoding of open mode; separate read and write bits that are
75  * independently testable: 1 greater than the above.
76  *
77  * XXX
78  * FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH,
79  * which was documented to use FREAD/FWRITE, continues to work.
80  */
81 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
82 #define	FREAD		0x00000001
83 #define	FWRITE		0x00000002
84 #endif
85 #define	O_NONBLOCK	0x00000004	/* no delay */
86 #define	O_APPEND	0x00000008	/* set append mode */
87 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
88 #define	O_SHLOCK	0x00000010	/* open with shared file lock */
89 #define	O_EXLOCK	0x00000020	/* open with exclusive file lock */
90 #define	O_ASYNC		0x00000040	/* signal pgrp when data ready */
91 #endif
92 #if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || \
93     (_POSIX_C_SOURCE - 0) >= 199309L || \
94     (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
95     (_XOPEN_SOURCE - 0) >= 500
96 #define	O_SYNC		0x00000080		/* synchronous writes */
97 #endif
98 #define	O_CREAT		0x00000200		/* create if nonexistent */
99 #define	O_TRUNC		0x00000400		/* truncate to zero length */
100 #define	O_EXCL		0x00000800		/* error if already exists */
101 
102 #if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || \
103     (_POSIX_C_SOURCE - 0) >= 199309L || (_XOPEN_SOURCE - 0) >= 500
104 #define	O_DSYNC		0x00010000	/* write: I/O data completion */
105 #define	O_RSYNC		0x00020000	/* read: I/O completion as for write */
106 #endif
107 
108 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
109 #define	O_ALT_IO	0x00040000	/* use alternate i/o semantics */
110 #endif
111 
112 /* defined by POSIX 1003.1; BSD default, but required to be bitwise distinct */
113 #define	O_NOCTTY	0x008000	/* don't assign controlling terminal */
114 
115 #ifdef _KERNEL
116 /* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */
117 #define	FFLAGS(oflags)	((oflags) + 1)
118 #define	OFLAGS(fflags)	((fflags) - 1)
119 
120 /* all bits settable during open(2) */
121 #define	O_MASK		(O_ACCMODE|O_NONBLOCK|O_APPEND|O_SHLOCK|O_EXLOCK|\
122 			 O_ASYNC|O_SYNC|O_CREAT|O_TRUNC|O_EXCL|O_DSYNC|\
123 			 O_RSYNC|O_NOCTTY|O_ALT_IO)
124 
125 #define	FMARK		0x00001000	/* mark during gc() */
126 #define	FDEFER		0x00002000	/* defer for next gc pass */
127 #define	FHASLOCK	0x00004000	/* descriptor holds advisory lock */
128 /*
129  * Note: The below is not a flag that can be used in the struct file.
130  * It's an option that can be passed to vn_open to make sure it doesn't
131  * follow a symlink on the last lookup
132  */
133 #define	FNOSYMLINK	0x00080000	/* Don't follow symlink for last
134 					   component */
135 /* bits to save after open(2) */
136 #define	FMASK		(FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FDSYNC|\
137 			 FRSYNC|FALTIO)
138 /* bits settable by fcntl(F_SETFL, ...) */
139 #define	FCNTLFLAGS	(FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FDSYNC|FRSYNC|FALTIO)
140 #endif /* _KERNEL */
141 
142 /*
143  * The O_* flags used to have only F* names, which were used in the kernel
144  * and by fcntl.  We retain the F* names for the kernel f_flags field
145  * and for backward compatibility for fcntl.
146  */
147 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
148 #define	FAPPEND		O_APPEND	/* kernel/compat */
149 #define	FASYNC		O_ASYNC		/* kernel/compat */
150 #define	O_FSYNC		O_SYNC		/* compat */
151 #define	FNDELAY		O_NONBLOCK	/* compat */
152 #define	O_NDELAY	O_NONBLOCK	/* compat */
153 #endif
154 #if defined(_KERNEL)
155 #define	FNONBLOCK	O_NONBLOCK	/* kernel */
156 #define	FFSYNC		O_SYNC		/* kernel */
157 #define	FDSYNC		O_DSYNC		/* kernel */
158 #define	FRSYNC		O_RSYNC		/* kernel */
159 #define	FALTIO		O_ALT_IO	/* kernel */
160 #endif
161 
162 /*
163  * Constants used for fcntl(2)
164  */
165 
166 /* command values */
167 #define	F_DUPFD		0		/* duplicate file descriptor */
168 #define	F_GETFD		1		/* get file descriptor flags */
169 #define	F_SETFD		2		/* set file descriptor flags */
170 #define	F_GETFL		3		/* get file status flags */
171 #define	F_SETFL		4		/* set file status flags */
172 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
173 #define	F_GETOWN	5		/* get SIGIO/SIGURG proc/pgrp */
174 #define F_SETOWN	6		/* set SIGIO/SIGURG proc/pgrp */
175 #endif
176 #define	F_GETLK		7		/* get record locking information */
177 #define	F_SETLK		8		/* set record locking information */
178 #define	F_SETLKW	9		/* F_SETLK; wait if blocked */
179 
180 /* file descriptor flags (F_GETFD, F_SETFD) */
181 #define	FD_CLOEXEC	1		/* close-on-exec flag */
182 
183 /* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */
184 #define	F_RDLCK		1		/* shared or read lock */
185 #define	F_UNLCK		2		/* unlock */
186 #define	F_WRLCK		3		/* exclusive or write lock */
187 #ifdef _KERNEL
188 #define	F_WAIT		0x010		/* Wait until lock is granted */
189 #define	F_FLOCK		0x020	 	/* Use flock(2) semantics for lock */
190 #define	F_POSIX		0x040	 	/* Use POSIX semantics for lock */
191 #endif
192 
193 /* Constants for fcntl's passed to the underlying fs - like ioctl's. */
194 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
195 #define	F_PARAM_MASK	0xfff
196 #define	F_PARAM_LEN(x)	(((x) >> 16) & F_PARAM_MASK)
197 #define	F_PARAM_MAX	4095
198 #define	F_FSCTL		(int)0x80000000	/* This fcntl goes to the fs */
199 #define	F_FSVOID	(int)0x40000000	/* no parameters */
200 #define	F_FSOUT		(int)0x20000000	/* copy out parameter */
201 #define	F_FSIN		(int)0x10000000	/* copy in parameter */
202 #define	F_FSINOUT	(F_FSIN | F_FSOUT)
203 #define	F_FSDIRMASK	(int)0x70000000	/* mask for IN/OUT/VOID */
204 #define	F_FSPRIV	(int)0x00008000	/* command is fs-specific */
205 
206 /*
207  * Define command macros for operations which, if implemented, must be
208  * the same for all fs's.
209  */
210 #define	_FCN(inout, num, len) \
211 		(F_FSCTL | inout | ((len & F_PARAM_MASK) << 16) | (num))
212 #define	_FCNO(c)	_FCN(F_FSVOID,	(c), 0)
213 #define	_FCNR(c, t)	_FCN(F_FSIN,	(c), (int)sizeof(t))
214 #define	_FCNW(c, t)	_FCN(F_FSOUT,	(c), (int)sizeof(t))
215 #define	_FCNRW(c, t)	_FCN(F_FSINOUT,	(c), (int)sizeof(t))
216 
217 /*
218  * Define command macros for fs-specific commands.
219  */
220 #define	_FCN_FSPRIV(inout, num, len) \
221 	(F_FSCTL | F_FSPRIV | inout | ((len & F_PARAM_MASK) << 16) | (num))
222 #define	_FCNO_FSPRIV(c)		_FCN_FSPRIV(F_FSVOID,	(c), 0)
223 #define	_FCNR_FSPRIV(c, t)	_FCN_FSPRIV(F_FSIN,	(c), (int)sizeof(t))
224 #define	_FCNW_FSPRIV(c, t)	_FCN_FSPRIV(F_FSOUT,	(c), (int)sizeof(t))
225 #define	_FCNRW_FSPRIV(c, t)	_FCN_FSPRIV(F_FSINOUT,	(c), (int)sizeof(t))
226 
227 #endif /* neither POSIX nor _XOPEN_SOURCE */
228 
229 /*
230  * Advisory file segment locking data type -
231  * information passed to system by user
232  */
233 struct flock {
234 	off_t	l_start;	/* starting offset */
235 	off_t	l_len;		/* len = 0 means until end of file */
236 	pid_t	l_pid;		/* lock owner */
237 	short	l_type;		/* lock type: read/write, etc. */
238 	short	l_whence;	/* type of l_start */
239 };
240 
241 
242 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
243 /* lock operations for flock(2) */
244 #define	LOCK_SH		0x01		/* shared file lock */
245 #define	LOCK_EX		0x02		/* exclusive file lock */
246 #define	LOCK_NB		0x04		/* don't block when locking */
247 #define	LOCK_UN		0x08		/* unlock file */
248 #endif
249 
250 /* Always ensure that these are consistent with <stdio.h> and <unistd.h>! */
251 #ifndef	SEEK_SET
252 #define	SEEK_SET	0	/* set file offset to offset */
253 #endif
254 #ifndef	SEEK_CUR
255 #define	SEEK_CUR	1	/* set file offset to current plus offset */
256 #endif
257 #ifndef	SEEK_END
258 #define	SEEK_END	2	/* set file offset to EOF plus offset */
259 #endif
260 
261 #ifndef _KERNEL
262 #include <sys/cdefs.h>
263 
264 __BEGIN_DECLS
265 int	open __P((const char *, int, ...));
266 int	creat __P((const char *, mode_t));
267 int	fcntl __P((int, int, ...));
268 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
269 int	flock __P((int, int));
270 #endif /* !_POSIX_C_SOURCE && !_XOPEN_SOURCE */
271 __END_DECLS
272 #endif /* !_KERNEL */
273 
274 #endif /* !_SYS_FCNTL_H_ */
275