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