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> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 25 | |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/select.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/un.h> |
| 31 | |
| 32 | #include <cutils/sockets.h> |
| 33 | #include <private/android_filesystem_config.h> |
| 34 | |
| 35 | static void usage(char *progname); |
| 36 | static int do_monitor(int sock, int stop_after_cmd); |
| 37 | static int do_cmd(int sock, int argc, char **argv); |
| 38 | |
| 39 | int main(int argc, char **argv) { |
| 40 | int sock; |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame^] | 41 | int wait_for_socket; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 42 | |
| 43 | if (argc < 2) |
| 44 | usage(argv[0]); |
| 45 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame^] | 46 | wait_for_socket = strcmp(argv[1], "--wait") == 0; |
| 47 | if(wait_for_socket) { |
| 48 | argv++; |
| 49 | argc--; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame^] | 52 | if(argc < 2) |
| 53 | exit(5); |
| 54 | |
| 55 | while ((sock = socket_local_client("vold", |
| 56 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 57 | SOCK_STREAM)) < 0) { |
| 58 | if(!wait_for_socket) { |
| 59 | fprintf(stderr, "Error connecting (%s)\n", strerror(errno)); |
| 60 | exit(4); |
| 61 | } else { |
| 62 | sleep(1); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (!strcmp(argv[1], "monitor")) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 67 | exit(do_monitor(sock, 0)); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame^] | 68 | } else { |
| 69 | exit(do_cmd(sock, argc, argv)); |
| 70 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static int do_cmd(int sock, int argc, char **argv) { |
Ken Sumrall | d4b3661 | 2012-03-09 16:48:48 -0800 | [diff] [blame] | 74 | char final_cmd[255] = "0 "; /* 0 is a (now required) sequence number */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 75 | int i; |
Chih-Wei Huang | 7929deb | 2013-02-10 22:57:14 +0800 | [diff] [blame] | 76 | size_t ret; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 77 | |
| 78 | for (i = 1; i < argc; i++) { |
| 79 | char *cmp; |
| 80 | |
| 81 | if (!index(argv[i], ' ')) |
| 82 | asprintf(&cmp, "%s%s", argv[i], (i == (argc -1)) ? "" : " "); |
| 83 | else |
| 84 | asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " "); |
| 85 | |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 86 | ret = strlcat(final_cmd, cmp, sizeof(final_cmd)); |
| 87 | if (ret >= sizeof(final_cmd)) |
| 88 | abort(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 89 | free(cmp); |
| 90 | } |
| 91 | |
| 92 | if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) { |
| 93 | perror("write"); |
| 94 | return errno; |
| 95 | } |
| 96 | |
| 97 | return do_monitor(sock, 1); |
| 98 | } |
| 99 | |
| 100 | static int do_monitor(int sock, int stop_after_cmd) { |
| 101 | char *buffer = malloc(4096); |
| 102 | |
| 103 | if (!stop_after_cmd) |
| 104 | printf("[Connected to Vold]\n"); |
| 105 | |
| 106 | while(1) { |
| 107 | fd_set read_fds; |
| 108 | struct timeval to; |
| 109 | int rc = 0; |
| 110 | |
| 111 | to.tv_sec = 10; |
| 112 | to.tv_usec = 0; |
| 113 | |
| 114 | FD_ZERO(&read_fds); |
| 115 | FD_SET(sock, &read_fds); |
| 116 | |
| 117 | if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) { |
| 118 | fprintf(stderr, "Error in select (%s)\n", strerror(errno)); |
| 119 | free(buffer); |
| 120 | return errno; |
| 121 | } else if (!rc) { |
| 122 | continue; |
| 123 | fprintf(stderr, "[TIMEOUT]\n"); |
| 124 | return ETIMEDOUT; |
| 125 | } else if (FD_ISSET(sock, &read_fds)) { |
| 126 | memset(buffer, 0, 4096); |
| 127 | if ((rc = read(sock, buffer, 4096)) <= 0) { |
| 128 | if (rc == 0) |
| 129 | fprintf(stderr, "Lost connection to Vold - did it crash?\n"); |
| 130 | else |
| 131 | fprintf(stderr, "Error reading data (%s)\n", strerror(errno)); |
| 132 | free(buffer); |
| 133 | if (rc == 0) |
| 134 | return ECONNRESET; |
| 135 | return errno; |
| 136 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame^] | 137 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 138 | int offset = 0; |
| 139 | int i = 0; |
| 140 | |
| 141 | for (i = 0; i < rc; i++) { |
| 142 | if (buffer[i] == '\0') { |
| 143 | int code; |
| 144 | char tmp[4]; |
| 145 | |
| 146 | strncpy(tmp, buffer + offset, 3); |
| 147 | tmp[3] = '\0'; |
| 148 | code = atoi(tmp); |
| 149 | |
| 150 | printf("%s\n", buffer + offset); |
| 151 | if (stop_after_cmd) { |
| 152 | if (code >= 200 && code < 600) |
| 153 | return 0; |
| 154 | } |
| 155 | offset = i + 1; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | free(buffer); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static void usage(char *progname) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame^] | 165 | fprintf(stderr, |
| 166 | "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname); |
| 167 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 168 | |