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 static int num_last_jobs = 0;
39 #define MAX_LAST_JOBS 10
41 static JCR *jobs = NULL; /* pointer to JCR chain */
42 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
44 void init_last_jobs_list()
46 struct s_last_job *job_entry;
47 last_jobs = new dlist(job_entry, &job_entry->link);
48 memset(&last_job, 0, sizeof(last_job));
51 void term_last_jobs_list()
53 for (void *je=NULL; (je=last_jobs->next(je)); ) {
59 void lock_last_jobs_list()
64 void unlock_last_jobs_list()
70 * Create a Job Control Record and link it into JCR chain
71 * Returns newly allocated JCR
72 * Note, since each daemon has a different JCR, he passes
75 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
78 struct sigaction sigtimer;
80 Dmsg0(200, "Enter new_jcr\n");
81 jcr = (JCR *)malloc(size);
83 jcr->my_thread_id = pthread_self();
84 jcr->sched_time = time(NULL);
85 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
87 pthread_mutex_init(&(jcr->mutex), NULL);
88 jcr->JobStatus = JS_Created; /* ready to run */
89 jcr->VolumeName = get_pool_memory(PM_FNAME);
90 jcr->VolumeName[0] = 0;
91 jcr->errmsg = get_pool_memory(PM_MESSAGE);
93 strcpy(jcr->Job, "*Console*"); /* default */
95 sigtimer.sa_flags = 0;
96 sigtimer.sa_handler = timeout_handler;
97 sigfillset(&sigtimer.sa_mask);
98 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
113 * Remove a JCR from the chain
114 * NOTE! The chain must be locked prior to calling
117 static void remove_jcr(JCR *jcr)
119 Dmsg0(150, "Enter remove_jcr\n");
121 Emsg0(M_ABORT, 0, "NULL jcr.\n");
123 if (!jcr->prev) { /* if no prev */
124 jobs = jcr->next; /* set new head */
126 jcr->prev->next = jcr->next; /* update prev */
129 jcr->next->prev = jcr->prev;
131 Dmsg0(150, "Leave remove_jcr\n");
135 * Free stuff common to all JCRs. N.B. Be careful to include only
136 * generic stuff in the common part of the jcr.
138 static void free_common_jcr(JCR *jcr)
140 /* Keep some statistics */
141 switch (jcr->JobType) {
147 last_job.JobType = jcr->JobType;
148 last_job.JobId = jcr->JobId;
149 last_job.VolSessionId = jcr->VolSessionId;
150 last_job.VolSessionTime = jcr->VolSessionTime;
151 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
152 last_job.JobFiles = jcr->JobFiles;
153 last_job.JobBytes = jcr->JobBytes;
154 last_job.JobStatus = jcr->JobStatus;
155 last_job.JobLevel = jcr->JobLevel;
156 last_job.start_time = jcr->start_time;
157 last_job.end_time = time(NULL);
162 pthread_mutex_destroy(&jcr->mutex);
164 close_msg(jcr); /* close messages for this job */
166 /* do this after closing messages */
167 if (jcr->client_name) {
168 free_pool_memory(jcr->client_name);
169 jcr->client_name = NULL;
172 if (jcr->sd_auth_key) {
173 free(jcr->sd_auth_key);
174 jcr->sd_auth_key = NULL;
176 if (jcr->VolumeName) {
177 free_pool_memory(jcr->VolumeName);
178 jcr->VolumeName = NULL;
181 if (jcr->dir_bsock) {
182 bnet_close(jcr->dir_bsock);
183 jcr->dir_bsock = NULL;
186 free_pool_memory(jcr->errmsg);
193 if (jcr->cached_path) {
194 free_pool_memory(jcr->cached_path);
195 jcr->cached_path = NULL;
198 free_getuser_cache();
199 free_getgroup_cache();
204 * Global routine to free a jcr
207 void b_free_jcr(char *file, int line, JCR *jcr)
209 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
213 void free_jcr(JCR *jcr)
216 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
219 struct s_last_job *je;
222 jcr->use_count--; /* decrement use count */
223 Dmsg3(200, "Dec jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
224 if (jcr->use_count > 0) { /* if in use */
226 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 */
236 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 (++num_last_jobs > MAX_LAST_JOBS) {
245 last_jobs->remove(last_jobs->first());
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)
332 for (jcr = jobs; jcr; jcr=jcr->next) {
333 if (strncmp(Job, jcr->Job, len) == 0) {
335 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
345 * Given a Job, find the JCR
346 * requires an exact match of names.
347 * Returns: jcr on success
350 JCR *get_jcr_by_full_name(char *Job)
355 for (jcr = jobs; jcr; jcr=jcr->next) {
356 if (strcmp(jcr->Job, Job) == 0) {
358 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
366 void set_jcr_job_status(JCR *jcr, int JobStatus)
369 * For a set of errors, ... keep the current status
370 * so it isn't lost. For all others, set it.
372 switch (jcr->JobStatus) {
373 case JS_ErrorTerminated:
380 jcr->JobStatus = JobStatus;
387 void lock_jcr_chain()
395 void unlock_jcr_chain()
401 JCR *get_next_jcr(JCR *jcr)
412 Dmsg1(200, "Inc jcr use_count=%d\n", rjcr->use_count);