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);
65 strcpy(jcr->Job, "*Console*"); /* default */
67 sigtimer.sa_flags = 0;
68 sigtimer.sa_handler = timeout_handler;
69 sigfillset(&sigtimer.sa_mask);
70 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
85 * Remove a JCR from the chain
86 * NOTE! The chain must be locked prior to calling
89 static void remove_jcr(JCR *jcr)
91 Dmsg0(150, "Enter remove_jcr\n");
93 Emsg0(M_ABORT, 0, "NULL jcr.\n");
95 if (!jcr->prev) { /* if no prev */
96 jobs = jcr->next; /* set new head */
98 jcr->prev->next = jcr->next; /* update prev */
101 jcr->next->prev = jcr->prev;
103 Dmsg0(150, "Leave remove_jcr\n");
107 * Free stuff common to all JCRs
109 static void free_common_jcr(JCR *jcr)
111 /* Keep some statistics */
112 switch (jcr->JobType) {
117 last_job.JobType = jcr->JobType;
118 last_job.JobId = jcr->JobId;
119 last_job.VolSessionId = jcr->VolSessionId;
120 last_job.VolSessionTime = jcr->VolSessionTime;
121 strcpy(last_job.Job, jcr->Job);
122 last_job.JobFiles = jcr->JobFiles;
123 last_job.JobBytes = jcr->JobBytes;
124 last_job.JobStatus = jcr->JobStatus;
125 last_job.start_time = jcr->start_time;
126 last_job.end_time = time(NULL);
131 pthread_mutex_destroy(&jcr->mutex);
133 close_msg(jcr); /* close messages for this job */
135 /* do this after closing messages */
136 if (jcr->client_name) {
137 free_pool_memory(jcr->client_name);
138 jcr->client_name = NULL;
141 if (jcr->sd_auth_key) {
142 Dmsg0(200, "Free JCR sd_auth_key\n");
143 free(jcr->sd_auth_key);
144 jcr->sd_auth_key = NULL;
146 if (jcr->VolumeName) {
147 free_pool_memory(jcr->VolumeName);
148 jcr->VolumeName = NULL;
151 if (jcr->dir_bsock) {
152 bnet_close(jcr->dir_bsock);
153 jcr->dir_bsock = NULL;
156 free_pool_memory(jcr->errmsg);
163 * Global routine to free a jcr
166 void b_free_jcr(char *file, int line, JCR *jcr)
168 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
172 void free_jcr(JCR *jcr)
174 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
179 jcr->use_count--; /* decrement use count */
180 Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
181 if (jcr->use_count > 0) { /* if in use */
183 Dmsg2(200, "jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
189 if (jcr->daemon_free_jcr) {
190 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
192 free_common_jcr(jcr);
195 close_msg(NULL); /* flush any daemon messages */
197 Dmsg0(200, "Exit free_jcr\n");
202 * Global routine to free a jcr
203 * JCR chain is already locked
205 void free_locked_jcr(JCR *jcr)
207 jcr->use_count--; /* decrement use count */
208 Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
209 if (jcr->use_count > 0) { /* if in use */
213 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
214 free_common_jcr(jcr);
221 * Given a JobId, find the JCR
222 * Returns: jcr on success
225 JCR *get_jcr_by_id(uint32_t JobId)
230 for (jcr = jobs; jcr; jcr=jcr->next) {
231 if (jcr->JobId == JobId) {
233 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
242 * Given a SessionId and SessionTime, find the JCR
243 * Returns: jcr on success
246 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
251 for (jcr = jobs; jcr; jcr=jcr->next) {
252 if (jcr->VolSessionId == SessionId &&
253 jcr->VolSessionTime == SessionTime) {
255 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
265 * Given a Job, find the JCR
266 * compares on the number of characters in Job
267 * thus allowing partial matches.
268 * Returns: jcr on success
271 JCR *get_jcr_by_partial_name(char *Job)
278 for (jcr = jobs; jcr; jcr=jcr->next) {
279 if (strncmp(Job, jcr->Job, len) == 0) {
281 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
291 * Given a Job, find the JCR
292 * requires an exact match of names.
293 * Returns: jcr on success
296 JCR *get_jcr_by_full_name(char *Job)
301 for (jcr = jobs; jcr; jcr=jcr->next) {
302 if (strcmp(jcr->Job, Job) == 0) {
304 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
312 void set_jcr_job_status(JCR *jcr, int JobStatus)
315 * For a set of errors, ... keep the current status
316 * so it isn't lost. For all others, set it.
318 switch (jcr->JobStatus) {
319 case JS_ErrorTerminated:
326 jcr->JobStatus = JobStatus;
333 void lock_jcr_chain()
341 void unlock_jcr_chain()
347 JCR *get_next_jcr(JCR *jcr)
358 Dmsg1(200, "Inc jcr use_count=%d\n", rjcr->use_count);