3.2. Root commands

The apache server under which eBox runs, and any Perl script that uses the eBox API run under a dedicated user id, the user is typically called “ebox”. eBox modules need to execute certain commands and write certain files with root privileges, this is done using sudo

You can invoke any command using the root() function within the EBox::Sudo Perl module. If the command fails, root() throws an EBox::Exceptions::Sudo::Command exception, so make sure you catch it if it is OK for the command to fail or if you want to inform the user in a different way. Anyway, you can use the output, error and exitValue exception methods to gather more information about the command failure. In the rare cases when the sudo program itself fails the exception raised is one of the EBox::Exceptions::Sudo::Wrapper kind and the last methods are not available.

Example 3.2. Using EBox::Sudo::root()

try {
  EBox::Sudo::root("/usr/bin/eject -t");
}
catch EBox::Exceptions::Sudo::Wrapper with {
  throw EBox::Exceptions::Internal("sudo program failed");
}
catch EBox::Exceptions::Sudo::Command with {
  my ($ex) = @_;
  EBox::debug("/usr/bin/eject failed. Stderr was " . (join "\n", @{ $ex->error() })) ;
  throw EBox::Exceptions::External("Cannot close CD-ROM tray. Please close it manually" );
}

EBox::Sudo also provides wrappers for frequent used commands executed with root privileges. stat provides a statistic wrapper, useful to get file system information for any file. If you need a more concise file information you can use fileTest to check files for simples queries like /usr/bin/test does.