Windows global stuff:
[krb5.git] / src / lib / krb5 / os / lock_file.c
1 /*
2  * lib/krb5/os/lock_file.c
3  *
4  * Copyright 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
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.
11  * 
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.
22  * 
23  *
24  * libos: krb5_lock_file routine
25  */
26
27 #include "k5-int.h"
28 #include <stdio.h>
29
30 #if !defined(_MSDOS) && !defined(HAVE_MACSOCK_H)
31
32 /* Unix version...  */
33
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #ifdef POSIX_FILE_LOCKS
39 #include <errno.h>
40 #include <fcntl.h>
41 #define SHARED_LOCK     F_RDLCK
42 #define EXCLUSIVE_LOCK  F_WRLCK
43 #define UNLOCK_LOCK     F_UNLCK
44 #else
45 #include <sys/file.h>
46 #define SHARED_LOCK     LOCK_SH
47 #define EXCLUSIVE_LOCK  LOCK_EX
48 #define UNLOCK_LOCK     LOCK_UN
49 #endif
50
51 extern int errno;
52
53 /*ARGSUSED*/
54 krb5_error_code
55 krb5_lock_file(context, filep, pathname, mode)
56     krb5_context context;
57     FILE *filep;
58     char *pathname;
59     int mode;
60 {
61 #ifdef POSIX_FILE_LOCKS
62     int lock_cmd = F_SETLKW;
63     struct flock lock_arg;
64 #define lock_flag lock_arg.l_type
65     lock_flag = -1;
66 #else
67     int lock_flag = -1;
68 #endif
69
70     switch (mode & ~KRB5_LOCKMODE_DONTBLOCK) {
71     case KRB5_LOCKMODE_SHARED:
72         lock_flag = SHARED_LOCK;
73         break;
74     case KRB5_LOCKMODE_EXCLUSIVE:
75         lock_flag = EXCLUSIVE_LOCK;
76         break;
77     case KRB5_LOCKMODE_UNLOCK:
78         lock_flag = UNLOCK_LOCK;
79         break;
80     }
81
82     if (lock_flag == -1)
83         return(KRB5_LIBOS_BADLOCKFLAG);
84
85     if (mode & KRB5_LOCKMODE_DONTBLOCK) {
86 #ifdef POSIX_FILE_LOCKS
87         lock_cmd = F_SETLK;
88 #else
89         lock_flag |= LOCK_NB;
90 #endif
91     }
92
93 #ifdef POSIX_FILE_LOCKS
94     lock_arg.l_whence = 0;
95     lock_arg.l_start = 0;
96     lock_arg.l_len = 0;
97     if (fcntl(fileno(filep), lock_cmd, &lock_arg) == -1) {
98         if (errno == EACCES || errno == EAGAIN) /* see POSIX/IEEE 1003.1-1988,
99                                                    6.5.2.4 */
100             return(EAGAIN);
101         return(errno);
102     }
103 #else
104     if (flock(fileno(filep), lock_flag) == -1)
105         return(errno);
106 #endif
107     return 0;
108 }
109 #else   /* MSDOS or Macintosh */
110
111 krb5_error_code
112 krb5_lock_file(context, filep, pathname, mode)
113     krb5_context context;
114     FILE *filep;
115     char *pathname;
116     int mode;
117 {
118     return 0;
119 }
120 #endif