Jump to content




Identifying API caller's environment

api computer lua

3 replies to this topic

#1 GreenByteSoftware

  • Members
  • 135 posts
  • LocationKaunas, Lithuania

Posted 26 March 2017 - 08: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 by GreenByteSoftware, 26 March 2017 - 08:38 AM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,459 posts

Posted 26 March 2017 - 02: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.

#3 GreenByteSoftware

  • Members
  • 135 posts
  • LocationKaunas, Lithuania

Posted 26 March 2017 - 05:00 PM

View PostLyqyd, on 26 March 2017 - 02:54 PM, said:

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 by GreenByteSoftware, 26 March 2017 - 05:01 PM.


#4 Exerro

  • Members
  • 801 posts

Posted 29 March 2017 - 11:19 AM

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users