5

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

Onik
  • 19,396
  • 14
  • 68
  • 91
user1844484
  • 101
  • 1
  • 3

1 Answers1

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,916
  • 2
  • 21
  • 36
digitizedx
  • 386
  • 5
  • 16
  • what do you mean by duplicating the received file descriptor before the parcel object is destroyed? – Lewis Z Jun 19 '17 at 07:15
  • 1
    I 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!!! – Lewis Z Jun 19 '17 at 07:27