Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing file descriptor using Android binder

How can I share file descriptor across process using Android binder IPC in C++? Can you post example also?

like image 739
user1844484 Avatar asked Jan 19 '13 11:01

user1844484


People also ask

Can file descriptor be shared?

File descriptors are generally unique to each process, but they can be shared by child processes created with a fork subroutine or copied by the fcntl, dup, and dup2 subroutines.

What is the purpose of binder in Android?

Binder is an Android-specific interprocess communication mechanism, and remote method invocation system. That is, one Android process can call a routine in another Android process, using binder to indentify the method to invoke and pass the arguments between processes.

What is a file descriptor Android?

Instances of the file descriptor class serve as an opaque handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes. The main practical use for a file descriptor is to create a FileInputStream or FileOutputStream to contain it.

What is a binder IPC?

Binder IPC Framework in Android Framework enables a remote invocation of the methods in other processes. A client process communicate to another server process and can run the methods in other process as it is done locally and can get the required data from the server process.


1 Answers

In client process we do the following to perform a binder transaction

remote()->transact(MYTRANSACTION, data, &reply, IBinder::FLAG_ONEWAY);

data and reply are of type Parcel. marshall and unmarshalling is done in native android using Parcel objects. It has the functionality to marshall a file descriptor.

data.writeFileDescriptor(fd);

In server process (i.e, Service in android), we call the following method to read the file descriptor in the server process.

int fd = data.readFileDescriptor();

sharing the file descriptor across process will be handled by the binder driver.

Important : duplicate the received file descriptor before the parcel object is destroyed.

You can find the implementation and explanation for native binder at Android-HelloWorldService

like image 63
digitizedx Avatar answered Oct 11 '22 13:10

digitizedx