[−][src]Module sunrise_libuser::argv
Argument handling
When starting a process, Loader will put the program's argument after the first ELF/NSO it loaded. This would usually be rtld for a "normal" program. RTLD will then pass a pointer to the program's main function. The program's arguments are passed completely unparsed. They're literally just a string stored in memory. To take into account the need for parsing, loader will allocate twice as much memory and then some, which should allow the program to copy the strings - adding a \0 when necessary - and then to create the argv pointer array.
The memory region allocated by the Loader will be used like this:
+------------------------+ < Always page-aligned.
| ProgramArguments |
| u32 allocated_size |
| u32 arguments_size |
+------------------------+
| 0x18 Reserved bytes |
+------------------------+
| Raw CmdLine |
| arguments_size bytes |
+------------------------+
| Argument Storage |
| arguments_size bytes |
+------------------------+
| Alignment bytes. Force |
| align to size_of(usize)|
+------------------------+
| System Argv |
| Array of pointers to |
| Argument Storage |
+------------------------+ < allocated_size
SunriseOS Extension: Envp
Passing environment variables to a subprogram is necessary for proper execution of the SunriseOS userspace. For instance, it is necessary to pass the current working directory to the subprocess so that executing the following script executes the ls in the right directory.
cd /etc
uutils ls
To achieve this, the same mechanism to pass arguments is used to pass
environment variables. The environment variables are passed as a string,
after the cmdline, as a \0
-separated list of VAR_NAME=VAR_VALUE. A
variable name may not have an = or a \0 in its name.
To separate the cmdline from the environment variables, a \0
is used. As
such, the region allocated by Loader will look like this:
+------------------------+ < Always page-aligned.
| ProgramArguments |
| u32 allocated_size |
| u32 arguments_size |
+------------------------+
| 0x18 Reserved bytes |
+------------------------+
| Raw CmdLine |
| arguments_size bytes |
| |
| +--------------------+ |
| | Arguments | |
| +--------------------+ |
| | \0 | |
| +--------------------+ |
| | Environment vars | |
| +--------------------+ |
| |
+------------------------+
| Argument Storage |
| arguments_size bytes |
+------------------------+
| Alignment bytes. Force |
| align to size_of(usize)|
+------------------------+
| System Argv |
| Array of pointers to |
| Argument Storage |
+------------------------+
| System Envp |
| Array of pointers to |
| environment vars |
+------------------------+ < allocated_size
Functions
__libuser_get_args | Get the arguments and environment. This will parse and setup the arguments and environment the first time it is called - modifying the argdata section in the process. This function is safe to call from multiple threads |
argc | Get the number of arguments in argv. |
argv | Get the argument array. It is guaranteed to have at least |
envp | Get the environ array. It is guaranteed to end with a NULL element. |