Skip to content

Commit

Permalink
launch_daemon: Added basic ability to stop/start jobs via API.
Browse files Browse the repository at this point in the history
* Stopping a job is very simplistic right now, and will have to be
  extended considerably, probably with its own job.
  • Loading branch information
axeld committed Nov 7, 2015
1 parent de0e15a commit 5860caa
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 58 deletions.
2 changes: 2 additions & 0 deletions headers/private/app/LaunchDaemonDefs.h
Expand Up @@ -26,6 +26,8 @@ namespace BPrivate {
enum {
B_GET_LAUNCH_DATA = 'lnda',
B_LAUNCH_TARGET = 'lntg',
B_LAUNCH_JOB = 'lnjo',
B_STOP_LAUNCH_JOB = 'lnsj',
B_LAUNCH_SESSION = 'lnse',
B_REGISTER_SESSION_DAEMON = 'lnrs',
B_REGISTER_LAUNCH_EVENT = 'lnre',
Expand Down
6 changes: 6 additions & 0 deletions headers/private/app/LaunchRoster.h
Expand Up @@ -34,6 +34,9 @@ class BLaunchRoster {
const BMessage* data = NULL,
const char* baseName = NULL);

status_t Start(const char* name);
status_t Stop(const char* name, bool force = false);

status_t StartSession(const char* login);

status_t RegisterEvent(const BMessenger& source,
Expand All @@ -56,6 +59,9 @@ class BLaunchRoster {
friend class Private;

void _InitMessenger();
status_t _SendRequest(BMessage& request);
status_t _SendRequest(BMessage& request,
BMessage& reply);
status_t _UpdateEvent(uint32 what,
const BMessenger& source, const char* name,
uint32 flags = 0);
Expand Down
113 changes: 60 additions & 53 deletions src/kits/app/LaunchRoster.cpp
Expand Up @@ -102,14 +102,7 @@ BLaunchRoster::GetData(const char* signature, BMessage& data)
if (status != B_OK)
return status;

// send the request
status = fMessenger.SendMessage(&request, &data);

// evaluate the reply
if (status == B_OK)
status = data.what;

return status;
return _SendRequest(request, data);
}


Expand Down Expand Up @@ -169,15 +162,43 @@ BLaunchRoster::Target(const char* name, const BMessage* data,
if (status != B_OK)
return status;

// send the request
BMessage result;
status = fMessenger.SendMessage(&request, &result);
return _SendRequest(request);
}

// evaluate the reply

status_t
BLaunchRoster::Start(const char* name)
{
if (name == NULL)
return B_BAD_VALUE;

BMessage request(B_LAUNCH_JOB);
status_t status = request.AddInt32("user", getuid());
if (status == B_OK)
status = result.what;
status = request.AddString("name", name);
if (status != B_OK)
return status;

return status;
return _SendRequest(request);
}


status_t
BLaunchRoster::Stop(const char* name, bool force)
{
if (name == NULL)
return B_BAD_VALUE;

BMessage request(B_STOP_LAUNCH_JOB);
status_t status = request.AddInt32("user", getuid());
if (status == B_OK)
status = request.AddString("name", name);
if (status == B_OK)
status = request.AddBool("force", force);
if (status != B_OK)
return status;

return _SendRequest(request);
}


Expand All @@ -194,15 +215,7 @@ BLaunchRoster::StartSession(const char* login)
if (status != B_OK)
return status;

// send the request
BMessage result;
status = fMessenger.SendMessage(&request, &result);

// evaluate the reply
if (status == B_OK)
status = result.what;

return status;
return _SendRequest(request);
}


Expand Down Expand Up @@ -245,12 +258,7 @@ BLaunchRoster::GetTargets(BStringList& targets)

// send the request
BMessage result;
status = fMessenger.SendMessage(&request, &result);

// evaluate the reply
if (status == B_OK)
status = result.what;

status = _SendRequest(request, result);
if (status == B_OK)
status = result.FindStrings("target", &targets);

Expand All @@ -277,12 +285,7 @@ BLaunchRoster::GetJobs(const char* target, BStringList& jobs)

// send the request
BMessage result;
status = fMessenger.SendMessage(&request, &result);

// evaluate the reply
if (status == B_OK)
status = result.what;

status = _SendRequest(request, result);
if (status == B_OK)
status = result.FindStrings("job", &jobs);

Expand Down Expand Up @@ -310,6 +313,26 @@ BLaunchRoster::_InitMessenger()
}


status_t
BLaunchRoster::_SendRequest(BMessage& request)
{
BMessage result;
return _SendRequest(request, result);
}


status_t
BLaunchRoster::_SendRequest(BMessage& request, BMessage& result)
{
// Send the request, and evaluate the reply
status_t status = fMessenger.SendMessage(&request, &result);
if (status == B_OK)
status = result.what;

return status;
}


status_t
BLaunchRoster::_UpdateEvent(uint32 what, const BMessenger& source,
const char* name, uint32 flags)
Expand All @@ -330,15 +353,7 @@ BLaunchRoster::_UpdateEvent(uint32 what, const BMessenger& source,
if (status != B_OK)
return status;

// send the request
BMessage result;
status = fMessenger.SendMessage(&request, &result);

// evaluate the reply
if (status == B_OK)
status = result.what;

return status;
return _SendRequest(request);
}


Expand All @@ -355,13 +370,5 @@ BLaunchRoster::_GetInfo(uint32 what, const char* name, BMessage& info)
if (status != B_OK)
return status;

// send the request
BMessage result;
status = fMessenger.SendMessage(&request, &info);

// evaluate the reply
if (status == B_OK)
status = result.what;

return status;
return _SendRequest(request, info);
}
28 changes: 27 additions & 1 deletion src/servers/launch/Job.cpp
Expand Up @@ -13,6 +13,7 @@
#include <Message.h>
#include <Roster.h>

#include <MessagePrivate.h>
#include <RosterPrivate.h>
#include <user_group.h>

Expand Down Expand Up @@ -43,6 +44,7 @@ Job::Job(const Job& other)
fEnabled(other.IsEnabled()),
fService(other.IsService()),
fCreateDefaultPort(other.CreateDefaultPort()),
fLaunching(other.IsLaunching()),
fInitStatus(B_NO_INIT),
fTeam(-1),
fLaunchStatus(B_NO_INIT),
Expand Down Expand Up @@ -380,7 +382,7 @@ bool
Job::CanBeLaunched() const
{
// Services cannot be launched while they are running
return !IsLaunching() && (!IsService() || !IsRunning());
return IsEnabled() && !IsLaunching() && (!IsService() || !IsRunning());
}


Expand Down Expand Up @@ -409,6 +411,30 @@ Job::HandleGetLaunchData(BMessage* message)
}


status_t
Job::GetMessenger(BMessenger& messenger)
{
PortMap::const_iterator iterator = fPortMap.begin();
for (; iterator != fPortMap.end(); iterator++) {
if (iterator->second.HasString("name"))
continue;

port_id port = (port_id)iterator->second.GetInt32("port", -1);
if (port >= 0) {
BMessenger::Private(messenger).SetTo(fTeam, port,
B_PREFERRED_TOKEN);
return B_OK;
}
}

// There is no default port, try roster via signature
BString signature = "application/";
signature << Name();

return messenger.SetTo(signature);
}


status_t
Job::Run()
{
Expand Down
2 changes: 2 additions & 0 deletions src/servers/launch/Job.h
Expand Up @@ -20,6 +20,7 @@

using namespace BSupportKit;
class BMessage;
class BMessenger;

class Finder;
class Job;
Expand Down Expand Up @@ -90,6 +91,7 @@ class Job : public BaseJob {
void SetLaunching(bool launching);

status_t HandleGetLaunchData(BMessage* message);
status_t GetMessenger(BMessenger& messenger);

virtual status_t Run();

Expand Down

0 comments on commit 5860caa

Please sign in to comment.