1 /* $NetBSD: fcntl.h,v 1.42 2012/01/25 00:28:35 christos 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. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)fcntl.h 8.3 (Berkeley) 1/21/94 37 */ 38 39 #ifndef _SYS_FCNTL_H_ 40 #define _SYS_FCNTL_H_ 41 42 /* 43 * This file includes the definitions for open and fcntl 44 * described by POSIX for <fcntl.h>; it also includes 45 * related kernel definitions. 46 */ 47 48 #ifndef _KERNEL 49 #include <sys/featuretest.h> 50 #include <sys/types.h> 51 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE) 52 #include <sys/stat.h> 53 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */ 54 #endif /* !_KERNEL */ 55 56 /* 57 * File status flags: these are used by open(2), fcntl(2). 58 * They are also used (indirectly) in the kernel file structure f_flags, 59 * which is a superset of the open/fcntl flags. Open flags and f_flags 60 * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags). 61 * Open/fcntl flags begin with O_; kernel-internal flags begin with F. 62 */ 63 /* open-only flags */ 64 #define O_RDONLY 0x00000000 /* open for reading only */ 65 #define O_WRONLY 0x00000001 /* open for writing only */ 66 #define O_RDWR 0x00000002 /* open for reading and writing */ 67 #define O_ACCMODE 0x00000003 /* mask for above modes */ 68 69 /* File status flags for open() and fcntl(). POSIX Table 6-5. */ 70 #define O_APPEND 02000 /* set append mode */ 71 #define O_NONBLOCK 04000 /* no delay */ 72 #define O_REOPEN 010000 /* automatically re-open device after driver 73 * restart 74 */ 75 #define O_CLOEXEC 020000 /* close on exec */ 76 #if defined(_NETBSD_SOURCE) 77 #define O_NOSIGPIPE 040000 /* don't deliver sigpipe */ 78 #endif 79 80 #ifndef __minix /* NOT SUPPORTED! */ 81 #if defined(_NETBSD_SOURCE) 82 #define O_SHLOCK 0x00000010 /* open with shared file lock */ 83 #define O_EXLOCK 0x00000020 /* open with exclusive file lock */ 84 #define O_ASYNC 0x00000040 /* signal pgrp when data ready */ 85 #endif 86 #if (_POSIX_C_SOURCE - 0) >= 199309L || \ 87 (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \ 88 (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE) 89 #define O_SYNC 0x00000080 /* synchronous writes */ 90 #endif 91 #if defined(_NETBSD_SOURCE) 92 #define O_NOFOLLOW 0x00000100 /* don't follow symlinks on the last */ 93 /* path component */ 94 #endif 95 #endif /* !__minix */ 96 97 /* Oflag values for open(). POSIX Table 6-4. */ 98 #define O_CREAT 00100 /* creat file if it doesn't exist */ 99 #define O_EXCL 00200 /* exclusive use flag */ 100 #define O_NOCTTY 00400 /* do not assign a controlling terminal */ 101 #define O_TRUNC 01000 /* truncate flag */ 102 103 #ifndef __minix /* NOT SUPPORTED! */ 104 #if (_POSIX_C_SOURCE - 0) >= 199309L || (_XOPEN_SOURCE - 0) >= 500 || \ 105 defined(_NETBSD_SOURCE) 106 #define O_DSYNC 0x00010000 /* write: I/O data completion */ 107 #define O_RSYNC 0x00020000 /* read: I/O completion as for write */ 108 #endif 109 110 #if defined(_NETBSD_SOURCE) 111 #define O_ALT_IO 0x00040000 /* use alternate i/o semantics */ 112 #define O_DIRECT 0x00080000 /* direct I/O hint */ 113 #endif 114 #endif /* !__minix */ 115 116 /* 117 * Constants used for fcntl(2) 118 */ 119 120 /* command values */ 121 /* These values are used for cmd in fcntl(). POSIX Table 6-1. */ 122 #define F_DUPFD 0 /* duplicate file descriptor */ 123 #define F_GETFD 1 /* get file descriptor flags */ 124 #define F_SETFD 2 /* set file descriptor flags */ 125 #define F_GETFL 3 /* get file status flags */ 126 #define F_SETFL 4 /* set file status flags */ 127 #define F_GETLK 5 /* get record locking information */ 128 #define F_SETLK 6 /* set record locking information */ 129 #define F_SETLKW 7 /* set record locking info; wait if blocked */ 130 #define F_FREESP 8 /* free a section of a regular file */ 131 #if defined(_NETBSD_SOURCE) 132 #define F_GETNOSIGPIPE 9 133 #define F_SETNOSIGPIPE 10 134 #endif 135 136 /* File descriptor flags used for fcntl(). POSIX Table 6-2. */ 137 #define FD_CLOEXEC 1 /* close on exec flag for third arg of fcntl */ 138 139 /* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */ 140 #define F_RDLCK 1 /* shared or read lock */ 141 #define F_WRLCK 2 /* exclusive or write lock */ 142 #define F_UNLCK 3 /* unlock */ 143 144 /* 145 * Advisory file segment locking data type - 146 * information passed to system by user 147 */ 148 struct flock { 149 short l_type; /* type: F_RDLCK, F_WRLCK, or F_UNLCK */ 150 short l_whence; /* flag for starting offset */ 151 off_t l_start; /* relative offset in bytes */ 152 off_t l_len; /* size; if 0, then until EOF */ 153 pid_t l_pid; /* process id of the locks' owner */ 154 }; 155 156 #if defined(_NETBSD_SOURCE) 157 /* lock operations for flock(2) */ 158 #define LOCK_SH F_RDLCK /* Shared lock */ 159 #define LOCK_EX F_WRLCK /* Exclusive lock */ 160 #define LOCK_NB 0x0080 /* Do not block when locking */ 161 #define LOCK_UN F_UNLCK /* Unlock */ 162 #endif 163 164 /* Always ensure that these are consistent with <stdio.h> and <unistd.h>! */ 165 #ifndef SEEK_SET 166 #define SEEK_SET 0 /* set file offset to offset */ 167 #endif 168 #ifndef SEEK_CUR 169 #define SEEK_CUR 1 /* set file offset to current plus offset */ 170 #endif 171 #ifndef SEEK_END 172 #define SEEK_END 2 /* set file offset to EOF plus offset */ 173 #endif 174 175 /* 176 * posix_advise advisories. 177 */ 178 179 #define POSIX_FADV_NORMAL 0 /* default advice / no advice */ 180 #define POSIX_FADV_RANDOM 1 /* random access */ 181 #define POSIX_FADV_SEQUENTIAL 2 /* sequential access(lower to higher) */ 182 #define POSIX_FADV_WILLNEED 3 /* be needed in near future */ 183 #define POSIX_FADV_DONTNEED 4 /* not be needed in near future */ 184 #define POSIX_FADV_NOREUSE 5 /* be accessed once */ 185 186 /* 187 * Constants for X/Open Extended API set 2 (a.k.a. C063) 188 * linkat(2) - also part of Posix-2008/XPG7 189 */ 190 #if (_POSIX_C_SOURCE - 0) >= 200809L || (_XOPEN_SOURCE - 0) >= 700 || \ 191 defined(_NETBSD_SOURCE) 192 #if defined(_INCOMPLETE_XOPEN_C063) || defined(_KERNEL) || defined(__minix) 193 #define AT_FDCWD -100 /* Use cwd for relative link target */ 194 #define AT_EACCESS 0x100 /* Use euid/egid for access checks */ 195 #define AT_SYMLINK_NOFOLLOW 0x200 /* Do not follow symlinks */ 196 #define AT_SYMLINK_FOLLOW 0x400 /* Follow symlinks */ 197 #define AT_REMOVEDIR 0x800 /* Remove directory only */ 198 #endif 199 #endif 200 201 202 #ifndef _KERNEL 203 #include <sys/cdefs.h> 204 205 __BEGIN_DECLS 206 int open(const char *, int, ...); 207 int creat(const char *, mode_t); 208 int fcntl(int, int, ...); 209 #if defined(_NETBSD_SOURCE) 210 int flock(int, int); 211 #endif /* _NETBSD_SOURCE */ 212 __END_DECLS 213 #endif /* _KERNEL */ 214 215 #endif /* !_SYS_FCNTL_H_ */ 216