Imported Upstream version 1.10.2
[platform/upstream/krb5.git] / src / lib / krb5 / os / lock_file.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/lock_file.c */
3 /*
4  * Copyright 1990, 1998 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.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26
27 #include "k5-int.h"
28 #include <stdio.h>
29
30 #if !defined(_WIN32)
31
32 /* Unix version...  */
33
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <errno.h>
39
40 #ifdef HAVE_FCNTL_H
41 #include <fcntl.h>
42 #endif
43
44 #if defined(HAVE_FCNTL_H) && defined(F_SETLKW) && defined(F_RDLCK)
45 #define POSIX_FILE_LOCKS
46 #endif
47
48 #ifdef HAVE_FLOCK
49 #ifndef sysvimp
50 #include <sys/file.h>
51 #endif
52 #else
53 #ifndef LOCK_SH
54 #define LOCK_SH 0
55 #define LOCK_EX 0
56 #define LOCK_UN 0
57 #endif
58 #endif
59
60 /*ARGSUSED*/
61 krb5_error_code
62 krb5_lock_file(krb5_context context, int fd, int mode)
63 {
64     int                 lock_flag = -1;
65     krb5_error_code     retval = 0;
66 #ifdef POSIX_FILE_LOCKS
67     int lock_cmd = F_SETLKW;
68     struct flock lock_arg = { 0 };
69 #endif
70
71     switch (mode & ~KRB5_LOCKMODE_DONTBLOCK) {
72     case KRB5_LOCKMODE_SHARED:
73 #ifdef POSIX_FILE_LOCKS
74         lock_arg.l_type = F_RDLCK;
75 #endif
76         lock_flag = LOCK_SH;
77         break;
78     case KRB5_LOCKMODE_EXCLUSIVE:
79 #ifdef POSIX_FILE_LOCKS
80         lock_arg.l_type = F_WRLCK;
81 #endif
82         lock_flag = LOCK_EX;
83         break;
84     case KRB5_LOCKMODE_UNLOCK:
85 #ifdef POSIX_FILE_LOCKS
86         lock_arg.l_type = F_UNLCK;
87 #endif
88         lock_flag = LOCK_UN;
89         break;
90     }
91
92     if (lock_flag == -1)
93         return(KRB5_LIBOS_BADLOCKFLAG);
94
95     if (mode & KRB5_LOCKMODE_DONTBLOCK) {
96 #ifdef POSIX_FILE_LOCKS
97         lock_cmd = F_SETLK;
98 #endif
99 #ifdef HAVE_FLOCK
100         lock_flag |= LOCK_NB;
101 #endif
102     }
103
104 #ifdef POSIX_FILE_LOCKS
105     lock_arg.l_whence = 0;
106     lock_arg.l_start = 0;
107     lock_arg.l_len = 0;
108     if (fcntl(fd, lock_cmd, &lock_arg) == -1) {
109         if (errno == EACCES || errno == EAGAIN) /* see POSIX/IEEE 1003.1-1988,
110                                                    6.5.2.4 */
111             return(EAGAIN);
112         if (errno != EINVAL)    /* Fall back to flock if we get EINVAL */
113             return(errno);
114         retval = errno;
115     } else
116         return 0;           /* We succeeded.  Yay. */
117 #endif
118
119 #ifdef HAVE_FLOCK
120     if (flock(fd, lock_flag) == -1)
121         retval = errno;
122 #endif
123
124     return retval;
125 }
126 #else   /* Windows or Macintosh */
127
128 krb5_error_code
129 krb5_lock_file(context, fd, mode)
130     krb5_context context;
131     int fd;
132     int mode;
133 {
134     return 0;
135 }
136 #endif