blob: 29a399a59a15c52e924305a45789aed17520cb26 [file] [log] [blame]
San Mehatd1830422010-01-15 08:02:39 -08001/*
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;
Robert Greenwalt745e09f2012-03-29 14:45:54 -070040 int cmdOffset = 0;
San Mehatd1830422010-01-15 08:02:39 -080041
42 if (argc < 2)
43 usage(argv[0]);
44
Robert Greenwalt745e09f2012-03-29 14:45:54 -070045 // 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],
San Mehatd1830422010-01-15 08:02:39 -080048 ANDROID_SOCKET_NAMESPACE_RESERVED,
49 SOCK_STREAM)) < 0) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -070050 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;
San Mehatd1830422010-01-15 08:02:39 -080060 }
61
Robert Greenwalt745e09f2012-03-29 14:45:54 -070062 if (!strcmp(argv[1+cmdOffset], "monitor"))
San Mehatd1830422010-01-15 08:02:39 -080063 exit(do_monitor(sock, 0));
Robert Greenwalt745e09f2012-03-29 14:45:54 -070064 exit(do_cmd(sock, argc-cmdOffset, &(argv[cmdOffset])));
San Mehatd1830422010-01-15 08:02:39 -080065}
66
67static int do_cmd(int sock, int argc, char **argv) {
Nick Kralevichad5b41f2012-07-19 18:48:05 -070068 char *final_cmd = strdup("0 ");
69 if (final_cmd == NULL) {
70 perror("strdup");
71 return errno;
72 }
73
San Mehatd1830422010-01-15 08:02:39 -080074 int i;
75
76 for (i = 1; i < argc; i++) {
Nick Kralevichad5b41f2012-07-19 18:48:05 -070077 if (index(argv[i], '"')) {
78 perror("argument with embedded quotes not allowed");
79 free(final_cmd);
80 return 1;
81 }
82 bool needs_quoting = index(argv[i], ' ');
83 const char *format = needs_quoting ? "%s\"%s\"%s" : "%s%s%s";
San Mehatd1830422010-01-15 08:02:39 -080084 char *cmp;
85
Nick Kralevichad5b41f2012-07-19 18:48:05 -070086 if (asprintf(&cmp, format, final_cmd, argv[i],
87 (i == (argc -1)) ? "" : " ") < 0) {
88 perror("malloc");
89 free(final_cmd);
90 return errno;
91 }
92 free(final_cmd);
93 final_cmd = cmp;
San Mehatd1830422010-01-15 08:02:39 -080094 }
95
96 if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
97 perror("write");
Nick Kralevichad5b41f2012-07-19 18:48:05 -070098 free(final_cmd);
San Mehatd1830422010-01-15 08:02:39 -080099 return errno;
100 }
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700101 free(final_cmd);
San Mehatd1830422010-01-15 08:02:39 -0800102
103 return do_monitor(sock, 1);
104}
105
106static int do_monitor(int sock, int stop_after_cmd) {
107 char *buffer = malloc(4096);
108
109 if (!stop_after_cmd)
110 printf("[Connected to Netd]\n");
111
112 while(1) {
113 fd_set read_fds;
114 struct timeval to;
115 int rc = 0;
116
117 to.tv_sec = 10;
118 to.tv_usec = 0;
119
120 FD_ZERO(&read_fds);
121 FD_SET(sock, &read_fds);
122
123 if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
124 fprintf(stderr, "Error in select (%s)\n", strerror(errno));
125 free(buffer);
126 return errno;
127 } else if (!rc) {
128 continue;
129 fprintf(stderr, "[TIMEOUT]\n");
130 return ETIMEDOUT;
131 } else if (FD_ISSET(sock, &read_fds)) {
132 memset(buffer, 0, 4096);
133 if ((rc = read(sock, buffer, 4096)) <= 0) {
134 if (rc == 0)
San Mehat9d10b342010-01-18 09:51:02 -0800135 fprintf(stderr, "Lost connection to Netd - did it crash?\n");
San Mehatd1830422010-01-15 08:02:39 -0800136 else
137 fprintf(stderr, "Error reading data (%s)\n", strerror(errno));
138 free(buffer);
139 if (rc == 0)
140 return ECONNRESET;
141 return errno;
142 }
Dmitry Shmidt44ec9032012-03-08 18:16:22 -0800143
San Mehatd1830422010-01-15 08:02:39 -0800144 int offset = 0;
145 int i = 0;
146
147 for (i = 0; i < rc; i++) {
148 if (buffer[i] == '\0') {
149 int code;
150 char tmp[4];
151
152 strncpy(tmp, buffer + offset, 3);
153 tmp[3] = '\0';
154 code = atoi(tmp);
155
156 printf("%s\n", buffer + offset);
157 if (stop_after_cmd) {
158 if (code >= 200 && code < 600)
159 return 0;
160 }
161 offset = i + 1;
162 }
163 }
164 }
165 }
166 free(buffer);
167 return 0;
168}
169
170static void usage(char *progname) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700171 fprintf(stderr, "Usage: %s [sockname] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
San Mehatd1830422010-01-15 08:02:39 -0800172 exit(1);
173}