blob: c6b2c92adaa575e40cde9783df66655f2764ba3a [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
Paul Lawrenced0b42952015-06-03 14:19:51 -070057 const char* sockname = "vold";
58 if (!strcmp(argv[1], "cryptfs")) {
59 sockname = "cryptd";
60 }
61
62 while ((sock = socket_local_client(sockname,
Paul Lawrencef4faa572014-01-29 13:31:03 -080063 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 {
Paul Lawrence0628fa22015-06-04 10:49:25 -070069 usleep(10000);
Paul Lawrencef4faa572014-01-29 13:31:03 -080070 }
71 }
72
73 if (!strcmp(argv[1], "monitor")) {
San Mehatf1b736b2009-10-10 17:22:08 -070074 exit(do_monitor(sock, 0));
Paul Lawrencef4faa572014-01-29 13:31:03 -080075 } else {
76 exit(do_cmd(sock, argc, argv));
77 }
San Mehatf1b736b2009-10-10 17:22:08 -070078}
79
80static int do_cmd(int sock, int argc, char **argv) {
Ken Sumralld4b36612012-03-09 16:48:48 -080081 char final_cmd[255] = "0 "; /* 0 is a (now required) sequence number */
Paul Lawrenced0b42952015-06-03 14:19:51 -070082
San Mehatf1b736b2009-10-10 17:22:08 -070083 int i;
Chih-Wei Huang7929deb2013-02-10 22:57:14 +080084 size_t ret;
San Mehatf1b736b2009-10-10 17:22:08 -070085
86 for (i = 1; i < argc; i++) {
87 char *cmp;
88
Dan Albertdd58c432014-06-05 11:44:40 -070089 if (!strchr(argv[i], ' '))
San Mehatf1b736b2009-10-10 17:22:08 -070090 asprintf(&cmp, "%s%s", argv[i], (i == (argc -1)) ? "" : " ");
91 else
92 asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " ");
93
Peter Bohm092aa1c2011-04-01 12:35:25 +020094 ret = strlcat(final_cmd, cmp, sizeof(final_cmd));
95 if (ret >= sizeof(final_cmd))
96 abort();
San Mehatf1b736b2009-10-10 17:22:08 -070097 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
108static 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 }
Paul Lawrencef4faa572014-01-29 13:31:03 -0800145
San Mehatf1b736b2009-10-10 17:22:08 -0700146 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
Henrik Baard21522662015-02-06 09:24:14 +0100154 strlcpy(tmp, buffer + offset, sizeof(tmp));
San Mehatf1b736b2009-10-10 17:22:08 -0700155 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
171static void usage(char *progname) {
Paul Lawrencef4faa572014-01-29 13:31:03 -0800172 fprintf(stderr,
173 "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
174 }