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.
26 #include <sys/socket.h>
27 #include <sys/select.h>
29 #include <sys/types.h>
32 #include <cutils/sockets.h>
33 #include <private/android_filesystem_config.h>
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);
39 int vdc_main(int argc, char **argv) {
46 wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
57 const char* sockname = "vold";
58 if (!strcmp(argv[1], "cryptfs")) {
62 while ((sock = socket_local_client(sockname,
63 ANDROID_SOCKET_NAMESPACE_RESERVED,
65 if(!wait_for_socket) {
66 fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
73 if (!strcmp(argv[1], "monitor")) {
74 exit(do_monitor(sock, 0));
76 exit(do_cmd(sock, argc, argv));
80 static int do_cmd(int sock, int argc, char **argv) {
81 char final_cmd[255] = "0 "; /* 0 is a (now required) sequence number */
86 for (i = 1; i < argc; i++) {
89 if (!strchr(argv[i], ' '))
90 asprintf(&cmp, "%s%s", argv[i], (i == (argc -1)) ? "" : " ");
92 asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " ");
94 ret = strlcat(final_cmd, cmp, sizeof(final_cmd));
95 if (ret >= sizeof(final_cmd))
100 if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
105 return do_monitor(sock, 1);
108 static int do_monitor(int sock, int stop_after_cmd) {
109 char *buffer = malloc(4096);
112 printf("[Connected to Vold]\n");
123 FD_SET(sock, &read_fds);
125 if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
126 fprintf(stderr, "Error in select (%s)\n", strerror(errno));
131 fprintf(stderr, "[TIMEOUT]\n");
133 } else if (FD_ISSET(sock, &read_fds)) {
134 memset(buffer, 0, 4096);
135 if ((rc = read(sock, buffer, 4096)) <= 0) {
137 fprintf(stderr, "Lost connection to Vold - did it crash?\n");
139 fprintf(stderr, "Error reading data (%s)\n", strerror(errno));
149 for (i = 0; i < rc; i++) {
150 if (buffer[i] == '\0') {
154 strlcpy(tmp, buffer + offset, sizeof(tmp));
157 printf("%s\n", buffer + offset);
158 if (stop_after_cmd) {
159 if (code >= 200 && code < 600)
171 static void usage(char *progname) {
173 "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
177 int main(int argc, char **argv) {
178 return vdc_main(argc, argv);