External commands - Error

Hello everybody,

I am having a problem regarding external command execution.

This is the piece of code:
function f (s : string) : Exec::Result
{
  local anagram : Exec::Command;
  local res : Exec::Result;
  res = [$exit_code = 0];
  local prog = "/home/riccardo/ngram-ids/ngram-ids/ngram-ids";
  local param = "-l /home/riccardo/ngram-ids/ngram-ids/save -m test -t 0 -U " + s;
  anagram = [$cmd = prog, $stdin = param];
  res = Exec::run(anagram);
  return res;
}
Basically the problem is that the variable res do not receive any value
from the execution of the program. Since this execution should be run
several times because is within a loop, I do not know if there are
problems of threading (like wait for the result). I also receive an
error like this:

1394205441.982764 warning: non-void function returns without a value:
Exec::run
1394205441.982764 error: return trigger in context which does not allow
delaying result

thank you in advance,

R.

Hi,

you have to use Exec::run inside of a when statement - because the command is run asynchronously results are not immediately available.

You can e.g. see https://github.com/bro/bro/blob/master/scripts/base/utils/dir.bro for an example that uses it.

Johanna

Thank you very much guys!

Hi guys,

I have inserted the command within the when statement
{
local s = "whatever";
local anagram : Exec::Command;
  local param = "-l /home/riccardo/ngram-ids/ngram-ids/save -m test -t 0
-U " + s;
  local prog = "/home/riccardo/ngram-ids/ngram-ids/ngram-ids" + param;
  anagram = [$cmd = prog];
  when ( local res = Exec::run(anagram) )
  {
    print "I AM IN! - Debug";
  }

  return "Not Executed!";
}

However seems it is not able to enter in the block. Like the program is
not executed at all. If I run the command from my command line it
perfectly works. I do not really understand why it does not go within
that block.

thank you for help,

R.

Sorry for writing another email.

This is a simple script that does not work, and basically follows the
same concept.

@load base/utils/exec

event bro_init()
{

  local cmd = Exec::Command($cmd="echo 'hello'");

  when (local res = Exec::run(cmd))
  {
    print "hello";
    print res$stdout;
  }

}

At the beginning of your script, add…

redef exit_only_after_terminate=T;

Bro normally shuts down if there is nothing feeding the event queue (and when statements are asynchronous and don’t count as feeding the event queue).

  .Seth