我正在包装libril库,并试图从那里打开一个套接字。尝试在/dev/socket中打开套接字失败,因为rild以用户"radio"的身份运行,并且它没有权限,在抽象命名空间中打开套接字似乎可以,但客户端(android应用程序)在尝试连接时会收到"permission denied"。我在纯Linux下制作了一个测试客户端和服务器,即使其中一个作为root用户运行,另一个作为低权限用户在不同的组中运行,两个进程也会连接起来。
有什么想法可以在电话上实现吗?我完善了部分绑定和连接中的代码,并给出了适当的长度。请注意,如果套接字配置不正确或服务器或客户端的长度不正确,则错误为"拒绝连接"。现在我得到"权限被拒绝"。我将感谢所有的帮助,包括如何通过其他方式进行沟通的想法。
服务器:
#define SOCKET_ADDR "test.socket"
void* initSRV(void* dummy) {
int err = 0;
int fdListen = -1;
int fdConnect = -1;
int i;
char buf[4096];
struct sockaddr_un sock;
socklen_t len = 0;
memset(&sock, 'x', sizeof(struct sockaddr_un));
sock.sun_family = AF_UNIX;
sock.sun_path[0] = ' ';
strncpy(sock.sun_path+1, SOCKET_ADDR, strlen(SOCKET_ADDR));
len = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(SOCKET_ADDR);
LOGD("Create socket %s. len=%d", SOCKET_ADDR, len);
fdListen = socket(AF_UNIX, SOCK_STREAM, 0);
LOGD("Bind socket %s.", SOCKET_ADDR);
err = bind(fdListen, (struct sockaddr*)(&sock), len);
if(0 != err) {
LOGE("Unable to bind socket errno:%d [%s]", errno, strerror(errno));
}
LOGD("Listening on socket %s.", SOCKET_ADDR);
err = listen(fdListen, 1);
if(0 != err) {
LOGE("Unable to listen socket errno:%d [%s]", errno, strerror(errno));
return(0);
}
while(1) {
LOGD("Accepting connections on socket [%d]%s.", len, SOCKET_ADDR);
fdConnect = accept(fdListen, (struct sockaddr *) &sock, &len);
if(0 > fdConnect) {
LOGE("Error accepting on socket: [%s]n",strerror(errno));
return(0);
}
while(len = recv(fdConnect, &buf, sizeof(buf), 0), len > 0) {
//echo back what we received
send(fdConnect, &buf, len, 0);
}
}
close(fdConnect);
}
客户:
m_socket = new LocalSocket();
m_socket_address = new LocalSocketAddress(SOCKET_NAME, LocalSocketAddress.Namespace.ABSTRACT);
try{
m_socket.connect(m_socket_address);
} catch (IOException e) {
m_socket = null;
m_log.e(e);
}
if(null != m_socket && m_socket.isConnected()) {
try {
m_socket.close();
} catch (IOException e) {
m_log.e(e);
} finally {
m_socket = null;
}
}
日志:
06-23 22:59:26.631 22681-22716/--- W/System.err? java.io.IOException: Permission denied
06-23 22:59:26.631 22681-22716/--- W/System.err? at android.net.LocalSocketImpl.connectLocal(Native Method)
06-23 22:59:26.631 22681-22716/--- W/System.err? at android.net.LocalSocketImpl.connect(LocalSocketImpl.java:293)
06-23 22:59:26.631 22681-22716/--- W/System.err? at android.net.LocalSocket.connect(LocalSocket.java:130)
在应用程序具有权限的路径/data/data/your.app.package
目录中创建套接字
#define SOCKET_ADDR "/data/data/com.my.app.package/test.socket"
然后您将能够访问它