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.14 (Berkeley) 07/01/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 (indirectly) in the kernel file structure f_flags, 26 * which is a superset of the open/fcntl flags. Open flags and f_flags 27 * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags). 28 * Open/fcntl flags begin with O_; kernel-internal 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 /* kernel/compat */ 82 #define FASYNC O_ASYNC /* kernel/compat */ 83 #define FFSYNC O_FSYNC /* kernel */ 84 #define FNONBLOCK O_NONBLOCK /* kernel */ 85 #define FNDELAY O_NONBLOCK /* compat */ 86 #define O_NDELAY O_NONBLOCK /* compat */ 87 #endif 88 89 /* 90 * Constants used for fcntl(2) 91 */ 92 93 /* command values */ 94 #define F_DUPFD 0 /* duplicate file descriptor */ 95 #define F_GETFD 1 /* get file descriptor flags */ 96 #define F_SETFD 2 /* set file descriptor flags */ 97 #define F_GETFL 3 /* get file status flags */ 98 #define F_SETFL 4 /* set file status flags */ 99 #ifndef _POSIX_SOURCE 100 #define F_GETOWN 5 /* get SIGIO/SIGURG proc/pgrp */ 101 #define F_SETOWN 6 /* set SIGIO/SIGURG proc/pgrp */ 102 #endif 103 #define F_GETLK 7 /* get record locking information */ 104 #define F_SETLK 8 /* set record locking information */ 105 #define F_SETLKW 9 /* F_SETLK; wait if blocked */ 106 107 /* file descriptor flags (F_GETFD, F_SETFD) */ 108 #define FD_CLOEXEC 1 /* close-on-exec flag */ 109 110 /* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */ 111 #define F_RDLCK 1 /* shared or read lock */ 112 #define F_UNLCK 2 /* unlock */ 113 #define F_WRLCK 3 /* exclusive or write lock */ 114 #ifdef KERNEL 115 #define F_WAIT 0x010 /* Wait until lock is granted */ 116 #define F_FLOCK 0x020 /* Use flock(2) semantics for lock */ 117 #define F_POSIX 0x040 /* Use POSIX semantics for lock */ 118 #endif 119 120 /* 121 * Advisory file segment locking data type - 122 * information passed to system by user 123 */ 124 struct flock { 125 short l_type; /* lock type: read/write, etc. */ 126 short l_whence; /* type of l_start */ 127 off_t l_start; /* starting offset */ 128 off_t l_len; /* len = 0 means until end of file */ 129 pid_t l_pid; /* lock owner */ 130 }; 131 132 133 #ifndef _POSIX_SOURCE 134 /* lock operations for flock(2) */ 135 #define LOCK_SH 0x01 /* shared file lock */ 136 #define LOCK_EX 0x02 /* exclusive file lock */ 137 #define LOCK_NB 0x04 /* don't block when locking */ 138 #define LOCK_UN 0x08 /* unlock file */ 139 #endif 140 141 142 #ifndef KERNEL 143 #include <sys/cdefs.h> 144 145 __BEGIN_DECLS 146 int open __P((const char *, int, ...)); 147 int creat __P((const char *, mode_t)); 148 int fcntl __P((int, int, ...)); 149 #ifndef _POSIX_SOURCE 150 int flock __P((int, int)); 151 #endif /* !_POSIX_SOURCE */ 152 __END_DECLS 153 #endif 154 155 #endif /* !_FCNTL_H_ */ 156