kagali-san
kagali-san

Reputation: 3082

Android (native): why my socket isn't available to other processes?

So I'm starting a custom native daemon via init.rc. It attempts to do either this:

x_con = android_get_control_socket("test");
int ret = listen(x_con, 1);
int new_s = accept(x_con, (struct sockaddr*)&peeraddr, &socklen);

Or this:

x_con = socket_local_server("test:, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM );
int new_s = accept(x_con, (struct sockaddr*)&peeraddr, &socklen);

On either occasion, I can see socket under /dev/socket, it has access mask of 0666 (due to params set in init.rc) but still can't accept any connection (seemingly being unavailable to other processes).

Is it due to 'Paranoid Network' Android's feature?

Upvotes: 1

Views: 2014

Answers (1)

kagali-san
kagali-san

Reputation: 3082

Android socket model seems to be very strange compared to "normal" sockets.

First, socket has to be initialized via init.rc, given certain permissions.

Next, server application opens it with:

x_con = android_get_control_socket("test");

Next, listen() and fnctl() calls are required:

listen(x_con, 1);
fcntl(x_con, F_SETFD, FD_CLOEXEC);

After that, a LocalSocket connection from Java can access socket, and socket can accept clients.

Setting non-blocking mode later is allowed too:

fnctl(x_con, F_SETFL, O_NONBLOCK);

Other combinations are unnecessary (and there are some guides like Working with the Radio Layer Interface (RIL) in Android - will not link to that low-grade manual - are stating that both socket_local_server and android_get_control_socket are required, which is clearly untrue).

Upvotes: 1

Related Questions