1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2019-2019 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 
24 #include "include/bareos.h"
25 #include "lib/thread_specific_data.h"
26 #include "lib/thread_specific_data_key.h"
27 #include "include/jcr.h"
28 
GetJcrFromThreadSpecificData()29 JobControlRecord* GetJcrFromThreadSpecificData()
30 {
31   return static_cast<JobControlRecord*>(
32       pthread_getspecific(ThreadSpecificDataKey::Key()));
33 }
34 
SetJcrInThreadSpecificData(JobControlRecord * jcr)35 void SetJcrInThreadSpecificData(JobControlRecord* jcr)
36 {
37   int status = pthread_setspecific(ThreadSpecificDataKey::Key(), jcr);
38   if (status != 0) {
39     BErrNo be;
40     Jmsg1(jcr, M_ABORT, 0, _("pthread_setspecific failed: ERR=%s\n"),
41           be.bstrerror(status));
42   }
43 }
44 
RemoveJcrFromThreadSpecificData(JobControlRecord * jcr)45 void RemoveJcrFromThreadSpecificData(JobControlRecord* jcr)
46 {
47   if (jcr == GetJcrFromThreadSpecificData()) {
48     SetJcrInThreadSpecificData(nullptr);
49   }
50 }
51 
GetJobIdFromThreadSpecificData()52 uint32_t GetJobIdFromThreadSpecificData()
53 {
54   JobControlRecord* jcr = GetJcrFromThreadSpecificData();
55   return (jcr ? jcr->JobId : 0);
56 }
57