San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 25 | #include <poll.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 26 | |
| 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 Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 33 | #include "android/os/IVold.h" |
| 34 | |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 36 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 37 | #include <binder/IServiceManager.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 38 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 39 | #include <cutils/sockets.h> |
| 40 | #include <private/android_filesystem_config.h> |
| 41 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 42 | #define ENABLE_BINDER 1 |
| 43 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 44 | static void usage(char *progname); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 45 | |
| 46 | #if !ENABLE_BINDER |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 47 | static int do_monitor(int sock, int stop_after_cmd); |
| 48 | static int do_cmd(int sock, int argc, char **argv); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 49 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 50 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 51 | static constexpr int kCommandTimeoutMs = 20 * 1000; |
| 52 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | int main(int argc, char **argv) { |
| 54 | int sock; |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 55 | int wait_for_socket; |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 56 | char *progname; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 57 | |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 58 | progname = argv[0]; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 59 | |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 60 | 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 Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 67 | wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 68 | if (wait_for_socket) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 69 | argv++; |
| 70 | argc--; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 73 | if (argc < 3) { |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 74 | usage(progname); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 75 | exit(5); |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 76 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 77 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 78 | #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 Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 106 | const char* sockname = "vold"; |
| 107 | if (!strcmp(argv[1], "cryptfs")) { |
| 108 | sockname = "cryptd"; |
| 109 | } |
| 110 | |
| 111 | while ((sock = socket_local_client(sockname, |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 112 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 113 | SOCK_STREAM)) < 0) { |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 114 | if (!wait_for_socket) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 115 | PLOG(ERROR) << "Error connecting to " << sockname; |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 116 | exit(4); |
| 117 | } else { |
Paul Lawrence | 0628fa2 | 2015-06-04 10:49:25 -0700 | [diff] [blame] | 118 | usleep(10000); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | if (!strcmp(argv[1], "monitor")) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 123 | exit(do_monitor(sock, 0)); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 124 | } else { |
| 125 | exit(do_cmd(sock, argc, argv)); |
| 126 | } |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 127 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 130 | #if !ENABLE_BINDER |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 131 | static int do_cmd(int sock, int argc, char **argv) { |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 132 | int seq = getpid(); |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 133 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 134 | 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 143 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 144 | if (i < argc - 1) { |
| 145 | cmd.push_back(' '); |
| 146 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 149 | if (TEMP_FAILURE_RETRY(write(sock, cmd.c_str(), cmd.length() + 1)) < 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 150 | PLOG(ERROR) << "Failed to write command"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 151 | return errno; |
| 152 | } |
| 153 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 154 | return do_monitor(sock, seq); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 157 | static int do_monitor(int sock, int stop_after_seq) { |
| 158 | char buffer[4096]; |
| 159 | int timeout = kCommandTimeoutMs; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 160 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 161 | if (stop_after_seq == 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 162 | LOG(INFO) << "Connected to vold"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 163 | timeout = -1; |
| 164 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 165 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 166 | 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 Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 170 | LOG(ERROR) << "Timeout waiting for " << stop_after_seq; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 171 | return ETIMEDOUT; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 172 | } else if (rc < 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 173 | PLOG(ERROR) << "Failed during poll"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 174 | return errno; |
| 175 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 176 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 177 | if (!(poll_sock.revents & POLLIN)) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 178 | LOG(INFO) << "No data; trying again"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 179 | continue; |
| 180 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 181 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 182 | memset(buffer, 0, sizeof(buffer)); |
| 183 | rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer))); |
| 184 | if (rc == 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 185 | LOG(ERROR) << "Lost connection, did vold crash?"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 186 | return ECONNRESET; |
| 187 | } else if (rc < 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 188 | PLOG(ERROR) << "Error reading data"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 189 | return errno; |
| 190 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 191 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 192 | 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 197 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 198 | 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 203 | return 0; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 204 | } else { |
| 205 | return code; |
| 206 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 207 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 208 | } |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 209 | |
| 210 | offset = i + 1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 214 | return EIO; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 215 | } |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame^] | 216 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 217 | |
| 218 | static void usage(char *progname) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 219 | LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 220 | } |