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