Back to index

nagios-plugins  1.4.16
Classes | Defines | Typedefs | Functions
utils_cmd.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  output
 types More...

Defines

#define CMD_INIT   cmd_init()
#define CMD_NO_ARRAYS   0x01 /* don't populate arrays at all */
#define CMD_NO_ASSOC   0x02 /* output.line won't point to buf */

Typedefs

typedef struct output

Functions

int cmd_run (const char *, output *, output *, int)
 prototypes
int cmd_run_array (char *const *, output *, output *, int)
int cmd_file_read (char *, output *, int)
void cmd_init (void)

Class Documentation

struct output

types

Definition at line 11 of file utils_cmd.h.

Class Members
char * buf
size_t buflen
size_t * lens
char ** line
size_t lines

Define Documentation

#define CMD_INIT   cmd_init()

Definition at line 29 of file utils_cmd.h.

#define CMD_NO_ARRAYS   0x01 /* don't populate arrays at all */

Definition at line 32 of file utils_cmd.h.

#define CMD_NO_ASSOC   0x02 /* output.line won't point to buf */

Definition at line 33 of file utils_cmd.h.


Typedef Documentation

typedef struct output

Definition at line 20 of file utils_cmd.h.


Function Documentation

int cmd_file_read ( char *  ,
output ,
int   
)

Definition at line 381 of file utils_cmd.c.

{
       int fd;
       if(out)
              memset (out, 0, sizeof(output));

       if ((fd = open(filename, O_RDONLY)) == -1) {
              die( STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno) );
       }
       
       if(out)
              out->lines = _cmd_fetch_output (fd, out, flags);

       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void cmd_init ( void  )
int cmd_run ( const char *  ,
output ,
output ,
int   
)

prototypes

Definition at line 284 of file utils_cmd.c.

{
       int fd, pfd_out[2], pfd_err[2];
       int i = 0, argc;
       size_t cmdlen;
       char **argv = NULL;
       char *cmd = NULL;
       char *str = NULL;

       if (cmdstring == NULL)
              return -1;

       /* initialize the structs */
       if (out)
              memset (out, 0, sizeof (output));
       if (err)
              memset (err, 0, sizeof (output));

       /* make copy of command string so strtok() doesn't silently modify it */
       /* (the calling program may want to access it later) */
       cmdlen = strlen (cmdstring);
       if ((cmd = malloc (cmdlen + 1)) == NULL)
              return -1;
       memcpy (cmd, cmdstring, cmdlen);
       cmd[cmdlen] = '\0';

       /* This is not a shell, so we don't handle "???" */
       if (strstr (cmdstring, "\"")) return -1;

       /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */
       if (strstr (cmdstring, " ' ") || strstr (cmdstring, "'''"))
              return -1;

       /* each arg must be whitespace-separated, so args can be a maximum
        * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */
       argc = (cmdlen >> 1) + 2;
       argv = calloc (sizeof (char *), argc);

       if (argv == NULL) {
              printf ("%s\n", _("Could not malloc argv array in popen()"));
              return -1;
       }

       /* get command arguments (stupidly, but fairly quickly) */
       while (cmd) {
              str = cmd;
              str += strspn (str, " \t\r\n");    /* trim any leading whitespace */

              if (strstr (str, "'") == str) {    /* handle SIMPLE quoted strings */
                     str++;
                     if (!strstr (str, "'"))
                            return -1;                                              /* balanced? */
                     cmd = 1 + strstr (str, "'");
                     str[strcspn (str, "'")] = 0;
              }
              else {
                     if (strpbrk (str, " \t\r\n")) {
                            cmd = 1 + strpbrk (str, " \t\r\n");
                            str[strcspn (str, " \t\r\n")] = 0;
                     }
                     else {
                            cmd = NULL;
                     }
              }

              if (cmd && strlen (cmd) == strspn (cmd, " \t\r\n"))
                     cmd = NULL;

              argv[i++] = str;
       }

       return cmd_run_array (argv, out, err, flags);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int cmd_run_array ( char *const ,
output ,
output ,
int   
)

Definition at line 359 of file utils_cmd.c.

{
       int fd, pfd_out[2], pfd_err[2];

       /* initialize the structs */
       if (out)
              memset (out, 0, sizeof (output));
       if (err)
              memset (err, 0, sizeof (output));

       if ((fd = _cmd_open (argv, pfd_out, pfd_err)) == -1)
              die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]);

       if (out)
              out->lines = _cmd_fetch_output (pfd_out[0], out, flags);
       if (err)
              err->lines = _cmd_fetch_output (pfd_err[0], err, flags);

       return _cmd_close (fd);
}

Here is the call graph for this function:

Here is the caller graph for this function: