/***********************************************************************
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC  *
* Copyright (C) 2001 by New Riders Publishing                          *
* See COPYRIGHT for license information.                               *
***********************************************************************/

#include 
#include 
#include 
#include 
#include 

#include "server.h"

/* HTML source for the start of the page we generate.  */

static char* page_start =
  "\n"
  " \n"
  "  
\n";

/* HTML source for the end of the page we generate.  */

static char* page_end =
  "  
\n" " \n" "\n"; void module_generate (int fd) { pid_t child_pid; int rval; /* Write the start of the page. */ write (fd, page_start, strlen (page_start)); /* Fork a child process. */ child_pid = fork (); if (child_pid == 0) { /* This is the child process. */ /* Set up an argumnet list for the invocation of df. */ char* argv[] = { "/bin/df", "-h", NULL }; /* Duplicate stdout and stderr to send data to the client socket. */ rval = dup2 (fd, STDOUT_FILENO); if (rval == -1) system_error ("dup2"); rval = dup2 (fd, STDERR_FILENO); if (rval == -1) system_error ("dup2"); /* Run df to show the free space on mounted file systems. */ execv (argv[0], argv); /* A call to execv does not return unless an error occurred. */ system_error ("execv"); } else if (child_pid > 0) { /* This is the parent process. Wait for the child process to finish. */ rval = waitpid (child_pid, NULL, 0); if (rval == -1) system_error ("waitpid"); } else /* The call to fork failed. */ system_error ("fork"); /* Write the end of the page. */ write (fd, page_end, strlen (page_end)); }