android - Send Java FD via UNIX Domain sockets using JNI -


i writting application android passes java fd taken parcelfiledescriptor.getfd() according [1] states int native fd.

now, fd, trying write on unix domain socket existing process listening. this, using jni , pass int received above jni function argument named fdtosend.

when jni code attempts call sendmsg(), error occurs stating "bad file number".

with google, seems socket connection might closed when call sendmsg(), cannot see how case.

the sendfd() method found in [2].

below bridging jni function:

jniexport jint jnicall java_com_example_myapp_appmanager_bridgesendfd(jnienv *env, jint fdtosend) {     int fd;     struct sockaddr_un addr;      if ( (fd = socket(af_unix, sock_stream, 0)) == -1) {         __android_log_print(android_log_error, appname, "socket() failed: %s (socket fd = %d)\n", strerror(errno), fd);         return (jint)-1;     }      memset(&addr, 0, sizeof(addr));     addr.sun_family = af_unix;     strncpy(addr.sun_path, "/data/data/com.example.myapp/sock_path", sizeof(addr.sun_path)-1);      if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {         __android_log_print(android_log_error, appname, "connect() failed: %s (fd = %d)\n", strerror(errno), fd);         return (jint)-1;     }      return (jint)sendfd(fd, (int)fdtosend); } 

[1] http://developer.android.com/reference/android/os/parcelfiledescriptor.html#getfd()

[2] https://stackoverflow.com/a/4491203/2796346

turns out issue jni function declaration.

the first arg jnienv, second jclass it's values passed in java (i.e. me, fdtosend).

so, guess since using second arg (supposed jclass), i'm getting memory reference or weird that.

as seen here: https://stackoverflow.com/a/10743451/2796346


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -