2 * Manipulation routines for Job Control Records
4 * Kern E. Sibbald, December 2000
6 * These routines are thread safe.
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 struct s_last_job last_job; /* last job run by this daemon */
34 static JCR *jobs = NULL; /* pointer to JCR chain */
35 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
38 * Create a Job Control Record and link it into JCR chain
39 * Returns newly allocated JCR
40 * Note, since each daemon has a different JCR, he passes
43 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
47 Dmsg0(200, "Enter new_jcr\n");
48 jcr = (JCR *) malloc(size);
50 jcr->my_thread_id = pthread_self();
52 jcr->sched_time = time(NULL);
53 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
60 pthread_mutex_init(&(jcr->mutex), NULL);
61 jcr->JobStatus = JS_Created; /* ready to run */
62 jcr->VolumeName = (char *) get_pool_memory(PM_FNAME);
63 jcr->VolumeName[0] = 0;
64 jcr->errmsg = (char *) get_pool_memory(PM_MESSAGE);
66 init_msg(jcr); /* init job message chain */
74 * Remove a JCR from the chain
75 * NOTE! The chain must be locked prior to calling
78 static void remove_jcr(JCR *jcr)
80 Dmsg0(150, "Enter remove_jcr\n");
82 Emsg0(M_ABORT, 0, "NULL jcr.\n");
84 if (!jcr->prev) { /* if no prev */
85 jobs = jcr->next; /* set new head */
87 jcr->prev->next = jcr->next; /* update prev */
90 jcr->next->prev = jcr->prev;
92 Dmsg0(150, "Leave remove_jcr\n");
96 * Free stuff common to all JCRs
98 static void free_common_jcr(JCR *jcr)
100 /* Keep some statistics */
101 switch (jcr->JobType) {
106 last_job.JobType = jcr->JobType;
107 last_job.JobId = jcr->JobId;
108 last_job.VolSessionId = jcr->VolSessionId;
109 last_job.VolSessionTime = jcr->VolSessionTime;
110 strcpy(last_job.Job, jcr->Job);
111 last_job.JobFiles = jcr->JobFiles;
112 last_job.JobBytes = jcr->JobBytes;
113 last_job.JobStatus = jcr->JobStatus;
114 last_job.start_time = jcr->start_time;
115 last_job.end_time = time(NULL);
120 pthread_mutex_destroy(&jcr->mutex);
122 close_msg(jcr); /* close messages for this job */
124 /* do this after closing messages */
125 if (jcr->client_name) {
126 free(jcr->client_name);
127 jcr->client_name = NULL;
130 if (jcr->sd_auth_key) {
131 Dmsg0(200, "Free JCR sd_auth_key\n");
132 free(jcr->sd_auth_key);
133 jcr->sd_auth_key = NULL;
135 if (jcr->VolumeName) {
136 free_pool_memory(jcr->VolumeName);
137 jcr->VolumeName = NULL;
140 if (jcr->dir_bsock) {
141 bnet_close(jcr->dir_bsock);
142 jcr->dir_bsock = NULL;
145 free_pool_memory(jcr->errmsg);
152 * Global routine to free a jcr
154 void free_jcr(JCR *jcr)
156 Dmsg0(200, "Enter free_jcr\n");
158 jcr->use_count--; /* decrement use count */
159 Dmsg1(200, "Decrement jcr use_count=%d\n", jcr->use_count);
160 if (jcr->use_count > 0) { /* if in use */
162 Dmsg1(200, "jcr use_count=%d\n", jcr->use_count);
167 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
168 free_common_jcr(jcr);
169 Dmsg0(200, "Exit free_jcr\n");
174 * Global routine to free a jcr
175 * JCR chain is already locked
177 void free_locked_jcr(JCR *jcr)
179 jcr->use_count--; /* decrement use count */
180 Dmsg1(200, "Decrement jcr use_count=%d\n", jcr->use_count);
181 if (jcr->use_count > 0) { /* if in use */
185 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
186 free_common_jcr(jcr);
193 * Given a JobId, find the JCR
194 * Returns: jcr on success
197 JCR *get_jcr_by_id(uint32_t JobId)
202 for (jcr = jobs; jcr; jcr=jcr->next) {
203 if (jcr->JobId == JobId) {
205 Dmsg1(200, "Increment jcr use_count=%d\n", jcr->use_count);
216 * Given a Job, find the JCR
217 * compares on the number of characters in Job
218 * thus allowing partial matches.
219 * Returns: jcr on success
222 JCR *get_jcr_by_partial_name(char *Job)
229 for (jcr = jobs; jcr; jcr=jcr->next) {
230 if (strncmp(Job, jcr->Job, len) == 0) {
232 Dmsg1(200, "Increment jcr use_count=%d\n", jcr->use_count);
242 * Given a Job, find the JCR
243 * requires an exact match of names.
244 * Returns: jcr on success
247 JCR *get_jcr_by_full_name(char *Job)
252 for (jcr = jobs; jcr; jcr=jcr->next) {
253 if (strcmp(jcr->Job, Job) == 0) {
255 Dmsg1(200, "Increment jcr use_count=%d\n", jcr->use_count);
266 void lock_jcr_chain()
274 void unlock_jcr_chain()
280 JCR *get_next_jcr(JCR *jcr)
291 Dmsg1(200, "Increment jcr use_count=%d\n", rjcr->use_count);