blob: 210ef22113fd4136ca1d8339b078ed5da8245933 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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 Bohm092aa1c2011-04-01 12:35:25 +020024#include <stdlib.h>
San Mehatf1b736b2009-10-10 17:22:08 -070025
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
35static void usage(char *progname);
36static int do_monitor(int sock, int stop_after_cmd);
37static int do_cmd(int sock, int argc, char **argv);
38
39int main(int argc, char **argv) {
40 int sock;
Paul Lawrencef4faa572014-01-29 13:31:03 -080041 int wait_for_socket;
San Mehatf1b736b2009-10-10 17:22:08 -070042
43 if (argc < 2)
44 usage(argv[0]);
45
Paul Lawrencef4faa572014-01-29 13:31:03 -080046 wait_for_socket = strcmp(argv[1], "--wait") == 0;
47 if(wait_for_socket) {
48 argv++;
49 argc--;
San Mehatf1b736b2009-10-10 17:22:08 -070050 }
51
Paul Lawrencef4faa572014-01-29 13:31:03 -080052 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 Mehatf1b736b2009-10-10 17:22:08 -070067 exit(do_monitor(sock, 0));
Paul Lawrencef4faa572014-01-29 13:31:03 -080068 } else {
69 exit(do_cmd(sock, argc, argv));
70 }
San Mehatf1b736b2009-10-10 17:22:08 -070071}
72
73static int do_cmd(int sock, int argc, char **argv) {
Ken Sumralld4b36612012-03-09 16:48:48 -080074 char final_cmd[255] = "0 "; /* 0 is a (now required) sequence number */
San Mehatf1b736b2009-10-10 17:22:08 -070075 int i;
Chih-Wei Huang7929deb2013-02-10 22:57:14 +080076 size_t ret;
San Mehatf1b736b2009-10-10 17:22:08 -070077
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 Bohm092aa1c2011-04-01 12:35:25 +020086 ret = strlcat(final_cmd, cmp, sizeof(final_cmd));
87 if (ret >= sizeof(final_cmd))
88 abort();
San Mehatf1b736b2009-10-10 17:22:08 -070089 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
100static 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 Lawrencef4faa572014-01-29 13:31:03 -0800137
San Mehatf1b736b2009-10-10 17:22:08 -0700138 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
164static void usage(char *progname) {
Paul Lawrencef4faa572014-01-29 13:31:03 -0800165 fprintf(stderr,
166 "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
167 }
San Mehatf1b736b2009-10-10 17:22:08 -0700168