launch_daemon: launching on demand now actually works.

* Was broken in two ways: if only the shortcut "on_demand" was used,
  the event didn't get created at all due to a bug in
  Events::AddOnDemand().
* Furthermore, _LaunchJob() always triggered a demand, but it should
  only do this when not called from a target.
This commit is contained in:
Axel Dörfler 2015-10-13 16:26:13 +02:00
parent 74b10f8e82
commit e048384d77
3 changed files with 24 additions and 14 deletions

View File

@ -474,15 +474,15 @@ Events::FromMessage(const BMessenger& target, const BMessage& message)
/*static*/ Event*
Events::AddOnDemand(Event* event)
Events::AddOnDemand(const BMessenger& target, Event* event)
{
OrEvent* orEvent = dynamic_cast<OrEvent*>(event);
if (orEvent == NULL) {
EventContainer* container = dynamic_cast<EventContainer*>(event);
if (container == NULL)
return NULL;
orEvent = new OrEvent(container->Owner(), container->Target());
if (container != NULL)
orEvent = new OrEvent(container->Owner(), container->Target());
else
orEvent = new OrEvent(NULL, target);
}
if (orEvent != event && event != NULL)
orEvent->AddEvent(event);

View File

@ -52,7 +52,7 @@ class Events {
public:
static Event* FromMessage(const BMessenger& target,
const BMessage& message);
static Event* AddOnDemand(Event* event);
static Event* AddOnDemand(const BMessenger& target, Event* event);
static bool ResolveRegisteredEvent(Event* event,
const char* name);
static void TriggerRegisteredEvent(Event* event,

View File

@ -53,6 +53,12 @@ static const char* kLaunchDirectory = "launch";
static const char* kUserLaunchDirectory = "user_launch";
enum launch_options {
FORCE_NOW = 0x01,
TRIGGER_DEMAND = 0x02
};
class Session {
public:
Session(uid_t user, const BMessenger& target);
@ -138,7 +144,7 @@ private:
void _InitJobs(Target* target);
void _LaunchJobs(Target* target,
bool forceNow = false);
void _LaunchJob(Job* job, bool forceNow = false);
void _LaunchJob(Job* job, uint32 options = 0);
void _AddTarget(Target* target);
void _SetCondition(BaseJob* job,
const BMessage& message);
@ -490,7 +496,7 @@ LaunchDaemon::_HandleGetLaunchData(BMessage* message)
if (reply.what == B_OK) {
// Launch the job if it hasn't been launched already
if (launchJob)
_LaunchJob(job);
_LaunchJob(job, TRIGGER_DEMAND);
DetachCurrentMessage();
status_t result = job->HandleGetLaunchData(message);
@ -1021,19 +1027,23 @@ LaunchDaemon::_LaunchJobs(Target* target, bool forceNow)
Calling this method will trigger a demand event.
*/
void
LaunchDaemon::_LaunchJob(Job* job, bool forceNow)
LaunchDaemon::_LaunchJob(Job* job, uint32 options)
{
if (job == NULL || job->IsLaunched() || (!forceNow
if (job == NULL || job->IsLaunched() || ((options & FORCE_NOW) == 0
&& (!job->EventHasTriggered() || !job->CheckCondition(*this)
|| Events::TriggerDemand(job->Event())))) {
|| ((options & TRIGGER_DEMAND) != 0
&& Events::TriggerDemand(job->Event()))))) {
return;
}
int32 count = job->Requirements().CountStrings();
for (int32 index = 0; index < count; index++) {
Job* requirement = FindJob(job->Requirements().StringAt(index));
if (requirement != NULL)
_LaunchJob(requirement);
if (requirement != NULL) {
// TODO: For jobs that have their communication channels set up,
// we would not need to trigger demand at this point
_LaunchJob(requirement, TRIGGER_DEMAND);
}
}
if (job->Target() != NULL)
@ -1087,7 +1097,7 @@ LaunchDaemon::_SetEvent(BaseJob* job, const BMessage& message)
}
if (message.GetBool("on_demand")) {
event = Events::AddOnDemand(event);
event = Events::AddOnDemand(this, event);
updated = true;
}