blob: 443511e7eaab7d2f0009cbcdcf2d585666e6ac4d [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>
26#include <utils/Vector.h>
27
28#include <getopt.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include <unistd.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070033#include <fcntl.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070034#include <sys/time.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070035#include <errno.h>
36
37#include "selinux/selinux.h"
38#include "selinux/android.h"
39
40#include <UniquePtr.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070041
42using namespace android;
43
44static int sort_func(const String16* lhs, const String16* rhs)
45{
46 return lhs->compare(*rhs);
47}
48
Dianne Hackborn1941a402016-08-29 12:30:43 -070049struct SecurityContext_Delete {
50 void operator()(security_context_t p) const {
51 freecon(p);
52 }
53};
54typedef UniquePtr<char[], SecurityContext_Delete> Unique_SecurityContext;
55
56class MyShellCallback : public BnShellCallback
57{
58public:
Dianne Hackborne5ed1992016-10-10 16:35:45 -070059 bool mActive = true;
60
Dianne Hackborn1941a402016-08-29 12:30:43 -070061 virtual int openOutputFile(const String16& path, const String16& seLinuxContext) {
62 String8 path8(path);
63 char cwd[256];
64 getcwd(cwd, 256);
65 String8 fullPath(cwd);
66 fullPath.appendPath(path8);
Dianne Hackborne5ed1992016-10-10 16:35:45 -070067 if (!mActive) {
68 aerr << "Open attempt after active for: " << fullPath << endl;
69 return -EPERM;
70 }
Dianne Hackborn1941a402016-08-29 12:30:43 -070071 int fd = open(fullPath.string(), O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG);
72 if (fd < 0) {
73 return fd;
74 }
75 if (is_selinux_enabled() && seLinuxContext.size() > 0) {
76 String8 seLinuxContext8(seLinuxContext);
77 security_context_t tmp = NULL;
78 int ret = getfilecon(fullPath.string(), &tmp);
79 Unique_SecurityContext context(tmp);
80 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
81 "file", "write", NULL);
82 if (accessGranted != 0) {
83 close(fd);
84 aerr << "System server has no access to file context " << context.get()
85 << " (from path " << fullPath.string() << ", context "
86 << seLinuxContext8.string() << ")" << endl;
87 return -EPERM;
88 }
89 }
90 return fd;
91 }
92};
93
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070094class MyResultReceiver : public BnResultReceiver
95{
96public:
97 virtual void send(int32_t /*resultCode*/) {
98 }
99};
100
101int main(int argc, char* const argv[])
102{
103 signal(SIGPIPE, SIG_IGN);
104 sp<ProcessState> proc = ProcessState::self();
105 proc->startThreadPool();
106
107 sp<IServiceManager> sm = defaultServiceManager();
108 fflush(stdout);
109 if (sm == NULL) {
110 ALOGE("Unable to get default service manager!");
111 aerr << "cmd: Unable to get default service manager!" << endl;
112 return 20;
113 }
114
115 if (argc == 1) {
116 aout << "cmd: no service specified; use -l to list all services" << endl;
117 return 20;
118 }
119
120 if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) {
121 Vector<String16> services = sm->listServices();
122 services.sort(sort_func);
123 aout << "Currently running services:" << endl;
124
125 for (size_t i=0; i<services.size(); i++) {
126 sp<IBinder> service = sm->checkService(services[i]);
127 if (service != NULL) {
128 aout << " " << services[i] << endl;
129 }
130 }
131 return 0;
132 }
133
134 Vector<String16> args;
135 for (int i=2; i<argc; i++) {
136 args.add(String16(argv[i]));
137 }
138 String16 cmd = String16(argv[1]);
139 sp<IBinder> service = sm->checkService(cmd);
140 if (service == NULL) {
141 aerr << "Can't find service: " << argv[1] << endl;
142 return 20;
143 }
144
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700145 sp<MyShellCallback> cb = new MyShellCallback();
146
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700147 // TODO: block until a result is returned to MyResultReceiver.
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700148 IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args,
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700149 cb, new MyResultReceiver());
150
151 cb->mActive = false;
152
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700153 return 0;
154}