1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2003-2012 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2019 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Kern Sibbald, May MMIII
24  */
25 
26 /**
27  * @file
28  *
29  * responsible for doing admin jobs
30  */
31 
32 #include "include/bareos.h"
33 #include "dird.h"
34 #include "dird/admin.h"
35 #include "dird/jcr_private.h"
36 #include "dird/job.h"
37 #include "dird/storage.h"
38 
39 namespace directordaemon {
40 
41 static const int debuglevel = 100;
42 
DoAdminInit(JobControlRecord * jcr)43 bool DoAdminInit(JobControlRecord* jcr)
44 {
45   FreeRstorage(jcr);
46   if (!AllowDuplicateJob(jcr)) { return false; }
47 
48   return true;
49 }
50 
51 /**
52  * Returns: false on failure
53  *          true  on success
54  */
do_admin(JobControlRecord * jcr)55 bool do_admin(JobControlRecord* jcr)
56 {
57   jcr->impl->jr.JobId = jcr->JobId;
58 
59   jcr->impl->fname = (char*)GetPoolMemory(PM_FNAME);
60 
61   /*
62    * Print Job Start message
63    */
64   Jmsg(jcr, M_INFO, 0, _("Start Admin JobId %d, Job=%s\n"), jcr->JobId,
65        jcr->Job);
66 
67   jcr->setJobStatus(JS_Running);
68   AdminCleanup(jcr, JS_Terminated);
69 
70   return true;
71 }
72 
73 /**
74  * Release resources allocated during backup.
75  */
AdminCleanup(JobControlRecord * jcr,int TermCode)76 void AdminCleanup(JobControlRecord* jcr, int TermCode)
77 {
78   char sdt[50], edt[50], schedt[50];
79   char term_code[100];
80   const char* TermMsg;
81   int msg_type;
82 
83   Dmsg0(debuglevel, "Enter AdminCleanup()\n");
84 
85   UpdateJobEnd(jcr, TermCode);
86 
87   if (!jcr->db->GetJobRecord(jcr, &jcr->impl->jr)) {
88     Jmsg(jcr, M_WARNING, 0,
89          _("Error getting Job record for Job report: ERR=%s"),
90          jcr->db->strerror());
91     jcr->setJobStatus(JS_ErrorTerminated);
92   }
93 
94   msg_type = M_INFO; /* by default INFO message */
95   switch (jcr->JobStatus) {
96     case JS_Terminated:
97       TermMsg = _("Admin OK");
98       break;
99     case JS_FatalError:
100     case JS_ErrorTerminated:
101       TermMsg = _("*** Admin Error ***");
102       msg_type = M_ERROR; /* Generate error message */
103       break;
104     case JS_Canceled:
105       TermMsg = _("Admin Canceled");
106       break;
107     default:
108       TermMsg = term_code;
109       sprintf(term_code, _("Inappropriate term code: %c\n"), jcr->JobStatus);
110       break;
111   }
112   bstrftimes(schedt, sizeof(schedt), jcr->impl->jr.SchedTime);
113   bstrftimes(sdt, sizeof(sdt), jcr->impl->jr.StartTime);
114   bstrftimes(edt, sizeof(edt), jcr->impl->jr.EndTime);
115 
116   Jmsg(jcr, msg_type, 0,
117        _("BAREOS %s (%s): %s\n"
118          "  JobId:                  %d\n"
119          "  Job:                    %s\n"
120          "  Scheduled time:         %s\n"
121          "  Start time:             %s\n"
122          "  End time:               %s\n"
123          "  Bareos binary info:     %s\n"
124          "  Termination:            %s\n\n"),
125        kBareosVersionStrings.Full, kBareosVersionStrings.ShortDate, edt, jcr->impl->jr.JobId,
126        jcr->impl->jr.Job, schedt, sdt, edt, kBareosVersionStrings.JoblogMessage,
127        TermMsg);
128 
129   Dmsg0(debuglevel, "Leave AdminCleanup()\n");
130 }
131 
132 } /* namespace directordaemon */
133