Tuesday, April 15, 2014

C on Android

Quick notes about C on Android:

Makefile:
CC=/opt/arm-2009q1/bin/arm-none-linux-gnueabi-gcc
CFLAGS=-static

all: hello
hello.c:
#include <stdio.h>

int main(int argc, char** argv) {
   printf("hello world\n");
   return 0;
}
Build and run:
ctest$ make hello
/opt/arm-2009q1/bin/arm-none-linux-gnueabi-gcc -static    hello.c   -o hello
ctest$ adb push hello /sdcard/
282 KB/s (587310 bytes in 2.029s)
ctest$ adb shell /sdcard/hello
/system/bin/sh: /sdcard/hello: can't execute: Permission denied
Needs exec permission on sdcard, so create tmpfs with that permission, and group id (sdcard_rw)
ctest$ adb shell id
uid=2000(shell) gid=2000(shell) groups=1003(graphics),1004(input),1007(log),1009(mount),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0
ctest$ adb shell
shell@msm8974:/ $ su
shell@msm8974:/ # mkdir /sdcard/bin
shell@msm8974:/ # mount -t tmpfs -o gid=1015 none /sdcard/bin
shell@msm8974:/ # mount
rootfs / rootfs ro,relatime 0 0
tmpfs /dev tmpfs rw,seclabel,nosuid,relatime,size=956332k,nr_inodes=177623,mode=755 0 0
devpts /dev/pts devpts rw,seclabel,relatime,mode=600 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,seclabel,relatime 0 0
selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
none /acct cgroup rw,relatime,cpuacct 0 0
tmpfs /mnt/secure tmpfs rw,seclabel,relatime,size=956332k,nr_inodes=177623,mode=700 0 0
tmpfs /mnt/asec tmpfs rw,seclabel,relatime,size=956332k,nr_inodes=177623,mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,seclabel,relatime,size=956332k,nr_inodes=177623,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,relatime,cpu 0 0
/dev/block/platform/msm_sdcc.1/by-name/system /system ext4 rw,seclabel,relatime,discard,data=ordered 0 0
/dev/block/platform/msm_sdcc.1/by-name/userdata /data ext4 rw,seclabel,nosuid,nodev,relatime,discard,noauto_da_alloc,data=ordered 0 0
/dev/block/platform/msm_sdcc.1/by-name/cache /cache ext4 rw,seclabel,nosuid,nodev,relatime,data=ordered 0 0
/dev/block/platform/msm_sdcc.1/by-name/persist /persist ext4 rw,seclabel,nosuid,nodev,relatime,data=ordered 0 0
/dev/block/platform/msm_sdcc.1/by-name/modem /firmware vfat ro,relatime,uid=1000,gid=1000,fmask=0337,dmask=0227,codepage=cp437,iocharset=iso8859-1,shortname=lower,errors=remount-ro 0 0
/dev/fuse /mnt/shell/emulated fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0
/dev/fuse /storage/emulated/legacy fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0
none /storage/emulated/legacy/bin tmpfs rw,seclabel,relatime,gid=1015 0 0
none /mnt/shell/emulated/0/bin tmpfs rw,seclabel,relatime,gid=1015 0 0
shell@msm8974:/ # ^D
shell@msm8974:/ $ ^D                                                          
Try running again
ctest$ adb push hello /sdcard/bin
128 KB/s (587310 bytes in 4.476s)
ctest$ adb shell /sdcard/bin/hello
hello world