我正在尝试将指向 ashmem 区域的文件描述符从服务(进程 A)传递到活动(进程 B)。在服务中,我将本机文件描述符放入 ParcelFileDescriptor 并将其放入包中,然后通过 Messenger 发送。但是,当我尝试在 Activity 中使用此文件描述符来 mmap() 时,我得到 errno == 9(EBADF,错误文件号)。
在 Service 的 JNI 中创建 ashmem 区域:
int fd;
int *buff;
JNIEXPORT jint JNICALL Java_com_example_testservice_Test_getTestFD
/* int Test.getTestFD() */
(JNIEnv * env, jclass jthis) {
fd = open("/dev/ashmem", O_RDWR); // I couldn't find library with ashmem_create_region
ioctl(fd, ASHMEM_SET_NAME, "my_mem");
ioctl(fd, ASHMEM_SET_SIZE, 640*480*12);
buff = mmap(NULL, 640*480*12, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(buff == MAP_FAILED)
return -1;
buff[0] = 4;
buff[1] = 5;
buff[2] = 6;
return fd;
}
将 fd 从 Service 发送到 Activity:
int nFD = Test.getTestFD();
ParcelFileDescriptor fd;
try {
fd = ParcelFileDescriptor.fromFd(nFD);
Message reply = Message.obtain(this, msg.what, nFD, 0);
Bundle b = new Bundle(1);
b.putParcelable("fd", fd);
reply.setData(b);
if(msg.replyTo == null) { // Activity asks for FD and we reply to it's message
Log.e("TestService", "Missing replyTo");
} else {
try {
msg.replyTo.send(reply);
} catch (RemoteException e) {
e.printStackTrace();
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
在 Activity 中接收 fd:
Bundle bundle = msg.getData();
ParcelFileDescriptor fd = bundle.getParcelable("fd");
int nFD = fd.getFd();
Log.d("ClientLib", "Received FD (Parcel): "+String.valueOf(nFD));
Log.d("ClientLib", "Received FD (raw): "+String.valueOf(msg.arg1));
int ret = Interface.SetDataFD(nFD); // implementation below
if(ret != 0)
Log.e("ClientLib", "SetDataFD (parcel) failed. Error code: "+String.valueOf(ret));
ret = Interface.SetDataFD(msg.arg1);
if(ret != 0)
Log.e("ClientLib", "SetDataFD (raw) failed. Error code: "+String.valueOf(ret));
在 Activity 的 JNI 中处理 fd :
int data_fd = -1;
JNIEXPORT
jint JNICALL Java_com_example_clientlib_Interface_SetDataFD
/* int SetDataFD(int fd) */
(JNIEnv * env, jclass jthis, jint fd)
{
data_fd = fd;
int * blah = (int*)mmap(NULL, 16, PROT_READ, MAP_SHARED, data_fd, 0);
if(blah == MAP_FAILED)
return errno;
return 0;
}
这是我在 logcat 中得到的:
D/ClientLib(22104): Received FD (Parcel): 53
D/ClientLib(22104): Received FD (raw): 49
E/ClientLib(22104): SetDataFD (parcel) failed. Error code: 9
E/ClientLib(22104): SetDataFD (raw) failed. Error code: 19
这里有什么问题?我什至不确定这是否是传递文件描述符的问题,我不知道我还能检查什么。