2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/socket.h>
28 #include <sys/select.h>
30 #include <sys/types.h>
33 #include <android-base/logging.h>
34 #include <android-base/stringprintf.h>
36 #include <cutils/sockets.h>
37 #include <private/android_filesystem_config.h>
39 static void usage(char *progname);
40 static int do_monitor(int sock, int stop_after_cmd);
41 static int do_cmd(int sock, int argc, char **argv);
43 static constexpr int kCommandTimeoutMs = 20 * 1000;
45 int main(int argc, char **argv) {
53 // If init is calling us then it's during boot and we should log to kmsg
54 android::base::InitLogging(argv, &android::base::KernelLogger);
56 android::base::InitLogging(argv, &android::base::StderrLogger);
59 wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
60 if (wait_for_socket) {
70 const char* sockname = "vold";
71 if (!strcmp(argv[1], "cryptfs")) {
75 while ((sock = socket_local_client(sockname,
76 ANDROID_SOCKET_NAMESPACE_RESERVED,
78 if (!wait_for_socket) {
79 PLOG(ERROR) << "Error connecting to " << sockname;
86 if (!strcmp(argv[1], "monitor")) {
87 exit(do_monitor(sock, 0));
89 exit(do_cmd(sock, argc, argv));
93 static int do_cmd(int sock, int argc, char **argv) {
96 std::string cmd(android::base::StringPrintf("%d ", seq));
97 for (int i = 1; i < argc; i++) {
98 if (!strchr(argv[i], ' ')) {
111 if (TEMP_FAILURE_RETRY(write(sock, cmd.c_str(), cmd.length() + 1)) < 0) {
112 PLOG(ERROR) << "Failed to write command";
116 return do_monitor(sock, seq);
119 static int do_monitor(int sock, int stop_after_seq) {
121 int timeout = kCommandTimeoutMs;
123 if (stop_after_seq == 0) {
124 LOG(INFO) << "Connected to vold";
129 struct pollfd poll_sock = { sock, POLLIN, 0 };
130 int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, timeout));
132 LOG(ERROR) << "Timeout waiting for " << stop_after_seq;
135 PLOG(ERROR) << "Failed during poll";
139 if (!(poll_sock.revents & POLLIN)) {
140 LOG(INFO) << "No data; trying again";
144 memset(buffer, 0, sizeof(buffer));
145 rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
147 LOG(ERROR) << "Lost connection, did vold crash?";
150 PLOG(ERROR) << "Error reading data";
155 for (int i = 0; i < rc; i++) {
156 if (buffer[i] == '\0') {
157 char* res = buffer + offset;
158 fprintf(stdout, "%s\n", res);
160 int code = atoi(strtok(res, " "));
161 if (code >= 200 && code < 600) {
162 int seq = atoi(strtok(nullptr, " "));
163 if (seq == stop_after_seq) {
179 static void usage(char *progname) {
180 LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]";