I'm a bit confused by the accesses permissions of a shared library constructor. I understand the constructor is run only once when the library is loaded. My question is one of permissions. If I create a design such that the executable that first loads the library has elevated permissions I assume it can then share those results with that other executable without elevated permissions that simply link to the library. Is this correct?
For instance, suppose in the constructor we read a file and set up a hash table using the values from that file. The file permissions are such that only root can read the file. Once the file is read other programs not running as root that need the values can get them from the hash table.
First executable that loads the shared library runs as root and loads the values.
All other programs that link to the shared library after it is initially loaded get access to the values even if they don't run as root. They get access to the data simply because they are using the shared library in which the initial values are already loaded.
Is this correct?
// pseudo code
// This is called on library load.
static void foo_constructor( ) __attribute__( ( constructor ) );
void foo_constructor( )
{
// Read in file with root only permissions and create map table
}
// This can be called by anyone using the shared library
std::map<std::string, std::string>& Foo:getMap()
{
// Return map
}