2

I'm attempting to code some error alerts into one of my firm's monitoring programs, but I'm not having much luck doing so with return values from the system() command in C. The code invokes an scp transaction to pull a file off another server.....I need it to flag an error if the system is down, or the connection otherwise fails.

I've tried determining what the error return value is with "echo $?" and it looks like 0 is a successful return, then 1 is for anything else, but even coding that into the application it's not picking up a failed attempt. I've just tried getting the return value from the command line, but I don't know whether that value is actually the one returned during execution of the application.

The bottom line is catching when the system() command returns a failure, so I'm not sure if there is a better solution out there.

Thanks, all.

EDIT: Here's the excerpt of code I'm using to identify the error:

if ( system(cmd) != 0 ); 
{
  sftpCrash = TRUE;
  printf("FTP crash detected.")
  return;
}

Then the int sftpCrash gets returned to a calling function, and executes like so:

if ( sftpCrash == TRUE)
{
  Node->color = RED; //Posts an error to our monitoring application
  sprintf(reason, "Failure on SFTP connection to %s. Please check server status.",    
  getenv("HOSTNAME"));
  printf(("SFTP crashed. Should post error alert."));
}

This is all being run and executed on a UNIX server.

Nick L.
  • 63
  • 3
  • 9

1 Answers1

2

The return value of system() is passed as part of the the return status. Use WEXITSTATUS(status) to retrieve it:

int status = system("my command");
if (status != -1) { // -1 means an error with the call itself
    int ret = WEXITSTATUS(status);
    ...
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Hi there, I tried using WEXITSTATUS to identify the return value but when compiling I get an error stating "Expression must have integral type". – Nick L. Jun 26 '13 at 14:37
  • @NickL. You added `#define _XOPEN_SOURCE` before including the first header, right? – Sergey Kalinichenko Jun 26 '13 at 14:38
  • The code and the compilers are prepackaged by a different team, so I don't know if it has been defined further up the code chain or not. – Nick L. Jun 26 '13 at 14:40
  • @NickL. The macro is defined as `(((status)>>8) & 0xFF)`, you can use it in place of `WEXITSTATUS`, like this: `int ret = (((status)>>8) & 0xFF);` – Sergey Kalinichenko Jun 26 '13 at 14:45
  • I tried this and finally got a return. The value is being returned as 1....it looks like it's completing successfully. Is this correct? – Nick L. Jun 26 '13 at 16:54
  • Strike that, I hardcoded it so that it would fail when opening the connection and it still returns a 1....does that mean the return code is always 1? – Nick L. Jun 26 '13 at 16:59
  • @NickL. It looks like FTP return code does not tell you much. Here is a [link to a question](http://stackoverflow.com/q/4899316/335858) with answers that describe possible work-arounds. – Sergey Kalinichenko Jun 26 '13 at 17:02
  • Thank you for the advice, dasblinkenlight....the first option seems possible but is there a way to implement that solution in C? – Nick L. Jun 26 '13 at 17:33
  • @NickL. I think you can, but you would need to implement a subset of FTP client for that. [Here is a link](http://stackoverflow.com/q/13762673/335858). – Sergey Kalinichenko Jun 26 '13 at 17:39
  • Thank you for all the assistance, I was able to go back and sort through the return codes to find a pattern that worked. I've coded it into my application. Much obliged. :) – Nick L. Jun 26 '13 at 22:08