xref: /aosp_15_r20/frameworks/native/libs/binder/IShellCallback.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 
19 #include <unistd.h>
20 #include <fcntl.h>
21 
22 #include <binder/IShellCallback.h>
23 
24 #include <binder/Parcel.h>
25 #include <utils/String8.h>
26 
27 namespace android {
28 
29 // ----------------------------------------------------------------------
30 
31 class BpShellCallback : public BpInterface<IShellCallback>
32 {
33 public:
BpShellCallback(const sp<IBinder> & impl)34     explicit BpShellCallback(const sp<IBinder>& impl)
35         : BpInterface<IShellCallback>(impl)
36     {
37     }
38 
openFile(const String16 & path,const String16 & seLinuxContext,const String16 & mode)39     virtual int openFile(const String16& path, const String16& seLinuxContext,
40             const String16& mode) {
41         Parcel data, reply;
42         data.writeInterfaceToken(IShellCallback::getInterfaceDescriptor());
43         data.writeString16(path);
44         data.writeString16(seLinuxContext);
45         data.writeString16(mode);
46         remote()->transact(OP_OPEN_OUTPUT_FILE, data, &reply, 0);
47         reply.readExceptionCode();
48         int fd = reply.readParcelFileDescriptor();
49         return fd >= 0 ? fcntl(fd, F_DUPFD_CLOEXEC, 0) : fd;
50 
51     }
52 };
53 
54 IMPLEMENT_META_INTERFACE(ShellCallback, "com.android.internal.os.IShellCallback")
55 
56 // ----------------------------------------------------------------------
57 
58 // NOLINTNEXTLINE(google-default-arguments)
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)59 status_t BnShellCallback::onTransact(
60     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
61 {
62     switch(code) {
63         case OP_OPEN_OUTPUT_FILE: {
64             CHECK_INTERFACE(IShellCallback, data, reply);
65             String16 path(data.readString16());
66             String16 seLinuxContext(data.readString16());
67             String16 mode(data.readString16());
68             int fd = openFile(path, seLinuxContext, mode);
69             if (reply != nullptr) {
70                 reply->writeNoException();
71                 if (fd >= 0) {
72                     reply->writeInt32(1);
73                     reply->writeParcelFileDescriptor(fd, true);
74                 } else {
75                     reply->writeInt32(0);
76                 }
77             } else if (fd >= 0) {
78                 close(fd);
79             }
80             return NO_ERROR;
81         } break;
82         default:
83             return BBinder::onTransact(code, data, reply, flags);
84     }
85 }
86 
87 } // namespace android
88