]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/job.c
change all void *jcr into JCR *jcr
[bacula/bacula] / bacula / src / stored / job.c
1 /*
2  *   Job control and execution for Storage Daemon
3  *
4  *   Version $Id$
5  *
6  */
7 /*
8    Copyright (C) 2000-2003 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29
30 /* Imported variables */
31 extern uint32_t VolSessionTime;
32
33 /* Imported functions */
34 extern uint32_t newVolSessionId();
35
36 /* Forward referenced functions */
37 static int use_device_cmd(JCR *jcr);
38
39 /* Requests from the Director daemon */
40 static char jobcmd[]     = "JobId=%d job=%127s job_name=%127s client_name=%127s \
41 type=%d level=%d FileSet=%127s NoAttr=%d SpoolAttr=%d FileSetMD5=%127s\n";
42 static char use_device[] = "use device=%s media_type=%s pool_name=%s pool_type=%s\n";
43
44 /* Responses sent to Director daemon */
45 static char OKjob[]     = "3000 OK Job SDid=%u SDtime=%u Authorization=%s\n";
46 static char OK_device[] = "3000 OK use device\n";
47 static char NO_device[] = "3914 Device \"%s\" not in SD Device resources.\n";
48 static char BAD_use[]   = "3913 Bad use command: %s\n";
49 static char BAD_job[]   = "3915 Bad Job command: %s\n";
50
51
52
53 /*
54  * Director requests us to start a job
55  * Basic tasks done here:
56  *  - We pickup the JobId to be run from the Director.
57  *  - We pickup the device, media, and pool from the Director
58  *  - Wait for a connection from the File Daemon (FD)
59  *  - Accept commands from the FD (i.e. run the job)
60  *  - Return when the connection is terminated or
61  *    there is an error.
62  */
63 int job_cmd(JCR *jcr)
64 {
65    int JobId, errstat;
66    char auth_key[100];
67    BSOCK *dir = jcr->dir_bsock;
68    POOLMEM *job_name, *client_name, *job, *fileset_name, *fileset_md5;
69    int JobType, level, spool_attributes, no_attributes;
70    struct timeval tv;
71    struct timezone tz;
72    struct timespec timeout;
73
74    /*
75     * Get JobId and permissions from Director
76     */
77
78    Dmsg1(130, "Job_cmd: %s\n", dir->msg);
79    job = get_memory(dir->msglen);
80    job_name = get_memory(dir->msglen);
81    client_name = get_memory(dir->msglen);
82    fileset_name = get_memory(dir->msglen);
83    fileset_md5 = get_memory(dir->msglen);
84    if (sscanf(dir->msg, jobcmd, &JobId, job, job_name, client_name,
85               &JobType, &level, fileset_name, &no_attributes,
86               &spool_attributes, fileset_md5) != 10) {
87       pm_strcpy(&jcr->errmsg, dir->msg);
88       bnet_fsend(dir, BAD_job, jcr->errmsg);
89       Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), jcr->errmsg);
90       free_memory(job);
91       free_memory(job_name);
92       free_memory(client_name);
93       free_memory(fileset_name);
94       free_memory(fileset_md5);
95       set_jcr_job_status(jcr, JS_ErrorTerminated);
96       return 0;
97    }
98    jcr->JobId = JobId;
99    jcr->VolSessionId = newVolSessionId();
100    jcr->VolSessionTime = VolSessionTime;
101    strcpy(jcr->Job, job);
102    unbash_spaces(job_name);
103    jcr->job_name = get_memory(strlen(job_name) + 1);
104    strcpy(jcr->job_name, job_name);
105    unbash_spaces(client_name);
106    jcr->client_name = get_memory(strlen(client_name) + 1);
107    strcpy(jcr->client_name, client_name);
108    unbash_spaces(fileset_name);
109    jcr->fileset_name = get_memory(strlen(fileset_name) + 1);
110    strcpy(jcr->fileset_name, fileset_name);
111    jcr->JobType = JobType;
112    jcr->JobLevel = level;
113    jcr->no_attributes = no_attributes;
114    jcr->spool_attributes = spool_attributes;
115    jcr->fileset_md5 = get_memory(strlen(fileset_md5) + 1);
116    strcpy(jcr->fileset_md5, fileset_md5);
117    free_memory(job);
118    free_memory(job_name);
119    free_memory(client_name);
120    free_memory(fileset_name);
121    free_memory(fileset_md5);
122
123    /* Initialize FD start condition variable */
124    if ((errstat = pthread_cond_init(&jcr->job_start_wait, NULL)) != 0) {
125       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
126       set_jcr_job_status(jcr, JS_ErrorTerminated);
127       return 0;
128    }
129    jcr->authenticated = FALSE;
130
131    /*
132     * Pass back an authorization key for the File daemon
133     */
134 #ifdef Old_way_not_so_good
135    gettimeofday(&tv, &tz);
136    srandom(tv.tv_usec + tv.tv_sec);
137    sprintf(auth_key, "%ld", (long)random());
138 #endif
139    makeSessionKey(auth_key, NULL, 1);
140    bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
141    Dmsg1(110, ">dird: %s", dir->msg);
142    jcr->sd_auth_key = bstrdup(auth_key);
143
144    /*
145     * Wait for the device, media, and pool information
146     */
147    if (!use_device_cmd(jcr)) {
148       set_jcr_job_status(jcr, JS_ErrorTerminated);
149       return 0;
150    }
151
152    set_jcr_job_status(jcr, JS_WaitFD);          /* wait for FD to connect */
153    dir_send_job_status(jcr);
154
155    gettimeofday(&tv, &tz);
156    timeout.tv_nsec = tv.tv_usec * 1000; 
157    timeout.tv_sec = tv.tv_sec + 30 * 60;        /* wait 30 minutes */
158
159
160    Dmsg1(200, "%s waiting on job_start_wait\n", jcr->Job);
161    /* Wait for the File daemon to contact us to start the Job,
162     *  when he does, we will be released, unless the 30 minutes
163     *  expires.
164     */
165    P(jcr->mutex);
166    for ( ;!job_canceled(jcr); ) {
167       errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
168       if (errstat == 0 || errstat == ETIMEDOUT) {
169          break;
170       }
171    }
172    V(jcr->mutex);
173
174    if (jcr->authenticated && !job_canceled(jcr)) {
175       run_job(jcr);                   /* Run the job */
176    }
177    return 0;
178 }
179
180 /*
181  * This entry point is only called if we have a separate
182  *   Storage Daemon Data port. Otherwise, the connection
183  *   is made to the main port, and if it is a File daemon
184  *   calling, handl_filed_connection() is called directly.
185  */
186 void connection_from_filed(void *arg)
187 {
188    BSOCK *fd = (BSOCK *)arg;
189    char job_name[MAX_NAME_LENGTH];
190
191    Dmsg0(110, "enter connection_from_filed\n");
192    if (bnet_recv(fd) <= 0) {
193       Emsg0(M_FATAL, 0, _("Unable to authenticate Client.\n"));
194       return;
195    }
196    Dmsg1(100, "got: %s\n", fd->msg);
197
198    if (fd->msglen < 17 || fd->msglen > 17+127 ||
199        sscanf(fd->msg, "Hello Start Job %127s\n", job_name) != 1) {
200       Emsg1(M_FATAL, 0, _("Bad Hello from FD: %s\n"), fd->msg);
201       return;
202    }
203    handle_filed_connection(fd, job_name);
204    return;
205 }
206
207 void handle_filed_connection(BSOCK *fd, char *job_name)
208 {
209    JCR *jcr;
210
211    if (!(jcr=get_jcr_by_full_name(job_name))) {
212       Jmsg1(NULL, M_FATAL, 0, _("Job name not found: %s\n"), job_name);
213       return;
214    }
215
216    jcr->file_bsock = fd;
217    jcr->file_bsock->jcr = jcr;
218
219    Dmsg1(110, "Found Job %s\n", job_name);
220
221    if (jcr->authenticated) {
222       Pmsg2(000, "Hey!!!! JobId %u Job %s already authenticated.\n", 
223          jcr->JobId, jcr->Job);
224    }
225   
226    /*
227     * Authenticate the File daemon
228     */
229    if (jcr->authenticated || !authenticate_filed(jcr)) {
230       Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
231       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
232    } else {
233       jcr->authenticated = TRUE;
234       Dmsg1(110, "OK Authentication Job %s\n", jcr->Job);
235    }
236
237    P(jcr->mutex);
238    if (!jcr->authenticated) {
239       jcr->JobStatus = JS_ErrorTerminated;
240    }
241    pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
242    V(jcr->mutex);
243    free_jcr(jcr);
244    return;
245 }
246
247
248 /*  
249  *   Use Device command from Director
250  *   He tells is what Device Name to use, the Media Type, 
251  *      the Pool Name, and the Pool Type.
252  *
253  *    Ensure that the device exists and is opened, then store
254  *      the media and pool info in the JCR.
255  */
256 static int use_device_cmd(JCR *jcr)
257 {
258    POOLMEM *dev_name, *media_type, *pool_name, *pool_type;
259    BSOCK *dir = jcr->dir_bsock;
260    DEVRES *device;
261
262    if (bnet_recv(dir) <= 0) {
263       Jmsg0(jcr, M_FATAL, 0, _("No Device from Director\n"));
264       return 0;
265    }
266    
267    Dmsg1(120, "Use device: %s", dir->msg);
268    dev_name = get_memory(dir->msglen);
269    media_type = get_memory(dir->msglen);
270    pool_name = get_memory(dir->msglen);
271    pool_type = get_memory(dir->msglen);
272    if (sscanf(dir->msg, use_device, dev_name, media_type, pool_name, pool_type) == 4) {
273       unbash_spaces(dev_name);
274       unbash_spaces(media_type);
275       unbash_spaces(pool_name);
276       unbash_spaces(pool_type);
277       device = NULL;
278       LockRes();
279       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
280          /* Find resource, and make sure we were able to open it */
281          if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
282             Dmsg1(120, "Found device %s\n", device->hdr.name);
283             jcr->pool_name = get_memory(strlen(pool_name) + 1);
284             strcpy(jcr->pool_name, pool_name);
285             jcr->pool_type = get_memory(strlen(pool_type) + 1);
286             strcpy(jcr->pool_type, pool_type);
287             jcr->media_type = get_memory(strlen(media_type) + 1);
288             strcpy(jcr->media_type, media_type);
289             jcr->dev_name = get_memory(strlen(dev_name) + 1);
290             strcpy(jcr->dev_name, dev_name);
291             jcr->device = device;
292             Dmsg4(120, use_device, dev_name, media_type, pool_name, pool_type);
293             free_memory(dev_name);
294             free_memory(media_type);
295             free_memory(pool_name);
296             free_memory(pool_type);
297             UnlockRes();
298             return bnet_fsend(dir, OK_device);
299          }
300       }
301       UnlockRes();
302       if (verbose) {
303          unbash_spaces(dir->msg);
304          pm_strcpy(&jcr->errmsg, dir->msg);
305          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
306       }
307       Jmsg(jcr, M_FATAL, 0, _("\n"
308          "     Device \"%s\" requested by Dir not found in SD Device resources.\n"),
309            dev_name);
310       bnet_fsend(dir, NO_device, dev_name);
311    } else {
312       unbash_spaces(dir->msg);
313       pm_strcpy(&jcr->errmsg, dir->msg);
314       if (verbose) {
315          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
316       }
317       Jmsg(jcr, M_FATAL, 0, _("Bad Use Device command: %s\n"), jcr->errmsg);
318       bnet_fsend(dir, BAD_use, jcr->errmsg);
319    }
320
321    free_memory(dev_name);
322    free_memory(media_type);
323    free_memory(pool_name);
324    free_memory(pool_type);
325    return 0;                          /* ERROR return */
326 }
327
328 /* 
329  * Destroy the Job Control Record and associated
330  * resources (sockets).
331  */
332 void stored_free_jcr(JCR *jcr) 
333 {
334    if (jcr->file_bsock) {
335       bnet_close(jcr->file_bsock);
336       jcr->file_bsock = NULL;
337    }
338    if (jcr->pool_name) {
339       free_memory(jcr->pool_name);
340    }
341    if (jcr->pool_type) {
342       free_memory(jcr->pool_type);
343    }
344    if (jcr->media_type) {
345       free_memory(jcr->media_type);
346    }
347    if (jcr->dev_name) {
348       free_memory(jcr->dev_name);
349    }
350    if (jcr->job_name) {
351       free_pool_memory(jcr->job_name);
352    }
353    if (jcr->client_name) {
354       free_memory(jcr->client_name);
355       jcr->client_name = NULL;
356    }
357    if (jcr->fileset_name) {
358       free_memory(jcr->fileset_name);
359    }
360    if (jcr->fileset_md5) {
361       free_memory(jcr->fileset_md5);
362    }
363    if (jcr->bsr) {
364       free_bsr(jcr->bsr);
365       jcr->bsr = NULL;
366    }
367    if (jcr->RestoreBootstrap) {
368       unlink(jcr->RestoreBootstrap);
369       free_pool_memory(jcr->RestoreBootstrap);
370       jcr->RestoreBootstrap = NULL;
371    }
372    if (jcr->next_dev && jcr->prev_dev) {
373       Emsg0(M_FATAL, 0, _("In free_jcr(), but still attached to device!!!!\n"));
374    }
375                       
376    return;
377 }