LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash vs sh, and Android (https://www.linuxquestions.org/questions/programming-9/bash-vs-sh-and-android-838549/)

jiml8 10-16-2010 06:02 PM

bash vs sh, and Android
 
I'm hacking an android right now (Samsung Intercept with Android 2.1) and I want to make a change to the init script.

I'm new to Android, so I'm still learning a lot of things. One thing is that apparently Android uses a stripped down version of the Bourne shell rather than any variant of Bash.

Further, the Samsung Intercept is a brand new phone and the hacker community is just starting to work on it. I have decided to join them. I have rooted the phone with the ixane ROM and that works well.

Now, this particular Android is hard-coded for everything it does when it boots; there are no conditional things based upon the existence or non-existence of a particular script in a particular directory (such as /etc/init.d).

I want to alter the init script to permit conditional startups. In other words, I want to implement /etc/init.d. In this fashion, I can make further hacks (and I immediately want to hack the system to move applications out of main memory and onto an sd card) without having to reload the kernel.

Now, in bash, I could accomplish this by adding this code fragment to my init script:

Code:

if [ -e /etc/init.d ];then
  for i in /etc/init.d/* ; do
        $i
  done
fi

and every script in directory /etc/init.d would be executed, if that directory exists.

Will this work in sh?

I can find out the hard way - by trying it - but this will involve several hours labor to make the attempt and, if it doesn't work, then I've wasted that time. Me asking is me trying to be efficient with my time.

If this doesn't work, given what the intent is, what sh code *should* work? Androids do have a stripped sh, but some Androids do support conditional startups such as I've depicted here. Mine doesn't, but that's just a thing that I want to correct. If necessary, I'll hack in some sh from some other Android.

How about the busybox shell? Could I do it using busybox?

I have asked the ROM maintainer to make this change, and I've offered to make it for him, but he hasn't responded. If he doesn't respond, I'll either fork his ROM or make my own and distribute it because I definitely want this capability.

hydraMax 10-16-2010 06:14 PM

Quote:

Originally Posted by jiml8 (Post 4129863)
I'm hacking an android right now (Samsung Intercept with Android 2.1) and I want to make a change to the init script.

I'm new to Android, so I'm still learning a lot of things. One thing is that apparently Android uses a stripped down version of the Bourne shell rather than any variant of Bash.

Further, the Samsung Intercept is a brand new phone and the hacker community is just starting to work on it. I have decided to join them. I have rooted the phone with the ixane ROM and that works well.

Now, this particular Android is hard-coded for everything it does when it boots; there are no conditional things based upon the existence or non-existence of a particular script in a particular directory (such as /etc/init.d).

I want to alter the init script to permit conditional startups. In other words, I want to implement /etc/init.d. In this fashion, I can make further hacks (and I immediately want to hack the system to move applications out of main memory and onto an sd card) without having to reload the kernel.

Now, in bash, I could accomplish this by adding this code fragment to my init script:

Code:

if [ -e /etc/init.d ];then
  for i in /etc/init.d/* ; do
        $i
  done
fi

and every script in directory /etc/init.d would be executed, if that directory exists.

Will this work in sh?

I can find out the hard way - by trying it - but this will involve several hours labor to make the attempt and, if it doesn't work, then I've wasted that time. Me asking is me trying to be efficient with my time.

If this doesn't work, given what the intent is, what sh code *should* work? Androids do have a stripped sh, but some Androids do support conditional startups such as I've depicted here. Mine doesn't, but that's just a thing that I want to correct. If necessary, I'll hack in some sh from some other Android.

How about the busybox shell? Could I do it using busybox?

I have asked the ROM maintainer to make this change, and I've offered to make it for him, but he hasn't responded. If he doesn't respond, I'll either fork his ROM or make my own and distribute it because I definitely want this capability.

Busybox would definitely work... plus you could build a 100 other common *nix utilities into the binary as well.

You got a blog or web site for this project? I've been playing around with my HTC Hero and am looking for good ideas.

GrapefruiTgirl 10-16-2010 06:38 PM

Hi Jim, hope this helps:

That code works fine in (in order of increasing minimalness) Bash, Dash, and Ash shells. I just tried all three. There's nothing Bash-specific about the code.

I'll go out on a limb and suggest that the code will work on whatever bourne-type shell your device has. I am willing to take full responsibility if I'm wrong. ;)

I wonder what they mean exactly by "stripped down Bourne shell"? Bash could be called the "stripped up" version I suppose..

Also, you're probably aware and have already thought of this, but: I presume everything potentially in that directory will be set +executable, and that filenames have no spaces (or if they do, put $i in "quotes" - that works too).

And, if environment (as in $ENV) matters, you might want to `source` the files rather than execute them, but that choice is yours. You can `source` non-executable files too, and the code will still run. Note that not all shells support one or the other (or maybe both) of "source" or its short version, "." (a dot).

Good luck!
Sasha

jiml8 10-16-2010 07:09 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4129883)
Hi Jim, hope this helps:

That code works fine in (in order of increasing minimalness) Bash, Dash, and Ash shells. I just tried all three. There's nothing Bash-specific about the code.

Well, that is exactly what I wanted to know. Busybox uses ash, so if it works in ash, it *should* work in busybox.

Quote:

I'll go out on a limb and suggest that the code will work on whatever bourne-type shell your device has. I am willing to take full responsibility if I'm wrong. ;)
I'm pretty sure it won't work, which is why I am asking. I found a website that describes the command set. It doesn't show any support for looping. I also have been looking over the scripts in the system, and there are no loops.

However, Android hackers are adding busybox to everything. There must be a reason for that; I was assuming it was because it was a space-conservative way to add a lot of capability.

So. The code fragment I defined above will get named /etc/setinitd.sh. I can alter the init script to run the command /sbin/busybox ash /etc/setinitd.sh.

Then I can create /etc/init.d, populate it with the one script I presently need and have the capability to later populate it with anything I need.

I can even test this completely (except for the alterations to the init script) without having to reload the kernel image.

Cool.

jiml8 10-16-2010 07:12 PM

Quote:

Originally Posted by hydraMax (Post 4129867)
Busybox would definitely work... plus you could build a 100 other common *nix utilities into the binary as well.

You got a blog or web site for this project? I've been playing around with my HTC Hero and am looking for good ideas.

Busybox isn't being deployed with Android, but hackers are quickly adding it. I have busybox on mine. I've had mine since Tuesday, and it no longer resembles what it was when I purchased it. :D

As I say, I'm new to Android. Blogs etc are in the future, perhaps. For the moment, I've joined http://forum.sdx-developers.com which is the most complete site I've found for the Samsung Intercept. Again, this is a brand new phone.

This is where I got the kernel I installed, and the recovery, and the JIT that enhanced the speed by a factor of 6, and where I've left a message for the guy who rooted the kernel.

GrapefruiTgirl 10-16-2010 07:45 PM

I don't see on that website you linked, any section about "shell syntax" or "shell commands" - many of what I see there in the "Commands" section and below, appear to me to be binary commands or scripts. Am I missing something on that page?

However, it's interesting that you haven't seen a single loop anywhere in the system scripts. I wonder, did they avoid them to save resources, or to keep code simple, or because the thing doesn't support them... I did come across (via Google) some reports of a malicious Android app+script that "ran in a loop", but there was no code shown.

Well, it's the weekend, so not as many posters around. by the time we finish guessing, and/or someone with an Android shell comes along and says yes or no, you maybe could have built & tested it. :D

I did see somewhere that the [ command may not be present in /bin, so you'd need to use `test` instead of [ ... ] but there too, there weren't much specifics - just that it was a stock Android device.

jiml8 10-16-2010 08:20 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4129910)
I don't see on that website you linked, any section about "shell syntax" or "shell commands" - many of what I see there in the "Commands" section and below, appear to me to be binary commands or scripts. Am I missing something on that page?

However, it's interesting that you haven't seen a single loop anywhere in the system scripts. I wonder, did they avoid them to save resources, or to keep code simple, or because the thing doesn't support them... I did come across (via Google) some reports of a malicious Android app+script that "ran in a loop", but there was no code shown.

Well, it's the weekend, so not as many posters around. by the time we finish guessing, and/or someone with an Android shell comes along and says yes or no, you maybe could have built & tested it. :D

I did see somewhere that the [ command may not be present in /bin, so you'd need to use `test` instead of [ ... ] but there too, there weren't much specifics - just that it was a stock Android device.

Those commands match up to the commands and syntax found in the scripts in the android. Since these are shell scripts, and since the particular commands are not found anyplace in binary directories or elsewhere, I infer that these are the commands used by the stripped sh.

However, I've just been playing with busybox on the android, and the ash shell makes things a lot nicer. I haven't yet tried to execute the command "busybox ash setinit.sh" to see what happens, because I haven't yet pushed the scripts and so forth to the android. But it certainly looks promising.

busybox ash has history at least, which the built-in shell doesn't have. By itself, that speeds things up.

GrapefruiTgirl 10-16-2010 08:24 PM

Sounds good - I'll like to hear if it works as planned or not.

jiml8 10-16-2010 08:48 PM

It works! Just built and tested it on the android. Does everything I want done.

Now it is just a matter of modifying the init script, and the change is permanent. If the ROM maintainer will accept the change, we're good to go forever.

jiml8 10-25-2010 04:06 AM

...and to finish the story, the ROM maintainer added the change, and now other people are putting things in /etc/init.d to deal with their problems. Because the init.d mechanism permits automatically mounting other partitions at boot time, I have moved a lot of stuff to a partition on my sd card, thus freeing a lot of memory in the phone which keeps it running without difficulty. I have also prepared a swap partition on the sd card, which is causing the phone to cache a lot more stuff than it did, which generally is improving performance.

So, this phone is now under my control, and will get changed to suit my whim.

GrapefruiTgirl 10-25-2010 05:05 AM

Quote:

Originally Posted by jiml8 (Post 4138345)
So, this phone is now under my control, and will get changed to suit my whim.

All your base are belong to us! Muwwahahahaha!

:) Nice going Jim, glad to hear you're pwn'ing the thing (is that the term??)


All times are GMT -5. The time now is 05:42 AM.