blob: 35700b427d8d579ec15394deecda2d1fb648ccfa [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:
59 virtual int openOutputFile(const String16& path, const String16& seLinuxContext) {
60 String8 path8(path);
61 char cwd[256];
62 getcwd(cwd, 256);
63 String8 fullPath(cwd);
64 fullPath.appendPath(path8);
65 int fd = open(fullPath.string(), O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG);
66 if (fd < 0) {
67 return fd;
68 }
69 if (is_selinux_enabled() && seLinuxContext.size() > 0) {
70 String8 seLinuxContext8(seLinuxContext);
71 security_context_t tmp = NULL;
72 int ret = getfilecon(fullPath.string(), &tmp);
73 Unique_SecurityContext context(tmp);
74 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
75 "file", "write", NULL);
76 if (accessGranted != 0) {
77 close(fd);
78 aerr << "System server has no access to file context " << context.get()
79 << " (from path " << fullPath.string() << ", context "
80 << seLinuxContext8.string() << ")" << endl;
81 return -EPERM;
82 }
83 }
84 return fd;
85 }
86};
87
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070088class MyResultReceiver : public BnResultReceiver
89{
90public:
91 virtual void send(int32_t /*resultCode*/) {
92 }
93};
94
95int main(int argc, char* const argv[])
96{
97 signal(SIGPIPE, SIG_IGN);
98 sp<ProcessState> proc = ProcessState::self();
99 proc->startThreadPool();
100
101 sp<IServiceManager> sm = defaultServiceManager();
102 fflush(stdout);
103 if (sm == NULL) {
104 ALOGE("Unable to get default service manager!");
105 aerr << "cmd: Unable to get default service manager!" << endl;
106 return 20;
107 }
108
109 if (argc == 1) {
110 aout << "cmd: no service specified; use -l to list all services" << endl;
111 return 20;
112 }
113
114 if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) {
115 Vector<String16> services = sm->listServices();
116 services.sort(sort_func);
117 aout << "Currently running services:" << endl;
118
119 for (size_t i=0; i<services.size(); i++) {
120 sp<IBinder> service = sm->checkService(services[i]);
121 if (service != NULL) {
122 aout << " " << services[i] << endl;
123 }
124 }
125 return 0;
126 }
127
128 Vector<String16> args;
129 for (int i=2; i<argc; i++) {
130 args.add(String16(argv[i]));
131 }
132 String16 cmd = String16(argv[1]);
133 sp<IBinder> service = sm->checkService(cmd);
134 if (service == NULL) {
135 aerr << "Can't find service: " << argv[1] << endl;
136 return 20;
137 }
138
139 // TODO: block until a result is returned to MyResultReceiver.
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700140 IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args,
Dianne Hackborn1941a402016-08-29 12:30:43 -0700141 new MyShellCallback(), new MyResultReceiver());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700142 return 0;
143}