2009-03-04 11:32:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-20 06:21:08 +08:00
|
|
|
#define TRACE_TAG TRACE_SERVICES
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2015-02-25 07:51:19 +08:00
|
|
|
#include <errno.h>
|
2013-03-30 09:22:36 +08:00
|
|
|
#include <stddef.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <stdio.h>
|
2015-02-25 07:51:19 +08:00
|
|
|
#include <stdlib.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <string.h>
|
2015-02-25 07:51:19 +08:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !ADB_HOST
|
2015-03-30 02:22:34 +08:00
|
|
|
#include "base/file.h"
|
2015-02-25 07:51:19 +08:00
|
|
|
#include "cutils/android_reboot.h"
|
|
|
|
#include "cutils/properties.h"
|
|
|
|
#endif
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
#include "adb.h"
|
2015-02-25 13:26:58 +08:00
|
|
|
#include "adb_io.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
#include "file_sync_service.h"
|
2015-03-17 05:58:32 +08:00
|
|
|
#include "remount_service.h"
|
2015-02-25 07:51:19 +08:00
|
|
|
#include "transport.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
typedef struct stinfo stinfo;
|
|
|
|
|
|
|
|
struct stinfo {
|
|
|
|
void (*func)(int fd, void *cookie);
|
|
|
|
int fd;
|
|
|
|
void *cookie;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void *service_bootstrap_func(void *x)
|
|
|
|
{
|
2015-02-26 09:51:28 +08:00
|
|
|
stinfo* sti = reinterpret_cast<stinfo*>(x);
|
2009-03-04 11:32:55 +08:00
|
|
|
sti->func(sti->fd, sti->cookie);
|
|
|
|
free(sti);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-21 07:04:53 +08:00
|
|
|
#if !ADB_HOST
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-03-14 04:04:37 +08:00
|
|
|
void restart_root_service(int fd, void *cookie)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
char value[PROPERTY_VALUE_MAX];
|
|
|
|
|
|
|
|
if (getuid() == 0) {
|
|
|
|
snprintf(buf, sizeof(buf), "adbd is already running as root\n");
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, buf, strlen(buf));
|
2009-03-14 04:04:37 +08:00
|
|
|
adb_close(fd);
|
|
|
|
} else {
|
|
|
|
property_get("ro.debuggable", value, "");
|
|
|
|
if (strcmp(value, "1") != 0) {
|
|
|
|
snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, buf, strlen(buf));
|
2009-08-25 06:58:40 +08:00
|
|
|
adb_close(fd);
|
2009-03-14 04:04:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 05:44:17 +08:00
|
|
|
property_set("service.adb.root", "1");
|
2009-03-14 04:04:37 +08:00
|
|
|
snprintf(buf, sizeof(buf), "restarting adbd as root\n");
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, buf, strlen(buf));
|
2009-03-14 04:04:37 +08:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 01:57:20 +08:00
|
|
|
void restart_unroot_service(int fd, void *cookie)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
|
|
|
|
if (getuid() != 0) {
|
|
|
|
snprintf(buf, sizeof(buf), "adbd not running as root\n");
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, buf, strlen(buf));
|
2014-10-07 01:57:20 +08:00
|
|
|
adb_close(fd);
|
|
|
|
} else {
|
|
|
|
property_set("service.adb.root", "0");
|
|
|
|
snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, buf, strlen(buf));
|
2014-10-07 01:57:20 +08:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-25 06:58:40 +08:00
|
|
|
void restart_tcp_service(int fd, void *cookie)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
char value[PROPERTY_VALUE_MAX];
|
2014-01-17 02:53:11 +08:00
|
|
|
int port = (int) (uintptr_t) cookie;
|
2009-08-25 06:58:40 +08:00
|
|
|
|
|
|
|
if (port <= 0) {
|
|
|
|
snprintf(buf, sizeof(buf), "invalid port\n");
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, buf, strlen(buf));
|
2009-08-25 06:58:40 +08:00
|
|
|
adb_close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(value, sizeof(value), "%d", port);
|
|
|
|
property_set("service.adb.tcp.port", value);
|
|
|
|
snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, buf, strlen(buf));
|
2009-08-25 06:58:40 +08:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void restart_usb_service(int fd, void *cookie)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
|
|
|
|
property_set("service.adb.tcp.port", "0");
|
|
|
|
snprintf(buf, sizeof(buf), "restarting in USB mode\n");
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, buf, strlen(buf));
|
2009-08-25 06:58:40 +08:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
|
2015-03-30 02:22:34 +08:00
|
|
|
static bool reboot_service_impl(int fd, const char* arg) {
|
|
|
|
const char* reboot_arg = arg;
|
|
|
|
bool auto_reboot = false;
|
|
|
|
|
|
|
|
if (strcmp(reboot_arg, "sideload-auto-reboot") == 0) {
|
|
|
|
auto_reboot = true;
|
|
|
|
reboot_arg = "sideload";
|
|
|
|
}
|
|
|
|
|
2009-08-05 08:37:51 +08:00
|
|
|
char buf[100];
|
2015-03-30 02:22:34 +08:00
|
|
|
// It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot"
|
|
|
|
// in the command file.
|
|
|
|
if (strcmp(reboot_arg, "sideload") == 0) {
|
|
|
|
if (getuid() != 0) {
|
|
|
|
snprintf(buf, sizeof(buf), "'adb root' is required for 'adb reboot sideload'.\n");
|
|
|
|
WriteStringFully(fd, buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* const recovery_dir = "/cache/recovery";
|
|
|
|
const char* const command_file = "/cache/recovery/command";
|
|
|
|
// Ensure /cache/recovery exists.
|
|
|
|
if (adb_mkdir(recovery_dir, 0770) == -1 && errno != EEXIST) {
|
|
|
|
D("Failed to create directory '%s': %s\n", recovery_dir, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_status = android::base::WriteStringToFile(
|
|
|
|
auto_reboot ? "--sideload_auto_reboot" : "--sideload", command_file);
|
|
|
|
if (!write_status) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
reboot_arg = "recovery";
|
|
|
|
}
|
2009-08-05 08:37:51 +08:00
|
|
|
|
|
|
|
sync();
|
2010-02-25 05:07:23 +08:00
|
|
|
|
2015-03-30 02:22:34 +08:00
|
|
|
char property_val[PROPERTY_VALUE_MAX];
|
|
|
|
int ret = snprintf(property_val, sizeof(property_val), "reboot,%s", reboot_arg);
|
|
|
|
if (ret >= static_cast<int>(sizeof(property_val))) {
|
2013-04-19 03:20:02 +08:00
|
|
|
snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
|
2015-03-30 02:22:34 +08:00
|
|
|
WriteStringFully(fd, buf);
|
|
|
|
return false;
|
2013-04-19 03:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = property_set(ANDROID_RB_PROPERTY, property_val);
|
2009-08-05 08:37:51 +08:00
|
|
|
if (ret < 0) {
|
2013-04-19 03:20:02 +08:00
|
|
|
snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
|
2015-03-30 02:22:34 +08:00
|
|
|
WriteStringFully(fd, buf);
|
|
|
|
return false;
|
2009-08-05 08:37:51 +08:00
|
|
|
}
|
2015-03-30 02:22:34 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reboot_service(int fd, void* arg)
|
|
|
|
{
|
|
|
|
if (reboot_service_impl(fd, static_cast<const char*>(arg))) {
|
|
|
|
// Don't return early. Give the reboot command time to take effect
|
|
|
|
// to avoid messing up scripts which do "adb reboot && adb wait-for-device"
|
|
|
|
while (1) {
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-20 04:52:58 +08:00
|
|
|
free(arg);
|
2009-08-05 08:37:51 +08:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
|
2013-03-22 04:07:42 +08:00
|
|
|
void reverse_service(int fd, void* arg)
|
|
|
|
{
|
2015-02-26 09:51:28 +08:00
|
|
|
const char* command = reinterpret_cast<const char*>(arg);
|
2013-03-22 04:07:42 +08:00
|
|
|
|
|
|
|
if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
|
|
|
|
sendfailmsg(fd, "not a reverse forwarding command");
|
|
|
|
}
|
|
|
|
free(arg);
|
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int create_service_thread(void (*func)(int, void *), void *cookie)
|
|
|
|
{
|
|
|
|
int s[2];
|
2015-02-26 09:51:28 +08:00
|
|
|
if (adb_socketpair(s)) {
|
2009-03-04 11:32:55 +08:00
|
|
|
printf("cannot create service socket pair\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-08-16 00:51:27 +08:00
|
|
|
D("socketpair: (%d,%d)", s[0], s[1]);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-02-26 09:51:28 +08:00
|
|
|
stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
|
|
|
|
if (sti == nullptr) {
|
|
|
|
fatal("cannot allocate stinfo");
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
sti->func = func;
|
|
|
|
sti->cookie = cookie;
|
|
|
|
sti->fd = s[1];
|
|
|
|
|
2015-02-26 09:51:28 +08:00
|
|
|
adb_thread_t t;
|
|
|
|
if (adb_thread_create(&t, service_bootstrap_func, sti)) {
|
2009-03-04 11:32:55 +08:00
|
|
|
free(sti);
|
|
|
|
adb_close(s[0]);
|
|
|
|
adb_close(s[1]);
|
|
|
|
printf("cannot create service thread\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
D("service thread started, %d:%d\n",s[0], s[1]);
|
|
|
|
return s[0];
|
|
|
|
}
|
|
|
|
|
2011-03-17 06:57:42 +08:00
|
|
|
#if !ADB_HOST
|
2014-05-27 09:30:43 +08:00
|
|
|
|
|
|
|
static void init_subproc_child()
|
|
|
|
{
|
|
|
|
setsid();
|
|
|
|
|
2014-06-21 07:30:54 +08:00
|
|
|
// Set OOM score adjustment to prevent killing
|
2014-07-19 11:57:35 +08:00
|
|
|
int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
|
2014-05-27 09:30:43 +08:00
|
|
|
if (fd >= 0) {
|
|
|
|
adb_write(fd, "0", 1);
|
|
|
|
adb_close(fd);
|
|
|
|
} else {
|
2014-06-21 07:30:54 +08:00
|
|
|
D("adb: unable to update oom_score_adj\n");
|
2014-05-27 09:30:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
2014-05-27 09:30:43 +08:00
|
|
|
D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
|
2014-11-12 01:24:11 +08:00
|
|
|
#if defined(_WIN32)
|
2014-05-27 09:30:43 +08:00
|
|
|
fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
|
2011-03-17 06:57:42 +08:00
|
|
|
return -1;
|
2014-11-12 01:24:11 +08:00
|
|
|
#else
|
2009-03-04 11:32:55 +08:00
|
|
|
int ptm;
|
|
|
|
|
2014-07-19 11:57:35 +08:00
|
|
|
ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
|
2009-03-04 11:32:55 +08:00
|
|
|
if(ptm < 0){
|
|
|
|
printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-30 02:32:16 +08:00
|
|
|
char devname[64];
|
|
|
|
if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
|
2009-03-04 11:32:55 +08:00
|
|
|
printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
|
2011-03-17 06:57:42 +08:00
|
|
|
adb_close(ptm);
|
2009-03-04 11:32:55 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-17 06:57:42 +08:00
|
|
|
*pid = fork();
|
|
|
|
if(*pid < 0) {
|
2009-03-04 11:32:55 +08:00
|
|
|
printf("- fork failed: %s -\n", strerror(errno));
|
2011-03-17 06:57:42 +08:00
|
|
|
adb_close(ptm);
|
2009-03-04 11:32:55 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-27 09:30:43 +08:00
|
|
|
if (*pid == 0) {
|
|
|
|
init_subproc_child();
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-07-19 11:57:35 +08:00
|
|
|
int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
|
2014-05-27 09:30:43 +08:00
|
|
|
if (pts < 0) {
|
2011-03-17 06:57:42 +08:00
|
|
|
fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-05-27 09:30:43 +08:00
|
|
|
dup2(pts, STDIN_FILENO);
|
|
|
|
dup2(pts, STDOUT_FILENO);
|
|
|
|
dup2(pts, STDERR_FILENO);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2011-02-02 10:57:41 +08:00
|
|
|
adb_close(pts);
|
2009-03-04 11:32:55 +08:00
|
|
|
adb_close(ptm);
|
|
|
|
|
2011-03-17 06:57:42 +08:00
|
|
|
execl(cmd, cmd, arg0, arg1, NULL);
|
|
|
|
fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
|
|
|
|
cmd, strerror(errno), errno);
|
|
|
|
exit(-1);
|
|
|
|
} else {
|
2009-03-04 11:32:55 +08:00
|
|
|
return ptm;
|
|
|
|
}
|
2014-11-12 01:24:11 +08:00
|
|
|
#endif /* !defined(_WIN32) */
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-05-27 09:30:43 +08:00
|
|
|
|
|
|
|
static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
|
|
|
|
{
|
|
|
|
D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
|
2014-11-12 01:24:11 +08:00
|
|
|
#if defined(_WIN32)
|
2014-05-27 09:30:43 +08:00
|
|
|
fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
|
|
|
|
return -1;
|
2014-11-12 01:24:11 +08:00
|
|
|
#else
|
2014-05-27 09:30:43 +08:00
|
|
|
|
|
|
|
// 0 is parent socket, 1 is child socket
|
|
|
|
int sv[2];
|
2014-08-16 00:51:27 +08:00
|
|
|
if (adb_socketpair(sv) < 0) {
|
2014-05-27 09:30:43 +08:00
|
|
|
printf("[ cannot create socket pair - %s ]\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-08-16 00:51:27 +08:00
|
|
|
D("socketpair: (%d,%d)", sv[0], sv[1]);
|
2014-05-27 09:30:43 +08:00
|
|
|
|
|
|
|
*pid = fork();
|
|
|
|
if (*pid < 0) {
|
|
|
|
printf("- fork failed: %s -\n", strerror(errno));
|
|
|
|
adb_close(sv[0]);
|
|
|
|
adb_close(sv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pid == 0) {
|
|
|
|
adb_close(sv[0]);
|
|
|
|
init_subproc_child();
|
|
|
|
|
|
|
|
dup2(sv[1], STDIN_FILENO);
|
|
|
|
dup2(sv[1], STDOUT_FILENO);
|
2014-06-10 08:30:57 +08:00
|
|
|
dup2(sv[1], STDERR_FILENO);
|
|
|
|
|
2014-05-27 09:30:43 +08:00
|
|
|
adb_close(sv[1]);
|
|
|
|
|
|
|
|
execl(cmd, cmd, arg0, arg1, NULL);
|
|
|
|
fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
|
|
|
|
cmd, strerror(errno), errno);
|
|
|
|
exit(-1);
|
|
|
|
} else {
|
|
|
|
adb_close(sv[1]);
|
|
|
|
return sv[0];
|
|
|
|
}
|
2014-11-12 01:24:11 +08:00
|
|
|
#endif /* !defined(_WIN32) */
|
2014-05-27 09:30:43 +08:00
|
|
|
}
|
2011-03-17 06:57:42 +08:00
|
|
|
#endif /* !ABD_HOST */
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
#define SHELL_COMMAND "/bin/sh"
|
|
|
|
#else
|
|
|
|
#define SHELL_COMMAND "/system/bin/sh"
|
|
|
|
#endif
|
|
|
|
|
2011-03-17 06:57:42 +08:00
|
|
|
#if !ADB_HOST
|
|
|
|
static void subproc_waiter_service(int fd, void *cookie)
|
|
|
|
{
|
2014-01-17 02:53:11 +08:00
|
|
|
pid_t pid = (pid_t) (uintptr_t) cookie;
|
2011-03-17 06:57:42 +08:00
|
|
|
|
|
|
|
D("entered. fd=%d of pid=%d\n", fd, pid);
|
|
|
|
for (;;) {
|
|
|
|
int status;
|
|
|
|
pid_t p = waitpid(pid, &status, 0);
|
|
|
|
if (p == pid) {
|
|
|
|
D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
|
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
D("*** Killed by signal %d\n", WTERMSIG(status));
|
|
|
|
break;
|
|
|
|
} else if (!WIFEXITED(status)) {
|
|
|
|
D("*** Didn't exit!!. status %d\n", status);
|
|
|
|
break;
|
|
|
|
} else if (WEXITSTATUS(status) >= 0) {
|
|
|
|
D("*** Exit code %d\n", WEXITSTATUS(status));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
|
|
|
|
if (SHELL_EXIT_NOTIFY_FD >=0) {
|
|
|
|
int res;
|
2015-02-25 13:26:58 +08:00
|
|
|
res = WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)) ? 0 : -1;
|
2011-03-17 06:57:42 +08:00
|
|
|
D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
|
|
|
|
SHELL_EXIT_NOTIFY_FD, pid, res, errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 09:30:43 +08:00
|
|
|
static int create_subproc_thread(const char *name, const subproc_mode mode)
|
2011-03-17 06:57:42 +08:00
|
|
|
{
|
|
|
|
adb_thread_t t;
|
|
|
|
int ret_fd;
|
2014-05-27 09:30:43 +08:00
|
|
|
pid_t pid = -1;
|
|
|
|
|
|
|
|
const char *arg0, *arg1;
|
|
|
|
if (name == 0 || *name == 0) {
|
|
|
|
arg0 = "-"; arg1 = 0;
|
2011-03-17 06:57:42 +08:00
|
|
|
} else {
|
2014-05-27 09:30:43 +08:00
|
|
|
arg0 = "-c"; arg1 = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case SUBPROC_PTY:
|
|
|
|
ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
|
|
|
|
break;
|
|
|
|
case SUBPROC_RAW:
|
|
|
|
ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "invalid subproc_mode %d\n", mode);
|
|
|
|
return -1;
|
2011-03-17 06:57:42 +08:00
|
|
|
}
|
2014-05-27 09:30:43 +08:00
|
|
|
D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
|
2011-03-17 06:57:42 +08:00
|
|
|
|
2015-02-26 09:51:28 +08:00
|
|
|
stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
|
2011-03-17 06:57:42 +08:00
|
|
|
if(sti == 0) fatal("cannot allocate stinfo");
|
|
|
|
sti->func = subproc_waiter_service;
|
2014-01-17 02:53:11 +08:00
|
|
|
sti->cookie = (void*) (uintptr_t) pid;
|
2011-03-17 06:57:42 +08:00
|
|
|
sti->fd = ret_fd;
|
|
|
|
|
2014-05-27 09:30:43 +08:00
|
|
|
if (adb_thread_create(&t, service_bootstrap_func, sti)) {
|
2011-03-17 06:57:42 +08:00
|
|
|
free(sti);
|
|
|
|
adb_close(ret_fd);
|
2014-05-27 09:30:43 +08:00
|
|
|
fprintf(stderr, "cannot create service thread\n");
|
2011-03-17 06:57:42 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-27 09:30:43 +08:00
|
|
|
D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
|
2011-03-17 06:57:42 +08:00
|
|
|
return ret_fd;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
int service_to_fd(const char *name)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if(!strncmp(name, "tcp:", 4)) {
|
|
|
|
int port = atoi(name + 4);
|
|
|
|
name = strchr(name + 4, ':');
|
|
|
|
if(name == 0) {
|
|
|
|
ret = socket_loopback_client(port, SOCK_STREAM);
|
|
|
|
if (ret >= 0)
|
|
|
|
disable_tcp_nagle(ret);
|
|
|
|
} else {
|
|
|
|
#if ADB_HOST
|
|
|
|
ret = socket_network_client(name + 1, port, SOCK_STREAM);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
|
|
|
|
} else if(!strncmp(name, "local:", 6)) {
|
|
|
|
ret = socket_local_client(name + 6,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
|
|
|
|
} else if(!strncmp(name, "localreserved:", 14)) {
|
|
|
|
ret = socket_local_client(name + 14,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
|
|
|
|
} else if(!strncmp(name, "localabstract:", 14)) {
|
|
|
|
ret = socket_local_client(name + 14,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
|
|
|
|
} else if(!strncmp(name, "localfilesystem:", 16)) {
|
|
|
|
ret = socket_local_client(name + 16,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
|
|
|
|
#endif
|
2013-02-21 07:04:53 +08:00
|
|
|
#if !ADB_HOST
|
2009-03-04 11:32:55 +08:00
|
|
|
} else if(!strncmp("dev:", name, 4)) {
|
2014-07-19 11:57:35 +08:00
|
|
|
ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
|
2009-03-04 11:32:55 +08:00
|
|
|
} else if(!strncmp(name, "framebuffer:", 12)) {
|
|
|
|
ret = create_service_thread(framebuffer_service, 0);
|
|
|
|
} else if (!strncmp(name, "jdwp:", 5)) {
|
|
|
|
ret = create_jdwp_connection_fd(atoi(name+5));
|
|
|
|
} else if(!HOST && !strncmp(name, "shell:", 6)) {
|
2014-05-27 09:30:43 +08:00
|
|
|
ret = create_subproc_thread(name + 6, SUBPROC_PTY);
|
|
|
|
} else if(!HOST && !strncmp(name, "exec:", 5)) {
|
|
|
|
ret = create_subproc_thread(name + 5, SUBPROC_RAW);
|
2009-03-04 11:32:55 +08:00
|
|
|
} else if(!strncmp(name, "sync:", 5)) {
|
|
|
|
ret = create_service_thread(file_sync_service, NULL);
|
|
|
|
} else if(!strncmp(name, "remount:", 8)) {
|
|
|
|
ret = create_service_thread(remount_service, NULL);
|
2009-08-05 08:37:51 +08:00
|
|
|
} else if(!strncmp(name, "reboot:", 7)) {
|
2009-09-20 04:52:58 +08:00
|
|
|
void* arg = strdup(name + 7);
|
2014-05-27 09:30:43 +08:00
|
|
|
if (arg == NULL) return -1;
|
2009-09-20 04:52:58 +08:00
|
|
|
ret = create_service_thread(reboot_service, arg);
|
2009-03-14 04:04:37 +08:00
|
|
|
} else if(!strncmp(name, "root:", 5)) {
|
|
|
|
ret = create_service_thread(restart_root_service, NULL);
|
2014-10-07 01:57:20 +08:00
|
|
|
} else if(!strncmp(name, "unroot:", 7)) {
|
|
|
|
ret = create_service_thread(restart_unroot_service, NULL);
|
2011-04-22 03:53:28 +08:00
|
|
|
} else if(!strncmp(name, "backup:", 7)) {
|
2014-05-27 09:30:43 +08:00
|
|
|
char* arg = strdup(name + 7);
|
2011-04-22 03:53:28 +08:00
|
|
|
if (arg == NULL) return -1;
|
2014-05-27 09:30:43 +08:00
|
|
|
char* c = arg;
|
|
|
|
for (; *c != '\0'; c++) {
|
|
|
|
if (*c == ':')
|
|
|
|
*c = ' ';
|
|
|
|
}
|
|
|
|
char* cmd;
|
|
|
|
if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
|
|
|
|
ret = create_subproc_thread(cmd, SUBPROC_RAW);
|
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
free(arg);
|
2011-05-18 06:52:54 +08:00
|
|
|
} else if(!strncmp(name, "restore:", 8)) {
|
2014-05-27 09:30:43 +08:00
|
|
|
ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
|
2009-08-25 06:58:40 +08:00
|
|
|
} else if(!strncmp(name, "tcpip:", 6)) {
|
|
|
|
int port;
|
2015-01-26 09:38:36 +08:00
|
|
|
if (sscanf(name + 6, "%d", &port) != 1) {
|
2009-08-25 06:58:40 +08:00
|
|
|
port = 0;
|
|
|
|
}
|
2014-01-17 02:53:11 +08:00
|
|
|
ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
|
2009-08-25 06:58:40 +08:00
|
|
|
} else if(!strncmp(name, "usb:", 4)) {
|
|
|
|
ret = create_service_thread(restart_usb_service, NULL);
|
2013-03-22 04:07:42 +08:00
|
|
|
} else if (!strncmp(name, "reverse:", 8)) {
|
|
|
|
char* cookie = strdup(name + 8);
|
|
|
|
if (cookie == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
} else {
|
|
|
|
ret = create_service_thread(reverse_service, cookie);
|
|
|
|
if (ret < 0) {
|
|
|
|
free(cookie);
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 22:22:49 +08:00
|
|
|
} else if(!strncmp(name, "disable-verity:", 15)) {
|
2014-12-04 07:31:57 +08:00
|
|
|
ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
|
|
|
|
} else if(!strncmp(name, "enable-verity:", 15)) {
|
|
|
|
ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
|
2009-03-04 11:32:55 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (ret >= 0) {
|
|
|
|
close_on_exec(ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
struct state_info {
|
|
|
|
transport_type transport;
|
|
|
|
char* serial;
|
|
|
|
int state;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void wait_for_state(int fd, void* cookie)
|
|
|
|
{
|
2015-02-26 09:51:28 +08:00
|
|
|
state_info* sinfo = reinterpret_cast<state_info*>(cookie);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
D("wait_for_state %d\n", sinfo->state);
|
|
|
|
|
2015-02-26 09:51:28 +08:00
|
|
|
const char* err = "unknown error";
|
2009-03-04 11:32:55 +08:00
|
|
|
atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
|
|
|
|
if(t != 0) {
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, "OKAY", 4);
|
2009-03-04 11:32:55 +08:00
|
|
|
} else {
|
|
|
|
sendfailmsg(fd, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sinfo->serial)
|
|
|
|
free(sinfo->serial);
|
|
|
|
free(sinfo);
|
|
|
|
adb_close(fd);
|
|
|
|
D("wait_for_state is done\n");
|
|
|
|
}
|
2013-03-30 09:22:36 +08:00
|
|
|
|
|
|
|
static void connect_device(char* host, char* buffer, int buffer_size)
|
|
|
|
{
|
|
|
|
int port, fd;
|
|
|
|
char* portstr = strchr(host, ':');
|
|
|
|
char hostbuf[100];
|
|
|
|
char serial[100];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
strncpy(hostbuf, host, sizeof(hostbuf) - 1);
|
|
|
|
if (portstr) {
|
|
|
|
if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
|
|
|
|
snprintf(buffer, buffer_size, "bad host name %s", host);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// zero terminate the host at the point we found the colon
|
|
|
|
hostbuf[portstr - host] = 0;
|
2015-01-26 09:38:36 +08:00
|
|
|
if (sscanf(portstr + 1, "%d", &port) != 1) {
|
2013-03-30 09:22:36 +08:00
|
|
|
snprintf(buffer, buffer_size, "bad port number %s", portstr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
|
|
|
|
|
2013-06-21 00:27:13 +08:00
|
|
|
fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
|
2013-03-30 09:22:36 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
D("client: connected on remote on fd %d\n", fd);
|
|
|
|
close_on_exec(fd);
|
|
|
|
disable_tcp_nagle(fd);
|
|
|
|
|
|
|
|
ret = register_socket_transport(fd, serial, port, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
adb_close(fd);
|
|
|
|
snprintf(buffer, buffer_size, "already connected to %s", serial);
|
|
|
|
} else {
|
|
|
|
snprintf(buffer, buffer_size, "connected to %s", serial);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void connect_emulator(char* port_spec, char* buffer, int buffer_size)
|
|
|
|
{
|
|
|
|
char* port_separator = strchr(port_spec, ',');
|
|
|
|
if (!port_separator) {
|
|
|
|
snprintf(buffer, buffer_size,
|
|
|
|
"unable to parse '%s' as <console port>,<adb port>",
|
|
|
|
port_spec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zero-terminate console port and make port_separator point to 2nd port.
|
|
|
|
*port_separator++ = 0;
|
|
|
|
int console_port = strtol(port_spec, NULL, 0);
|
|
|
|
int adb_port = strtol(port_separator, NULL, 0);
|
|
|
|
if (!(console_port > 0 && adb_port > 0)) {
|
|
|
|
*(port_separator - 1) = ',';
|
|
|
|
snprintf(buffer, buffer_size,
|
|
|
|
"Invalid port numbers: Expected positive numbers, got '%s'",
|
|
|
|
port_spec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the emulator is already known.
|
|
|
|
* Note: There's a small but harmless race condition here: An emulator not
|
|
|
|
* present just yet could be registered by another invocation right
|
|
|
|
* after doing this check here. However, local_connect protects
|
|
|
|
* against double-registration too. From here, a better error message
|
|
|
|
* can be produced. In the case of the race condition, the very specific
|
|
|
|
* error message won't be shown, but the data doesn't get corrupted. */
|
|
|
|
atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
|
|
|
|
if (known_emulator != NULL) {
|
|
|
|
snprintf(buffer, buffer_size,
|
|
|
|
"Emulator on port %d already registered.", adb_port);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if more emulators can be registered. Similar unproblematic
|
|
|
|
* race condition as above. */
|
|
|
|
int candidate_slot = get_available_local_transport_index();
|
|
|
|
if (candidate_slot < 0) {
|
|
|
|
snprintf(buffer, buffer_size, "Cannot accept more emulators.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Preconditions met, try to connect to the emulator. */
|
|
|
|
if (!local_connect_arbitrary_ports(console_port, adb_port)) {
|
|
|
|
snprintf(buffer, buffer_size,
|
|
|
|
"Connected to emulator on ports %d,%d", console_port, adb_port);
|
|
|
|
} else {
|
|
|
|
snprintf(buffer, buffer_size,
|
|
|
|
"Could not connect to emulator on ports %d,%d",
|
|
|
|
console_port, adb_port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void connect_service(int fd, void* cookie)
|
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
char resp[4096];
|
2015-02-26 09:51:28 +08:00
|
|
|
char *host = reinterpret_cast<char*>(cookie);
|
2013-03-30 09:22:36 +08:00
|
|
|
|
|
|
|
if (!strncmp(host, "emu:", 4)) {
|
|
|
|
connect_emulator(host + 4, buf, sizeof(buf));
|
|
|
|
} else {
|
|
|
|
connect_device(host, buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send response for emulator and device
|
|
|
|
snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteFdExactly(fd, resp, strlen(resp));
|
2013-03-30 09:22:36 +08:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
asocket* host_service_to_socket(const char* name, const char *serial)
|
|
|
|
{
|
|
|
|
if (!strcmp(name,"track-devices")) {
|
|
|
|
return create_device_tracker();
|
|
|
|
} else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
|
2015-02-26 09:51:28 +08:00
|
|
|
auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info)));
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
if (serial)
|
|
|
|
sinfo->serial = strdup(serial);
|
|
|
|
else
|
|
|
|
sinfo->serial = NULL;
|
|
|
|
|
|
|
|
name += strlen("wait-for-");
|
|
|
|
|
|
|
|
if (!strncmp(name, "local", strlen("local"))) {
|
|
|
|
sinfo->transport = kTransportLocal;
|
|
|
|
sinfo->state = CS_DEVICE;
|
|
|
|
} else if (!strncmp(name, "usb", strlen("usb"))) {
|
|
|
|
sinfo->transport = kTransportUsb;
|
|
|
|
sinfo->state = CS_DEVICE;
|
|
|
|
} else if (!strncmp(name, "any", strlen("any"))) {
|
|
|
|
sinfo->transport = kTransportAny;
|
|
|
|
sinfo->state = CS_DEVICE;
|
|
|
|
} else {
|
|
|
|
free(sinfo);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = create_service_thread(wait_for_state, sinfo);
|
|
|
|
return create_local_socket(fd);
|
2013-03-30 09:22:36 +08:00
|
|
|
} else if (!strncmp(name, "connect:", 8)) {
|
|
|
|
const char *host = name + 8;
|
|
|
|
int fd = create_service_thread(connect_service, (void *)host);
|
|
|
|
return create_local_socket(fd);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* ADB_HOST */
|