]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/job.c
660d6adb9a8cd6d61f0dc2fb19ec2726f17da0f4
[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[] = "3914 Device \"%s\" not in SD Device resources.\n";
54 static char BAD_use[]   = "3913 Bad use command: %s\n";
55 static char BAD_job[]   = "3915 Bad Job command: %s\n";
56 static char OK_query[]  = "3001 OK query append=%d read=%d num_writers=%d "
57    "num_waiting=%d open=%d use_count=%d labeled=%d "
58    "media_type=%s volume_name=%s";
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, "Job_cmd: %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       Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), jcr->errmsg);
91       set_jcr_job_status(jcr, JS_ErrorTerminated);
92       return false;
93    }
94    /*
95     * Since this job could be rescheduled, we
96     *  check to see if we have it already. If so
97     *  free the old jcr and use the new one.
98     */
99    ojcr = get_jcr_by_full_name(job.c_str());
100    if (ojcr && !ojcr->authenticated) {
101       Dmsg2(100, "Found ojcr=0x%x Job %s\n", (unsigned)(long)ojcr, job.c_str());
102       free_jcr(ojcr);
103    }
104    jcr->JobId = JobId;
105    jcr->VolSessionId = newVolSessionId();
106    jcr->VolSessionTime = VolSessionTime;
107    bstrncpy(jcr->Job, job, sizeof(jcr->Job));
108    unbash_spaces(job_name);
109    jcr->job_name = get_pool_memory(PM_NAME);
110    pm_strcpy(jcr->job_name, job_name);
111    unbash_spaces(client_name);
112    jcr->client_name = get_pool_memory(PM_NAME);
113    pm_strcpy(jcr->client_name, client_name);
114    unbash_spaces(fileset_name);
115    jcr->fileset_name = get_pool_memory(PM_NAME);
116    pm_strcpy(jcr->fileset_name, fileset_name);
117    jcr->JobType = JobType;
118    jcr->JobLevel = level;
119    jcr->no_attributes = no_attributes;
120    jcr->spool_attributes = spool_attributes;
121    jcr->spool_data = spool_data;
122    jcr->write_part_after_job = write_part_after_job;
123    jcr->fileset_md5 = get_pool_memory(PM_NAME);
124    pm_strcpy(jcr->fileset_md5, fileset_md5);
125
126    jcr->authenticated = false;
127
128    /*
129     * Pass back an authorization key for the File daemon
130     */
131    make_session_key(auth_key, NULL, 1);
132    bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
133    Dmsg1(110, ">dird: %s", dir->msg);
134    jcr->sd_auth_key = bstrdup(auth_key);
135    memset(auth_key, 0, sizeof(auth_key));
136    return true;
137 }
138
139 bool use_cmd(JCR *jcr) 
140 {
141    /*
142     * Wait for the device, media, and pool information
143     */
144    if (!use_device_cmd(jcr)) {
145       set_jcr_job_status(jcr, JS_ErrorTerminated);
146       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
147       return false;
148    }
149    return true;
150 }
151
152 bool run_cmd(JCR *jcr)
153 {
154    struct timeval tv;
155    struct timezone tz;
156    struct timespec timeout;
157    int errstat;
158
159    /* The following jobs don't need the FD */
160    switch (jcr->JobType) {
161    case JT_MIGRATION:
162    case JT_COPY:
163    case JT_ARCHIVE:
164       jcr->authenticated = true;
165       run_job(jcr);
166       return false;
167    }
168
169    set_jcr_job_status(jcr, JS_WaitFD);          /* wait for FD to connect */
170    dir_send_job_status(jcr);
171
172    gettimeofday(&tv, &tz);
173    timeout.tv_nsec = tv.tv_usec * 1000;
174    timeout.tv_sec = tv.tv_sec + 30 * 60;        /* wait 30 minutes */
175
176    Dmsg1(100, "%s waiting on FD to contact SD\n", jcr->Job);
177    /*
178     * Wait for the File daemon to contact us to start the Job,
179     *  when he does, we will be released, unless the 30 minutes
180     *  expires.
181     */
182    P(jcr->mutex);
183    for ( ;!job_canceled(jcr); ) {
184       errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
185       if (errstat == 0 || errstat == ETIMEDOUT) {
186          break;
187       }
188    }
189    V(jcr->mutex);
190
191    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
192
193    if (jcr->authenticated && !job_canceled(jcr)) {
194       Dmsg1(100, "Running job %s\n", jcr->Job);
195       run_job(jcr);                   /* Run the job */
196    }
197    return false;
198 }
199
200 /*
201  * After receiving a connection (in job.c) if it is
202  *   from the File daemon, this routine is called.
203  */
204 void handle_filed_connection(BSOCK *fd, char *job_name)
205 {
206    JCR *jcr;
207
208    bmicrosleep(0, 50000);             /* wait 50 millisecs */
209    if (!(jcr=get_jcr_by_full_name(job_name))) {
210       Jmsg1(NULL, M_FATAL, 0, _("Job name not found: %s\n"), job_name);
211       Dmsg1(100, "Job name not found: %s\n", job_name);
212       return;
213    }
214
215    jcr->file_bsock = fd;
216    jcr->file_bsock->jcr = jcr;
217
218    Dmsg1(110, "Found Job %s\n", job_name);
219
220    if (jcr->authenticated) {
221       Jmsg2(jcr, M_FATAL, 0, "Hey!!!! JobId %u Job %s already authenticated.\n",
222          jcr->JobId, jcr->Job);
223       free_jcr(jcr);
224       return;
225    }
226
227    /*
228     * Authenticate the File daemon
229     */
230    if (jcr->authenticated || !authenticate_filed(jcr)) {
231       Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
232       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
233    } else {
234       jcr->authenticated = true;
235       Dmsg1(110, "OK Authentication Job %s\n", jcr->Job);
236    }
237
238    P(jcr->mutex);
239    if (!jcr->authenticated) {
240       set_jcr_job_status(jcr, JS_ErrorTerminated);
241    }
242    pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
243    V(jcr->mutex);
244    free_jcr(jcr);
245    return;
246 }
247
248
249 /*
250  *   Use Device command from Director
251  *   He tells is what Device Name to use, the Media Type,
252  *      the Pool Name, and the Pool Type.
253  *
254  *    Ensure that the device exists and is opened, then store
255  *      the media and pool info in the JCR.
256  */
257 static bool use_device_cmd(JCR *jcr)
258 {
259    POOL_MEM dev_name, media_type, pool_name, pool_type;
260    BSOCK *dir = jcr->dir_bsock;
261    DEVRES *device;
262    int append;
263    bool ok;
264
265    Dmsg1(120, "Use device: %s", dir->msg);
266    /*
267     * If there are multiple devices, the director sends us
268     *   use_device for each device that it wants to use.
269     */
270    ok = sscanf(dir->msg, use_device, dev_name.c_str(), media_type.c_str(),
271                pool_name.c_str(), pool_type.c_str(), &append) == 5;
272    if (ok) {
273       unbash_spaces(dev_name);
274       unbash_spaces(media_type);
275       unbash_spaces(pool_name);
276       unbash_spaces(pool_type);
277       LockRes();
278       foreach_res(device, R_DEVICE) {
279          /* Find resource, and make sure we were able to open it */
280          if (fnmatch(dev_name.c_str(), device->hdr.name, 0) == 0 &&
281              device->dev && strcmp(device->media_type, media_type.c_str()) == 0) {
282             const int name_len = MAX_NAME_LENGTH;
283             DCR *dcr = new_dcr(jcr, device->dev);
284             UnlockRes();
285             Dmsg1(120, "Found device %s\n", device->hdr.name);
286             bstrncpy(dcr->pool_name, pool_name, name_len);
287             bstrncpy(dcr->pool_type, pool_type, name_len);
288             bstrncpy(dcr->media_type, media_type, name_len);
289             bstrncpy(dcr->dev_name, dev_name, name_len);
290             jcr->dcr = dcr;
291             if (!dcr) {
292                bnet_fsend(dir, _("Could not get dcr for device: %s\n"), dev_name.c_str());
293                return false;
294             }
295             if (append == SD_APPEND) {
296                ok = reserve_device_for_append(jcr, device->dev);
297             } else {
298                ok = reserve_device_for_read(jcr, device->dev);
299             }
300             if (!ok) {
301                bnet_fsend(dir, _("Could not get dcr for device: %s\n"), dev_name.c_str());
302                free_dcr(jcr->dcr);
303                return false;
304             }
305             Dmsg1(220, "Got: %s", dir->msg);
306             return bnet_fsend(dir, OK_device);
307          }
308       }
309       UnlockRes();
310       if (verbose) {
311          unbash_spaces(dir->msg);
312          pm_strcpy(jcr->errmsg, dir->msg);
313          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
314       }
315       Jmsg(jcr, M_FATAL, 0, _("\n"
316          "     Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
317            dev_name.c_str(), media_type.c_str());
318       bnet_fsend(dir, NO_device, dev_name.c_str());
319    } else {
320       unbash_spaces(dir->msg);
321       pm_strcpy(jcr->errmsg, dir->msg);
322       if (verbose) {
323          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
324       }
325       Jmsg(jcr, M_FATAL, 0, _("Bad Use Device command: %s\n"), jcr->errmsg);
326       bnet_fsend(dir, BAD_use, jcr->errmsg);
327    }
328
329    return false;                      /* ERROR return */
330 }
331
332 /*
333  *   Query Device command from Director
334  *   Sends Storage Daemon's information on the device to the
335  *    caller (presumably the Director).
336  *   This command always returns "true" so that the line is
337  *    not closed on an error.
338  *
339  */
340 bool query_cmd(JCR *jcr)
341 {
342    POOL_MEM dev_name;
343    BSOCK *dir = jcr->dir_bsock;
344    DEVRES *device;
345    bool ok;
346
347    Dmsg1(120, "Query: %s", dir->msg);
348
349    ok = sscanf(dir->msg, query_device, dev_name.c_str()) == 1;
350    if (ok) {
351       unbash_spaces(dev_name);
352       LockRes();
353       foreach_res(device, R_DEVICE) {
354          /* Find resource, and make sure we were able to open it */
355          if (fnmatch(dev_name.c_str(), device->hdr.name, 0) == 0 &&
356              device->dev) {
357             DEVICE *dev = device->dev;
358             POOL_MEM VolumeName, MediaType;
359             UnlockRes();
360             if (dev->is_labeled()) {
361                pm_strcpy(VolumeName, dev->VolHdr.VolName);
362             } else {
363                pm_strcpy(VolumeName, "");
364             }
365             bash_spaces(VolumeName);
366             pm_strcpy(MediaType, device->media_type);
367             bash_spaces(MediaType);
368             return bnet_fsend(dir, OK_query, dev->can_append()!=0,
369                dev->can_read()!=0, dev->num_writers, dev->num_waiting,
370                dev->is_open()!=0, dev->use_count, dev->is_labeled()!=0,
371                MediaType.c_str(), VolumeName.c_str());
372          }
373       }
374       UnlockRes();
375       unbash_spaces(dir->msg);
376       pm_strcpy(jcr->errmsg, dir->msg);
377       bnet_fsend(dir, NO_device, dev_name.c_str());
378    } else {
379       unbash_spaces(dir->msg);
380       pm_strcpy(jcr->errmsg, dir->msg);
381       bnet_fsend(dir, BAD_query, jcr->errmsg);
382    }
383    return true;
384 }
385
386
387 /*
388  * Destroy the Job Control Record and associated
389  * resources (sockets).
390  */
391 void stored_free_jcr(JCR *jcr)
392 {
393    if (jcr->file_bsock) {
394       bnet_close(jcr->file_bsock);
395       jcr->file_bsock = NULL;
396    }
397    if (jcr->job_name) {
398       free_pool_memory(jcr->job_name);
399    }
400    if (jcr->client_name) {
401       free_memory(jcr->client_name);
402       jcr->client_name = NULL;
403    }
404    if (jcr->fileset_name) {
405       free_memory(jcr->fileset_name);
406    }
407    if (jcr->fileset_md5) {
408       free_memory(jcr->fileset_md5);
409    }
410    if (jcr->bsr) {
411       free_bsr(jcr->bsr);
412       jcr->bsr = NULL;
413    }
414    if (jcr->RestoreBootstrap) {
415       unlink(jcr->RestoreBootstrap);
416       free_pool_memory(jcr->RestoreBootstrap);
417       jcr->RestoreBootstrap = NULL;
418    }
419    if (jcr->next_dev || jcr->prev_dev) {
420       Emsg0(M_FATAL, 0, _("In free_jcr(), but still attached to device!!!!\n"));
421    }
422    pthread_cond_destroy(&jcr->job_start_wait);
423    if (jcr->dcr) {
424       free_dcr(jcr->dcr);
425       jcr->dcr = NULL;
426    }
427    return;
428 }