Skip to content

Commit

Permalink
there is an attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
stsdc committed Dec 6, 2023
1 parent 70eb435 commit aa43cef
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 38 deletions.
60 changes: 32 additions & 28 deletions src/Managers/Process.vala
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@ public class Monitor.Process : GLib.Object {
stat.pid = _pid;

// getting uid
GTop.ProcUid proc_uid;
GTop.get_proc_uid (out proc_uid, stat.pid);
uid = proc_uid.uid;

get_uid ();

// getting username
// @TOFIX: Can't get username for postgres when started from docker (?)
// @TOFIX: Can't get username for postgres which
// is started from docker (?)
unowned Posix.Passwd passwd = Posix.getpwuid (uid);
if (passwd != null) {
username = passwd.pw_name;
Expand All @@ -98,16 +96,22 @@ public class Monitor.Process : GLib.Object {
get_usage (0, 1);
}

private void get_uid () {
GTop.ProcUid proc_uid;
GTop.get_proc_uid (out proc_uid, stat.pid);
uid = proc_uid.uid;
}


// Updates the process to get latest information
// Returns if the update was successful
public bool update (uint64 cpu_total, uint64 cpu_last_total) {
exists = parse_stat ();
if (exists) {
get_usage (cpu_total, cpu_last_total);
parse_io ();
parse_statm ();
get_open_files ();
// parse_io ();
// parse_statm ();
// get_open_files ();
}
return exists;
}
Expand Down Expand Up @@ -251,26 +255,26 @@ public class Monitor.Process : GLib.Object {
}

private bool get_open_files () {
try {
string directory = "/proc/%d/fd".printf (stat.pid);
Dir dir = Dir.open (directory, 0);
string ? name = null;
while ((name = dir.read_name ()) != null) {
string path = Path.build_filename (directory, name);

if (FileUtils.test (path, FileTest.IS_SYMLINK)) {
string real_path = FileUtils.read_link (path);
// debug(content);
open_files_paths.add (real_path);
}
}
} catch (FileError err) {
if (err is FileError.ACCES) {
fd_permission_error (err.message);
} else {
warning (err.message);
}
}
// try {
// string directory = "/proc/%d/fd".printf (stat.pid);
// Dir dir = Dir.open (directory, 0);
// string ? name = null;
// while ((name = dir.read_name ()) != null) {
// string path = Path.build_filename (directory, name);

// if (FileUtils.test (path, FileTest.IS_SYMLINK)) {
// string real_path = FileUtils.read_link (path);
// // debug(content);
// open_files_paths.add (real_path);
// }
// }
// } catch (FileError err) {
// if (err is FileError.ACCES) {
// fd_permission_error (err.message);
// } else {
// warning (err.message);
// }
// }
return true;
}

Expand Down
23 changes: 23 additions & 0 deletions src/Managers/ProcessFlatpak.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Monitor.ProcessFlatpak : Process {
public ProcessFlatpak (int _pid) {
base (_pid);
}
// Kills the process
public new bool kill () {
// Sends a kill signal that cannot be ignored
if (Posix.kill (stat.pid, Posix.Signal.KILL) == 0) {
return true;
}
return false;
}

// Ends the process
public new bool end () {
// Sends a terminate signal
if (Posix.kill (stat.pid, Posix.Signal.TERM) == 0) {
return true;
}
return false;
}

}
30 changes: 25 additions & 5 deletions src/Managers/ProcessManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,34 @@ namespace Monitor {
// var pids = GTop.get_proclist (out proclist, GTop.GLIBTOP_KERN_PROC_UID, uid);
var pids = GTop.get_proclist (out proclist, GTop.GLIBTOP_KERN_PROC_ALL, uid);

for (int i = 0; i < proclist.number; i++) {
int pid = pids[i];
if (ProcessUtils.is_flatpak_env ()) {
var pp = ProcessProvider.get_default ();
var pp_pids = pp.get_pids ();

if (!process_list.has_key (pid) && !kernel_process_blacklist.contains (pid)) {
add_process (pid);
// foreach (var pid in pp_pids) {
// debug ("yeah %d", pid);
// }

debug ("Size %u", pp_pids.length ());

foreach (int pid in pp_pids) {
if (!process_list.has_key (pid) && !kernel_process_blacklist.contains (pid)) {
add_process (pid);
}
}
debug ("process_list size %u", process_list.size);
} else {
// for (int i = 0; i < proclist.number; i++) {
// int pid = pids[i];

// if (!process_list.has_key (pid) && !kernel_process_blacklist.contains (pid)) {
// add_process (pid);
// }
// }
}



cpu_last_used = used;
cpu_last_total = cpu_data.total;
cpu_last_useds = useds;
Expand Down Expand Up @@ -225,7 +245,7 @@ namespace Monitor {
return true;
}
// some processes have semicolon in command
// do not sanitizing to improve readability
// do not sanitize to improve readability
else if (process.command.split (" ")[0].contains (":")) {
process.application_name = process.command;
return true;
Expand Down
109 changes: 109 additions & 0 deletions src/Managers/ProcessProvider.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
namespace Monitor {
public class ProcessProvider {
private static GLib.Once<ProcessProvider> instance;
public static unowned ProcessProvider get_default () {
return instance.once (() => { return new ProcessProvider (); });
}

public Gee.HashSet<int> pids = new Gee.HashSet<int> ();

public ProcessProvider () {
}

public GLib.List<int> get_pids () {
var new_pids = new GLib.List<int> ();

try {
string[] spawn_args = { "flatpak-spawn", "--host", "ls" };
string[] spawn_env = Environ.get ();
Pid child_pid;

int standard_input;
int standard_output;
int standard_error;


string ls_stdout;
string ls_stderr;
int ls_status;

// GLib.Process.spawn_async_with_pipes
// (
// "/proc/",
// spawn_args,
// spawn_env,
// SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
// null,
// out child_pid,
// out standard_input,
// out standard_output,
// out standard_error
// );

GLib.Process.spawn_command_line_sync (
"flatpak-spawn --host ls /proc/",
out ls_stdout,
out ls_stderr,
out ls_status);

// stdout:
// IOChannel output = new IOChannel.unix_new (standard_output);
// output.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => {
// return process_line (channel, condition, new_pids);
// });

//// stderr:
// IOChannel error = new IOChannel.unix_new (standard_error);
// error.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => {
//// return process_line (channel, condition, "stderr");
// return true;
// });

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

// debug (ls_stdout);
foreach (var line in ls_stdout.strip ().split ("\n")) {
if (line[0].isdigit ()) {
// print ("---->" + line + "\n");
new_pids.append (int.parse (line));
}
}


} catch (SpawnError e) {
error ("Error: %s\n", e.message);
}
return new_pids;
}

private bool process_line (IOChannel channel, IOCondition condition, GLib.List<int> _pids) {
if (condition == IOCondition.HUP) {
// debug ("%s: The fd has been closed.\n", stream_name);
return false;
}

try {
string line;
channel.read_line (out line, null, null);
if (line[0].isdigit ()) {
print ("%d\n", int.parse (line));
// pids.add (line.strip ());

}
} catch (IOChannelError e) {
warning ("IOChannelError: %s\n", e.message);
return false;
} catch (ConvertError e) {
warning ("ConvertError: %s\n", e.message);
return false;
}

return true;
}

}
}
Loading

0 comments on commit aa43cef

Please sign in to comment.