BLPreserveBootArgs.c [plain text]
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include "bless.h"
#include "bless_private.h"
static const char *remove_boot_args[] = {
"rd",
"rp",
"boot-uuid",
NULL
};
int BLPreserveBootArgs(BLContextPtr context,
const char *input,
char *output,
int outputLen)
{
char oldbootargs[1024];
char bootargs[1024];
size_t bootleft=sizeof(bootargs)-1;
char *token, *restargs;
int firstarg=1;
strncpy(oldbootargs, input, sizeof(oldbootargs)-1);
oldbootargs[sizeof(oldbootargs)-1] = '\0';
memset(bootargs, 0, sizeof(bootargs));
contextprintf(context, kBLLogLevelVerbose, "Old boot-args: %s\n", oldbootargs);
restargs = oldbootargs;
while((token = strsep(&restargs, " ")) != NULL) {
int shouldbesaved = 1, i;
contextprintf(context, kBLLogLevelVerbose, "\tGot token: %s\n", token);
for(i=0; remove_boot_args[i]; i++) {
if(remove_boot_args[i][0] == '-') {
if(strcmp(remove_boot_args[i], token) == 0) {
shouldbesaved = 0;
break;
}
} else {
int keylen = strlen(remove_boot_args[i]);
if(strlen(token) >= keylen+1
&& strncmp(remove_boot_args[i], token, keylen) == 0
&& token[keylen] == '=') {
shouldbesaved = 0;
break;
}
}
}
if(shouldbesaved) {
if(firstarg) {
firstarg = 0;
} else {
strncat(bootargs, " ", bootleft);
bootleft--;
}
contextprintf(context, kBLLogLevelVerbose, "\tPreserving: %s\n", token);
strncat(bootargs, token, bootleft);
bootleft -= strlen(token);
}
}
strlcpy(output, bootargs, outputLen);
return 0;
}