Execute shell commands - android

I developed a standalone executable for Android, and I try to execute some shell commands in the c++ code.
I firstly tried to use popen and it worked on some devices but there is a specific device which fails.
exec and fork also not working on this device and even "system()" doesn't.
I understand the libc of Android (bionic) may not include these calls but I can't find a generic way to execute some shell commands which works in all of my devices.
I also couldn't find any documentation about it in the NDK documentation.
That is very weird because Android Java API expose the "Runtime.exec()" function which works great, then why it's so hard to do it in the native side.
EDIT:
errno of system is "Success".

Related

Compile and run ARM assembly code via SSH on Android Lollipop

I am trying to run some ARM assembly codes on Snapdragon 810 development board to evaluate performance on Cortex-A53 and A57 processors. My codes are nothing to do with android applications and they are C/Assembly coeds. Moreover, I want to get remote access to the board preferably via SSH and run gcc commands. The board default OS in Android Lollipop and my first intention was to install Linux on it to make things work desirably. However, Qualcomm customer support informed me that Linux isn't supported by this board and I have to deal with Android.
I've already searched over various forums. Some of them suggest to root the Android device, install QuickSSHD or SSHDroid on the device and simply SSH to it. However, I am not sure if the provided console has the capability of running gcc commands, generating executable and running it. Others, suggest to generate executable using cross-compilation and push the executable via adb console and run it on the Android device. This approach makes more sense, but I need to have remote access via SSH to the board and edit my code on the device continuously.
My question is, what is the best and easiest approach to get remote access via SSH to this device, compile and run C/ARM Assembly code, transfer files and get the real timing of my codes?!
Cross compile is the easiest option to generate the executable. Else you will have to port GCC to the target first. Don't even bother.
And Adb is a lot better when dealing with Android devices as you need not install any additional applications/executables to get it working. Adb can work on TCP connection as well. So there is no need of SSH for the task. And if the device is rooted, "adb root" followed by "adb shell" would give you the root console.

How to invoke "./configure; make; make install" on Android adb

I have a program that I would like to install on an android phone (x86). The program can be installed on a Linux PC. The procedure of installing the program on the PC is:
$./configure
$make
$make install
Now I would like to do the above through android ADB so I can install it on the phone. What are the utilities needed? (I cannot find "make" in busybox http://www.busybox.net/live_bbox/live_bbox.html)
Building your program directly on the phone probably isn't going to be practical. The number of dependencies that make will wind up invoking during the build process will be huge, and either not exist or not work well on the device itself.
It would be better to look at cross-compiling, where you use a different system to build a binary that is suitable for your device.
It wouldn't be possible to provide more specific info without more detail, but be warned: you're probably in for a big project.
Maybe start here? http://forum.xda-developers.com/showthread.php?t=2723240

Android Systrace tool internally uses strace or ftrace?

Systrace Android tool calls internally a tool called atrace which is and extension of ftrace or strace (Linux tools).
If we connect via the emulator console (ADB shell) to Android Jelly Bean we can execute strace tool but we can not execute ftrace tool (command not installed).
Doing some reseach over the Internet I found that strace is a predecessor of ftrace:
http://crtags.blogspot.de/2012/04/dtrace-ftrace-ltrace-strace-so-many-to.html
Looking to Android Source Code, the most "internal" reference that I found is Trace.h file:
http://androidxref.com/4.1.1/xref/frameworks/native/include/utils/Trace.h
I think that this file is then resolved to a native Linux driver.
However, I'm still unable to know if this implementation driver belongs to strace or ftrace. "Normally", it should be ftrace because it is newer, but in that case I don't know why we can not run ftrace from the emulator. In contrast, strace is completely available from the emulator.
Then, does someone knows if Systrace Android tool uses at a very low level strace or ftrace?
Thank you!
I've follow the instructions found here, related to ftrace,:
http://www.linuxforu.com/2010/11/kernel-tracing-with-ftrace-part-1/
Everything works perfect. I wasn't able to execute ftrace before because interacting and retrieving data is done via logical paths.
To execute ftrace, after correctly configuration (many steps...), inside adb shell:
root#android:# cat /sys/kernel/debug/tracing/trace > mytracefile.txt
atrace makes configuration process easier, so we can access to trace information the following way:
root#android:# atrace -s -w -t 100 > mytracefile.txt
On the other hand, I've found information about executing strace here:
http://www.hokstad.com/5-simple-ways-to-troubleshoot-using-strace
All indicated examples were successfully executed in my environment using adb shell.
Interaction and access to results of both tools are very different.
Now I can say that systrace Android tool is based in atrace which in turn is based in ftrace.
strace is also supported but serves to another purposes and is not related to systrace.
Regards,

Android apps: making shell command-line tools that executes stuff in the app

I know how to start activities through a shell, run shell commands through Java, etc. But what about creating a command-line tool in Java? I want the user to be able to use the GUI as well as a CLI (i.e. adb shell or a Terminal emulator).
Is it possible to write the code in Java? Or do I need to delve into the NDK? Being able to write it in Java would be a lot easier due to all the available libraries etc.

How to write dead simple native SERVER program (not app) for Android devices?

First of all, I believe, it's not a duplicate question. I don't want to write a native app with NDK, I just want to use Android device as a cheap Linux server. Server, in this context, means a program, which has no UI. Also, the server is written in bare C++, but uses no libraries at all, so that's why should that question differ from others.
The story
We've written a small server for Linux, it has a webserver interface, so you connect to it with a vanilla browser, and you can play with it. We've compiled it to Linux/x86, Linux/ARM (GuruPlug), Linux/PPC (some kind of NAS), and Mac OS X Darwin platforms.
The Linux/ARM version also runs well on my Palm Pré smartphone. I've just copied the executable onto my phone (in webOS scene, there's no such thing like jailbreak, there's an official "developer mode", which if you activate by typing a secret key combination, you can log in to your phone with ssh or use local ssh app). So, our small server program (daemon, may say) is running on even the smallest Linux devices. (It's funny, when connect with your giga-powered desktop machine's browser to a smartphone server.)
The server program is designed to run on even the smallest Linux (as I said, it runs on NAS, router, smartphone), it requires only STDLIB and PTHREADS.
The question
What't the straight way to get that simple server program run on an Android device?
We have no Android device yet. But I assume, any ARM-based one will do. The server's memory footprint is so small, that it will be no problem.
I assume, the device must be rooted to copy executable to it and run.
I don't want to install a whole new Linux on the device. It should keep running Android, while our server works in the background. The Linux distribution which enough for Dalvik, is enough for our server, I'm pretty sure.
The server should use no external things (USB, touchscreen, audio, camera, mic - thanks, not) only a port for webserver (e.g. 8080), which is not allocated by Android.
I have ARM GCC toolchain, the installation of it is not the part of the question (except if there're tricks).
The installation of the program should be manual (scp, whatever). Also, I don't want to make a package or any automated deployment.
The problem of the launching of the server program is another question, we'd ignore now.
So, I want just do the same thing with an Android device (phone, tablet), just as I did on my webOS device: copy the server program and run on it. Computers are for that, running programs, aren't they? ;)
I have not really tried it, but it should be possible to use the ARM GCC compiler you have, or the one provided by the Android NDK to compile native application, the same as you would use it on your desktop.
If your application is small (not too many files), you can manage the compilation by hand (or in a simple Makefile). The following commands are from the LuaJIT compilation guide for Android:
/tmp$ cat test.c
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
/tmp$ NDK=/opt/ndk
/tmp$ NDKABI=8
/tmp$ NDKVER=$NDK/toolchains/arm-linux-androideabi-4.4.3
/tmp$ NDKP=$NDKVER/prebuilt/darwin-x86/bin/arm-linux-androideabi-
/tmp$ NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
/tmp$ ${NDKP}gcc $NDKF -o test test.c
/tmp$ file test
test: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
Obviously, use linux-x86 instead of darwin-x86 if you are on Linux (not Mac OS X, also not sure about Windows), and change the path to NDK to where you installed it.
Now you have to move the compiled binary somewhere to your Android device, and run adb shell to see if it works:
/tmp$ ADB=/opt/sdk/platform-tools/adb
/tmp$ $ADB push test /data/
332 KB/s (3343 bytes in 0.009s)
/tmp$ $ADB shell
# /data/test
Hello world!
You definitely need a rooted android device.
I recommend that you install busybox to get a bunch of nice command line tools such as wget, tar, gzip and many more.
You can get shell access to your device by using adb shell and then su to get root access. You can either use adb push or wget to put your server on the device.
The /system partition is mounted as read only and depending on the manufacturer there are a lot of protections that restrict you from writing there so you should copy your server somewhere on the /data partition (e.g. mkdir /data/myserver). After this, everything should be pretty straight forward.
Perhaps the question is more a sysadmin issue (how to install your executable on your Android device) than a developer issue (how to cross compile a C program for Android)?
I have a chroot partition with my android galaxy phone. It is arm with floating point processor, so all kinds of compilers work. Warning to you: the busybox/android distro utils don't run as GNU software. Their tar have problem unpacking the first thing I tried. POSIX tar not supported. Their zip have problem with some field 1 or 8. Their mount works like somthing from space. Their partition itself that you will want to have software put on does not support sym link. After a few weeks working on the chroot partition, I start to feel that the whole android is just a new underlying BIOS, with lots of firmware.

Categories

Resources