blob: 6927e92dd6c7cafa919dbfc8e48f08c9f71b61cf [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;
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070042 char *progname;
San Mehatf1b736b2009-10-10 17:22:08 -070043
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070044 progname = argv[0];
San Mehatf1b736b2009-10-10 17:22:08 -070045
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070046 wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -080047 if(wait_for_socket) {
48 argv++;
49 argc--;
San Mehatf1b736b2009-10-10 17:22:08 -070050 }
51
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070052 if(argc < 2) {
53 usage(progname);
Paul Lawrencef4faa572014-01-29 13:31:03 -080054 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070055 }
Paul Lawrencef4faa572014-01-29 13:31:03 -080056
57 while ((sock = socket_local_client("vold",
58 ANDROID_SOCKET_NAMESPACE_RESERVED,
59 SOCK_STREAM)) < 0) {
60 if(!wait_for_socket) {
61 fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
62 exit(4);
63 } else {
Paul Lawrence13486032014-02-03 13:28:11 -080064 sleep(1);
Paul Lawrencef4faa572014-01-29 13:31:03 -080065 }
66 }
67
68 if (!strcmp(argv[1], "monitor")) {
San Mehatf1b736b2009-10-10 17:22:08 -070069 exit(do_monitor(sock, 0));
Paul Lawrencef4faa572014-01-29 13:31:03 -080070 } else {
71 exit(do_cmd(sock, argc, argv));
72 }
San Mehatf1b736b2009-10-10 17:22:08 -070073}
74
75static int do_cmd(int sock, int argc, char **argv) {
Ken Sumralld4b36612012-03-09 16:48:48 -080076 char final_cmd[255] = "0 "; /* 0 is a (now required) sequence number */
San Mehatf1b736b2009-10-10 17:22:08 -070077 int i;
Chih-Wei Huang7929deb2013-02-10 22:57:14 +080078 size_t ret;
San Mehatf1b736b2009-10-10 17:22:08 -070079
80 for (i = 1; i < argc; i++) {
81 char *cmp;
82
Dan Albertdd58c432014-06-05 11:44:40 -070083 if (!strchr(argv[i], ' '))
San Mehatf1b736b2009-10-10 17:22:08 -070084 asprintf(&cmp, "%s%s", argv[i], (i == (argc -1)) ? "" : " ");
85 else
86 asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " ");
87
Peter Bohm092aa1c2011-04-01 12:35:25 +020088 ret = strlcat(final_cmd, cmp, sizeof(final_cmd));
89 if (ret >= sizeof(final_cmd))
90 abort();
San Mehatf1b736b2009-10-10 17:22:08 -070091 free(cmp);
92 }
93
94 if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
95 perror("write");
96 return errno;
97 }
98
99 return do_monitor(sock, 1);
100}
101
102static int do_monitor(int sock, int stop_after_cmd) {
103 char *buffer = malloc(4096);
104
105 if (!stop_after_cmd)
106 printf("[Connected to Vold]\n");
107
108 while(1) {
109 fd_set read_fds;
110 struct timeval to;
111 int rc = 0;
112
113 to.tv_sec = 10;
114 to.tv_usec = 0;
115
116 FD_ZERO(&read_fds);
117 FD_SET(sock, &read_fds);
118
119 if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
120 fprintf(stderr, "Error in select (%s)\n", strerror(errno));
121 free(buffer);
122 return errno;
123 } else if (!rc) {
124 continue;
125 fprintf(stderr, "[TIMEOUT]\n");
126 return ETIMEDOUT;
127 } else if (FD_ISSET(sock, &read_fds)) {
128 memset(buffer, 0, 4096);
129 if ((rc = read(sock, buffer, 4096)) <= 0) {
130 if (rc == 0)
131 fprintf(stderr, "Lost connection to Vold - did it crash?\n");
132 else
133 fprintf(stderr, "Error reading data (%s)\n", strerror(errno));
134 free(buffer);
135 if (rc == 0)
136 return ECONNRESET;
137 return errno;
138 }
Paul Lawrencef4faa572014-01-29 13:31:03 -0800139
San Mehatf1b736b2009-10-10 17:22:08 -0700140 int offset = 0;
141 int i = 0;
142
143 for (i = 0; i < rc; i++) {
144 if (buffer[i] == '\0') {
145 int code;
146 char tmp[4];
147
Henrik Baard21522662015-02-06 09:24:14 +0100148 strlcpy(tmp, buffer + offset, sizeof(tmp));
San Mehatf1b736b2009-10-10 17:22:08 -0700149 code = atoi(tmp);
150
151 printf("%s\n", buffer + offset);
152 if (stop_after_cmd) {
153 if (code >= 200 && code < 600)
154 return 0;
155 }
156 offset = i + 1;
157 }
158 }
159 }
160 }
161 free(buffer);
162 return 0;
163}
164
165static void usage(char *progname) {
Paul Lawrencef4faa572014-01-29 13:31:03 -0800166 fprintf(stderr,
167 "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
168 }
San Mehatf1b736b2009-10-10 17:22:08 -0700169