40 string[] tempPlaces = {
41 Environment.get_tmp_dir(),
43 Environment.get_home_dir() +
"/.temp",
47 foreach (
string tempPlace
in tempPlaces) {
48 string std_out, std_err;
55 if ((std_out ==
null) || (std_out.strip() !=
"ok")){
64 stderr.printf(
"No usable temp directory was found!\n");
67 public int exec_sync (
string cmd, out
string? std_out =
null, out
string? std_err =
null){
75 Process.spawn_command_line_sync(cmd, out std_out, out std_err, out status);
85 out
string? std_out =
null, out
string? std_err =
null,
86 bool supress_errors =
false,
bool run_as_admin =
false,
87 bool cleanup_tmp =
true,
bool print_to_terminal =
false){
96 if (sh_file ==
null) {
101 string sh_file_admin =
"";
105 var script_admin =
"#!/usr/bin/env bash\n";
106 script_admin +=
"pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY";
109 sh_file_admin = GLib.Path.build_filename(
file_parent(sh_file),
"script-admin.sh");
115 string[] argv =
new string[1];
117 argv[0] = sh_file_admin;
123 string[] env = Environ.get();
127 if (print_to_terminal){
133 SpawnFlags.SEARCH_PATH,
146 SpawnFlags.SEARCH_PATH,
164 if (!supress_errors){
183 string[] argv =
new string[1];
184 argv[0] = scriptfile;
186 string[] env = Environ.get();
189 Process.spawn_async_with_pipes(
193 SpawnFlags.SEARCH_PATH,
212 string cmd = command;
215 string? user =
TeeJee.
System.get_username_from_uid(uid);
217 cmd =
"pkexec --user %s env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS ".printf(user) + cmd;
226 bool force_locale =
true,
bool supress_errors =
false){
228 string sh_path = script_path;
233 var script =
new StringBuilder();
234 script.append (
"#!/usr/bin/env bash\n");
235 script.append (
"\n");
237 script.append(
"LANG=C\n");
238 script.append(
"LC_ALL=C.UTF-8\n");
240 script.append (
"\n");
241 script.append (
"%s\n".printf(commands));
242 script.append (
"\n\nexitCode=$?\n");
243 script.append (
"echo ${exitCode} > status\n");
245 if ((sh_path ==
null) || (sh_path.length == 0)){
251 var file = File.new_for_path (sh_path);
252 if (file.query_exists ()) {
255 var file_stream = file.create (FileCreateFlags.REPLACE_DESTINATION);
256 var data_stream =
new DataOutputStream (file_stream);
257 data_stream.put_string (script.str);
261 Posix.chmod (sh_path, 0744);
266 if (!supress_errors){
284 string? path = Environment.find_program_in_path(cmd_tool);
285 return (path !=
null) && (path.length > 0);
291 string pidStr = (pid <= 0 ?
"self" : pid.to_string());
292 string path =
"/proc/%s/exe".printf(pidStr);
295 link = GLib.FileUtils.read_link(path);
300 return GLib.Path.get_basename(link);
305 string pidStr = (process <= 0 ?
"self" : process.to_string());
306 string path =
"/proc/%s/stat".printf(pidStr);
308 string details = stats.split(
")", 2)[1];
309 string[] splitted = details.split(
" ", 3);
310 if(splitted.length == 3) {
311 return int.parse(splitted[2]);
314 log_debug(
"can not parse process stat %s".printf(stats));
321 GLib.File file = GLib.File.new_for_path(
"/proc/%d".printf(process));
323 GLib.FileInfo info = file.query_info(FileAttribute.UNIX_UID, GLib.FileQueryInfoFlags.NONE);
324 return (
int) info.get_attribute_uint32(FileAttribute.UNIX_UID);
325 }
catch(GLib.Error e) {
326 log_debug(
"failed to fetch user of process %i %s".printf(process, e.message));
340 }
while(user != targetUser && ppid > 1);
341 if(user == targetUser) {
356 public string?
get_env(
string[] env,
string name,
string? default_value =
null) {
357 foreach(
string env_var
in env) {
358 string[] splitted = env_var.split(
"=", 2);
359 if(splitted[0] == name) {
360 if (splitted.length == 2) {
365 return default_value;
373 File procfs = File.new_for_path(
"/proc/");
376 FileEnumerator enumerator = procfs.enumerate_children(FileAttribute.STANDARD_NAME, FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
378 Pid[] childList = {};
379 while ((info = enumerator.next_file()) !=
null) {
380 if(info.get_file_type() != FileType.DIRECTORY) {
385 string name = info.get_name();
388 if(!uint64.try_parse(name, out pid)) {
393 string? fileCont =
file_read(
"/proc/%s/stat".printf(name));
394 if(fileCont ==
null) {
404 int index = fileCont.last_index_of_char(
')');
405 string parseline = fileCont.substring(index);
406 string[] split = parseline.split(
" ", 4);
407 if(split.length != 4) {
409 log_error(
"can not parse state of %ld".printf((
long) pid));
413 uint64 ppid = uint64.parse(split[2]);
414 if(ppid != 0 && ppid == parent_pid) {
416 childList += (Pid) pid;
422 log_error(
"Failed to get child processes of %ld".printf(parent_pid));
454 Posix.kill (process_pid, sig);
457 foreach (Pid pid
in child_pids){
458 Posix.kill (pid, sig);
469 if (Posix.getpriority (Posix.PRIO_PROCESS, procID) != prio)
470 Posix.setpriority (Posix.PRIO_PROCESS, procID, prio);
477 return Posix.getpriority (Posix.PRIO_PROCESS, procID);
string escape_single_quote(string file_path)
string? file_read(string file_path)
string file_parent(string file_path)
string?[] file_read_array(string file_path, char delimiter='\n')
bool file_delete(string file_path)
static bool dir_delete(string dir_path, bool show_message=false)
bool dir_create(string dir_path, bool show_message=false)
void log_debug(string message)
void log_error(string message, bool highlight=false, bool is_warning=false)
string timestamp_numeric()
string random_string(int length=8, string charset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")
Pid get_process_parent(Pid process=-1)
int exec_script_async(string script)
int get_euid_of_process(Pid process)
int exec_script_sync(string script, out string? std_out=null, out string? std_err=null, bool supress_errors=false, bool run_as_admin=false, bool cleanup_tmp=true, bool print_to_terminal=false)
void process_send_signal(Pid process_pid, Posix.Signal sig, bool children=true)
void process_set_priority_low(Pid procID)
string get_temp_file_path()
string?[] get_process_env(Pid pid)
void process_set_priority_normal(Pid procID)
void process_quit(Pid process_pid, bool killChildren=true)
string? get_env(string[] env, string name, string? default_value=null)
int process_get_priority(Pid procID)
void process_set_priority(Pid procID, int prio)
int exec_sync(string cmd, out string? std_out=null, out string? std_err=null)
void process_kill(Pid process_pid, bool killChildren=true)
static int exec_user_async(string command)
string? save_bash_script_temp(string commands, string? script_path=null, bool force_locale=true, bool supress_errors=false)
string get_process_exe_name(long pid=-1)
static bool cmd_exists(string cmd_tool)
Pid[] get_process_children(Pid parent_pid)