Back to index

nagios-plugins  1.4.16
child_test.c
Go to the documentation of this file.
00001 /* Base code taken from http://www-h.eng.cam.ac.uk/help/tpl/unix/fork.html
00002  * Fix for redhat suggested by Ptere Pramberger, peter@pramberger.at */
00003 #include <unistd.h>
00004 #include <sys/wait.h>
00005 #include <stdio.h>
00006 #include <sys/types.h>
00007 #include <signal.h>
00008 void popen_sigchld_handler (int);
00009 int childtermd;
00010 
00011 int main(){
00012  char str[1024];
00013  int pipefd[2];
00014  pid_t pid;
00015  int status, died;
00016 
00017         if (signal (SIGCHLD, popen_sigchld_handler) == SIG_ERR) {
00018                 printf ("Cannot catch SIGCHLD\n");
00019               _exit(-1);
00020         }
00021 
00022   pipe (pipefd);
00023   switch(pid=fork()){
00024    case -1: 
00025            printf("can't fork\n");
00026             _exit(-1);
00027    
00028    case 0 : /* this is the code the child runs */
00029             close(1);      /* close stdout */
00030             /* pipefd[1] is for writing to the pipe. We want the output
00031              * that used to go to the standard output (file descriptor 1)
00032              * to be written to the pipe. The following command does this,
00033              * creating a new file descripter 1 (the lowest available) 
00034              * that writes where pipefd[1] goes. */
00035             dup (pipefd[1]); /* points pipefd at file descriptor */
00036             /* the child isn't going to read from the pipe, so
00037              * pipefd[0] can be closed */
00038             close (pipefd[0]);
00039 
00040            /* These are the commands to run, with success commented. dig and nslookup only problems */
00041             /*execl ("/bin/date","date",0);*/                  /* 100% */
00042            /*execl ("/bin/cat", "cat", "/etc/hosts", 0);*/     /* 100% */
00043            /*execl ("/usr/bin/dig", "dig", "redhat.com", 0);*/ /* 69% */
00044            /*execl("/bin/sleep", "sleep", "1", 0);*/           /* 100% */
00045             execl ("/usr/bin/nslookup","nslookup","redhat.com",0); /* 90% (after 100 tests), 40% (after 10 tests) */
00046             /*execl ("/bin/ping","ping","-c","1","localhost",0);*/    /* 100% */
00047             /*execl ("/bin/ping","ping","-c","1","192.168.10.32",0);*/       /* 100% */
00048            _exit(0);
00049 
00050    default: /* this is the code the parent runs */
00051 
00052             close(0); /* close stdin */
00053             /* Set file descriptor 0 (stdin) to read from the pipe */
00054             dup (pipefd[0]);
00055             /* the parent isn't going to write to the pipe */
00056             close (pipefd[1]);
00057             /* Now read from the pipe */
00058             fgets(str, 1023, stdin);
00059             /*printf("1st line output is %s\n", str);*/
00060 
00061            /*while (!childtermd);*/  /* Uncomment this line to fix */
00062 
00063             died= wait(&status);
00064            /*printf("died=%d status=%d\n", died, status);*/
00065            if (died > 0) _exit(0);
00066            else          _exit(1);
00067    }
00068 }
00069 
00070 void
00071 popen_sigchld_handler (int signo)
00072 {
00073         if (signo == SIGCHLD) {
00074                 /*printf("Caught sigchld\n");*/
00075                 childtermd = 1;
00076         }
00077 }