ParcelFileDescriptor.detachFD() and JNI

591 views
Skip to first unread message

Lance Blais

unread,
Oct 6, 2013, 12:53:16 PM10/6/13
to andro...@googlegroups.com
I've seen a few JNI related questions, so I hope this is an ok place for them.

I'm trying to share a FD between Java and C in my Android app.

Basically, in Java, I have a ParcelFileDescriptor and I'm calling detachFd() which gives me an int usually in the billions (like 1469054977).

This int is being passed via JNI to a C program which (for now) just read() from that Fd.

When this occurs, I get an error of "Bad file number".

I've attached the C code, hopefully someone has some ideas as to why this doesn't work.

JNIEXPORT jint JNICALL Java_com_example_myapp_Manager_bridgeSendFd(JNIEnv *env, jint fdToSend) {

char buffer[4096];
if (read(fdToSend, buffer, 4096) < 0){
__android_log_print(ANDROID_LOG_ERROR, APPNAME, "read() from fdToSend failed: %s (fd = %d)", strerror(errno), fdToSend);
return (jint)-1;
}
        return 0;
}

David Given

unread,
Oct 6, 2013, 1:46:44 PM10/6/13
to andro...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 06/10/13 17:53, Lance Blais wrote:
[...]
> Basically, in Java, I have a ParcelFileDescriptor and I'm calling
> detachFd() which gives me an int usually in the billions (like
> 1469054977).

That's definitely not a valid file descriptor --- it should be a small
integer (tens or low hundreds). My guess is that the
ParcelFileDescriptor doesn't actually contain a file descriptor. In
hex the value you give above is 0x57900001, which is vaguely suspicious...

- --
???? ?????????????? ????? http://www.cowlark.com ?????
? "I must have spent at least ten minutes out of my life talking to
? this joker like he was a sane person. I want a refund." --- Louann
? Miller, on rasfw
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Using GnuPG with Icedove - http://www.enigmail.net/

iEYEARECAAYFAlJRogEACgkQf9E0noFvlzjJFgCghTLBFuC7ymNqjODdDKTOyo2S
eOwAnjB223xO4N0o9BqY827QFeQC6MRE
=mGVE
-----END PGP SIGNATURE-----

Lance Blais

unread,
Oct 6, 2013, 1:54:47 PM10/6/13
to andro...@googlegroups.com
Ok, so on the Java side it's in the 35 - 50 region. On the JNI side, it's a ridiculous number.

It's being passed as a jint to the JNI function. Is it possible that it's not being cast correctly?

Dianne Hackborn

unread,
Oct 6, 2013, 1:55:48 PM10/6/13
to android-ndk
The fd should be a small integer.  Can you open the ParcelFileDescriptor on the Java side?


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/groups/opt_out.



--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Dianne Hackborn

unread,
Oct 6, 2013, 1:56:20 PM10/6/13
to android-ndk
Yeah there is something wrong with your JNI code.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/groups/opt_out.

Lance Blais

unread,
Oct 6, 2013, 2:00:22 PM10/6/13
to andro...@googlegroups.com
Ok, I got it. The JNI definition is wrong (my bad).

The first arg is the JNIEnv, the second is a jclass, then it's the Args to be passed from Java.

I guess I was reading bad memory or something. It's good now.



On Sunday, October 6, 2013 12:53:16 PM UTC-4, Lance Blais wrote:
Reply all
Reply to author
Forward
0 new messages