Unable to do AF_UNIX socket IPC in Android using NDK: Operation not permitted

瘦欲@ 提交于 2019-12-11 03:49:24

问题


I am attempting to use unix-domain sockets and am developing under the NDK. I am routinely receiving Operation not permitted errors for various actions related to trivial AF_UNIX/SOCK_DGRAM type IPC.

I am able to successfully bind both processes to a particular interface but am unable to either connect or send/sendto without getting the error mentioned above. The bound interfaces do appear on the file system if an appropriate ls is done in the specified directory. Also, each interface name is given explicit other-read/write access using chmod to prevent any permissions problems.

Process A (Consumer):

struct sockaddr_un local_addr;
int ctrl_fd;

/* Clean the structure and copy over our observable address */
bzero(&local_addr, sizeof(struct sockaddr_un));
strncpy(local_addr.sun_path, local_sock_name, sizeof(local_sock_name));
local_addr.sun_family = AF_UNIX;

ctrl_fd = socket(AF_UNIX, SOCK_DGRAM, 0);

bind(ctrl_fd, (struct sockaddr *) &local_addr, sizeof(struct sockaddr_un));

/* Associate with our path */
connect(ctrl_fd, (struct sockaddr *) &local_addr, sizeof(struct sockaddr_un));

while(1){
    recvLen = recv(ctrl_fd, buf, sizeof(buf), 0);
    printf("Received: %s\n", buf);
}

Process B (Producer):

int sock_fd;
struct sockaddr_un dest_addr, local_addr;

/* Get our socket handle */
sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0);

/* Define where we are to connect */
bzero(&dest_addr, sizeof(struct sockaddr_un));
strncpy(dest_addr.sun_path, dest_name, sizeof(dest_name));
dest_addr.sun_family = AF_UNIX;

/* Define who we are */
bzero(&local_addr, sizeof(struct sockaddr_un));
strncpy(local_addr.sun_path, local_name, sizeof(local_name));
local_addr.sun_family = AF_UNIX;

/* Make a name for ourself */
bind(sock_fd, (struct sockaddr *) &local_addr, sizeof(struct sockaddr_un));

/* Associate fd with remote address */
connect(sock_fd, (struct sockaddr *) &dest_addr, sizeof(struct sockaddr_un));

while(1){
    sleep(10);
    send(sock_fd, "test", sizeof("test"), 0);
}

However, I have managed only to be denied by Operation not permitted on both connect and send. Additionally, I tried removing the connect call entirely and simply using sendto, but I get the same error.

Note: I have removed any error printing from the snippet for brevity. In the actual code I am determining these errors by checking the return values of each sys-call and doing a strerror(errno) to get the respective error message.

来源:https://stackoverflow.com/questions/32100814/unable-to-do-af-unix-socket-ipc-in-android-using-ndk-operation-not-permitted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!