Catching SIGINT(ctrl+c) signal in GNU/LINUX

/*SIGINT
 *RUN the code and press ctrl+c ,This signal is sent to all processes in the foreground process group when the user
 *enters the interrupt character (usually Ctrl-C). The default behavior is to
 *terminate; however, processes can elect to catch and handle this signal, and
 *generally do so to clean up before terminating.*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
/* handler for SIGINT */
static void sigint_handler (int signo)
{

  printf (“Caught SIGINT!\n”);
  exit (EXIT_SUCCESS);
}
int main (void)
{
/*
* Register sigint_handler as our signal handler
* for SIGINT.
*/
if (signal (SIGINT, sigint_handler) == SIG_ERR) {
 fprintf (stderr, “Cannot handle SIGINT!\n”);
exit (EXIT_FAILURE);
}

pause (); //pause waits till a signal is generated
return 0;
}

Signals and Description of Default action in GNU/LINUX

Signals and Description of Default action

 

  1. SIGABRT Sent by abort( ) Terminate with core dump

  2. SIGALRM Sent by alarm( ) Terminate

  3. SIGBUS Hardware or alignment error Terminate with core dump

  4. SIGCHLD Child has terminated Ignored

  5. SIGCONT Process has continued after being stopped Ignored

  6. SIGFPE Arithmetic exception Terminate with core dump

  7. SIGHUP Process’s controlling terminal was closed (most frequently, Terminate the user logged out)

  8. SIGILL Process tried to execute an illegal instruction Terminate with core dump SIGINT User generated the interrupt character (Ctrl-C) Terminate

  9. SIGIO Asynchronous I/O event Terminate

  10. SIGKILL Uncatchable process termination Terminate

  11. SIGPIPE Process wrote to a pipe but there are no readers Terminate

  12. SIGPROF Profiling timer expired Terminate

  13. SIGPWR Power failure Terminate

  14. SIGQUIT User generated the quit character (Ctrl-\) Terminate with core dump

  15. SIGSEGV Memory access violation Terminate with core dump

  16. SIGSTKFLT Coprocessor stack fault Terminate

  17. SIGSTOP Suspends execution of the process Stop

  18. SIGSYS Process tried to execute an invalid system call Terminate with core dump

  19. SIGTERM Catchable process termination Terminate

  20. SIGTRAP Break point encountered Terminate with core dump

  21. SIGTSTP User generated the suspend character (Ctrl-Z) Stop

  22. SIGTTIN Background process read from controlling terminal Stop

  23. SIGTTOU Background process wrote to controlling terminal Stop

  24. SIGURG Urgent I/O pending Ignored

  25. SIGUSR1 Process-defined signal Terminate

  26. SIGUSR2 Process-defined signal Terminate

  27. SIGVTALRM Generated by setitimer( ) when called with the Terminate ITIMER_VIRTUAL flag

  28. SIGWINCH Size of controlling terminal window changed Ignored

  29. SIGXCPU Processor resource limits were exceeded Terminate with core dump

  30. SIGXFSZ File resource limits were exceeded Terminate with core dump

 

Illustration of ‘isatty’ system call in GNU/LINUX

/* Hackintosh rao , codes for GNU/LINUX*/
/* int isatty(fd);
   fd is the file descriptor of a open file
   if the terminal is associated with the file whose Fd is given then ,
   isatty returns 1 , else
   it returns 0
   this system call is used to check whether any open file  with a file descriptor
   is connected to a terminal or not */
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
    if(!(isatty(fileno(stdout))))  /*fileno function returns the filedescriptor*/
    {
        write(2,”\n\nstandard o/p not associated with control terminal\n\n”,60);
        /*cannot write into standard output with File descriptor 1 , when the
        terminal is not associated with it , that is when standard o/p is redirected
        using ‘>’ operator when the program is invoked, so the o/p is written into
        standard error with file descriptor 2*/
        exit(0);
    }
    else

    {
        printf(“\no/p is associated with the control terminal\n\n”);
    }

    return 0;
}

illustration of lseek system call in GNU/LINUX

 

The lseek( ) system call is provided to set the file position of a file descriptor to a given value.

#include <sys/types.h>

#include <unistd.h>

off_t lseek (int fd, off_t pos, int origin);

 

Description of the arguments to lseek

1.fd=file descriptor of the open file

 

2.pos=offset

 

3.The behavior of lseek( ) depends on the origin argument, which can be one of the following:

SEEK_CUR

The current file position of fd is set to its current value plus pos, which can be negative, zero, or positive. A pos of zero returns the current file position value

 

new file postion=current file position + offset(the pos argument to lseek)

 

SEEK_END

The current file position of fd is set to the current length of the file plus pos,

which can be negative, zero, or positive. A pos of zero sets the offset to the end

of the file.

 

new file postion= file position of End Of the File + offset

SEEK_SET

The current file position of fd is set to pos.the offset is measured from the beggining of the file. A pos of zero sets the offset to the beginning of the file.

 

new file postion= offset

The call returns the new file position on success. On error, it returns -1 and errno is set as

appropriate.

 

For example, to set the file position of fd to 1000:

off_t ret;

ret = lseek (fd, (off_t) 1000, SEEK_SET);

if (ret == (off_t) -1)

/* error */

 

 

Alternatively, to set the file position of fd to the end of the file:

off_t ret;

ret = lseek (fd, 0, SEEK_END);

if (ret == (off_t) -1)

/* error */

 

As lseek( ) returns the updated file position, it can be used to find the current file position

via a SEEK_CUR to zero:

int pos;

pos = lseek (fd, 0, SEEK_CUR);

if (pos == (off_t) -1)

/* error */

else

/* ‘pos’ is the current position of fd */

By far, the most common uses of lseek( ) are seeking to the beginning, seeking to the end,

or determining the current file position of a file descriptor.

 

 

ILLUSTRATION OF a ORPHAN PROCESS AND ASSIGNMENT OF INIT AS A PARENT TO SUCH A PROCESS USING FORK

//DOWNLOAD LINK FOR THE FILE:http://www.slideshare.net/hackintoshrao/forkexpeImage

/* Hackintoshrao:codes for GNU/LINUX

   ILLUSTRATION OF ORPHAN PROCESS AND ASSIGNMENT OF INIT AS A PARENT TO SUCH A PROCESS
   SYSTEM CALLS USED
   1.fork()
   2.getpid()
   3.getppid()
   4.sleep()
   */
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
int main()
{
    int pid;
    pid=fork();
    if(pid==0)
    {

        printf(“\nChild process here \n”);
        printf(“\nchild here..Parent id=%d,Process id=%d\n”,getppid(),getpid());
        sleep(3);
        printf(“\nChild process here,now its a orphan process,so init is my parent process now  \n”);
        printf(“\nchild here…Parent id=%d,Process id=%d\n”,getppid(),getpid());
        /*its clearly evident in the o/p that the parent process Id of the child process will now be =1,
         the init process is made as a parent */
    }
    else{
        sleep(1);
        /*since parent process finishes earlier than the child ,the latter becomes a orphan process */
        printf(“\nparent process here \n”);
        printf(“\nparent process..Process id=%d\n”,getpid());
        printf(“\nparent process Exiting,making the child process a orphan …….\n\n”) ;

    }
}

A Simple Program to programatically invoke your favorite executables using FORK and EXEC System calls in GNU/LINUX

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<wait.h>
void printerror(void);
int main()
{
    int pid,choice;
    int *wai;
    printf(“\nThe process id is:%d “,getpid());
    sleep(2);
    if((pid=fork())==-1)
    {
        printf(“\nerror Forking “);
        exit(0);

    }
    else if(pid==0)
    {
        printf(“\n\nChild process in action,the process Id of the child is %d\n”,getpid());
        printf(“\n\nParent process Id is %d”,getppid());
        sleep(3);
        printf(“\n1.Shell\n2.Firefox\n3.VLC\n4.EXIT\n”);/*just list the path of the executables you want to open*/

        printf(“\t\tMake the choice: “);
        scanf(“%d”,&choice);
        switch(choice)
        {
            case 1:execv(“/bin/bash”,0);//exec the executables
                   printerror();

            case 2:execv(“/usr/bin/firefox”,0);/*this wont work in case the executables doesnt exist*/
                   printerror();
            case 3:execv(“/usr/bin/vlc”,0);
                   printerror();
            default:exit(1);
        }
    }
    else
    {
        wait(wai);
        printf(“\n\nParent process is in action “);
        printf(“\nchild returnds=%d\n\n\n”,*wai);
        
        
    }

}
void printerror()
{
    printf(“\nError opening the executable , check if this exists”);
    exit(0);
}
Image

 

Program to print the size,i-node number and Owner Id in Gnu/Linux

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<pwd.h>
int main()
{

    struct stat *info;

    int i;
    char *path;
    path=(char *)malloc(200);
    printf(“\nEnter the path of the file whose information is to be found: “);
    scanf(“%s”,path);
    i=stat(path,info);

    if(i==-1)
    {
        printf(“\nstat failed\n\n\n\n”);
        exit(0);
    }

    printf(“\nSize of the file:%d Bytes”,info->st_size);
    printf(“\nOwner Id of the file:%d “,info->st_uid);
    printf(“\nGroup Id of the file owner:%d “,info->st_gid);

    printf(“\nInode NO:%d\n\n\n\n “,info->st_ino);
    return 0;
}

PROGRAM TO GET THE COMPLETE INFORMATION OF THE USER USING “getpwuid” system call in gnu/LINUX

/*DOWNLOAD LINK OF THE FILE:http://www.slideshare.net/hackintoshrao/usrinfogetpwuid*/
/*HACKINTOSH RAO \m/ , codes for GNU/LINUX
  PROGRAM TO GET THE COMPLETE INFORMATION OF THE USER USING “getpwuid” system call in gnu/LINUX
  SYSImageTEM CALLS USED: 1) struct passwd *getpwuid(int uid);
  DESCRIPTION: “getpwuid” system call takes uid of the a user as parameter and then fills in the entries of the built in structure
              ‘passwd'( check out the header pwd.h under /usr/include to get the details of the passwd structure) and returns a
               pointer to it .Using the pointer the members of the structure are accessed to get the user info
                     2)uid_t/int getuid()
  DESCRIPTION:’getuid’ system call returns UID of a user who runs the executable of the code*/
#include<pwd.h> //header for getpwuid system call
#include<unistd.h>//header for getuid system call
#include<stdlib.h>
int main()
{
    struct passwd *usrinfo;
    usrinfo=getpwuid(getuid());
    printf(“\nUser name: %s”,usrinfo->pw_name);
    printf(“\n\nUserID:%d”,usrinfo->pw_uid);
    printf(“\n\nEncrypted password of the user:%s”,usrinfo->pw_passwd);
    printf(“\n\nGroup ID:%d”,usrinfo->pw_gid);
    printf(“\n\nReal name:%s”,usrinfo->pw_gecos);
    printf(“\n\nHOME DIRECTORY OF THE USER:%s”,usrinfo->pw_dir);
    printf(“\n\nDefault login shell:%s\n\n\n\n”,usrinfo->pw_shell);

    return 0;

}

A Simple guide to write programs which can be made use only by a specific user in A GNU/LINUX system using getuid and getlogin system calls

/* hackintosh rao \m/ , codes for GNU/LINUX
   Illustration of code which can only be made use
  System calls used:1) uid_t getuid(void);
                    2) char *getlogin(void); */
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
    uid_t uid;/*uid_t is a build in data type, which can hold small numbers*/
    char* username;
    uid=getuid();
    username=getlogin();
    if(uid==1000)//uid of user karthic
    {
        printf(“\nUID is:%d\nWELCOME Username:%s\n\n\n “,uid,username);
        printf(“\nI AM AT YOUR SERVICE\n\n”);
        /*write functionalities here which can only be made use by user with UId 1000*/
        /*not even can root can unlock this,if if root executes the code he cannot make use of it*/

    }
    else
     printf(“\nWrong User, the program is authorized to be used only by %s\n\n”,username);

    return 0;
}
Image

Program to print the user ID and Username in GNU/LINUX using getuid and getlogin system calls

/* hackintosh rao \m/ , codes for GNU/LINUX
   program to print the user ID and username
  System calls used:1) uid_t getuid(void);
                    2) char *getlogin(void); */
#include<stdlib.h>Image
#include<unistd.h>
#include<sys/types.h>
int main()
{
    uid_t uid;/*uid_t is a build in data type, which can hold small numbers*/
    char* username;
    uid=getuid();
    username=getlogin();
    printf(“\nUID is:%d\nUsername:%s\n\n\n “,uid,username);
    return 0;
}