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);
73 * Remove a JCR from the chain
74 * NOTE! The chain must be locked prior to calling
77 static void remove_jcr(JCR *jcr)
79 Dmsg0(150, "Enter remove_jcr\n");
81 Emsg0(M_ABORT, 0, "NULL jcr.\n");
83 if (!jcr->prev) { /* if no prev */
84 jobs = jcr->next; /* set new head */
86 jcr->prev->next = jcr->next; /* update prev */
89 jcr->next->prev = jcr->prev;
91 Dmsg0(150, "Leave remove_jcr\n");
95 * Free stuff common to all JCRs
97 static void free_common_jcr(JCR *jcr)
99 /* Keep some statistics */
100 switch (jcr->JobType) {
105 last_job.JobType = jcr->JobType;
106 last_job.JobId = jcr->JobId;
107 last_job.VolSessionId = jcr->VolSessionId;
108 last_job.VolSessionTime = jcr->VolSessionTime;
109 strcpy(last_job.Job, jcr->Job);
110 last_job.JobFiles = jcr->JobFiles;
111 last_job.JobBytes = jcr->JobBytes;
112 last_job.JobStatus = jcr->JobStatus;
113 last_job.start_time = jcr->start_time;
114 last_job.end_time = time(NULL);
119 pthread_mutex_destroy(&jcr->mutex);
121 close_msg(jcr); /* close messages for this job */
123 /* do this after closing messages */
124 if (jcr->client_name) {
125 free(jcr->client_name);
126 jcr->client_name = NULL;
129 if (jcr->sd_auth_key) {
130 Dmsg0(200, "Free JCR sd_auth_key\n");
131 free(jcr->sd_auth_key);
132 jcr->sd_auth_key = NULL;
134 if (jcr->VolumeName) {
135 free_pool_memory(jcr->VolumeName);
136 jcr->VolumeName = NULL;
139 if (jcr->dir_bsock) {
140 bnet_close(jcr->dir_bsock);
141 jcr->dir_bsock = NULL;
144 free_pool_memory(jcr->errmsg);
151 * Global routine to free a jcr
153 void free_jcr(JCR *jcr)
155 Dmsg0(200, "Enter free_jcr\n");
157 jcr->use_count--; /* decrement use count */
158 Dmsg1(200, "Decrement jcr use_count=%d\n", jcr->use_count);
159 if (jcr->use_count > 0) { /* if in use */
161 Dmsg1(200, "jcr use_count=%d\n", jcr->use_count);
166 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
167 free_common_jcr(jcr);
168 Dmsg0(200, "Exit free_jcr\n");
173 * Global routine to free a jcr
174 * JCR chain is already locked
176 void free_locked_jcr(JCR *jcr)
178 jcr->use_count--; /* decrement use count */
179 Dmsg1(200, "Decrement jcr use_count=%d\n", jcr->use_count);
180 if (jcr->use_count > 0) { /* if in use */
184 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
185 free_common_jcr(jcr);
192 * Given a JobId, find the JCR
193 * Returns: jcr on success
196 JCR *get_jcr_by_id(uint32_t JobId)
201 for (jcr = jobs; jcr; jcr=jcr->next) {
202 if (jcr->JobId == JobId) {
204 Dmsg1(200, "Increment jcr use_count=%d\n", jcr->use_count);
215 * Given a Job, find the JCR
216 * compares on the number of characters in Job
217 * thus allowing partial matches.
218 * Returns: jcr on success
221 JCR *get_jcr_by_partial_name(char *Job)
228 for (jcr = jobs; jcr; jcr=jcr->next) {
229 if (strncmp(Job, jcr->Job, len) == 0) {
231 Dmsg1(200, "Increment jcr use_count=%d\n", jcr->use_count);
241 * Given a Job, find the JCR
242 * requires an exact match of names.
243 * Returns: jcr on success
246 JCR *get_jcr_by_full_name(char *Job)
251 for (jcr = jobs; jcr; jcr=jcr->next) {
252 if (strcmp(jcr->Job, Job) == 0) {
254 Dmsg1(200, "Increment jcr use_count=%d\n", jcr->use_count);
265 void lock_jcr_chain()
273 void unlock_jcr_chain()
279 JCR *get_next_jcr(JCR *jcr)
290 Dmsg1(200, "Increment jcr use_count=%d\n", rjcr->use_count);