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 mp_chr(jcr->VolumeName)[0] = 0;
63 jcr->errmsg = get_pool_memory(PM_MESSAGE);
64 mp_chr(jcr->errmsg)[0] = 0;
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. N.B. Be careful to include only
108 * generic stuff in the common part of the jcr.
110 static void free_common_jcr(JCR *jcr)
112 /* Keep some statistics */
113 switch (jcr->JobType) {
118 last_job.JobType = jcr->JobType;
119 last_job.JobId = jcr->JobId;
120 last_job.VolSessionId = jcr->VolSessionId;
121 last_job.VolSessionTime = jcr->VolSessionTime;
122 strcpy(last_job.Job, jcr->Job);
123 last_job.JobFiles = jcr->JobFiles;
124 last_job.JobBytes = jcr->JobBytes;
125 last_job.JobStatus = jcr->JobStatus;
126 last_job.start_time = jcr->start_time;
127 last_job.end_time = time(NULL);
132 pthread_mutex_destroy(&jcr->mutex);
134 close_msg(jcr); /* close messages for this job */
136 /* do this after closing messages */
137 if (jcr->client_name) {
138 free_pool_memory(jcr->client_name);
139 jcr->client_name = NULL;
142 if (jcr->sd_auth_key) {
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 if (jcr->cached_path) {
164 free_pool_memory(jcr->cached_path);
165 jcr->cached_path = NULL;
168 free_getuser_cache();
169 free_getgroup_cache();
174 * Global routine to free a jcr
177 void b_free_jcr(char *file, int line, JCR *jcr)
179 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
183 void free_jcr(JCR *jcr)
185 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
190 jcr->use_count--; /* decrement use count */
191 Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
192 if (jcr->use_count > 0) { /* if in use */
194 Dmsg2(200, "jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
200 if (jcr->daemon_free_jcr) {
201 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
203 free_common_jcr(jcr);
206 close_msg(NULL); /* flush any daemon messages */
208 Dmsg0(200, "Exit free_jcr\n");
213 * Global routine to free a jcr
214 * JCR chain is already locked
216 void free_locked_jcr(JCR *jcr)
218 jcr->use_count--; /* decrement use count */
219 Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
220 if (jcr->use_count > 0) { /* if in use */
224 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
225 free_common_jcr(jcr);
232 * Given a JobId, find the JCR
233 * Returns: jcr on success
236 JCR *get_jcr_by_id(uint32_t JobId)
241 for (jcr = jobs; jcr; jcr=jcr->next) {
242 if (jcr->JobId == JobId) {
244 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
253 * Given a SessionId and SessionTime, find the JCR
254 * Returns: jcr on success
257 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
262 for (jcr = jobs; jcr; jcr=jcr->next) {
263 if (jcr->VolSessionId == SessionId &&
264 jcr->VolSessionTime == SessionTime) {
266 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
276 * Given a Job, find the JCR
277 * compares on the number of characters in Job
278 * thus allowing partial matches.
279 * Returns: jcr on success
282 JCR *get_jcr_by_partial_name(char *Job)
289 for (jcr = jobs; jcr; jcr=jcr->next) {
290 if (strncmp(Job, jcr->Job, len) == 0) {
292 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
302 * Given a Job, find the JCR
303 * requires an exact match of names.
304 * Returns: jcr on success
307 JCR *get_jcr_by_full_name(char *Job)
312 for (jcr = jobs; jcr; jcr=jcr->next) {
313 if (strcmp(jcr->Job, Job) == 0) {
315 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
323 void set_jcr_job_status(JCR *jcr, int JobStatus)
326 * For a set of errors, ... keep the current status
327 * so it isn't lost. For all others, set it.
329 switch (jcr->JobStatus) {
330 case JS_ErrorTerminated:
337 jcr->JobStatus = JobStatus;
344 void lock_jcr_chain()
352 void unlock_jcr_chain()
358 JCR *get_next_jcr(JCR *jcr)
369 Dmsg1(200, "Inc jcr use_count=%d\n", rjcr->use_count);