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