Tag Archive: Operating system



In college days, I heard about the init process. It is the root of process tree and first process started by the kernel and so on. I wanted to see what happens if we kill it. So I tried a lot but no success. Let’s kill the init process and observe what is happening

Note: Fedora switched systemd a while back, if you running Fedora you will find process “systemd” is with pid 1 instead of init.

Image

Now, Years later I was reading Linux Kernel Development I was going through kernel sources mentioned in the book and all of a sudden I found the answer.

Note: File Paths mentioned here are relative to kernel source tree. I am using linux kernel 2.6.34.

In kernel/fork.c, in function copy_process(…) I found this

/*
* Siblings of global init remain as zombies on exit since they are
* not reaped by their parent (swapper). To solve this and to avoid
* multi-rooted process trees, prevent global and container-inits
* from creating siblings.
*/
if ((clone_flags & CLONE_PARENT) && current->signal->flags & SIGNAL_UNKILLABLE)
return ERR_PTR(-EINVAL);

Mark the above code as Listing 1. We know that operating system keep information about a process in process control block (PCB). In Linux, PCB is implemented as structure called task_struct declared in include/linux/sched.h. task_struct store all the important information about a process like it’s pid, memory mapping, signal mask etc…

Now, task_struct contains pointer to structure signal_struct named signal. Inside signal_struct field flags (unsigned int) refers to signal flags for the given process

struct task_struct {
…................
signal_struct *signal
…................
}

Form include/linux/sched.h
struct signal_struct {
…....................
unsigned int flags;      /* see SIGNAL_* flags below*/
…....................
}</pre>
#define SIGNAL_UNKILLABLE       0x00000040 /* for init: ignore fatal signals */

The above code (marked listing 1) prevents init from creating any siblings. In the above code current refers to the pointer  to task_struct for current process. init is the only process for which current->signal->flags is set to SIGNAL_UNKILLABLE. So, if init tries to create siblings using clone syscall, -EINVAL is returned.

Let’s see what happens when we remove the SIGNAL_UNKILLABLE flag from init. To do this I wrote a loadable kernel module. To learn how to write a loadable kernel module refer to

1. The Linux Kernel Module Programming Guide by Peter Salzman.

He also has a great tutorial on gdb you can find it on this link.

2. Book: Linux Device Drivers from O’REILLY.

Excerpt from the code.

/* Description: Remove UNKILLABLE flag from init */
…............
…............
static int hello_init(void)
{
struct task_struct *task;
    struct task_struct *init_ptr = NULL;
for (task = current; task != &init_task; task = task->parent) {
       init_ptr = task;
}
/* init_ptr now points to init, and task now points to init's parent i.e. swapper with pid 0 */
printk(KERN_ALERT "I am %s with pid %i\n", task->comm, task->pid);
printk(KERN_ALERT "I am %s with pid %i\n", init_ptr->comm, init_ptr->pid);
printk(KERN_ALERT "0x%x\n", init_ptr->signal->flags);

/* remove the UNKILLALBE flag */
init_ptr->signal->flags = init_ptr->signal->flags & ~SIGNAL_UNKILLABLE;
printk(KERN_ALERT "0x%x\n", init_ptr->signal->flags);
return 0;
}
…................
…................

When the above code is compiled, module hello.ko is generated. When we issue

“insmod hello.ko” command module initialization function hell_init is called.

When we insert the module using insmod command, current points to task_struct of insmod process, so in the for loop we keep on we keep on moving up the process tree until we encounter init_task as show in the picture below.

for (task = current; task != &init_task; task = task->parent) {
init_ptr = task;
}

Screen Shot 2013-08-16 at 8.20.04 PM

So, with first iteration we move from insmod to bash and so on until we reach init_task. Despite it’s name init_task is parent of init process and has pid of 0. It’s statically allocated and is not visible in user space.

Screen Shot 2013-08-16 at 9.14.20 PM

After that I am able to kill init using command “kill -9 1”. The moment I do kernel panics. I get core dump using kdump. Below is the dmesg output.

DMESG Output

NOTE: Since wordpress.com doesn’t allow you to create iFrames and manual code, We faced lot of issues while formatting this post. There are still issues with the alignment. We regret the inconvenience caused to you. Soon we will be moving to a personal domain and work for the same has already been started.

References

1. Manpages: man 7 signal, man ps

2. Linux Kernel Development By Robert Love

3. Linux Kernel Sources 2.6.34

4. Tools you may find useful: ctags, cscope, kdump.

Pages : Follow flossstuff on Twitter      


We all need to look for word definition. Online dictionaries and wikipedia are great but we are not always connected to the net. sdcv is a console version of StarDict dictionary. Using sdcv you can search for definitions while still offline. So here are the step to install and make use of this great program.

Step 1. Installation

On Ubuntu

Open Up terminal and type:

$ sudo apt-get install sdcv

On Fedora

$ su -c “yum install sdcv”

On Mac OS X

Mac OS comes with a great GUI based dictionary, but cli enthusiasts will enjoy sdcv.

Please install Macports and then open Finder, Go to Application Folder > Utilities Folder and launch terminal and type

$sudo port install sdcv

Step 2. Download Dictionary Files

Now that we have installed sdcv, we need to download dictionary files from the net.

Go to this site1 , site 2  and download dictionary files according to you requirements. For open source fans, Free On-Line Dictionary of Computing (FOLDOC) and Jargon file are must. They are both available from site1.

FOLDOC: Dictionary for computing subjects.

Jargon File: “A comprehensive compendium of hacker slang illuminating many aspects of hackish tradition, folklore, and humor.” – From Jargon File Homepage.

GNU/Linux English-English Dictionary: You can download it form here.

Step 3. Install downloaded Dictionary Files

Now that you have downloaded the dictionary files we need to put them in the folder where sdcv looks for dictionary files when invoked.

On Linux based systems do the following

$sudo mkdir /usr/share/stardict/dic/

$sudo tar -xvjf downloaded.tar.bz2 -C /usr/share/stardict/dic or

$sudo tar -xvzf downlaoded.tar.gz -C /usr/share/stardict/dic

Note: Make sure the user who will be invoking the sdcv command has read and execute permissions on /usr/share/stardict/dic/ and its subfolders.

On Mac OS X

$mkdir -p $HOME/.stardict/dic

$sudo tar -xvjf downloaded.tar.bz2 -C $HOME/.stardict/dic

Note: Unfortunately you need to repeat this process for every user that needs access to these dictionary files.

Step 4. Enjoy sdcv

Searching for definition

$ sdcv Linux                         (looks for linux in the dictionary.)

$ sdcv                                  (invoes sdcv in the interactive mode, press Control-D to quit).

In earlier article we covered how to use espeak, now you can combine sdcv and espeak together. Sdcv to look for definition and espeak for pronunciation.

$ sdcv Linux && espeak Linux

Here is a picture.

"sdcv"

Note: Sometimes due to the long definition of a particular word text scrolls down too fast for you to read, advanced users can set SDCV_PAGER environment variable to /usr/bin/less. Now, less will be used to display to the dictionary’s article. You may consider adding this to your .bashrc file. Check out manpage for sdcv for more information.

Pages : Follow flossstuff on Twitter      


Sometimes we want out Linux box to speak to/for us.This ability is very useful to make announcements and alerts in case of many critical events in our machines or can be used just for sheer fun. There exists a command line synthesized known as espeak that comes pre installed in Ubuntu. You can also install it on debian systems by typing the command sudo apt-get install espeak  in a command console .For all other operating systems you can install it from source available at http://espeak.sourceforge.net/download.html The usage of espeak is pretty straight forward, just  type  espeak   “text to announce”  in a text console and listen your computer speaking the text.Some basic options to control espeak  are :

a <integer>     sets the amplitude/loudness of the speech
f <filename>  speaks the content of the given file
p <integer>     sets the pitch/frequency of the speech

s <integer>     sets the speed of the speech in words/minute 

Espeak is a cross platform utility that runs on more than half a dozen of OS platforms. So go through its man page by typing man espeak in a text console , explore it more and make your machine speak.

Pages : Follow ankurtwi on Twitter      


This trick (by Sumit Rai) was published in Linux For You (LFY) under the Tips And Tricks Section. Sharing it with all the Linux Geeks out there :)

When you format a partition in Linux , 5% of the total space gets reserved for privileged  processes, by default. This is done so that system processes continue to function correctly ,if the filesystem gets full. This is useful for your ‘root’ partition.However if you have let us say 50GB separate home partition, you may want to make use of few additional GBs by reducing the percentage of the reserved space. You can use tune2fs command to change  the default allocation of space  reserved for privileged  processes. Command for the same is :
#tune2fs -m 1  /dev/sda6
Here  we have changed space reserved for privileged  processes on /dev/sda6 to 1 percent. You can use your device file according to the target partition instead of  ‘/dev/sda6′ . You can see the effected change by mounting the filesytem and checking available space using df command before and after you execute the above command. This command is quite useful in so called ‘Emergency’, so do remember it :)

Pages : Follow ankurtwi on Twitter      


According to Ubuntu developers, the Ubuntu 12.04 (Long Term Support) has a new release schedule. There will be two Alpha versions, two Beta versions, then there will be a release of the Release Candidate version and finally the complete version will be released. It’s code name is “Precise Pangolin”. The schedule of the release of Ubuntu 12.04 with exact date is as follows.

Alpha 1 release date: December 1st 2011
Alpha 2 release date: February 2nd 2012
Beta 1 release date: March 1st 2012
Beta 2 release date: March 22nd 2012
Release Candidate release date: April 19th 2012
Ubuntu 12.04 Final release date: April 26th 2012

The Ubuntu 12.04 being a LTS release will be expected by many to be a strong OS with remarkable performance and support for features.

Pages : Follow ankurtwi on Twitter      


In this previous part of this article we learned about  types of shells . In most of the Linux distributions , bash shell is the default shell. So we are going to discuss the working of bash shell in this article . Fasten your seat belts and get ready for the bash ride :D

Normally we think that when we type a command, shell looks for the command in all the directories defined in the PATH environment variable. But in real it goes through the following sequence to reach the above step.

  1. Redirection
  2. Aliases
  3. Expansion
  4. Shell Function
  5. Shell Builtin
  6. Hash table
  7. PATH variable

Now lets first discuss them one by one:

1. Redirection: Consider the example given in the following snapshot:     

Redirection

Here files file1, file2, file3 exists in the current directory as seen from the output of ls command but when we redirect the output to out.txt file, the out.txt file should contain the output of ls command i.e. file1 file2 file3 but in real it also includes out.txt. This happens because before ls is executed redirection is done and file out.txt is created in current directory (we can’t redirect output to a file that doesn’t exit).

2. Aliases: After redirection shell moves to aliases.

Aliases

Here I have defined alias ls=cat , now when I type ls out.txt , ls is replaced with cat and contents of out.txt file is displayes. To make the shell ignore alias use , precede the command name with backslash (“\”).

3. Expansion: According to bash manpage there are seven kind of expansions : brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion. Discussing all of them is beyond the scope of this article, refer to bash manpage (man bash) for more information. If the command you typed contain any variable name, wildcards (*,? etc) shell expands them. As an example refer to the screenshot below :

Expansion

Here $HOME becomes /home/sumit (my home directory) after expansion and file* is expanded to file1, file2, and file3.

4. Shell Function: Like many programming languages bash allows you to define functions. A function in bash may contain multiple commands. You can execute a function just by typing its name. Example : Look  in the figure below, here I have defined an alias by name “ls” and a function by the same name. Since alias gets priority over function, when I first type ls and press enter /bin/ls is executed. Next I have used backspace to ignore alias. After deleting alias using unalias command, since there is no alias left ls corresponds to function ls. 

Shell Function

5. Shell Builtin : Some commands are a part of bash code itself, they are called Shell builtin commands. They are given priority over hash table and directories in PATH variable. Example : echo is a shell builtin command, you can use type command to determine how a given keyboard will be interpreted. 

Shell Builtin

Here its clear that echo is shell builtin command, echo also exist in /bin/ folder but if you type echo and hit enter , it will be ignored since builtin command are  of more  priority. Notice how the output of “type ls” changes after defining the alias by the same name. Once we delete alias and function ls, bash searched hash table, since ls is not in hash table is look in PATH and its found in /bin/ls. 

6. Hash table: The concept of hash table is similar to cashing in Linux, shell store the full path of all the executed commands to speed things up. Now in the snapshot below I have started a new shell, after that I execute ls command (since there is no alias or function by the same name, it executes /bin/ls because /bin is in PATH). Then after executing echo and firefox, I have used hash command to look at hash table. The first column “hits” display cache hits and “command” column displays full path of the command. Because  echo is internal command its not displayed in hash table. After I execute ls one more time its hit column changes from 1 to 2.

Hash Table

7. PATH variable: Last of all shell searches for a given command in directories listed in PATH environment variable. You can look at the contents of PATH by typing

$echo $PATH

To add any directory to PATH variable use

$export PATH=$PATH:/path/to/dir

where /path/to/dir is the absolute path to the directory you want to add. For more information you can read bash manage ($man bash).     Also to make the changed PATH settings permanent add the line export PATH=$PATH:/path/to/dir to .bashrc file in your home directory.

$echo “PATH=$PATH:/path/to/dir” >> /home/your_user_name/.bashrc

Note: Make sure you use >> instead of >, otherwise .bashrc file will get overwritten instead of appending.

About Author : This post is written by Sumit Rai. He is second in command of this blog. Sumit loves to play with grub and spends most of his time in manipulating the hardware through shell.  He is trying his hands on assembly languages also. You can contact him at sumitrai96@gmail.com .

If you want to distribute your knowledge of open-source or Linux in any form like we are doing , feel free to contact us . We can do this through this ad-free blog. We think this is the only way we can repay a little to the work of GOD RICHARD STALLMAN and LINUS TORVALDS .

Pages : Follow ankurtwi on Twitter      

Types Of Shells


Shells plays an important role in Linux and Unix system. Everything that is happening on your distro is either because of the shell or the kernel. Moreover, a backend  shell is initialized as soon as you made a login . There are lots of shells in the Unix and Linux system. Let’s try to know more about them :

What is a shell ?

Shell is the software installed on your system that works as command-line interpreter i.e. it takes the commands you type, interpret them and gives them to operating system (kernel) through system calls to execute. There are many different shells available to choose from in Linux.

C Shell : C shell was created by Bill Joy, while he was studying at University of California, Berkeley. He released it under BSD license, the syntax of C shell is similar to C programming language hence the name C shell.

TC Shell: An improved version of C shell, provides some additional features like command line completion and editing. In most of Linux systems /bin/csh is linked to /bin/tcsh and it alternatives, so when you try to start the C shell, TC shell gets started.

C shell linked to its alternatives

Korn Shell (ksh): Korn shell was developed by David Korn at AT&T Bell Laboratories. It contains may features of C shell and Bourne shell.

Bourne Shell(sh): The default shell in many Unix systems developed by Stephen Bourne of AT&T Bell Laboratories.

GNU Bourne-Again Shell (bash): Bash shell is default shell in most of Linux distributions. Its a free and open source clone of bourne shell. It was developed by Brian Fox for the GNU project. Its the most feature rich shell available and is compatible with bourne shell.

To find out all the shells installed on your system type “$cat /etc/shells”. Your default shell is defined /etc/passwd file.

My Default Shell

To switch to a different shell on your system just type the name of that shell (as in /etc/shells) and hit enter.

Z Shell(zsh):  Sorry I forgot to write about this shell (Don’t Know How :( ). Thanks to all of you for reminding me. Well The Z shell (zsh) is a Unix shell that can be used as an interactive login shell and as a powerful command interpreter for shell scripting. Zsh can be thought of as an extended Bourne shell with a large number of improvements, including some features of bash, ksh, and tcsh. It includes features like automated spell correction, editing  multiple line command within a single buffer and many more.

In the next part of the article we will learn about “How bash shell interprets a command”. So Google about the shells more and more. If want to become a Linux administrator, then you must  have  a good control over the shells.

About Author : This post is written by Sumit Rai. He is second in command of this blog. Sumit loves to play with grub and spends most of his time in manipulating the hardware through shell.  He is trying his hands on assembly languages also. You can contact him at sumitrai96@gmail.com .

Pages : Follow ankurtwi on Twitter      


Ubuntu 10.04 was released last year but it’s still popular among most of the Linux users because of its eye catchy look. Moreover its a LTS release. First thing I noticed when I installed the distro was the missing network applet (bug) . Because of that I was unable to connect to the wireless network. I posted the same in the ubuntu forums community and figured a way out. I posted the solution over there  and it got great response in return too. It got 25,000+ views over there in the forums. Link for the same is http://ubuntuforums.org/showthread.php?t=1469625 . So I thought of sharing it over my blog too . Just follow the steps below and missing network applet will appear on your screen after reboot :

1. In the terminal type  “sudo gedit /etc/NetworkManager/nm-system-settings.conf” (without quotes)
2.Change the “managed=false” to “managed=true” and then save it.
3. Then in the terminal type “sudo killall nm-system-settings”
4. Reboot your system
This solution worked for me :)

Pages : Follow ankurtwi on Twitter      


Linux is generally considered as a geek’s operating system and I am completely agree with it. Linux is not only limited to work only. You can also enjoy a lot while working on it. There are many crazy commands which will bring a big smile on your face. Below we are going to discuss few out of them.

1. Oneko : This utility remembers me of the cat mouse game. Cat chases the mouse pointer regardless of its position. You have to download it from the repository by typing “sudo apt-get install oneko” (without quotes)  in ubuntu .Then just launch it by typing oneko . You will see a cat following your mouse pointer. You can terminate this command by using the famous Ctrl-c.

2. Xpenguins:  We all know about the  relationship  of the Linux with the penguin “Tux”.  By using this utility you will see penguins dancing around your desktop. This command is partially GUI based but can be invoked using the CLI. You can install it by using “sudo apt-get install xpenguins” and can invoke it simply typing xpenguins on your terminal. To terminate follow the same Ctrl-c rule.

3.  Fortune :  You can generate famous quotes using this. It uses the random function during the generation. You can find it in the “fortune-mod” package.  For the installation of the package you need to type “sudo apt-get install fortune-mod” . Launch it by typing  fortune . You can add it  in your bashrc file so that everytime you log in you see a message .

4. Cowsay and Xcowsay : Want to say something to anyone using a cow as a medium?? If yes this utility fits the best. CLI version is cowsay and GUI version is xcowsay . You can find them in the cowsay and xcowsay package respectively. To invoke follow the syntax : cowsay text (CLI) and xcowsay text(GUI) . Example – Let’s say “hello” through the cow.

cowsay hello

xcowsay hello

5. sl (oops ls) : As we all know that the “ls” utility (used for listing of files) is used at a very high rate in  Linux. But sometimes due to high speed typing we typed it “sl” and “ls”. If you had “sl” utility installed you will see a train engine passing by in your command line warning you that you have misspelled the ls. Install sl by using “sudo apt-get install sl”  and invoke it using sl.

There are lots of others crazy commands too. Google them and have fun!!!

Pages : Follow ankurtwi on Twitter      


I was running through various articles on the web and found this great interesting concept. There is an inbuilt Linux emulator within the browser. It means that you can learn the Unix/Linux from the browser without installing  the actual operating system. All you need is a good internet connection. This is a great recommendation for the newbie. They can check out their compatibility with the Linux using  this PC emulator and if find’s Linux  interesting, then they  can install the real operating system on their  systems later. This can also be used for the testing purposes. Technology behind this is JavaScript. Don’t know how the creator (Fabrice Bellard)  did this  but hats off to him .

Link to the PC emulator - http://bellard.org/jslinux/

Pages : Follow ankurtwi on Twitter      

Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: