Writing Your Own Shell

Shell

[ad#hlinks]

On system boot up, one can see the login screen. We log in to the system using our user name, followed by our password. The login name is looked up in the system password file (usually /etc/passwd). If the login name is found, the password is verified. The encrypted password for a user can be seen in the file /etc/shadow, immediately preceded by the user name and a colon. Once the password is verified, we are logged into the system.

Once we log in, we can see the command shell where we usually enter our commands to execute. The shell, as described by Richard Stevens in his book Advanced Programming in the Unix Environment, is a command-line interpreter that reads user input and execute commands.

This was the entry point for me. One program (our shell) executing another program (what the user types at the prompt). I knew that execve and its family of functions could do this, but never thought about its practical use.
[ad#ad-2]

A note on execve()

Briefly, execve and its family of functions helps to initiate new programs. The family consists of the functions:

  • execl
  • execv
  • execle
  • execve
  • execlp
  • execvp
int execve(const char *filename, char *const argv[], char *const envp[]);

is the prototype as given in the man page for execve. The filename is the complete path of the executable, argv and envp are the array of strings containing argument variables and environment variables respectively.

In fact, the actual system call is sys_execve (for execve function) and other functions in this family are just C wrapper functions around execve. Now, let us write a small program using execve. See listing below:

listing1.c

Compiling and running the a.out for the above program gives the output of /bin/ls command. Now try this. Put a printf statement soon after the execve call and run the code.

I will not go in to the details of wrappers of execve. There are good books, one of which I have already mentioned (from Richard Stevens), which explains the execve family in detail.

[ad#ad-2]

Some basics

Before we start writing our shell, we shall look at the sequence of events that occur, from the point when user types something at the shell to the point when he sees the output of the command that he typed. One would have never guessed that so much processing happens even for listing of files.

When the user hits the ‘Enter’ key after typing “/bin/ls”, the program which runs the command (the shell) forks a new process. This process invokes the execve system call for running “/bin/ls”. The complete path, “/bin/ls” is passed as a parameter to execve along with the command line argument (argv) and environment variables (envp). The system call handler sys_execve checks for existence of the file. If the file exists, then it checks whether it is in the executable file format. Guess why? If the file is in executable file format, the execution context of the current process is altered. Finally, when the system call sys_execve terminates, “/bin/ls” is executed and the user sees the directory listing. Ooh!
[ad#ad-2]

Let’s Start

Had enough of theories? Let us start with some basic features of the command shell. The listing below tries to interpret the ‘Enter’ key being pressed by the user at the command prompt.

listing2.c

This is simple. Something like the mandatory “hello world” program that a programmer writes while learning a new programming language. Whenever user hits the ‘Enter’ key, the command shell appears again. On running this code, if user hits Ctrl+D, the program terminates. This is similar to your default shell. When you hit Ctrl+D, you will log out of the system.

Let us add another feature to interpret a Ctrl+C input also. It can be done simply by registering the signal handler for SIGINT. And what should the signal handler do? Let us see the code in listing 3.

listing3.c

Run the program and hit Ctrl+C. What happens? You will see the command prompt again. Something that we see when we hit Ctrl+C in the shell that we use.

Now try this. Remove the statement fflush(stdout) and run the program. For those who cannot predict the output, the hint is fflush forces the execution of underlying write function for the standard output.

Command Execution

Let us expand the features of our shell to execute some basic commands. Primarily we will read user inputs, check if such a command exists, and execute it.

I am reading the user inputs using getchar(). Every character read is placed in a temporary array. The temporary array will be parsed later to frame the complete command, along with its command line options. Reading characters should go on until the user hits the ‘Enter’ key. This is shown in listing 4.

listing4.c

Now we have the string which consists of characters that the user has typed at our command prompt. Now we have to parse it, to separate the command and the command options. To make it more clear, let us assume that the user types the command

gcc -o hello hello.c

We will then have the command line arguments as

argv[0] = "gcc"
argv[1] = "-o"
argv[2] = "hello"
argv[3] = "hello.c"

Instead of using argv, we will create our own data structure (array of strings) to store command line arguments. The listing below defines the function fill_argv. It takes the user input string as a parameter and parses it to fill my_argv data structure. We distinguish the command and the command line options with intermediate blank spaces (‘ ‘).

listing5.c

The user input string is scanned one character at a time. Characters between the blanks are copied into my_argv data structure. I have limited the number of arguments to 10, an arbitrary decision: we can have more that 10.

Finally we will have the whole user input string in my_argv[0] to my_argv[9]. The command will be my_argv[0] and the command options (if any) will be from my_argv[1] to my_argv[k] where k<9. What next?

After parsing, we have to find out if the command exists. Calls to execve will fail if the command does not exist. Note that the command passed should be the complete path. The environment variable PATH stores the different paths where the binaries could be present. The paths (one or more) are stored in PATH and are separated by a colon. These paths has to be searched for the command.

The search can be avoided by use of execlp or execvp which I am trying to purposely avoid. execlp and execvp do this search automatically.

The listing below defines a function that checks for the existence of the command.

listing6.c

[ad#ad-2]
attach_path function in the listing 6 will be called if its parameter cmd does not have a ‘/’ character. When the command has a ‘/’, it means that the user is specifying a path for the command. So, we have:

if(index(cmd, '/') == NULL) {
	attach_path(cmd);
	.....
}

The function attach_path uses an array of strings, which is initialized with the paths defined by the environment variable PATH. This initialization is given in the listing below:

listing7.c

The above listing shows two functions. The function get_path_string takes the environment variable as a parameter and reads the value for the entry PATH. For example, we have

PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/hiran/bin

The the function uses strstr from the standard library to get the pointer to the beginning of the complete string. This is used by the function insert_path_str_to_search in listing 7 to parse different paths and store them in a variable which is used to determine existence of paths. There are other, more efficient methods for parsing, but for now I could only think of this.

After the function attach_path determines the command’s existence, it invokes execve for executing the command. Note that attach_path copies the complete path with the command. For example, if the user inputs ‘ls’, then attach_path modifies it to ‘/bin/ls’. This string is then passed while calling execve along with the command line arguments (if any) and the environment variables. The listing below shows this:

listing8.c

Here, execve is called in the child process, so that the context of the parent process is retained.

Complete Code and Incompleteness

The listing below is the complete code which I have (inefficiently) written.

listing9.c

Compile and run the code to see [MY_SHELL ]. Try to run some basic commands; it should work. This should also support compiling and running small programs. Do not get surprised if ‘cd’ does not work. This and several other commands are built-in with the shell.

[ad#ad-2]

You can make this shell the default by editing /etc/passwd or using the ‘chsh’ command. The next time you login, you will see [MY_SHELL ] instead of your previous default shell.

Conclusion

The primary idea was to make readers familiar with what Linux does when it executes a command. The code given here does not support all the features that bash, csh and ksh do. Support for ‘Tab’, ‘Page Up/Down’ as seen in bash (but not in ksh) can be implemented. Other features like support for shell programming, modifying environment variables during runtime, etc. are essential. A thorough look at the source code for bash is not an easy task because of the various complexities involved, but would help you develop a full featured command interpreter. Of course, reading the source code is not the complete answer. I am also trying to find other ways, but lack of time does not permit me. Have fun and enjoy……

[ad#ad-2]

245 thoughts on “Writing Your Own Shell”

  1. Thanks for making this this really great article. I’ve been checking all the search engines for this general kind of content and it’s been amazingly diffficult to find with any real quality. I will absolutely keep returning for more. Thank You So Much.

  2. Thanks this made for intresting reading. I adore your wordpress theme, i keep coming back here and i dont know why. I just genuinely like your web site lol… I recently read something simular to this i think they may of stolen the blog?

  3. There’s a lot of fascinating stuff on this site, I made lots of summaries on some of the really interesting bits that I hadn’t come across. There is also a whole bunch more about this on Wikipedia, and and at this geekier info site so check that out too. I’m really into it and really interested in finding out more so if any of you reading this might be generous enough to put comments on this thread that would be really appreciated.

  4. What a comment!! Very informative and also easy to understand. Looking for more such comments!! Do you have a myspace?
    I recommended it on stumbleupon. The only thing that it’s missing is a bit of new design. Nevertheless thank you for this blog.

  5. hey there, this might be little offtopic, but i am hosting my site on hostgator and they will suspend my hosting in 4days, so i would like to ask you which hosting do you use or recommend?

  6. hey, this might be little offtopic, but i am hosting my site on hostgator and they will suspend my hosting in 4days, so i would like to ask you which hosting do you use or recommend?

  7. What a post!! Very informative also easy to understand. Looking for more such comments!! Do you have a twitter or a facebook?
    I recommended it on stumbleupon. The only thing that it’s missing is a bit of color. Anyway thank you for this blog.

  8. Pretty oiroew post. Never thought that it was this straightforward after all. I have spent a great deal of my time searching for someone to jkdhsjghthis subject clearly and you’re the only person that ever did that. I really mcbxvmv it! Keep up the great posts!

  9. What a writeup!! Very informative also easy to understand. Looking for more such blogposts!! Do you have a twitter?
    I recommended it on digg. The only thing that it’s missing is a bit of new design. Anyway thank you for this blog.

  10. I would like to say, I seriously enjoy your work on this blog and the quality posts you make. These type of post tend to be what keeps me going through the day time. I detected this post right after a great companion of my very own proposed it to me. I do a little blogging and site-building personally and I am always thankful to see others giving high quality info towards community. I will certainly be following and have bookmarked your website to my facebook account for others to view.

  11. What a blog post!! Very informative also easy to understand. Looking for more such blog posts!! Do you have a facebook?
    I recommended it on digg. The only thing that it’s missing is a bit of color. Anyway thank you for this blog.

  12. What a blogpost!! Very informative also easy to understand. Looking for more such blog posts!! Do you have a myspace or a facebook?
    I recommended it on digg. The only thing that it’s missing is a bit of color. However thank you for this information.

  13. What a write!! Very informative also easy to understand. Looking for more such comments!! Do you have a twitter or a facebook?
    I recommended it on digg. The only thing that it’s missing is a bit of speed, the pictures are appearing slowly. However thank you for this information.

  14. Before I meet your pilot website, I incredible didn’t know something about this subject that I will use in my school task. After I read page by page, I found the significant thing for my initial idea, great article in your webpage, I discover many missing file and data in my book guidance, but you gave me the several options. I am postgraduate college and want to finish my subject; it is hard task (for me). I spent over 2 weeks, just browsing and finding this nice article.

  15. Hey, very nice website. I actually came across this on Bing, and I am happy I did. I will definately be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just absorbing as much info as I can at the moment. Thank You

  16. Nice discussion here. I want to join with you by giving my comments in your web page. First of all, your page and design of your website is so elegant, it is attractive. Also, in explaining your opinion and other news, frankly you are the best one. I’ve never met other person with special skills in my working environment. By using this comment page, you are great to give me opportunities for announcing some events. Secondly, I’ve checked your website in search engine result page; some of your website pages have ranked very well. Additionally, your topic is relevant with my desired one. Lastly, if you don’t mind, could you join with us as a team, in order to develop website, not only general web page, but also many specific websites we must create for our missions. From time to time, we, I and my team have great position for you to develop that.

  17. I wish you all good day, this site is really nice I would always follow this site. Help me a lot of timedata would be btained from this site, or hope to. But I want you to know that this site is really good Thanks a lot for the kind of perfect topic about this topic. It’s good to buy an essay about this good topic. Nice article, very helpful

  18. Hey there 🙂 I just wanted to comment your wonderful blog here & say that I really like your blog layout and the way you write too. You make blogging look easy lol. I currently blog too but im not nearly as good as you are. anyways nice writing, cya later

  19. I am extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it’s rare to see a nice blog like this one these days.. 🙂

  20. Took me time to go through all of the comments, but I truly enjoyed the article. It proved being incredibly helpful to me and I am positive to all of the commenters here! It is generally wonderful when you can not just be informed, but in addition entertained! I am sure you had enjoyment writing this post.

  21. The most succinct plus up to date info I stumbled upon about this topic. Sure pleased that I discovered the site by accident. I’ll be signing up for the feed so that I will receive the newest posts. Love the information here.

  22. Thanks a lot for write about rather great informations. Your web site is greatI am impressed by the specifics that you have on this blog. It exhibits how properly you understand this topic. Bookmarked this valuable web page, will appear back for much more. You, my friend, awesome! I found just the material I already searched everywhere and just wasn’t able to uncover. What a perfect site. Like this site your internet site is one of my new much-loved.I similar to this knowledge proven and it has given me some type of motivation to possess accomplishment for some reason, so maintain up the decent perform!

  23. Hello, this is a really fascinating web blog and ive loved reading several of the articles and posts contained upon the site, sustain the great work and hope to read a lot more exciting articles in the time to come.

  24. Thanks a lot you for this webpage. Thats all I may say. You most surely have created this webpage into something speciel. You clearly know what you are accomplishing, youve included so many corners.kind regards

  25. Greetings, this is a genuinely absorbing web blog and I have cherished studying many of the content and posts contained on the web site, keep up the outstanding work and desire to read a good deal more stimulating articles in the future.

  26. Hey, very nice website. I actually came across this on Bing, and I am happy I did. I will definately be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just absorbing as much info as I can at the moment. Thank You

  27. I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.

  28. This blog is awesome it’s got all the points i sought after to talk about, it has fulfilled my knowledge, i just appreciated this blog and i need to subscribe so can you please tell whilst your weblog gets up to date and what is the procedure to subscribe in details.

  29. Hi, the place did you get this info can you please assist this with some proof or you may say some good reference as I and others will actually appreciate. This data is actually good and I will say will always be useful if we try it risk free. So when you can again it up. That may actually assist us all. And this may deliver some good reputation to you.

  30. I have read a few of the articles on your website now, and I really like your style of blogging. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

  31. Hi. I just noticed that your blog looks like it has a few code errors at the very top of your site’s page. I’m not sure if everybody is getting this same bugginess when browsing your blog? I am employing a totally different browser than most people, referred to as Opera, so that is what might be causing it? I just wanted to make sure you know. Thanks for posting some great postings and I’ll try to return back with a completely different browser to check things out!

  32. You ought to seriously think about working on growing this blog into a serious voice in this market. You obviously have a good knowledge of the areas all of us are searching for on this website anyways and you could potentially even earn a dollar or three from some advertising. I would explore following recent news and raising the volume of blog posts you make and I bet you’d start earning some great traffic in the near future. Just an idea, good luck in whatever you do!

  33. Hi, found your website by good fortune in the early hours, but I’m please that I did! As well as being full of contributors and also very easy to understand unlike scores that I come across! (My site, is a review of hypnotherapy downloads). I was trying to work out what menu layout you had implemented, can somone give me an idea? The site I’ve made is a review site for hypnotherapy downloads. I’ve got a similar layout on my website, which is a site looking at hypnotherapy downloads, – yet somehow it seems to load swifter here, although this site appear to have much more content. Have you been employing any widgets that have increased speed? : Sorry to go on, but lastly to say, I appreciate such a great site and it’s obviously a excellent inspiration for my humble evaluation blog of hypnotherapy downloads. I’ll be looking to enhance my own blog attempt. I definitely will visit your brilliant site as often as I can.

  34. Hi, thank you a lot for the great article. I had wanted to sign up to your feed. However unfortunately it does not seem to be working for me. Anyone having this difficulty? I’ll visit in a bit to check. Anyways thank you again for your good read.

  35. At this point I have nice collection of Black Hat SEO tools, like proxy manager and some proprietary tools that I paid a seo software programmer to make for me. Overall I am pretty happy with my collection and my SEO clients love me!

  36. Hi admin! I have a small request. I was just searching for some info on this topic you wrote and found this blog. Some really awesome stuff you got here, can I please link to this post on my new website I am currently working on? It would be great :). I will check back again later to see how you responded. Thanks, Criss Scott .

  37. Sorry for the huge review, but I’m really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it’s the right choice for you.

  38. Hey, been recently keeping with all your writing for a long time, though never commented on a single thing before. Thought I would personally give my opinion. I think this content you submitted pretty educational. There initially were a few items which weren’t straightforward, yet all around, the article was in fact very good — not that you need me personally suggesting exactly what you currently know, hehe. Regardless, continue the good work!

  39. TRULLY interesting Blog, Bro! even though this was not what i was looking for (I am on thesearch for full-length slips as a surprise gift for my wife )… I certainly plan on visiting again!.By the way what’s is the most sexy night Gown or slip anybody can suggest ? thanks a lot and i will bookmark your article : PHP, MySQL, .NET, IIS, Networking, and Many More… » Blog Archive » Writing Your Own Shell…

  40. Wonderful blog! I truly love how it’s easy on my eyes and the info are well written. I am wondering how I may be notified whenever a new post has been made. I have subscribed to your rss feed which must do the trick! Have a nice day!

  41. Fantastic blog! I truly love how it’s easy on my eyes and also the facts are well written. I am wondering how I can be notified whenever a new post has been made. I have subscribed to your rss feed which should do the trick! Have a nice day!

  42. Hi, been recently following all your posting on the internet, yet never commented a single thing before. Reckoned I would personally post something. I found this content you wrote highly instructive. There were a few details which were not straightforward, but on the whole, the artilce was in fact goof — not that you need me telling you exactly what you realize, lol. Anyways, keep it up!

  43. Hello, been recently following all your writing on the internet, though never ever commented on a single thing before. Reckoned I would personally give my opinion. I think this article you composed extremely instructive. There were just a few ideas which weren’t very clear, yet by and large, the write-up was a good one — not that you require me letting you know just what you realize, : ). Anyhow, continue the good work!

  44. The well written summary assited me very much! Saved the blog, extremely excellent topics just about everywhere that I read here! I really like the info, thank you.

  45. Thank you for making the sincere attempt to divulge this. I feel very strong about it and would like to read more. If it’s allright, as you gain more in depth knowledge, would you mind writing more articles similar to this one with more information? It would be very helpful and useful for me and my co-workers.

  46. I differ with most folks right here; since I discovered this blog publish I could not cease until I used to be done, even though it wasn’t just what I had been looking for, was indeed an ideal learn though. I’ll instantaneously take your RSS feed to keep up knowledgeable of any updates.

  47. Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it’s nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.

  48. I agree with your post absolutely and I am now interested in reading some more of your posts on your blog and see what you have to say. Do you mind if I tweet your blog post out to my followers on twitter? I think they would also enjoy the blog post. Thanks.

  49. A Quite easy to follow article . Every time i read your blog i see a different view. In addtition , as a fresh developer, i have to say that the structure of your website is nice . Could you reply with the name of the template? . I find it hard to choose among all these themes and widgets.
    Thank you.

  50. Hello, this is a really fascinating web blog and ive loved reading several of the articles and posts contained upon the site, sustain the great work and hope to read a lot more exciting articles in the time to come.

  51. forever updated. That was exactly what I needed to read today. Good post! I am also going to write a blog post about this… thanks Thanks for the article. Information was clear and very detailed. This article is very good.

  52. Very nice and informative article. Thanks for the quality content and I hope you update your blog frequently as I`m interested in this topic. I`ve already bookmarked this article. Thanx!

  53. You ought to certainly think about working on growing this blog into a major authority in this market. You obviously have a grasp handle of the topics everyone is looking for on this site anyways and you could certainly even earn a buck or two off of some advertising. I would explore following recent topics and raising the amount of write ups you put up and I guarantee you’d begin seeing some amazing targeted traffic in the near future. Just a thought, good luck in whatever you do!

  54. could have included a link here and we could have checked it out. it’s gonna be alot of little things that cause it not any one major advertisement. Anytime you write anything anywhere or whenever you get a chance you need to open your mouth about it. Once people start looking at the content and enjoying it they will start to link to it also

  55. your works are really improving traffic then you can do some social media service, social bookmarking and press release distribution are very powerful for increase traffic to your website

  56. Hello! Ive checked on your site..why not try to put on links portion on your site..I do link building, search engine optimization for lots of online business of my client to increase traffic on the sites..you need the best marketing strategy that would save time and effort.. just email me ill be willing to help..check out my agents of value..Good Luck!

  57. Pingback: Yosho
  58. Pingback: Yoshol

Leave a Reply

Your email address will not be published. Required fields are marked *