This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GreenByteSoftware's profile picture

Identifying API caller's environment

Started by GreenByteSoftware, 26 March 2017 - 07:36 AM
GreenByteSoftware #1
Posted 26 March 2017 - 09:36 AM
TL;DR - how to make APIs identify the caller environment and act differently based on it.

Hello,
is there any way to identify the program started with os.run? I am trying to make it so that every program runs on a specific UID and has different levels of file/api access. The best way would be so that no craftOS API is broken. I already am able to replace the os.run API so I can modify it.

My current code is as follows:

function os.run( _tEnv, _sPath, ... )
  local tEnv = _tEnv
  lastid = lastid+1
  local pid = lastid
  ptable[pid] = {_sPath, uid}

  ret = _osrun(tEnv, _sPath, ...)

  ptable[pid] = nil
  return ret
end
Where _osrun is the native os.run and a ptable and lastid are local variables.

So, I have a process table, now the problem is that how do I get a parent uid/pid in any API (including os.run)? I could store something like a function or a table so that if a variable in program's environment is altered, then it would make calls as a nobody user.
Edited on 26 March 2017 - 07:38 AM
Lyqyd #2
Posted 26 March 2017 - 03:54 PM
In LyqydOS, there's a process API, and one field of that API is process.active. The PID of the active process is kept in that field, so if you utilized a similar setup, all you'd have to do is check that field from your other APIs to know which process is starting the new one. It could also be set up as a getter that fetches a local variable, to prevent other processes from writing to the value.
GreenByteSoftware #3
Posted 26 March 2017 - 06:00 PM
In LyqydOS, there's a process API, and one field of that API is process.active. The PID of the active process is kept in that field, so if you utilized a similar setup, all you'd have to do is check that field from your other APIs to know which process is starting the new one. It could also be set up as a getter that fetches a local variable, to prevent other processes from writing to the value.
Perfect! I set up a local table's table from which a public process table is set every time a coroutine is resumed and this way I can know who is calling anything.
Edited on 26 March 2017 - 04:01 PM
Exerro #4
Posted 29 March 2017 - 12:19 PM
Lyqyd's suggestion is a better solution to this problem imo, but nonetheless, if you want to get a caller's environment (provided this is still Lua 5.1) you can use getfenv(2).

Spoiler