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>
24 #include <stdlib.h>
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
main(int argc,char ** argv)39 int main(int argc, char **argv) {
40 int sock;
41 int wait_for_socket;
42 char *progname;
43
44 progname = argv[0];
45
46 wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
47 if(wait_for_socket) {
48 argv++;
49 argc--;
50 }
51
52 if(argc < 2) {
53 usage(progname);
54 exit(5);
55 }
56
57 const char* sockname = "vold";
58 if (!strcmp(argv[1], "cryptfs")) {
59 sockname = "cryptd";
60 }
61
62 while ((sock = socket_local_client(sockname,
63 ANDROID_SOCKET_NAMESPACE_RESERVED,
64 SOCK_STREAM)) < 0) {
65 if(!wait_for_socket) {
66 fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
67 exit(4);
68 } else {
69 usleep(10000);
70 }
71 }
72
73 if (!strcmp(argv[1], "monitor")) {
74 exit(do_monitor(sock, 0));
75 } else {
76 exit(do_cmd(sock, argc, argv));
77 }
78 }
79
do_cmd(int sock,int argc,char ** 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 */
82
83 int i;
84 size_t ret;
85
86 for (i = 1; i < argc; i++) {
87 char *cmp;
88
89 if (!strchr(argv[i], ' '))
90 asprintf(&cmp, "%s%s", argv[i], (i == (argc -1)) ? "" : " ");
91 else
92 asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " ");
93
94 ret = strlcat(final_cmd, cmp, sizeof(final_cmd));
95 if (ret >= sizeof(final_cmd))
96 abort();
97 free(cmp);
98 }
99
100 if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
101 perror("write");
102 return errno;
103 }
104
105 return do_monitor(sock, 1);
106 }
107
do_monitor(int sock,int stop_after_cmd)108 static int do_monitor(int sock, int stop_after_cmd) {
109 char *buffer = malloc(4096);
110
111 if (!stop_after_cmd)
112 printf("[Connected to Vold]\n");
113
114 while(1) {
115 fd_set read_fds;
116 struct timeval to;
117 int rc = 0;
118
119 to.tv_sec = 10;
120 to.tv_usec = 0;
121
122 FD_ZERO(&read_fds);
123 FD_SET(sock, &read_fds);
124
125 if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
126 fprintf(stderr, "Error in select (%s)\n", strerror(errno));
127 free(buffer);
128 return errno;
129 } else if (!rc) {
130 continue;
131 fprintf(stderr, "[TIMEOUT]\n");
132 return ETIMEDOUT;
133 } else if (FD_ISSET(sock, &read_fds)) {
134 memset(buffer, 0, 4096);
135 if ((rc = read(sock, buffer, 4096)) <= 0) {
136 if (rc == 0)
137 fprintf(stderr, "Lost connection to Vold - did it crash?\n");
138 else
139 fprintf(stderr, "Error reading data (%s)\n", strerror(errno));
140 free(buffer);
141 if (rc == 0)
142 return ECONNRESET;
143 return errno;
144 }
145
146 int offset = 0;
147 int i = 0;
148
149 for (i = 0; i < rc; i++) {
150 if (buffer[i] == '\0') {
151 int code;
152 char tmp[4];
153
154 strlcpy(tmp, buffer + offset, sizeof(tmp));
155 code = atoi(tmp);
156
157 printf("%s\n", buffer + offset);
158 if (stop_after_cmd) {
159 if (code >= 200 && code < 600)
160 return 0;
161 }
162 offset = i + 1;
163 }
164 }
165 }
166 }
167 free(buffer);
168 return 0;
169 }
170
usage(char * progname)171 static void usage(char *progname) {
172 fprintf(stderr,
173 "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
174 }
175