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

Tips and snipplets for Operating System creators!

Started by gamax92, 01 April 2013 - 06:15 AM
gamax92 #1
Posted 01 April 2013 - 08:15 AM
This will go along with my "What is an Operating System" post, and here will feature various tips and pieces of code that can help out operating system writers.

Self file protection
One of the things many people want is the Operating System to protect itself. There are many ways to do so, but for simplicity, I'll post a simple way and an advanced way.
  • The simple way
  • The simple way is to just use fs.open() on all the files that you want to protect, and then don't close them. When the computer reboots, the handles will be closed so not to worry. It would still be advised to close them before rebooting or shutting down. Example
    -- We want to make sure that /myos/important and /myos/alsoimportant cannot be deleted.
    local protected = {}
    table.insert(protected,fs.open("/myos/important","r"))
    table.insert(protected,fs.open("/myos/alsoimportant","r"))
    In this method, by putting the handles in the table, we can still pull them out and close them if needed. This method isn't 100% secure and still allows you to edit the file. I'm also getting reports that its not working for them.
  • The advanced way
  • The advanced way would be to hook onto the fs api and make sure the user cannot use the api on any of the protected files. This is a bit complicated and rather differs by OS, but here is a concept.
    
    local protected = {}
    protected["myos/important"] = true
    protected["myos/alsoimportant"] = true
    
    local _fs_delete = fs.delete -- Backup the delete function
    fs.delete = function(path)
    	if protected[shell.resolve(path)] ~= true then
    			_fs_delete(path)
    	else
    		error("Access Denied", 2)
    	end
    end
Crash handlers
If your Operating System crashes for any reason, its important to make it not drop to shell. In the shell the user can roam free and do whatever they want to in your os.
It is also nice to have something to help the user or give a message as to why it crashed.
The way we do this is to wrap the Operating System code or files in a pcall.
error, message = pcall(shell.run,"/myos/bootup")
if error == false then -- It crashed!
	print("MyOS has CRASHED! :(/>\n\n" .. message)
	while true do sleep(0.05) end
end

Recommendation from Cloudy
If your OS is useless and not deserving of the name, then you should add this to your code.
error("Custom OS's are mostly useless and not deserving of the name")

Tutorials
If you are looking for some tutorials, please check out ComputerCraft's tutorials forum.
Tutorials
MudkipTheEpic #2
Posted 01 April 2013 - 04:40 PM
This will go along with my "What is an Operating System" post, and here will feature various tips and pieces of code that can help out operating system writers.

Self file protection
One of the things many people want is the Operating System to protect itself. There are many ways to do so, but for simplicity, I'll post a simple way and an advanced way.
  • The simple way
  • The simple way is to just use fs.open() on all the files that you want to protect, and then don't close them. When the computer reboots, the handles will be closed so not to worry. It would still be advised to close them before rebooting or shutting down. Example
    -- We want to make sure that /myos/important and /myos/alsoimportant cannot be deleted.
    local protected = {}
    table.insert(protected,fs.open("/myos/important","r"))
    table.insert(protected,fs.open("/myos/alsoimportant","r"))
    In this method, by putting the handles in the table, we can still pull them out and close them if needed.
  • The advanced way
  • The advanced way would be to hook onto the fs api and make sure the user cannot use the api on any of the protected files. This is a bit complicated and rather differs by OS, so not gonna post it.
Crash handlers
If your Operating System crashes for any reason, its important to make it not drop to shell. In the shell the user can roam free and do whatever they want to in your os.
It is also nice to have something to help the user or give a message as to why it crashed.
The way we do this is to wrap the Operating System code or files in a pcall.
error, message = pcall(dofile,"/myos/bootup")
if error == false then -- It crashed!
	print("MyOS has CRASHED! :(/>/>/>/>/>\n\n" .. message)
	while true do sleep(0.05) end
end


Umm.. when I do that I can still delete, copy, move, edit the files all I want…
theoriginalbit #3
Posted 01 April 2013 - 04:56 PM
Crash handlers
If your Operating System crashes for any reason, its important to make it not drop to shell. In the shell the user can roam free and do whatever they want to in your os.
It is also nice to have something to help the user or give a message as to why it crashed.
The way we do this is to wrap the Operating System code or files in a pcall.
error, message = pcall(dofile,"/myos/bootup")
if error == false then -- It crashed!
    print("MyOS has CRASHED!\n\n" .. message)
    while true do sleep(0.05) end
end
Well actually this code has problems too…. sleep can still be terminated and they can get back to the shell from that… you would want to reboot the computer after they have say pressed a key or clicked… also to be nice and easy, instead of a dofile… just protect the very first function that is called when the computer boots up… or you can even protect a shell.run if you really want to

local ok, err = pcall( shell.run, "someProgram")
and then you can handle the error from there… the best thing about protecting the top level (first called) function is all other functions below it are protected too so if one of those error (unless you handle it somewhere) it will go back to that very first pcall… for more information, have a read of my BSoD tutorial here
gamax92 #4
Posted 02 April 2013 - 01:34 PM
Stuff here …


Umm.. when I do that I can still delete, copy, move, edit the files all I want…

I tested it myself on CC-EMU and Minecraft, it prevents you from deleting and moving it. Also, why would it prevent you from copying it?
MudkipTheEpic #5
Posted 02 April 2013 - 01:39 PM
Uhh… I'll put footage on pastebin. (Thank gosh lightshot)

Edit: All right, link:

pastebin get KZeS6BjL testvideo
gamax92 #6
Posted 02 April 2013 - 02:01 PM
pastebin get 88twXr35 testvideo

But okay, I'll add a recommendation in the tips to not use that.
Cloudy #7
Posted 02 April 2013 - 02:37 PM
error("Custom OS's are mostly useless and not deserving of the name")

This snippet should be in all OS's.
Sammich Lord #8
Posted 02 April 2013 - 02:38 PM
error("Custom OS's are mostly useless and not deserving of the name")

This snippet should be in all OS's.
I did not expect you to say that :P/>
bbqroast #9
Posted 05 April 2013 - 12:54 PM
error("Custom OS's are mostly useless and not deserving of the name")

This snippet should be in all OS's.
^This. 10/12 programs posted here are "OSes" can't something cool happen?