|
| 1 | +//===-- Implementation of internal fcntl ----------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "src/__support/OSUtil/fcntl.h" |
| 10 | + |
| 11 | +#include "hdr/fcntl_macros.h" |
| 12 | +#include "hdr/types/struct_f_owner_ex.h" |
| 13 | +#include "hdr/types/struct_flock.h" |
| 14 | +#include "hdr/types/struct_flock64.h" |
| 15 | +#include "src/__support/OSUtil/syscall.h" // For internal syscall function. |
| 16 | +#include "src/__support/common.h" |
| 17 | +#include "src/errno/libc_errno.h" |
| 18 | + |
| 19 | +#include <stdarg.h> |
| 20 | +#include <sys/syscall.h> // For syscall numbers. |
| 21 | + |
| 22 | +namespace LIBC_NAMESPACE::internal { |
| 23 | + |
| 24 | +int fcntl(int fd, int cmd, void *arg) { |
| 25 | + switch (cmd) { |
| 26 | + case F_OFD_SETLKW: { |
| 27 | + struct flock *flk = reinterpret_cast<struct flock *>(arg); |
| 28 | + // convert the struct to a flock64 |
| 29 | + struct flock64 flk64; |
| 30 | + flk64.l_type = flk->l_type; |
| 31 | + flk64.l_whence = flk->l_whence; |
| 32 | + flk64.l_start = flk->l_start; |
| 33 | + flk64.l_len = flk->l_len; |
| 34 | + flk64.l_pid = flk->l_pid; |
| 35 | + // create a syscall |
| 36 | + return LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, cmd, &flk64); |
| 37 | + } |
| 38 | + case F_OFD_GETLK: |
| 39 | + case F_OFD_SETLK: { |
| 40 | + struct flock *flk = reinterpret_cast<struct flock *>(arg); |
| 41 | + // convert the struct to a flock64 |
| 42 | + struct flock64 flk64; |
| 43 | + flk64.l_type = flk->l_type; |
| 44 | + flk64.l_whence = flk->l_whence; |
| 45 | + flk64.l_start = flk->l_start; |
| 46 | + flk64.l_len = flk->l_len; |
| 47 | + flk64.l_pid = flk->l_pid; |
| 48 | + // create a syscall |
| 49 | + int retVal = LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, cmd, &flk64); |
| 50 | + // On failure, return |
| 51 | + if (retVal == -1) |
| 52 | + return -1; |
| 53 | + // Check for overflow, i.e. the offsets are not the same when cast |
| 54 | + // to off_t from off64_t. |
| 55 | + if (static_cast<off_t>(flk64.l_len) != flk64.l_len || |
| 56 | + static_cast<off_t>(flk64.l_start) != flk64.l_start) { |
| 57 | + libc_errno = EOVERFLOW; |
| 58 | + return -1; |
| 59 | + } |
| 60 | + // Now copy back into flk, in case flk64 got modified |
| 61 | + flk->l_type = flk64.l_type; |
| 62 | + flk->l_whence = flk64.l_whence; |
| 63 | + flk->l_start = flk64.l_start; |
| 64 | + flk->l_len = flk64.l_len; |
| 65 | + flk->l_pid = flk64.l_pid; |
| 66 | + return retVal; |
| 67 | + } |
| 68 | + case F_GETOWN: { |
| 69 | + struct f_owner_ex fex; |
| 70 | + int retVal = |
| 71 | + LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex); |
| 72 | + if (retVal == -EINVAL) |
| 73 | + return LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, cmd, |
| 74 | + reinterpret_cast<void *>(arg)); |
| 75 | + if (static_cast<unsigned long>(retVal) <= -4096UL) |
| 76 | + return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid; |
| 77 | + |
| 78 | + libc_errno = -retVal; |
| 79 | + return -1; |
| 80 | + } |
| 81 | + // The general case |
| 82 | + default: { |
| 83 | + int retVal = LIBC_NAMESPACE::syscall_impl<int>( |
| 84 | + SYS_fcntl, fd, cmd, reinterpret_cast<void *>(arg)); |
| 85 | + if (retVal >= 0) { |
| 86 | + return retVal; |
| 87 | + } |
| 88 | + libc_errno = -retVal; |
| 89 | + return -1; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace LIBC_NAMESPACE::internal |
0 commit comments