blob: ec59637331d056e30da419acb88359ecef20e4e5 [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
Bernie Innocenti23bf9272018-05-23 15:45:40 +090025#include <sys/poll.h>
San Mehatd1830422010-01-15 08:02:39 -080026#include <sys/socket.h>
San Mehatd1830422010-01-15 08:02:39 -080027#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 }
Yi Kongbdfd57e2018-07-25 13:26:10 -070079 if (final_cmd == nullptr) {
JP Abgrallbe746812013-06-20 11:18:07 -070080 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) {
Elliott Hughes12c32782016-06-13 13:28:17 -0700118 char *buffer = (char *)malloc(4096);
San Mehatd1830422010-01-15 08:02:39 -0800119
120 if (!stop_after_cmd)
121 printf("[Connected to Netd]\n");
122
123 while(1) {
San Mehatd1830422010-01-15 08:02:39 -0800124 int rc = 0;
Bernie Innocenti23bf9272018-05-23 15:45:40 +0900125 struct pollfd fds = { .fd = sock, .events = POLLIN };
126 const int timeout_msecs = 10 * 1000;
127 rc = TEMP_FAILURE_RETRY(poll(&fds, 1, timeout_msecs));
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700128 if (rc < 0) {
JP Abgrallbe746812013-06-20 11:18:07 -0700129 int res = errno;
Bernie Innocenti23bf9272018-05-23 15:45:40 +0900130 fprintf(stderr, "Error in poll(): %s\n", strerror(res));
San Mehatd1830422010-01-15 08:02:39 -0800131 free(buffer);
JP Abgrallbe746812013-06-20 11:18:07 -0700132 return res;
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700133 }
134 if (rc == 0) {
San Mehatd1830422010-01-15 08:02:39 -0800135 continue;
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700136 }
Bernie Innocenti23bf9272018-05-23 15:45:40 +0900137 if (!(fds.revents & (POLLIN | POLLERR))) {
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700138 continue;
139 }
Dmitry Shmidt44ec9032012-03-08 18:16:22 -0800140
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700141 memset(buffer, 0, 4096);
142 if ((rc = read(sock, buffer, 4096)) <= 0) {
143 int res = errno;
144 if (rc == 0)
145 fprintf(stderr, "Lost connection to Netd - did it crash?\n");
146 else
147 fprintf(stderr, "Error reading data (%s)\n", strerror(res));
148 free(buffer);
149 if (rc == 0)
150 return ECONNRESET;
151 return res;
152 }
San Mehatd1830422010-01-15 08:02:39 -0800153
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700154 int offset = 0;
155 int i = 0;
San Mehatd1830422010-01-15 08:02:39 -0800156
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700157 for (i = 0; i < rc; i++) {
158 if (buffer[i] == '\0') {
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700159 char tmp[4];
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700160 strncpy(tmp, buffer + offset, 3);
161 tmp[3] = '\0';
Bernie Innocenti1bcc25a2018-08-10 17:15:00 +0900162 long code = strtol(tmp, nullptr, 10);
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700163
164 printf("%s\n", buffer + offset);
165 if (stop_after_cmd) {
166 if (code >= 200 && code < 600)
167 return 0;
San Mehatd1830422010-01-15 08:02:39 -0800168 }
Rom Lemarchandbfa9b6a2016-03-22 14:19:30 -0700169 offset = i + 1;
San Mehatd1830422010-01-15 08:02:39 -0800170 }
171 }
172 }
173 free(buffer);
174 return 0;
175}
176
177static void usage(char *progname) {
JP Abgrallbe746812013-06-20 11:18:07 -0700178 fprintf(stderr, "Usage: %s [<sockname>] ([monitor] | ([<cmd_seq_num>] <cmd> [arg ...]))\n", progname);
San Mehatd1830422010-01-15 08:02:39 -0800179 exit(1);
180}