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 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 27 | #include <sys/select.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/un.h> |
| 31 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 32 | #include "android/os/IVold.h" |
| 33 | |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 34 | #include <android-base/logging.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 36 | #include <binder/IServiceManager.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 37 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
| 39 | |
| 40 | static void usage(char *progname); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 41 | |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 42 | static android::sp<android::IBinder> getServiceAggressive() { |
| 43 | android::sp<android::IBinder> res; |
| 44 | auto sm = android::defaultServiceManager(); |
| 45 | auto name = android::String16("vold"); |
| 46 | for (int i = 0; i < 500; i++) { |
| 47 | res = sm->checkService(name); |
| 48 | if (res) { |
| 49 | LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold"; |
| 50 | break; |
| 51 | } |
| 52 | usleep(10000); // 10ms |
| 53 | } |
| 54 | return res; |
| 55 | } |
| 56 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 57 | int main(int argc, char **argv) { |
| 58 | int sock; |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 59 | int wait; |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 60 | char *progname; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 61 | |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 62 | progname = argv[0]; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 63 | |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 64 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 65 | if (getppid() == 1) { |
| 66 | // If init is calling us then it's during boot and we should log to kmsg |
| 67 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 68 | } else { |
| 69 | android::base::InitLogging(argv, &android::base::StderrLogger); |
| 70 | } |
| 71 | |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 72 | wait = argc > 1 && strcmp(argv[1], "--wait") == 0; |
| 73 | if (wait) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 74 | argv++; |
| 75 | argc--; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 78 | if (argc < 3) { |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 79 | usage(progname); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 80 | exit(5); |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 81 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 82 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 83 | std::string arg1 = argv[1]; |
| 84 | std::string arg2 = argv[2]; |
| 85 | |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 86 | android::sp<android::IBinder> binder = getServiceAggressive(); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 87 | if (!binder) { |
| 88 | LOG(ERROR) << "Failed to obtain vold Binder"; |
| 89 | exit(EINVAL); |
| 90 | } |
| 91 | auto vold = android::interface_cast<android::os::IVold>(binder); |
| 92 | |
| 93 | if (arg1 == "cryptfs" && arg2 == "enablefilecrypto") { |
| 94 | exit(vold->fbeEnable().isOk() ? 0 : ENOTTY); |
| 95 | } else if (arg1 == "cryptfs" && arg2 == "init_user0") { |
| 96 | exit(vold->initUser0().isOk() ? 0 : ENOTTY); |
| 97 | } else if (arg1 == "cryptfs" && arg2 == "enablecrypto") { |
| 98 | int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT; |
| 99 | int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_IN_PLACE |
| 100 | | android::os::IVold::ENCRYPTION_FLAG_NO_UI; |
| 101 | exit(vold->fdeEnable(passwordType, "", encryptionFlags).isOk() ? 0 : ENOTTY); |
Jeff Sharkey | 57b1874 | 2017-09-18 13:49:51 -0600 | [diff] [blame] | 102 | } else if (arg1 == "cryptfs" && arg2 == "mountdefaultencrypted") { |
| 103 | exit(vold->mountDefaultEncrypted().isOk() ? 0 : ENOTTY); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 104 | } else if (arg1 == "volume" && arg2 == "shutdown") { |
| 105 | exit(vold->shutdown().isOk() ? 0 : ENOTTY); |
| 106 | } else { |
| 107 | LOG(ERROR) << "Raw commands are no longer supported"; |
| 108 | exit(EINVAL); |
| 109 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 112 | static void usage(char *progname) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 113 | LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 114 | } |