【发布时间】:2023-04-10 23:05:01
【问题描述】:
我写了一个android ion使用的例子:
父进程创建一个管道,然后打开“/dev/ion”设备,ION_IOC_ALLOC的ioctl,ION_IOC_MAP和mmap的ioctl,最后我把ION_IOC_MAP返回的fd和长度通过管道传递给子进程。
子进程从管道中读取fd和length,读取是ok的,但是我做ION_IOC_IMPORT的时候返回-1,errno是9,perror是“Bad file descriptor”。
这两个进程都是root用户,selinux是许可的。
父进程键码:
ion_fd = open("/dev/ion", O_RDONLY);
if (ion_fd < 0) {
ALOGE("Failed to open ion device\n");
return -EIO;
}
alloc_data.len = 0x1000;
alloc_data.align = 0x1000;
alloc_data.heap_id_mask = ION_HEAP(ION_SYSTEM_HEAP_ID);
alloc_data.flags = ION_SECURE;
rc = ioctl(ion_fd, ION_IOC_ALLOC, &alloc_data);
if (rc) {
ALOGE( "Failed to allocate uspace ion buffer\n");
goto uimp_alloc_err;
}
fd_data.handle = alloc_data.handle;
rc = ioctl(ion_fd, ION_IOC_MAP, &fd_data);
if (rc < 0) {
ALOGE("unable to ion map buffer\n");
goto uimp_map_err;
}
map_fd = fd_data.fd;
addr = mmap(NULL, alloc_data.len,
PROT_READ | PROT_WRITE,
MAP_SHARED , map_fd, 0);
if (!addr) {
ALOGE("mmap failed\n");
rc = -EIO;
goto uimp_mmap_err;
}
write_pattern((unsigned long)addr, alloc_data.len);
fd_data.handle = alloc_data.handle;
rc = ioctl(ion_fd, ION_IOC_SHARE, &fd_data);
if (rc < 0) {
ALOGE( "unable to share ion buffer\n");
goto uimp_share_err;
}
itoa(fd_data.fd, ubuf, sizeof(int) * 8);
if (0 > sock_write(wr_fd, ubuf, sizeof(int) * 8))
goto uimp_sock_err;
itoa(alloc_data.len, ubuf, sizeof(int) * 8);
if (0 > sock_write(wr_fd, ubuf, sizeof(int) * 8))
goto uimp_sock_err;
do {
tpid = wait(&child_status);
} while (tpid != child_pid);
子进程键码:
if (0 > sock_read(fd, cbuf, sizeof(int) * 8))
return -EIO;
fd_data.fd = atoi(cbuf);
/* receive buf length */
if (0 > sock_read(fd, cbuf, sizeof(int) * 8))
return -EIO;
size = atoi(cbuf);
ion_fd = open("/dev/ion", O_RDONLY);
if (ion_fd < 0) {
rc = -EINVAL;
goto child_args_err;
}
rc = ioctl(ion_fd, ION_IOC_IMPORT, &fd_data); // it failed here
if (rc) {
ALOGE( "ION_IOC_IMPORT failed %d errno %d\n", rc, errno);
perror("ioctl");
rc = -EIO;
goto child_imp_err;
}
addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_SHARED, fd_data.fd, 0);
if (!addr) {
perror("mmap");
rc = -EIO;
goto child_mmap_err;
}
【问题讨论】:
标签: android c++ c android-ion