xref: /aosp_15_r20/frameworks/native/libs/binder/IShellCallback.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "ShellCallback"
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <unistd.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <fcntl.h>
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker #include <binder/IShellCallback.h>
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker #include <binder/Parcel.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <utils/String8.h>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker namespace android {
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker // ----------------------------------------------------------------------
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker class BpShellCallback : public BpInterface<IShellCallback>
32*38e8c45fSAndroid Build Coastguard Worker {
33*38e8c45fSAndroid Build Coastguard Worker public:
BpShellCallback(const sp<IBinder> & impl)34*38e8c45fSAndroid Build Coastguard Worker     explicit BpShellCallback(const sp<IBinder>& impl)
35*38e8c45fSAndroid Build Coastguard Worker         : BpInterface<IShellCallback>(impl)
36*38e8c45fSAndroid Build Coastguard Worker     {
37*38e8c45fSAndroid Build Coastguard Worker     }
38*38e8c45fSAndroid Build Coastguard Worker 
openFile(const String16 & path,const String16 & seLinuxContext,const String16 & mode)39*38e8c45fSAndroid Build Coastguard Worker     virtual int openFile(const String16& path, const String16& seLinuxContext,
40*38e8c45fSAndroid Build Coastguard Worker             const String16& mode) {
41*38e8c45fSAndroid Build Coastguard Worker         Parcel data, reply;
42*38e8c45fSAndroid Build Coastguard Worker         data.writeInterfaceToken(IShellCallback::getInterfaceDescriptor());
43*38e8c45fSAndroid Build Coastguard Worker         data.writeString16(path);
44*38e8c45fSAndroid Build Coastguard Worker         data.writeString16(seLinuxContext);
45*38e8c45fSAndroid Build Coastguard Worker         data.writeString16(mode);
46*38e8c45fSAndroid Build Coastguard Worker         remote()->transact(OP_OPEN_OUTPUT_FILE, data, &reply, 0);
47*38e8c45fSAndroid Build Coastguard Worker         reply.readExceptionCode();
48*38e8c45fSAndroid Build Coastguard Worker         int fd = reply.readParcelFileDescriptor();
49*38e8c45fSAndroid Build Coastguard Worker         return fd >= 0 ? fcntl(fd, F_DUPFD_CLOEXEC, 0) : fd;
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker     }
52*38e8c45fSAndroid Build Coastguard Worker };
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker IMPLEMENT_META_INTERFACE(ShellCallback, "com.android.internal.os.IShellCallback")
55*38e8c45fSAndroid Build Coastguard Worker 
56*38e8c45fSAndroid Build Coastguard Worker // ----------------------------------------------------------------------
57*38e8c45fSAndroid Build Coastguard Worker 
58*38e8c45fSAndroid Build Coastguard Worker // NOLINTNEXTLINE(google-default-arguments)
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)59*38e8c45fSAndroid Build Coastguard Worker status_t BnShellCallback::onTransact(
60*38e8c45fSAndroid Build Coastguard Worker     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
61*38e8c45fSAndroid Build Coastguard Worker {
62*38e8c45fSAndroid Build Coastguard Worker     switch(code) {
63*38e8c45fSAndroid Build Coastguard Worker         case OP_OPEN_OUTPUT_FILE: {
64*38e8c45fSAndroid Build Coastguard Worker             CHECK_INTERFACE(IShellCallback, data, reply);
65*38e8c45fSAndroid Build Coastguard Worker             String16 path(data.readString16());
66*38e8c45fSAndroid Build Coastguard Worker             String16 seLinuxContext(data.readString16());
67*38e8c45fSAndroid Build Coastguard Worker             String16 mode(data.readString16());
68*38e8c45fSAndroid Build Coastguard Worker             int fd = openFile(path, seLinuxContext, mode);
69*38e8c45fSAndroid Build Coastguard Worker             if (reply != nullptr) {
70*38e8c45fSAndroid Build Coastguard Worker                 reply->writeNoException();
71*38e8c45fSAndroid Build Coastguard Worker                 if (fd >= 0) {
72*38e8c45fSAndroid Build Coastguard Worker                     reply->writeInt32(1);
73*38e8c45fSAndroid Build Coastguard Worker                     reply->writeParcelFileDescriptor(fd, true);
74*38e8c45fSAndroid Build Coastguard Worker                 } else {
75*38e8c45fSAndroid Build Coastguard Worker                     reply->writeInt32(0);
76*38e8c45fSAndroid Build Coastguard Worker                 }
77*38e8c45fSAndroid Build Coastguard Worker             } else if (fd >= 0) {
78*38e8c45fSAndroid Build Coastguard Worker                 close(fd);
79*38e8c45fSAndroid Build Coastguard Worker             }
80*38e8c45fSAndroid Build Coastguard Worker             return NO_ERROR;
81*38e8c45fSAndroid Build Coastguard Worker         } break;
82*38e8c45fSAndroid Build Coastguard Worker         default:
83*38e8c45fSAndroid Build Coastguard Worker             return BBinder::onTransact(code, data, reply, flags);
84*38e8c45fSAndroid Build Coastguard Worker     }
85*38e8c45fSAndroid Build Coastguard Worker }
86*38e8c45fSAndroid Build Coastguard Worker 
87*38e8c45fSAndroid Build Coastguard Worker } // namespace android
88