| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [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 "ShellCallback" | 
 | 18 |  | 
| Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 19 | #include <unistd.h> | 
 | 20 | #include <fcntl.h> | 
 | 21 |  | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 22 | #include <binder/IShellCallback.h> | 
 | 23 |  | 
 | 24 | #include <utils/Log.h> | 
 | 25 | #include <binder/Parcel.h> | 
 | 26 | #include <utils/String8.h> | 
 | 27 |  | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 28 | namespace android { | 
 | 29 |  | 
 | 30 | // ---------------------------------------------------------------------- | 
 | 31 |  | 
 | 32 | class BpShellCallback : public BpInterface<IShellCallback> | 
 | 33 | { | 
 | 34 | public: | 
 | 35 |     explicit BpShellCallback(const sp<IBinder>& impl) | 
 | 36 |         : BpInterface<IShellCallback>(impl) | 
 | 37 |     { | 
 | 38 |     } | 
 | 39 |  | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 40 |     virtual int openFile(const String16& path, const String16& seLinuxContext, | 
 | 41 |             const String16& mode) { | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 42 |         Parcel data, reply; | 
 | 43 |         data.writeInterfaceToken(IShellCallback::getInterfaceDescriptor()); | 
 | 44 |         data.writeString16(path); | 
 | 45 |         data.writeString16(seLinuxContext); | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 46 |         data.writeString16(mode); | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 47 |         remote()->transact(OP_OPEN_OUTPUT_FILE, data, &reply, 0); | 
 | 48 |         reply.readExceptionCode(); | 
 | 49 |         int fd = reply.readParcelFileDescriptor(); | 
| Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 50 |         return fd >= 0 ? fcntl(fd, F_DUPFD_CLOEXEC, 0) : fd; | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 51 |  | 
 | 52 |     } | 
 | 53 | }; | 
 | 54 |  | 
 | 55 | IMPLEMENT_META_INTERFACE(ShellCallback, "com.android.internal.os.IShellCallback"); | 
 | 56 |  | 
 | 57 | // ---------------------------------------------------------------------- | 
 | 58 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 59 | // NOLINTNEXTLINE(google-default-arguments) | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 60 | status_t BnShellCallback::onTransact( | 
 | 61 |     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
 | 62 | { | 
 | 63 |     switch(code) { | 
 | 64 |         case OP_OPEN_OUTPUT_FILE: { | 
 | 65 |             CHECK_INTERFACE(IShellCallback, data, reply); | 
 | 66 |             String16 path(data.readString16()); | 
 | 67 |             String16 seLinuxContext(data.readString16()); | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 68 |             String16 mode(data.readString16()); | 
 | 69 |             int fd = openFile(path, seLinuxContext, mode); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 70 |             if (reply != nullptr) { | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 71 |                 reply->writeNoException(); | 
 | 72 |                 if (fd >= 0) { | 
 | 73 |                     reply->writeInt32(1); | 
 | 74 |                     reply->writeParcelFileDescriptor(fd, true); | 
 | 75 |                 } else { | 
 | 76 |                     reply->writeInt32(0); | 
 | 77 |                 } | 
 | 78 |             } else if (fd >= 0) { | 
 | 79 |                 close(fd); | 
 | 80 |             } | 
 | 81 |             return NO_ERROR; | 
 | 82 |         } break; | 
 | 83 |         default: | 
 | 84 |             return BBinder::onTransact(code, data, reply, flags); | 
 | 85 |     } | 
 | 86 | } | 
 | 87 |  | 
| Steven Moreland | 6511af5 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 88 | } // namespace android |