blob: 3a38641b3ceccc21c420239a6a879961c00b0c28 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
2 * Copyright (C) 2008 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#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <signal.h>
22#include <errno.h>
23#include <fcntl.h>
Peter Bohm092aa1c2011-04-01 12:35:25 +020024#include <stdlib.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070025#include <poll.h>
San Mehatf1b736b2009-10-10 17:22:08 -070026
27#include <sys/socket.h>
28#include <sys/select.h>
29#include <sys/time.h>
30#include <sys/types.h>
31#include <sys/un.h>
32
Jeff Sharkey99f92682017-09-13 18:43:44 -060033#include "android/os/IVold.h"
34
Tom Cherryf71511a2017-03-29 16:50:28 -070035#include <android-base/logging.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070036#include <android-base/stringprintf.h>
Jeff Sharkey99f92682017-09-13 18:43:44 -060037#include <binder/IServiceManager.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070038
San Mehatf1b736b2009-10-10 17:22:08 -070039#include <cutils/sockets.h>
40#include <private/android_filesystem_config.h>
41
Jeff Sharkey99f92682017-09-13 18:43:44 -060042#define ENABLE_BINDER 1
43
San Mehatf1b736b2009-10-10 17:22:08 -070044static void usage(char *progname);
Jeff Sharkey99f92682017-09-13 18:43:44 -060045
46#if !ENABLE_BINDER
San Mehatf1b736b2009-10-10 17:22:08 -070047static int do_monitor(int sock, int stop_after_cmd);
48static int do_cmd(int sock, int argc, char **argv);
Jeff Sharkey99f92682017-09-13 18:43:44 -060049#endif
San Mehatf1b736b2009-10-10 17:22:08 -070050
Jeff Sharkey47695b22016-02-01 17:02:29 -070051static constexpr int kCommandTimeoutMs = 20 * 1000;
52
San Mehatf1b736b2009-10-10 17:22:08 -070053int main(int argc, char **argv) {
54 int sock;
Paul Lawrencef4faa572014-01-29 13:31:03 -080055 int wait_for_socket;
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070056 char *progname;
San Mehatf1b736b2009-10-10 17:22:08 -070057
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070058 progname = argv[0];
San Mehatf1b736b2009-10-10 17:22:08 -070059
Tom Cherryf71511a2017-03-29 16:50:28 -070060 if (getppid() == 1) {
61 // If init is calling us then it's during boot and we should log to kmsg
62 android::base::InitLogging(argv, &android::base::KernelLogger);
63 } else {
64 android::base::InitLogging(argv, &android::base::StderrLogger);
65 }
66
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070067 wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
Jeff Sharkey47695b22016-02-01 17:02:29 -070068 if (wait_for_socket) {
Paul Lawrencef4faa572014-01-29 13:31:03 -080069 argv++;
70 argc--;
San Mehatf1b736b2009-10-10 17:22:08 -070071 }
72
Jeff Sharkey99f92682017-09-13 18:43:44 -060073 if (argc < 3) {
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070074 usage(progname);
Paul Lawrencef4faa572014-01-29 13:31:03 -080075 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070076 }
Paul Lawrencef4faa572014-01-29 13:31:03 -080077
Jeff Sharkey99f92682017-09-13 18:43:44 -060078#if ENABLE_BINDER
79 std::string arg1 = argv[1];
80 std::string arg2 = argv[2];
81
82 android::sp<android::IBinder> binder = android::defaultServiceManager()->getService(
83 android::String16("vold"));
84 if (!binder) {
85 LOG(ERROR) << "Failed to obtain vold Binder";
86 exit(EINVAL);
87 }
88 auto vold = android::interface_cast<android::os::IVold>(binder);
89
90 if (arg1 == "cryptfs" && arg2 == "enablefilecrypto") {
91 exit(vold->fbeEnable().isOk() ? 0 : ENOTTY);
92 } else if (arg1 == "cryptfs" && arg2 == "init_user0") {
93 exit(vold->initUser0().isOk() ? 0 : ENOTTY);
94 } else if (arg1 == "cryptfs" && arg2 == "enablecrypto") {
95 int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
96 int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_IN_PLACE
97 | android::os::IVold::ENCRYPTION_FLAG_NO_UI;
98 exit(vold->fdeEnable(passwordType, "", encryptionFlags).isOk() ? 0 : ENOTTY);
99 } else if (arg1 == "volume" && arg2 == "shutdown") {
100 exit(vold->shutdown().isOk() ? 0 : ENOTTY);
101 } else {
102 LOG(ERROR) << "Raw commands are no longer supported";
103 exit(EINVAL);
104 }
105#else
Paul Lawrenced0b42952015-06-03 14:19:51 -0700106 const char* sockname = "vold";
107 if (!strcmp(argv[1], "cryptfs")) {
108 sockname = "cryptd";
109 }
110
111 while ((sock = socket_local_client(sockname,
Paul Lawrencef4faa572014-01-29 13:31:03 -0800112 ANDROID_SOCKET_NAMESPACE_RESERVED,
113 SOCK_STREAM)) < 0) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700114 if (!wait_for_socket) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700115 PLOG(ERROR) << "Error connecting to " << sockname;
Paul Lawrencef4faa572014-01-29 13:31:03 -0800116 exit(4);
117 } else {
Paul Lawrence0628fa22015-06-04 10:49:25 -0700118 usleep(10000);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800119 }
120 }
121
122 if (!strcmp(argv[1], "monitor")) {
San Mehatf1b736b2009-10-10 17:22:08 -0700123 exit(do_monitor(sock, 0));
Paul Lawrencef4faa572014-01-29 13:31:03 -0800124 } else {
125 exit(do_cmd(sock, argc, argv));
126 }
Jeff Sharkey99f92682017-09-13 18:43:44 -0600127#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700128}
129
Jeff Sharkey99f92682017-09-13 18:43:44 -0600130#if !ENABLE_BINDER
San Mehatf1b736b2009-10-10 17:22:08 -0700131static int do_cmd(int sock, int argc, char **argv) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700132 int seq = getpid();
Paul Lawrenced0b42952015-06-03 14:19:51 -0700133
Jeff Sharkey47695b22016-02-01 17:02:29 -0700134 std::string cmd(android::base::StringPrintf("%d ", seq));
135 for (int i = 1; i < argc; i++) {
136 if (!strchr(argv[i], ' ')) {
137 cmd.append(argv[i]);
138 } else {
139 cmd.push_back('\"');
140 cmd.append(argv[i]);
141 cmd.push_back('\"');
142 }
San Mehatf1b736b2009-10-10 17:22:08 -0700143
Jeff Sharkey47695b22016-02-01 17:02:29 -0700144 if (i < argc - 1) {
145 cmd.push_back(' ');
146 }
San Mehatf1b736b2009-10-10 17:22:08 -0700147 }
148
Jeff Sharkey47695b22016-02-01 17:02:29 -0700149 if (TEMP_FAILURE_RETRY(write(sock, cmd.c_str(), cmd.length() + 1)) < 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700150 PLOG(ERROR) << "Failed to write command";
San Mehatf1b736b2009-10-10 17:22:08 -0700151 return errno;
152 }
153
Jeff Sharkey47695b22016-02-01 17:02:29 -0700154 return do_monitor(sock, seq);
San Mehatf1b736b2009-10-10 17:22:08 -0700155}
156
Jeff Sharkey47695b22016-02-01 17:02:29 -0700157static int do_monitor(int sock, int stop_after_seq) {
158 char buffer[4096];
159 int timeout = kCommandTimeoutMs;
San Mehatf1b736b2009-10-10 17:22:08 -0700160
Jeff Sharkey47695b22016-02-01 17:02:29 -0700161 if (stop_after_seq == 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700162 LOG(INFO) << "Connected to vold";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700163 timeout = -1;
164 }
San Mehatf1b736b2009-10-10 17:22:08 -0700165
Jeff Sharkey47695b22016-02-01 17:02:29 -0700166 while (1) {
167 struct pollfd poll_sock = { sock, POLLIN, 0 };
168 int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, timeout));
169 if (rc == 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700170 LOG(ERROR) << "Timeout waiting for " << stop_after_seq;
San Mehatf1b736b2009-10-10 17:22:08 -0700171 return ETIMEDOUT;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700172 } else if (rc < 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700173 PLOG(ERROR) << "Failed during poll";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700174 return errno;
175 }
Paul Lawrencef4faa572014-01-29 13:31:03 -0800176
Jeff Sharkey47695b22016-02-01 17:02:29 -0700177 if (!(poll_sock.revents & POLLIN)) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700178 LOG(INFO) << "No data; trying again";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700179 continue;
180 }
San Mehatf1b736b2009-10-10 17:22:08 -0700181
Jeff Sharkey47695b22016-02-01 17:02:29 -0700182 memset(buffer, 0, sizeof(buffer));
183 rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
184 if (rc == 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700185 LOG(ERROR) << "Lost connection, did vold crash?";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700186 return ECONNRESET;
187 } else if (rc < 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700188 PLOG(ERROR) << "Error reading data";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700189 return errno;
190 }
San Mehatf1b736b2009-10-10 17:22:08 -0700191
Jeff Sharkey47695b22016-02-01 17:02:29 -0700192 int offset = 0;
193 for (int i = 0; i < rc; i++) {
194 if (buffer[i] == '\0') {
195 char* res = buffer + offset;
196 fprintf(stdout, "%s\n", res);
San Mehatf1b736b2009-10-10 17:22:08 -0700197
Jeff Sharkey47695b22016-02-01 17:02:29 -0700198 int code = atoi(strtok(res, " "));
199 if (code >= 200 && code < 600) {
200 int seq = atoi(strtok(nullptr, " "));
201 if (seq == stop_after_seq) {
202 if (code == 200) {
San Mehatf1b736b2009-10-10 17:22:08 -0700203 return 0;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700204 } else {
205 return code;
206 }
San Mehatf1b736b2009-10-10 17:22:08 -0700207 }
San Mehatf1b736b2009-10-10 17:22:08 -0700208 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700209
210 offset = i + 1;
San Mehatf1b736b2009-10-10 17:22:08 -0700211 }
212 }
213 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700214 return EIO;
San Mehatf1b736b2009-10-10 17:22:08 -0700215}
Jeff Sharkey99f92682017-09-13 18:43:44 -0600216#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700217
218static void usage(char *progname) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700219 LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700220}