Can octave determine itself if it is running inside the 'flatpak sandbox'?

Can octave determine itself if it is running inside the ‘flatpak sandbox’?
I try find a method how it can determine it, but couldn’t get it done:
octave_in_flatpak.m (336 Bytes)
Then if it is possible I could make it determine itself its required code depending on the case:

  if (inside_sandbox),
    [s,msg]=system("flatpak-spawn --host sensors");
  else
    [s,msg]=system("sensors");
  end;

What does [s,msg]=system("flatpak-spawn --host sensors"); return when you are not inside a sandbox?

Maybe you could use that result in a helper function.
Might look similar to this:

function ret = is_flatpak()

persistent flatpak = ~system ("flatpak-spawn --host ls > 0")

ret = flatpak;

endfunction

With the persistent variable the system call should be happening only once per Octave session.

I think the simplest method is to check for existence of the file /.flatpak-info.

if (exist ('/.flatpak-info') == 2)
  disp ('Octave is running in a Flatpak sandbox');
else
  disp ('Octave is not running in a Flatpak sandbox');
endif
1 Like

This seem to work in current case…
Also if octave also had itself command to check about it.
Is this some sandbox hidden files, cause I can not find it otherwise…?
But in case I have flatpak, but octave installed from snap/apt this might fail.

If I combine the both, it becomes:

# Determine whether octave is running inside the sandbox or without it...
ocinsb=false;
if (exist('/.flatpak-info')==2),
  ocinsb=~system ("flatpak-spawn --host ls > 0");
end;

But I have no idea what the ls-command is doing here…

I try different code:
octave_in_fsb.m (461 Bytes)

Also there might not be the ‘flatpak-spawn’ available if the octave is from ‘apt’ or ‘snap’
and system has flatpak for some other apps.

It looks octave has a lot of function, but it can not use the system/CPU/HDD/Device temperature sensors.

Maybe I just had found it working method accidentally…

# Determine whether octave is running inside the sandbox or without it...
ocinsb=false;
[s,msg]=system("which flatpak-spawn 2>&-");
if (length(msg)>0),
  ocinsb=true;
end;

Isn’t that the point of this entire exercise? A test that passes inside Flatpak and fails outside?

Spawning a shell to run which flatpak-spawn certainly works, but I think it’s overkill, and it’s far less efficient. Using exist to check for the existence of a known file is orders of magnitude faster.

Use either exist ('/.flatpak-info') == 2 or exist ('/usr/bin/flatpak-spawn') == 2 if you prefer. Both are much simpler and more efficient than system ("which flatpak-spawn").

1 Like

Another unique identifier is to test for the existence of FLATPAK_ID in the process environment.

if (isempty (getenv ('FLATPAK_ID')))
  …
endif
2 Likes

This could be useful also.