无法连接到Android 5.1上的本机本地套接字

时间:2015-06-26 08:26:55

标签: android sockets android-ndk android-networking unix-socket

我有commad-line工具,它发送广播和等待结果。

服务器代码(省略错误处理):

    int makeAddr(const char* name, struct sockaddr_un* pAddr, socklen_t* pSockLen)
    {
        int nameLen = strlen(name);
        pAddr->sun_path[0] = '\0';
        strcpy(pAddr->sun_path+1, name);
        pAddr->sun_family = AF_LOCAL;
        *pSockLen = 1 + nameLen + offsetof(struct sockaddr_un, sun_path);
        return 0;
    }

    int main(int argc, char* argv[])
    {
        //...

        // Create socket in abstract namespace
        struct sockaddr_un sockAddr = {0};
        socklen_t sockLen;
        makeAddr("SOCKET_NAME", &sockAddr, &sockLen);

        int serverFd = socket(AF_LOCAL, SOCK_STREAM, PF_UNIX));

        bind(serverFd, (const struct sockaddr*) &sockAddr, sockLen);

        listen(serverFd, 5);

        //set socket non-blocking
        int flags = fcntl(serverFd, F_GETFL, 0);
        fcntl(serverFd, F_SETFL, flags | O_NONBLOCK);

        pollfd pollFd = {0};
        pollFd.fd = serverFd;
        pollFd.events = POLLIN | POLLRDHUP;

        // Send brodcast 
        system("am broadcast ...");

        for(;;)
        {
            // Wait result 
            int pollResult = poll(&pollFd, 1, timeout);
        // Process result

Java客户端代码:

public class CommandReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

            LocalSocket socket = new LocalSocket();
            LocalSocketAddress address = new LocalSocketAddress("SOCKET_NAME", LocalSocketAddress.Namespace.ABSTRACT);
            socket.connect(address);

            // Do something and send result asynchronously 

这一切都适用于Android之前的5.1。

在android 5.1上,connect()抛出异常:

  

java.io.IOException:Permission denied

     

在android.net.LocalSocketImpl.connectLocal(原生方法)

     

在android.net.LocalSocketImpl.connect(LocalSocketImpl.java:290)

     

在android.net.LocalSocket.connect(LocalSocket.java:130)

在日志中有错误:

  

07-27 09:56:08.179:W:type = 1400 audit(0.0:3675):avc:拒绝{connectto} for path = 00636F6D2E6B6173706572736B792E6B617368656C6C2E534F434B45542D36313838363731373231343337393833373637 scontext = u:r:untrusted_app:s0 tcontext = u:r:shell :s0 tclass = unix_stream_socket permissive = 0

我发现了相对的android修复: https://android.googlesource.com/device/moto/shamu/+/b2db40f

那么,自从Android 5.1以后它将不再起作用了? 我能做些什么(可能以某种方式设置权限)? 或者是否有另一种机制从本机命令行工具发送数据并等待结果?

1 个答案:

答案 0 :(得分:1)

使用管道代替套接字: O_RDWR on named pipes with poll()

在java进程中创建管道:

        mkfifo(cpath, 0);
        chmod(cpath, S_IRWXU|S_IROTH|S_IRGRP);

在本机进程中打开管道:

int serverFd = open(PIPE_NAME, O_RDONLY | O_NONBLOCK);
pollfd pollFd = {0};
pollFd.fd = serverFd.get();
pollFd.events = POLLIN | POLLRDHUP;

// send command
....

for(;;)
{
    int pollResult = poll(&pollFd, 1, timeout);