xref: /aosp_15_r20/external/llvm-libc/src/unistd/linux/dup2.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Linux implementation of dup2 --------------------------------------===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker 
9*71db0c75SAndroid Build Coastguard Worker #include "src/unistd/dup2.h"
10*71db0c75SAndroid Build Coastguard Worker 
11*71db0c75SAndroid Build Coastguard Worker #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12*71db0c75SAndroid Build Coastguard Worker #include "src/__support/common.h"
13*71db0c75SAndroid Build Coastguard Worker 
14*71db0c75SAndroid Build Coastguard Worker #include "hdr/fcntl_macros.h"
15*71db0c75SAndroid Build Coastguard Worker #include "src/__support/macros/config.h"
16*71db0c75SAndroid Build Coastguard Worker #include "src/errno/libc_errno.h"
17*71db0c75SAndroid Build Coastguard Worker #include <sys/syscall.h> // For syscall numbers.
18*71db0c75SAndroid Build Coastguard Worker 
19*71db0c75SAndroid Build Coastguard Worker namespace LIBC_NAMESPACE_DECL {
20*71db0c75SAndroid Build Coastguard Worker 
21*71db0c75SAndroid Build Coastguard Worker LLVM_LIBC_FUNCTION(int, dup2, (int oldfd, int newfd)) {
22*71db0c75SAndroid Build Coastguard Worker #ifdef SYS_dup2
23*71db0c75SAndroid Build Coastguard Worker   // If dup2 syscall is available, we make use of directly.
24*71db0c75SAndroid Build Coastguard Worker   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_dup2, oldfd, newfd);
25*71db0c75SAndroid Build Coastguard Worker #elif defined(SYS_dup3)
26*71db0c75SAndroid Build Coastguard Worker   // If dup2 syscall is not available, we try using the dup3 syscall. However,
27*71db0c75SAndroid Build Coastguard Worker   // dup3 fails if oldfd is the same as newfd. So, we handle that case
28*71db0c75SAndroid Build Coastguard Worker   // separately before making the dup3 syscall.
29*71db0c75SAndroid Build Coastguard Worker   if (oldfd == newfd) {
30*71db0c75SAndroid Build Coastguard Worker     // Check if oldfd is actually a valid file descriptor.
31*71db0c75SAndroid Build Coastguard Worker #if SYS_fcntl
32*71db0c75SAndroid Build Coastguard Worker     int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, oldfd, F_GETFD);
33*71db0c75SAndroid Build Coastguard Worker #elif defined(SYS_fcntl64)
34*71db0c75SAndroid Build Coastguard Worker     // Same as fcntl but can handle large offsets
35*71db0c75SAndroid Build Coastguard Worker     static_assert(sizeof(off_t) == 8);
36*71db0c75SAndroid Build Coastguard Worker     int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl64, oldfd, F_GETFD);
37*71db0c75SAndroid Build Coastguard Worker #else
38*71db0c75SAndroid Build Coastguard Worker #error "SYS_fcntl and SYS_fcntl64 syscalls not available."
39*71db0c75SAndroid Build Coastguard Worker #endif
40*71db0c75SAndroid Build Coastguard Worker     if (ret >= 0)
41*71db0c75SAndroid Build Coastguard Worker       return oldfd;
42*71db0c75SAndroid Build Coastguard Worker     libc_errno = -ret;
43*71db0c75SAndroid Build Coastguard Worker     return -1;
44*71db0c75SAndroid Build Coastguard Worker   }
45*71db0c75SAndroid Build Coastguard Worker   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_dup3, oldfd, newfd, 0);
46*71db0c75SAndroid Build Coastguard Worker #else
47*71db0c75SAndroid Build Coastguard Worker #error "dup2 and dup3 syscalls not available."
48*71db0c75SAndroid Build Coastguard Worker #endif
49*71db0c75SAndroid Build Coastguard Worker   if (ret < 0) {
50*71db0c75SAndroid Build Coastguard Worker     libc_errno = -ret;
51*71db0c75SAndroid Build Coastguard Worker     return -1;
52*71db0c75SAndroid Build Coastguard Worker   }
53*71db0c75SAndroid Build Coastguard Worker   return ret;
54*71db0c75SAndroid Build Coastguard Worker }
55*71db0c75SAndroid Build Coastguard Worker 
56*71db0c75SAndroid Build Coastguard Worker } // namespace LIBC_NAMESPACE_DECL
57