Saturday, May 21, 2011

Code intendation

In linux, indenting the C code is very easy only if you happen to know tools for doing it. One such tool is "indent". It is a linux command that can be used to indent the C code. By default it indents the code in "gnu" style.
>indent file.c
 By using this command, we can even convert from one C style to another. For instance if you are acquainted to linux style of coding but you have a file that is in different style of coding, then use this command to convert the coding style to linux style of coding
>indent -linux file.c
Here, linux is an option to indent. There are many more options, so start exploring. 

Thursday, May 5, 2011

Temporary files - C, Shell and Perl way

All most all of the programing languages allow us to create temporary files and directories. In this post I am going to explore the creation of temporary files in C, Perl and Shell script.

C Implentation:
To create temporary files in C, we make use of the library function mkstemp () which is defined under stdlib header file. The syntax of mkstemp ()
int mkstemp (char *template);
The mkstemp file takes a character array (make sure that its not a character constant but an array, as it will be modified) and returns a file descriptor to the file. It will create a file with the default permissions.

Here, the template is a character array which has its last 6 characters marked "XXXXXX". These characters will be used to generate a unique name.

An example of template can be "fileXXXXXX";

Note: mkstemp() will create the temporary file in the current directory.  To create a file in a particular directory prefix the absolute path of the directory like to create a file under /tmp use
"/tmp/fileXXXXXX"
A sample C program
#include <stdio.h>
#include <stdlib.h>

int
main ()
{
        char filename[] = "/tmp/fileXXXXXX";
        int  fd;

        fd = mkstemp (filename);

        printf ("file=%s\n", filename);

        /* perform the operation on the file desc
         * ....
         */

        close (fd);
        unlink (filename);

        return 0;
}
The temporary file is not deleted hence it must be deleted manually.

You may also look into "mkdtemp (3)", "mkostemp (3)"

Shell implementation:
The create a temporary file in shell scripts, make use of $tempfile. It creates a temporary file in /tmp directory by default.

Sample program:
#!/bin/sh

x=$(tempfile)

## Perform operations on file

rm $x
 Perl implementation:
Perl makes use of the File module to create the temporary files. I think perl gives more liberty to us. We can tune it according to our need like one of the main things that other languages missed is the deletion of the temporary file when the process terminates and many more. Like Shell perl also creates the temporary file under /tmp directory.

Sample program:
#!/usr/bin/perl

use File::Temp 'tempfile';

($handle, $filename) = tempfile (UNLINK => 1, SUFFIX => '.dsk');

## perform the operation on the file
The tempfile function returns a handle and the filename.

Monday, April 25, 2011

Bochs(2.4.6): A cross platform IA-32 Emulator HOWTO

Hello Everyone,

Few days back I installed Bochs from source code, I needed it start my work on Pintos (I will write about this in future posts), it was fun doing it. Bochs is free open source project hosted on sourceforge, so you can download it for free and use it and also make changes to the source and send in your patches and post questions on there mailing list, I must say its one of the active mailing list.

Bochs is a portable x86 and x86-64 IBM PC compatible emulator and a debugger mostly written in C++ and distributed as free software under GNU. So what it means is that it provides an environment similar to the pc but with in the OS (hope I am not confusing), thus providing virtualization similar to another popular open source project Qemu. These emulator's by there name emulate the physical hardware for the OS to run on, they go to an extent of providing a pseudo environment where the OS thinks that it is running on the bare metal hardware but it will be one more process in the user's process list which the user can kill, restart and do what ever he wants with it. So these emulator's are commonly used by
  • OS kernel developers because they have to just start a new process when the kernel crashes rather than restarting the system.
  • Most of them use it to try out new OS distributions, mainly to run apps which are not available on there base OS. So a user using a LINUX machine can run window's within his/her machine.
 Bochs is capable of emulating
  • Harddrives
  • CD Drives
  • Floppy drives
Unlike  Qemu (which uses KVM), Bochs doesnot provide any cpu virtualization thus one can find that it is much slower to run.

In this post I will be demonstrating the installation of Bocs(2.4.6) from the sourcecode on Ubuntu (if you haven't tried it yet, go grab an Installation CD, it's worth a try). Before we do that, Bochs requires few packages to be pre-installed. So, follow to steps to install Bochs(2.4.6):
  1. Since it is written in C++, it requires a c++ compiler, so run
    sudo apt-get install build-essential
  2. The following packages are required to enable X-window's gui used by Bochs to provide a terminal when we run our OS.
    sudo apt-get install libx11-dev
    sudo apt-get install xserver-xorg-dev
    sudo apt-get install xorg-dev
  3. Ncurses  library, it provides API's, that allows the programmers to write text in a terminal (follow the wiki link http://en.wikipedia.org/wiki/Ncurses for more info).
    sudo apt-get install libncurses5-dev
  4. Flex is a free software alternative to lex
    sudo apt-get intall flex
  5. Lastly, GTK library to enable the internal debugger (use --enable-debugger option while running configure).
    apt-get install libgtk2.0-dev
    apt-get install libgtk2.0-0-dbg

Once these packages are installed, we are just few steps.
  • Download the gipped package and unzip it using
    tar xf bochs-2.4.6.tar.gz
  • cd into the un-tarred directory and run configure (follow the link to see more compiling options). I used
    ./configure --enable-gdb-stub --with-x --with-x11 --with-term --with-nogui --prefix installation-path
    to install the simple bochs and
    ./configure --enable-debugger --enable-disasm --with-x --with-x11 --with-term --with-nogui --prefix installation-path
    to install the Bochs with internal debugger enabled. Use prefix to give different names to two different binaries.

  • Now run
    make && make install
That's it. Now start using it :)

Wednesday, April 20, 2011

Scrolling up and down the Terminal

I will be posting some of the interesting and useful tips to ease the use of command line in Linux. Today's tip is HOWTO scroll through the terminal page up and down. To scroll through the terminal use

           cntrl + shift + [up arrow | down arrow] [page up | page down]

Tuesday, April 19, 2011

Passwordless SSH login HOWTO

Hello Folks,

Its been a long time since my last post, but now I would try to be more consistent. In this post I am going to explain the step's to setup "Passwordless SSH" to a remote machine.

Quite often we feel the need to login to a remote machine (maybe to backup our daily work or to run few test's..) what ever maybe the reason, we endup entering the password of that machine. Today we will learn howto setup a "Passwordless SSH" to a remote machine.

Before we begin, there are few prerequisites that are needed to successfully perform the task at hand. The remote machine should have sshd (ssh server) installed on it. If not install it using the command

     > sudo apt-get install openssh-server

The above command works only on Ubuntu, on other OS's like fedora use "yum install" etc.

Now a days most of the OS's have openssh-client installed on them by default, if not present install it. This is to be present on the machine from which you are going to ssh to the remote machine.

Once these are done, follow the step's below to successfully perform passwordless ssh login.

1. Enter the command

     >ssh-keygen -t rsa
       Generating public/private rsa key pair.
       Enter file in which to save the key (/user/.ssh/id_rsa):
       Enter passphrase (empty for no passphrase):
       Enter same passphrase again:
       Your identification has been saved in /user/.ssh/id_rsa.
       Your public key has been saved in /user/.ssh/id_rsa.pub.
       The key fingerprint is:
       e2:8e:8a:91:f2:db:06:77:22:29:27:e0:4f:7a:3b:71 user@user-laptop
       The key's randomart image is:
       +--[ RSA 2048]----+
        |                           |
        |                           |
        |                           |
        |.                          |
        |o  .  . S                |
        |oo=ooE..            |
        |++++oo.            |
        |.= =.o                 |
        |. === .               |
       +--------------------------+

For more info on ssh-keygen go through its man page.

When it asks for the file just press enter and when it asks for the passphrase enter the passphrase else just press enter.

Now it would have generated two files id_rsa and id_rsa.pub in /user/.ssh directory. The id_rsa file is the private key which you must never disclose it to anyone.

2. Now copy the public key to the remoter machine. One of my favorite ssh command variant for this is

    > ssh-copy-id -i /user/.ssh/id_rsa.pub remote-user@remote-machine

That's it, now you can login into the remote machine without entering the password.


One of the common problem that we face while using ssh to login to the remote machine may be

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
af:b6:ab:a9:c5:5e:ab:bb:b8:1c:1c:d6:82:0c:ce:9b.
Please contact your system administrator.
Add correct host key in /user/.ssh/known_hosts to get rid of this message.
Offending key in /user/.ssh/known_hosts:34
RSA host key for remote-machine-ip has changed and you have requested strict checking.
Host key verification failed.


If you face this problem, then just delete the file /user/.ssh/known_hosts and you are good to go.

Sunday, March 20, 2011

Toggling through the directory hierarchy

I recently came across pushd and popd commands and found them to be of great help. I use command line to do a most of my work, while I am working there are lot of time's when I move deep into the directory hierarchy and then return to the directory I was previously working in. This is sometime's  tiring and time consuming. If you are one of them and don't know about the pushd and popd command's then you are at the right place. Let me explain it with an example -

       $ pwd
       /home/linux_code

Now you wish to look at the code which might be under fs/minix and then later want to come back to the current directory then first push the current directory to the stack (that is what I think pushd stands for -push directory). It takes an argument (the directory to be saved/pushed).

      $pushd .
      $cd fs/minix

Once you are done with the work just use the popd command to return to the previously saved direcotory. 

      $popd
      $pwd
      /home/linux_code

Also note that, if you start pushing more than one directory on to the stack, then it will be popped in the reverse order (since stack follows Last In First Out (LIFO) order). So start using.

Friday, February 18, 2011

Hello World

Hello Folks,

This Junaid. I am working as a software engineer. I am new to this blogging world and  have been thinking of writing a blog for quite some time now. The questions that I had in my mind was when there are hundreds of blogs on the net that speak on almost every topic then what is the need of one more blog. A simple answer to it is that it will help me document my work and if it helps any one else then its an added point.

So here I am with my first post , being a software engineer's, many of you must have observed that "HELLO WORLD" is the very first program that every programming language book teaches us to write. Thus, I thought why not start my blog with the title "hello world".

I am an admirer of open-source philosophy and a linux user by choice. In this blog I 'll be speaking about linux, emerging technologies, open source tools, file system and many more things. So, folks do follow up regularly and do write your valuable comments.