1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <memory>
18 
19 #include "android-base/cmsg.h"
20 #include "android-base/file.h"
21 #include "android-base/mapped_file.h"
22 #include "android-base/unique_fd.h"
23 
24 namespace android {
25 namespace base {
26 
27 // These ABI-compatibility shims are in a separate file for two reasons:
28 //   1. If they were in the file with the actual functions, it prevents calls to
29 //      those functions by other functions in the file, due to ambiguity.
30 //   2. We will hopefully be able to delete these quickly.
31 
32 #if !defined(_WIN32)
SendFileDescriptorVector(int sockfd,const void * data,size_t len,const std::vector<int> & fds)33 ssize_t SendFileDescriptorVector(int sockfd, const void* data, size_t len,
34                                  const std::vector<int>& fds) {
35   return SendFileDescriptorVector(borrowed_fd(sockfd), data, len, fds);
36 }
37 
ReceiveFileDescriptorVector(int sockfd,void * data,size_t len,size_t max_fds,std::vector<unique_fd> * fds)38 ssize_t ReceiveFileDescriptorVector(int sockfd, void* data, size_t len, size_t max_fds,
39                                     std::vector<unique_fd>* fds) {
40   return ReceiveFileDescriptorVector(borrowed_fd(sockfd), data, len, max_fds, fds);
41 }
42 #endif
43 
ReadFdToString(int fd,std::string * content)44 bool ReadFdToString(int fd, std::string* content) {
45   return ReadFdToString(borrowed_fd(fd), content);
46 }
47 
WriteStringToFd(const std::string & content,int fd)48 bool WriteStringToFd(const std::string& content, int fd) {
49   return WriteStringToFd(content, borrowed_fd(fd));
50 }
51 
ReadFully(int fd,void * data,size_t byte_count)52 bool ReadFully(int fd, void* data, size_t byte_count) {
53   return ReadFully(borrowed_fd(fd), data, byte_count);
54 }
55 
ReadFullyAtOffset(int fd,void * data,size_t byte_count,off64_t offset)56 bool ReadFullyAtOffset(int fd, void* data, size_t byte_count, off64_t offset) {
57   return ReadFullyAtOffset(borrowed_fd(fd), data, byte_count, offset);
58 }
59 
WriteFully(int fd,const void * data,size_t byte_count)60 bool WriteFully(int fd, const void* data, size_t byte_count) {
61   return WriteFully(borrowed_fd(fd), data, byte_count);
62 }
63 
64 #if defined(__LP64__)
65 #define MAPPEDFILE_FROMFD _ZN10MappedFile6FromFdEilmi
66 #else
67 #define MAPPEDFILE_FROMFD _ZN10MappedFile6FromFdEixmi
68 #endif
69 
70 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
MAPPEDFILE_FROMFD(int fd,off64_t offset,size_t length,int prot)71 extern "C" std::unique_ptr<MappedFile> MAPPEDFILE_FROMFD(int fd, off64_t offset, size_t length,
72                                                          int prot) {
73   return MappedFile::FromFd(fd, offset, length, prot);
74 }
75 
76 }  // namespace base
77 }  // namespace android
78