Skip to content

Commit

Permalink
Add pseudo dependency support by allowing us to say ObjectStartPriori…
Browse files Browse the repository at this point in the history
…ty=udev+1 etc
  • Loading branch information
Subsentient committed Jun 14, 2015
1 parent 7d54fa8 commit 1981670
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
85 changes: 83 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static void PriorityAlias_Shutdown(void);
static void RLInheritance_Add(const char *Inheriter, const char *Inherited);
static Bool RLInheritance_Check(const char *Inheriter, const char *Inherited);
static void RLInheritance_Shutdown(void);
static unsigned PriorityOfLookup(const char *const ObjectID, Bool IsStartingMode);

/*Used for error handling in InitConfig() by ConfigProblem(CurConfigFile, ).*/
enum { CONFIG_EMISSINGVAL = 1, CONFIG_EBADVAL, CONFIG_ETRUNCATED, CONFIG_EAFTER,
Expand Down Expand Up @@ -494,6 +495,12 @@ ReturnCode InitConfig(const char *CurConfigFile)
}
Alias[TInc] = '\0';

if (!ValidIdentifierName(Alias))
{ //We have to check if this contains odd characters we might want to do something with in config ourselves, and disallow it.
ConfigProblem(CurConfigFile, CONFIG_EBADVAL, CurrentAttribute, Alias, LineNum);
continue;
}

if (*TWorker == '\0')
{
ConfigProblem(CurConfigFile, CONFIG_EBADVAL, CurrentAttribute, DelimCurr, LineNum);
Expand Down Expand Up @@ -1075,6 +1082,17 @@ ReturnCode InitConfig(const char *CurConfigFile)
*Temp = '\0';
}

if (!ValidIdentifierName(DelimCurr))
{ //We have to check if this contains odd characters we might want to do something with in config ourselves, and disallow it.
snprintf(DelimCurr, sizeof DelimCurr, "object%u%u%u_badid", rand(), rand(), rand()); //Generate a new ID since this one is bad.

//Now alert them.
snprintf(ErrBuf, sizeof ErrBuf, CONFIGWARNTXT "ObjectID contains invalid character. Generating new ID: \"%s\".", DelimCurr);
SpitWarning(ErrBuf);
WriteLogLine(ErrBuf, true);
}


DelimCurr[MAX_DESCRIPT_SIZE - 1] = '\0'; /*Chop it off to prevent overflow.*/

if (!(CurObj = AddObjectToTable(DelimCurr, CurConfigFile))) /*Sets this as our current object.*/
Expand Down Expand Up @@ -1680,13 +1698,34 @@ ReturnCode InitConfig(const char *CurConfigFile)
if (!AllNumeric(DelimCurr)) /*Make sure we are getting a number, not Shakespeare.*/
{ /*No number? We're probably looking at an alias.*/
unsigned TmpTarget = 0;
signed int Change = 0;
Bool PositiveChange = false;

char *Lookup = strpbrk(DelimCurr, "-+");
if (Lookup != NULL && AllNumeric(Lookup + 1))
{ //They provided an increment or decrement.
PositiveChange = *Lookup == '+';
*Lookup = '\0';
Change = atoi(Lookup + 1);
}

if (!(TmpTarget = PriorityAlias_Lookup(DelimCurr)))
if (!(TmpTarget = PriorityAlias_Lookup(DelimCurr)) && !(TmpTarget = PriorityOfLookup(DelimCurr, true)))
{
ConfigProblem(CurConfigFile, CONFIG_EBADVAL, CurrentAttribute, DelimCurr, LineNum);
continue;
}

if (Change)
{ //They incremented or decremented.
if (PositiveChange)
{
TmpTarget += Change;
}
else
{
TmpTarget -= Change;
}
}
CurObj->ObjectStartPriority = TmpTarget;
continue;
}
Expand Down Expand Up @@ -1718,13 +1757,36 @@ ReturnCode InitConfig(const char *CurConfigFile)
if (!AllNumeric(DelimCurr))
{
unsigned TmpTarget = 0;
signed int Change = 0;
Bool PositiveChange = false;

if (!(TmpTarget = PriorityAlias_Lookup(DelimCurr)))
char *Lookup = strpbrk(DelimCurr, "-+");
if (Lookup != NULL && AllNumeric(Lookup + 1))
{ //They provided an increment or decrement.
PositiveChange = *Lookup == '+';
*Lookup = '\0';
Change = atoi(Lookup + 1);
}


if (!(TmpTarget = PriorityAlias_Lookup(DelimCurr)) && !(TmpTarget = PriorityOfLookup(DelimCurr, false)))
{
ConfigProblem(CurConfigFile, CONFIG_EBADVAL, CurrentAttribute, DelimCurr, LineNum);
continue;
}

if (Change)
{ //They incremented or decremented.
if (PositiveChange)
{
TmpTarget += Change;
}
else
{
TmpTarget -= Change;
}
}

CurObj->ObjectStopPriority = TmpTarget;
continue;
}
Expand Down Expand Up @@ -2636,6 +2698,25 @@ ObjTable *LookupObjectInTable(const char *ObjectID)
return NULL;
}

static unsigned PriorityOfLookup(const char *const ObjectID, Bool IsStartingMode)
{
ObjTable *Worker = ObjectTable;

if (!ObjectTable) return 0;

for (; Worker->Next; Worker = Worker->Next)
{
if (!strcmp(ObjectID, Worker->ObjectID))
{
return IsStartingMode ? Worker->ObjectStartPriority : Worker->ObjectStopPriority;
}
}

return 0;
}



/*Get the max priority number we need to scan.*/
unsigned GetHighestPriority(Bool WantStartPriority)
{
Expand Down
1 change: 1 addition & 0 deletions src/epoch.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ extern unsigned ReadPIDFile(const ObjTable *InObj);
extern ReturnCode WriteLogLine(const char *InStream, Bool AddDate);
extern unsigned AdvancedPIDFind(ObjTable *InObj, Bool UpdatePID);
extern Bool ProcAvailable(void);
extern Bool ValidIdentifierName(const char *const Identifier);

/*main.c*/
extern Bool KCmdLineObjCmd_Check(const char *ObjectID, Bool StartMode);
Expand Down
14 changes: 14 additions & 0 deletions src/utilfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,17 @@ Bool ProcAvailable(void)

return !stat("/proc/cmdline", &FileStat);
}

Bool ValidIdentifierName(const char *const Identifier)
{
const char *Worker = Identifier;

for (; *Worker; ++Worker)
{
if (!isalnum(*Worker) && *Worker != '_') return false;
}

return true;
}


0 comments on commit 1981670

Please sign in to comment.