xref: /original-bsd/sys/sys/fcntl.h (revision 60749750)
1 /*-
2  * Copyright (c) 1983, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)fcntl.h	8.3 (Berkeley) 01/21/94
13  */
14 
15 #ifndef _SYS_FCNTL_H_
16 #define	_SYS_FCNTL_H_
17 
18 /*
19  * This file includes the definitions for open and fcntl
20  * described by POSIX for <fcntl.h>; it also includes
21  * related kernel definitions.
22  */
23 
24 #ifndef KERNEL
25 #include <sys/types.h>
26 #endif
27 
28 /*
29  * File status flags: these are used by open(2), fcntl(2).
30  * They are also used (indirectly) in the kernel file structure f_flags,
31  * which is a superset of the open/fcntl flags.  Open flags and f_flags
32  * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags).
33  * Open/fcntl flags begin with O_; kernel-internal flags begin with F.
34  */
35 /* open-only flags */
36 #define	O_RDONLY	0x0000		/* open for reading only */
37 #define	O_WRONLY	0x0001		/* open for writing only */
38 #define	O_RDWR		0x0002		/* open for reading and writing */
39 #define	O_ACCMODE	0x0003		/* mask for above modes */
40 
41 /*
42  * Kernel encoding of open mode; separate read and write bits that are
43  * independently testable: 1 greater than the above.
44  *
45  * XXX
46  * FREAD and FWRITE are excluded from the #ifdef KERNEL so that TIOCFLUSH,
47  * which was documented to use FREAD/FWRITE, continues to work.
48  */
49 #ifndef _POSIX_SOURCE
50 #define	FREAD		0x0001
51 #define	FWRITE		0x0002
52 #endif
53 #define	O_NONBLOCK	0x0004		/* no delay */
54 #define	O_APPEND	0x0008		/* set append mode */
55 #ifndef _POSIX_SOURCE
56 #define	O_SHLOCK	0x0010		/* open with shared file lock */
57 #define	O_EXLOCK	0x0020		/* open with exclusive file lock */
58 #define	O_ASYNC		0x0040		/* signal pgrp when data ready */
59 #define	O_FSYNC		0x0080		/* synchronous writes */
60 #endif
61 #define	O_CREAT		0x0200		/* create if nonexistant */
62 #define	O_TRUNC		0x0400		/* truncate to zero length */
63 #define	O_EXCL		0x0800		/* error if already exists */
64 #ifdef KERNEL
65 #define	FMARK		0x1000		/* mark during gc() */
66 #define	FDEFER		0x2000		/* defer for next gc pass */
67 #define	FHASLOCK	0x4000		/* descriptor holds advisory lock */
68 #endif
69 
70 /* defined by POSIX 1003.1; BSD default, so no bit required */
71 #define	O_NOCTTY	0		/* don't assign controlling terminal */
72 
73 #ifdef KERNEL
74 /* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */
75 #define	FFLAGS(oflags)	((oflags) + 1)
76 #define	OFLAGS(fflags)	((fflags) - 1)
77 
78 /* bits to save after open */
79 #define	FMASK		(FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK)
80 /* bits settable by fcntl(F_SETFL, ...) */
81 #define	FCNTLFLAGS	(FAPPEND|FASYNC|FFSYNC|FNONBLOCK)
82 #endif
83 
84 /*
85  * The O_* flags used to have only F* names, which were used in the kernel
86  * and by fcntl.  We retain the F* names for the kernel f_flags field
87  * and for backward compatibility for fcntl.
88  */
89 #ifndef _POSIX_SOURCE
90 #define	FAPPEND		O_APPEND	/* kernel/compat */
91 #define	FASYNC		O_ASYNC		/* kernel/compat */
92 #define	FFSYNC		O_FSYNC		/* kernel */
93 #define	FNONBLOCK	O_NONBLOCK	/* kernel */
94 #define	FNDELAY		O_NONBLOCK	/* compat */
95 #define	O_NDELAY	O_NONBLOCK	/* compat */
96 #endif
97 
98 /*
99  * Constants used for fcntl(2)
100  */
101 
102 /* command values */
103 #define	F_DUPFD		0		/* duplicate file descriptor */
104 #define	F_GETFD		1		/* get file descriptor flags */
105 #define	F_SETFD		2		/* set file descriptor flags */
106 #define	F_GETFL		3		/* get file status flags */
107 #define	F_SETFL		4		/* set file status flags */
108 #ifndef _POSIX_SOURCE
109 #define	F_GETOWN	5		/* get SIGIO/SIGURG proc/pgrp */
110 #define F_SETOWN	6		/* set SIGIO/SIGURG proc/pgrp */
111 #endif
112 #define	F_GETLK		7		/* get record locking information */
113 #define	F_SETLK		8		/* set record locking information */
114 #define	F_SETLKW	9		/* F_SETLK; wait if blocked */
115 
116 /* file descriptor flags (F_GETFD, F_SETFD) */
117 #define	FD_CLOEXEC	1		/* close-on-exec flag */
118 
119 /* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */
120 #define	F_RDLCK		1		/* shared or read lock */
121 #define	F_UNLCK		2		/* unlock */
122 #define	F_WRLCK		3		/* exclusive or write lock */
123 #ifdef KERNEL
124 #define	F_WAIT		0x010		/* Wait until lock is granted */
125 #define	F_FLOCK		0x020	 	/* Use flock(2) semantics for lock */
126 #define	F_POSIX		0x040	 	/* Use POSIX semantics for lock */
127 #endif
128 
129 /*
130  * Advisory file segment locking data type -
131  * information passed to system by user
132  */
133 struct flock {
134 	off_t	l_start;	/* starting offset */
135 	off_t	l_len;		/* len = 0 means until end of file */
136 	pid_t	l_pid;		/* lock owner */
137 	short	l_type;		/* lock type: read/write, etc. */
138 	short	l_whence;	/* type of l_start */
139 };
140 
141 
142 #ifndef _POSIX_SOURCE
143 /* lock operations for flock(2) */
144 #define	LOCK_SH		0x01		/* shared file lock */
145 #define	LOCK_EX		0x02		/* exclusive file lock */
146 #define	LOCK_NB		0x04		/* don't block when locking */
147 #define	LOCK_UN		0x08		/* unlock file */
148 #endif
149 
150 
151 #ifndef KERNEL
152 #include <sys/cdefs.h>
153 
154 __BEGIN_DECLS
155 int	open __P((const char *, int, ...));
156 int	creat __P((const char *, mode_t));
157 int	fcntl __P((int, int, ...));
158 #ifndef _POSIX_SOURCE
159 int	flock __P((int, int));
160 #endif /* !_POSIX_SOURCE */
161 __END_DECLS
162 #endif
163 
164 #endif /* !_SYS_FCNTL_H_ */
165