13.3 Separation of Command-Line Options and Environment Variables
The preceding section introduced the parameter parsing function parse_args(). After evaluating the early command-line parameters, the start_kernel() function invokes parse_args() once again, utilizing the unknown_bootoption() function to handle unknown options encountered during the boot sequence. In this secondary execution path, start_kernel() invokes parse_args() in the following manner:
after_dashes = parse_args("Booting kernel", static_command_line, __start___param, __stop___param - __start___param, -1, -1, NULL, &unknown_bootoption)
The descriptive identity tracking string designated for log diagnostics is "Booting kernel". The pointer tracking the target string to be parsed is static_command_line, which encapsulates the combined contents of extra_command_line and command_line, wherein the contents of command_line append directly to the end of extra_command_line. The registry of valid options that this routine is authorized to handle is specified by the pointer __start___param, and the total count of these listed variables evaluates to __stop___param - __start___param. Because the maximum and minimum initcall levels passed to the routine are both specified as -1, all incoming parameters are directly routed to the unknown_bootoption() callback function for custom parsing.
The unknown_bootoption() function is defined within the source file git/init/main.c, and its definition is implemented as follows:
static int __init unknown_bootoption(char *param, char *val, const char *unused, void *arg)
{
size_t len = strlen(param);
repair_env_string(param, val);
if (obsolete_checksetup(param))
return 0;
if (strnchr(param, len, '.'))
return 0;
if (panic_later)
return 0;
if (val) {
unsigned int i;
for (i = 0; envp_init[i]; i++) {
if (i == MAX_INIT_ENVS) {
panic_later = "env";
panic_param = param;
}
if (!strncmp(param, envp_init[i], len+1))
break;
}
envp_init[i] = param;
} else {
unsigned int i;
for (i = 0; argv_init[i]; i++) {
if (i == MAX_INIT_ARGS) {
panic_later = "init";
panic_param = param;
}
}
argv_init[i] = param;
}
return 0;
}
As established from previous descriptions, the helper routine next_arg() replaces the literal structural characters = and " within arguments formatted as param=val and param="val" with null characters ('\0'). To restore this data, unknown_bootoption() first invokes the function repair_env_string(param, val) to uniformly reconstitute the target tokens back into the explicit param=val schema. If the original format matched the param="val" schema, repair_env_string() also strips out the extraneous embedded null markers.
Following this repair pass, it invokes obsolete_checksetup() to ensure that the processed command-line argument does not rely on an outdated format specification. Once the parameter syntax format is validated to satisfy all layout constraints, unknown_bootoption() begins separating the parameters from their matching values. If the parameter value field val evaluates to a non-null pointer—meaning the parameter satisfies the explicit param=val schema—this argument is classified as an environment variable. Under this scenario, if the environment variable tracking array envp_init possesses available slots and the current parameter has not yet been registered, the parameter string is saved directly into envp_init.
Conversely, if the parameter lacks an associated value assignment—meaning it follows the raw param schema—it is classified as a command-line option. Similarly, if the command-line option tracking array argv_init contains unallocated slots and the argument does not yet exist within that repository, the param pointer is saved directly into argv_init. If the routing logic fails to discover a valid unallocated index slot inside either envp_init or argv_init, the unknown_bootoption routine populates the tracking variables panic_later and panic_param to trigger a kernel panic fault at a later execution phase.
Upon concluding this variable separation workflow, if an execution fault occurs, parse_args() returns the memory address of the matching exception handling handler. If the command line payload is processed successfully without errors, the function returns the exact memory address pointing immediately after the double-hyphen delimiter -- inside the static_command_line string. Therefore, provided that the command-line arguments conform to the required syntax and the arrays envp_init and argv_init possess sufficient storage space, the after_dashes pointer tracks the string sequence placed after the -- marker, meaning it maps directly to the explicit variables forwarded to the user-space init() process. The structural workflow of separating command options from environment variables is illustrated in Figure 13-2.
Figure 13-2 Workflow for Separating Command Options and Environment Variables