blob: 014c99593c5345a3a58b0f9cfc758b33adbe99a9 [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 Hackborn1941a402016-08-29 12:30:43 -070064 virtual int openOutputFile(const String16& path, const String16& seLinuxContext) {
65 String8 path8(path);
66 char cwd[256];
67 getcwd(cwd, 256);
68 String8 fullPath(cwd);
69 fullPath.appendPath(path8);
Dianne Hackborne5ed1992016-10-10 16:35:45 -070070 if (!mActive) {
71 aerr << "Open attempt after active for: " << fullPath << endl;
72 return -EPERM;
73 }
Dianne Hackborn1941a402016-08-29 12:30:43 -070074 int fd = open(fullPath.string(), O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG);
75 if (fd < 0) {
76 return fd;
77 }
78 if (is_selinux_enabled() && seLinuxContext.size() > 0) {
79 String8 seLinuxContext8(seLinuxContext);
80 security_context_t tmp = NULL;
81 int ret = getfilecon(fullPath.string(), &tmp);
82 Unique_SecurityContext context(tmp);
83 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
84 "file", "write", NULL);
85 if (accessGranted != 0) {
86 close(fd);
87 aerr << "System server has no access to file context " << context.get()
88 << " (from path " << fullPath.string() << ", context "
89 << seLinuxContext8.string() << ")" << endl;
90 return -EPERM;
91 }
92 }
93 return fd;
94 }
95};
96
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070097class MyResultReceiver : public BnResultReceiver
98{
99public:
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700100 Mutex mMutex;
101 Condition mCondition;
102 bool mHaveResult = false;
103 int32_t mResult = 0;
104
105 virtual void send(int32_t resultCode) {
106 AutoMutex _l(mMutex);
107 mResult = resultCode;
108 mHaveResult = true;
109 mCondition.signal();
110 }
111
112 int32_t waitForResult() {
113 AutoMutex _l(mMutex);
114 while (!mHaveResult) {
115 mCondition.wait(mMutex);
116 }
117 return mResult;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700118 }
119};
120
121int main(int argc, char* const argv[])
122{
123 signal(SIGPIPE, SIG_IGN);
124 sp<ProcessState> proc = ProcessState::self();
Martijn Coenenc21bc9a2017-05-11 09:45:59 -0700125 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
126 // not allowed to spawn any additional threads, but we still spawn
127 // a binder thread from userspace when we call startThreadPool().
128 // This is safe because we only have 2 callbacks, neither of which
129 // block.
130 // See b/36066697 for rationale
131 proc->setThreadPoolMaxThreadCount(0);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700132 proc->startThreadPool();
133
134 sp<IServiceManager> sm = defaultServiceManager();
135 fflush(stdout);
136 if (sm == NULL) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700137 ALOGW("Unable to get default service manager!");
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700138 aerr << "cmd: Unable to get default service manager!" << endl;
139 return 20;
140 }
141
142 if (argc == 1) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700143 aerr << "cmd: No service specified; use -l to list all services" << endl;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700144 return 20;
145 }
146
147 if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) {
148 Vector<String16> services = sm->listServices();
149 services.sort(sort_func);
150 aout << "Currently running services:" << endl;
151
152 for (size_t i=0; i<services.size(); i++) {
153 sp<IBinder> service = sm->checkService(services[i]);
154 if (service != NULL) {
155 aout << " " << services[i] << endl;
156 }
157 }
158 return 0;
159 }
160
161 Vector<String16> args;
162 for (int i=2; i<argc; i++) {
163 args.add(String16(argv[i]));
164 }
165 String16 cmd = String16(argv[1]);
166 sp<IBinder> service = sm->checkService(cmd);
167 if (service == NULL) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700168 ALOGW("Can't find service %s", argv[1]);
169 aerr << "cmd: Can't find service: " << argv[1] << endl;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700170 return 20;
171 }
172
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700173 sp<MyShellCallback> cb = new MyShellCallback();
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700174 sp<MyResultReceiver> result = new MyResultReceiver();
175
176#if DEBUG
177 ALOGD("cmd: Invoking %s in=%d, out=%d, err=%d", argv[1], STDIN_FILENO, STDOUT_FILENO,
178 STDERR_FILENO);
179#endif
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700180
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700181 // TODO: block until a result is returned to MyResultReceiver.
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700182 status_t err = IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args,
183 cb, result);
184 if (err < 0) {
185 const char* errstr;
186 switch (err) {
187 case BAD_TYPE: errstr = "Bad type"; break;
188 case FAILED_TRANSACTION: errstr = "Failed transaction"; break;
189 case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break;
190 case UNEXPECTED_NULL: errstr = "Unexpected null"; break;
191 default: errstr = strerror(-err); break;
192 }
193 ALOGW("Failure calling service %s: %s (%d)", argv[1], errstr, -err);
194 aout << "cmd: Failure calling service " << argv[1] << ": " << errstr << " ("
195 << (-err) << ")" << endl;
196 return err;
197 }
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700198
199 cb->mActive = false;
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700200 status_t res = result->waitForResult();
201#if DEBUG
202 ALOGD("result=%d", (int)res);
203#endif
204 return res;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700205}