How can I share file descriptor across process using Android binder IPC in C++? Can you post example also?
Asked
Active
Viewed 5,408 times
1 Answers
7
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
qtmfld
- 2,606
- 2
- 16
- 33
digitizedx
- 396
- 3
- 14
-
what do you mean by duplicating the received file descriptor before the parcel object is destroyed? – Lewisou Jun 19 '17 at 07:15
-
1I got it. I always got illegal fd errors when doing mmap until I used the dup system call to duplicate the fd. Your "Note" statement is really important!!! – Lewisou Jun 19 '17 at 07:27