Back to index

nagios-plugins  1.4.16
Defines | Functions | Variables
pst3.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <procfs.h>
#include <sys/types32.h>

Go to the source code of this file.

Defines

#define PROC_DIR   "/proc"
#define ARGS   30

Functions

void usage ()
int main (int argc, char **argv)

Variables

static char * szProg

Define Documentation

#define ARGS   30

Definition at line 59 of file pst3.c.

#define PROC_DIR   "/proc"

Definition at line 58 of file pst3.c.


Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 74 of file pst3.c.

{
  DIR *procdir;
  struct dirent *proc;
  char ps_name[ARGS];
  char as_name[ARGS];
  psinfo_t psinfo;

  /* Set our program name global */
  if ((szProg = strrchr(argv[0], '/')) != NULL)
    szProg++;
  else
    szProg = argv[0];

  /* if given any parameters, print out help */
  if(argc > 1) {
    (void)usage();
    exit(1);
  }

  /* Make sure that our euid is root */
  if (geteuid() != 0)
  {
    fprintf(stderr, "%s: This program can only be run by the root user!\n", szProg);
    exit(1);
  }

  if ((procdir = opendir(PROC_DIR)) == NULL) {
    fprintf(stderr, "%s: cannot open PROC directory %s\n", szProg, PROC_DIR);
    exit(1);
  }

  /* Display column headings */
  printf("%c %5s %5s %5s %6s %6s %4s %s %s\n",
    'S',
    "UID",
    "PID",
    "PPID",
    "VSZ",
    "RSS",
    "%CPU",
    "COMMAND",
    "ARGS"
  );

  /* Zip through all of the process entries */
  while((proc = readdir(procdir))) {
    int ps_fd;
    int as_fd;
    off_t argoff;
    int i;
    char *args;
    char *procname;
    char *ptr;
    int argslen;
    uintptr_t args_addr;;
    uintptr_t *args_vecs;;
    int args_count;

    if(proc->d_name[0] == '.')
      continue;

    sprintf(ps_name,"%s/%s/%s",PROC_DIR,proc->d_name,"psinfo");
    sprintf(as_name,"%s/%s/%s",PROC_DIR,proc->d_name,"as");
try_again:
    if((ps_fd = open(ps_name, O_RDONLY)) == -1)
      continue;

    if((as_fd = open(as_name, O_RDONLY)) == -1)
      continue;

    if(read(ps_fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
      int err = errno;
      close(ps_fd);
      close(as_fd);
      if(err == EAGAIN) goto try_again;
      if(err != ENOENT)
        fprintf(stderr, "%s: read() on %s: %s\n", szProg,
          ps_name, strerror(err));
      continue;
    }
    close(ps_fd);

    /* system process, ignore since the previous version did */
    if(
      psinfo.pr_nlwp == 0 ||
      strcmp(psinfo.pr_lwp.pr_clname, "SYS") == 0
    ) {
      continue;
    }

    /* get the procname to match previous versions */
    procname = strdup(psinfo.pr_psargs);
    if((ptr = strchr(procname, ' ')) != NULL)
        *ptr = '\0';
    if((ptr = strrchr(procname, '/')) != NULL)
        ptr++;
    else
        ptr = procname;

    /*
     * print out what we currently know
     */
    printf("%c %5d %5d %5d %6lu %6lu %4.1f %s ",
      psinfo.pr_lwp.pr_sname,
      psinfo.pr_euid,
      psinfo.pr_pid,
      psinfo.pr_ppid,
      psinfo.pr_size,
      psinfo.pr_rssize,
      ((float)(psinfo.pr_pctcpu) / 0x8000 * 100.0),
      ptr
    );
    free(procname);

    /*
     * and now for the command line stuff
     */

    args_addr = psinfo.pr_argv;
    args_count = psinfo.pr_argc;
    args_vecs = malloc(args_count * sizeof(uintptr_t));

    if(psinfo.pr_dmodel == PR_MODEL_NATIVE) {
      /* this process matches target process */
      pread(as_fd,args_vecs, args_count * sizeof(uintptr_t),
        args_addr);
    } else {
      /* this process is 64bit, target process is 32 bit*/
      caddr32_t *args_vecs32 = (caddr32_t *)args_vecs;
      pread(as_fd,args_vecs32,args_count * sizeof(caddr32_t),
        args_addr);
      for (i=args_count-1;i>=0;--i)
        args_vecs[i]=args_vecs32[i];
    }

    /*
     * now read in the args - if what we read in fills buffer
     * resize buffer and reread that bit again
     */
    argslen=ARGS;
    args=malloc(argslen+1);
    for(i=0;i<args_count;i++) {
      memset(args,'\0',argslen+1);
      if(pread(as_fd, args, argslen, args_vecs[i]) <= 0) {
        break;
      }
      args[argslen]='\0';
      if(strlen(args) == argslen){
        argslen += ARGS;
        args = realloc(args, argslen + 1);
        i--;
        continue;
      }
      printf(" %s", args);
    }
    free(args_vecs);
    free(args);
    close(as_fd);
    printf("\n");
  }

  (void) closedir(procdir);

  return (0);
}

Here is the call graph for this function:

void usage ( void  )

Variable Documentation

char* szProg [static]

Definition at line 65 of file pst3.c.