Alternalte for /proc/devices/bus/usb in ubuntu

Many Devices programmers who use Ubuntu face a problem while writing USB device drivers ,that is the proc window /proc/devices/bus/usb/devices which is part of usbfs doesnt exist in ubuntu.But there exists a alternate window file to know the list of detected USB devices .The file /sys/kernel/debug/usb/devices gives the same information as one would get from the /proc window .

 

 

XHTML and CSS-3:Table and List styling using basic CSS

<!DOCTYPE html>
<html>
<head>
	<title>CSS TABLES</title>
	<style type="text/css">
	table {border:4px solid blue;}       <!-- CSS and XHTML for styling tables> 
	tr    {background-color:red}
	td    {border:2px dashed yellow} 

	</style>
</head>

<body>
<table>
	<tr>   
		<td>CONTENT1</td>
		<td>CONTENT2</td>
		<td>CONTENT3</td>
	</tr>
	<tr>   
		<td>CONTENT4</td>
		<td>CONTENT5</td>
		<td>CONTENT6</td>
	</tr>
</table>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
	<title>CSS TABLES</title>
	<style type="text/css">
	ul {list-style-type:circle;
	    border:1px dotted blue;}			
	ol {list-style-image:url(linux.png);}    
	<!-- This sets the bullets of ordered list to the image linux.jpg -->
	</style>
</head>

<body>
 	<ul>
		<li>Linux Device Drivers</li>
		<li>JAVA SCRIPT</li>
		<li>LINUX SYSTEM PROGRAMMING</li>
	</ul>
	<ol>
		<li>Linux Device Drivers</li>
		<li>JAVA SCRIPT</li>
		<li>LINUX SYSTEM PROGRAMMING</li>
	</ol>
</body>
</html>

XHTML and CSS-2:BASIC HTML AND CSS

<!doctype html>
<html>
<head>
<title>LISTS</title>
<style type="text/css">
	
        
        h2 {text-align:center;}           <!-- TO align h2 header to centre of the page -->
	p  {text-indent:30px;}            <!-- To indent starting of each paragraph by 30 pixels -->
	ul {color:red;font-style:italic;} <!-- CSS snippet to make all elements in unordered list as red and italic--> 
	ol {color:blue;font-weight:bold;} <!-- To change the ordered list section to blue -->
</style>

</head>
<body>
<h2>MISS YOU</h2>
<p>Here is the unordered Lists</p>
<ul>                                      <!--<ul> is the tag for the unordered list --> 
	<li>karthic</li>
	<li>Suresh</li>
	<li>sun</li>
	<li>RAO</li>
</ul>
<p>Here is the ordered list</p>           <!--<ol> is the tag for the ordered list -->  
<ol>
	<li>Karthic Rao</li>
	<li>Suna</li>
	<li>suresh</li>
</ol>
<p>HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH,</p>	
	
</body>

</html>

Linux Device Drivers-2:My first driver

The headers used to write a Device driver code or a kernel program are not found in /usr/include , they are found in /usr/src/linux-headers-$(uname -r)

Here is a small Device driver code which registers itself .When A device driver is registered using alloc_chrdev_region() , its entry is made in the file /proc/devices with the name mentioned in the same function call .The messages printed using printk() function will not appear on standard output , instead the message can be viewed in the last few lines of dmesg command.

#include<linux/types.h>
#include<linux/kdev_t.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
dev_t devno;
/*first 12 Bits of this 32-bit data type is used to store the MAJOR number of the device and i
  remaining 20 bits for the minor number*/
static int __init constructor(void)/*initialization function,this runs when the module is loaded on top of kernel*/
{
 
 int ret;

 if((ret=alloc_chrdev_region(&devno,0,3,”RAO’s DRIVER”))<0) /*Used to dynamically allocate the major number for the device ,defined in <linux/fs.h>,The device is       
                                                             registered and A device by name “RAO’s DRIVER ” will be seen inside the file /proc/devices*/        
  return ret;
 printk(KERN_INFO “\nMajor NO=%d,MINOR NUMBER=%d\n”,MAJOR(devno),MINOR(devno));/*Kernel’s equivalent of printf,the output can be viewed by using dmesg command*/
 return 0;
}

static void __exit destructor(void)/*Kernel automatically loads this function when the module removed from top of kernel*/
{

 unregister_chrdev_region(devno,3); /*To unregister the registered device */
 printk(KERN_INFO “\nRAO’s module removed”);
}

module_init(constructor);/*Tells the kernel the name of the function to be called when the module is loaded*/
module_exit(destructor);/*Tells the kernel the name of the function to be called when the module is removed*/

MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“karthic RAO”);
/*More information about hte code is given to the kernel using MODULE_ macros*/
MODULE_DESCRIPTION(“FIRST CHAR DRIVER:D”);

[/sourcecode]

The source code of the Makefile

# If called directly from the command line, invoke the kernel build system.
ifeq ($(KERNELRELEASE),)

    KERNEL_SOURCE := /usr/src/linux-headers-$(shell uname -r)
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNEL_SOURCE) SUBDIRS=$(PWD) modules

clean:
    $(MAKE) -C $(KERNEL_SOURCE) SUBDIRS=$(PWD) clean

# Otherwise KERNELRELEASE is defined; we’ve been invoked from the
# kernel build system and can use its language.
else

    obj-m := test.o

endif

[/sourcecode]

Steps to compile and run the code:Name the source code as test.c and place the Makefile in the same directory.The steps are depicted in the image below

ImageImage

Everything to programatically understand about Zombie processes and Avoiding them under GNU/LINUX

A Zombie is a process which has died but its entry still remains in the process table.This happens in unix and unix-like Operating systems when parent doesnot execute wait() system call for its child processes .

USEFULNESS OF ZOMBIES

*Parent process can fetch the information of the child process from the process table entry even after the child has died by expecting wait() or waitpid() system call .

*The pid of the zombie process will not be allocated to any new process

Disadvantages of Zombie processes

*The presence of a few zombies is not worrisome in itself, but may indicate a problem that would grow serious under heavier loads. Since there is no memory allocated to zombie processes except for the process table entry itself, the primary concern with many zombies is not running out of memory, but rather running out of process ID numbers.

SIGCHLD signal

When a child process exists , it sends a SIGCHLD signal to the parent , by default this signal is not taken into account unless a signal handler is setup

Ways of avoiding creation of Zombie processes

*Execute wait() system call in the sequential code of the parent

  Image

#include<stdio.h>

#include<unistd.h>

#include<time.h>

int main()

{

pid_t pid;

int status;

pid=fork();

if(pid==0) /*child proces exists*/

{

exit(0);

}

printf(“\nThe process Id of the Zombie child process is %d\n\n\n”,pid);

system(“ps -aeo pid,stat | grep Z”);

/*obtain the process list with the status information and grep for Z which indicates that the proces is a Zombie */

printf(“\n\n\nZ+ Indicates that the process with PID=%d is a Zombie running in foreground\n\n”,pid);

sleep(5);

pid=wait(&status);/*wait returns the pid of the child that was terminated */

printf(“\n\nThe Zombie status of the child process pid=%d is removed\n\n”,pid);

system(“ps -aeo pid,stat | grep Z”);/*this call doesnt result in any output since there Zombie status of the child process no more exists, since wait system call is executed in the parent*/

sleep(5);

exit(0);

}

*Execute waitpid() system call in the sequential code of the parent

 Image

#include <sys/wait.h>

#include <stdlib.h>

#include <unistd.h>

 

int main(void)

{

pid_t pids[10];

int i;

char message[]=”\n\nTen Zombie child processes created the shell command inside call to system illustrates this\n\n”;

 

for (i = 9; i >= 0; –i) {

pids[i] = fork();

if (pids[i] == 0) {

 

_exit(0);

}

}

/*for each iteration of the for loop fork() is called and the child process is made to exit without waiting for them in the parent , this results in creation of 10 Zombies , which is clearly illustrated the ps command inside the call to system()*/

write(1,message,sizeof(message));

system(“ps -aeo pid,stat | grep Z”);

for (i = 9; i >= 0; –i)

 waitpid(pids[i], NULL, 0);/*Equivalent of executing wait() call ten times */

return 0;

}

*setup a signal handler using signal() or sigaction() system call use wait() system call inside it

Image 

#include<signal.h>

#include<stdio.h>

#include<unistd.h>

#include<time.h>

void Child_handler(int status)

{

pid_t pid;

printf(“\n\nSIGCHLD generated and caught “);

pid=wait(&status);

printf(“\n\nProcess Id of the Zombie Child = %d\n\n”,pid);

return ;

}

int main()

{

pid_t pid;

int status;

pid=fork();

signal(SIGCHLD,Child_handler);

/*Child_handler is the function where control moves onto when SIGCHLD is generated , that is when the child dies*/

if(pid==0) /*child proces exists*/

{

exit(0);

}

sleep(2);

exit(0);

}

*In modern implementations by explicitly ignoring SIGCHLD by setting its handler to SIG_IGN

#include<signal.h>

#include<stdio.h>

#include<unistd.h>

#include<time.h>

int main()

{

pid_t pid;

int status;

pid=fork();

signal(SIGCHLD,SIG_IGN);

/*In modern implementations of the Linux systems , by explicitly setting the signal handler for the SIGCHLD signal to SIG_IGN zombies can be avoided*/

if(pid==0) /*child proces exists*/

{

exit(0);

}

system(“ps -aeo pid,stat | grep Z”);/*This execution of shell command doesnt give any output since There are no Zombie processes*/

printf(“\n\nNo Zombies created \n\n”);

sleep(2);

 exit(0);

}

*When the parent process dies init becomes parent of all the Child processes including the Zombie child processes

Implementation of Circular Queue

//The code will not run under most of the Compilers in Windows OS
//Many of the declration techniques used the code doesnot work in old complilers like TURBO C
//The code is tested in Code blocks 10.05 and GCC under LINUX operating system
//download link for code blocks compliler http://www.codeblocks.org/downloads
//It would be very easy to download and use code blocks if you are using Ubuntu linux distribution
//Or else GCC can be used which comes along with any GNU/LINUX distributions
#define n 5//Set the size of queue here
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
typedef struct
{
    int key;

}element;
int rear=-1;
int front=-1;

element queue[n];
void queuefull(bool* fulltest)//This function is used to set the flag that the queue is full
{
    *fulltest=false;
}
void queueempty(bool* emptytest)//This function is used to set the flag that the queue is empty
{
    *emptytest=false;
}
void addq(int item,bool *fulltest,bool *emptytest)//This function is used to add elements to the Queue
{
    *emptytest=true;
    rear=(rear+1)%(n);
    if(rear==front)
     queuefull(fulltest);
    else
    {
          queue[rear].key=item;
          if(rear==n-1&&front==-1)//if this line is not present than when no deletions are performed that is when front =-1, the loop runs again and again and overwrites the queue forth and back
            queuefull(fulltest);
          if((rear+1)%(n)==front)
            queuefull(fulltest);
    }
}
int deleteq(bool *emptytest,bool *fulltest)//This function is used to delete the queue elements
{
    *fulltest=true;

    if(front==rear)
     queueempty(emptytest);
    else
 {
     front=(front+1)%(n);
     if(front==rear)
     queueempty(emptytest);

     return queue[front].key;

 }

}

void testfunction()//*This function is used to just print the values of front and rear
{
    printf("**testing the vales front=%d,value=%d\trear=%d,value=%d*********",front,queue[front].key,rear,queue[rear].key);
        printf("\n");

}
void showqueue(bool *emptytest,bool *fulltest)//This function is used to print the queue
{
    int i;

    if(!*emptytest)
    {
        printf("\n*****Queue is empty******\n");
        return ;
    }
    if((front==-1)||(front==-1&&!*fulltest))
    {
        if(front==-1&&!fulltest)
         printf("\nQueue is full");
        printf("\n\tHmmmm...You havent deleted any element yet and total no of elements=%d",rear+1);
        printf("\nThe elements are : ");
        for(i=0;i<=rear;i++)
        {
            printf("queue[%d]=%d",i,queue[i].key);
            printf(" ");
        }
         printf("\n");
         return ;
    }
    if(!*fulltest)
    {
        printf("\nqueue is full and it contains (n-1) elements: ");
        if(rear>front)
        {
            for(i=front+1;i<=rear;i++)
            {
                printf("queue[%d]=%d \t",i,queue[i].key);
            }
            printf("\n");
        }

        if(rear<front)
        {
            printf("\nstack is full and it contains (n-1) elements: ");
            for(i=front+1;i<n;i++)
             printf("queue[%d]=%d\t",i,queue[i].key);
            for(i=0;i<front;i++)
             printf("queue[%d]=%d\t",i,queue[i].key);
            printf("\n");
        }
        return ;
    }
        if(1)
        {
            if(rear>front)
            {
                printf("\nThe total no.of.elements in the queue is: %d\n",rear-front);
                printf("\nHmmmm...There is still some space left in queue..its elements are : ");
                for(i=front+1;i<=rear;i++)
                {
                    printf("queue[%d]=%d",i,queue[i].key);
                    printf(" ");
                }
                printf("\n");
            }
            if(rear<front)
            {
                printf("\nThe total no.of.elements in the queue is : %d\n ",((n-1)-front)+(front-rear));
                for(i=front+1;i<n;i++)
                   printf("queue[%d]=%d\t",i,queue[i].key);
                for(i=0;i<front;i++)
                   printf("queue[%d]=%d\t",i,queue[i].key);
            }
            printf("\n");
       }

    }

int main()
{

    int choice,item;
    bool fulltest=true;//the values true =1 and false =0 are defined in stdbool.h
    bool emptytest=false;

    for(;;)
    {
        testfunction();
        if(fulltest&&emptytest)
        {
            printf("\n*****************Enter the operation you want to perform on the QUEUE************\n");
            printf("1.ADD\n2.DELETE\n3.SHOW \n4.EXIT\n:  ");

             scanf("%d",&choice);
             switch(choice)
             {
                  case 1: printf("\nEnter the item to be added to the queue: ");
                        scanf("%d",&item);
                        addq(item,&fulltest,&emptytest);
                        break;
                  case 2:deleteq(&emptytest,&fulltest);
                        break;
                  case 3:showqueue(&emptytest,&fulltest);
                        break;
                  default:exit(0);

             }
        }

        if(!fulltest)
        {
             testfunction();
             printf("\n***QUEUE is full , you can perform only these following operations****");
             printf("\n1.DELETE \n2.SHOW\n 3.EXIT\n: ");
             scanf("%d",&choice);
             switch(choice)
             {
                 case 1:deleteq(&emptytest,&fulltest);
                  break;
                 case 2:showqueue(&emptytest,&fulltest);
                  break;
                 default:exit(0);

             }
        }
        if(!emptytest)
        {
            testfunction();
            printf("\n******Queue is empty, only following operations are possible*********\n ");
            printf("1.ADD\n2.SHOW\n3.EXIT\n:");
            scanf("%d",&choice);
            switch(choice)
            {
                case 1: printf("\nEnter the item to be added to the queue: ");
                        scanf("%d",&item);
                        addq(item,&fulltest,&emptytest);
                        break;
                case 2:showqueue(&emptytest,&fulltest);
                        break;
                default :
                        exit(0) ;

            }
        }
    }
}

Macros for true=1 and false=0

#include<stdio.h>

int main()
{

printf(“%d %d “,true , false );

}
This piece of code gives an error as “true” and “false” ,which we are trying to print are undeclared.

 

#include<stdio.h>
#include<stdbool.h>
int main()
{

printf(“%d %d “,true , false );

}

but by adding the header <stdbool.h> and running the code gives you can output of 1 0 , as the macros for true=1 and false =0 is defined in <stdbool.h>.

 

Executing a shell script

(NOTE:Make sure the path where you have saved the file and your current working directory in the terminal are same )Let XYZ be the name of the shell script you have created .By executing the command , “/bin/sh XYZ” in the terminal the script can be executed .The script can be invoked like any other executable , to do it first the script has to be given a executable permission .Use “chmod +x XYZ” , to give executable permission . Then use “./XYZ”  on the terminal to invoke the script.If you want to invoke the script just by calling its name , just use this command “PATH=$PATH:” to set the shell environment variable to look into the current path too.Now just execute the script from the terminal by using its name , “XYZ”, and script works !!Specifying the path prepended with ./ does have one other advantage: It ensures that you don’t accidentally execute another command on the system with the same name as your script file.

Metaploit penetration tool

Metasploit is must need tool for any hacker …This tool can be used to check whether a vulnerability exists on your computer or network …. a Exploit for a vulnerability is a not a easy task ..This requires skills like Assembly language , reverse engineering , socket programming , Shell coding(writing shell codes) and much much more .But metasploit has made it very easy by providing you with the Database of exploits which can be directly tested to hack a computer either on LAN or on internet ….For more information on using metasploit please refer to “Gray hat hacking 3 rd edition”