I have a class extending android.os.Handler. An instance of this handler is passed to the constructor of a Messenger. The Messenger‘s IBinder from getBinder is passed as the result of onBind events in my service. Messages sent via the binder from remote applications do go to the handler’s handleMessage method, however calls to Binder.getCallingUid and Binder.getCallingPid within handleMessage always return the uid and pid of the service’s process (which is definitely not the same process as the remote application). handleMessage is definitely part of an IPC transaction, isn’t it? So where have I gone wrong? I need this to work for authentication of connecting applications.
Thanks in advance.
Edit
OK. I have this horrible feeling that handleMessage is not part of the IPC transaction, because that happens in separate threads for AIDL which puts messages in a queue for the Messenger. Is there any other way of getting the User ID and Process ID of the caller?
Make a custom
Handlerclass and overridesendMessageAtTime(this is the only overridable posting method in theHandlerclass ), and then use it to create aMessengerreturned fromonBind.In the
sendMessageAtTimemethod, you can get the pid/uid of the calling remote application bygetCallingPidandgetCallingUid.But, in IPC using
Messenger, as opposed to using AIDL,getCallingPidwill always return 0 because the message sending by remote apps is asynchronous; transactions withIBinder.FLAG_ONEWAY.Therefore the uid is the only information available.