-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
Job.cpp
637 lines (483 loc) · 11.4 KB
/
Job.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
* Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
#include "Job.h"
#include <stdlib.h>
#include <Entry.h>
#include <Looper.h>
#include <Message.h>
#include <Roster.h>
#include <MessagePrivate.h>
#include <RosterPrivate.h>
#include <user_group.h>
#include "Target.h"
#include "Utility.h"
Job::Job(const char* name)
:
BaseJob(name),
fEnabled(true),
fService(false),
fCreateDefaultPort(false),
fLaunching(false),
fInitStatus(B_NO_INIT),
fTeam(-1),
fLaunchStatus(B_NO_INIT),
fTarget(NULL),
fPendingLaunchDataReplies(0, false)
{
mutex_init(&fLaunchStatusLock, "launch status lock");
}
Job::Job(const Job& other)
:
BaseJob(other.Name()),
fEnabled(other.IsEnabled()),
fService(other.IsService()),
fCreateDefaultPort(other.CreateDefaultPort()),
fLaunching(other.IsLaunching()),
fInitStatus(B_NO_INIT),
fTeam(-1),
fLaunchStatus(B_NO_INIT),
fTarget(other.Target()),
fPendingLaunchDataReplies(0, false)
{
mutex_init(&fLaunchStatusLock, "launch status lock");
fCondition = other.fCondition;
// TODO: copy events
//fEvent = other.fEvent;
fEnvironment = other.fEnvironment;
fSourceFiles = other.fSourceFiles;
for (int32 i = 0; i < other.Arguments().CountStrings(); i++)
AddArgument(other.Arguments().StringAt(i));
for (int32 i = 0; i < other.Requirements().CountStrings(); i++)
AddRequirement(other.Requirements().StringAt(i));
PortMap::const_iterator constIterator = other.Ports().begin();
for (; constIterator != other.Ports().end(); constIterator++) {
fPortMap.insert(
std::make_pair(constIterator->first, constIterator->second));
}
PortMap::iterator iterator = fPortMap.begin();
for (; iterator != fPortMap.end(); iterator++)
iterator->second.RemoveData("port");
}
Job::~Job()
{
_DeletePorts();
}
::TeamRegistrator*
Job::TeamRegistrator() const
{
return fTeamRegistrator;
}
void
Job::SetTeamRegistrator(::TeamRegistrator* registrator)
{
fTeamRegistrator = registrator;
}
bool
Job::IsEnabled() const
{
return fEnabled;
}
void
Job::SetEnabled(bool enable)
{
fEnabled = enable;
}
bool
Job::IsService() const
{
return fService;
}
void
Job::SetService(bool service)
{
fService = service;
}
bool
Job::CreateDefaultPort() const
{
return fCreateDefaultPort;
}
void
Job::SetCreateDefaultPort(bool createPort)
{
fCreateDefaultPort = createPort;
}
void
Job::AddPort(BMessage& data)
{
const char* name = data.GetString("name");
fPortMap.insert(std::pair<BString, BMessage>(BString(name), data));
}
const BStringList&
Job::Arguments() const
{
return fArguments;
}
BStringList&
Job::Arguments()
{
return fArguments;
}
void
Job::AddArgument(const char* argument)
{
fArguments.Add(argument);
}
::Target*
Job::Target() const
{
return fTarget;
}
void
Job::SetTarget(::Target* target)
{
fTarget = target;
}
const BStringList&
Job::Requirements() const
{
return fRequirements;
}
BStringList&
Job::Requirements()
{
return fRequirements;
}
void
Job::AddRequirement(const char* requirement)
{
fRequirements.Add(requirement);
}
bool
Job::CheckCondition(ConditionContext& context) const
{
if (Target() != NULL && !Target()->HasLaunched())
return false;
return BaseJob::CheckCondition(context);
}
status_t
Job::Init(const Finder& finder, std::set<BString>& dependencies)
{
// Only initialize the jobs once
if (fInitStatus != B_NO_INIT)
return fInitStatus;
fInitStatus = B_OK;
if (fTarget != NULL)
fTarget->AddDependency(this);
// Check dependencies
for (int32 index = 0; index < Requirements().CountStrings(); index++) {
const BString& requires = Requirements().StringAt(index);
if (dependencies.find(requires) != dependencies.end()) {
// Found a cyclic dependency
// TODO: log error
return fInitStatus = B_ERROR;
}
dependencies.insert(requires);
Job* dependency = finder.FindJob(requires);
if (dependency != NULL) {
std::set<BString> subDependencies = dependencies;
fInitStatus = dependency->Init(finder, subDependencies);
if (fInitStatus != B_OK) {
// TODO: log error
return fInitStatus;
}
fInitStatus = _AddRequirement(dependency);
} else {
::Target* target = finder.FindTarget(requires);
if (target != NULL)
fInitStatus = _AddRequirement(dependency);
else {
// Could not find dependency
fInitStatus = B_NAME_NOT_FOUND;
}
}
if (fInitStatus != B_OK) {
// TODO: log error
return fInitStatus;
}
}
return fInitStatus;
}
status_t
Job::InitCheck() const
{
return fInitStatus;
}
team_id
Job::Team() const
{
return fTeam;
}
const PortMap&
Job::Ports() const
{
return fPortMap;
}
port_id
Job::Port(const char* name) const
{
PortMap::const_iterator found = fPortMap.find(name);
if (found != fPortMap.end())
return found->second.GetInt32("port", -1);
return B_NAME_NOT_FOUND;
}
status_t
Job::Launch()
{
// Build environment
std::vector<const char*> environment;
for (const char** variable = (const char**)environ; variable[0] != NULL;
variable++) {
environment.push_back(variable[0]);
}
if (Target() != NULL)
_AddStringList(environment, Target()->Environment());
_AddStringList(environment, Environment());
// Resolve source files
BStringList sourceFilesEnvironment;
GetSourceFilesEnvironment(sourceFilesEnvironment);
_AddStringList(environment, sourceFilesEnvironment);
environment.push_back(NULL);
if (fArguments.IsEmpty()) {
// Launch by signature
BString signature("application/");
signature << Name();
return _Launch(signature.String(), NULL, 0, NULL, &environment[0]);
}
// Build argument vector
entry_ref ref;
status_t status = get_ref_for_path(
Utility::TranslatePath(fArguments.StringAt(0).String()), &ref);
if (status != B_OK) {
_SetLaunchStatus(status);
return status;
}
std::vector<BString> strings;
std::vector<const char*> args;
size_t count = fArguments.CountStrings() - 1;
if (count > 0) {
for (int32 i = 1; i < fArguments.CountStrings(); i++) {
strings.push_back(Utility::TranslatePath(fArguments.StringAt(i)));
args.push_back(strings.back());
}
args.push_back(NULL);
}
// Launch via entry_ref
return _Launch(NULL, &ref, count, &args[0], &environment[0]);
}
bool
Job::IsLaunched() const
{
return fLaunchStatus != B_NO_INIT;
}
bool
Job::IsRunning() const
{
return fTeam >= 0;
}
void
Job::TeamDeleted()
{
fTeam = -1;
if (IsService())
SetState(B_JOB_STATE_WAITING_TO_RUN);
}
bool
Job::CanBeLaunched() const
{
// Services cannot be launched while they are running
return IsEnabled() && !IsLaunching() && (!IsService() || !IsRunning());
}
bool
Job::IsLaunching() const
{
return fLaunching;
}
void
Job::SetLaunching(bool launching)
{
fLaunching = launching;
}
status_t
Job::HandleGetLaunchData(BMessage* message)
{
MutexLocker launchLocker(fLaunchStatusLock);
if (IsLaunched())
return _SendLaunchDataReply(message);
return fPendingLaunchDataReplies.AddItem(message) ? B_OK : B_NO_MEMORY;
}
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()
{
status_t status = BJob::Run();
// Jobs can be relaunched at any time
if (!IsService())
SetState(B_JOB_STATE_WAITING_TO_RUN);
return status;
}
status_t
Job::Execute()
{
status_t status = B_OK;
if (!IsRunning() || !IsService())
status = Launch();
else
debug_printf("Ignore launching %s\n", Name());
fLaunching = false;
return status;
}
void
Job::_DeletePorts()
{
PortMap::const_iterator iterator = Ports().begin();
for (; iterator != Ports().end(); iterator++) {
port_id port = iterator->second.GetInt32("port", -1);
if (port >= 0)
delete_port(port);
}
}
status_t
Job::_AddRequirement(BJob* dependency)
{
if (dependency == NULL)
return B_OK;
switch (dependency->State()) {
case B_JOB_STATE_WAITING_TO_RUN:
case B_JOB_STATE_STARTED:
case B_JOB_STATE_IN_PROGRESS:
AddDependency(dependency);
break;
case B_JOB_STATE_SUCCEEDED:
// Just queue it without any dependencies
break;
case B_JOB_STATE_FAILED:
case B_JOB_STATE_ABORTED:
// TODO: return appropriate error
return B_BAD_VALUE;
}
return B_OK;
}
void
Job::_AddStringList(std::vector<const char*>& array, const BStringList& list)
{
int32 count = list.CountStrings();
for (int32 index = 0; index < count; index++) {
array.push_back(list.StringAt(index).String());
}
}
void
Job::_SetLaunchStatus(status_t launchStatus)
{
MutexLocker launchLocker(fLaunchStatusLock);
fLaunchStatus = launchStatus != B_NO_INIT ? launchStatus : B_ERROR;
launchLocker.Unlock();
if (fTeamRegistrator != NULL)
fTeamRegistrator->RegisterTeam(this);
_SendPendingLaunchDataReplies();
}
status_t
Job::_SendLaunchDataReply(BMessage* message)
{
BMessage reply(fTeam < 0 ? fTeam : (uint32)B_OK);
if (reply.what == B_OK) {
reply.AddInt32("team", fTeam);
PortMap::const_iterator iterator = fPortMap.begin();
for (; iterator != fPortMap.end(); iterator++) {
BString name;
if (iterator->second.HasString("name"))
name << iterator->second.GetString("name") << "_";
name << "port";
reply.AddInt32(name.String(),
iterator->second.GetInt32("port", -1));
}
}
message->SendReply(&reply);
delete message;
return B_OK;
}
void
Job::_SendPendingLaunchDataReplies()
{
for (int32 i = 0; i < fPendingLaunchDataReplies.CountItems(); i++)
_SendLaunchDataReply(fPendingLaunchDataReplies.ItemAt(i));
fPendingLaunchDataReplies.MakeEmpty();
}
status_t
Job::_CreateAndTransferPorts()
{
// TODO: prefix system ports with "system:"
bool defaultPort = false;
for (PortMap::iterator iterator = fPortMap.begin();
iterator != fPortMap.end(); iterator++) {
BString name(Name());
const char* suffix = iterator->second.GetString("name");
if (suffix != NULL)
name << ':' << suffix;
else
defaultPort = true;
const int32 capacity = iterator->second.GetInt32("capacity",
B_LOOPER_PORT_DEFAULT_CAPACITY);
port_id port = create_port(capacity, name.String());
if (port < 0)
return port;
status_t result = set_port_owner(port, fTeam);
if (result != B_OK)
return result;
iterator->second.SetInt32("port", port);
if (name == "x-vnd.haiku-registrar:auth") {
// Allow the launch_daemon to access the registrar authentication
BPrivate::set_registrar_authentication_port(port);
}
}
if (fCreateDefaultPort && !defaultPort) {
BMessage data;
data.AddInt32("capacity", B_LOOPER_PORT_DEFAULT_CAPACITY);
port_id port = create_port(B_LOOPER_PORT_DEFAULT_CAPACITY, Name());
if (port < 0)
return port;
status_t result = set_port_owner(port, fTeam);
if (result != B_OK)
return result;
data.SetInt32("port", port);
AddPort(data);
}
return B_OK;
}
status_t
Job::_Launch(const char* signature, entry_ref* ref, int argCount,
const char* const* args, const char** environment)
{
thread_id mainThread = -1;
status_t result = BRoster::Private().Launch(signature, ref, NULL, argCount,
args, environment, &fTeam, &mainThread, true);
if (result == B_OK) {
result = _CreateAndTransferPorts();
if (result == B_OK)
resume_thread(mainThread);
else
kill_thread(mainThread);
}
_SetLaunchStatus(result);
return result;
}