blob: 40dbbf551c0a20d2c98ffad62db4310e884aff5c [file] [log] [blame]
Dianne Hackborn23eb1e22015-10-07 17:35:27 -07001/*
2 * Copyright (C) 2015 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 "cmd"
18
19#include <utils/Log.h>
20#include <binder/Parcel.h>
21#include <binder/ProcessState.h>
22#include <binder/IResultReceiver.h>
23#include <binder/IServiceManager.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070024#include <binder/IShellCallback.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070025#include <binder/TextOutput.h>
Dianne Hackborn3d9eb952016-10-17 17:40:47 -070026#include <utils/Condition.h>
27#include <utils/Mutex.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070028#include <utils/Vector.h>
29
30#include <getopt.h>
31#include <stdlib.h>
32#include <stdio.h>
33#include <string.h>
34#include <unistd.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070035#include <fcntl.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070036#include <sys/time.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070037#include <errno.h>
Jiyong Park4e7d18a2017-08-07 20:48:32 +090038#include <memory>
Dianne Hackborn1941a402016-08-29 12:30:43 -070039
40#include "selinux/selinux.h"
41#include "selinux/android.h"
42
Dianne Hackborn3d9eb952016-10-17 17:40:47 -070043#define DEBUG 0
44
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070045using namespace android;
46
47static int sort_func(const String16* lhs, const String16* rhs)
48{
49 return lhs->compare(*rhs);
50}
51
Dianne Hackborn1941a402016-08-29 12:30:43 -070052struct SecurityContext_Delete {
53 void operator()(security_context_t p) const {
54 freecon(p);
55 }
56};
Jiyong Park4e7d18a2017-08-07 20:48:32 +090057typedef std::unique_ptr<char[], SecurityContext_Delete> Unique_SecurityContext;
Dianne Hackborn1941a402016-08-29 12:30:43 -070058
59class MyShellCallback : public BnShellCallback
60{
61public:
Dianne Hackborne5ed1992016-10-10 16:35:45 -070062 bool mActive = true;
63
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070064 virtual int openFile(const String16& path, const String16& seLinuxContext,
65 const String16& mode) {
Dianne Hackborn1941a402016-08-29 12:30:43 -070066 String8 path8(path);
67 char cwd[256];
68 getcwd(cwd, 256);
69 String8 fullPath(cwd);
70 fullPath.appendPath(path8);
Dianne Hackborne5ed1992016-10-10 16:35:45 -070071 if (!mActive) {
72 aerr << "Open attempt after active for: " << fullPath << endl;
73 return -EPERM;
74 }
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070075 int flags = 0;
76 bool checkRead = false;
77 bool checkWrite = false;
78 if (mode == String16("w")) {
79 flags = O_WRONLY|O_CREAT|O_TRUNC;
80 checkWrite = true;
81 } else if (mode == String16("w+")) {
82 flags = O_RDWR|O_CREAT|O_TRUNC;
83 checkRead = checkWrite = true;
84 } else if (mode == String16("r")) {
85 flags = O_RDONLY;
86 checkRead = true;
87 } else if (mode == String16("r+")) {
88 flags = O_RDWR;
89 checkRead = checkWrite = true;
90 } else {
91 aerr << "Invalid mode requested: " << mode.string() << endl;
92 return -EINVAL;
93 }
94 int fd = open(fullPath.string(), flags, S_IRWXU|S_IRWXG);
Dianne Hackborn1941a402016-08-29 12:30:43 -070095 if (fd < 0) {
96 return fd;
97 }
98 if (is_selinux_enabled() && seLinuxContext.size() > 0) {
99 String8 seLinuxContext8(seLinuxContext);
100 security_context_t tmp = NULL;
101 int ret = getfilecon(fullPath.string(), &tmp);
102 Unique_SecurityContext context(tmp);
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700103 if (checkWrite) {
104 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
105 "file", "write", NULL);
106 if (accessGranted != 0) {
107 close(fd);
108 aerr << "System server has no access to write file context " << context.get()
109 << " (from path " << fullPath.string() << ", context "
110 << seLinuxContext8.string() << ")" << endl;
111 return -EPERM;
112 }
113 }
114 if (checkRead) {
115 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
116 "file", "read", NULL);
117 if (accessGranted != 0) {
118 close(fd);
119 aerr << "System server has no access to read file context " << context.get()
120 << " (from path " << fullPath.string() << ", context "
121 << seLinuxContext8.string() << ")" << endl;
122 return -EPERM;
123 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700124 }
125 }
126 return fd;
127 }
128};
129
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700130class MyResultReceiver : public BnResultReceiver
131{
132public:
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700133 Mutex mMutex;
134 Condition mCondition;
135 bool mHaveResult = false;
136 int32_t mResult = 0;
137
138 virtual void send(int32_t resultCode) {
139 AutoMutex _l(mMutex);
140 mResult = resultCode;
141 mHaveResult = true;
142 mCondition.signal();
143 }
144
145 int32_t waitForResult() {
146 AutoMutex _l(mMutex);
147 while (!mHaveResult) {
148 mCondition.wait(mMutex);
149 }
150 return mResult;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700151 }
152};
153
154int main(int argc, char* const argv[])
155{
156 signal(SIGPIPE, SIG_IGN);
157 sp<ProcessState> proc = ProcessState::self();
Martijn Coenenc21bc9a2017-05-11 09:45:59 -0700158 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
159 // not allowed to spawn any additional threads, but we still spawn
160 // a binder thread from userspace when we call startThreadPool().
161 // This is safe because we only have 2 callbacks, neither of which
162 // block.
163 // See b/36066697 for rationale
164 proc->setThreadPoolMaxThreadCount(0);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700165 proc->startThreadPool();
166
167 sp<IServiceManager> sm = defaultServiceManager();
168 fflush(stdout);
169 if (sm == NULL) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700170 ALOGW("Unable to get default service manager!");
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700171 aerr << "cmd: Unable to get default service manager!" << endl;
172 return 20;
173 }
174
175 if (argc == 1) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700176 aerr << "cmd: No service specified; use -l to list all services" << endl;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700177 return 20;
178 }
179
180 if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) {
181 Vector<String16> services = sm->listServices();
182 services.sort(sort_func);
183 aout << "Currently running services:" << endl;
184
185 for (size_t i=0; i<services.size(); i++) {
186 sp<IBinder> service = sm->checkService(services[i]);
187 if (service != NULL) {
188 aout << " " << services[i] << endl;
189 }
190 }
191 return 0;
192 }
193
194 Vector<String16> args;
195 for (int i=2; i<argc; i++) {
196 args.add(String16(argv[i]));
197 }
198 String16 cmd = String16(argv[1]);
199 sp<IBinder> service = sm->checkService(cmd);
200 if (service == NULL) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700201 ALOGW("Can't find service %s", argv[1]);
202 aerr << "cmd: Can't find service: " << argv[1] << endl;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700203 return 20;
204 }
205
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700206 sp<MyShellCallback> cb = new MyShellCallback();
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700207 sp<MyResultReceiver> result = new MyResultReceiver();
208
209#if DEBUG
210 ALOGD("cmd: Invoking %s in=%d, out=%d, err=%d", argv[1], STDIN_FILENO, STDOUT_FILENO,
211 STDERR_FILENO);
212#endif
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700213
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700214 // TODO: block until a result is returned to MyResultReceiver.
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700215 status_t err = IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args,
216 cb, result);
217 if (err < 0) {
218 const char* errstr;
219 switch (err) {
220 case BAD_TYPE: errstr = "Bad type"; break;
221 case FAILED_TRANSACTION: errstr = "Failed transaction"; break;
222 case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break;
223 case UNEXPECTED_NULL: errstr = "Unexpected null"; break;
224 default: errstr = strerror(-err); break;
225 }
226 ALOGW("Failure calling service %s: %s (%d)", argv[1], errstr, -err);
227 aout << "cmd: Failure calling service " << argv[1] << ": " << errstr << " ("
228 << (-err) << ")" << endl;
229 return err;
230 }
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700231
232 cb->mActive = false;
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700233 status_t res = result->waitForResult();
234#if DEBUG
235 ALOGD("result=%d", (int)res);
236#endif
237 return res;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700238}