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
25#include <sys/socket.h>
26#include <sys/select.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <sys/un.h>
30
31#include <cutils/sockets.h>
32#include <private/android_filesystem_config.h>
33
34static void usage(char *progname);
35static int do_monitor(int sock, int stop_after_cmd);
36static int do_cmd(int sock, int argc, char **argv);
37
38int main(int argc, char **argv) {
39    int sock;
40    int cmdOffset = 0;
41
42    if (argc < 2)
43        usage(argv[0]);
44
45    // try interpreting the first arg as the socket name - if it fails go back to netd
46
47    if ((sock = socket_local_client(argv[1],
48                                     ANDROID_SOCKET_NAMESPACE_RESERVED,
49                                     SOCK_STREAM)) < 0) {
50        if ((sock = socket_local_client("netd",
51                                         ANDROID_SOCKET_NAMESPACE_RESERVED,
52                                         SOCK_STREAM)) < 0) {
53            fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
54            exit(4);
55        }
56    } else {
57        if (argc < 3) usage(argv[0]);
58        printf("Using alt socket %s\n", argv[1]);
59        cmdOffset = 1;
60    }
61
62    if (!strcmp(argv[1+cmdOffset], "monitor"))
63        exit(do_monitor(sock, 0));
64    exit(do_cmd(sock, argc-cmdOffset, &(argv[cmdOffset])));
65}
66
67static int do_cmd(int sock, int argc, char **argv) {
68    char *final_cmd;
69    char *conv_ptr;
70    int i;
71
72    /* Check if 1st arg is cmd sequence number */
73    strtol(argv[1], &conv_ptr, 10);
74    if (conv_ptr == argv[1]) {
75        final_cmd = strdup("0 ");
76    } else {
77        final_cmd = strdup("");
78    }
79    if (final_cmd == NULL) {
80        int res = errno;
81        perror("strdup failed");
82        return res;
83    }
84
85    for (i = 1; i < argc; i++) {
86        if (strchr(argv[i], '"')) {
87            perror("argument with embedded quotes not allowed");
88            free(final_cmd);
89            return 1;
90        }
91        bool needs_quoting = strchr(argv[i], ' ');
92        const char *format = needs_quoting ? "%s\"%s\"%s" : "%s%s%s";
93        char *tmp_final_cmd;
94
95        if (asprintf(&tmp_final_cmd, format, final_cmd, argv[i],
96                     (i == (argc - 1)) ? "" : " ") < 0) {
97            int res = errno;
98            perror("failed asprintf");
99            free(final_cmd);
100            return res;
101        }
102        free(final_cmd);
103        final_cmd = tmp_final_cmd;
104    }
105
106    if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
107        int res = errno;
108        perror("write");
109        free(final_cmd);
110        return res;
111    }
112    free(final_cmd);
113
114    return do_monitor(sock, 1);
115}
116
117static int do_monitor(int sock, int stop_after_cmd) {
118    char *buffer = malloc(4096);
119
120    if (!stop_after_cmd)
121        printf("[Connected to Netd]\n");
122
123    while(1) {
124        fd_set read_fds;
125        struct timeval to;
126        int rc = 0;
127
128        to.tv_sec = 10;
129        to.tv_usec = 0;
130
131        FD_ZERO(&read_fds);
132        FD_SET(sock, &read_fds);
133
134        if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
135            int res = errno;
136            fprintf(stderr, "Error in select (%s)\n", strerror(errno));
137            free(buffer);
138            return res;
139        } else if (!rc) {
140            continue;
141            fprintf(stderr, "[TIMEOUT]\n");
142            return ETIMEDOUT;
143        } else if (FD_ISSET(sock, &read_fds)) {
144            memset(buffer, 0, 4096);
145            if ((rc = read(sock, buffer, 4096)) <= 0) {
146                int res = errno;
147                if (rc == 0)
148                    fprintf(stderr, "Lost connection to Netd - did it crash?\n");
149                else
150                    fprintf(stderr, "Error reading data (%s)\n", strerror(errno));
151                free(buffer);
152                if (rc == 0)
153                    return ECONNRESET;
154                return res;
155            }
156
157            int offset = 0;
158            int i = 0;
159
160            for (i = 0; i < rc; i++) {
161                if (buffer[i] == '\0') {
162                    int code;
163                    char tmp[4];
164
165                    strncpy(tmp, buffer + offset, 3);
166                    tmp[3] = '\0';
167                    code = atoi(tmp);
168
169                    printf("%s\n", buffer + offset);
170                    if (stop_after_cmd) {
171                        if (code >= 200 && code < 600)
172                            return 0;
173                    }
174                    offset = i + 1;
175                }
176            }
177        }
178    }
179    free(buffer);
180    return 0;
181}
182
183static void usage(char *progname) {
184    fprintf(stderr, "Usage: %s [<sockname>] ([monitor] | ([<cmd_seq_num>] <cmd> [arg ...]))\n", progname);
185    exit(1);
186}
187