withPrivileges method

void withPrivileges(
  1. RunPrivileged action, {
  2. bool allowUnprivileged = false,
})

Run action with root UID and gid

Implementation

void withPrivileges(RunPrivileged action, {bool allowUnprivileged = false}) {
  final startedPriviledged = Shell.current.isPrivilegedProcess;
  if (!allowUnprivileged && !startedPriviledged) {
    throw ShellException(
      'You can only use withPrivileges when running as a privileged user.',
    );
  }
  final isprivileged = geteuid() == 0;

  if (!isprivileged && startedPriviledged) {
    restorePrivileges();
  }

  action();

  /// If the code was originally running privileged then
  /// we leave it as it was.
  if (!isprivileged && startedPriviledged) {
    releasePrivileges();
  }
}