/src/php-src/ext/standard/flock_compat.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Author: Sascha Schumann <sascha@schumann.cx> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | #include "php.h" |
16 | | #include <errno.h> |
17 | | #include "ext/standard/flock_compat.h" |
18 | | |
19 | | #ifndef HAVE_FLOCK |
20 | | PHPAPI int flock(int fd, int operation) |
21 | | { |
22 | | return php_flock(fd, operation); |
23 | | } |
24 | | #endif /* !defined(HAVE_FLOCK) */ |
25 | | |
26 | | PHPAPI int php_flock(int fd, int operation) |
27 | | #ifdef HAVE_STRUCT_FLOCK /* {{{ */ |
28 | 0 | { |
29 | 0 | struct flock flck; |
30 | 0 | int ret; |
31 | |
|
32 | 0 | flck.l_start = flck.l_len = 0; |
33 | 0 | flck.l_whence = SEEK_SET; |
34 | |
|
35 | 0 | if (operation & LOCK_SH) |
36 | 0 | flck.l_type = F_RDLCK; |
37 | 0 | else if (operation & LOCK_EX) |
38 | 0 | flck.l_type = F_WRLCK; |
39 | 0 | else if (operation & LOCK_UN) |
40 | 0 | flck.l_type = F_UNLCK; |
41 | 0 | else { |
42 | 0 | errno = EINVAL; |
43 | 0 | return -1; |
44 | 0 | } |
45 | | |
46 | 0 | ret = fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &flck); |
47 | |
|
48 | 0 | if ((operation & LOCK_NB) && ret == -1 && |
49 | 0 | (errno == EACCES || errno == EAGAIN)) |
50 | 0 | errno = EWOULDBLOCK; |
51 | |
|
52 | 0 | if (ret != -1) ret = 0; |
53 | |
|
54 | 0 | return ret; |
55 | 0 | } |
56 | | /* }}} */ |
57 | | #elif defined(PHP_WIN32) /* {{{ */ |
58 | | /* |
59 | | * Program: Unix compatibility routines |
60 | | * |
61 | | * Author: Mark Crispin |
62 | | * Networks and Distributed Computing |
63 | | * Computing & Communications |
64 | | * University of Washington |
65 | | * Administration Building, AG-44 |
66 | | * Seattle, WA 98195 |
67 | | * Internet: MRC@CAC.Washington.EDU |
68 | | * |
69 | | * Date: 14 September 1996 |
70 | | * Last Edited: 14 August 1997 |
71 | | * |
72 | | * Copyright 1997 by the University of Washington |
73 | | * |
74 | | * Permission to use, copy, modify, and distribute this software and its |
75 | | * documentation for any purpose and without fee is hereby granted, provided |
76 | | * that the above copyright notice appears in all copies and that both the |
77 | | * above copyright notice and this permission notice appear in supporting |
78 | | * documentation, and that the name of the University of Washington not be |
79 | | * used in advertising or publicity pertaining to distribution of the software |
80 | | * without specific, written prior permission. This software is made available |
81 | | * "as is", and |
82 | | * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, |
83 | | * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN |
84 | | * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL, |
85 | | * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
86 | | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT |
87 | | * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION |
88 | | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
89 | | * |
90 | | */ |
91 | | /* DEDICATION |
92 | | |
93 | | * This file is dedicated to my dog, Unix, also known as Yun-chan and |
94 | | * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast. Unix |
95 | | * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after |
96 | | * a two-month bout with cirrhosis of the liver. |
97 | | * |
98 | | * He was a dear friend, and I miss him terribly. |
99 | | * |
100 | | * Lift a leg, Yunie. Luv ya forever!!!! |
101 | | */ |
102 | | { |
103 | | HANDLE hdl = (HANDLE) _get_osfhandle(fd); |
104 | | DWORD low = 0xFFFFFFFF, high = 0xFFFFFFFF; |
105 | | OVERLAPPED offset = {0, 0, {{0}}, NULL}; |
106 | | DWORD err; |
107 | | |
108 | | if (INVALID_HANDLE_VALUE == hdl) { |
109 | | _set_errno(EBADF); |
110 | | return -1; /* error in file descriptor */ |
111 | | } |
112 | | /* bug for bug compatible with Unix */ |
113 | | UnlockFileEx(hdl, 0, low, high, &offset); |
114 | | switch (operation & ~LOCK_NB) { /* translate to LockFileEx() op */ |
115 | | case LOCK_EX: /* exclusive */ |
116 | | if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK + |
117 | | ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0), |
118 | | 0, low, high, &offset)) |
119 | | return 0; |
120 | | break; |
121 | | case LOCK_SH: /* shared */ |
122 | | if (LockFileEx(hdl, ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0), |
123 | | 0, low, high, &offset)) |
124 | | return 0; |
125 | | break; |
126 | | case LOCK_UN: /* unlock */ |
127 | | return 0; /* always succeeds */ |
128 | | default: /* default */ |
129 | | break; |
130 | | } |
131 | | |
132 | | err = GetLastError(); |
133 | | if (ERROR_LOCK_VIOLATION == err || ERROR_SHARING_VIOLATION == err) { |
134 | | _set_errno(EWOULDBLOCK); |
135 | | } else { |
136 | | _set_errno(EINVAL); /* bad call */ |
137 | | } |
138 | | |
139 | | return -1; |
140 | | } |
141 | | /* }}} */ |
142 | | #else |
143 | | #warning no proper flock support for your site |
144 | | { |
145 | | errno = 0; |
146 | | return 0; |
147 | | } |
148 | | #endif |