I'm trying to execute a process as root from my application that runs with regular privileges using pkexec
.
When I invoke pkexec
synchronously everything is fine but when I run it asynchronously with the following code :
private void execute_process_async () {
if (permission.allowed ) {
...
Pid child_pid;
var cli = "%s/my_exec".printf (Build.PKGDATADIR);
try {
Process.spawn_async (null,
{ "pkexec", cli,
settings.scrollbar_width.to_string (),
settings.scrollbar_button_radius.to_string (),
settings.active_tab_underline_color
},
Environ.get (),
SpawnFlags.SEARCH_PATH,
null,
out child_pid);
} catch (SpawnError e) {
report_error ("error while executing '%s'. Message: '%s'.".printf (cli, e.message)) ;
}
}
}
I get an a rather grim error message:
Refusing to render service to dead parents.
Do you know what is happening?
It is not allowed to run
pkexec
in the background by fork and exec and then terminating the parent. The process becomes an orphan and belongs toinit
(ppid == 1). See https://lists.ubuntu.com/archives/foundations-bugs/2012-July/100103.html.You're giving control of an application to another user (usually root). The default way is to run the application with
pkexec
as root from a shell script. This way the parent is a shell and notinit
.E. g. GParted (
/usr/bin/gparted-pkexec
) and GameConqueror (/usr/bin/gameconqueror
) do it like this as shown below:Create a shell script as
/usr/local/bin/foo.sh
:Give executable permission to above script:
If you are using
.desktop
file, changeExec
value as:Make sure that your policy file in
/usr/share/polkit-1/actions/
has following entry to allow GUI:That’s it!
Check out this link it worked for me.
Bash alias setup for using aliases to run programs with out having to type
Instead it becomes alias(application-name):