Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define LOG_TAG "DropBoxManager" |
| 18 | |
| 19 | #include <android/os/DropBoxManager.h> |
| 20 | |
Jeff Sharkey | 8eebdbe | 2021-01-11 12:40:49 -0700 | [diff] [blame] | 21 | #include <android-base/unique_fd.h> |
Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 22 | #include <binder/IServiceManager.h> |
Jeff Sharkey | 8eebdbe | 2021-01-11 12:40:49 -0700 | [diff] [blame] | 23 | #include <binder/ParcelFileDescriptor.h> |
Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 24 | #include <com/android/internal/os/IDropBoxManagerService.h> |
| 25 | #include <cutils/log.h> |
| 26 | |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <fcntl.h> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace os { |
| 33 | |
| 34 | using namespace ::com::android::internal::os; |
| 35 | |
| 36 | DropBoxManager::Entry::Entry() |
| 37 | :mTag(), |
| 38 | mTimeMillis(0), |
| 39 | mFlags(IS_EMPTY), |
| 40 | mData(), |
| 41 | mFd() |
| 42 | { |
| 43 | mFlags = IS_EMPTY; |
| 44 | } |
| 45 | |
| 46 | DropBoxManager::Entry::Entry(const String16& tag, int32_t flags) |
| 47 | :mTag(tag), |
| 48 | mTimeMillis(0), |
| 49 | mFlags(flags), |
| 50 | mData(), |
| 51 | mFd() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | DropBoxManager::Entry::Entry(const String16& tag, int32_t flags, int fd) |
| 56 | :mTag(tag), |
| 57 | mTimeMillis(0), |
| 58 | mFlags(flags), |
| 59 | mData(), |
| 60 | mFd(fd) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | DropBoxManager::Entry::~Entry() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | status_t |
| 69 | DropBoxManager::Entry::writeToParcel(Parcel* out) const |
| 70 | { |
| 71 | status_t err; |
| 72 | |
| 73 | err = out->writeString16(mTag); |
| 74 | if (err != NO_ERROR) { |
| 75 | return err; |
| 76 | } |
| 77 | |
| 78 | err = out->writeInt64(mTimeMillis); |
| 79 | if (err != NO_ERROR) { |
| 80 | return err; |
| 81 | } |
| 82 | |
| 83 | if (mFd.get() != -1) { |
| 84 | err = out->writeInt32(mFlags & ~HAS_BYTE_ARRAY); // Clear bit just to be safe |
| 85 | if (err != NO_ERROR) { |
| 86 | return err; |
| 87 | } |
| 88 | ALOGD("writing fd %d\n", mFd.get()); |
| 89 | err = out->writeParcelFileDescriptor(mFd); |
| 90 | if (err != NO_ERROR) { |
| 91 | return err; |
| 92 | } |
| 93 | } else { |
| 94 | err = out->writeInt32(mFlags | HAS_BYTE_ARRAY); |
| 95 | if (err != NO_ERROR) { |
| 96 | return err; |
| 97 | } |
| 98 | err = out->writeByteVector(mData); |
| 99 | if (err != NO_ERROR) { |
| 100 | return err; |
| 101 | } |
| 102 | } |
| 103 | return NO_ERROR; |
| 104 | } |
| 105 | |
| 106 | status_t |
| 107 | DropBoxManager::Entry::readFromParcel(const Parcel* in) |
| 108 | { |
| 109 | status_t err; |
| 110 | |
| 111 | err = in->readString16(&mTag); |
| 112 | if (err != NO_ERROR) { |
| 113 | return err; |
| 114 | } |
| 115 | |
| 116 | err = in->readInt64(&mTimeMillis); |
| 117 | if (err != NO_ERROR) { |
| 118 | return err; |
| 119 | } |
| 120 | |
| 121 | err = in->readInt32(&mFlags); |
| 122 | if (err != NO_ERROR) { |
| 123 | return err; |
| 124 | } |
| 125 | |
| 126 | if ((mFlags & HAS_BYTE_ARRAY) != 0) { |
| 127 | err = in->readByteVector(&mData); |
| 128 | if (err != NO_ERROR) { |
| 129 | return err; |
| 130 | } |
| 131 | mFlags &= ~HAS_BYTE_ARRAY; |
| 132 | } else { |
| 133 | int fd; |
| 134 | fd = in->readParcelFileDescriptor(); |
| 135 | if (fd == -1) { |
| 136 | return EBADF; |
| 137 | } |
| 138 | fd = dup(fd); |
| 139 | if (fd == -1) { |
| 140 | return errno; |
| 141 | } |
| 142 | mFd.reset(fd); |
| 143 | } |
| 144 | |
| 145 | return NO_ERROR; |
| 146 | } |
| 147 | |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 148 | const vector<uint8_t>& |
| 149 | DropBoxManager::Entry::getData() const |
| 150 | { |
| 151 | return mData; |
| 152 | } |
| 153 | |
| 154 | const unique_fd& |
| 155 | DropBoxManager::Entry::getFd() const |
| 156 | { |
| 157 | return mFd; |
| 158 | } |
| 159 | |
| 160 | int32_t |
| 161 | DropBoxManager::Entry::getFlags() const |
| 162 | { |
| 163 | return mFlags; |
| 164 | } |
| 165 | |
| 166 | int64_t |
| 167 | DropBoxManager::Entry::getTimestamp() const |
| 168 | { |
| 169 | return mTimeMillis; |
| 170 | } |
Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 171 | |
| 172 | DropBoxManager::DropBoxManager() |
| 173 | { |
| 174 | } |
| 175 | |
| 176 | DropBoxManager::~DropBoxManager() |
| 177 | { |
| 178 | } |
| 179 | |
| 180 | Status |
| 181 | DropBoxManager::addText(const String16& tag, const string& text) |
| 182 | { |
Jeff Sharkey | 8eebdbe | 2021-01-11 12:40:49 -0700 | [diff] [blame] | 183 | return addData(tag, reinterpret_cast<uint8_t const*>(text.c_str()), text.size(), IS_TEXT); |
Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | Status |
| 187 | DropBoxManager::addData(const String16& tag, uint8_t const* data, |
| 188 | size_t size, int flags) |
| 189 | { |
Jeff Sharkey | 8eebdbe | 2021-01-11 12:40:49 -0700 | [diff] [blame] | 190 | sp<IDropBoxManagerService> service = interface_cast<IDropBoxManagerService>( |
| 191 | defaultServiceManager()->getService(android::String16("dropbox"))); |
| 192 | if (service == NULL) { |
| 193 | return Status::fromExceptionCode(Status::EX_NULL_POINTER, "can't find dropbox service"); |
| 194 | } |
| 195 | ALOGD("About to call service->add()"); |
| 196 | vector<uint8_t> dataArg; |
| 197 | dataArg.assign(data, data + size); |
| 198 | Status status = service->addData(tag, dataArg, flags); |
Tomasz Wasilczyk | 3815d34 | 2023-08-10 23:54:44 +0000 | [diff] [blame^] | 199 | ALOGD("service->add returned %s", status.toString8().c_str()); |
Jeff Sharkey | 8eebdbe | 2021-01-11 12:40:49 -0700 | [diff] [blame] | 200 | return status; |
Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | Status |
| 204 | DropBoxManager::addFile(const String16& tag, const string& filename, int flags) |
| 205 | { |
| 206 | int fd = open(filename.c_str(), O_RDONLY); |
| 207 | if (fd == -1) { |
| 208 | string message("addFile can't open file: "); |
| 209 | message += filename; |
| 210 | ALOGW("DropboxManager: %s", message.c_str()); |
| 211 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, message.c_str()); |
| 212 | } |
Sami Kyostila | 52c866e | 2018-01-12 18:28:13 +0000 | [diff] [blame] | 213 | return addFile(tag, fd, flags); |
| 214 | } |
Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 215 | |
Sami Kyostila | 52c866e | 2018-01-12 18:28:13 +0000 | [diff] [blame] | 216 | Status |
| 217 | DropBoxManager::addFile(const String16& tag, int fd, int flags) |
| 218 | { |
Primiano Tucci | 3729389 | 2018-01-26 17:28:58 +0000 | [diff] [blame] | 219 | if (fd == -1) { |
| 220 | string message("invalid fd (-1) passed to to addFile"); |
| 221 | ALOGW("DropboxManager: %s", message.c_str()); |
| 222 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, message.c_str()); |
| 223 | } |
Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 224 | sp<IDropBoxManagerService> service = interface_cast<IDropBoxManagerService>( |
| 225 | defaultServiceManager()->getService(android::String16("dropbox"))); |
| 226 | if (service == NULL) { |
| 227 | return Status::fromExceptionCode(Status::EX_NULL_POINTER, "can't find dropbox service"); |
| 228 | } |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 229 | ALOGD("About to call service->add()"); |
Jeff Sharkey | 8eebdbe | 2021-01-11 12:40:49 -0700 | [diff] [blame] | 230 | android::base::unique_fd uniqueFd(fd); |
| 231 | android::os::ParcelFileDescriptor parcelFd(std::move(uniqueFd)); |
| 232 | Status status = service->addFile(tag, parcelFd, flags); |
Tomasz Wasilczyk | 3815d34 | 2023-08-10 23:54:44 +0000 | [diff] [blame^] | 233 | ALOGD("service->add returned %s", status.toString8().c_str()); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 234 | return status; |
Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Joe Onorato | de5b027d | 2016-11-23 16:16:12 -0800 | [diff] [blame] | 237 | }} // namespace android::os |