Merge change 4227 into donut
* changes: nexctl: Refactor so Nexus can be tested from scripts easily.
This commit is contained in:
commit
2d0a6ca27c
1 changed files with 76 additions and 52 deletions
128
nexus/nexctl.c
128
nexus/nexctl.c
|
|
@ -29,25 +29,59 @@
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
|
||||||
#include <cutils/sockets.h>
|
#include <cutils/sockets.h>
|
||||||
|
|
||||||
#include <private/android_filesystem_config.h>
|
#include <private/android_filesystem_config.h>
|
||||||
|
|
||||||
|
static void usage(char *progname);
|
||||||
|
static int do_monitor(int sock, int stop_after_cmd);
|
||||||
|
static int do_cmd(int sock, int argc, char **argv);
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int sock;
|
int sock;
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
usage(argv[0]);
|
||||||
|
|
||||||
if ((sock = socket_local_client("nexus",
|
if ((sock = socket_local_client("nexus",
|
||||||
ANDROID_SOCKET_NAMESPACE_RESERVED,
|
ANDROID_SOCKET_NAMESPACE_RESERVED,
|
||||||
SOCK_STREAM)) < 0) {
|
SOCK_STREAM)) < 0) {
|
||||||
fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
|
fprintf(stderr, "Error connecting (%s)\n", strerror(errno));
|
||||||
exit(1);
|
exit(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Connected to nexus\n");
|
if (!strcmp(argv[1], "monitor"))
|
||||||
|
exit(do_monitor(sock, 0));
|
||||||
|
exit(do_cmd(sock, argc, argv));
|
||||||
|
}
|
||||||
|
|
||||||
char line[255];
|
static int do_cmd(int sock, int argc, char **argv) {
|
||||||
|
char final_cmd[255] = { '\0' };
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 1; i < argc; i++) {
|
||||||
|
char *cmp;
|
||||||
|
|
||||||
|
if (!index(argv[i], ' '))
|
||||||
|
asprintf(&cmp, "%s%s", argv[i], (i == (argc -1)) ? "" : " ");
|
||||||
|
else
|
||||||
|
asprintf(&cmp, "\"%s\"%s", argv[i], (i == (argc -1)) ? "" : " ");
|
||||||
|
|
||||||
|
strcat(final_cmd, cmp);
|
||||||
|
free(cmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write(sock, final_cmd, strlen(final_cmd) + 1) < 0) {
|
||||||
|
perror("write");
|
||||||
|
return errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
return do_monitor(sock, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int do_monitor(int sock, int stop_after_cmd) {
|
||||||
char *buffer = malloc(4096);
|
char *buffer = malloc(4096);
|
||||||
int cursor = 0;
|
|
||||||
int col = 0;
|
if (!stop_after_cmd)
|
||||||
|
printf("[Connected to Nexus]\n");
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
fd_set read_fds;
|
fd_set read_fds;
|
||||||
|
|
@ -59,66 +93,56 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
FD_ZERO(&read_fds);
|
FD_ZERO(&read_fds);
|
||||||
FD_SET(sock, &read_fds);
|
FD_SET(sock, &read_fds);
|
||||||
FD_SET(0, &read_fds);
|
|
||||||
|
|
||||||
if (col == 0) {
|
|
||||||
fprintf(stdout, "-> ");
|
|
||||||
fflush(stdout);
|
|
||||||
col = 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
|
if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
|
||||||
fprintf(stderr, "Error in select (%s)\n", strerror(errno));
|
fprintf(stderr, "Error in select (%s)\n", strerror(errno));
|
||||||
exit(2);
|
free(buffer);
|
||||||
|
return errno;
|
||||||
} else if (!rc) {
|
} else if (!rc) {
|
||||||
continue;
|
continue;
|
||||||
|
fprintf(stderr, "[TIMEOUT]\n");
|
||||||
|
return ETIMEDOUT;
|
||||||
} else if (FD_ISSET(sock, &read_fds)) {
|
} else if (FD_ISSET(sock, &read_fds)) {
|
||||||
memset(buffer, 0, 4096);
|
memset(buffer, 0, 4096);
|
||||||
if ((rc = read(sock, buffer, 4096)) <= 0) {
|
if ((rc = read(sock, buffer, 4096)) <= 0) {
|
||||||
fprintf(stderr, "Error reading response (%s)\n", strerror(errno));
|
if (rc == 0)
|
||||||
exit(2);
|
fprintf(stderr, "Lost connection to Nexus - did it crash?\n");
|
||||||
}
|
else
|
||||||
int i;
|
fprintf(stderr, "Error reading data (%s)\n", strerror(errno));
|
||||||
for (i = 0; i < col; i++) {
|
free(buffer);
|
||||||
fprintf(stdout, "%c", 8);
|
if (rc == 0)
|
||||||
|
return ECONNRESET;
|
||||||
|
return errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int offset = 0;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
printf("%s", buffer);
|
for (i = 0; i < rc; i++) {
|
||||||
printf("-> ");
|
if (buffer[i] == '\0') {
|
||||||
for (i = 0; i < cursor; i++) {
|
int code;
|
||||||
fprintf(stdout, "%c", line[i]);
|
char tmp[4];
|
||||||
}
|
|
||||||
fflush(stdout);
|
|
||||||
} else if (FD_ISSET(0, &read_fds)) {
|
|
||||||
char c;
|
|
||||||
|
|
||||||
if ((rc = read(0, &c, 1)) < 0) {
|
strncpy(tmp, buffer + offset, 3);
|
||||||
fprintf(stderr, "Error reading from terminal (%s)\n", strerror(errno));
|
tmp[3] = '\0';
|
||||||
exit(2);
|
code = atoi(tmp);
|
||||||
} else if (!rc) {
|
|
||||||
fprintf(stderr, "0 length read from terminal\n");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(stdout, "%c", c);
|
printf("%s\n", buffer + offset);
|
||||||
fflush(stdout);
|
if (stop_after_cmd) {
|
||||||
|
if (code >= 200 && code < 600)
|
||||||
line[cursor] = c;
|
return 0;
|
||||||
|
}
|
||||||
if (c == '\n') {
|
offset = i + 1;
|
||||||
if ((rc = write(sock, line, strlen(line))) < 0) {
|
|
||||||
fprintf(stderr, "Error writing to nexus (%s)\n", strerror(errno));
|
|
||||||
exit(2);
|
|
||||||
}
|
}
|
||||||
memset(line, 0, sizeof(line));
|
|
||||||
cursor = 0;
|
|
||||||
col = 0;
|
|
||||||
} else {
|
|
||||||
cursor++;
|
|
||||||
col++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(buffer);
|
||||||
exit(0);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void usage(char *progname) {
|
||||||
|
fprintf(stderr, "Usage: %s <monitor>|<cmd> [arg1] [arg2...]\n", progname);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue