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 | |
| 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 Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 33 | #include <android-base/logging.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
| 35 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | #include <cutils/sockets.h> |
| 37 | #include <private/android_filesystem_config.h> |
| 38 | |
| 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); |
| 42 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 43 | static constexpr int kCommandTimeoutMs = 20 * 1000; |
| 44 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | int main(int argc, char **argv) { |
| 46 | int sock; |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 47 | int wait_for_socket; |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 48 | char *progname; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 49 | |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 50 | progname = argv[0]; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 51 | |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 52 | 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 Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 59 | wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 60 | if (wait_for_socket) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 61 | argv++; |
| 62 | argc--; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 65 | if (argc < 2) { |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 66 | usage(progname); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 67 | exit(5); |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 68 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 69 | |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 70 | const char* sockname = "vold"; |
| 71 | if (!strcmp(argv[1], "cryptfs")) { |
| 72 | sockname = "cryptd"; |
| 73 | } |
| 74 | |
| 75 | while ((sock = socket_local_client(sockname, |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 76 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 77 | SOCK_STREAM)) < 0) { |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 78 | if (!wait_for_socket) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 79 | PLOG(ERROR) << "Error connecting to " << sockname; |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 80 | exit(4); |
| 81 | } else { |
Paul Lawrence | 0628fa2 | 2015-06-04 10:49:25 -0700 | [diff] [blame] | 82 | usleep(10000); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | if (!strcmp(argv[1], "monitor")) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 87 | exit(do_monitor(sock, 0)); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 88 | } else { |
| 89 | exit(do_cmd(sock, argc, argv)); |
| 90 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static int do_cmd(int sock, int argc, char **argv) { |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 94 | int seq = getpid(); |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 95 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 96 | 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 105 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 106 | if (i < argc - 1) { |
| 107 | cmd.push_back(' '); |
| 108 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 111 | if (TEMP_FAILURE_RETRY(write(sock, cmd.c_str(), cmd.length() + 1)) < 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 112 | PLOG(ERROR) << "Failed to write command"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 113 | return errno; |
| 114 | } |
| 115 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 116 | return do_monitor(sock, seq); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 119 | static int do_monitor(int sock, int stop_after_seq) { |
| 120 | char buffer[4096]; |
| 121 | int timeout = kCommandTimeoutMs; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 122 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 123 | if (stop_after_seq == 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 124 | LOG(INFO) << "Connected to vold"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 125 | timeout = -1; |
| 126 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 127 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 128 | 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 Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 132 | LOG(ERROR) << "Timeout waiting for " << stop_after_seq; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 133 | return ETIMEDOUT; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 134 | } else if (rc < 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 135 | PLOG(ERROR) << "Failed during poll"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 136 | return errno; |
| 137 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 138 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 139 | if (!(poll_sock.revents & POLLIN)) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 140 | LOG(INFO) << "No data; trying again"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 141 | continue; |
| 142 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 143 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 144 | memset(buffer, 0, sizeof(buffer)); |
| 145 | rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer))); |
| 146 | if (rc == 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 147 | LOG(ERROR) << "Lost connection, did vold crash?"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 148 | return ECONNRESET; |
| 149 | } else if (rc < 0) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 150 | PLOG(ERROR) << "Error reading data"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 151 | return errno; |
| 152 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 153 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 154 | 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 159 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 160 | 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 165 | return 0; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 166 | } else { |
| 167 | return code; |
| 168 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 169 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 170 | } |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 171 | |
| 172 | offset = i + 1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 176 | return EIO; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static void usage(char *progname) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame^] | 180 | LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 181 | } |