• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
42     if (argc < 2)
43         usage(argv[0]);
44 
45     if ((sock = socket_local_client("vold",
46                                      ANDROID_SOCKET_NAMESPACE_RESERVED,
47                                      SOCK_STREAM)) < 0) {
48         fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
49         exit(4);
50     }
51 
52     if (!strcmp(argv[1], "monitor"))
53         exit(do_monitor(sock, 0));
54     exit(do_cmd(sock, argc, argv));
55 }
56 
do_cmd(int sock,int argc,char ** argv)57 static int do_cmd(int sock, int argc, char **argv) {
58     char final_cmd[255] = "0 "; /* 0 is a (now required) sequence number */
59     int i;
60     size_t ret;
61 
62     for (i = 1; i < argc; i++) {
63         char *cmp;
64 
65         if (!index(argv[i], ' '))
66             asprintf(&cmp, "%s%s", argv[i], (i == (argc -1)) ? "" : " ");
67         else
68             asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " ");
69 
70         ret = strlcat(final_cmd, cmp, sizeof(final_cmd));
71         if (ret >= sizeof(final_cmd))
72             abort();
73         free(cmp);
74     }
75 
76     if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
77         perror("write");
78         return errno;
79     }
80 
81     return do_monitor(sock, 1);
82 }
83 
do_monitor(int sock,int stop_after_cmd)84 static int do_monitor(int sock, int stop_after_cmd) {
85     char *buffer = malloc(4096);
86 
87     if (!stop_after_cmd)
88         printf("[Connected to Vold]\n");
89 
90     while(1) {
91         fd_set read_fds;
92         struct timeval to;
93         int rc = 0;
94 
95         to.tv_sec = 10;
96         to.tv_usec = 0;
97 
98         FD_ZERO(&read_fds);
99         FD_SET(sock, &read_fds);
100 
101         if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
102             fprintf(stderr, "Error in select (%s)\n", strerror(errno));
103             free(buffer);
104             return errno;
105         } else if (!rc) {
106             continue;
107             fprintf(stderr, "[TIMEOUT]\n");
108             return ETIMEDOUT;
109         } else if (FD_ISSET(sock, &read_fds)) {
110             memset(buffer, 0, 4096);
111             if ((rc = read(sock, buffer, 4096)) <= 0) {
112                 if (rc == 0)
113                     fprintf(stderr, "Lost connection to Vold - did it crash?\n");
114                 else
115                     fprintf(stderr, "Error reading data (%s)\n", strerror(errno));
116                 free(buffer);
117                 if (rc == 0)
118                     return ECONNRESET;
119                 return errno;
120             }
121 
122             int offset = 0;
123             int i = 0;
124 
125             for (i = 0; i < rc; i++) {
126                 if (buffer[i] == '\0') {
127                     int code;
128                     char tmp[4];
129 
130                     strncpy(tmp, buffer + offset, 3);
131                     tmp[3] = '\0';
132                     code = atoi(tmp);
133 
134                     printf("%s\n", buffer + offset);
135                     if (stop_after_cmd) {
136                         if (code >= 200 && code < 600)
137                             return 0;
138                     }
139                     offset = i + 1;
140                 }
141             }
142         }
143     }
144     free(buffer);
145     return 0;
146 }
147 
usage(char * progname)148 static void usage(char *progname) {
149     fprintf(stderr, "Usage: %s <monitor>|<cmd> [arg1] [arg2...]\n", progname);
150     exit(1);
151 }
152 
153