1f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat/*
2f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * Copyright (C) 2008 The Android Open Source Project
3f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *
4f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * Licensed under the Apache License, Version 2.0 (the "License");
5f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * you may not use this file except in compliance with the License.
6f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * You may obtain a copy of the License at
7f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *
8f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *      http://www.apache.org/licenses/LICENSE-2.0
9f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *
10f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * Unless required by applicable law or agreed to in writing, software
11f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * distributed under the License is distributed on an "AS IS" BASIS,
12f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * See the License for the specific language governing permissions and
14f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * limitations under the License.
15f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat */
16f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
17f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <stdio.h>
18f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <stdlib.h>
19f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <unistd.h>
20f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <string.h>
21f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <signal.h>
22f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <errno.h>
23f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <fcntl.h>
24092aa1c585fedd9e169eece41b8a471f1739908aPeter Bohm#include <stdlib.h>
25f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
26f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <sys/socket.h>
27f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <sys/select.h>
28f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <sys/time.h>
29f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <sys/types.h>
30f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <sys/un.h>
31f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
32f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <cutils/sockets.h>
33f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <private/android_filesystem_config.h>
34f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
35f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatstatic void usage(char *progname);
36f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatstatic int do_monitor(int sock, int stop_after_cmd);
37f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatstatic int do_cmd(int sock, int argc, char **argv);
38f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
39f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatint main(int argc, char **argv) {
40f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    int sock;
41f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
42f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    if (argc < 2)
43f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        usage(argv[0]);
44f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
45f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    if ((sock = socket_local_client("vold",
46f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                                     ANDROID_SOCKET_NAMESPACE_RESERVED,
47f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                                     SOCK_STREAM)) < 0) {
48f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
49f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        exit(4);
50f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    }
51f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
52f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    if (!strcmp(argv[1], "monitor"))
53f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        exit(do_monitor(sock, 0));
54f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    exit(do_cmd(sock, argc, argv));
55f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
56f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
57f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatstatic int do_cmd(int sock, int argc, char **argv) {
58d4b3661cae1664ca07025b7f0dacc621de0ed93fKen Sumrall    char final_cmd[255] = "0 "; /* 0 is a (now required) sequence number */
59f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    int i;
60092aa1c585fedd9e169eece41b8a471f1739908aPeter Bohm    int ret;
61f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
62f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    for (i = 1; i < argc; i++) {
63f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        char *cmp;
64f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
65f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        if (!index(argv[i], ' '))
66f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            asprintf(&cmp, "%s%s", argv[i], (i == (argc -1)) ? "" : " ");
67f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        else
68f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " ");
69f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
70092aa1c585fedd9e169eece41b8a471f1739908aPeter Bohm        ret = strlcat(final_cmd, cmp, sizeof(final_cmd));
71092aa1c585fedd9e169eece41b8a471f1739908aPeter Bohm        if (ret >= sizeof(final_cmd))
72092aa1c585fedd9e169eece41b8a471f1739908aPeter Bohm            abort();
73f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        free(cmp);
74f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    }
75f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
76f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
77f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        perror("write");
78f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        return errno;
79f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    }
80f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
81f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    return do_monitor(sock, 1);
82f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
83f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
84f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatstatic int do_monitor(int sock, int stop_after_cmd) {
85f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    char *buffer = malloc(4096);
86f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
87f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    if (!stop_after_cmd)
88f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        printf("[Connected to Vold]\n");
89f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
90f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    while(1) {
91f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        fd_set read_fds;
92f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        struct timeval to;
93f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        int rc = 0;
94f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
95f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        to.tv_sec = 10;
96f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        to.tv_usec = 0;
97f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
98f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        FD_ZERO(&read_fds);
99f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        FD_SET(sock, &read_fds);
100f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
101f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
102f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            fprintf(stderr, "Error in select (%s)\n", strerror(errno));
103f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            free(buffer);
104f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            return errno;
105f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        } else if (!rc) {
106f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            continue;
107f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            fprintf(stderr, "[TIMEOUT]\n");
108f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            return ETIMEDOUT;
109f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        } else if (FD_ISSET(sock, &read_fds)) {
110f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            memset(buffer, 0, 4096);
111f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            if ((rc = read(sock, buffer, 4096)) <= 0) {
112f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                if (rc == 0)
113f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    fprintf(stderr, "Lost connection to Vold - did it crash?\n");
114f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                else
115f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    fprintf(stderr, "Error reading data (%s)\n", strerror(errno));
116f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                free(buffer);
117f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                if (rc == 0)
118f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    return ECONNRESET;
119f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                return errno;
120f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            }
121f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
122f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            int offset = 0;
123f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            int i = 0;
124f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
125f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            for (i = 0; i < rc; i++) {
126f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                if (buffer[i] == '\0') {
127f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    int code;
128f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    char tmp[4];
129f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
130f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    strncpy(tmp, buffer + offset, 3);
131f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    tmp[3] = '\0';
132f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    code = atoi(tmp);
133f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
134f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    printf("%s\n", buffer + offset);
135f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    if (stop_after_cmd) {
136f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                        if (code >= 200 && code < 600)
137f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                            return 0;
138f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    }
139f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                    offset = i + 1;
140f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                }
141f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            }
142f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        }
143f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    }
144f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    free(buffer);
145f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    return 0;
146f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
147f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
148f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatstatic void usage(char *progname) {
149f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    fprintf(stderr, "Usage: %s <monitor>|<cmd> [arg1] [arg2...]\n", progname);
150f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    exit(1);
151f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
152f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
153