LJ Archive CD

Listing 1. Enabling and Disabling Accounting to a File

/* pa.c
 * Linux demonstration program.
 * Logs process accounting information to a
 * file specified on the command line.
 * If no filename is specified, process
 * accounting is switched off.
 */
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int
main (int argc, char **argv)
{

   int rc;

   if (argc == 1) /* No arguments - switch off */
   {
      printf("Turning off process accounting.\n");
      if ( (rc = acct (NULL)) )
      {
   if (errno == ENOSYS)
   {
     printf
       ("It appears your kernel does not"
        " include accounting support\n");
   }
        perror("Problem turning off accounting");
   return rc;
      }
   }


   else /* cmd line arg - switch accounting on */
   {
      printf
    ("Attempting to log to file %s.\n",
     argv[1]);
      rc =
    creat (argv[1],
      S_IRWXU | S_IRGRP | S_IROTH);

      if (rc == -1)
      {
    perror("Problem creating log file");
    return rc;
      }

      if ( (rc = acct (argv[1])) )
      {
   perror("Problem in acct() call");
   return rc;
      }

   }

   return 0;
}

LJ Archive CD