/
jcr.c
1317 lines (1180 loc) · 31.4 KB
/
jcr.c
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Manipulation routines for Job Control Records and
* handling of last_jobs_list.
*
* Kern E. Sibbald, December 2000
*
* These routines are thread safe.
*
* The job list routines were re-written in May 2005 to
* eliminate the global lock while traversing the list, and
* to use the dlist subroutines. The locking is now done
* on the list each time the list is modified or traversed.
* That is it is "micro-locked" rather than globally locked.
* The result is that there is one lock/unlock for each entry
* in the list while traversing it rather than a single lock
* at the beginning of a traversal and one at the end. This
* incurs slightly more overhead, but effectively eliminates
* the possibilty of race conditions. In addition, with the
* exception of the global locking of the list during the
* re-reading of the config file, no recursion is needed.
*
*/
#include "bareos.h"
#include "jcr.h"
const int dbglvl = 3400;
/* External variables we reference */
/* External referenced functions */
void free_bregexps(alist *bregexps);
/* Forward referenced functions */
extern "C" void timeout_handler(int sig);
static void jcr_timeout_check(watchdog_t *self);
#ifdef TRACE_JCR_CHAIN
static void b_lock_jcr_chain(const char *filen, int line);
static void b_unlock_jcr_chain(const char *filen, int line);
#define lock_jcr_chain() b_lock_jcr_chain(__FILE__, __LINE__);
#define unlock_jcr_chain() b_unlock_jcr_chain(__FILE__, __LINE__);
#else
static void lock_jcr_chain();
static void unlock_jcr_chain();
#endif
int num_jobs_run;
dlist *last_jobs = NULL;
const int max_last_jobs = 10;
static dlist *jcrs = NULL; /* JCR chain */
static pthread_mutex_t jcr_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t job_start_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t last_jobs_mutex = PTHREAD_MUTEX_INITIALIZER;
#ifdef HAVE_WIN32
static bool tsd_initialized = false;
static pthread_key_t jcr_key; /* Pointer to jcr for each thread */
#else
#ifdef PTHREAD_ONCE_KEY_NP
static pthread_key_t jcr_key = PTHREAD_ONCE_KEY_NP;
#else
static pthread_key_t jcr_key; /* Pointer to jcr for each thread */
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
#endif
#endif
static char Job_status[] = "Status Job=%s JobStatus=%d\n";
void lock_jobs()
{
P(job_start_mutex);
}
void unlock_jobs()
{
V(job_start_mutex);
}
void init_last_jobs_list()
{
JCR *jcr = NULL;
struct s_last_job *job_entry = NULL;
if (!last_jobs) {
last_jobs = New(dlist(job_entry, &job_entry->link));
}
if (!jcrs) {
jcrs = New(dlist(jcr, &jcr->link));
}
}
void term_last_jobs_list()
{
if (last_jobs) {
lock_last_jobs_list();
while (!last_jobs->empty()) {
void *je = last_jobs->first();
last_jobs->remove(je);
free(je);
}
delete last_jobs;
last_jobs = NULL;
unlock_last_jobs_list();
}
if (jcrs) {
delete jcrs;
jcrs = NULL;
}
}
bool read_last_jobs_list(int fd, uint64_t addr)
{
struct s_last_job *je, job;
uint32_t num;
bool ok = true;
Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
if (addr == 0 || lseek(fd, (boffset_t)addr, SEEK_SET) < 0) {
return false;
}
if (read(fd, &num, sizeof(num)) != sizeof(num)) {
return false;
}
Dmsg1(100, "Read num_items=%d\n", num);
if (num > 4 * max_last_jobs) { /* sanity check */
return false;
}
lock_last_jobs_list();
for ( ; num; num--) {
if (read(fd, &job, sizeof(job)) != sizeof(job)) {
berrno be;
Pmsg1(000, "Read job entry. ERR=%s\n", be.bstrerror());
ok = false;
break;
}
if (job.JobId > 0) {
je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
memcpy((char *)je, (char *)&job, sizeof(job));
if (!last_jobs) {
init_last_jobs_list();
}
last_jobs->append(je);
if (last_jobs->size() > max_last_jobs) {
je = (struct s_last_job *)last_jobs->first();
last_jobs->remove(je);
free(je);
}
}
}
unlock_last_jobs_list();
return ok;
}
uint64_t write_last_jobs_list(int fd, uint64_t addr)
{
struct s_last_job *je;
uint32_t num;
ssize_t status;
Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
if (lseek(fd, (boffset_t)addr, SEEK_SET) < 0) {
return 0;
}
if (last_jobs) {
lock_last_jobs_list();
/*
* First record is number of entires
*/
num = last_jobs->size();
if (write(fd, &num, sizeof(num)) != sizeof(num)) {
berrno be;
Pmsg1(000, "Error writing num_items: ERR=%s\n", be.bstrerror());
goto bail_out;
}
foreach_dlist(je, last_jobs) {
if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
berrno be;
Pmsg1(000, "Error writing job: ERR=%s\n", be.bstrerror());
goto bail_out;
}
}
unlock_last_jobs_list();
}
/*
* Return current address
*/
status = lseek(fd, 0, SEEK_CUR);
if (status < 0) {
status = 0;
}
return status;
bail_out:
unlock_last_jobs_list();
return 0;
}
void lock_last_jobs_list()
{
P(last_jobs_mutex);
}
void unlock_last_jobs_list()
{
V(last_jobs_mutex);
}
/*
* Get an ASCII representation of the Operation being performed as an english Noun
*/
const char *JCR::get_OperationName()
{
switch(m_JobType) {
case JT_BACKUP:
return _("Backup");
case JT_VERIFY:
return _("Verifying");
case JT_RESTORE:
return _("Restoring");
case JT_ARCHIVE:
return _("Archiving");
case JT_COPY:
return _("Copying");
case JT_MIGRATE:
return _("Migration");
case JT_SCAN:
return _("Scanning");
default:
return _("Unknown operation");
}
}
/*
* Get an ASCII representation of the Action being performed either an english Verb or Adjective
*/
const char *JCR::get_ActionName(bool past)
{
switch(m_JobType) {
case JT_BACKUP:
return _("backup");
case JT_VERIFY:
return (past) ? _("verified") : _("verify");
case JT_RESTORE:
return (past) ? _("restored") : _("restore");
case JT_ARCHIVE:
return (past) ? _("archived") : _("archive");
case JT_COPY:
return (past) ? _("copied") : _("copy");
case JT_MIGRATE:
return (past) ? _("migrated") : _("migrate");
case JT_SCAN:
return (past) ? _("scanned") : _("scan");
default:
return _("unknown action");
}
}
bool JCR::JobReads()
{
switch (m_JobType) {
case JT_VERIFY:
case JT_RESTORE:
case JT_COPY:
case JT_MIGRATE:
return true;
case JT_BACKUP:
if (m_JobLevel == L_VIRTUAL_FULL) {
return true;
}
break;
default:
break;
}
return false;
}
/*
* Push a subroutine address into the job end callback stack
*/
void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr,void *), void *ctx)
{
jcr->job_end_push.append((void *)job_end_cb);
jcr->job_end_push.append(ctx);
}
/*
* Pop each job_end subroutine and call it
*/
static void job_end_pop(JCR *jcr)
{
void (*job_end_cb)(JCR *jcr, void *ctx);
void *ctx;
for (int i=jcr->job_end_push.size()-1; i > 0; ) {
ctx = jcr->job_end_push.get(i--);
job_end_cb = (void (*)(JCR *,void *))jcr->job_end_push.get(i--);
job_end_cb(jcr, ctx);
}
}
/*
* Create thread key for thread specific data.
*/
static void create_jcr_key()
{
int status;
#ifdef PTHREAD_ONCE_KEY_NP
status = pthread_key_create_once_np(&jcr_key, NULL);
#else
status = pthread_key_create(&jcr_key, NULL);
#endif
if (status != 0) {
berrno be;
Jmsg1(NULL, M_ABORT, 0, _("pthread key create failed: ERR=%s\n"),
be.bstrerror(status));
}
}
/*
* Setup thread key for thread specific data.
*/
void setup_tsd_key()
{
#ifdef HAVE_WIN32
P(jcr_lock);
if (!tsd_initialized) {
create_jcr_key();
tsd_initialized = true;
}
V(jcr_lock);
#else
#ifdef PTHREAD_ONCE_KEY_NP
create_jcr_key();
#else
int status;
status = pthread_once(&key_once, create_jcr_key);
if (status != 0) {
berrno be;
Jmsg1(NULL, M_ABORT, 0, _("pthread_once failed. ERR=%s\n"), be.bstrerror(status));
}
#endif
#endif
}
/*
* Create a Job Control Record and link it into JCR chain
* Returns newly allocated JCR
*
* Note, since each daemon has a different JCR, he passes
* us the size.
*/
JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
{
JCR *jcr;
MQUEUE_ITEM *item = NULL;
struct sigaction sigtimer;
int status;
Dmsg0(dbglvl, "Enter new_jcr\n");
setup_tsd_key();
jcr = (JCR *)malloc(size);
memset(jcr, 0, size);
jcr->msg_queue = New(dlist(item, &item->link));
if ((status = pthread_mutex_init(&jcr->msg_queue_mutex, NULL)) != 0) {
berrno be;
Jmsg(NULL, M_ABORT, 0, _("Could not init msg_queue mutex. ERR=%s\n"),
be.bstrerror(status));
}
jcr->job_end_push.init(1, false);
jcr->sched_time = time(NULL);
jcr->initial_sched_time = jcr->sched_time;
jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
jcr->init_mutex();
jcr->inc_use_count();
jcr->VolumeName = get_pool_memory(PM_FNAME);
jcr->VolumeName[0] = 0;
jcr->errmsg = get_pool_memory(PM_MESSAGE);
jcr->errmsg[0] = 0;
jcr->comment = get_pool_memory(PM_FNAME);
jcr->comment[0] = 0;
/*
* Setup some dummy values
*/
bstrncpy(jcr->Job, "*System*", sizeof(jcr->Job));
jcr->JobId = 0;
jcr->setJobType(JT_SYSTEM); /* internal job until defined */
jcr->setJobLevel(L_NONE);
jcr->setJobStatus(JS_Created); /* ready to run */
sigtimer.sa_flags = 0;
sigtimer.sa_handler = timeout_handler;
sigfillset(&sigtimer.sa_mask);
sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
/*
* Locking jobs is a global lock that is needed
* so that the Director can stop new jobs from being
* added to the jcr chain while it processes a new
* conf file and does the job_end_push().
*/
lock_jobs();
lock_jcr_chain();
if (!jcrs) {
jcrs = New(dlist(jcr, &jcr->link));
}
jcrs->append(jcr);
unlock_jcr_chain();
unlock_jobs();
return jcr;
}
/*
* Remove a JCR from the chain
*
* NOTE! The chain must be locked prior to calling this routine.
*/
static void remove_jcr(JCR *jcr)
{
Dmsg0(dbglvl, "Enter remove_jcr\n");
if (!jcr) {
Emsg0(M_ABORT, 0, _("NULL jcr.\n"));
}
jcrs->remove(jcr);
Dmsg0(dbglvl, "Leave remove_jcr\n");
}
/*
* Free stuff common to all JCRs. N.B. Be careful to include only
* generic stuff in the common part of the jcr.
*/
static void free_common_jcr(JCR *jcr)
{
/*
* Uses jcr lock/unlock
*/
remove_jcr_from_tsd(jcr);
jcr->set_killable(false);
jcr->destroy_mutex();
if (jcr->msg_queue) {
delete jcr->msg_queue;
jcr->msg_queue = NULL;
pthread_mutex_destroy(&jcr->msg_queue_mutex);
}
if (jcr->client_name) {
free_pool_memory(jcr->client_name);
jcr->client_name = NULL;
}
if (jcr->attr) {
free_pool_memory(jcr->attr);
jcr->attr = NULL;
}
if (jcr->sd_auth_key) {
free(jcr->sd_auth_key);
jcr->sd_auth_key = NULL;
}
if (jcr->VolumeName) {
free_pool_memory(jcr->VolumeName);
jcr->VolumeName = NULL;
}
if (jcr->dir_bsock) {
jcr->dir_bsock->close();
jcr->dir_bsock = NULL;
}
if (jcr->errmsg) {
free_pool_memory(jcr->errmsg);
jcr->errmsg = NULL;
}
if (jcr->where) {
free(jcr->where);
jcr->where = NULL;
}
if (jcr->RegexWhere) {
free(jcr->RegexWhere);
jcr->RegexWhere = NULL;
}
if (jcr->where_bregexp) {
free_bregexps(jcr->where_bregexp);
delete jcr->where_bregexp;
jcr->where_bregexp = NULL;
}
if (jcr->cached_path) {
free_pool_memory(jcr->cached_path);
jcr->cached_path = NULL;
jcr->cached_pnl = 0;
}
if (jcr->id_list) {
free_guid_list(jcr->id_list);
jcr->id_list = NULL;
}
if (jcr->comment) {
free_pool_memory(jcr->comment);
jcr->comment = NULL;
}
free(jcr);
}
/*
* Global routine to free a jcr
*/
#ifdef DEBUG
void b_free_jcr(const char *file, int line, JCR *jcr)
{
struct s_last_job *je;
Dmsg3(dbglvl, "Enter free_jcr jid=%u from %s:%d\n", jcr->JobId, file, line);
#else
void free_jcr(JCR *jcr)
{
struct s_last_job *je;
Dmsg3(dbglvl, "Enter free_jcr jid=%u use_count=%d Job=%s\n",
jcr->JobId, jcr->use_count(), jcr->Job);
#endif
lock_jcr_chain();
jcr->dec_use_count(); /* decrement use count */
if (jcr->use_count() < 0) {
Jmsg2(jcr, M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
jcr->use_count(), jcr->JobId);
}
if (jcr->JobId > 0) {
Dmsg3(dbglvl, "Dec free_jcr jid=%u use_count=%d Job=%s\n",
jcr->JobId, jcr->use_count(), jcr->Job);
}
if (jcr->use_count() > 0) { /* if in use */
unlock_jcr_chain();
return;
}
if (jcr->JobId > 0) {
Dmsg3(dbglvl, "remove jcr jid=%u use_count=%d Job=%s\n",
jcr->JobId, jcr->use_count(), jcr->Job);
}
remove_jcr(jcr); /* remove Jcr from chain */
unlock_jcr_chain();
dequeue_messages(jcr);
job_end_pop(jcr); /* pop and call hooked routines */
Dmsg1(dbglvl, "End job=%d\n", jcr->JobId);
/*
* Keep some statistics
*/
switch (jcr->getJobType()) {
case JT_BACKUP:
case JT_VERIFY:
case JT_RESTORE:
case JT_MIGRATE:
case JT_COPY:
case JT_ADMIN:
/*
* Keep list of last jobs, but not Console where JobId==0
*/
if (jcr->JobId > 0) {
lock_last_jobs_list();
num_jobs_run++;
je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
memset(je, 0, sizeof(struct s_last_job)); /* zero in case unset fields */
je->Errors = jcr->JobErrors;
je->JobType = jcr->getJobType();
je->JobId = jcr->JobId;
je->VolSessionId = jcr->VolSessionId;
je->VolSessionTime = jcr->VolSessionTime;
bstrncpy(je->Job, jcr->Job, sizeof(je->Job));
je->JobFiles = jcr->JobFiles;
je->JobBytes = jcr->JobBytes;
je->JobStatus = jcr->JobStatus;
je->JobLevel = jcr->getJobLevel();
je->start_time = jcr->start_time;
je->end_time = time(NULL);
if (!last_jobs) {
init_last_jobs_list();
}
last_jobs->append(je);
if (last_jobs->size() > max_last_jobs) {
je = (struct s_last_job *)last_jobs->first();
last_jobs->remove(je);
free(je);
}
unlock_last_jobs_list();
}
break;
default:
break;
}
close_msg(jcr); /* close messages for this job */
if (jcr->daemon_free_jcr) {
jcr->daemon_free_jcr(jcr); /* call daemon free routine */
}
free_common_jcr(jcr);
close_msg(NULL); /* flush any daemon messages */
Dmsg0(dbglvl, "Exit free_jcr\n");
}
/*
* Remove jcr from thread specific data, but
* but make sure it is us who are attached.
*/
void remove_jcr_from_tsd(JCR *jcr)
{
JCR *tjcr = get_jcr_from_tsd();
if (tjcr == jcr) {
set_jcr_in_tsd(INVALID_JCR);
}
}
void JCR::set_killable(bool killable)
{
JCR *jcr = this;
jcr->lock();
jcr->my_thread_killable = killable;
if (killable) {
jcr->my_thread_id = pthread_self();
} else {
memset(&jcr->my_thread_id, 0, sizeof(jcr->my_thread_id));
}
jcr->unlock();
}
/*
* Put this jcr in the thread specifc data
* if update_thread_info is true and the jcr is valid,
* we update the my_thread_id in the JCR
*/
void set_jcr_in_tsd(JCR *jcr)
{
int status;
status = pthread_setspecific(jcr_key, (void *)jcr);
if (status != 0) {
berrno be;
Jmsg1(jcr, M_ABORT, 0, _("pthread_setspecific failed: ERR=%s\n"),
be.bstrerror(status));
}
}
void JCR::my_thread_send_signal(int sig)
{
this->lock();
if (this->is_killable() &&
!pthread_equal(this->my_thread_id, pthread_self()))
{
Dmsg1(800, "Send kill to jid=%d\n", this->JobId);
pthread_kill(this->my_thread_id, sig);
} else if (!this->is_killable()) {
Dmsg1(10, "Warning, can't send kill to jid=%d\n", this->JobId);
}
this->unlock();
}
/*
* Give me the jcr that is attached to this thread
*/
JCR *get_jcr_from_tsd()
{
JCR *jcr = (JCR *)pthread_getspecific(jcr_key);
/*
* Set any INVALID_JCR to NULL which the rest of BAREOS understands
*/
if (jcr == INVALID_JCR) {
jcr = NULL;
}
return jcr;
}
/*
* Find which JobId corresponds to the current thread
*/
uint32_t get_jobid_from_tsd()
{
JCR *jcr;
uint32_t JobId = 0;
jcr = get_jcr_from_tsd();
if (jcr) {
JobId = (uint32_t)jcr->JobId;
}
return JobId;
}
/*
* Given a JobId, find the JCR
*
* Returns: jcr on success
* NULL on failure
*/
JCR *get_jcr_by_id(uint32_t JobId)
{
JCR *jcr;
foreach_jcr(jcr) {
if (jcr->JobId == JobId) {
jcr->inc_use_count();
Dmsg3(dbglvl, "Inc get_jcr jid=%u use_count=%d Job=%s\n",
jcr->JobId, jcr->use_count(), jcr->Job);
break;
}
}
endeach_jcr(jcr);
return jcr;
}
/*
* Given a thread id, find the JobId
*
* Returns: JobId on success
* 0 on failure
*/
uint32_t get_jobid_from_tid(pthread_t tid)
{
JCR *jcr = NULL;
bool found = false;
foreach_jcr(jcr) {
if (pthread_equal(jcr->my_thread_id, tid)) {
found = true;
break;
}
}
endeach_jcr(jcr);
if (found) {
return jcr->JobId;
}
return 0;
}
/*
* Given a SessionId and SessionTime, find the JCR
*
* Returns: jcr on success
* NULL on failure
*/
JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
{
JCR *jcr;
foreach_jcr(jcr) {
if (jcr->VolSessionId == SessionId &&
jcr->VolSessionTime == SessionTime) {
jcr->inc_use_count();
Dmsg3(dbglvl, "Inc get_jcr jid=%u use_count=%d Job=%s\n",
jcr->JobId, jcr->use_count(), jcr->Job);
break;
}
}
endeach_jcr(jcr);
return jcr;
}
/*
* Given a Job, find the JCR compares on the number of
* characters in Job thus allowing partial matches.
*
* Returns: jcr on success
* NULL on failure
*/
JCR *get_jcr_by_partial_name(char *Job)
{
JCR *jcr;
int len;
if (!Job) {
return NULL;
}
len = strlen(Job);
foreach_jcr(jcr) {
if (bstrncmp(Job, jcr->Job, len)) {
jcr->inc_use_count();
Dmsg3(dbglvl, "Inc get_jcr jid=%u use_count=%d Job=%s\n",
jcr->JobId, jcr->use_count(), jcr->Job);
break;
}
}
endeach_jcr(jcr);
return jcr;
}
/*
* Given a Job, find the JCR requires an exact match of names.
*
* Returns: jcr on success
* NULL on failure
*/
JCR *get_jcr_by_full_name(char *Job)
{
JCR *jcr;
if (!Job) {
return NULL;
}
foreach_jcr(jcr) {
if (bstrcmp(jcr->Job, Job)) {
jcr->inc_use_count();
Dmsg3(dbglvl, "Inc get_jcr jid=%u use_count=%d Job=%s\n",
jcr->JobId, jcr->use_count(), jcr->Job);
break;
}
}
endeach_jcr(jcr);
return jcr;
}
static void update_wait_time(JCR *jcr, int newJobStatus)
{
bool enter_in_waittime;
int oldJobStatus = jcr->JobStatus;
switch (newJobStatus) {
case JS_WaitFD:
case JS_WaitSD:
case JS_WaitMedia:
case JS_WaitMount:
case JS_WaitStoreRes:
case JS_WaitJobRes:
case JS_WaitClientRes:
case JS_WaitMaxJobs:
case JS_WaitPriority:
enter_in_waittime = true;
break;
default:
enter_in_waittime = false; /* not a Wait situation */
break;
}
/*
* If we were previously waiting and are not any more
* we want to update the wait_time variable, which is
* the start of waiting.
*/
switch (oldJobStatus) {
case JS_WaitFD:
case JS_WaitSD:
case JS_WaitMedia:
case JS_WaitMount:
case JS_WaitStoreRes:
case JS_WaitJobRes:
case JS_WaitClientRes:
case JS_WaitMaxJobs:
case JS_WaitPriority:
if (!enter_in_waittime) { /* we get out the wait time */
jcr->wait_time_sum += (time(NULL) - jcr->wait_time);
jcr->wait_time = 0;
}
break;
default:
/*
* If wait state is new, we keep current time for watchdog MaxWaitTime
*/
if (enter_in_waittime) {
jcr->wait_time = time(NULL);
}
break;
}
}
/*
* Priority runs from 0 (lowest) to 10 (highest)
*/
static int get_status_priority(int JobStatus)
{
int priority = 0;
switch (JobStatus) {
case JS_Incomplete:
priority = 10;
break;
case JS_ErrorTerminated:
case JS_FatalError:
case JS_Canceled:
priority = 9;
break;
case JS_Error:
priority = 8;
break;
case JS_Differences:
priority = 7;
break;
}
return priority;
}
/*
* Send Job status to Director
*/
bool JCR::sendJobStatus()
{
JCR *jcr = this;
if (jcr->dir_bsock) {
return jcr->dir_bsock->fsend(Job_status, jcr->Job, jcr->JobStatus);
}
return true;
}
/*
* Set and send Job status to Director
*/
bool JCR::sendJobStatus(int newJobStatus)
{
JCR *jcr = this;
if (!jcr->is_JobStatus(newJobStatus)) {
setJobStatus(newJobStatus);
if (jcr->dir_bsock) {
return jcr->dir_bsock->fsend(Job_status, jcr->Job, jcr->JobStatus);
}
}
return true;
}
void JCR::setJobStarted()
{
JCR *jcr = this;
jcr->job_started = true;
jcr->job_started_time = time(NULL);
}
void JCR::setJobStatus(int newJobStatus)
{
JCR *jcr = this;
int priority, old_priority;
int oldJobStatus = jcr->JobStatus;
priority = get_status_priority(newJobStatus);
old_priority = get_status_priority(oldJobStatus);
Dmsg2(800, "set_jcr_job_status(%s, %c)\n", Job, newJobStatus);
/*
* Update wait_time depending on newJobStatus and oldJobStatus
*/
update_wait_time(jcr, newJobStatus);
/*
* For a set of errors, ... keep the current status
* so it isn't lost. For all others, set it.
*/
Dmsg2(800, "OnEntry JobStatus=%c newJobstatus=%c\n", oldJobStatus, newJobStatus);
/*
* If status priority is > than proposed new status, change it.
* If status priority == new priority and both are zero, take the new status.
* If it is not zero, then we keep the first non-zero "error" that occurred.
*/
if (priority > old_priority || (
priority == 0 && old_priority == 0)) {
Dmsg4(800, "Set new stat. old: %c,%d new: %c,%d\n",
jcr->JobStatus, old_priority, newJobStatus, priority);