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 struct s_last_job last_job; /* last job run by this daemon */
36 static JCR *jobs = NULL; /* pointer to JCR chain */
37 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
40 * Create a Job Control Record and link it into JCR chain
41 * Returns newly allocated JCR
42 * Note, since each daemon has a different JCR, he passes
45 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
49 Dmsg0(200, "Enter new_jcr\n");
50 jcr = (JCR *)malloc(size);
52 jcr->my_thread_id = pthread_self();
53 jcr->sched_time = time(NULL);
54 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
56 pthread_mutex_init(&(jcr->mutex), NULL);
57 jcr->JobStatus = JS_Created; /* ready to run */
58 jcr->VolumeName = get_pool_memory(PM_FNAME);
59 jcr->VolumeName[0] = 0;
60 jcr->errmsg = get_pool_memory(PM_MESSAGE);
76 * Remove a JCR from the chain
77 * NOTE! The chain must be locked prior to calling
80 static void remove_jcr(JCR *jcr)
82 Dmsg0(150, "Enter remove_jcr\n");
84 Emsg0(M_ABORT, 0, "NULL jcr.\n");
86 if (!jcr->prev) { /* if no prev */
87 jobs = jcr->next; /* set new head */
89 jcr->prev->next = jcr->next; /* update prev */
92 jcr->next->prev = jcr->prev;
94 Dmsg0(150, "Leave remove_jcr\n");
98 * Free stuff common to all JCRs
100 static void free_common_jcr(JCR *jcr)
102 /* Keep some statistics */
103 switch (jcr->JobType) {
108 last_job.JobType = jcr->JobType;
109 last_job.JobId = jcr->JobId;
110 last_job.VolSessionId = jcr->VolSessionId;
111 last_job.VolSessionTime = jcr->VolSessionTime;
112 strcpy(last_job.Job, jcr->Job);
113 last_job.JobFiles = jcr->JobFiles;
114 last_job.JobBytes = jcr->JobBytes;
115 last_job.JobStatus = jcr->JobStatus;
116 last_job.start_time = jcr->start_time;
117 last_job.end_time = time(NULL);
122 pthread_mutex_destroy(&jcr->mutex);
124 close_msg(jcr); /* close messages for this job */
126 /* do this after closing messages */
127 if (jcr->client_name) {
128 free_pool_memory(jcr->client_name);
129 jcr->client_name = NULL;
132 if (jcr->sd_auth_key) {
133 Dmsg0(200, "Free JCR sd_auth_key\n");
134 free(jcr->sd_auth_key);
135 jcr->sd_auth_key = NULL;
137 if (jcr->VolumeName) {
138 free_pool_memory(jcr->VolumeName);
139 jcr->VolumeName = NULL;
142 if (jcr->dir_bsock) {
143 bnet_close(jcr->dir_bsock);
144 jcr->dir_bsock = NULL;
147 free_pool_memory(jcr->errmsg);
154 * Global routine to free a jcr
157 void b_free_jcr(char *file, int line, JCR *jcr)
159 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
163 void free_jcr(JCR *jcr)
165 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
170 jcr->use_count--; /* decrement use count */
171 Dmsg2(200, "Decrement jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
172 if (jcr->use_count > 0) { /* if in use */
174 Dmsg2(200, "jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
180 if (jcr->daemon_free_jcr) {
181 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
183 free_common_jcr(jcr);
186 close_msg(NULL); /* flush any daemon messages */
188 Dmsg0(200, "Exit free_jcr\n");
193 * Global routine to free a jcr
194 * JCR chain is already locked
196 void free_locked_jcr(JCR *jcr)
198 jcr->use_count--; /* decrement use count */
199 Dmsg2(200, "Decrement jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
200 if (jcr->use_count > 0) { /* if in use */
204 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
205 free_common_jcr(jcr);
212 * Given a JobId, find the JCR
213 * Returns: jcr on success
216 JCR *get_jcr_by_id(uint32_t JobId)
221 for (jcr = jobs; jcr; jcr=jcr->next) {
222 if (jcr->JobId == JobId) {
224 Dmsg2(200, "Increment jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
233 * Given a SessionId and SessionTime, find the JCR
234 * Returns: jcr on success
237 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
242 for (jcr = jobs; jcr; jcr=jcr->next) {
243 if (jcr->VolSessionId == SessionId &&
244 jcr->VolSessionTime == SessionTime) {
246 Dmsg2(200, "Increment jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
256 * Given a Job, find the JCR
257 * compares on the number of characters in Job
258 * thus allowing partial matches.
259 * Returns: jcr on success
262 JCR *get_jcr_by_partial_name(char *Job)
269 for (jcr = jobs; jcr; jcr=jcr->next) {
270 if (strncmp(Job, jcr->Job, len) == 0) {
272 Dmsg2(200, "Increment jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
282 * Given a Job, find the JCR
283 * requires an exact match of names.
284 * Returns: jcr on success
287 JCR *get_jcr_by_full_name(char *Job)
292 for (jcr = jobs; jcr; jcr=jcr->next) {
293 if (strcmp(jcr->Job, Job) == 0) {
295 Dmsg2(200, "Increment jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
303 void set_jcr_job_status(JCR *jcr, int JobStatus)
306 * For a set of errors, ... keep the current status
307 * so it isn't lost. For all others, set it.
309 switch (jcr->JobStatus) {
310 case JS_ErrorTerminated:
317 jcr->JobStatus = JobStatus;
324 void lock_jcr_chain()
332 void unlock_jcr_chain()
338 JCR *get_next_jcr(JCR *jcr)
349 Dmsg1(200, "Increment jcr use_count=%d\n", rjcr->use_count);