]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/job.c
- Rework some of the autochanger data so that the DIR has
[bacula/bacula] / bacula / src / stored / job.c
1 /*
2  *   Job control and execution for Storage Daemon
3  *
4  *   Kern Sibbald, MM
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2005 Kern Sibbald
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "stored.h"
31
32 /* Imported variables */
33 extern uint32_t VolSessionTime;
34
35 /* Imported functions */
36 extern uint32_t newVolSessionId();
37
38 /* Forward referenced functions */
39 static bool use_device_cmd(JCR *jcr);
40
41 /* Requests from the Director daemon */
42 static char jobcmd[] = "JobId=%d job=%127s job_name=%127s client_name=%127s "
43       "type=%d level=%d FileSet=%127s NoAttr=%d SpoolAttr=%d FileSetMD5=%127s "
44       "SpoolData=%d  WritePartAfterJob=%d";
45 static char use_device[]  = "use device=%127s media_type=%127s "
46    "pool_name=%127s pool_type=%127s PoolId=%lld append=%d";
47 static char query_device[] = "query device=%127s";
48
49
50 /* Responses sent to Director daemon */
51 static char OKjob[]     = "3000 OK Job SDid=%u SDtime=%u Authorization=%s\n";
52 static char OK_device[] = "3000 OK use device device=%s\n";
53 static char NO_device[] = "3924 Device \"%s\" not in SD Device resources.\n";
54 static char NOT_open[]  = "3925 Device \"%s\" could not be opened or does not exist.\n";
55 static char BAD_use[]   = "3913 Bad use command: %s\n";
56 static char BAD_job[]   = "3915 Bad Job command: %s\n";
57 static char OK_query[]  = "3001 OK query\n";
58 static char NO_query[]  = "3918 Query failed\n";
59 static char BAD_query[] = "3917 Bad query command: %s\n";
60
61 /*
62  * Director requests us to start a job
63  * Basic tasks done here:
64  *  - We pickup the JobId to be run from the Director.
65  *  - We pickup the device, media, and pool from the Director
66  *  - Wait for a connection from the File Daemon (FD)
67  *  - Accept commands from the FD (i.e. run the job)
68  *  - Return when the connection is terminated or
69  *    there is an error.
70  */
71 bool job_cmd(JCR *jcr)
72 {
73    int JobId;
74    char auth_key[100];
75    BSOCK *dir = jcr->dir_bsock;
76    POOL_MEM job_name, client_name, job, fileset_name, fileset_md5;
77    int JobType, level, spool_attributes, no_attributes, spool_data, write_part_after_job;
78    JCR *ojcr;
79
80    /*
81     * Get JobId and permissions from Director
82     */
83    Dmsg1(100, "<dird: %s\n", dir->msg);
84    if (sscanf(dir->msg, jobcmd, &JobId, job.c_str(), job_name.c_str(),
85               client_name.c_str(),
86               &JobType, &level, fileset_name.c_str(), &no_attributes,
87               &spool_attributes, fileset_md5.c_str(), &spool_data, &write_part_after_job) != 12) {
88       pm_strcpy(jcr->errmsg, dir->msg);
89       bnet_fsend(dir, BAD_job, jcr->errmsg);
90       Dmsg1(100, ">dird: %s\n", dir->msg);
91       Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), jcr->errmsg);
92       set_jcr_job_status(jcr, JS_ErrorTerminated);
93       return false;
94    }
95    /*
96     * Since this job could be rescheduled, we
97     *  check to see if we have it already. If so
98     *  free the old jcr and use the new one.
99     */
100    ojcr = get_jcr_by_full_name(job.c_str());
101    if (ojcr && !ojcr->authenticated) {
102       Dmsg2(100, "Found ojcr=0x%x Job %s\n", (unsigned)(long)ojcr, job.c_str());
103       free_jcr(ojcr);
104    }
105    jcr->JobId = JobId;
106    jcr->VolSessionId = newVolSessionId();
107    jcr->VolSessionTime = VolSessionTime;
108    bstrncpy(jcr->Job, job, sizeof(jcr->Job));
109    unbash_spaces(job_name);
110    jcr->job_name = get_pool_memory(PM_NAME);
111    pm_strcpy(jcr->job_name, job_name);
112    unbash_spaces(client_name);
113    jcr->client_name = get_pool_memory(PM_NAME);
114    pm_strcpy(jcr->client_name, client_name);
115    unbash_spaces(fileset_name);
116    jcr->fileset_name = get_pool_memory(PM_NAME);
117    pm_strcpy(jcr->fileset_name, fileset_name);
118    jcr->JobType = JobType;
119    jcr->JobLevel = level;
120    jcr->no_attributes = no_attributes;
121    jcr->spool_attributes = spool_attributes;
122    jcr->spool_data = spool_data;
123    jcr->write_part_after_job = write_part_after_job;
124    jcr->fileset_md5 = get_pool_memory(PM_NAME);
125    pm_strcpy(jcr->fileset_md5, fileset_md5);
126
127    jcr->authenticated = false;
128
129    /*
130     * Pass back an authorization key for the File daemon
131     */
132    make_session_key(auth_key, NULL, 1);
133    bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
134    Dmsg1(100, ">dird: %s", dir->msg);
135    jcr->sd_auth_key = bstrdup(auth_key);
136    memset(auth_key, 0, sizeof(auth_key));
137    return true;
138 }
139
140 bool use_cmd(JCR *jcr) 
141 {
142    /*
143     * Wait for the device, media, and pool information
144     */
145    if (!use_device_cmd(jcr)) {
146       set_jcr_job_status(jcr, JS_ErrorTerminated);
147       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
148       return false;
149    }
150    return true;
151 }
152
153 bool run_cmd(JCR *jcr)
154 {
155    struct timeval tv;
156    struct timezone tz;
157    struct timespec timeout;
158    int errstat;
159
160    Dmsg1(100, "Run_cmd: %s\n", jcr->dir_bsock->msg);
161    /* The following jobs don't need the FD */
162    switch (jcr->JobType) {
163    case JT_MIGRATION:
164    case JT_COPY:
165    case JT_ARCHIVE:
166       jcr->authenticated = true;
167       run_job(jcr);
168       return false;
169    }
170
171    set_jcr_job_status(jcr, JS_WaitFD);          /* wait for FD to connect */
172    dir_send_job_status(jcr);
173
174    gettimeofday(&tv, &tz);
175    timeout.tv_nsec = tv.tv_usec * 1000;
176    timeout.tv_sec = tv.tv_sec + 30 * 60;        /* wait 30 minutes */
177
178    Dmsg1(100, "%s waiting on FD to contact SD\n", jcr->Job);
179    /*
180     * Wait for the File daemon to contact us to start the Job,
181     *  when he does, we will be released, unless the 30 minutes
182     *  expires.
183     */
184    P(jcr->mutex);
185    for ( ;!job_canceled(jcr); ) {
186       errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
187       if (errstat == 0 || errstat == ETIMEDOUT) {
188          break;
189       }
190    }
191    V(jcr->mutex);
192
193    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
194
195    if (jcr->authenticated && !job_canceled(jcr)) {
196       Dmsg1(100, "Running job %s\n", jcr->Job);
197       run_job(jcr);                   /* Run the job */
198    }
199    return false;
200 }
201
202 /*
203  * After receiving a connection (in job.c) if it is
204  *   from the File daemon, this routine is called.
205  */
206 void handle_filed_connection(BSOCK *fd, char *job_name)
207 {
208    JCR *jcr;
209
210    bmicrosleep(0, 50000);             /* wait 50 millisecs */
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       Dmsg1(100, "Job name not found: %s\n", job_name);
214       return;
215    }
216
217    jcr->file_bsock = fd;
218    jcr->file_bsock->jcr = jcr;
219
220    Dmsg1(110, "Found Job %s\n", job_name);
221
222    if (jcr->authenticated) {
223       Jmsg2(jcr, M_FATAL, 0, "Hey!!!! JobId %u Job %s already authenticated.\n",
224          jcr->JobId, jcr->Job);
225       free_jcr(jcr);
226       return;
227    }
228
229    /*
230     * Authenticate the File daemon
231     */
232    if (jcr->authenticated || !authenticate_filed(jcr)) {
233       Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
234       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
235    } else {
236       jcr->authenticated = true;
237       Dmsg1(110, "OK Authentication Job %s\n", jcr->Job);
238    }
239
240    P(jcr->mutex);
241    if (!jcr->authenticated) {
242       set_jcr_job_status(jcr, JS_ErrorTerminated);
243    }
244    pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
245    V(jcr->mutex);
246    free_jcr(jcr);
247    return;
248 }
249
250
251 /*
252  *   Use Device command from Director
253  *   He tells is what Device Name to use, the Media Type,
254  *      the Pool Name, and the Pool Type.
255  *
256  *    Ensure that the device exists and is opened, then store
257  *      the media and pool info in the JCR.
258  */
259 static bool use_device_cmd(JCR *jcr)
260 {
261    POOL_MEM dev_name, media_type, pool_name, pool_type;
262    BSOCK *dir = jcr->dir_bsock;
263    DEVRES *device;
264    uint64_t PoolId;
265    AUTOCHANGER *changer;
266    int append;
267    bool ok;
268
269    /*
270     * If there are multiple devices, the director sends us
271     *   use_device for each device that it wants to use.
272     */
273    Dmsg1(100, "<dird: %s", dir->msg);
274    ok = sscanf(dir->msg, use_device, dev_name.c_str(), media_type.c_str(),
275                pool_name.c_str(), pool_type.c_str(), &PoolId, &append) == 6;
276    if (ok) {
277       unbash_spaces(dev_name);
278       unbash_spaces(media_type);
279       unbash_spaces(pool_name);
280       unbash_spaces(pool_type);
281       LockRes();
282       foreach_res(device, R_DEVICE) {
283          /* Find resource, and make sure we were able to open it */
284          if (fnmatch(dev_name.c_str(), device->hdr.name, 0) == 0 &&
285              strcmp(device->media_type, media_type.c_str()) == 0) {
286             const int name_len = MAX_NAME_LENGTH;
287             DCR *dcr;
288             UnlockRes();
289             if (!device->dev) {
290                device->dev = init_dev(jcr, NULL, device);
291             }
292             if (!device->dev) {
293                Jmsg(jcr, M_WARNING, 0, _("\n"
294                   "     Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
295                     dev_name.c_str());
296                bnet_fsend(dir, NOT_open, dev_name.c_str());
297                Dmsg1(100, ">dird: %s\n", dir->msg);
298                return false;
299             }  
300             dcr = new_dcr(jcr, device->dev);
301             if (!dcr) {
302                bnet_fsend(dir, _("3926 Could not get dcr for device: %s\n"), dev_name.c_str());
303                Dmsg1(100, ">dird: %s\n", dir->msg);
304                return false;
305             }
306             Dmsg1(100, "Found device %s\n", device->hdr.name);
307             bstrncpy(dcr->pool_name, pool_name, name_len);
308             bstrncpy(dcr->pool_type, pool_type, name_len);
309             bstrncpy(dcr->media_type, media_type, name_len);
310             bstrncpy(dcr->dev_name, dev_name, name_len);
311             dcr->PoolId = PoolId;
312             jcr->dcr = dcr;
313             if (append == SD_APPEND) {
314                ok = reserve_device_for_append(jcr, device->dev);
315             } else {
316                ok = reserve_device_for_read(jcr, device->dev);
317             }
318             if (!ok) {
319                bnet_fsend(dir, _("3927 Could not reserve device: %s\n"), dev_name.c_str());
320                Dmsg1(100, ">dird: %s\n", dir->msg);
321                free_dcr(jcr->dcr);
322                return false;
323             }
324             Dmsg1(220, "Got: %s", dir->msg);
325             bash_spaces(dev_name);
326             ok = bnet_fsend(dir, OK_device, dev_name.c_str());
327             Dmsg1(100, ">dird: %s\n", dir->msg);
328             return ok;
329          }
330       }
331
332       foreach_res(changer, R_AUTOCHANGER) {
333          /* Find resource, and make sure we were able to open it */
334          if (fnmatch(dev_name.c_str(), changer->hdr.name, 0) == 0) {
335             const int name_len = MAX_NAME_LENGTH;
336             DCR *dcr;
337             /* Try each device in this AutoChanger */
338             foreach_alist(device, changer->device) {
339                Dmsg1(100, "Try changer device %s\n", device->hdr.name);
340                if (!device->dev) {
341                   device->dev = init_dev(jcr, NULL, device);
342                }
343                if (!device->dev) {
344                   Dmsg1(100, "Device %s could not be opened. Skipped\n", dev_name.c_str());
345                   Jmsg(jcr, M_WARNING, 0, _("\n"
346                      "     Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
347                        device->hdr.name, dev_name.c_str());
348                   continue;
349                }
350                if (!device->dev->autoselect) {
351                   continue;           /* device is not available */
352                }
353                dcr = new_dcr(jcr, device->dev);
354                if (!dcr) {
355                   bnet_fsend(dir, _("3926 Could not get dcr for device: %s\n"), dev_name.c_str());
356                   Dmsg1(100, ">dird: %s\n", dir->msg);
357                   UnlockRes();
358                   return false;
359                }
360                Dmsg1(100, "Found changer device %s\n", device->hdr.name);
361                bstrncpy(dcr->pool_name, pool_name, name_len);
362                bstrncpy(dcr->pool_type, pool_type, name_len);
363                bstrncpy(dcr->media_type, media_type, name_len);
364                bstrncpy(dcr->dev_name, dev_name, name_len);
365                jcr->dcr = dcr;
366                if (append == SD_APPEND) {
367                   ok = reserve_device_for_append(jcr, device->dev);
368                } else {
369                   ok = reserve_device_for_read(jcr, device->dev);
370                }
371                if (!ok) {
372                   Jmsg(jcr, M_WARNING, 0, _("Could not reserve device: %s\n"), dev_name.c_str());
373                   free_dcr(jcr->dcr);
374                   continue;
375                }
376                Dmsg1(100, "Device %s opened.\n", dev_name.c_str());
377                UnlockRes();
378                pm_strcpy(dev_name, device->hdr.name);
379                bash_spaces(dev_name);
380                ok = bnet_fsend(dir, OK_device, dev_name.c_str());
381                Dmsg1(100, ">dird: %s\n", dir->msg);
382                return ok;
383             }
384             break;                    /* we found it but could not open a device */
385          }
386       }
387
388       UnlockRes();
389       if (verbose) {
390          unbash_spaces(dir->msg);
391          pm_strcpy(jcr->errmsg, dir->msg);
392          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
393       }
394       Jmsg(jcr, M_FATAL, 0, _("\n"
395          "     Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
396            dev_name.c_str(), media_type.c_str());
397       bnet_fsend(dir, NO_device, dev_name.c_str());
398       Dmsg1(100, ">dird: %s\n", dir->msg);
399    } else {
400       unbash_spaces(dir->msg);
401       pm_strcpy(jcr->errmsg, dir->msg);
402       if (verbose) {
403          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
404       }
405       Jmsg(jcr, M_FATAL, 0, _("Bad Use Device command: %s\n"), jcr->errmsg);
406       bnet_fsend(dir, BAD_use, jcr->errmsg);
407       Dmsg1(100, ">dird: %s\n", dir->msg);
408    }
409
410    return false;                      /* ERROR return */
411 }
412
413 /*
414  *   Query Device command from Director
415  *   Sends Storage Daemon's information on the device to the
416  *    caller (presumably the Director).
417  *   This command always returns "true" so that the line is
418  *    not closed on an error.
419  *
420  */
421 bool query_cmd(JCR *jcr)
422 {
423    POOL_MEM dev_name, VolumeName, MediaType, ChangerName;
424    BSOCK *dir = jcr->dir_bsock;
425    DEVRES *device;
426    AUTOCHANGER *changer;
427    bool ok;
428
429    Dmsg1(100, "Query_cmd: %s", dir->msg);
430    ok = sscanf(dir->msg, query_device, dev_name.c_str()) == 1;
431    Dmsg1(100, "<dird: %s\n", dir->msg);
432    if (ok) {
433       unbash_spaces(dev_name);
434       LockRes();
435       foreach_res(device, R_DEVICE) {
436          /* Find resource, and make sure we were able to open it */
437          if (fnmatch(dev_name.c_str(), device->hdr.name, 0) == 0) {
438             if (!device->dev) {
439                device->dev = init_dev(jcr, NULL, device);
440             }
441             if (!device->dev) {
442                break;
443             }  
444             UnlockRes();
445             ok = dir_update_device(jcr, device->dev);
446             if (ok) {
447                ok = bnet_fsend(dir, OK_query);
448             } else {
449                bnet_fsend(dir, NO_query);
450             }
451             return ok;
452          }
453       }
454       foreach_res(changer, R_AUTOCHANGER) {
455          /* Find resource, and make sure we were able to open it */
456          if (fnmatch(dev_name.c_str(), changer->hdr.name, 0) == 0) {
457             UnlockRes();
458             if (!changer->device || changer->device->size() == 0) {
459                continue;              /* no devices */
460             }
461             ok = dir_update_changer(jcr, changer);
462             if (ok) {
463                ok = bnet_fsend(dir, OK_query);
464             } else {
465                bnet_fsend(dir, NO_query);
466             }
467             return ok;
468          }
469       }
470       /* If we get here, the device/autochanger was not found */
471       UnlockRes();
472       unbash_spaces(dir->msg);
473       pm_strcpy(jcr->errmsg, dir->msg);
474       bnet_fsend(dir, NO_device, dev_name.c_str());
475       Dmsg1(100, ">dird: %s\n", dir->msg);
476    } else {
477       unbash_spaces(dir->msg);
478       pm_strcpy(jcr->errmsg, dir->msg);
479       bnet_fsend(dir, BAD_query, jcr->errmsg);
480       Dmsg1(100, ">dird: %s\n", dir->msg);
481    }
482
483    return true;
484 }
485
486
487 /*
488  * Destroy the Job Control Record and associated
489  * resources (sockets).
490  */
491 void stored_free_jcr(JCR *jcr)
492 {
493    if (jcr->file_bsock) {
494       bnet_close(jcr->file_bsock);
495       jcr->file_bsock = NULL;
496    }
497    if (jcr->job_name) {
498       free_pool_memory(jcr->job_name);
499    }
500    if (jcr->client_name) {
501       free_memory(jcr->client_name);
502       jcr->client_name = NULL;
503    }
504    if (jcr->fileset_name) {
505       free_memory(jcr->fileset_name);
506    }
507    if (jcr->fileset_md5) {
508       free_memory(jcr->fileset_md5);
509    }
510    if (jcr->bsr) {
511       free_bsr(jcr->bsr);
512       jcr->bsr = NULL;
513    }
514    if (jcr->RestoreBootstrap) {
515       unlink(jcr->RestoreBootstrap);
516       free_pool_memory(jcr->RestoreBootstrap);
517       jcr->RestoreBootstrap = NULL;
518    }
519    if (jcr->next_dev || jcr->prev_dev) {
520       Emsg0(M_FATAL, 0, _("In free_jcr(), but still attached to device!!!!\n"));
521    }
522    pthread_cond_destroy(&jcr->job_start_wait);
523    if (jcr->dcrs) {
524       delete jcr->dcrs;
525    }
526    jcr->dcrs = NULL;
527    if (jcr->dcr) {
528       free_dcr(jcr->dcr);
529       jcr->dcr = NULL;
530    }
531    return;
532 }