7
votes

I am trying to run a shell script through an android app. The script has a command which just runs a jar on the device. When I run this command directly on the shell using adb, everything works fine. But when I run it through the script using the android app, I get a permission denied exception (open failed: EACCES (Permission denied)) for the files created in /data/local/tmp folder. Can anyone guide in how to resolve this issue?

This is what my manifest looks like

<manifest ….>
   <uses-sdk …>
    <uses-permission… .>
    …
    <uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    …
    <application …>
    …
    </application>
</manifest>

Thanks.

4

4 Answers

11
votes

If I understand the scenario correctly, you create the script on the fly, and use /data/local/tmp as an easy location that is both publicly writable and executable. Once, this was possible. But on recent versions of Android, security has been tightened.

Your app can execute files under /data/data/${your.package}. You can use getContext().getFilesDir() to reliably obtain the full path. Note that you still need to use chmod 500 to ensure that the file has executable permission.

If you have some fixed executables (binaries or scripts) that must be installed with your app, there is a clever trick to let the system package installer take care of all that for you: make sure the file has a name "libsomething.so" and put it in /libs/armeabi directory under the Eclipse project root. After installation, the files will be present in getContext().getApplicationInfo().nativeLibraryDir directory with executable permissions set.


PS You don't need the WRITE_EXTERNAL_STORAGE permission for this to work (maybe you need it for other things your app does).


PPS You can run a shell script from anywhere, including /sdcard, and you don't need executable permission for the script. Instead, use sh -c source full.path.to.script.

2
votes

You can use the adb interface to copy and/or push files to the /data/local/tmp folder but if you want to use/see them in the terminal app you will need to (in adb interface) first

cd /data/local/tmp 

then make a folder inside the folder. Example

mkdir folder 

next change the permissions

chmod - R 777 folder 

Now you have a folder you can read and write to.

A few things that I would like to know is how to make the system think that the su binary is in the /system/bin folder (without copying) because I can only get tmp... root access because even with root access, I can not remount the system directory as rw because the zte-paragon has its system partition formatted as a read-only file-system

0
votes

I think the reason is that your Andorid is not been rooted. the "/data" have the root Permission. So root your phone first, and give your app the permission. you can try command:"su" int the shell change to root.

0
votes

Make sure the /data/local/tmp location is world-executable on your device:

adb shell ls -ld /data/ /data/local/ /data/local/tmp/

drwxrwx--x system   system            XXXX-XX-XX XX:XX
drwxr-x--x root     root              XXXX-XX-XX XX:XX
drwxrwx--x shell    shell             XXXX-XX-XX XX:XX
#        ^ all x here == OK!

If it's not, there's nothing you can do, and you need to use the standard storage (internal /data/data/{package} or external /sdcard/).

Make sure you made the file world-readable.

adb shell chmod o+r /data/local/tmp/myfile.txt

You don't need any permissions.