blob: 4eb26cdf13e57fe604211a9d3c5d2b1b54549553 [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
Jeff Sharkey47695b22016-02-01 17:02:29 -070033#include <android-base/stringprintf.h>
34
San Mehatf1b736b2009-10-10 17:22:08 -070035#include <cutils/sockets.h>
36#include <private/android_filesystem_config.h>
37
38static void usage(char *progname);
39static int do_monitor(int sock, int stop_after_cmd);
40static int do_cmd(int sock, int argc, char **argv);
41
Jeff Sharkey47695b22016-02-01 17:02:29 -070042static constexpr int kCommandTimeoutMs = 20 * 1000;
43
San Mehatf1b736b2009-10-10 17:22:08 -070044int main(int argc, char **argv) {
45 int sock;
Paul Lawrencef4faa572014-01-29 13:31:03 -080046 int wait_for_socket;
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070047 char *progname;
San Mehatf1b736b2009-10-10 17:22:08 -070048
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070049 progname = argv[0];
San Mehatf1b736b2009-10-10 17:22:08 -070050
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070051 wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
Jeff Sharkey47695b22016-02-01 17:02:29 -070052 if (wait_for_socket) {
Paul Lawrencef4faa572014-01-29 13:31:03 -080053 argv++;
54 argc--;
San Mehatf1b736b2009-10-10 17:22:08 -070055 }
56
Jeff Sharkey47695b22016-02-01 17:02:29 -070057 if (argc < 2) {
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070058 usage(progname);
Paul Lawrencef4faa572014-01-29 13:31:03 -080059 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070060 }
Paul Lawrencef4faa572014-01-29 13:31:03 -080061
Paul Lawrenced0b42952015-06-03 14:19:51 -070062 const char* sockname = "vold";
63 if (!strcmp(argv[1], "cryptfs")) {
64 sockname = "cryptd";
65 }
66
67 while ((sock = socket_local_client(sockname,
Paul Lawrencef4faa572014-01-29 13:31:03 -080068 ANDROID_SOCKET_NAMESPACE_RESERVED,
69 SOCK_STREAM)) < 0) {
Jeff Sharkey47695b22016-02-01 17:02:29 -070070 if (!wait_for_socket) {
71 fprintf(stdout, "Error connecting to %s: %s\n", sockname, strerror(errno));
Paul Lawrencef4faa572014-01-29 13:31:03 -080072 exit(4);
73 } else {
Paul Lawrence0628fa22015-06-04 10:49:25 -070074 usleep(10000);
Paul Lawrencef4faa572014-01-29 13:31:03 -080075 }
76 }
77
78 if (!strcmp(argv[1], "monitor")) {
San Mehatf1b736b2009-10-10 17:22:08 -070079 exit(do_monitor(sock, 0));
Paul Lawrencef4faa572014-01-29 13:31:03 -080080 } else {
81 exit(do_cmd(sock, argc, argv));
82 }
San Mehatf1b736b2009-10-10 17:22:08 -070083}
84
85static int do_cmd(int sock, int argc, char **argv) {
Jeff Sharkey47695b22016-02-01 17:02:29 -070086 int seq = getpid();
Paul Lawrenced0b42952015-06-03 14:19:51 -070087
Jeff Sharkey47695b22016-02-01 17:02:29 -070088 std::string cmd(android::base::StringPrintf("%d ", seq));
89 for (int i = 1; i < argc; i++) {
90 if (!strchr(argv[i], ' ')) {
91 cmd.append(argv[i]);
92 } else {
93 cmd.push_back('\"');
94 cmd.append(argv[i]);
95 cmd.push_back('\"');
96 }
San Mehatf1b736b2009-10-10 17:22:08 -070097
Jeff Sharkey47695b22016-02-01 17:02:29 -070098 if (i < argc - 1) {
99 cmd.push_back(' ');
100 }
San Mehatf1b736b2009-10-10 17:22:08 -0700101 }
102
Jeff Sharkey47695b22016-02-01 17:02:29 -0700103 if (TEMP_FAILURE_RETRY(write(sock, cmd.c_str(), cmd.length() + 1)) < 0) {
104 fprintf(stderr, "Failed to write command: %s\n", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700105 return errno;
106 }
107
Jeff Sharkey47695b22016-02-01 17:02:29 -0700108 return do_monitor(sock, seq);
San Mehatf1b736b2009-10-10 17:22:08 -0700109}
110
Jeff Sharkey47695b22016-02-01 17:02:29 -0700111static int do_monitor(int sock, int stop_after_seq) {
112 char buffer[4096];
113 int timeout = kCommandTimeoutMs;
San Mehatf1b736b2009-10-10 17:22:08 -0700114
Jeff Sharkey47695b22016-02-01 17:02:29 -0700115 if (stop_after_seq == 0) {
116 fprintf(stderr, "Connected to vold\n");
117 timeout = -1;
118 }
San Mehatf1b736b2009-10-10 17:22:08 -0700119
Jeff Sharkey47695b22016-02-01 17:02:29 -0700120 while (1) {
121 struct pollfd poll_sock = { sock, POLLIN, 0 };
122 int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, timeout));
123 if (rc == 0) {
124 fprintf(stderr, "Timeout waiting for %d\n", stop_after_seq);
San Mehatf1b736b2009-10-10 17:22:08 -0700125 return ETIMEDOUT;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700126 } else if (rc < 0) {
127 fprintf(stderr, "Failed during poll: %s\n", strerror(errno));
128 return errno;
129 }
Paul Lawrencef4faa572014-01-29 13:31:03 -0800130
Jeff Sharkey47695b22016-02-01 17:02:29 -0700131 if (!(poll_sock.revents & POLLIN)) {
132 fprintf(stderr, "No data; trying again\n");
133 continue;
134 }
San Mehatf1b736b2009-10-10 17:22:08 -0700135
Jeff Sharkey47695b22016-02-01 17:02:29 -0700136 memset(buffer, 0, sizeof(buffer));
137 rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
138 if (rc == 0) {
139 fprintf(stderr, "Lost connection, did vold crash?\n");
140 return ECONNRESET;
141 } else if (rc < 0) {
142 fprintf(stderr, "Error reading data: %s\n", strerror(errno));
143 return errno;
144 }
San Mehatf1b736b2009-10-10 17:22:08 -0700145
Jeff Sharkey47695b22016-02-01 17:02:29 -0700146 int offset = 0;
147 for (int i = 0; i < rc; i++) {
148 if (buffer[i] == '\0') {
149 char* res = buffer + offset;
150 fprintf(stdout, "%s\n", res);
San Mehatf1b736b2009-10-10 17:22:08 -0700151
Jeff Sharkey47695b22016-02-01 17:02:29 -0700152 int code = atoi(strtok(res, " "));
153 if (code >= 200 && code < 600) {
154 int seq = atoi(strtok(nullptr, " "));
155 if (seq == stop_after_seq) {
156 if (code == 200) {
San Mehatf1b736b2009-10-10 17:22:08 -0700157 return 0;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700158 } else {
159 return code;
160 }
San Mehatf1b736b2009-10-10 17:22:08 -0700161 }
San Mehatf1b736b2009-10-10 17:22:08 -0700162 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700163
164 offset = i + 1;
San Mehatf1b736b2009-10-10 17:22:08 -0700165 }
166 }
167 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700168 return EIO;
San Mehatf1b736b2009-10-10 17:22:08 -0700169}
170
171static void usage(char *progname) {
Paul Lawrencef4faa572014-01-29 13:31:03 -0800172 fprintf(stderr,
173 "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700174}