Vala, PolicyKit & DBUS

I’m trying to expand systemadm(GUI of systemd to manage daemons). I have matter with access to systemd via DBus. Systemadm get list of services and put it to listview. Matter is once user click stop/start and service is selected.

There’s my code in Vala:


void get_polkit_access(string path) {

  var authority = Polkit.Authority.get_sync(null);
  var subject = new Polkit.UnixProcess(Posix.getpid());
  var auth  =  Polkit.Authority.get();
  var is_ok = auth.check_authorization_sync(subject, path, null, Polkit.CheckAuthorizationFlags.ALLOW_USER_INTERACTION, null);
  
  if (is_ok.get_is_authorized()) {
  
    stdout.printf("OK!
");
  }
}

void on_start() {
  if (mwindow.current_unit == null) {
  
    return;
  }
  
  try {
    mwindow.current_unit->start("replace");
  }
  catch (DBusError e) {
  
    if (DBusError.is_remote_error(e)) {
      string remote = DBusError.get_remote_error(e);
      
      if (dbus_error_to_errno(remote) == Posix.EACCES) {
      
       // get_polkit_access("org.freedesktop.systemd1.reload-daemon");
        get_polkit_access("org.freedesktop.systemd1.manage-units");
        try {
        mwindow.current_unit->start("replace");
        }
        catch (Error e) {
        
          mwindow.show_error(e);
        }
      }
      else {
        
        mwindow.show_error(e);
      }
    }
    else {
      
      mwindow.show_error(e);
    }
      
  }
  catch (Error e) {
    
    mwindow.show_error(e);
  }
}

PolicyKit seems to grant privileges, because prompt for input password is displaying only first time user click start/stop. Matter is message that Access is Denied(DBus error is returned) always get returned and nothing happens.

systemd calls polkit itself (and using different subject kind); it is using hashed polkit response so it simply looks like polkit does not grant you necessary privileges.

Thanks for response. I call systemd via DBus, not invoking systemctl command. Is that true, what you wrote, in this case?

Yes. systemctl does the same D-Bus invocation at the end.