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 JCR *jobs = NULL; /* pointer to JCR chain */
39 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
42 * Create a Job Control Record and link it into JCR chain
43 * Returns newly allocated JCR
44 * Note, since each daemon has a different JCR, he passes
47 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
50 struct sigaction sigtimer;
52 Dmsg0(200, "Enter new_jcr\n");
53 jcr = (JCR *)malloc(size);
55 jcr->my_thread_id = pthread_self();
56 jcr->sched_time = time(NULL);
57 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
59 pthread_mutex_init(&(jcr->mutex), NULL);
60 jcr->JobStatus = JS_Created; /* ready to run */
61 jcr->VolumeName = get_pool_memory(PM_FNAME);
62 jcr->VolumeName[0] = 0;
63 jcr->errmsg = get_pool_memory(PM_MESSAGE);
66 sigtimer.sa_flags = 0;
67 sigtimer.sa_handler = timeout_handler;
68 sigfillset(&sigtimer.sa_mask);
69 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
84 * Remove a JCR from the chain
85 * NOTE! The chain must be locked prior to calling
88 static void remove_jcr(JCR *jcr)
90 Dmsg0(150, "Enter remove_jcr\n");
92 Emsg0(M_ABORT, 0, "NULL jcr.\n");
94 if (!jcr->prev) { /* if no prev */
95 jobs = jcr->next; /* set new head */
97 jcr->prev->next = jcr->next; /* update prev */
100 jcr->next->prev = jcr->prev;
102 Dmsg0(150, "Leave remove_jcr\n");
106 * Free stuff common to all JCRs
108 static void free_common_jcr(JCR *jcr)
110 /* Keep some statistics */
111 switch (jcr->JobType) {
116 last_job.JobType = jcr->JobType;
117 last_job.JobId = jcr->JobId;
118 last_job.VolSessionId = jcr->VolSessionId;
119 last_job.VolSessionTime = jcr->VolSessionTime;
120 strcpy(last_job.Job, jcr->Job);
121 last_job.JobFiles = jcr->JobFiles;
122 last_job.JobBytes = jcr->JobBytes;
123 last_job.JobStatus = jcr->JobStatus;
124 last_job.start_time = jcr->start_time;
125 last_job.end_time = time(NULL);
130 pthread_mutex_destroy(&jcr->mutex);
132 close_msg(jcr); /* close messages for this job */
134 /* do this after closing messages */
135 if (jcr->client_name) {
136 free_pool_memory(jcr->client_name);
137 jcr->client_name = NULL;
140 if (jcr->sd_auth_key) {
141 Dmsg0(200, "Free JCR sd_auth_key\n");
142 free(jcr->sd_auth_key);
143 jcr->sd_auth_key = NULL;
145 if (jcr->VolumeName) {
146 free_pool_memory(jcr->VolumeName);
147 jcr->VolumeName = NULL;
150 if (jcr->dir_bsock) {
151 bnet_close(jcr->dir_bsock);
152 jcr->dir_bsock = NULL;
155 free_pool_memory(jcr->errmsg);
162 * Global routine to free a jcr
165 void b_free_jcr(char *file, int line, JCR *jcr)
167 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
171 void free_jcr(JCR *jcr)
173 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
178 jcr->use_count--; /* decrement use count */
179 Dmsg2(200, "Decrement jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
180 if (jcr->use_count > 0) { /* if in use */
182 Dmsg2(200, "jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
188 if (jcr->daemon_free_jcr) {
189 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
191 free_common_jcr(jcr);
194 close_msg(NULL); /* flush any daemon messages */
196 Dmsg0(200, "Exit free_jcr\n");
201 * Global routine to free a jcr
202 * JCR chain is already locked
204 void free_locked_jcr(JCR *jcr)
206 jcr->use_count--; /* decrement use count */
207 Dmsg2(200, "Decrement jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
208 if (jcr->use_count > 0) { /* if in use */
212 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
213 free_common_jcr(jcr);
220 * Given a JobId, find the JCR
221 * Returns: jcr on success
224 JCR *get_jcr_by_id(uint32_t JobId)
229 for (jcr = jobs; jcr; jcr=jcr->next) {
230 if (jcr->JobId == JobId) {
232 Dmsg2(200, "Increment jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
241 * Given a SessionId and SessionTime, find the JCR
242 * Returns: jcr on success
245 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
250 for (jcr = jobs; jcr; jcr=jcr->next) {
251 if (jcr->VolSessionId == SessionId &&
252 jcr->VolSessionTime == SessionTime) {
254 Dmsg2(200, "Increment jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
264 * Given a Job, find the JCR
265 * compares on the number of characters in Job
266 * thus allowing partial matches.
267 * Returns: jcr on success
270 JCR *get_jcr_by_partial_name(char *Job)
277 for (jcr = jobs; jcr; jcr=jcr->next) {
278 if (strncmp(Job, jcr->Job, len) == 0) {
280 Dmsg2(200, "Increment jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
290 * Given a Job, find the JCR
291 * requires an exact match of names.
292 * Returns: jcr on success
295 JCR *get_jcr_by_full_name(char *Job)
300 for (jcr = jobs; jcr; jcr=jcr->next) {
301 if (strcmp(jcr->Job, Job) == 0) {
303 Dmsg2(200, "Increment jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
311 void set_jcr_job_status(JCR *jcr, int JobStatus)
314 * For a set of errors, ... keep the current status
315 * so it isn't lost. For all others, set it.
317 switch (jcr->JobStatus) {
318 case JS_ErrorTerminated:
325 jcr->JobStatus = JobStatus;
332 void lock_jcr_chain()
340 void unlock_jcr_chain()
346 JCR *get_next_jcr(JCR *jcr)
357 Dmsg1(200, "Increment jcr use_count=%d\n", rjcr->use_count);