LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-16-2010, 06:02 PM   #1
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
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.
 
Old 10-16-2010, 06:14 PM   #2
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
Quote:
Originally Posted by jiml8 View Post
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.
 
Old 10-16-2010, 06:38 PM   #3
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
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
 
1 members found this post helpful.
Old 10-16-2010, 07:09 PM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 114Reputation: 114
Quote:
Originally Posted by GrapefruiTgirl View Post
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.
 
Old 10-16-2010, 07:12 PM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 114Reputation: 114
Quote:
Originally Posted by hydraMax View Post
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.

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.
 
Old 10-16-2010, 07:45 PM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
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.

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.
 
Old 10-16-2010, 08:20 PM   #7
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 114Reputation: 114
Quote:
Originally Posted by GrapefruiTgirl View Post
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.

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.
 
Old 10-16-2010, 08:24 PM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Sounds good - I'll like to hear if it works as planned or not.
 
Old 10-16-2010, 08:48 PM   #9
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 114Reputation: 114
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.
 
Old 10-25-2010, 04:06 AM   #10
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 114Reputation: 114
...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.
 
Old 10-25-2010, 05:05 AM   #11
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Quote:
Originally Posted by jiml8 View Post
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??)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Android Plugin for Eclipse IDE: No option for "Android Project" Kenny_Strawn Programming 20 10-18-2011 09:52 PM
LXer: Android-x86 - run Google Android on a netbook LXer Syndicated Linux News 0 12-06-2009 01:30 PM
LXer: The Curious Case of Android v. Android LXer Syndicated Linux News 0 05-07-2009 01:50 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM


All times are GMT -5. The time now is 06:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration