OSDN Git Service

Snap for 7348217 from 0ae52740907cdcf0bdbf023cddc5b4d6db92c046 to sc-v2-release
[android-x86/system-vold.git] / vdc.cpp
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 <errno.h>
18 #include <fcntl.h>
19 #include <poll.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <sys/select.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/un.h>
30
31 #include "android/os/IVold.h"
32
33 #include <android-base/logging.h>
34 #include <android-base/parsebool.h>
35 #include <android-base/parseint.h>
36 #include <android-base/stringprintf.h>
37 #include <android-base/strings.h>
38 #include <binder/IServiceManager.h>
39 #include <binder/Status.h>
40
41 #include <private/android_filesystem_config.h>
42
43 static void usage(char* progname);
44
45 static android::sp<android::IBinder> getServiceAggressive() {
46     android::sp<android::IBinder> res;
47     auto sm = android::defaultServiceManager();
48     auto name = android::String16("vold");
49     for (int i = 0; i < 5000; i++) {
50         res = sm->checkService(name);
51         if (res) {
52             LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold";
53             break;
54         }
55         usleep(10000);  // 10ms
56     }
57     return res;
58 }
59
60 static void checkStatus(std::vector<std::string>& cmd, android::binder::Status status) {
61     if (status.isOk()) return;
62     std::string command = ::android::base::Join(cmd, " ");
63     LOG(ERROR) << "Command: " << command << " Failed: " << status.toString8().string();
64     exit(ENOTTY);
65 }
66
67 int main(int argc, char** argv) {
68     setenv("ANDROID_LOG_TAGS", "*:v", 1);
69     if (getppid() == 1) {
70         // If init is calling us then it's during boot and we should log to kmsg
71         android::base::InitLogging(argv, &android::base::KernelLogger);
72     } else {
73         android::base::InitLogging(argv, &android::base::StderrLogger);
74     }
75     std::vector<std::string> args(argv + 1, argv + argc);
76
77     if (args.size() > 0 && args[0] == "--wait") {
78         // Just ignore the --wait flag
79         args.erase(args.begin());
80     }
81
82     if (args.size() < 2) {
83         usage(argv[0]);
84         exit(5);
85     }
86     android::sp<android::IBinder> binder = getServiceAggressive();
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 (args[0] == "cryptfs" && args[1] == "enablefilecrypto") {
94         checkStatus(args, vold->fbeEnable());
95     } else if (args[0] == "cryptfs" && args[1] == "init_user0") {
96         checkStatus(args, vold->initUser0());
97     } else if (args[0] == "cryptfs" && args[1] == "enablecrypto") {
98         int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
99         int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI;
100         checkStatus(args, vold->fdeEnable(passwordType, "", encryptionFlags));
101     } else if (args[0] == "cryptfs" && args[1] == "mountdefaultencrypted") {
102         checkStatus(args, vold->mountDefaultEncrypted());
103     } else if (args[0] == "volume" && args[1] == "abort_fuse") {
104         checkStatus(args, vold->abortFuse());
105     } else if (args[0] == "volume" && args[1] == "shutdown") {
106         checkStatus(args, vold->shutdown());
107     } else if (args[0] == "volume" && args[1] == "reset") {
108         checkStatus(args, vold->reset());
109     } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 4) {
110         checkStatus(args, vold->mountFstab(args[2], args[3]));
111     } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 6) {
112         auto shouldFormat = android::base::ParseBool(args[4]);
113         if (shouldFormat == android::base::ParseBoolResult::kError) exit(EINVAL);
114         checkStatus(args, vold->encryptFstab(args[2], args[3],
115                                              shouldFormat == android::base::ParseBoolResult::kTrue,
116                                              args[5]));
117     } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
118         bool supported = false;
119         checkStatus(args, vold->supportsCheckpoint(&supported));
120         return supported ? 1 : 0;
121     } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" &&
122                args.size() == 2) {
123         bool supported = false;
124         checkStatus(args, vold->supportsBlockCheckpoint(&supported));
125         return supported ? 1 : 0;
126     } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) {
127         bool supported = false;
128         checkStatus(args, vold->supportsFileCheckpoint(&supported));
129         return supported ? 1 : 0;
130     } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) {
131         int retry;
132         if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
133         checkStatus(args, vold->startCheckpoint(retry));
134     } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) {
135         bool enabled = false;
136         checkStatus(args, vold->needsCheckpoint(&enabled));
137         return enabled ? 1 : 0;
138     } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) {
139         bool enabled = false;
140         checkStatus(args, vold->needsRollback(&enabled));
141         return enabled ? 1 : 0;
142     } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) {
143         checkStatus(args, vold->commitChanges());
144     } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) {
145         checkStatus(args, vold->prepareCheckpoint());
146     } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) {
147         checkStatus(args, vold->restoreCheckpoint(args[2]));
148     } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) {
149         int count;
150         if (!android::base::ParseInt(args[3], &count)) exit(EINVAL);
151         checkStatus(args, vold->restoreCheckpointPart(args[2], count));
152     } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) {
153         checkStatus(args, vold->markBootAttempt());
154     } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) {
155         int retry;
156         if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
157         checkStatus(args, vold->abortChanges(args[2], retry != 0));
158     } else if (args[0] == "checkpoint" && args[1] == "resetCheckpoint") {
159         checkStatus(args, vold->resetCheckpoint());
160     } else if (args[0] == "keymaster" && args[1] == "earlyBootEnded") {
161         checkStatus(args, vold->earlyBootEnded());
162     } else {
163         LOG(ERROR) << "Raw commands are no longer supported";
164         exit(EINVAL);
165     }
166     return 0;
167 }
168
169 static void usage(char* progname) {
170     LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]";
171 }