So I’m writing a package manager that is approaching initial release. Consider the following flow of code:
# Some code that needs root privalages. Database syncing, etc.
...
...
# Building packages. *Does not require root.*
# In fact, this is a call to a child process through the shell and it will complain if called by root.
...
...
# Copying built packages to root-owned locations. Needs root.
# Installing packages. Needs root.
I’d like to be able to run the program with sudo initially, as the copying part above is done in native code and will fail if the entire program wasn’t run with root privileges to begin with. The building process however needs to not be done as root. My question is in essence, would there be a way “deny” or “halt” the root privileges before calling the child process that does the building? It would need to be in such a way that when control returns to the parent the root-state would still be in effect for the actual installation to take place unhindered.
Please and thanks in advance.
Also, here is the repo is anyone is interested.
So it seems that when a program is run with sudo, the environment variable SUDO_USER is created. By reading that, I can then run the child process with
su -c.