Popen on android NDK

Is popen not supported by Android NDK?

I have read this page and am wondering if this is true.

The same is possible with POSIX popen (), but currently it is not so supported by bionic, so you cannot use it in Android JNI. Instead, you can use system () and transfer the output to a file, and then read that file later. It looks like the Java approach will be if you do the rendering in Java.

But I also read someone suggesting using popen. I also tried this myself, but sometimes my application crashes and I don't know why.

Is it safe to use popups in android ndk?

+4
source share
2 answers

I think it depends on which version of NDK you are using.

Also, looking at the gingerbread sources tree for bionic, I found a popen implementation. The implementation may not be 100% posix libc correct, but it is at least functional to some extent.

Using NDK v6, the following example compiles without any problems and it runs on my Android device.

#include <stdio.h> #include <stdlib.h> int main() { FILE *fpipe; char *command="/system/bin/ps"; char line[256]; if ( !(fpipe = (FILE*)popen(command,"r")) ) exit(1) while ( fgets( line, sizeof line, fpipe)) { puts(line); } pclose(fpipe); } 

UPDATE: It seems that instead of previous versions of ICS, vfork () is used instead of fork (), and it is known that vfork () causes stack corruption.

So, from ICS, onward popen should be kept in use, but on earlier versions of Android it is available, but verry buggy.

+7
source

Indeed, popen() will not work on Android. As described by GLIBC , any UNIX operating system needs a C library to make all system calls. These include the distribution of streaming memory, working with files, etc. Since Android is based on Linux, it needs such a library, but since it is a small mobile OS, Google decided to write a "lite version" of libc bionic. This library does not include popen() , so you cannot use it. The NDK documentation contains a description of the bionic library that lives in your NDK directory (for some reason it is offline). Hope this helps.

+4
source

Source: https://habr.com/ru/post/1391643/


All Articles