Skip to content

Commit

Permalink
clean hashmaps in ProcessProvider; retrieve children for a process
Browse files Browse the repository at this point in the history
  • Loading branch information
stsdc committed Dec 13, 2023
1 parent a94f2da commit 5a00128
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 55 deletions.
6 changes: 5 additions & 1 deletion flatpak/workaround/process_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def GetProcesses(self, name, sender=None, conn=None):
"stat": "",
"statm": "",
"status": "",
"io": ""
"io": "",
"children": ""
}
with open(f'/proc/{pid}/cmdline', 'rb') as file:
process["cmdline"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' '))
Expand All @@ -51,6 +52,9 @@ def GetProcesses(self, name, sender=None, conn=None):

with open(f'/proc/{pid}/status', 'rb') as file:
process["status"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' '))

with open(f'/proc/{pid}/task/{pid}/children', 'rb') as file:
process["children"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' '))

with open(f'/proc/{pid}/statm', 'rb') as file:
process["statm"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' '))
Expand Down
4 changes: 2 additions & 2 deletions src/Managers/Process.vala
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public class Monitor.Process : GLib.Object {
var process_provider = ProcessProvider.get_default ();
string ? status = process_provider.pids_status.get (this.stat.pid);
var status_line = status.split ("\n");
return int.parse(status_line[8].split("\t")[1]);
return int.parse (status_line[8].split ("\t")[1]);

}
GTop.ProcUid proc_uid;
GTop.get_proc_uid (out proc_uid, stat.pid);
Expand Down
14 changes: 10 additions & 4 deletions src/Managers/ProcessProvider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace Monitor {
return instance.once (() => { return new ProcessProvider (); });
}

public Gee.HashSet<int> pids = new Gee.HashSet<int> ();
public Gee.HashMap<int, string> pids_cmdline = new Gee.HashMap<int, string> ();
public Gee.HashMap<int, string> pids_stat = new Gee.HashMap<int, string> ();
public Gee.HashMap<int, string> pids_io = new Gee.HashMap<int, string> ();
public Gee.HashMap<int, string> pids_status = new Gee.HashMap<int, string> ();
public Gee.HashMap<int, string> pids_children = new Gee.HashMap<int, string> ();

DBusWorkaroundClient dbus_workaround_client;

Expand All @@ -22,17 +22,23 @@ namespace Monitor {
public int[] get_pids () {
if (ProcessUtils.is_flatpak_env ()) {
int[] pids;
pids_cmdline.clear ();
pids_stat.clear ();
pids_io.clear ();
pids_status.clear ();
pids_children.clear ();
try {
HashTable<string, string>[] procs = dbus_workaround_client.interface.get_processes ("");
pids = new int[procs.length];
debug ("Workaround: pids: %d", procs.length);
debug ("Workaround: retrieved pids: %d", procs.length);
for (int i = 0; i < procs.length; i++) {
// debug (procs[i]["pid"]);
// debug (procs[i]["pid"]);
pids[i] = int.parse (procs[i]["pid"]);
pids_cmdline.set (pids[i], procs[i]["cmdline"]);
pids_stat.set (pids[i], procs[i]["stat"]);
pids_status.set (pids[i], procs[i]["status"]);
pids_io.set (pids[i], procs[i]["io"]);
pids_children.set (pids[i], procs[i]["children"]);
pids_status.set (pids[i], procs[i]["status"]);
}
} catch (Error e) {
warning (e.message);
Expand Down
96 changes: 48 additions & 48 deletions src/Managers/ProcessUtils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class Monitor.ProcessUtils {
}

public static string ? read_file (string path) {
// if (ProcessUtils.is_flatpak_env ()) {
// return ProcessUtils.read_file_on_host (path);
// }
// if (ProcessUtils.is_flatpak_env ()) {
// return ProcessUtils.read_file_on_host (path);
// }

return ProcessUtils.read_file_native (path);
}
Expand Down Expand Up @@ -78,60 +78,60 @@ public class Monitor.ProcessUtils {
Pid child_pid;

try {
// string command = "flatpak-spawn --host cat " + path + " | grep -av DEBUG";
// string command = "flatpak-spawn --host cat " + path + " | grep -av DEBUG";
debug (command);
GLib.Process.spawn_command_line_sync
(
command,
out stdout,
out stderr,
out status
command,
out stdout,
out stderr,
out status
);


// GLib.Process.spawn_async_with_pipes
// ("/",
// spawn_args,
// Environ.get (),
// SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
// null,
// out child_pid,
// out standard_input,
// out standard_output,
// out standard_error
// );
// GLib.Process.spawn_async_with_pipes
// ("/",
// spawn_args,
// Environ.get (),
// SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
// null,
// out child_pid,
// out standard_input,
// out standard_output,
// out standard_error
// );
// debug ("Status: %d", status);
} catch (SpawnError e) {
error (e.message);
}
// IOChannel output = new IOChannel.unix_new (standard_output);
// output.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => {
// if (condition == IOCondition.HUP) {
// print ("%s: The fd has been closed.\n", "stream_name");
// return false;
// }
// try {
// string line;
// channel.read_line (out line, null, null);
// print ("%s: %s", "stream_name", line);
// } catch (IOChannelError e) {
// print ("%s: IOChannelError: %s\n", "stream_name", e.message);
// return false;
// } catch (ConvertError e) {
// print ("%s: ConvertError: %s\n", "stream_name", e.message);
// return false;
// }
// return true;
// });

// ChildWatch.add (child_pid, (pid, status) => {
// // Triggered when the child indicated by child_pid exits
// GLib.Process.close_pid (pid);
// });

// IOChannel output = new IOChannel.unix_new (standard_output);
// output.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => {
// if (condition == IOCondition.HUP) {
// print ("%s: The fd has been closed.\n", "stream_name");
// return false;
// }

// try {
// string line;
// channel.read_line (out line, null, null);
// print ("%s: %s", "stream_name", line);
// } catch (IOChannelError e) {
// print ("%s: IOChannelError: %s\n", "stream_name", e.message);
// return false;
// } catch (ConvertError e) {
// print ("%s: ConvertError: %s\n", "stream_name", e.message);
// return false;
// }

// return true;
// });

// ChildWatch.add (child_pid, (pid, status) => {
//// Triggered when the child indicated by child_pid exits
// GLib.Process.close_pid (pid);
// });


string ? stdout_no_debug = "";

if (stdout == "" || status != 0) {
Expand All @@ -145,7 +145,7 @@ public class Monitor.ProcessUtils {
}
}
// print (stdout_no_debug);
// return null;
// return null;
return stdout_no_debug;
}

Expand Down

0 comments on commit 5a00128

Please sign in to comment.