尝试通过TCP通过adb连接到Android设备时显示“error:closed”

时间:2015-03-27 06:36:16

标签: android tcp adb

我正在ARMv7开发板上构建Android系统。出于某种原因,我想使用“adb shell”从我的PC操作系统。开发板和PC通过以太网连接,因为Android系统使用NFS服务器作为其根文件系统。这是我尝试过的(我在Android设备上有root访问权限):

在Android设备上(通过串口使用putty访问):

android@ubuntu:~$ setprop service.adb.tcp.port 5555
android@ubuntu:~$ stop adbd
android@ubuntu:~$ start adbd

在Ubuntu主机上:

android@ubuntu:~$ adb connect 192.168.0.85:5555
connected to 192.168.0.85:5555
android@ubuntu:~$ adb shell
error: closed
android@ubuntu:~$ adb devices
List of devices attached
192.168.0.85:5555       device

如消息所示,通过adb的连接似乎成功(连接到...),但是我不能“adb shell”。最奇怪的是,我仍然可以通过“adb devices”看到设备连接。

我试图杀死adb服务器并重启它,但它也不起作用。

1 个答案:

答案 0 :(得分:3)

我研究了adb的源代码,用gdb调试,最后找到了根本原因。

基本上,为了响应主机命令adb shelladbd(在Android设备上运行的守护程序)应该打开一个伪终端,并分叉另一个子进程来运行shell。这些是在create_subproc_pty中的system/core/adb/services.c函数中实现的:

static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
{
    ....

    int ptm;

    ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
    if(ptm < 0){
        printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
        return -1;
    }

    ....

    *pid = fork();
    if(*pid < 0) {
        printf("- fork failed: %s -\n", strerror(errno));
        adb_close(ptm);
        return -1;
    }

    ....
}

我在开发板上发现,unix_open功能失败了。这是因为PTY驱动程序没有内置到内核中,因此在系统上找不到设备/dev/ptmx

要解决此问题,只需选择Character Devices - Unix98 PTY驱动程序并重建内核,然后adb shell即可。