blob: 7ab5fccbdde82e8b43fe9152530c6bf61782a69e [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
Tom Cherryf71511a2017-03-29 16:50:28 -070033#include <android-base/logging.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070034#include <android-base/stringprintf.h>
35
San Mehatf1b736b2009-10-10 17:22:08 -070036#include <cutils/sockets.h>
37#include <private/android_filesystem_config.h>
38
39static void usage(char *progname);
40static int do_monitor(int sock, int stop_after_cmd);
41static int do_cmd(int sock, int argc, char **argv);
42
Jeff Sharkey47695b22016-02-01 17:02:29 -070043static constexpr int kCommandTimeoutMs = 20 * 1000;
44
San Mehatf1b736b2009-10-10 17:22:08 -070045int main(int argc, char **argv) {
46 int sock;
Paul Lawrencef4faa572014-01-29 13:31:03 -080047 int wait_for_socket;
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070048 char *progname;
San Mehatf1b736b2009-10-10 17:22:08 -070049
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070050 progname = argv[0];
San Mehatf1b736b2009-10-10 17:22:08 -070051
Tom Cherryf71511a2017-03-29 16:50:28 -070052 if (getppid() == 1) {
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);
55 } else {
56 android::base::InitLogging(argv, &android::base::StderrLogger);
57 }
58
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070059 wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
Jeff Sharkey47695b22016-02-01 17:02:29 -070060 if (wait_for_socket) {
Paul Lawrencef4faa572014-01-29 13:31:03 -080061 argv++;
62 argc--;
San Mehatf1b736b2009-10-10 17:22:08 -070063 }
64
Jeff Sharkey47695b22016-02-01 17:02:29 -070065 if (argc < 2) {
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070066 usage(progname);
Paul Lawrencef4faa572014-01-29 13:31:03 -080067 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070068 }
Paul Lawrencef4faa572014-01-29 13:31:03 -080069
Paul Lawrenced0b42952015-06-03 14:19:51 -070070 const char* sockname = "vold";
71 if (!strcmp(argv[1], "cryptfs")) {
72 sockname = "cryptd";
73 }
74
75 while ((sock = socket_local_client(sockname,
Paul Lawrencef4faa572014-01-29 13:31:03 -080076 ANDROID_SOCKET_NAMESPACE_RESERVED,
77 SOCK_STREAM)) < 0) {
Jeff Sharkey47695b22016-02-01 17:02:29 -070078 if (!wait_for_socket) {
Tom Cherryf71511a2017-03-29 16:50:28 -070079 PLOG(ERROR) << "Error connecting to " << sockname;
Paul Lawrencef4faa572014-01-29 13:31:03 -080080 exit(4);
81 } else {
Paul Lawrence0628fa22015-06-04 10:49:25 -070082 usleep(10000);
Paul Lawrencef4faa572014-01-29 13:31:03 -080083 }
84 }
85
86 if (!strcmp(argv[1], "monitor")) {
San Mehatf1b736b2009-10-10 17:22:08 -070087 exit(do_monitor(sock, 0));
Paul Lawrencef4faa572014-01-29 13:31:03 -080088 } else {
89 exit(do_cmd(sock, argc, argv));
90 }
San Mehatf1b736b2009-10-10 17:22:08 -070091}
92
93static int do_cmd(int sock, int argc, char **argv) {
Jeff Sharkey47695b22016-02-01 17:02:29 -070094 int seq = getpid();
Paul Lawrenced0b42952015-06-03 14:19:51 -070095
Jeff Sharkey47695b22016-02-01 17:02:29 -070096 std::string cmd(android::base::StringPrintf("%d ", seq));
97 for (int i = 1; i < argc; i++) {
98 if (!strchr(argv[i], ' ')) {
99 cmd.append(argv[i]);
100 } else {
101 cmd.push_back('\"');
102 cmd.append(argv[i]);
103 cmd.push_back('\"');
104 }
San Mehatf1b736b2009-10-10 17:22:08 -0700105
Jeff Sharkey47695b22016-02-01 17:02:29 -0700106 if (i < argc - 1) {
107 cmd.push_back(' ');
108 }
San Mehatf1b736b2009-10-10 17:22:08 -0700109 }
110
Jeff Sharkey47695b22016-02-01 17:02:29 -0700111 if (TEMP_FAILURE_RETRY(write(sock, cmd.c_str(), cmd.length() + 1)) < 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700112 PLOG(ERROR) << "Failed to write command";
San Mehatf1b736b2009-10-10 17:22:08 -0700113 return errno;
114 }
115
Jeff Sharkey47695b22016-02-01 17:02:29 -0700116 return do_monitor(sock, seq);
San Mehatf1b736b2009-10-10 17:22:08 -0700117}
118
Jeff Sharkey47695b22016-02-01 17:02:29 -0700119static int do_monitor(int sock, int stop_after_seq) {
120 char buffer[4096];
121 int timeout = kCommandTimeoutMs;
San Mehatf1b736b2009-10-10 17:22:08 -0700122
Jeff Sharkey47695b22016-02-01 17:02:29 -0700123 if (stop_after_seq == 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700124 LOG(INFO) << "Connected to vold";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700125 timeout = -1;
126 }
San Mehatf1b736b2009-10-10 17:22:08 -0700127
Jeff Sharkey47695b22016-02-01 17:02:29 -0700128 while (1) {
129 struct pollfd poll_sock = { sock, POLLIN, 0 };
130 int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, timeout));
131 if (rc == 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700132 LOG(ERROR) << "Timeout waiting for " << stop_after_seq;
San Mehatf1b736b2009-10-10 17:22:08 -0700133 return ETIMEDOUT;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700134 } else if (rc < 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700135 PLOG(ERROR) << "Failed during poll";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700136 return errno;
137 }
Paul Lawrencef4faa572014-01-29 13:31:03 -0800138
Jeff Sharkey47695b22016-02-01 17:02:29 -0700139 if (!(poll_sock.revents & POLLIN)) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700140 LOG(INFO) << "No data; trying again";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700141 continue;
142 }
San Mehatf1b736b2009-10-10 17:22:08 -0700143
Jeff Sharkey47695b22016-02-01 17:02:29 -0700144 memset(buffer, 0, sizeof(buffer));
145 rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
146 if (rc == 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700147 LOG(ERROR) << "Lost connection, did vold crash?";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700148 return ECONNRESET;
149 } else if (rc < 0) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700150 PLOG(ERROR) << "Error reading data";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700151 return errno;
152 }
San Mehatf1b736b2009-10-10 17:22:08 -0700153
Jeff Sharkey47695b22016-02-01 17:02:29 -0700154 int offset = 0;
155 for (int i = 0; i < rc; i++) {
156 if (buffer[i] == '\0') {
157 char* res = buffer + offset;
158 fprintf(stdout, "%s\n", res);
San Mehatf1b736b2009-10-10 17:22:08 -0700159
Jeff Sharkey47695b22016-02-01 17:02:29 -0700160 int code = atoi(strtok(res, " "));
161 if (code >= 200 && code < 600) {
162 int seq = atoi(strtok(nullptr, " "));
163 if (seq == stop_after_seq) {
164 if (code == 200) {
San Mehatf1b736b2009-10-10 17:22:08 -0700165 return 0;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700166 } else {
167 return code;
168 }
San Mehatf1b736b2009-10-10 17:22:08 -0700169 }
San Mehatf1b736b2009-10-10 17:22:08 -0700170 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700171
172 offset = i + 1;
San Mehatf1b736b2009-10-10 17:22:08 -0700173 }
174 }
175 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700176 return EIO;
San Mehatf1b736b2009-10-10 17:22:08 -0700177}
178
179static void usage(char *progname) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700180 LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700181}