2 * Manipulation routines for Job Control Records
4 * Kern E. Sibbald, December 2000
8 * These routines are thread safe.
12 Copyright (C) 2000-2003 Kern Sibbald and John Walker
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write to the Free
26 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 extern void timeout_handler(int sig);
36 struct s_last_job last_job; /* last job run by this daemon */
38 #define MAX_LAST_JOBS 10
40 static JCR *jobs = NULL; /* pointer to JCR chain */
42 /* Mutex for locking various jcr chains while updating */
43 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
45 void init_last_jobs_list()
47 struct s_last_job *job_entry;
48 last_jobs = new dlist(job_entry, &job_entry->link);
49 memset(&last_job, 0, sizeof(last_job));
52 void term_last_jobs_list()
54 for (void *je=NULL; (je=last_jobs->next(je)); ) {
60 void lock_last_jobs_list()
65 void unlock_last_jobs_list()
71 * Create a Job Control Record and link it into JCR chain
72 * Returns newly allocated JCR
73 * Note, since each daemon has a different JCR, he passes
76 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
79 struct sigaction sigtimer;
81 Dmsg0(200, "Enter new_jcr\n");
82 jcr = (JCR *)malloc(size);
84 jcr->my_thread_id = pthread_self();
85 jcr->sched_time = time(NULL);
86 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
88 pthread_mutex_init(&(jcr->mutex), NULL);
89 jcr->JobStatus = JS_Created; /* ready to run */
90 jcr->VolumeName = get_pool_memory(PM_FNAME);
91 jcr->VolumeName[0] = 0;
92 jcr->errmsg = get_pool_memory(PM_MESSAGE);
94 strcpy(jcr->Job, "*Console*"); /* default */
96 sigtimer.sa_flags = 0;
97 sigtimer.sa_handler = timeout_handler;
98 sigfillset(&sigtimer.sa_mask);
99 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
114 * Remove a JCR from the chain
115 * NOTE! The chain must be locked prior to calling
118 static void remove_jcr(JCR *jcr)
120 Dmsg0(150, "Enter remove_jcr\n");
122 Emsg0(M_ABORT, 0, "NULL jcr.\n");
124 if (!jcr->prev) { /* if no prev */
125 jobs = jcr->next; /* set new head */
127 jcr->prev->next = jcr->next; /* update prev */
130 jcr->next->prev = jcr->prev;
132 Dmsg0(150, "Leave remove_jcr\n");
136 * Free stuff common to all JCRs. N.B. Be careful to include only
137 * generic stuff in the common part of the jcr.
139 static void free_common_jcr(JCR *jcr)
141 /* Keep some statistics */
142 switch (jcr->JobType) {
148 last_job.JobType = jcr->JobType;
149 last_job.JobId = jcr->JobId;
150 last_job.VolSessionId = jcr->VolSessionId;
151 last_job.VolSessionTime = jcr->VolSessionTime;
152 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
153 last_job.JobFiles = jcr->JobFiles;
154 last_job.JobBytes = jcr->JobBytes;
155 last_job.JobStatus = jcr->JobStatus;
156 last_job.JobLevel = jcr->JobLevel;
157 last_job.start_time = jcr->start_time;
158 last_job.end_time = time(NULL);
163 pthread_mutex_destroy(&jcr->mutex);
165 close_msg(jcr); /* close messages for this job */
167 /* do this after closing messages */
168 if (jcr->client_name) {
169 free_pool_memory(jcr->client_name);
170 jcr->client_name = NULL;
173 if (jcr->sd_auth_key) {
174 free(jcr->sd_auth_key);
175 jcr->sd_auth_key = NULL;
177 if (jcr->VolumeName) {
178 free_pool_memory(jcr->VolumeName);
179 jcr->VolumeName = NULL;
182 if (jcr->dir_bsock) {
183 bnet_close(jcr->dir_bsock);
184 jcr->dir_bsock = NULL;
187 free_pool_memory(jcr->errmsg);
194 if (jcr->cached_path) {
195 free_pool_memory(jcr->cached_path);
196 jcr->cached_path = NULL;
199 free_getuser_cache();
200 free_getgroup_cache();
205 * Global routine to free a jcr
208 void b_free_jcr(char *file, int line, JCR *jcr)
210 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
214 void free_jcr(JCR *jcr)
217 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
220 struct s_last_job *je;
223 jcr->use_count--; /* decrement use count */
224 Dmsg3(200, "Dec jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
225 if (jcr->use_count > 0) { /* if in use */
227 Dmsg2(200, "jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
232 Dmsg1(200, "End job=%d\n", jcr->JobId);
233 if (jcr->daemon_free_jcr) {
234 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
237 free_common_jcr(jcr);
239 /* Keep list of last jobs, but not Console where JobId==0 */
240 if (last_job.JobId > 0) {
241 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
242 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
243 last_jobs->append(je);
244 if (last_jobs->size() > MAX_LAST_JOBS) {
245 last_jobs->remove(last_jobs->first());
247 last_job.JobId = 0; /* zap last job */
249 close_msg(NULL); /* flush any daemon messages */
251 Dmsg0(200, "Exit free_jcr\n");
256 * Global routine to free a jcr
257 * JCR chain is already locked
259 void free_locked_jcr(JCR *jcr)
261 jcr->use_count--; /* decrement use count */
262 Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
263 if (jcr->use_count > 0) { /* if in use */
267 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
268 free_common_jcr(jcr);
275 * Given a JobId, find the JCR
276 * Returns: jcr on success
279 JCR *get_jcr_by_id(uint32_t JobId)
284 for (jcr = jobs; jcr; jcr=jcr->next) {
285 if (jcr->JobId == JobId) {
287 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
296 * Given a SessionId and SessionTime, find the JCR
297 * Returns: jcr on success
300 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
305 for (jcr = jobs; jcr; jcr=jcr->next) {
306 if (jcr->VolSessionId == SessionId &&
307 jcr->VolSessionTime == SessionTime) {
309 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
319 * Given a Job, find the JCR
320 * compares on the number of characters in Job
321 * thus allowing partial matches.
322 * Returns: jcr on success
325 JCR *get_jcr_by_partial_name(char *Job)
335 for (jcr = jobs; jcr; jcr=jcr->next) {
336 if (strncmp(Job, jcr->Job, len) == 0) {
338 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
348 * Given a Job, find the JCR
349 * requires an exact match of names.
350 * Returns: jcr on success
353 JCR *get_jcr_by_full_name(char *Job)
361 for (jcr = jobs; jcr; jcr=jcr->next) {
362 if (strcmp(jcr->Job, Job) == 0) {
364 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
372 void set_jcr_job_status(JCR *jcr, int JobStatus)
375 * For a set of errors, ... keep the current status
376 * so it isn't lost. For all others, set it.
378 switch (jcr->JobStatus) {
379 case JS_ErrorTerminated:
386 jcr->JobStatus = JobStatus;
393 void lock_jcr_chain()
401 void unlock_jcr_chain()
407 JCR *get_next_jcr(JCR *jcr)
418 Dmsg1(200, "Inc jcr use_count=%d\n", rjcr->use_count);