Skip to content

Commit bcf715d

Browse files
committed
[FIXES] After testing
1 parent a02f6bb commit bcf715d

2 files changed

Lines changed: 76 additions & 22 deletions

File tree

Launcher.cpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Plugin {
55

66
SERVICE_REGISTRATION(Launcher, 1, 0);
77

8+
/* static */ Launcher::ProcessObserver Launcher::_observer;
89

910
/* virtual */ const string Launcher::Initialize(PluginHost::IShell* service)
1011
{
@@ -16,18 +17,54 @@ SERVICE_REGISTRATION(Launcher, 1, 0);
1617
// Setup skip URL for right offset.
1718
_service = service;
1819

19-
2020
config.FromString(_service->ConfigLine());
21+
22+
_closeTime = (config.CloseTime.Value());
2123
Core::Process::Options options(config.Command.Value().c_str());
2224
auto iter = config.Parameters.Elements();
2325

24-
return message;
26+
while (iter.Next() == true) {
27+
const Config::Parameter& element(iter.Current());
28+
29+
if ((element.Option.IsSet() == true) && (element.Option.Value().empty() == false)) {
30+
if ((element.Value.IsSet() == true) && (element.Value.Value().empty() == false)) {
31+
options.Set(element.Option.Value(), element.Value.Value());
32+
}
33+
else {
34+
options.Set(element.Option.Value());
35+
}
36+
}
37+
}
38+
39+
_observer.Register(&_notification);
40+
41+
// Well if we where able to parse the parameters (if needed) we are ready to start it..
42+
_process.Launch(options, &_pid);
43+
44+
if (_pid == 0) {
45+
_observer.Unregister(&_notification);
46+
message = _T("Could not spawn the requested app/script [") + config.Command.Value() + ']';
47+
}
48+
49+
return (message);
2550
}
2651

2752
/* virtual */ void Launcher::Deinitialize(PluginHost::IShell* service)
2853
{
2954
ASSERT(_service == service);
3055

56+
_observer.Unregister(&_notification);
57+
58+
if (_process.IsActive() == true) {
59+
// First try a gentle touch....
60+
_process.Kill(false);
61+
62+
// Wait for a maximum of 3 Seconds before we shoot the process!!
63+
if (_process.WaitProcessCompleted(_closeTime * 1000) != Core::ERROR_NONE) {
64+
_process.Kill(true);
65+
}
66+
}
67+
3168
_service = nullptr;
3269
}
3370

@@ -41,9 +78,19 @@ void Launcher::Update(const ProcessObserver::Info& info)
4178
{
4279
// This can potentially be called on a socket thread, so the deactivation (wich in turn kills this object) must be done
4380
// on a seperate thread. Also make sure this call-stack can be unwound before we are totally destructed.
44-
if (_process.Id() == info.Id()) {
81+
if (_pid == info.Id()) {
82+
4583
ASSERT(_service != nullptr);
46-
PluginHost::WorkerPool::Instance().Submit(PluginHost::IShell::Job::Create(_service, PluginHost::IShell::DEACTIVATED, PluginHost::IShell::FAILURE));
84+
85+
if (info.Event() == ProcessObserver::Info::EVENT_EXIT) {
86+
87+
if ((info.ExitCode() & 0xFFFF) == 0) {
88+
PluginHost::WorkerPool::Instance().Submit(PluginHost::IShell::Job::Create(_service, PluginHost::IShell::DEACTIVATED, PluginHost::IShell::AUTOMATIC));
89+
}
90+
else {
91+
PluginHost::WorkerPool::Instance().Submit(PluginHost::IShell::Job::Create(_service, PluginHost::IShell::DEACTIVATED, PluginHost::IShell::FAILURE));
92+
}
93+
}
4794
}
4895
}
4996

Launcher.h

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Launcher : public PluginHost::IPlugin {
3737
Info(const uint8_t buffer[], const uint16_t length)
3838
: _status(PROC_CN_MCAST_IGNORE) {
3939
if (Ingest(buffer, length) == false) {
40-
printf("This failed !!!!\n");
40+
TRACE_L1("This failed !!!!\n");
4141
_info.what = proc_event::PROC_EVENT_NONE;
4242
}
4343
}
@@ -129,7 +129,7 @@ class Launcher : public PluginHost::IPlugin {
129129

130130
public:
131131
Channel(ProcessObserver& parent)
132-
: Core::SocketNetlink(Core::NodeId(NETLINK_CONNECTOR, ::getpid(), CN_IDX_PROC))
132+
: Core::SocketNetlink(Core::NodeId(NETLINK_CONNECTOR, 0, CN_IDX_PROC))
133133
, _parent(parent) {
134134
}
135135
virtual ~Channel() {
@@ -179,37 +179,34 @@ class Launcher : public PluginHost::IPlugin {
179179
auto found = std::find(_callbacks.begin(), _callbacks.end(), observer);
180180
ASSERT(found != _callbacks.end());
181181
_callbacks.erase(found);
182-
if (_callbacks.empty())
182+
if (_callbacks.empty()) {
183183
Close();
184+
}
184185
_adminLock.Unlock();
185186
}
186187

187188
private:
188189
bool Open() {
189-
bool succeeded = (_channel.IsOpen() == true);
190+
bool succeeded = true;
191+
ASSERT (_channel.IsOpen() == false);
190192

191-
if ((succeeded == false) && (_channel.Open(Core::infinite) == Core::ERROR_NONE)) {
193+
if (_channel.Open(Core::infinite) == Core::ERROR_NONE) {
192194
Info message(true);
193195

194196
if (_channel.Send(message, Core::infinite) != Core::ERROR_NONE) {
195197
_channel.Close(Core::infinite);
196-
}
197-
else {
198-
succeeded = true;
198+
succeeded = false;
199199
}
200200
}
201201
return (succeeded);
202202
}
203203
bool Close() {
204-
bool succeeded = (_channel.IsOpen() == false);
205-
206-
if (succeeded == false) {
204+
if (_channel.IsOpen() == true) {
207205

208206
Info message(false);
209207
_channel.Send (message, Core::infinite);
210-
_channel.Close(Core::infinite);
211-
succeeded = true;
212208
}
209+
_channel.Close(Core::infinite);
213210

214211
return (Core::ERROR_NONE);
215212
}
@@ -260,11 +257,16 @@ class Launcher : public PluginHost::IPlugin {
260257
Launcher& _parent;
261258
};
262259

260+
public:
263261
class Config : public Core::JSON::Container {
264262
private:
263+
Config(const Config&) = delete;
264+
Config& operator=(const Config&) = delete;
265+
266+
public:
265267
class Parameter : public Core::JSON::Container {
266268
private:
267-
Parameter& operator=(const Parameter&);
269+
Parameter& operator=(const Parameter&) = delete;
268270

269271
public:
270272
Parameter()
@@ -289,17 +291,16 @@ class Launcher : public PluginHost::IPlugin {
289291
Core::JSON::String Value;
290292
};
291293

292-
Config(const Config&);
293-
Config& operator=(const Config&);
294-
295294
public:
296295
Config()
297296
: Core::JSON::Container()
298297
, Command()
299298
, Parameters()
299+
, CloseTime(3)
300300
{
301301
Add(_T("command"), &Command);
302302
Add(_T("parameters"), &Parameters);
303+
Add(_T("closetime"), &CloseTime);
303304
}
304305
~Config()
305306
{
@@ -308,6 +309,7 @@ class Launcher : public PluginHost::IPlugin {
308309
public:
309310
Core::JSON::String Command;
310311
Core::JSON::ArrayType<Parameter> Parameters;
312+
Core::JSON::DecUInt8 CloseTime;
311313
};
312314

313315
public:
@@ -317,6 +319,8 @@ class Launcher : public PluginHost::IPlugin {
317319
Launcher()
318320
: _service(nullptr)
319321
, _process(false)
322+
, _pid(0)
323+
, _closeTime(0)
320324
, _notification(this)
321325
{
322326
}
@@ -359,8 +363,11 @@ class Launcher : public PluginHost::IPlugin {
359363
private:
360364
PluginHost::IShell* _service;
361365
Core::Process _process;
362-
static ProcessObserver _observer;
366+
uint32_t _pid;
367+
uint8_t _closeTime;
363368
Core::Sink<Notification> _notification;
369+
370+
static ProcessObserver _observer;
364371
};
365372

366373
} //namespace Plugin

0 commit comments

Comments
 (0)