blob: 00fb77c06d19a024da2c67001ffa7fca82556570 [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) {
JP Abgrallbe746812013-06-20 11:18:07 -070068 char *final_cmd;
69 char *conv_ptr;
San Mehatd1830422010-01-15 08:02:39 -080070 int i;
71
JP Abgrallbe746812013-06-20 11:18:07 -070072 /* 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
San Mehatd1830422010-01-15 08:02:39 -080085 for (i = 1; i < argc; i++) {
Dan Albert602b80d2014-06-05 11:35:41 -070086 if (strchr(argv[i], '"')) {
Nick Kralevichad5b41f2012-07-19 18:48:05 -070087 perror("argument with embedded quotes not allowed");
88 free(final_cmd);
89 return 1;
90 }
Dan Albert602b80d2014-06-05 11:35:41 -070091 bool needs_quoting = strchr(argv[i], ' ');
Nick Kralevichad5b41f2012-07-19 18:48:05 -070092 const char *format = needs_quoting ? "%s\"%s\"%s" : "%s%s%s";
JP Abgrallbe746812013-06-20 11:18:07 -070093 char *tmp_final_cmd;
San Mehatd1830422010-01-15 08:02:39 -080094
JP Abgrallbe746812013-06-20 11:18:07 -070095 if (asprintf(&tmp_final_cmd, format, final_cmd, argv[i],
96 (i == (argc - 1)) ? "" : " ") < 0) {
97 int res = errno;
98 perror("failed asprintf");
Nick Kralevichad5b41f2012-07-19 18:48:05 -070099 free(final_cmd);
JP Abgrallbe746812013-06-20 11:18:07 -0700100 return res;
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700101 }
102 free(final_cmd);
JP Abgrallbe746812013-06-20 11:18:07 -0700103 final_cmd = tmp_final_cmd;
San Mehatd1830422010-01-15 08:02:39 -0800104 }
105
106 if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
JP Abgrallbe746812013-06-20 11:18:07 -0700107 int res = errno;
San Mehatd1830422010-01-15 08:02:39 -0800108 perror("write");
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700109 free(final_cmd);
JP Abgrallbe746812013-06-20 11:18:07 -0700110 return res;
San Mehatd1830422010-01-15 08:02:39 -0800111 }
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700112 free(final_cmd);
San Mehatd1830422010-01-15 08:02:39 -0800113
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
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700134 rc = TEMP_FAILURE_RETRY(select(sock +1, &read_fds, NULL, NULL, &to));
135 if (rc < 0) {
JP Abgrallbe746812013-06-20 11:18:07 -0700136 int res = errno;
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700137 fprintf(stderr, "Error in select (%s)\n", strerror(res));
San Mehatd1830422010-01-15 08:02:39 -0800138 free(buffer);
JP Abgrallbe746812013-06-20 11:18:07 -0700139 return res;
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700140 }
141 if (rc == 0) {
San Mehatd1830422010-01-15 08:02:39 -0800142 continue;
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700143 }
144 if (!FD_ISSET(sock, &read_fds)) {
145 continue;
146 }
Dmitry Shmidt44ec9032012-03-08 18:16:22 -0800147
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700148 memset(buffer, 0, 4096);
149 if ((rc = read(sock, buffer, 4096)) <= 0) {
150 int res = errno;
151 if (rc == 0)
152 fprintf(stderr, "Lost connection to Netd - did it crash?\n");
153 else
154 fprintf(stderr, "Error reading data (%s)\n", strerror(res));
155 free(buffer);
156 if (rc == 0)
157 return ECONNRESET;
158 return res;
159 }
San Mehatd1830422010-01-15 08:02:39 -0800160
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700161 int offset = 0;
162 int i = 0;
San Mehatd1830422010-01-15 08:02:39 -0800163
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700164 for (i = 0; i < rc; i++) {
165 if (buffer[i] == '\0') {
166 int code;
167 char tmp[4];
San Mehatd1830422010-01-15 08:02:39 -0800168
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700169 strncpy(tmp, buffer + offset, 3);
170 tmp[3] = '\0';
171 code = atoi(tmp);
172
173 printf("%s\n", buffer + offset);
174 if (stop_after_cmd) {
175 if (code >= 200 && code < 600)
176 return 0;
San Mehatd1830422010-01-15 08:02:39 -0800177 }
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700178 offset = i + 1;
San Mehatd1830422010-01-15 08:02:39 -0800179 }
180 }
181 }
182 free(buffer);
183 return 0;
184}
185
186static void usage(char *progname) {
JP Abgrallbe746812013-06-20 11:18:07 -0700187 fprintf(stderr, "Usage: %s [<sockname>] ([monitor] | ([<cmd_seq_num>] <cmd> [arg ...]))\n", progname);
San Mehatd1830422010-01-15 08:02:39 -0800188 exit(1);
189}