]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/job.c
- Corrected some typos in the make_xxx_tables.in files.
[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 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\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 append=%d read=%d num_writers=%d "
58    "num_waiting=%d open=%d use_count=%d labeled=%d offline=%d autochanger=%d "
59    "media_type=%s volume_name=%s";
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, write_part_after_job;
79    JCR *ojcr;
80
81    /*
82     * Get JobId and permissions from Director
83     */
84    Dmsg1(100, "Job_cmd: %s\n", dir->msg);
85    if (sscanf(dir->msg, jobcmd, &JobId, job.c_str(), job_name.c_str(),
86               client_name.c_str(),
87               &JobType, &level, fileset_name.c_str(), &no_attributes,
88               &spool_attributes, fileset_md5.c_str(), &spool_data, &write_part_after_job) != 12) {
89       pm_strcpy(jcr->errmsg, dir->msg);
90       bnet_fsend(dir, BAD_job, jcr->errmsg);
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(110, ">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    /* The following jobs don't need the FD */
161    switch (jcr->JobType) {
162    case JT_MIGRATION:
163    case JT_COPY:
164    case JT_ARCHIVE:
165       jcr->authenticated = true;
166       run_job(jcr);
167       return false;
168    }
169
170    set_jcr_job_status(jcr, JS_WaitFD);          /* wait for FD to connect */
171    dir_send_job_status(jcr);
172
173    gettimeofday(&tv, &tz);
174    timeout.tv_nsec = tv.tv_usec * 1000;
175    timeout.tv_sec = tv.tv_sec + 30 * 60;        /* wait 30 minutes */
176
177    Dmsg1(100, "%s waiting on FD to contact SD\n", jcr->Job);
178    /*
179     * Wait for the File daemon to contact us to start the Job,
180     *  when he does, we will be released, unless the 30 minutes
181     *  expires.
182     */
183    P(jcr->mutex);
184    for ( ;!job_canceled(jcr); ) {
185       errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
186       if (errstat == 0 || errstat == ETIMEDOUT) {
187          break;
188       }
189    }
190    V(jcr->mutex);
191
192    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
193
194    if (jcr->authenticated && !job_canceled(jcr)) {
195       Dmsg1(100, "Running job %s\n", jcr->Job);
196       run_job(jcr);                   /* Run the job */
197    }
198    return false;
199 }
200
201 /*
202  * After receiving a connection (in job.c) if it is
203  *   from the File daemon, this routine is called.
204  */
205 void handle_filed_connection(BSOCK *fd, char *job_name)
206 {
207    JCR *jcr;
208
209    bmicrosleep(0, 50000);             /* wait 50 millisecs */
210    if (!(jcr=get_jcr_by_full_name(job_name))) {
211       Jmsg1(NULL, M_FATAL, 0, _("Job name not found: %s\n"), job_name);
212       Dmsg1(100, "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       Jmsg2(jcr, M_FATAL, 0, "Hey!!!! JobId %u Job %s already authenticated.\n",
223          jcr->JobId, jcr->Job);
224       free_jcr(jcr);
225       return;
226    }
227
228    /*
229     * Authenticate the File daemon
230     */
231    if (jcr->authenticated || !authenticate_filed(jcr)) {
232       Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
233       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
234    } else {
235       jcr->authenticated = true;
236       Dmsg1(110, "OK Authentication Job %s\n", jcr->Job);
237    }
238
239    P(jcr->mutex);
240    if (!jcr->authenticated) {
241       set_jcr_job_status(jcr, JS_ErrorTerminated);
242    }
243    pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
244    V(jcr->mutex);
245    free_jcr(jcr);
246    return;
247 }
248
249
250 /*
251  *   Use Device command from Director
252  *   He tells is what Device Name to use, the Media Type,
253  *      the Pool Name, and the Pool Type.
254  *
255  *    Ensure that the device exists and is opened, then store
256  *      the media and pool info in the JCR.
257  */
258 static bool use_device_cmd(JCR *jcr)
259 {
260    POOL_MEM dev_name, media_type, pool_name, pool_type;
261    BSOCK *dir = jcr->dir_bsock;
262    DEVRES *device;
263    int append;
264    bool ok;
265
266    Dmsg1(120, "Use device: %s", dir->msg);
267    /*
268     * If there are multiple devices, the director sends us
269     *   use_device for each device that it wants to use.
270     */
271    ok = sscanf(dir->msg, use_device, dev_name.c_str(), media_type.c_str(),
272                pool_name.c_str(), pool_type.c_str(), &append) == 5;
273    if (ok) {
274       unbash_spaces(dev_name);
275       unbash_spaces(media_type);
276       unbash_spaces(pool_name);
277       unbash_spaces(pool_type);
278       LockRes();
279       foreach_res(device, R_DEVICE) {
280          /* Find resource, and make sure we were able to open it */
281          if (fnmatch(dev_name.c_str(), device->hdr.name, 0) == 0 &&
282              strcmp(device->media_type, media_type.c_str()) == 0) {
283             const int name_len = MAX_NAME_LENGTH;
284             DCR *dcr;
285             UnlockRes();
286             if (!device->dev) {
287                device->dev = init_dev(jcr, NULL, device);
288             }
289             if (!device->dev) {
290                Jmsg(jcr, M_FATAL, 0, _("\n"
291                   "     Archive \"%s\" requested by DIR could not be opened or does not exist.\n"),
292                     dev_name.c_str());
293                bnet_fsend(dir, NOT_open, dev_name.c_str());
294                return false;
295             }  
296             dcr = new_dcr(jcr, device->dev);
297             if (!dcr) {
298                bnet_fsend(dir, _("3926 Could not get dcr for device: %s\n"), dev_name.c_str());
299                return false;
300             }
301             Dmsg1(120, "Found device %s\n", device->hdr.name);
302             bstrncpy(dcr->pool_name, pool_name, name_len);
303             bstrncpy(dcr->pool_type, pool_type, name_len);
304             bstrncpy(dcr->media_type, media_type, name_len);
305             bstrncpy(dcr->dev_name, dev_name, name_len);
306             jcr->dcr = dcr;
307             if (append == SD_APPEND) {
308                ok = reserve_device_for_append(jcr, device->dev);
309             } else {
310                ok = reserve_device_for_read(jcr, device->dev);
311             }
312             if (!ok) {
313                bnet_fsend(dir, _("3927 Could not reserve device: %s\n"), dev_name.c_str());
314                free_dcr(jcr->dcr);
315                return false;
316             }
317             Dmsg1(220, "Got: %s", dir->msg);
318             return bnet_fsend(dir, OK_device);
319          }
320       }
321       UnlockRes();
322       if (verbose) {
323          unbash_spaces(dir->msg);
324          pm_strcpy(jcr->errmsg, dir->msg);
325          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
326       }
327       Jmsg(jcr, M_FATAL, 0, _("\n"
328          "     Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
329            dev_name.c_str(), media_type.c_str());
330       bnet_fsend(dir, NO_device, dev_name.c_str());
331    } else {
332       unbash_spaces(dir->msg);
333       pm_strcpy(jcr->errmsg, dir->msg);
334       if (verbose) {
335          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
336       }
337       Jmsg(jcr, M_FATAL, 0, _("Bad Use Device command: %s\n"), jcr->errmsg);
338       bnet_fsend(dir, BAD_use, jcr->errmsg);
339    }
340
341    return false;                      /* ERROR return */
342 }
343
344 /*
345  *   Query Device command from Director
346  *   Sends Storage Daemon's information on the device to the
347  *    caller (presumably the Director).
348  *   This command always returns "true" so that the line is
349  *    not closed on an error.
350  *
351  */
352 bool query_cmd(JCR *jcr)
353 {
354    POOL_MEM dev_name;
355    BSOCK *dir = jcr->dir_bsock;
356    DEVRES *device;
357    bool ok;
358
359    Dmsg1(120, "Query: %s", dir->msg);
360
361    ok = sscanf(dir->msg, query_device, dev_name.c_str()) == 1;
362    if (ok) {
363       unbash_spaces(dev_name);
364       LockRes();
365       foreach_res(device, R_DEVICE) {
366          /* Find resource, and make sure we were able to open it */
367          if (fnmatch(dev_name.c_str(), device->hdr.name, 0) == 0) {
368             if (!device->dev) {
369                device->dev = init_dev(jcr, NULL, device);
370             }
371             if (!device->dev) {
372                break;
373             }  
374             DEVICE *dev = device->dev;
375             POOL_MEM VolumeName, MediaType;
376             UnlockRes();
377             if (dev->is_labeled()) {
378                pm_strcpy(VolumeName, dev->VolHdr.VolName);
379             } else {
380                pm_strcpy(VolumeName, "");
381             }
382             bash_spaces(VolumeName);
383             pm_strcpy(MediaType, device->media_type);
384             bash_spaces(MediaType);
385             return bnet_fsend(dir, OK_query, dev->can_append()!=0,
386                dev->can_read()!=0, dev->num_writers, dev->num_waiting,
387                dev->is_open()!=0, dev->use_count, dev->is_labeled()!=0,
388                dev->is_offline()!=0, device->changer_res!=NULL, 
389                MediaType.c_str(), VolumeName.c_str());
390          }
391       }
392       UnlockRes();
393       unbash_spaces(dir->msg);
394       pm_strcpy(jcr->errmsg, dir->msg);
395       bnet_fsend(dir, NO_device, dev_name.c_str());
396    } else {
397       unbash_spaces(dir->msg);
398       pm_strcpy(jcr->errmsg, dir->msg);
399       bnet_fsend(dir, BAD_query, jcr->errmsg);
400    }
401    return true;
402 }
403
404
405 /*
406  * Destroy the Job Control Record and associated
407  * resources (sockets).
408  */
409 void stored_free_jcr(JCR *jcr)
410 {
411    if (jcr->file_bsock) {
412       bnet_close(jcr->file_bsock);
413       jcr->file_bsock = NULL;
414    }
415    if (jcr->job_name) {
416       free_pool_memory(jcr->job_name);
417    }
418    if (jcr->client_name) {
419       free_memory(jcr->client_name);
420       jcr->client_name = NULL;
421    }
422    if (jcr->fileset_name) {
423       free_memory(jcr->fileset_name);
424    }
425    if (jcr->fileset_md5) {
426       free_memory(jcr->fileset_md5);
427    }
428    if (jcr->bsr) {
429       free_bsr(jcr->bsr);
430       jcr->bsr = NULL;
431    }
432    if (jcr->RestoreBootstrap) {
433       unlink(jcr->RestoreBootstrap);
434       free_pool_memory(jcr->RestoreBootstrap);
435       jcr->RestoreBootstrap = NULL;
436    }
437    if (jcr->next_dev || jcr->prev_dev) {
438       Emsg0(M_FATAL, 0, _("In free_jcr(), but still attached to device!!!!\n"));
439    }
440    pthread_cond_destroy(&jcr->job_start_wait);
441    if (jcr->dcr) {
442       free_dcr(jcr->dcr);
443       jcr->dcr = NULL;
444    }
445    return;
446 }