launch_daemon: Improved job overwriting.

* Certain things are set on job construction only.
* Now checks if the message has the field before updating it.
* Might not be final yet (it is confusing that 'requires' will add to
  the requirements, but 'launch' will replace all arguments).
This commit is contained in:
Axel Dörfler
2015-07-22 20:43:49 +02:00
parent 4c67f79c2c
commit 1281fcd3cb
3 changed files with 43 additions and 17 deletions
+8 -1
View File
@@ -37,8 +37,15 @@ BaseJob::Condition() const
} }
::Condition*
BaseJob::Condition()
{
return fCondition;
}
void void
BaseJob::SetCondition(const ::Condition* condition) BaseJob::SetCondition(::Condition* condition)
{ {
fCondition = condition; fCondition = condition;
} }
+3 -2
View File
@@ -23,11 +23,12 @@ public:
const char* Name() const; const char* Name() const;
const ::Condition* Condition() const; const ::Condition* Condition() const;
void SetCondition(const ::Condition* condition); ::Condition* Condition();
void SetCondition(::Condition* condition);
bool CheckCondition(ConditionContext& context) const; bool CheckCondition(ConditionContext& context) const;
protected: protected:
const ::Condition* fCondition; ::Condition* fCondition;
}; };
+32 -14
View File
@@ -583,26 +583,39 @@ LaunchDaemon::_AddJob(Target* target, bool service, BMessage& message)
name.ToLower(); name.ToLower();
Job* job = FindJob(name); Job* job = FindJob(name);
if (job == NULL) if (job == NULL) {
job = new Job(name); job = new (std::nothrow) Job(name);
if (job == NULL)
return;
job->SetEnabled(!message.GetBool("disabled", !job->IsEnabled())); job->SetService(service);
job->SetService(service); job->SetCreateDefaultPort(service);
job->SetCreateDefaultPort(!message.GetBool("legacy", !service)); job->SetTarget(target);
job->SetTarget(target); }
if (message.HasBool("disabled"))
job->SetEnabled(!message.GetBool("disabled", !job->IsEnabled()));
if (message.HasBool("legacy"))
job->SetCreateDefaultPort(!message.GetBool("legacy", !service));
_SetCondition(job, message); _SetCondition(job, message);
BMessage portMessage; BMessage portMessage;
for (int32 index = 0; for (int32 index = 0;
message.FindMessage("port", index, &portMessage) == B_OK; index++) { message.FindMessage("port", index, &portMessage) == B_OK; index++) {
job->AddPort(portMessage); job->AddPort(portMessage);
} }
const char* argument; if (message.HasString("launch")) {
for (int32 index = 0; job->Arguments().MakeEmpty();
message.FindString("launch", index, &argument) == B_OK; index++) {
job->AddArgument(argument); const char* argument;
for (int32 index = 0; message.FindString("launch", index, &argument)
== B_OK; index++) {
job->AddArgument(argument);
}
} }
const char* requirement; const char* requirement;
@@ -683,16 +696,21 @@ LaunchDaemon::_AddTarget(Target* target)
void void
LaunchDaemon::_SetCondition(BaseJob* job, const BMessage& message) LaunchDaemon::_SetCondition(BaseJob* job, const BMessage& message)
{ {
Condition* condition = NULL; Condition* condition = job->Condition();
bool updated = false;
BMessage conditions; BMessage conditions;
if (message.FindMessage("if", &conditions) == B_OK) if (message.FindMessage("if", &conditions) == B_OK) {
condition = Conditions::FromMessage(conditions); condition = Conditions::FromMessage(conditions);
updated = true;
}
if (message.GetBool("no_safemode")) if (message.GetBool("no_safemode")) {
condition = Conditions::AddNotSafeMode(condition); condition = Conditions::AddNotSafeMode(condition);
updated = true;
}
if (condition != NULL) if (updated)
job->SetCondition(condition); job->SetCondition(condition);
} }