releasePrivileges method

  1. @override
void releasePrivileges()
override

On Linux and MacOS systems makes the script run as a non-privileged user even when started with sudo.

This method is used to overcome issues when running as sudo where the script would change the ownership to root:root for any created/modified file.

This method is normally called as the first line of your main() method.

Calling this method on Windows is unnecessary but harmless.

Calling this method for a non-privileged user has no affect.

On Linux and MacOS releasing privileges sets the uid and gid to the user's original privileges so any files that are created/modified get the original user's uid/gid.

The user environment is also restored in dcli's env map and for child processes. On Linux this includes the XDG runtime directory and D-Bus session address used by per-user desktop and credential services. Native libraries should be given these values explicitly; this method does not mutate libc's process environment.

You should use this method in conjuctions with withPrivileges so that only specific parts of your code run with privileges.

You must NEVER call releasePrivileges within a withPrivileges action.

void main(){

 ///  downgrade script to not run as sudo
 Shell.current.releasePrivileges();

 /// ... do some non-sudo things

 /// any code within the following code block will be run
 /// with sudo privileges.
 Shell.current.withPrivileges(() {
   copyTree('\etc\keys', '\some\insecure\location');
  });
}

See:

Implementation

@override
void releasePrivileges() {
  // no op.
  verbose(() => 'releasePrivileges called on UnknownShell. ignored');
}