2 * lib/krb5/os/lock_file.c
4 * Copyright 1990 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. M.I.T. makes no representations about the suitability of
20 * this software for any purpose. It is provided "as is" without express
21 * or implied warranty.
24 * libos: krb5_lock_file routine
30 #if !defined(_MSDOS) && !defined(HAVE_MACSOCK_H)
38 #ifdef POSIX_FILE_LOCKS
41 #define SHARED_LOCK F_RDLCK
42 #define EXCLUSIVE_LOCK F_WRLCK
43 #define UNLOCK_LOCK F_UNLCK
46 #define SHARED_LOCK LOCK_SH
47 #define EXCLUSIVE_LOCK LOCK_EX
48 #define UNLOCK_LOCK LOCK_UN
55 krb5_lock_file(context, filep, pathname, mode)
61 #ifdef POSIX_FILE_LOCKS
62 int lock_cmd = F_SETLKW;
63 struct flock lock_arg;
64 #define lock_flag lock_arg.l_type
70 switch (mode & ~KRB5_LOCKMODE_DONTBLOCK) {
71 case KRB5_LOCKMODE_SHARED:
72 lock_flag = SHARED_LOCK;
74 case KRB5_LOCKMODE_EXCLUSIVE:
75 lock_flag = EXCLUSIVE_LOCK;
77 case KRB5_LOCKMODE_UNLOCK:
78 lock_flag = UNLOCK_LOCK;
83 return(KRB5_LIBOS_BADLOCKFLAG);
85 if (mode & KRB5_LOCKMODE_DONTBLOCK) {
86 #ifdef POSIX_FILE_LOCKS
93 #ifdef POSIX_FILE_LOCKS
94 lock_arg.l_whence = 0;
97 if (fcntl(fileno(filep), lock_cmd, &lock_arg) == -1) {
98 if (errno == EACCES || errno == EAGAIN) /* see POSIX/IEEE 1003.1-1988,
104 if (flock(fileno(filep), lock_flag) == -1)
109 #else /* MSDOS or Macintosh */
112 krb5_lock_file(context, filep, pathname, mode)
113 krb5_context context;