| 1 | /* f_setlk -- locking part of fcntl |
| 2 | Copyright (C) 2014-2024 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/file.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <hurd.h> |
| 23 | #include <unistd.h> |
| 24 | #include <errno.h> |
| 25 | |
| 26 | /* XXX |
| 27 | We need new RPCs to support POSIX.1 fcntl file locking!! |
| 28 | For the time being we support the whole-file case only, |
| 29 | with all kinds of WRONG WRONG WRONG semantics, |
| 30 | by using flock. This is definitely the Wrong Thing, |
| 31 | but it might be better than nothing (?). */ |
| 32 | int |
| 33 | __f_setlk (int fd, int type, int whence, __off64_t start, __off64_t len, int wait) |
| 34 | { |
| 35 | int cmd = 0; |
| 36 | |
| 37 | switch (type) |
| 38 | { |
| 39 | case F_RDLCK: cmd = LOCK_SH; break; |
| 40 | case F_WRLCK: cmd = LOCK_EX; break; |
| 41 | case F_UNLCK: cmd = LOCK_UN; break; |
| 42 | default: |
| 43 | return __hurd_fail (EINVAL); |
| 44 | } |
| 45 | |
| 46 | if (cmd != LOCK_UN && wait == 0) |
| 47 | cmd |= LOCK_NB; |
| 48 | |
| 49 | if (whence == SEEK_CUR) |
| 50 | { |
| 51 | /* In case the target position is 0, we can support it below. */ |
| 52 | __off64_t cur = __lseek64 (fd: fd, offset: 0, SEEK_CUR); |
| 53 | |
| 54 | if (cur >= 0) |
| 55 | { |
| 56 | start = cur + start; |
| 57 | whence = SEEK_SET; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | switch (whence) |
| 62 | { |
| 63 | case SEEK_SET: |
| 64 | if (start == 0 && len == 0) /* Whole file request. */ |
| 65 | break; |
| 66 | /* It seems to be common for applications to lock the first |
| 67 | byte of the file when they are really doing whole-file locking. |
| 68 | So, since it's so wrong already, might as well do that too. */ |
| 69 | if (start == 0 && len == 1) |
| 70 | break; |
| 71 | /* FALLTHROUGH */ |
| 72 | case SEEK_CUR: |
| 73 | case SEEK_END: |
| 74 | return __hurd_fail (ENOTSUP); |
| 75 | default: |
| 76 | return __hurd_fail (EINVAL); |
| 77 | } |
| 78 | |
| 79 | return __flock (fd: fd, operation: cmd); |
| 80 | } |
| 81 | |